1

I'm using JINT to load a javascript file into a small C# app and call a number of methods with some parameters from C#. I want to get the return values of those methods. This is what I'm doing:

        JintEngine engine = new JintEngine();
        string file = File.ReadAllText(@"C:\thejavascript.js");
        engine.Run(file);            
        Console.WriteLine(engine.CallFunction("themethod")); 

The javascript method is as follows:

        function themethod() {   
            console.log("Hello!");
                return "Finished";
        }

I can see 'Finished' in the VisualStudio console when I run the code above, but the call to send 'Hello!' to the AS3 console is nowhere to be seen. It's not in the flashlog.txt file even. In fact, I think JINT throws an error, as the may not be a 'console' object in existance since there's no browser.

How can I trap what's sent to the console from javascript that's run in JINT? There is an example on the JINT download site that had SystemConsole' or something (not System.Console), but I have not been successfu in getting this to work. I want to be able to add debugging code to the javascript and see it in the console in VisualStudio.

Any ideas? This is making debugging what I'm doing very difficult. . .

Thanks in advance.

1 Answers1

2

In C#:

        JintEngine engine = new JintEngine();
        engine.SetFunction("__log", new Action<object>(Console.WriteLine));
        var billy = engine.CallFunction("testMethod"); 

In Javascript:

var customConsole= {};

customConsole.__log = function(args){ var toCall = '';
for(var i = 0, l = arguments.length; i <l; i++){ toCall += arguments[i] + ' '; }
__log(toCall);};

function testMethod() {         
    customConsole.__log("Log this. . .");
}
  • There isn't a function engine.SetFunction() in the current version -- https://jint.codeplex.com/releases/view/71783 this is the version it is in. The others are on the right. – Cullub Aug 15 '14 at 21:18