view helloworld.cpp @ 2:993424504eb7

This netbeans project for single thread application it didn't work yet, may be the spidermonkey's version is the reason and I'll try to solve this
author Sara
date Sun, 05 Jan 2014 14:13:51 -0800
parents
children
line source
3 /* Include the JSAPI header file to get access to SpiderMonkey. */
4 #include "jsapi.h"
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 };
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 }
23 int main(int argc, const char *argv[])
24 {
25 /* JSAPI variables. */
26 JSRuntime *rt;
27 JSContext *cx;
28 JSObject *global;
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;
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);
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;
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;
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;
76 ok = JS_EvaluateScript(cx, global, script, strlen(script),
77 filename, lineno, &rval);
78 if ( rval == JS_FALSE)//rval == JS_NULL |
79 return 1;
81 str = JS_ValueToString(cx, rval);
82 printf("%s\n", JS_EncodeString(cx, str));
84 /* End of your application code */
86 /* Clean things up and shut down SpiderMonkey. */
87 JS_DestroyContext(cx);
88 JS_DestroyRuntime(rt);
89 JS_ShutDown();
90 return 0;
91 }