I will assume you will be beginning with a new console project and have referenced the netMonkey assembly.

using SpiderMonkey;

public class Program
{
    public static void Main()
    {
    }
}

Creating the runtime and context

Runtime runtime = new Runtime(1024 * 1024 * 8);
Context context = new Context(runtime);
context.SetOptions(Option.VarObjFix | Option.Jit | Option.MethodJit);
context.SetVersion(Version.Latest);
context.SetErrorReporter(ReportError);

This code seems simple enough, the first line creates the runtime and sets the memory is is allowed to use to 8MB.
The second line shouldn't need explaining, but the third line is slightly more complicated. It tells the context to Jit javascript code. More detailed explanations are on the Mozilla website.
The fourth line tells the context to use the latest version of javascript, you can change this to any value that SpiderMonkey supports.
The fifth line tells the context the function to call when errors appear, the implementation will follow shortly.

By now you may be wondering what Runtimes and Contexts are.

A Runtime, is the space in which the JavaScript variables, objects, scripts, and contexts used by your application are allocated.
Every Context and every object in an application lives within a Runtime.
They cannot travel to other runtimes or be shared across runtimes. Most applications only need one runtime.

A Context, is like a little machine that can do many things involving JavaScript code and objects.
It can compile and execute scripts, get and set object properties, call JavaScript functions, convert JavaScript data from one type to another, create objects, and so on.

The Error Reporter

public static void ReportError(Context context, string message, ErrorReport report)
{
    Console.WriteLine("{0}:{1}:{2}", report.FileName ?? "<no filename>", report.LineNumber, message);
}

Pretty basic, this is how SpiderMonkey tells you about javascript syntax/runtime errors. You would typically output this to a console/file/message box.

The Global Object

private static Class globalClass = new Class("global", ClassFlags.GlobalFlags);

This is the class definition, with the default values for a global class.

JSObject global = JSObject.NewGlobalObject(context, globalClass);
context.InitStandardClasses(global);

This creates a JSObject out of the class definition that we gave it.
The second line of code creates all the standard classes that javascript has, such as Object, Array, Number, Math, etc.

Actually running some javascript!

JSVal val;
bool ok = context.EvaluateScript(global, "'Hello' + ' World!'", null, 1, out val);
if (ok) Console.WriteLine(val);

Pretty simply, a JSVal is a javascript value. It can be null, undefined, a double, a number, an int, a string, a boolean or an object.
The second, slightly complicated line of code will take that global object we created before, the code and some other unimportant stuff and output a boolean and a variable.
The expression 'Hello' + ' World!' that we pass into the function is the javascript, the result of this gets put into the val variable.
The null is the filename, since we are not reading this from a file it is fine to set it to null.
The 1 is the line number, we only have one line.

If the script that we give it has errors in it, it will return false and call the Error Reporter function we gave it.
If it doesn't have an error it will print out the value that is stored in the JSVal variable.

Cleaning up

context.Dispose();
runtime.Dispose();
Runtime.Shutdown();

Simple. It frees the memory that it all uses. If you don't call this then the evil memory leak demons will come to your house and eat you.

Last edited May 17, 2011 at 8:36 PM by Programmdude, version 1

Comments

No comments yet.