Skip to content

Add environment to all library methods #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 73 additions & 19 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4749,19 +4749,28 @@ int Deinitialize() {
}

v8::MaybeLocal<v8::Value> Run(const std::string& path) {
return Run(_environment, path);
}

v8::MaybeLocal<v8::Value> Run(Environment* env, const std::string& path) {
// TODO(cmfcmf) Read entire file into string.
// There is most certainly a better way
// https://stackoverflow.com/a/2602258/2560557
std::ifstream t(path);
std::stringstream buffer;
buffer << t.rdbuf();

return Evaluate(buffer.str());
return Evaluate(_environment, buffer.str());
}

v8::MaybeLocal<v8::Value> Evaluate(const std::string& js_code) {
EscapableHandleScope scope(_environment->isolate());
TryCatch try_catch(_environment->isolate());
return Evaluate(_environment, js_code);
}

v8::MaybeLocal<v8::Value> Evaluate(Environment* env,
const std::string& js_code) {
EscapableHandleScope scope(env->isolate());
TryCatch try_catch(env->isolate());

// try_catch must be nonverbose to disable FatalException() handler,
// we will handle exceptions ourself.
Expand All @@ -4771,12 +4780,12 @@ v8::MaybeLocal<v8::Value> Evaluate(const std::string& js_code) {
// This is used for debugging
// ScriptOrigin origin(filename);
MaybeLocal<v8::Script> script = v8::Script::Compile(
_environment->context(),
v8::String::NewFromUtf8(node_isolate, js_code.c_str())
env->context(),
v8::String::NewFromUtf8(env->isolate(), js_code.c_str())
/*removed param: origin*/);

if (script.IsEmpty()) {
ReportException(_environment, try_catch);
ReportException(env, try_catch);
return MaybeLocal<v8::Value>();
}

Expand Down Expand Up @@ -4805,16 +4814,23 @@ void RunEventLoop(const std::function<void()>& callback,
}

v8::MaybeLocal<v8::Object> GetRootObject() {
if (context.IsEmpty()) {
return MaybeLocal<v8::Object>();
}
return context->Global();
return GetRootObject(_environment);
}

v8::MaybeLocal<v8::Object> GetRootObject(Environment* env) {
return env->context()->Global();
}

v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> receiver,
v8::Local<v8::Function> function,
const std::vector<v8::Local<v8::Value>>& args) {
return Call(_environment, receiver, function, args);
}

v8::MaybeLocal<v8::Value> Call(Environment* env,
v8::Local<v8::Object> receiver,
v8::Local<v8::Function> function,
const std::vector<v8::Local<v8::Value>>& args) {
return function->Call(receiver,
args.size(),
const_cast<v8::Local<v8::Value>*>(&args[0]));
Expand All @@ -4823,8 +4839,15 @@ v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> receiver,
v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> object,
const std::string& function_name,
const std::vector<v8::Local<v8::Value>>& args) {
return Call(_environment, object, function_name, args);
}

v8::MaybeLocal<v8::Value> Call(Environment* env,
v8::Local<v8::Object> object,
const std::string& function_name,
const std::vector<v8::Local<v8::Value>>& args) {
MaybeLocal<v8::String> maybe_function_name =
v8::String::NewFromUtf8(node_isolate, function_name.c_str());
v8::String::NewFromUtf8(env->isolate(), function_name.c_str());

Local<v8::String> v8_function_name;

Expand All @@ -4844,12 +4867,17 @@ v8::MaybeLocal<v8::Value> Call(v8::Local<v8::Object> object,
return MaybeLocal<v8::Value>();
}

return Call(object, v8::Local<v8::Function>::Cast(value), args);
return Call(env, object, v8::Local<v8::Function>::Cast(value), args);
}

v8::MaybeLocal<v8::Object> IncludeModule(const std::string& name) {
return IncludeModule(_environment, name);
}

v8::MaybeLocal<v8::Object> IncludeModule(Environment* env,
const std::string& name) {
MaybeLocal<v8::String> maybe_arg =
v8::String::NewFromUtf8(node_isolate, name.c_str());
v8::String::NewFromUtf8(env->isolate(), name.c_str());

Local<v8::String> arg;

Expand All @@ -4860,14 +4888,14 @@ v8::MaybeLocal<v8::Object> IncludeModule(const std::string& name) {

Local<v8::Object> root_object;

if (!GetRootObject().ToLocal(&root_object)) {
if (!GetRootObject(env).ToLocal(&root_object)) {
// cannot get root object
return MaybeLocal<v8::Object>();
}

std::vector<Local<v8::Value>> args = { arg };

MaybeLocal<v8::Value> maybe_module = Call(root_object, "require", args);
MaybeLocal<v8::Value> maybe_module = Call(env, root_object, "require", args);
Local<v8::Value> module;

if (!maybe_module.ToLocal(&module)) {
Expand All @@ -4880,8 +4908,14 @@ v8::MaybeLocal<v8::Object> IncludeModule(const std::string& name) {

v8::MaybeLocal<v8::Value> GetValue(v8::Local<v8::Object> object,
const std::string& value_name) {
return GetValue(_environment, object, value_name);
}

v8::MaybeLocal<v8::Value> GetValue(Environment* env,
v8::Local<v8::Object> object,
const std::string& value_name) {
MaybeLocal<v8::String> maybe_key =
v8::String::NewFromUtf8(node_isolate, value_name.c_str());
v8::String::NewFromUtf8(env->isolate(), value_name.c_str());

Local<v8::String> key;

Expand All @@ -4897,6 +4931,14 @@ void RegisterModule(const std::string& name,
const addon_context_register_func& callback,
void* priv,
const std::string& target) {
RegisterModule(_environment, name, callback, priv, target);
}

void RegisterModule(Environment* env,
const std::string& name,
const addon_context_register_func& callback,
void* priv,
const std::string& target) {
node::node_module* module = new node::node_module();

module->nm_version = NODE_MODULE_VERSION;
Expand All @@ -4909,18 +4951,30 @@ void RegisterModule(const std::string& name,
node_module_register(module);

if (target != "") {
Evaluate("const " + target + " = process.binding('" + name + "')");
Evaluate(env, "const " + target + " = process.binding('" + name + "')");
}
}

void RegisterModule(const std::string& name,
const std::map<std::string,
v8::FunctionCallback>& module_functions,
v8::FunctionCallback>& module_functions,
const std::string& target) {
RegisterModule(_environment,
name,
module_functions,
target);
}

void RegisterModule(Environment* env,
const std::string& name,
const std::map<std::string,
v8::FunctionCallback>& module_functions,
const std::string& target) {
auto map_on_heap = new const std::map<std::string,
v8::FunctionCallback>(module_functions);

RegisterModule(name,
RegisterModule(env,
name,
node::_RegisterModuleCallback,
const_cast<std::map<std::string,
v8::FunctionCallback>*>(map_on_heap),
Expand Down
Loading