annotate SpiderMonkey_tests/helloworld.cpp @ 14:7eb1a37c12da

the two parallel threads with two different isolates
author Sara
date Sat, 15 Feb 2014 13:17:49 -0800
parents
children
rev   line source
seanhalle@6 1
seanhalle@6 2
seanhalle@6 3 /* Include the JSAPI header file to get access to SpiderMonkey. */
seanhalle@6 4 #include "jsapi.h"
seanhalle@6 5
seanhalle@6 6 /* The class of the global object. */
seanhalle@6 7 static JSClass global_class = {
seanhalle@6 8 "global", JSCLASS_GLOBAL_FLAGS,
seanhalle@6 9 JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
seanhalle@6 10 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
seanhalle@6 11 JSCLASS_NO_OPTIONAL_MEMBERS
seanhalle@6 12 };
seanhalle@6 13
seanhalle@6 14 /* The error reporter callback. */
seanhalle@6 15 void reportError(JSContext *cx, const char *message, JSErrorReport *report)
seanhalle@6 16 {
seanhalle@6 17 fprintf(stderr, "%s:%u:%s\n",
seanhalle@6 18 report->filename ? report->filename : "<no filename=\"filename\">",
seanhalle@6 19 (unsigned int) report->lineno,
seanhalle@6 20 message);
seanhalle@6 21 }
seanhalle@6 22
seanhalle@6 23 int main(int argc, const char *argv[])
seanhalle@6 24 {
seanhalle@6 25 /* JSAPI variables. */
seanhalle@6 26 JSRuntime *rt;
seanhalle@6 27 JSContext *cx;
seanhalle@6 28 JSObject *global;
seanhalle@6 29
seanhalle@6 30 /* Create a JS runtime. You always need at least one runtime per process. */
seanhalle@6 31 rt = JS_NewRuntime(8 * 1024 * 1024);
seanhalle@6 32 if (rt == NULL)
seanhalle@6 33 return 1;
seanhalle@6 34
seanhalle@6 35 /*
seanhalle@6 36 * Create a context. You always need a context per thread.
seanhalle@6 37 * Note that this program is not multi-threaded.
seanhalle@6 38 */
seanhalle@6 39 cx = JS_NewContext(rt, 8192);
seanhalle@6 40 if (cx == NULL)
seanhalle@6 41 return 1;
seanhalle@6 42 JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
seanhalle@6 43 JS_SetVersion(cx, JSVERSION_LATEST);
seanhalle@6 44 JS_SetErrorReporter(cx, reportError);
seanhalle@6 45
seanhalle@6 46 /*
seanhalle@6 47 * Create the global object in a new compartment.
seanhalle@6 48 * You always need a global object per context.
seanhalle@6 49 */
seanhalle@6 50 global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
seanhalle@6 51 if (global == NULL)
seanhalle@6 52 return 1;
seanhalle@6 53
seanhalle@6 54 /*
seanhalle@6 55 * Populate the global object with the standard JavaScript
seanhalle@6 56 * function and object classes, such as Object, Array, Date.
seanhalle@6 57 */
seanhalle@6 58 if (!JS_InitStandardClasses(cx, global))
seanhalle@6 59 return 1;
seanhalle@6 60
seanhalle@6 61 /* Your application code here. This may include JSAPI calls
seanhalle@6 62 * to create your own custom JavaScript objects and to run scripts.
seanhalle@6 63 *
seanhalle@6 64 * The following example code creates a literal JavaScript script,
seanhalle@6 65 * evaluates it, and prints the result to stdout.
seanhalle@6 66 *
seanhalle@6 67 * Errors are conventionally saved in a JSBool variable named ok.
seanhalle@6 68 */
seanhalle@6 69 const char *script = "'Hello ' + 'World!'";
seanhalle@6 70 jsval rval;
seanhalle@6 71 JSString *str;
seanhalle@6 72 JSBool ok;
seanhalle@6 73 const char *filename = "noname";
seanhalle@6 74 uintN lineno = 0;
seanhalle@6 75
seanhalle@6 76 ok = JS_EvaluateScript(cx, global, script, strlen(script),
seanhalle@6 77 filename, lineno, &rval);
seanhalle@6 78 if ( rval == JS_FALSE)//rval == JS_NULL |
seanhalle@6 79 return 1;
seanhalle@6 80
seanhalle@6 81 str = JS_ValueToString(cx, rval);
seanhalle@6 82 printf("%s\n", JS_EncodeString(cx, str));
seanhalle@6 83
seanhalle@6 84 /* End of your application code */
seanhalle@6 85
seanhalle@6 86 /* Clean things up and shut down SpiderMonkey. */
seanhalle@6 87 JS_DestroyContext(cx);
seanhalle@6 88 JS_DestroyRuntime(rt);
seanhalle@6 89 JS_ShutDown();
seanhalle@6 90 return 0;
seanhalle@6 91 }