MCPcopy Create free account
hub / github.com/Snapchat/Valdi / initializeContext

Method initializeContext

valdi/src/valdi/runtime/JavaScript/JavaScriptRuntime.cpp:462–500  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

460 return;
461 }
462
463 // Just in case postInit() was never called
464 _initLock.leaveIfNotCompleted();
465
466 _dispatchQueue->async([self = strongSmallRef(this)]() { self->teardownOnJsThread(true); });
467}
468
469void JavaScriptRuntime::requestExecutionTermination() {
470 // requestExecutionTermination() on the context only sets a sticky atomic flag (Hermes also triggers a
471 // non-blocking async break), so it is safe to call while holding _mutex. Doing so keeps it mutually
472 // exclusive with teardownOnJsThread(), which clears _javaScriptContext under the same lock: we never
473 // hold a strong context reference past the lock, avoiding both the teardown SC_ASSERT race and a
474 // wrong-thread context destruction if the dispatch queue is already torn down.
475 std::lock_guard<Mutex> lock(_mutex);
476 if (_isDisposed || _javaScriptContext == nullptr) {
477 return;
478 }
479 _javaScriptContext->requestExecutionTermination();
480}
481
482void JavaScriptRuntime::teardown(bool destroyContext) {
483 auto wasDisposed = _isDisposed.exchange(true);
484 if (wasDisposed) {
485 return;
486 }
487
488 // Just in case postInit() was never called
489 _initLock.leaveIfNotCompleted();
490
491 markWorkersHostTornDown();
492
493 // Workers share this runtime's listener (see createWorker); detach them so a worker
494 // outliving this runtime does not keep a dangling reference. A worker task holding a
495 // RetainedListener keeps the listener's owner alive until the call completes.
496 for (const auto& jsWorker : getAllWorkers()) {
497 jsWorker->setListener(nullptr, {});
498 }
499
500 if (destroyContext) {
501 _dispatchQueue->safeSync([&]() { teardownOnJsThread(true); });
502 } else {
503 teardownOnJsThread(false);

Callers

nothing calls this directly

Calls 10

extractErrorMethod · 0.80
initializeMethod · 0.65
getMethod · 0.65
VoidClass · 0.50
getRuntimeTweaksMethod · 0.45
getNameMethod · 0.45
createJsContextMethod · 0.45
startDebuggerMethod · 0.45
setContextMethod · 0.45

Tested by

no test coverage detected