Last active
August 29, 2015 13:57
-
-
Save keithjohnston/9559345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "jsapi.h" | |
// Compile for OSX: | |
// /usr/bin/clang++ -I include -m32 -std=c++0x -g helloworld.cpp -lobjc libjs_static.a -l z -l m | |
#include "jsapi.h" | |
/* The class of the global object. */ | |
static JSClass global_class = { | |
"global", | |
JSCLASS_GLOBAL_FLAGS, | |
JS_PropertyStub, | |
JS_DeletePropertyStub, | |
JS_PropertyStub, | |
JS_StrictPropertyStub, | |
JS_EnumerateStub, | |
JS_ResolveStub, | |
JS_ConvertStub | |
}; | |
int main(int argc, const char *argv[]) | |
{ | |
JSRuntime *rt; | |
JSContext *cx; | |
JS::RootedObject *global; | |
JS_Init(); | |
rt = JS_NewRuntime(8L * 1024 * 1024, JS_NO_HELPER_THREADS); | |
if (rt == NULL) | |
return 1; | |
cx = JS_NewContext(rt, 8192); | |
if (cx == NULL) | |
return 1; | |
global = new JS::RootedObject(cx, JS_NewGlobalObject(cx, &global_class, nullptr, JS::DontFireOnNewGlobalHook)); | |
if (global == NULL) | |
return 1; | |
const char *script = "'hello'+'world'"; | |
jsval rval; | |
JSString *str; | |
bool ok; | |
const char *filename = "noname"; | |
int lineno = 0; | |
{ | |
JSAutoCompartment ac(cx, *global); | |
ok = JS_EvaluateScript(cx, *global, script, strlen(script), filename, lineno, &rval); | |
if (rval.isNull() | rval.isFalse() ) | |
return 1; | |
} | |
str = JSVAL_TO_STRING(rval); | |
printf("%s\n", JS_EncodeString(cx, str)); | |
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