spidermonkey_vm *sm_initialize(long thread_stack, long heap_size) { spidermonkey_vm *vm = ejs_alloc(sizeof(spidermonkey_vm)); spidermonkey_state *state = ejs_alloc(sizeof(spidermonkey_state)); state->branch_count = 0; state->error = NULL; state->terminate = 0; int gc_size = (int) heap_size * 0.25; vm->runtime = JS_NewRuntime(MAX_GC_SIZE); JS_SetGCParameter(vm->runtime, JSGC_MAX_BYTES, heap_size); JS_SetGCParameter(vm->runtime, JSGC_MAX_MALLOC_BYTES, gc_size); vm->context = JS_NewContext(vm->runtime, 8192); JS_SetScriptStackQuota(vm->context, thread_stack); begin_request(vm); JS_SetOptions(vm->context, JSOPTION_VAROBJFIX); JS_SetOptions(vm->context, JSOPTION_STRICT); JS_SetOptions(vm->context, JSOPTION_COMPILE_N_GO); JS_SetOptions(vm->context, JSVERSION_LATEST); vm->global = JS_NewCompartmentAndGlobalObject(vm->context, &global_class, NULL); JS_InitStandardClasses(vm->context, vm->global); JS_SetErrorReporter(vm->context, on_error); JS_SetOperationCallback(vm->context, on_branch); JS_SetContextPrivate(vm->context, state); JSNative funptr = (JSNative) &js_log; JS_DefineFunction(vm->context, JS_GetGlobalObject(vm->context), "ejsLog", funptr, 0, 0); end_request(vm); return vm; }
JSBool XPCJSRuntime::OnJSContextNew(JSContext *cx) { NS_TIME_FUNCTION; // if it is our first context then we need to generate our string ids JSBool ok = JS_TRUE; if(JSID_IS_VOID(mStrIDs[0])) { JS_SetGCParameterForThread(cx, JSGC_MAX_CODE_CACHE_BYTES, 16 * 1024 * 1024); JSAutoRequest ar(cx); for(uintN i = 0; i < IDX_TOTAL_COUNT; i++) { JSString* str = JS_InternString(cx, mStrings[i]); if(!str || !JS_ValueToId(cx, STRING_TO_JSVAL(str), &mStrIDs[i])) { mStrIDs[0] = JSID_VOID; ok = JS_FALSE; break; } mStrJSVals[i] = STRING_TO_JSVAL(str); } } if (!ok) return JS_FALSE; XPCPerThreadData* tls = XPCPerThreadData::GetData(cx); if(!tls) return JS_FALSE; XPCContext* xpc = new XPCContext(this, cx); if (!xpc) return JS_FALSE; JS_SetNativeStackQuota(cx, 128 * sizeof(size_t) * 1024); JS_SetScriptStackQuota(cx, 25 * sizeof(size_t) * 1024 * 1024); // we want to mark the global object ourselves since we use a different color JS_ToggleOptions(cx, JSOPTION_UNROOTED_GLOBAL); return JS_TRUE; }
static void SetContextOptions(JSContext *cx) { jsuword stackLimit; if (gMaxStackSize == 0) { /* * Disable checking for stack overflow if limit is zero. */ stackLimit = 0; } else { #if JS_STACK_GROWTH_DIRECTION > 0 stackLimit = gStackBase + gMaxStackSize; #else stackLimit = gStackBase - gMaxStackSize; #endif } JS_SetThreadStackLimit(cx, stackLimit); JS_SetScriptStackQuota(cx, gScriptStackQuota); }
ContextCallback(JSContext *cx, uintN operation) { XPCJSRuntime* self = nsXPConnect::GetRuntime(); if (self) { if (operation == JSCONTEXT_NEW) { // Set the limits on the native and script stack space. XPCPerThreadData* tls = XPCPerThreadData::GetData(cx); if(tls) { JS_SetThreadStackLimit(cx, tls->GetStackLimit()); } JS_SetScriptStackQuota(cx, 100*1024*1024); } } return gOldJSContextCallback ? gOldJSContextCallback(cx, operation) : JS_TRUE; }
jaegermonkey_vm *sm_initialize(long thread_stack, long heap_size) { jaegermonkey_vm *vm = (jaegermonkey_vm*) driver_alloc(sizeof(jaegermonkey_vm)); int gc_size = (int) heap_size * 0.25; vm->runtime = JS_NewRuntime(MAX_GC_SIZE); JS_SetGCParameter(vm->runtime, JSGC_MAX_BYTES, heap_size); JS_SetGCParameter(vm->runtime, JSGC_MAX_MALLOC_BYTES, gc_size); vm->context = JS_NewContext(vm->runtime, 8192); JS_SetScriptStackQuota(vm->context, thread_stack); begin_request(vm); JS_SetOptions(vm->context, JSOPTION_VAROBJFIX); JS_SetOptions(vm->context, JSOPTION_STRICT); JS_SetOptions(vm->context, JSOPTION_COMPILE_N_GO); JS_SetOptions(vm->context, JSVERSION_LATEST); vm->global = JS_NewGlobalObject(vm->context, &global_class); JS_InitStandardClasses(vm->context, vm->global); JS_SetErrorReporter(vm->context, on_error); JS_DefineFunction(vm->context, JS_GetGlobalObject(vm->context), "ejsLog", js_log, 2, 0); end_request(vm); vm->invoke_count = 0; return vm; }
JSBool XPCJSRuntime::OnJSContextNew(JSContext *cx) { NS_TIME_FUNCTION; // if it is our first context then we need to generate our string ids JSBool ok = JS_TRUE; if(!mStrIDs[0]) { JS_SetGCParameterForThread(cx, JSGC_MAX_CODE_CACHE_BYTES, 16 * 1024 * 1024); JSAutoRequest ar(cx); for(uintN i = 0; i < IDX_TOTAL_COUNT; i++) { JSString* str = JS_InternString(cx, mStrings[i]); if(!str || !JS_ValueToId(cx, STRING_TO_JSVAL(str), &mStrIDs[i])) { mStrIDs[0] = 0; ok = JS_FALSE; break; } mStrJSVals[i] = STRING_TO_JSVAL(str); } } if (!ok) return JS_FALSE; XPCPerThreadData* tls = XPCPerThreadData::GetData(cx); if(!tls) return JS_FALSE; XPCContext* xpc = new XPCContext(this, cx); if (!xpc) return JS_FALSE; JS_SetNativeStackQuota(cx, 512 * 1024); JS_SetScriptStackQuota(cx, 100 * 1024 * 1024); return JS_TRUE; }
static GObject* gjs_context_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_params) { GObject *object; GjsContext *js_context; guint32 options_flags; JSVersion js_version; object = (* G_OBJECT_CLASS (gjs_context_parent_class)->constructor) (type, n_construct_properties, construct_params); js_context = GJS_CONTEXT(object); if (js_context->runtime == NULL) { js_context->runtime = JS_NewRuntime(32*1024*1024 /* max bytes */); if (js_context->runtime == NULL) gjs_fatal("Failed to create javascript runtime"); JS_SetGCParameter(js_context->runtime, JSGC_MAX_BYTES, 0xffffffff); js_context->we_own_runtime = TRUE; gjs_runtime_init(js_context->runtime); } js_context->context = JS_NewContext(js_context->runtime, 8192 /* stack chunk size */); if (js_context->context == NULL) gjs_fatal("Failed to create javascript context"); JS_BeginRequest(js_context->context); /* same as firefox, see discussion at * https://bugzilla.mozilla.org/show_bug.cgi?id=420869 */ JS_SetScriptStackQuota(js_context->context, 100*1024*1024); /* JSOPTION_DONT_REPORT_UNCAUGHT: Don't send exceptions to our * error report handler; instead leave them set. This allows us * to get at the exception object. * * JSOPTION_STRICT: Report warnings to error reporter function. */ options_flags = JSOPTION_DONT_REPORT_UNCAUGHT | JSOPTION_STRICT; if (!g_getenv("GJS_DISABLE_JIT")) { gjs_debug(GJS_DEBUG_CONTEXT, "Enabling JIT"); options_flags |= JSOPTION_METHODJIT; } JS_SetOptions(js_context->context, JS_GetOptions(js_context->context) | options_flags); JS_SetLocaleCallbacks(js_context->context, &gjs_locale_callbacks); JS_SetErrorReporter(js_context->context, gjs_error_reporter); /* set ourselves as the private data */ JS_SetContextPrivate(js_context->context, js_context); js_version = JS_StringToVersion(js_context->jsversion_string); /* It doesn't make sense to throw here; just use the default if we * don't know. */ if (js_version == JSVERSION_UNKNOWN) js_version = JSVERSION_DEFAULT; /* Set the version if we need to. */ if (js_version != JSVERSION_DEFAULT && JS_GetVersion(js_context->context) != js_version) { gjs_debug(GJS_DEBUG_CONTEXT, "Changing JavaScript version to %s from %s", JS_VersionToString(js_version), JS_VersionToString(JS_GetVersion(js_context->context))); JS_SetVersion(js_context->context, js_version); } if (!gjs_init_context_standard(js_context->context)) gjs_fatal("Failed to initialize context"); js_context->global = JS_GetGlobalObject(js_context->context); if (!JS_DefineProperty(js_context->context, js_context->global, "window", OBJECT_TO_JSVAL(js_context->global), NULL, NULL, JSPROP_READONLY | JSPROP_PERMANENT)) gjs_fatal("No memory to export global object as 'window'"); /* Define a global function called log() */ if (!JS_DefineFunction(js_context->context, js_context->global, "log", (JSNative)gjs_log, 1, GJS_MODULE_PROP_FLAGS)) gjs_fatal("Failed to define log function"); if (!JS_DefineFunction(js_context->context, js_context->global, "logError", (JSNative)gjs_log_error, 2, GJS_MODULE_PROP_FLAGS)) gjs_fatal("Failed to define logError function"); /* Define global functions called print() and printerr() */ if (!JS_DefineFunction(js_context->context, js_context->global, "print", (JSNative)gjs_print, 3, GJS_MODULE_PROP_FLAGS)) gjs_fatal("Failed to define print function"); if (!JS_DefineFunction(js_context->context, js_context->global, "printerr", (JSNative)gjs_printerr, 4, GJS_MODULE_PROP_FLAGS)) gjs_fatal("Failed to define printerr function"); /* We need to know what the default context is, since it's the context whose * global object is used to load imported JS modules. We currently say that * it's the context of the runtime's owner, but if we needed to support * externally created runtimes, we could define it in some other fashion. */ if (js_context->we_own_runtime) { gjs_runtime_set_default_context(js_context->runtime, js_context->context); } else { if (gjs_runtime_get_default_context(js_context->runtime) == NULL) gjs_fatal("GjsContext created for a runtime not owned by GJS"); } /* We create the global-to-runtime root importer with the * passed-in search path. If someone else already created * the root importer, this is a no-op. */ if (!gjs_create_root_importer(js_context->context, js_context->search_path ? (const char**) js_context->search_path : NULL, TRUE)) gjs_fatal("Failed to create root importer"); /* Now copy the global root importer (which we just created, * if it didn't exist) to our global object */ if (!gjs_define_root_importer(js_context->context, js_context->global, "imports")) gjs_fatal("Failed to point 'imports' property at root importer"); if (js_context->we_own_runtime) { js_context->profiler = gjs_profiler_new(js_context->runtime); } if (!gjs_is_registered_native_module(js_context->context, NULL, "gi")) gjs_register_native_module("gi", gjs_define_gi_stuff, GJS_NATIVE_SUPPLIES_MODULE_OBJ); /* For GjsDBus */ { char *priv_typelib_dir = g_build_filename (PKGLIBDIR, "girepository-1.0", NULL); g_irepository_prepend_search_path(priv_typelib_dir); g_free (priv_typelib_dir); } if (js_context->gc_notifications_enabled) JS_SetGCCallback(js_context->context, gjs_on_context_gc); JS_EndRequest(js_context->context); g_static_mutex_lock (&contexts_lock); all_contexts = g_list_prepend(all_contexts, object); g_static_mutex_unlock (&contexts_lock); return object; }