Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all variables (and functions names) from the global scope in Jint?

I'm using Jint and I wonder if there is a way from the C# perspective to get all defined variables and functions available in the global scope of the script.

For example, given the following Javascript code in the file "test.js":

function globalFunc(a, b) {
    var localVar = a + b;
    return localVar;
}

var globalVar = 5;

... and given the following C# code (using Jint 2.4.0):

Jint.Engine engine = new Jint.Engine();
engine.Execute(System.IO.File.ReadAllText("test.js");

// the following method call do not exist in Jint, just here for example...
JsValue[] variables = engine.GetGlobalScopeVariables();

then expecting variables to contains two values, namely a variable named "globalFunc" of type "Function" and a variable named "globalVar" of type "Number".

Expected answers may lead to some Jint hacking. I'm open to that as well.

Thanks!

like image 872
formixian Avatar asked Oct 12 '25 14:10

formixian


1 Answers

The variables and functions are accessible in engine.Global.Properties. It does however also hold 'system variables'.

By comparing the entries in the dictionary after you executed your script to the entries before you executed it you can get information about which variables and functions that was added.

like image 199
fbjornlund Avatar answered Oct 14 '25 04:10

fbjornlund



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!