Skip to content

Commit

Permalink
Fixes #4588
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner committed Sep 10, 2023
1 parent afcbed2 commit d8937df
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/bun.js/bindings/ZigGlobalObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3075,11 +3075,21 @@ void GlobalObject::finishCreation(VM& vm)
JSC::Identifier userAgentIdentifier = JSC::Identifier::fromString(init.vm, "userAgent"_s);
JSC::Identifier hardwareConcurrencyIdentifier = JSC::Identifier::fromString(init.vm, "hardwareConcurrency"_s);

JSC::JSObject* obj = JSC::constructEmptyObject(init.owner, init.owner->objectPrototype(), 3);
JSC::JSObject* obj = JSC::constructEmptyObject(init.owner, init.owner->objectPrototype(), 4);
obj->putDirect(init.vm, userAgentIdentifier, JSC::jsString(init.vm, str));
obj->putDirect(init.vm, init.vm.propertyNames->toStringTagSymbol,
jsNontrivialString(init.vm, "Navigator"_s), JSC::PropertyAttribute::DontEnum | JSC::PropertyAttribute::ReadOnly);

// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform
// https://github.com/oven-sh/bun/issues/4588
#if OS(DARWIN)
obj->putDirect(init.vm, JSC::Identifier::fromString(init.vm, "platform"_s), JSC::jsString(init.vm, "MacIntel"_s));
#elif OS(WINDOWS)
obj->putDirect(init.vm, JSC::Identifier::fromString(init.vm, "platform"_s), JSC::jsString(init.vm, "Win32"_s));
#else if OS(LINUX)
obj->putDirect(init.vm, JSC::Identifier::fromString(init.vm, "platform"_s), JSC::jsString(init.vm, "Linux x86_64"_s));
#endif

obj->putDirect(init.vm, hardwareConcurrencyIdentifier, JSC::jsNumber(cpuCount));
init.set(
obj);
Expand Down
7 changes: 7 additions & 0 deletions test/js/web/web-globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,11 @@ test("navigator", () => {
const userAgent = `Bun/${version}`;
expect(navigator.hardwareConcurrency > 0).toBe(true);
expect(navigator.userAgent).toBe(userAgent);
if (process.platform === "darwin") {
expect(navigator.platform).toBe("MacIntel");
} else if (process.platform === "win32") {
expect(navigator.platform).toBe("Win32");
} else if (process.platform === "linux") {
expect(navigator.platform).toBe("Linux");
}
});

0 comments on commit d8937df

Please sign in to comment.