Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions test-app/app/src/main/assets/app/tests/testMessaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,52 @@ describe("Messaging runtime edges", function () {
});
});

describe("environment data keys", function () {
it("keeps keys containing NUL distinct from their prefixes", function () {
var wt = require("node:worker_threads");
var prefix = "environment-key";
var key = prefix + "\0suffix";
try {
wt.setEnvironmentData(prefix, "prefix");
wt.setEnvironmentData(key, "full key");
expect(wt.getEnvironmentData(prefix)).toBe("prefix");
expect(wt.getEnvironmentData(key)).toBe("full key");
wt.setEnvironmentData(key);
expect(wt.getEnvironmentData(key)).toBeUndefined();
expect(wt.getEnvironmentData(prefix)).toBe("prefix");
} finally {
wt.setEnvironmentData(key);
wt.setEnvironmentData(prefix);
}
});

function thrownMessage(fn) {
try {
fn();
} catch (e) {
return e && e.message;
}
return "nothing thrown";
}

it("throws what a key's toString throws instead of using the empty-string key", function () {
var wt = require("node:worker_threads");
var key = { toString: function () { throw new Error("key toString failed"); } };
wt.setEnvironmentData("", "empty");
try {
expect(thrownMessage(function () { wt.setEnvironmentData(key, "other"); }))
.toBe("key toString failed");
expect(thrownMessage(function () { wt.getEnvironmentData(key); }))
.toBe("key toString failed");
expect(thrownMessage(function () { wt.setEnvironmentData(key); }))
.toBe("key toString failed");
expect(wt.getEnvironmentData("")).toBe("empty");
} finally {
wt.setEnvironmentData("");
}
});
});

describe("worker error reporting", function () {
// A worker boots on its own thread, so the first error arrives whenever
// the runner gets to it; specs wait for it and only then settle for
Expand Down
21 changes: 19 additions & 2 deletions test-app/runtime/src/main/cpp/Messaging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -935,13 +935,27 @@ void SetEmitMessageCallback(const FunctionCallbackInfo<Value>& info) {
state->emitMessage.Reset(isolate, info[0].As<v8::Function>());
}

// The key as the string the store files it under. A key whose conversion throws
// leaves that exception pending for the caller rather than standing in for "".
static bool EnvironmentDataKey(Isolate* isolate, Local<Value> value, std::string& key) {
Local<v8::String> str;
if (!value->ToString(isolate->GetCurrentContext()).ToLocal(&str)) {
return false;
}
key = ArgConverter::ToString(isolate, str);
return true;
}

void SetEnvironmentDataCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
if (info.Length() < 1) {
return;
}
Local<Context> context = isolate->GetCurrentContext();
std::string key = ArgConverter::ToString(isolate, info[0]);
std::string key;
if (!EnvironmentDataKey(isolate, info[0], key)) {
return;
}
if (info.Length() < 2 || info[1]->IsUndefined()) {
std::lock_guard<std::mutex> lock(g_environmentDataMutex);
g_environmentData.erase(key);
Expand All @@ -964,7 +978,10 @@ void GetEnvironmentDataCallback(const FunctionCallbackInfo<Value>& info) {
if (info.Length() < 1) {
return;
}
std::string key = ArgConverter::ToString(isolate, info[0]);
std::string key;
if (!EnvironmentDataKey(isolate, info[0], key)) {
return;
}
std::shared_ptr<serialization::SerializedValue> stored;
{
std::lock_guard<std::mutex> lock(g_environmentDataMutex);
Expand Down
Loading