Skip to content

Commit 7aa5a99

Browse files
bnoordhuisMylesBorins
authored andcommitted
src: make cross-context MakeCallback() calls work
Check that invoking a callback on a receiver from a different context works. It ran afoul of an `env->context() == isolate->GetCurrentContext()` assertion so retrieve the environment from the callback context and the context to enter from the environment's context() method. We could also have retrieved the environment from the receiver's context and that would have made little practical difference. It just seemed more correct to get it from the callback context because that is the actual execution context. PR-URL: #9221 Reviewed-By: Anna Henningsen <[email protected]>
1 parent b1dc2d4 commit 7aa5a99

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

src/node.cc

+30-27
Original file line numberDiff line numberDiff line change
@@ -1378,45 +1378,48 @@ Local<Value> MakeCallback(Environment* env,
13781378

13791379

13801380
Local<Value> MakeCallback(Isolate* isolate,
1381-
Local<Object> recv,
1382-
const char* method,
1383-
int argc,
1384-
Local<Value> argv[]) {
1381+
Local<Object> recv,
1382+
const char* method,
1383+
int argc,
1384+
Local<Value> argv[]) {
13851385
EscapableHandleScope handle_scope(isolate);
1386-
Local<Context> context = recv->CreationContext();
1387-
Environment* env = Environment::GetCurrent(context);
1388-
Context::Scope context_scope(context);
1386+
Local<String> method_string = OneByteString(isolate, method);
13891387
return handle_scope.Escape(
1390-
Local<Value>::New(isolate, MakeCallback(env, recv, method, argc, argv)));
1388+
MakeCallback(isolate, recv, method_string, argc, argv));
13911389
}
13921390

13931391

13941392
Local<Value> MakeCallback(Isolate* isolate,
1395-
Local<Object> recv,
1396-
Local<String> symbol,
1397-
int argc,
1398-
Local<Value> argv[]) {
1393+
Local<Object> recv,
1394+
Local<String> symbol,
1395+
int argc,
1396+
Local<Value> argv[]) {
13991397
EscapableHandleScope handle_scope(isolate);
1400-
Local<Context> context = recv->CreationContext();
1401-
Environment* env = Environment::GetCurrent(context);
1402-
Context::Scope context_scope(context);
1403-
return handle_scope.Escape(
1404-
Local<Value>::New(isolate, MakeCallback(env, recv, symbol, argc, argv)));
1398+
Local<Value> callback_v = recv->Get(symbol);
1399+
if (callback_v.IsEmpty()) return Local<Value>();
1400+
if (!callback_v->IsFunction()) return Local<Value>();
1401+
Local<Function> callback = callback_v.As<Function>();
1402+
return handle_scope.Escape(MakeCallback(isolate, recv, callback, argc, argv));
14051403
}
14061404

14071405

14081406
Local<Value> MakeCallback(Isolate* isolate,
1409-
Local<Object> recv,
1410-
Local<Function> callback,
1411-
int argc,
1412-
Local<Value> argv[]) {
1407+
Local<Object> recv,
1408+
Local<Function> callback,
1409+
int argc,
1410+
Local<Value> argv[]) {
1411+
// Observe the following two subtleties:
1412+
//
1413+
// 1. The environment is retrieved from the callback function's context.
1414+
// 2. The context to enter is retrieved from the environment.
1415+
//
1416+
// Because of the AssignToContext() call in src/node_contextify.cc,
1417+
// the two contexts need not be the same.
14131418
EscapableHandleScope handle_scope(isolate);
1414-
Local<Context> context = recv->CreationContext();
1415-
Environment* env = Environment::GetCurrent(context);
1416-
Context::Scope context_scope(context);
1417-
return handle_scope.Escape(Local<Value>::New(
1418-
isolate,
1419-
MakeCallback(env, recv.As<Value>(), callback, argc, argv)));
1419+
Environment* env = Environment::GetCurrent(callback->CreationContext());
1420+
Context::Scope context_scope(env->context());
1421+
return handle_scope.Escape(
1422+
MakeCallback(env, recv.As<Value>(), callback, argc, argv));
14201423
}
14211424

14221425

test/addons/make-callback/test.js

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const recv = {
3636
assert.strictEqual(42, makeCallback(recv, 'one'));
3737
assert.strictEqual(42, makeCallback(recv, 'two', 1337));
3838

39+
// Check that callbacks on a receiver from a different context works.
40+
const foreignObject = vm.runInNewContext('({ fortytwo() { return 42; } })');
41+
assert.strictEqual(42, makeCallback(foreignObject, 'fortytwo'));
42+
3943
// Check that the callback is made in the context of the receiver.
4044
const target = vm.runInNewContext(`
4145
(function($Object) {

0 commit comments

Comments
 (0)