How to use iMonkey in an iOS app

iMonkey looks like an interesting way of embedding a JS runtime in an iOS app, but I can’t find any examples on how to actually run some JS code.

I can build/link the lib, include the jsapi.h header (from the src directory), but it trips over various linker errors (‘undefined symbol for architecture…’) when I try some example code from spider monkey (see below). To be clear, this is pretty much a copy-paste from a Mac related posting on another site, I am not at all sure if this is how it should be done. I am sure I have the right static library (universal) for my architecture (simulator).

Anyone knows how to do this?

#include "jsapi.h"

.....

JSRuntime *rt;
JSContext *cx;
JSObject  *global;

/* Create a JS runtime. */
rt = JS_NewRuntime(8L * 1024L * 1024L);

/* Create a context. */
cx = JS_NewContext(rt, 8192);
JS_SetOptions(cx, JSOPTION_VAROBJFIX);

/* Create the global object. */
global = JS_NewObject(cx, &global_class, NULL, NULL);

/* Populate the global object with the standard globals,
 like Object and Array. */
if (!JS_InitStandardClasses(cx, global))
    @throw [[NSException alloc]initWithName:@"JSerror" reason:@"jserrpr" userInfo:nil];

/* Cleanup. */
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();

Leave a Comment