zibbr

Random 3d and p2p stuff that was originally the beginnings of an overly ambitious p2p virtual world thingy
git clone https://code.literati.org/zibbr.git
Log | Files | Refs

js-test.c (2011B)


      1 #include <string.h>
      2 #include <jsapi.h>
      3 #include "js-funcs.h"
      4 
      5 /* The class of the global object. */
      6 static JSClass global_class = {
      7     "global", JSCLASS_GLOBAL_FLAGS,
      8     JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
      9     JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
     10     JSCLASS_NO_OPTIONAL_MEMBERS
     11 };
     12 
     13 /* The error reporter callback. */
     14 void reportError(JSContext *cx, const char *message, JSErrorReport *report)
     15 {
     16     fprintf(stderr, "%s:%u:%s\n",
     17             report->filename ? report->filename : "<no filename>",
     18             (unsigned int) report->lineno,
     19             message);
     20 }
     21 
     22 int main(int argc, const char *argv[])
     23 {
     24     /* JS variables. */
     25     JSRuntime *rt;
     26     JSContext *cx;
     27     JSObject  *global;
     28 
     29     /* Create a JS runtime. */
     30     rt = JS_NewRuntime(8L * 1024L * 1024L);
     31     if (rt == NULL)
     32         return 1;
     33 
     34     /* Create a context. */
     35     cx = JS_NewContext(rt, 8192);
     36     if (cx == NULL)
     37         return 1;
     38     JS_SetOptions(cx, JSOPTION_VAROBJFIX);
     39     JS_SetVersion(cx, JSVERSION_1_7);
     40     JS_SetErrorReporter(cx, reportError);
     41 
     42     /* Create the global object. */
     43     global = JS_NewObject(cx, &global_class, NULL, NULL);
     44     if (global == NULL)
     45         return 1;
     46 
     47     /* Populate the global object with the standard globals,
     48        like Object and Array. */
     49     if (!JS_InitStandardClasses(cx, global))
     50         return 1;
     51 
     52     if (!setup_js_functions(cx, global)) {
     53         return 1;
     54     }
     55 
     56     /* Your application code here. This may include JSAPI calls
     57        to create your own custom JS objects and run scripts. */
     58     jsval rval;
     59     JSBool ok;
     60 
     61     /*
     62      * Some example source in a C string.  Larger, non-null-terminated buffers
     63      * can be used, if you pass the buffer length to JS_EvaluateScript.
     64      */
     65     JSScript *script = JS_CompileFile(cx, global, "main.js");
     66 
     67     ok = JS_ExecuteScript(cx, global, script, &rval);
     68 
     69     /* Cleanup. */
     70     JS_DestroyContext(cx);
     71     JS_DestroyRuntime(rt);
     72     JS_ShutDown();
     73     return 0;
     74 }
     75 
     76