Skip to content

Commit c342bda

Browse files
bnoordhuisevanlucas
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 <anna@addaleax.net>
1 parent 85a9295 commit c342bda

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
@@ -1304,45 +1304,48 @@ Local<Value> MakeCallback(Environment* env,
13041304

13051305

13061306
Local<Value> MakeCallback(Isolate* isolate,
1307-
Local<Object> recv,
1308-
const char* method,
1309-
int argc,
1310-
Local<Value> argv[]) {
1307+
Local<Object> recv,
1308+
const char* method,
1309+
int argc,
1310+
Local<Value> argv[]) {
13111311
EscapableHandleScope handle_scope(isolate);
1312-
Local<Context> context = recv->CreationContext();
1313-
Environment* env = Environment::GetCurrent(context);
1314-
Context::Scope context_scope(context);
1312+
Local<String> method_string = OneByteString(isolate, method);
13151313
return handle_scope.Escape(
1316-
Local<Value>::New(isolate, MakeCallback(env, recv, method, argc, argv)));
1314+
MakeCallback(isolate, recv, method_string, argc, argv));
13171315
}
13181316

13191317

13201318
Local<Value> MakeCallback(Isolate* isolate,
1321-
Local<Object> recv,
1322-
Local<String> symbol,
1323-
int argc,
1324-
Local<Value> argv[]) {
1319+
Local<Object> recv,
1320+
Local<String> symbol,
1321+
int argc,
1322+
Local<Value> argv[]) {
13251323
EscapableHandleScope handle_scope(isolate);
1326-
Local<Context> context = recv->CreationContext();
1327-
Environment* env = Environment::GetCurrent(context);
1328-
Context::Scope context_scope(context);
1329-
return handle_scope.Escape(
1330-
Local<Value>::New(isolate, MakeCallback(env, recv, symbol, argc, argv)));
1324+
Local<Value> callback_v = recv->Get(symbol);
1325+
if (callback_v.IsEmpty()) return Local<Value>();
1326+
if (!callback_v->IsFunction()) return Local<Value>();
1327+
Local<Function> callback = callback_v.As<Function>();
1328+
return handle_scope.Escape(MakeCallback(isolate, recv, callback, argc, argv));
13311329
}
13321330

13331331

13341332
Local<Value> MakeCallback(Isolate* isolate,
1335-
Local<Object> recv,
1336-
Local<Function> callback,
1337-
int argc,
1338-
Local<Value> argv[]) {
1333+
Local<Object> recv,
1334+
Local<Function> callback,
1335+
int argc,
1336+
Local<Value> argv[]) {
1337+
// Observe the following two subtleties:
1338+
//
1339+
// 1. The environment is retrieved from the callback function's context.
1340+
// 2. The context to enter is retrieved from the environment.
1341+
//
1342+
// Because of the AssignToContext() call in src/node_contextify.cc,
1343+
// the two contexts need not be the same.
13391344
EscapableHandleScope handle_scope(isolate);
1340-
Local<Context> context = recv->CreationContext();
1341-
Environment* env = Environment::GetCurrent(context);
1342-
Context::Scope context_scope(context);
1343-
return handle_scope.Escape(Local<Value>::New(
1344-
isolate,
1345-
MakeCallback(env, recv.As<Value>(), callback, argc, argv)));
1345+
Environment* env = Environment::GetCurrent(callback->CreationContext());
1346+
Context::Scope context_scope(env->context());
1347+
return handle_scope.Escape(
1348+
MakeCallback(env, recv.As<Value>(), callback, argc, argv));
13461349
}
13471350

13481351

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)