1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| using EvalCallback = std::function<void(const v8::Local<v8::Context>&, const v8::Local<v8::Value>&, void*)>;
class NodeInstance { public: uv_loop_t loop_; std::shared_ptr<node::ArrayBufferAllocator> allocator_; v8::Isolate* isolate_; node::IsolateData* isolate_data_; node::Environment* env_; v8::Global<v8::Context> context_; std::mutex mu_;
NodeInstance() noexcept: loop_(), allocator_(nullptr), isolate_(nullptr), isolate_data_(nullptr), env_(nullptr), context_(), mu_() {}
int Eval(const std::string& script, const EvalCallback& callback, void* data, std::string* errout);
void SpinEventLoop();
static NodeInstance* instance; static NodeInstance* Get(); static int Free(); };
NodeInstance* NodeInstance::instance = nullptr;
void NodeInstance::SpinEventLoop() { v8::SealHandleScope seal(isolate_); bool more; do { uv_run(&loop_, UV_RUN_DEFAULT);
platform->DrainTasks(isolate_); more = uv_loop_alive(&loop_); if (more) continue; node::EmitBeforeExit(env_); more = uv_loop_alive(&loop_); } while (more); }
NodeInstance* NodeInstance::Get() { if (instance != nullptr) return instance; std::unique_ptr<NodeInstance> node_instance = std::make_unique<NodeInstance>(); int ret = uv_loop_init(&node_instance->loop_); if (ret != 0) { fprintf(stderr, "%s: Failed to initialize loop: %s\n", args[0].c_str(), uv_err_name(ret)); return nullptr; } node_instance->allocator_ = node::ArrayBufferAllocator::Create();
node_instance->isolate_ = node::NewIsolate(node_instance->allocator_, &node_instance->loop_, platform.get()); if (node_instance->isolate_ == nullptr) { fprintf(stderr, "%s: Failed to initialize V8 Isolate\n", args[0].c_str()); return nullptr; }
v8::Locker locker(node_instance->isolate_); v8::Isolate::Scope isolate_scope(node_instance->isolate_); node_instance->isolate_data_ = node::CreateIsolateData(node_instance->isolate_, &node_instance->loop_, platform.get(), node_instance->allocator_.get());
v8::HandleScope handle_scope(node_instance->isolate_); v8::Local<v8::Context> context = node::NewContext(node_instance->isolate_); node_instance->context_.Reset(node_instance->isolate_, context);
if (context.IsEmpty()) { fprintf(stderr, "%s: Failed to initialize V8 Context\n", args[0].c_str()); return nullptr; }
v8::Context::Scope context_scope(context); node_instance->env_ = node::CreateEnvironment(node_instance->isolate_data_, context, args, exec_args); node::AddLinkedBinding(node_instance->env_, "android", init, nullptr);
v8::TryCatch trycatch(node_instance->isolate_); v8::MaybeLocal<v8::Value> loadenv_ret = node::LoadEnvironment(node_instance->env_, "(function () {" "const androidLogd = process._linkedBinding('android').androidLogd;" "const log = function (...args) { androidLogd(require('util').format(...args)) };" "console.log = log;" "console.info = log;" "console.debug = log;" "console.warn = log;" "console.error = log;" "})();" "globalThis.require = require('module').createRequire(process.cwd() + '/');");
if (loadenv_ret.IsEmpty()) { if (trycatch.HasCaught()) { v8::String::Utf8Value err(node_instance->isolate_, trycatch.Exception()); const char* errmsg = *err; fprintf(stderr, "%s\n", errmsg); } return nullptr; } node_instance->SpinEventLoop(); instance = node_instance.release(); return instance; }
int NodeInstance::Free() { if (!instance) return 0; NodeInstance *node_instance = instance; v8::Isolate* isolate = node_instance->isolate_; bool platform_finished = false; int exit_code;
{ v8::Locker locker(isolate); v8::Isolate::Scope isolate_scope(isolate); exit_code = node::EmitExit(node_instance->env_); node::Stop(node_instance->env_); node::FreeEnvironment(node_instance->env_);
platform->AddIsolateFinishedCallback(isolate, [](void *data) { *static_cast<bool *>(data) = true; }, &platform_finished); platform->UnregisterIsolate(isolate); node_instance->context_.Reset(); node::FreeIsolateData(node_instance->isolate_data_); }
isolate->Dispose(); node_instance->allocator_.reset(); while (!platform_finished) uv_run(&node_instance->loop_, UV_RUN_ONCE); int err = uv_loop_close(&node_instance->loop_); assert(err == 0); instance = nullptr; return exit_code; }
int NodeInstance::Eval(const std::string& script, const EvalCallback& callback, void* data, std::string* errout) { const std::lock_guard<std::mutex> lock(mu_); v8::Locker locker(isolate_); v8::Isolate::Scope isolate_scope(isolate_); v8::HandleScope handle_scope(isolate_); v8::Local<v8::String> runScript = v8::String::NewFromUtf8(isolate_, script.c_str()).ToLocalChecked(); auto context = context_.Get(isolate_); v8::Context::Scope context_scope(context); auto maybe_script = v8::Script::Compile(context, runScript); if (maybe_script.IsEmpty()) { return 1; }
v8::TryCatch trycatch(isolate_); auto script_result = maybe_script.ToLocalChecked()->Run(context); if (script_result.IsEmpty()) { if (trycatch.HasCaught()) { v8::String::Utf8Value err(isolate_, trycatch.Exception()); if (errout) *errout = ToCString(err); } return 1; }
if (callback) { callback(context, script_result.ToLocalChecked(), data); } SpinEventLoop(); return 0; }
|