Skip to content

Instantly share code, notes, and snippets.

@siscia
Created June 6, 2015 13:42
Show Gist options
  • Select an option

  • Save siscia/ad4a9ba4a03be3666b3f to your computer and use it in GitHub Desktop.

Select an option

Save siscia/ad4a9ba4a03be3666b3f to your computer and use it in GitHub Desktop.
Try to embed MonkeyScript
#include "jsapi.h"
#include <stdio.h>
using namespace JS;
// The class of the global object.
static JSClass globalClass = {
"global",
JSCLASS_GLOBAL_FLAGS,
JS_PropertyStub,
JS_DeletePropertyStub,
JS_PropertyStub,
JS_StrictPropertyStub,
JS_EnumerateStub,
JS_ResolveStub,
JS_ConvertStub,
nullptr, nullptr, nullptr, nullptr,
JS_GlobalObjectTraceHook
};
int main(int argc, const char *argv[]) {
// Initialize the JS engine.
if (!JS_Init())
return 1;
// Create a JS runtime.
JSRuntime *rt = JS::JS_NewRuntime(8L * 1024L * 1024L, JS_USE_HELPER_THREADS);
if (!rt)
return 1;
// Create a context.
JSContext *cx = JS_NewContext(rt, 8080);
if (!cx)
return 1;
const char * source = "var a = 'hello'";
printf("1\n");
JS::OwningCompileOptions options(cx);
options.setIntroductionType("js shell file");
//.setUTF8(true);
//.setFileAndLine(cx, "typein", 1);
RootedScript script(cx);
RootedObject glob(cx);
glob = JS_NewGlobalObject(cx, &globalClass, NULL, JS::DontFireOnNewGlobalHook);
printf("2\n");
script = JS::Compile(cx, glob, options, source, strlen(source));
if (!script)
return false;
printf("3\n");
// Shut everything down.
JS_DestroyContext(cx);
JS_DestroyRuntime(rt);
JS_ShutDown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment