user7489391 user7489391 - 1 month ago 7
C# Question

How to find my javascript function in Jint in C#?

I wrote code below

static void Main(string[] args)
{
var engine = new Engine();
var file = File.ReadAllText(@"...\Desktop\beautify.js");
var final = file + Environment.NewLine + "(function () {return js_beautify('var a = 2;');})();";
var exec = engine.Execute(final);
var r = exec.GetCompletionValue().ToString();
Console.WriteLine(r);
Console.Read();

}


but the Jint show error

Jint.Runtime.JavaScriptException: 'js_beautify is not defined'


Can anyone help me how to register external js libraries like beautify.js and then call functions of them in Jint ?

Answer Source

It should be

 var engine = new Engine().Execute("var global = {};").Execute(content);
 var js_beautify = engine.Execute("global.js_beautify").GetCompletionValue();
 var result = engine.Invoke(js_beautify, "var a = 2;").AsString();