2020-02-28 10:54:13 +08:00
|
|
|
|
#include "node.h"
|
|
|
|
|
#include "uv.h"
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
|
|
// Note: This file is being referred to from doc/api/embedding.md, and excerpts
|
|
|
|
|
// from it are included in the documentation. Try to keep these in sync.
|
|
|
|
|
|
2020-10-11 21:13:19 +08:00
|
|
|
|
using node::CommonEnvironmentSetup;
|
2020-02-28 10:54:13 +08:00
|
|
|
|
using node::Environment;
|
|
|
|
|
using node::MultiIsolatePlatform;
|
|
|
|
|
using v8::Context;
|
|
|
|
|
using v8::HandleScope;
|
|
|
|
|
using v8::Isolate;
|
|
|
|
|
using v8::Locker;
|
|
|
|
|
using v8::MaybeLocal;
|
|
|
|
|
using v8::V8;
|
2020-07-31 23:58:53 +08:00
|
|
|
|
using v8::Value;
|
2020-02-28 10:54:13 +08:00
|
|
|
|
|
|
|
|
|
static int RunNodeInstance(MultiIsolatePlatform* platform,
|
|
|
|
|
const std::vector<std::string>& args,
|
|
|
|
|
const std::vector<std::string>& exec_args);
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2020-08-13 02:35:40 +08:00
|
|
|
|
argv = uv_setup_args(argc, argv);
|
2020-02-28 10:54:13 +08:00
|
|
|
|
std::vector<std::string> args(argv, argv + argc);
|
|
|
|
|
std::vector<std::string> exec_args;
|
|
|
|
|
std::vector<std::string> errors;
|
|
|
|
|
int exit_code = node::InitializeNodeWithArgs(&args, &exec_args, &errors);
|
|
|
|
|
for (const std::string& error : errors)
|
|
|
|
|
fprintf(stderr, "%s: %s\n", args[0].c_str(), error.c_str());
|
|
|
|
|
if (exit_code != 0) {
|
|
|
|
|
return exit_code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<MultiIsolatePlatform> platform =
|
|
|
|
|
MultiIsolatePlatform::Create(4);
|
|
|
|
|
V8::InitializePlatform(platform.get());
|
|
|
|
|
V8::Initialize();
|
|
|
|
|
|
|
|
|
|
int ret = RunNodeInstance(platform.get(), args, exec_args);
|
|
|
|
|
|
|
|
|
|
V8::Dispose();
|
|
|
|
|
V8::ShutdownPlatform();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int RunNodeInstance(MultiIsolatePlatform* platform,
|
|
|
|
|
const std::vector<std::string>& args,
|
|
|
|
|
const std::vector<std::string>& exec_args) {
|
|
|
|
|
int exit_code = 0;
|
|
|
|
|
|
2020-10-11 21:13:19 +08:00
|
|
|
|
std::vector<std::string> errors;
|
|
|
|
|
std::unique_ptr<CommonEnvironmentSetup> setup =
|
|
|
|
|
CommonEnvironmentSetup::Create(platform, &errors, args, exec_args);
|
|
|
|
|
if (!setup) {
|
|
|
|
|
for (const std::string& err : errors)
|
|
|
|
|
fprintf(stderr, "%s: %s\n", args[0].c_str(), err.c_str());
|
2020-02-28 10:54:13 +08:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-11 21:13:19 +08:00
|
|
|
|
Isolate* isolate = setup->isolate();
|
|
|
|
|
Environment* env = setup->env();
|
|
|
|
|
|
2020-02-28 10:54:13 +08:00
|
|
|
|
{
|
|
|
|
|
Locker locker(isolate);
|
|
|
|
|
Isolate::Scope isolate_scope(isolate);
|
|
|
|
|
HandleScope handle_scope(isolate);
|
2020-10-11 21:13:19 +08:00
|
|
|
|
Context::Scope context_scope(setup->context());
|
2020-02-28 10:54:13 +08:00
|
|
|
|
|
|
|
|
|
MaybeLocal<Value> loadenv_ret = node::LoadEnvironment(
|
2020-10-11 21:13:19 +08:00
|
|
|
|
env,
|
2020-02-28 10:54:13 +08:00
|
|
|
|
"const publicRequire ="
|
|
|
|
|
" require('module').createRequire(process.cwd() + '/');"
|
|
|
|
|
"globalThis.require = publicRequire;"
|
2020-06-19 23:20:53 +08:00
|
|
|
|
"globalThis.embedVars = { nön_ascıı: '🏳️🌈' };"
|
2020-02-28 10:54:13 +08:00
|
|
|
|
"require('vm').runInThisContext(process.argv[1]);");
|
|
|
|
|
|
|
|
|
|
if (loadenv_ret.IsEmpty()) // There has been a JS exception.
|
|
|
|
|
return 1;
|
|
|
|
|
|
2020-10-11 21:13:19 +08:00
|
|
|
|
exit_code = node::SpinEventLoop(env).FromMaybe(1);
|
2020-10-04 05:29:41 +08:00
|
|
|
|
|
2020-10-11 21:13:19 +08:00
|
|
|
|
node::Stop(env);
|
2020-02-28 10:54:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return exit_code;
|
|
|
|
|
}
|