PyObject* Runtime_new(PyTypeObject* type, PyObject* args, PyObject* kwargs) { Runtime* self = NULL; unsigned int stacksize = 0x2000000; // 32 MiB heap size. if (!PyArg_ParseTuple(args, "|I", &stacksize)) goto error; #ifdef SPIDERMONKEY_31 if (!JS_Init()) { PyErr_SetString(JSError, "Failed to initilaize JS engine"); goto error; } #endif self = (Runtime*) type->tp_alloc(type, 0); if(self == NULL) goto error; self->rt = JS_NewRuntime(stacksize, JS_NO_HELPER_THREADS); if(self->rt == NULL) { PyErr_SetString(JSError, "Failed to allocate new JSRuntime."); goto error; } goto success; error: Py_XDECREF(self); self = NULL; success: return (PyObject*) self; }
void SetupScripting() { /* Initialize the JS engine -- new/required as of SpiderMonkey 31. */ JS_Init(120000); /* Create a JS runtime. */ rt = JS_NewRuntime(8L * 1024L * 1024L); /* Create a context. */ cx = JS_NewContext(rt, 8192); JS_SetOptions(cx, JSOPTION_VAROBJFIX); JS_SetErrorReporter(cx, reportError); /* Enter a request before running anything in the context */ JSAutoRequest ar(cx); /* Create the global object in a new compartment. */ global = JS_NewGlobalObject(cx, &global_class, NULL); /* Set the context's global */ JSAutoCompartment ac(cx, global); JS_SetGlobalObject(cx, global); /* Populate the global object with the standard globals, like Object and Array. */ JS_InitStandardClasses(cx, global); }
int main(int argc, char *argv[]) { int total = 0; int failures = 0; const char *filter = (argc == 2) ? argv[1] : nullptr; if (!JS_Init()) { printf("TEST-UNEXPECTED-FAIL | jsapi-tests | JS_Init() failed.\n"); return 1; } for (JSAPITest *test = JSAPITest::list; test; test = test->next) { const char *name = test->name(); if (filter && strstr(name, filter) == nullptr) continue; total += 1; printf("%s\n", name); if (!test->init()) { printf("TEST-UNEXPECTED-FAIL | %s | Failed to initialize.\n", name); failures++; test->uninit(); continue; } JS::HandleObject global = JS::HandleObject::fromMarkedLocation(test->global.unsafeGet()); if (test->run(global)) { printf("TEST-PASS | %s | ok\n", name); } else { JSAPITestString messages = test->messages(); printf("%s | %s | %.*s\n", (test->knownFail ? "TEST-KNOWN-FAIL" : "TEST-UNEXPECTED-FAIL"), name, (int) messages.length(), messages.begin()); if (!test->knownFail) failures++; } test->uninit(); } JS_ShutDown(); if (failures) { printf("\n%d unexpected failure%s.\n", failures, (failures == 1 ? "" : "s")); return 1; } printf("\nPassed: ran %d tests.\n", total); return 0; }
int cScriptEngine::Init() { m_rt = JS_Init(0x100000); if(m_rt != NULL) { m_cx = JS_NewContext(m_rt, 0x1000); } if(m_cx != NULL) { m_gObj = JS_NewObject(m_cx, &_js_globalClass, 0, 0); } if(m_gObj != NULL) { JS_InitStandardClasses(m_cx, m_gObj); } return 0; }
static void sajsj_InitLocked() { JRIEnv *env; /* if jsj has already been initialized, we don't do this * standalone jsj setup, and none of the stuff in this * file gets used */ if (!JSJ_Init(&sajsjCallbacks)) return; jsjiTask = JS_Init(8L * 1024L * 1024L); jsmon = PR_NewMonitor(); globalContext = JS_NewContext(jsjiTask, 8192); PR_ASSERT(globalContext); globalObject = NULL; JS_AddRoot(globalContext, &globalObject); globalObject = JS_NewObject(globalContext, &sajsj_global_class, NULL, NULL); if (!globalObject) { PR_ASSERT(globalObject); goto trash_cx; } if (!JS_InitStandardClasses(globalContext, globalObject)) goto trash_cx; if (!JSJ_InitContext(globalContext, globalObject)) goto trash_cx; env = JRI_GetCurrentEnv(); PR_ASSERT(env); return; trash_cx: JS_DestroyContext(globalContext); globalContext = NULL; /* FIXME error codes? */ return; }
MozJSScriptEngine::MozJSScriptEngine() { uassert(ErrorCodes::JSInterpreterFailure, "Failed to JS_Init()", JS_Init()); }
bool CompileFile(const std::string &inputFilePath, const std::string &outputFilePath) { bool result = false; std::string ofp; if (!outputFilePath.empty()) { ofp = outputFilePath; } else { ofp = RemoveFileExt(inputFilePath) + BYTE_CODE_FILE_EXT; } if (!JS_Init()) return false; std::cout << "Input file: " << inputFilePath << std::endl; JSRuntime * runtime = JS_NewRuntime(10 * 1024 * 1024, JS_NO_HELPER_THREADS); JSContext *cx = JS_NewContext(runtime, 10240); JS_SetOptions(cx, JSOPTION_TYPE_INFERENCE); JS::CompartmentOptions options; options.setVersion(JSVERSION_LATEST); JS::RootedObject global(cx, JS_NewGlobalObject(cx, &GlobalClass, NULL, JS::DontFireOnNewGlobalHook, options)); JS_SetErrorReporter(cx, &ReportError); { JSAutoCompartment ac(cx, global); if (JS_InitStandardClasses(cx, global)) { JS_InitReflect(cx, global); JS_FireOnNewGlobalObject(cx, global); JS::CompileOptions options(cx); options.setUTF8(true); options.setSourcePolicy(JS::CompileOptions::NO_SOURCE); std::cout << "Compiling ..." << std::endl; JS::RootedScript script(cx, JS::Compile(cx, global, options, inputFilePath.c_str())); if (script) { void *data = NULL; uint32_t length = 0; std::cout << "Encoding ..." << std::endl; data = JS_EncodeScript(cx, script, &length); if (data) { if (WriteFile(ofp, data, length)) { std::cout << "Done! " << "Output file: " << ofp << std::endl; result = true; } } } else { std::cout << "Compiled " << inputFilePath << " fails!" << std::endl; } } else { std::cout << "JS_InitStandardClasses failed! " << std::endl; } } if (cx) { JS_DestroyContext(cx); cx = NULL; } if (runtime) { JS_DestroyRuntime(runtime); runtime = NULL; } JS_ShutDown(); return result; }
MozJSScriptEngine::MozJSScriptEngine() { uassert(ErrorCodes::JSInterpreterFailure, "Failed to JS_Init()", JS_Init()); js::DisableExtraThreads(); }