Skip to content
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

src: make cross-context MakeCallback() calls work #9221

Merged
merged 1 commit into from
Oct 25, 2016
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
57 changes: 30 additions & 27 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1304,45 +1304,48 @@ Local<Value> MakeCallback(Environment* env,


Local<Value> MakeCallback(Isolate* isolate,
Local<Object> recv,
const char* method,
int argc,
Local<Value> argv[]) {
Local<Object> recv,
const char* method,
int argc,
Local<Value> argv[]) {
EscapableHandleScope handle_scope(isolate);
Local<Context> context = recv->CreationContext();
Environment* env = Environment::GetCurrent(context);
Context::Scope context_scope(context);
Local<String> method_string = OneByteString(isolate, method);
return handle_scope.Escape(
Local<Value>::New(isolate, MakeCallback(env, recv, method, argc, argv)));
MakeCallback(isolate, recv, method_string, argc, argv));
}


Local<Value> MakeCallback(Isolate* isolate,
Local<Object> recv,
Local<String> symbol,
int argc,
Local<Value> argv[]) {
Local<Object> recv,
Local<String> symbol,
int argc,
Local<Value> argv[]) {
EscapableHandleScope handle_scope(isolate);
Local<Context> context = recv->CreationContext();
Environment* env = Environment::GetCurrent(context);
Context::Scope context_scope(context);
return handle_scope.Escape(
Local<Value>::New(isolate, MakeCallback(env, recv, symbol, argc, argv)));
Local<Value> callback_v = recv->Get(symbol);
if (callback_v.IsEmpty()) return Local<Value>();
if (!callback_v->IsFunction()) return Local<Value>();
Local<Function> callback = callback_v.As<Function>();
return handle_scope.Escape(MakeCallback(isolate, recv, callback, argc, argv));
}


Local<Value> MakeCallback(Isolate* isolate,
Local<Object> recv,
Local<Function> callback,
int argc,
Local<Value> argv[]) {
Local<Object> recv,
Local<Function> callback,
int argc,
Local<Value> argv[]) {
// Observe the following two subtleties:
//
// 1. The environment is retrieved from the callback function's context.
// 2. The context to enter is retrieved from the environment.
//
// Because of the AssignToContext() call in src/node_contextify.cc,
// the two contexts need not be the same.
EscapableHandleScope handle_scope(isolate);
Local<Context> context = recv->CreationContext();
Environment* env = Environment::GetCurrent(context);
Context::Scope context_scope(context);
return handle_scope.Escape(Local<Value>::New(
isolate,
MakeCallback(env, recv.As<Value>(), callback, argc, argv)));
Environment* env = Environment::GetCurrent(callback->CreationContext());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback->CreationContext() seems a little more correct than recv->CreationContext() because the former is the context the actual callback takes place in but it's admittedly a mostly academic difference.

Context::Scope context_scope(env->context());
return handle_scope.Escape(
MakeCallback(env, recv.As<Value>(), callback, argc, argv));
}


Expand Down
4 changes: 4 additions & 0 deletions test/addons/make-callback/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const recv = {
assert.strictEqual(42, makeCallback(recv, 'one'));
assert.strictEqual(42, makeCallback(recv, 'two', 1337));

// Check that callbacks on a receiver from a different context works.
const foreignObject = vm.runInNewContext('({ fortytwo() { return 42; } })');
assert.strictEqual(42, makeCallback(foreignObject, 'fortytwo'));

// Check that the callback is made in the context of the receiver.
const target = vm.runInNewContext(`
(function($Object) {
Expand Down