implementing callbacks like setTimeout

Feb 22, 2012 at 7:30 AM

We are trying to use Jurassic to run our unit tests as part of our build process.

We would like to be able to support some browser functionality like window.setTimeout as objects in our space.

We have it so that it succesfully calls back to a C# object we have written, but we need a way of passing a javascript callback to the setTimer function that we call back when the appropriaate amount of time has changed.

Our current code likes something like this:

        public int setTimeout(ObjectInstance callback, int millisec, string lang = null)
            Timer timer = new Timer(obj => this.Engine.CallGlobalFunction(callback), callback, millisec, Timeout.Infinite);
            return id;
 Only problem is that the callback which is passed in comes in as a string which is the serialized form of the entire function.

Is there a recommended way of accomplishing this?


Feb 22, 2012 at 10:30 AM
Edited Feb 22, 2012 at 10:32 AM

You can take a look at my implementation if you want to. The commented out instances are the ones that invoke strings.

Feb 22, 2012 at 11:37 AM

Instead of using CallGlobalFunction, check if the the callback is of type FunctionInstance - if it is, invoke the Call() method on it.

Feb 22, 2012 at 10:25 PM

That did the trick!  Thanks for the speedy response and really useful library!