I am trying to get the surrounding js code of the current scope as text. It’s meant to be embedded in a live editor afterwards. Is that possible? In the worst case, it can go through Rhino.
thanks a lot
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In general, no. JavaScript does not provide a reliable way to introspect over variable definitions or the call stack.
arguments.calleeandarguments.callerdo provide introspection into the set of functions on the call stack as long as there are no recursive calls, so you can extract source code by walking that. But it can be defeated byWhen
fis called,gwill be unable to determine that it was called byfby looking atarguments.caller.They don’t allow introspection over the set of defined symbols, e.g. those introduced via
withorcatchso any attempt to enumerate the available local variables at the...inby examining the call stack and function source code will miss some defined symbols.
The call stack is also different from the stack of closures. For example, in
evalruns in a context that has more symbols available than the call stack would suggest sincecounteris not on the call stack at the pointevalis called.Rhino allows access to the current scope as a
Scriptablewhich exposes thegetIdsmethod which can be used to enumerate the names in the current stack frame, and you can walk up to higher stack frames viagetPrototype.