#include "lib_handle.h" #include using namespace v8; using std::unique_ptr; namespace ivm { /** * Stateless transferable interface */ auto LibHandle::LibTransferable::TransferIn() -> Local { return ClassHandle::NewInstance(); } /** * ivm.lib API container */ auto LibHandle::Definition() -> Local { return Inherit(MakeClass( "Lib", nullptr, "hrtime", MemberFunction{}, "privateSymbol", MemberFunction{}, "testHang", MemberFunction{}, "testOOM", MemberFunction{} )); } auto LibHandle::TransferOut() -> unique_ptr { return std::make_unique(); } // NOLINTNEXTLINE(readability-convert-member-functions-to-static) auto LibHandle::Hrtime(MaybeLocal maybe_diff) -> Local { Isolate* isolate = Isolate::GetCurrent(); Local context = isolate->GetCurrentContext(); uint64_t time = uv_hrtime(); constexpr auto kNanos = (uint64_t)1e9; Local diff; if (maybe_diff.ToLocal(&diff)) { if (diff->Length() != 2) { throw RuntimeTypeError("hrtime diff must be 2-length array"); } uint64_t time_diff = Unmaybe(diff->Get(context, 0)).As()->Value() * kNanos + Unmaybe(diff->Get(context, 1)).As()->Value(); time -= time_diff; } Local ret = Array::New(isolate, 2); Unmaybe(ret->Set(context, 0, Uint32::New(isolate, (uint32_t)(time / kNanos)))); Unmaybe(ret->Set(context, 1, Uint32::New(isolate, (uint32_t)(time - (time / kNanos) * kNanos)))); return ret; } // NOLINTNEXTLINE(readability-convert-member-functions-to-static) auto LibHandle::PrivateSymbol(MaybeLocal maybe_name) -> Local { Local name{}; if (maybe_name.ToLocal(&name)) { /* nothing */ } auto symbol = Private::New(Isolate::GetCurrent(), name); return *reinterpret_cast*>(&symbol); } // NOLINTNEXTLINE(readability-convert-member-functions-to-static) auto LibHandle::TestHang() -> Local { auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(60); while (std::chrono::steady_clock::now() < deadline) {} return Undefined(Isolate::GetCurrent()); } // NOLINTNEXTLINE(readability-convert-member-functions-to-static) auto LibHandle::TestOOM() -> Local { Isolate* isolate = Isolate::GetCurrent(); for (;;) { Array::New(isolate, 128); } return {}; } } // namespace ivm