Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix console.log(globalThis) crash #3175 #3267

Merged
merged 2 commits into from
Feb 14, 2025
Merged
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
13 changes: 8 additions & 5 deletions src/workerd/api/global-scope.c++
Original file line number Diff line number Diff line change
Expand Up @@ -880,16 +880,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);
},
};
2 changes: 1 addition & 1 deletion types/generated-snapshot/2022-03-21/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2022-03-21/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2022-08-04/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2022-08-04/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2022-10-31/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2022-10-31/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2022-11-30/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2022-11-30/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ export declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2023-03-01/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2023-03-01/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ export declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2023-07-01/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/2023-07-01/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ export declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/experimental/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
2 changes: 1 addition & 1 deletion types/generated-snapshot/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export declare abstract class Navigator {
| URLSearchParams,
): boolean;
readonly userAgent: string;
readonly gpu: GPU;
readonly gpu?: GPU;
}
/**
* The Workers runtime supports a subset of the Performance API, used to measure timing and performance,
Expand Down
Loading