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

Method doPostInit

valdi/src/valdi/runtime/JavaScript/JavaScriptWorker.cpp:74–111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

72
73void JavaScriptWorker::postMessage(const Ref<JavaScriptMessage>& message) {
74 _workerRuntime->dispatchOnJsThread(JsThreadDispatchReason::WorkerPostMessage,
75 JavaScriptTaskScheduleTypeDefault,
76 0,
77 [self = strongSmallRef(this), message](JavaScriptEntryParameters& entry) {
78 self->doPostMessage(entry, message);
79 });
80}
81
82void JavaScriptWorker::close() {
83 doClose();
84}
85
86void JavaScriptWorker::terminate() {
87 {
88 std::lock_guard<Mutex> lock(_mutex);
89 if (_state != State::Running) {
90 return;
91 }
92 _state = State::Terminated;
93 _hostOnMessage = nullptr;
94 }
95
96 // Aggressive termination forces the JS engine to abort mid-execution (even a frozen worker), which
97 // tears down in-flight bridge calls and causes teardown-race side effects. In cooperative mode we
98 // skip the forced abort: the worker drains its current work on the serial JS queue and the
99 // async-queued teardown then runs cleanly after it (matching self.close()).
100 if (!_workerRuntime->cooperativeTermination()) {
101 _workerRuntime->requestExecutionTermination();
102 }
103 _workerRuntime->requestFullTeardown();
104}
105
106void JavaScriptWorker::doPostInit() {
107 auto weakSelf = weakRef(this);
108 // Set up globals in the worker runtime
109 // - onmessage
110 // - postMessage
111 // - close
112 // - location, https://developer.mozilla.org/en-US/docs/Web/API/WorkerLocation, only href and search are populated
113 _workerRuntime->setValueToGlobalObject(STRING_LITERAL("onmessage"), Value::undefined());
114 auto postMessageFunc = [weakSelf](const ValueFunctionCallContext& callContext) -> Value {

Callers 1

postInitMethod · 0.95

Calls 12

closeMethod · 0.95
weakRefFunction · 0.85
dispatchMessageFunction · 0.85
getParameterMethod · 0.80
substringMethod · 0.80
evalModuleSyncMethod · 0.80
valueMethod · 0.65
ValueClass · 0.50
lockMethod · 0.45
indexOfMethod · 0.45
has_valueMethod · 0.45

Tested by

no test coverage detected