| 195 | }; |
| 196 | |
| 197 | JSContext* gjs_create_js_context(GjsContextPrivate* uninitialized_gjs) { |
| 198 | g_assert(gjs_is_inited); |
| 199 | JSContext* cx = JS_NewContext(/* max bytes = */ 32 * 1024 * 1024); |
| 200 | if (!cx) |
| 201 | return nullptr; |
| 202 | |
| 203 | if (!JS::InitSelfHostedCode(cx)) { |
| 204 | JS_DestroyContext(cx); |
| 205 | return nullptr; |
| 206 | } |
| 207 | |
| 208 | // For additional context on these options, see |
| 209 | // https://searchfox.org/mozilla-esr91/rev/c49725508e97c1e2e2bb3bf9ed0ba14b2016abac/js/public/GCAPI.h#53 |
| 210 | JS_SetNativeStackQuota(cx, 1024UL * 1024UL); |
| 211 | JS_SetGCParameter(cx, JSGC_MAX_BYTES, -1); |
| 212 | JS_SetGCParameter(cx, JSGC_INCREMENTAL_GC_ENABLED, 1); |
| 213 | JS_SetGCParameter(cx, JSGC_SLICE_TIME_BUDGET_MS, 10); |
| 214 | |
| 215 | // set ourselves as the private data |
| 216 | JS_SetContextPrivate(cx, uninitialized_gjs); |
| 217 | |
| 218 | JS_SetSecurityCallbacks(cx, &security_callbacks); |
| 219 | JS_InitDestroyPrincipalsCallback(cx, &ModuleLoaderPrincipals::destroy); |
| 220 | JS_AddFinalizeCallback(cx, gjs_finalize_callback, uninitialized_gjs); |
| 221 | JS::SetWarningReporter(cx, gjs_warning_reporter); |
| 222 | JS::SetJobQueue(cx, dynamic_cast<JS::JobQueue*>(uninitialized_gjs)); |
| 223 | JS::SetPromiseRejectionTrackerCallback(cx, on_promise_unhandled_rejection, |
| 224 | uninitialized_gjs); |
| 225 | JS::SetHostCleanupFinalizationRegistryCallback( |
| 226 | cx, on_cleanup_finalization_registry, uninitialized_gjs); |
| 227 | js::SetDOMCallbacks(cx, &dom_callbacks); |
| 228 | |
| 229 | // We use this to handle "lazy sources" that SpiderMonkey doesn't need to |
| 230 | // keep in memory. Most sources should be kept in memory, but we can skip |
| 231 | // doing that for the realm bootstrap code, as it is already in memory in |
| 232 | // the form of a GResource. Instead we use the "source hook" to retrieve it. |
| 233 | auto hook = mozilla::MakeUnique<GjsSourceHook>(); |
| 234 | js::SetSourceHook(cx, std::move(hook)); |
| 235 | |
| 236 | if (g_getenv("GJS_DISABLE_EXTRA_WARNINGS")) { |
| 237 | g_warning( |
| 238 | "GJS_DISABLE_EXTRA_WARNINGS has been removed, GJS no longer logs " |
| 239 | "extra warnings."); |
| 240 | } |
| 241 | |
| 242 | bool enable_jit = !(g_getenv("GJS_DISABLE_JIT")); |
| 243 | if (enable_jit) { |
| 244 | gjs_debug(GJS_DEBUG_CONTEXT, "Enabling JIT"); |
| 245 | } |
| 246 | JS::ContextOptionsRef(cx).setAsmJS(enable_jit); |
| 247 | |
| 248 | uint32_t value = enable_jit ? 1 : 0; |
| 249 | |
| 250 | JS_SetGlobalJitCompilerOption( |
| 251 | cx, JSJitCompilerOption::JSJITCOMPILER_ION_ENABLE, value); |
| 252 | JS_SetGlobalJitCompilerOption( |
| 253 | cx, JSJitCompilerOption::JSJITCOMPILER_BASELINE_ENABLE, value); |
| 254 | JS_SetGlobalJitCompilerOption( |
no test coverage detected