aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-08-01 16:42:49 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-08-01 16:42:49 -0700
commit214dc039e031124bed3021e5d97a54864089ad84 (patch)
tree954f452bc4ecfb871cab6b30d8e2057d4e16fb1a
parente2b39d0807ddbc47b06b5da3a35854d0164e9cb0 (diff)
downloadbun-214dc039e031124bed3021e5d97a54864089ad84.tar.gz
bun-214dc039e031124bed3021e5d97a54864089ad84.tar.zst
bun-214dc039e031124bed3021e5d97a54864089ad84.zip
Implement Worker.prototype.threadId
-rw-r--r--src/bun.js/bindings/webcore/JSWorker.cpp19
-rw-r--r--src/bun.js/bindings/webcore/Worker.h1
-rw-r--r--test/js/web/worker.test.ts1
3 files changed, 19 insertions, 2 deletions
diff --git a/src/bun.js/bindings/webcore/JSWorker.cpp b/src/bun.js/bindings/webcore/JSWorker.cpp
index 4ddd97520..882c9da89 100644
--- a/src/bun.js/bindings/webcore/JSWorker.cpp
+++ b/src/bun.js/bindings/webcore/JSWorker.cpp
@@ -188,16 +188,31 @@ template<> void JSWorkerDOMConstructor::initializeProperties(VM& vm, JSDOMGlobal
putDirect(vm, vm.propertyNames->prototype, JSWorker::prototype(vm, globalObject), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::DontDelete);
}
+JSC_DEFINE_CUSTOM_GETTER(jsWorker_threadIdGetter, (JSGlobalObject * lexicalGlobalObject, EncodedJSValue thisValue, PropertyName))
+{
+ auto* castedThis = jsDynamicCast<JSWorker*>(JSValue::decode(thisValue));
+ if (UNLIKELY(!castedThis))
+ return JSValue::encode(jsUndefined());
+
+ // Main thread starts at 1
+ // so we say it's 0
+ //
+ // Note that we cannot use posix thread ids here because we don't know their thread id until the thread starts
+ //
+ return JSValue::encode(jsNumber(castedThis->wrapped().clientIdentifier() - 1));
+}
+
/* Hash table for prototype */
static const HashTableValue JSWorkerPrototypeTableValues[] = {
{ "constructor"_s, static_cast<unsigned>(PropertyAttribute::DontEnum), NoIntrinsic, { HashTableValue::GetterSetterType, jsWorkerConstructor, 0 } },
+ { "onerror"_s, JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute, NoIntrinsic, { HashTableValue::GetterSetterType, jsWorker_onerror, setJSWorker_onerror } },
{ "onmessage"_s, JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute, NoIntrinsic, { HashTableValue::GetterSetterType, jsWorker_onmessage, setJSWorker_onmessage } },
{ "onmessageerror"_s, JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute, NoIntrinsic, { HashTableValue::GetterSetterType, jsWorker_onmessageerror, setJSWorker_onmessageerror } },
- { "onerror"_s, JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute, NoIntrinsic, { HashTableValue::GetterSetterType, jsWorker_onerror, setJSWorker_onerror } },
- { "terminate"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsWorkerPrototypeFunction_terminate, 0 } },
{ "postMessage"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsWorkerPrototypeFunction_postMessage, 1 } },
{ "ref"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsWorkerPrototypeFunction_ref, 0 } },
+ { "terminate"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsWorkerPrototypeFunction_terminate, 0 } },
+ { "threadId"_s, JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute, NoIntrinsic, { HashTableValue::GetterSetterType, jsWorker_threadIdGetter, nullptr } },
{ "unref"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsWorkerPrototypeFunction_unref, 0 } },
};
diff --git a/src/bun.js/bindings/webcore/Worker.h b/src/bun.js/bindings/webcore/Worker.h
index 76607d8da..7a6f0b032 100644
--- a/src/bun.js/bindings/webcore/Worker.h
+++ b/src/bun.js/bindings/webcore/Worker.h
@@ -96,6 +96,7 @@ public:
void dispatchError(WTF::String message);
void dispatchExit();
ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
+ ScriptExecutionContextIdentifier clientIdentifier() const { return m_clientIdentifier; }
private:
Worker(ScriptExecutionContext&, WorkerOptions&&);
diff --git a/test/js/web/worker.test.ts b/test/js/web/worker.test.ts
index 02d9af5d8..bc55e30fb 100644
--- a/test/js/web/worker.test.ts
+++ b/test/js/web/worker.test.ts
@@ -4,6 +4,7 @@ test("worker", done => {
const worker = new Worker(new URL("worker-fixture.js", import.meta.url).href, {
smol: true,
});
+ expect(worker.threadId).toBe(0);
worker.postMessage("hello");
worker.onerror = e => {
done(e.error);