jurassic
jurassic copied to clipboard
Inspect variables bound in closure
Is there any way to list or otherwise access variables bound in the current functional scope when javascript calls back to C#?
This would be beneficial for people wanting to build an interactive debugger for js running on jurassic. At present we are limited to showing global variables. It would be nice if there was some way to accomplish this for scoped variables as well.
I recently rewrote how scoped variables work. If you have a reference to the current RuntimeScope you can just call runtimeScope.GetValue(variableName)
. Unfortunately there can be many scope instances in a single method and there's no way to know which scope is the correct one at runtime (the compiler keeps track of this during compilation).
Sounds promising - iterating through a number of scopes might work fine, if it's possible to get a list of them.
Last we checked, we were unfortunately unable to upgrade our Jurassic 2.2.2 since we evaluate lodash.js into the js-environment, and a regression a while back made that fail. That issue is still open, so we're probably still stuck.
@lsim I did a fix for the lodash symbol issue, it's in version 3.2.4.
Great! We'll have another go then. Closure variable access will make a nice improvement to our debugger.
I recently rewrote how scoped variables work. If you have a reference to the current RuntimeScope you can just call
runtimeScope.GetValue(variableName)
. Unfortunately there can be many scope instances in a single method and there's no way to know which scope is the correct one at runtime (the compiler keeps track of this during compilation).
I seem to have successfully upgraded to 2.3.6. 🎉
I'm now trying to figure out how to get a reference to the current RuntimeScope in a [JsFunction]
-annotated C# method invoked from js-country. I had a look at references to RuntimeScope
in the jurassic sources but I didn't spot any actionable clues.
Is there an example somewhere I could look at?
I seem to have successfully upgraded to 2.3.6. 🎉
You mean 3.2.6? Great 👍
I'm now trying to figure out how to get a reference to the current RuntimeScope in a [JsFunction]-annotated C# method invoked from js-country.
The short answer: you can't. What do you need this for, exactly? The RuntimeScope class is used to implement variable scopes in nested functions and with statements; it should not be relevant inside your C# method...?
Oops - yes: 3.2.6
We have in our platform various features aimed at making debugging user scripts easier. It currently allows inspecting root scope variables only. With access to the current RuntimeScope, we might be able to allow interaction with variables in other scopes also.
That makes sense. Unfortunately I haven't made this easy. There's a few options but none of them are particularly nice:
- Build your own private copy of Jurassic. Then you can change it how you like (at the expense of making upgrading harder).
-
UserDefinedFunction
has aninternal RuntimeScope ParentScope
property, which you could access via reflection. AUserDefinedFunction
instance is created whenever you define a function in JS -- so you could define it in the global scope usingExecute()
and then get a reference to it usingGetGlobalVariable()
. -
ArgumentsInstance
has aprivate RuntimeScope bindings
which is also accessible via reflection. You can get aArgumentsInstance
instance by passing in "arguments" to your function.
Yeah, not great, sorry :-/