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

[browser] detect and assert engine features #88846

Merged
merged 5 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion src/mono/wasm/runtime/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

import { wrap_as_cancelable_promise } from "./cancelable-promise";
import { Module, loaderHelpers } from "./globals";
import { ENVIRONMENT_IS_NODE, Module, loaderHelpers } from "./globals";
import { MemoryViewType, Span } from "./marshal";
import type { VoidPtr } from "./types/emscripten";

Expand All @@ -11,6 +11,12 @@ export function http_wasm_supports_streaming_response(): boolean {
}

export function http_wasm_create_abort_controler(): AbortController {
if (typeof globalThis.fetch !== "function" || typeof globalThis.AbortController !== "function") {
const message = ENVIRONMENT_IS_NODE
? "Please install fetch package to enable HTTP client support."
: "This browser doesn't support fetch API. Please use a modern browser.";
throw new Error(message);
}
return new AbortController();
}

Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/loader/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

import type { DotnetHostBuilder } from "../types";
import { mono_exit } from "./exit";
import { sanityCheck } from "./polyfills";
import { HostBuilder, createEmscripten } from "./run";

// export external API
const dotnet: DotnetHostBuilder = new HostBuilder();
const exit = mono_exit;
const legacyEntrypoint = createEmscripten;

sanityCheck();

export { dotnet, exit };
export default legacyEntrypoint;
17 changes: 17 additions & 0 deletions src/mono/wasm/runtime/loader/polyfills.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import MonoWasmThreads from "consts:monoWasmThreads";

import type { DotnetModuleInternal } from "../types/internal";
import { INTERNAL, ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, loaderHelpers, ENVIRONMENT_IS_WEB } from "./globals";
Expand All @@ -14,6 +18,19 @@ const URLPolyfill = class URL {
}
};

export function sanityCheck() {
mono_assert(ENVIRONMENT_IS_SHELL || typeof globalThis.URL === "function", "This browser/engine doesn't support URL API. Please use a modern version.");
mono_assert(typeof globalThis.BigInt64Array === "function", "This browser/engine doesn't support BigInt64Array API. Please use a modern version.");
if (MonoWasmThreads) {
mono_assert(!ENVIRONMENT_IS_SHELL && !ENVIRONMENT_IS_NODE, "This build of dotnet is multi-threaded, it doesn't support shell environments like V8 or NodeJS.");
mono_assert(globalThis.SharedArrayBuffer !== undefined, "SharedArrayBuffer is not enabled on this page. Please use a modern browser and set Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy http headers. See also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer#security_requirements");
mono_assert(typeof globalThis.EventTarget === "function", "This browser/engine doesn't support EventTarget API. Please use a modern version.");
}

// TODO detect other (WASM) features that are required for the runtime
// See https://github.com/dotnet/runtime/issues/84574
}

export async function detect_features_and_polyfill(module: DotnetModuleInternal): Promise<void> {

const scriptUrlQuery =/* webpackIgnore: true */import.meta.url;
Expand Down
3 changes: 3 additions & 0 deletions src/mono/wasm/runtime/loader/worker.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { MonoConfig } from "../types";
import { MonoConfigInternal } from "../types/internal";
import { deep_merge_config, normalizeConfig } from "./config";
Expand Down
11 changes: 10 additions & 1 deletion src/mono/wasm/runtime/web-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import MonoWasmThreads from "consts:monoWasmThreads";

import { prevent_timer_throttling } from "./scheduling";
import { Queue } from "./queue";
import { createPromiseController } from "./globals";
import { ENVIRONMENT_IS_NODE, ENVIRONMENT_IS_SHELL, createPromiseController } from "./globals";
import { setI32, localHeapViewU8 } from "./memory";
import { VoidPtr } from "./types/emscripten";
import { PromiseController } from "./types/internal";
Expand All @@ -27,6 +27,15 @@ const ws_send_buffer_blocking_threshold = 65536;
const emptyBuffer = new Uint8Array();

export function ws_wasm_create(uri: string, sub_protocols: string[] | null, receive_status_ptr: VoidPtr, onClosed: (code: number, reason: string) => void): WebSocketExtension {
if (ENVIRONMENT_IS_SHELL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shoud we move it after the typeof globalThis.WebSocket !== "function" condition?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some random polyfill ? I don't think so.

Copy link
Member

@maraf maraf Jul 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for some polyfill, or if it happens to be added... What is the reason to be that restrictive?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has complex dynamic behavior and even the nodeJS polyfill doen't work well in all use-cases. I'm just loud and clear this is not supported.

throw new Error("WebSockets are not supported in shell JS engine.");
}
if (typeof globalThis.WebSocket !== "function") {
const message = ENVIRONMENT_IS_NODE
? "Please install WebSocket package to enable networking support."
: "This browser doesn't support WebSocket API. Please use a modern browser.";
throw new Error(message);
}
mono_assert(uri && typeof uri === "string", () => `ERR12: Invalid uri ${typeof uri}`);

const ws = new globalThis.WebSocket(uri, sub_protocols || undefined) as WebSocketExtension;
Expand Down