changeset 0:299e986f3250

This the minimal example
author Sara
date Fri, 13 Dec 2013 10:25:32 -0800
parents
children 4ac33e06cb09
files helloworld.cpp
diffstat 1 files changed, 91 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/helloworld.cpp	Fri Dec 13 10:25:32 2013 -0800
     1.3 @@ -0,0 +1,91 @@
     1.4 +
     1.5 +
     1.6 +/* Include the JSAPI header file to get access to SpiderMonkey. */
     1.7 +#include "jsapi.h"
     1.8 +
     1.9 +/* The class of the global object. */
    1.10 +static JSClass global_class = {
    1.11 +    "global", JSCLASS_GLOBAL_FLAGS,
    1.12 +    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
    1.13 +    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
    1.14 +    JSCLASS_NO_OPTIONAL_MEMBERS
    1.15 +};
    1.16 +
    1.17 +/* The error reporter callback. */
    1.18 +void reportError(JSContext *cx, const char *message, JSErrorReport *report)
    1.19 +{
    1.20 +    fprintf(stderr, "%s:%u:%s\n",
    1.21 +            report->filename ? report->filename : "<no filename=\"filename\">",
    1.22 +            (unsigned int) report->lineno,
    1.23 +            message);
    1.24 +}
    1.25 +
    1.26 +int main(int argc, const char *argv[])
    1.27 +{
    1.28 +    /* JSAPI variables. */
    1.29 +    JSRuntime *rt;
    1.30 +    JSContext *cx;
    1.31 +    JSObject  *global;
    1.32 +
    1.33 +    /* Create a JS runtime. You always need at least one runtime per process. */
    1.34 +    rt = JS_NewRuntime(8 * 1024 * 1024);
    1.35 +    if (rt == NULL)
    1.36 +        return 1;
    1.37 +
    1.38 +    /* 
    1.39 +     * Create a context. You always need a context per thread.
    1.40 +     * Note that this program is not multi-threaded.
    1.41 +     */
    1.42 +    cx = JS_NewContext(rt, 8192);
    1.43 +    if (cx == NULL)
    1.44 +        return 1;
    1.45 +    JS_SetOptions(cx, JSOPTION_VAROBJFIX | JSOPTION_JIT | JSOPTION_METHODJIT);
    1.46 +    JS_SetVersion(cx, JSVERSION_LATEST);
    1.47 +    JS_SetErrorReporter(cx, reportError);
    1.48 +
    1.49 +    /*
    1.50 +     * Create the global object in a new compartment.
    1.51 +     * You always need a global object per context.
    1.52 +     */
    1.53 +    global = JS_NewCompartmentAndGlobalObject(cx, &global_class, NULL);
    1.54 +    if (global == NULL)
    1.55 +        return 1;
    1.56 +
    1.57 +    /*
    1.58 +     * Populate the global object with the standard JavaScript
    1.59 +     * function and object classes, such as Object, Array, Date.
    1.60 +     */
    1.61 +    if (!JS_InitStandardClasses(cx, global))
    1.62 +        return 1;
    1.63 +
    1.64 +    /* Your application code here. This may include JSAPI calls
    1.65 +     * to create your own custom JavaScript objects and to run scripts.
    1.66 +     *
    1.67 +     * The following example code creates a literal JavaScript script,
    1.68 +     * evaluates it, and prints the result to stdout.
    1.69 +     *
    1.70 +     * Errors are conventionally saved in a JSBool variable named ok.
    1.71 +     */
    1.72 +    const char *script = "'Hello ' + 'World!'";
    1.73 +    jsval rval;
    1.74 +    JSString *str;
    1.75 +    JSBool ok;
    1.76 +    const char *filename = "noname";
    1.77 +    uintN lineno = 0;
    1.78 +
    1.79 +    ok = JS_EvaluateScript(cx, global, script, strlen(script),
    1.80 +                           filename, lineno, &rval);
    1.81 +    if ( rval == JS_FALSE)//rval == JS_NULL |
    1.82 +        return 1;
    1.83 +
    1.84 +    str = JS_ValueToString(cx, rval);
    1.85 +    printf("%s\n", JS_EncodeString(cx, str));
    1.86 +
    1.87 +    /* End of your application code */
    1.88 +
    1.89 +    /* Clean things up and shut down SpiderMonkey. */
    1.90 +    JS_DestroyContext(cx);
    1.91 +    JS_DestroyRuntime(rt);
    1.92 +    JS_ShutDown();
    1.93 +    return 0;
    1.94 +}