Advertisement
chaircrusher

Spidermonkey JS45 Init

May 24th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is an example for SpiderMonkey 31.
  2. // For SpiderMonkey 24 or 38, see each comment.
  3.  
  4. // following code might be needed in some case
  5. // #define __STDC_LIMIT_MACROS
  6. // #include <stdint.h>
  7. #if defined(JSAPI_DEBUG)
  8. #define DEBUG
  9. #endif
  10. #include <iostream>
  11. #include <jsapi.h>
  12. #include <js/Initialization.h>
  13.  
  14. // TEST CODE FOR JS_InitClass
  15. static bool TESTO_good(JSContext *cx, unsigned int argc, JS::Value *vp) {
  16.     JS::CallArgs      args = JS::CallArgsFromVp(argc, vp);
  17.     JS::RootedObject obj( cx, JS_THIS_OBJECT(cx, vp) );
  18.     args.rval().setBoolean(true);
  19.     return true;
  20. }
  21.  
  22. static JSFunctionSpec TESTO_methods[] = {
  23.     JS_FS("good", TESTO_good, 1, 0),
  24.     JS_FS_END
  25. };
  26.  
  27. static void TESTO_dtor(JSFreeOp *, JSObject *obj) {
  28. }
  29.  
  30. static JSClass TESTO = {
  31.     "TESTO", JSCLASS_HAS_PRIVATE,
  32.     0,                                    // JSPropertyOp        addProperty;
  33.     0,                                    // JSDeletePropertyOp  delProperty;
  34.     0,                                    // JSPropertyOp        getProperty;
  35.     0,                                    // JSStrictPropertyOp  setProperty;
  36.     0,                                    // JSEnumerateOp       enumerate;
  37.     0,                                    // JSResolveOp         resolve;
  38.     0,                                    // JSConvertOp         convert;
  39.     TESTO_dtor,               // FinalizeOpType      finalize;
  40.     0,                                    // JSNative            call;
  41.     0,                                    // JSHasInstanceOp     hasInstance;
  42.     0,                                    // JSNative            construct;
  43.     0,    // JSTraceOp           trace
  44. };
  45.  
  46. static bool TESTO_ctor(JSContext *cx, unsigned int argc, JS::Value *vp) {
  47.     JS::CallArgs      args = JS::CallArgsFromVp(argc, vp);
  48.     JS::RootedObject obj(cx);
  49.     if (args.isConstructing()) {
  50.         obj = JS_NewObject(cx, &TESTO);
  51.         if (!obj)
  52.             return false;
  53.     } else {
  54.         obj = JS_THIS_OBJECT(cx, vp);
  55.     }
  56.     args.rval().setObject(*obj);
  57.     return true;
  58. }
  59.  
  60. bool init_TESTO(JSContext *cx, JS::HandleObject globs) {
  61.     if(JS_InitClass(cx, globs, nullptr, &TESTO,
  62.                          TESTO_ctor, 0, nullptr, TESTO_methods, nullptr, nullptr) == nullptr)
  63.         abort();
  64.     return true;
  65. }
  66.  
  67. // KW ADD -- need JS_GlobalObjectTraceHook
  68. JSClass global_class = {
  69.     "global", JSCLASS_GLOBAL_FLAGS,
  70.     0,                                    // JSPropertyOp        addProperty;
  71.     0,                                    // JSDeletePropertyOp  delProperty;
  72.     0,                                    // JSPropertyOp        getProperty;
  73.     0,                                    // JSStrictPropertyOp  setProperty;
  74.     0,                                    // JSEnumerateOp       enumerate;
  75.     0,                                    // JSResolveOp         resolve;
  76.     0,                                    // JSConvertOp         convert;
  77.     0,                                    // FinalizeOpType      finalize;
  78.     0,                                    // JSNative            call;
  79.     0,                                    // JSHasInstanceOp     hasInstance;
  80.     0,                                    // JSNative            construct;
  81.     JS_GlobalObjectTraceHook,     // JSTraceOp           trace
  82. };
  83.  
  84. void JSErrorHandler(JSContext *cx, const char *message, JSErrorReport* what) {
  85.     std::cout << "ERROR: " << message;
  86.     if (what->filename)
  87.         std::cout << ": file: " << what->filename;
  88.     std::cout << ", line:" << (int) what->lineno
  89.                  << ", column:" << (int) what->column
  90.                  << std::endl;
  91. }
  92.  
  93. int main(int argc, const char *argv[])
  94. {
  95.     // [SpiderMonkey 24] JS_Init does not exist. Remove this line.
  96.     JS_Init();
  97.  
  98.     // [SpiderMonkey 38] useHelperThreads parameter is removed.
  99.     JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024);
  100.     // JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024, JS_USE_HELPER_THREADS);
  101.     if (!rt)
  102.         return 1;
  103.  
  104.     JSContext *cx = JS_NewContext(rt, 8192);
  105.     if (!cx)
  106.         return 1;
  107.      // KW ADD -- everything that happens needs to be inside a request.
  108.      JS_BeginRequest(cx);
  109.  
  110.     // [SpiderMonkey 24] hookOption parameter does not exist.
  111.     // JS::RootedObject global(cx, JS_NewGlobalObject(cx, &global_class, nullptr));
  112.      // KW ADD -- move global down into brace so it will go out of
  113.     // scope before DestroyContext
  114.  
  115.      // KW ADD -- move rval inside scope, so
  116.      // it will be destroyed before cleanup at end
  117.     {
  118.          JS::RootedValue rval(cx);
  119.          JS::RootedObject global(cx,
  120.                                          JS_NewGlobalObject(cx, &global_class, nullptr, JS::FireOnNewGlobalHook));
  121.          if (!global)
  122.              return 1;
  123.         JSAutoCompartment ac(cx, global);
  124.       JS_InitStandardClasses(cx, global);
  125.         init_TESTO(cx, global);
  126.         // set error reporter
  127.         JS_SetErrorReporter(rt, JSErrorHandler);
  128.  
  129.  
  130.       const char *script = "'hello'+'world, it is '+new Date();\n"
  131.             "var TESTO = new TESTO(\"testzip.zip\");\n"
  132.             "if (!TESTO || !TESTO.good())\n"
  133.             "throw \"FAIL: cannot create TESTO\";\n";
  134.       const char *filename = "noname";
  135.       int lineno = 1;
  136.       // [SpiderMonkey 24] The type of rval parameter is 'jsval *'.
  137.       // bool ok = JS_EvaluateScript(cx, global, script, strlen(script), filename, lineno, rval.address());
  138.       // [SpiderMonkey 38] JS_EvaluateScript is replaced with JS::Evaluate.
  139.       JS::CompileOptions opts(cx);
  140.       opts.setFileAndLine(filename, lineno);
  141.       bool ok = JS::Evaluate(cx, opts, script, strlen(script), &rval);
  142.       // bool ok = JS_EvaluateScript(cx, global, script, strlen(script), filename, lineno, &rval);
  143.       if (!ok)
  144.         return 1;
  145.  
  146.     JSString *str = rval.toString();
  147.     printf("%s\n", JS_EncodeString(cx, str));
  148.     }
  149.      // KW ADD
  150.      JS_EndRequest(cx);
  151.     JS_DestroyContext(cx);
  152.     JS_DestroyRuntime(rt);
  153.     JS_ShutDown();
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement