Skip to content

Commit

Permalink
Fixed console.log(globalThis) crash
Browse files Browse the repository at this point in the history
  • Loading branch information
EngoDev committed Jan 2, 2025
1 parent 8620d14 commit 8415734
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/workerd/api/global-scope.c++
Original file line number Diff line number Diff line change
Expand Up @@ -884,16 +884,19 @@ double Performance::now() {
}

#ifdef WORKERD_EXPERIMENTAL_ENABLE_WEBGPU
jsg::Ref<api::gpu::GPU> Navigator::getGPU(CompatibilityFlags::Reader flags) {
jsg::Optional<jsg::Ref<api::gpu::GPU>> Navigator::getGPU(CompatibilityFlags::Reader flags) {
// is this a durable object?
KJ_IF_SOME(actor, IoContext::current().getActor()) {
JSG_REQUIRE(actor.getPersistent() != kj::none, TypeError,
"webgpu api is only available in Durable Objects (no storage)");
if (actor.getPersistent() == kj::none) {
return kj::none;
}
} else {
JSG_FAIL_REQUIRE(TypeError, "webgpu api is only available in Durable Objects");
return kj::none;
};

JSG_REQUIRE(flags.getWebgpu(), TypeError, "webgpu needs the webgpu compatibility flag set");
if (!flags.getWebgpu()) {
return kj::none;
}

return jsg::alloc<api::gpu::GPU>();
}
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/global-scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Navigator: public jsg::Object {
return "Cloudflare-Workers"_kj;
}
#ifdef WORKERD_EXPERIMENTAL_ENABLE_WEBGPU
jsg::Ref<api::gpu::GPU> getGPU(CompatibilityFlags::Reader flags);
jsg::Optional<jsg::Ref<api::gpu::GPU>> getGPU(CompatibilityFlags::Reader flags);
#endif

bool sendBeacon(jsg::Lock& js, kj::String url, jsg::Optional<Body::Initializer> body);
Expand Down
7 changes: 7 additions & 0 deletions src/workerd/api/tests/global-scope-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import {
deepStrictEqual,
strictEqual,
throws,
doesNotThrow,
notStrictEqual,
ok,
} from 'node:assert';

import { AsyncLocalStorage } from 'node:async_hooks';
import util from 'node:util';

export const navigatorUserAgent = {
async test() {
Expand Down Expand Up @@ -744,3 +746,8 @@ export const toStringTag = {
strictEqual(new Headers()[internalFlag], internalFlag);
},
};
export const validateGlobalThis = {
test() {
util.inspect(globalThis);
},
};

0 comments on commit 8415734

Please sign in to comment.