From 8415734f577e6685a735dc9c84773b4482c80a1c Mon Sep 17 00:00:00 2001 From: EngoDev Date: Tue, 24 Dec 2024 01:07:08 +0000 Subject: [PATCH] Fixed console.log(globalThis) crash --- src/workerd/api/global-scope.c++ | 13 ++++++++----- src/workerd/api/global-scope.h | 2 +- src/workerd/api/tests/global-scope-test.js | 7 +++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/workerd/api/global-scope.c++ b/src/workerd/api/global-scope.c++ index cfee9034790..cc12a31f92f 100644 --- a/src/workerd/api/global-scope.c++ +++ b/src/workerd/api/global-scope.c++ @@ -884,16 +884,19 @@ double Performance::now() { } #ifdef WORKERD_EXPERIMENTAL_ENABLE_WEBGPU -jsg::Ref Navigator::getGPU(CompatibilityFlags::Reader flags) { +jsg::Optional> 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(); } diff --git a/src/workerd/api/global-scope.h b/src/workerd/api/global-scope.h index a8ed3f5f565..c39961e4e2c 100644 --- a/src/workerd/api/global-scope.h +++ b/src/workerd/api/global-scope.h @@ -74,7 +74,7 @@ class Navigator: public jsg::Object { return "Cloudflare-Workers"_kj; } #ifdef WORKERD_EXPERIMENTAL_ENABLE_WEBGPU - jsg::Ref getGPU(CompatibilityFlags::Reader flags); + jsg::Optional> getGPU(CompatibilityFlags::Reader flags); #endif bool sendBeacon(jsg::Lock& js, kj::String url, jsg::Optional body); diff --git a/src/workerd/api/tests/global-scope-test.js b/src/workerd/api/tests/global-scope-test.js index 3fa2884b7ec..37858e920ef 100644 --- a/src/workerd/api/tests/global-scope-test.js +++ b/src/workerd/api/tests/global-scope-test.js @@ -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() { @@ -744,3 +746,8 @@ export const toStringTag = { strictEqual(new Headers()[internalFlag], internalFlag); }, }; +export const validateGlobalThis = { + test() { + util.inspect(globalThis); + }, +};