How to access local variables of calling function frame/context/closure?

70 views
Skip to first unread message

Jonas

unread,
Oct 22, 2012, 3:09:31 PM10/22/12
to v8-u...@googlegroups.com
Hello,

I'm modifying V8 for my master thesis and for this need to access variables from the frame/context/closure(?) from which a function is called.
In my scenario I have calls to a C++ method from JS like this:

// a global variable
var global
= "global_value";

function foo
(){
    var global
= "local_value";
    myCppFunction
("global", global);
}

foo
();


Now I would like to know how in the implementation of myCppFunction I could access the function local variable "global" (NOT value, this would be given by the 2nd parameter) from 'foo'?
Handle<Value> MyCppFunction(const Arguments& args){
    Local<String> varName = args[0]->ToString();
    Local<String> varValue = args[1]->ToString(); // this holds "local_value"
    // how to access the variable "global" in the scope of 'foo' using varName?
}

I tried to play around with StackFrameLocator as I thought the Frame for the function from which my C++ function is called would have to be the previous on the stack. But I didn't manage to find the variable.
Can someone give me a hint how I could access the desired variable? Is there a way via internal::Isolate::Current()?

Best regards
Jonas

Yang Guo

unread,
Oct 29, 2012, 5:02:12 AM10/29/12
to v8-u...@googlegroups.com
What you describe sounds similar to what the built-in debugger does. Check out test/mjsunit/debug-evaluate-locals.js

Yang
Reply all
Reply to author
Forward
0 new messages