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