| 72 | |
| 73 | void 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 | |
| 82 | void JavaScriptWorker::close() { |
| 83 | doClose(); |
| 84 | } |
| 85 | |
| 86 | void 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 | |
| 106 | void 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 { |
no test coverage detected