Skip to content

Instantly share code, notes, and snippets.

@zckevin
Last active July 7, 2019 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zckevin/a930cbcbb949dd7694eb0fee1b29dfe3 to your computer and use it in GitHub Desktop.
Save zckevin/a930cbcbb949dd7694eb0fee1b29dfe3 to your computer and use it in GitHub Desktop.
Accessing variables trapped by JavaScript closure using V8 runtime functions.
diff --git a/src/runtime/runtime-debug.cc b/src/runtime/runtime-debug.cc
index 98aa3b98e7..2e424ead9e 100644
--- a/src/runtime/runtime-debug.cc
+++ b/src/runtime/runtime-debug.cc
@@ -322,18 +322,19 @@ RUNTIME_FUNCTION(Runtime_GetGeneratorScopeDetails) {
HandleScope scope(isolate);
DCHECK_EQ(2, args.length());
- if (!args[0]->IsJSGeneratorObject()) {
- return ReadOnlyRoots(isolate).undefined_value();
- }
// Check arguments.
- CONVERT_ARG_HANDLE_CHECKED(JSGeneratorObject, gen, 0);
+ CONVERT_ARG_HANDLE_CHECKED(JSFunction, gen, 0);
CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]);
// Only inspect suspended generator scopes.
- if (!gen->is_suspended()) {
- return ReadOnlyRoots(isolate).undefined_value();
- }
// Find the requested scope.
int n = 0;
// ./d8 --allow-natives-syntax ./test.js
const kScopeDetailsObjectIndex = 1;
function noop() {
return "sf"
}
function out(arg) {
let obj = {
f: noop,
v: 100,
};
return function() {
return arg + obj.v;
}
}
let inner = out(91);
// %DebugPrint(inner);
// %SetGeneratorScopeVariableValue(inner, 0, "arg", 100)
// print(inner())
try {
for (let i = 0; i <= 5; i++) {
// Patch v8 source file `runtime/runtime-debug.cc` function `Runtime_GetGeneratorScopeDetails`
let details = %GetGeneratorScopeDetails(inner, i)
let closure = details[kScopeDetailsObjectIndex];
for (let key in closure) {
inner[`_${key}`] = closure[key]
}
}
} catch (err) {
err = null
}
// prints "sf"
console.log(inner._hehe.f())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment