Skip to content

Commit 19252d6

Browse files
pavelsavaraTanayParikhSteveSandersonMS
authored
[wasm] dotnet.js modularization (#38721)
* - use import to load dotnet.js instead of html tag - updated createEmscriptenModuleInstance to use the new createDotnetRuntime() API - removed @types/emscripten type definitions - added src/Components/Web.JS/@types/dotnet temporarily, until we figure out how to pull this out of Microsoft.NETCore.App.Runtime.Mono.browser-wasm.7.0.0-dev.nupkg - fixed usages of addRunDependency, removeRunDependency - mapped System_Object and other types to MonoObject etc - global Module is no longer needed by runtime - therefore removed __wasmmodulecallback__ - removed few cwrap calls and replaced them with API usage * fix merge * private Co-authored-by: Tanay Parikh <[email protected]> * Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Tanay Parikh <[email protected]> * update dotnet.d.ts * fix build * Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Steve Sanderson <[email protected]> * feedback * implement SRI via link modulepreload * fix * fix * Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Steve Sanderson <[email protected]> * Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Steve Sanderson <[email protected]> * Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Steve Sanderson <[email protected]> * Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Steve Sanderson <[email protected]> * Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Steve Sanderson <[email protected]> * Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Steve Sanderson <[email protected]> * Update src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts Co-authored-by: Steve Sanderson <[email protected]> * feedback Co-authored-by: Tanay Parikh <[email protected]> Co-authored-by: Steve Sanderson <[email protected]>
1 parent 9b736b4 commit 19252d6

File tree

10 files changed

+654
-257
lines changed

10 files changed

+654
-257
lines changed
Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
1+
//! Licensed to the .NET Foundation under one or more agreements.
2+
//! The .NET Foundation licenses this file to you under the MIT license.
3+
//!
4+
//! This is generated file, see src/mono/wasm/runtime/rollup.config.js
5+
6+
//! This is not considered public API with backward compatibility guarantees.
7+
8+
declare interface ManagedPointer {
9+
__brandManagedPointer: "ManagedPointer";
10+
}
11+
declare interface NativePointer {
12+
__brandNativePointer: "NativePointer";
13+
}
14+
declare interface VoidPtr extends NativePointer {
15+
__brand: "VoidPtr";
16+
}
17+
declare interface CharPtr extends NativePointer {
18+
__brand: "CharPtr";
19+
}
20+
declare interface Int32Ptr extends NativePointer {
21+
__brand: "Int32Ptr";
22+
}
23+
declare interface EmscriptenModule {
24+
HEAP8: Int8Array;
25+
HEAP16: Int16Array;
26+
HEAP32: Int32Array;
27+
HEAPU8: Uint8Array;
28+
HEAPU16: Uint16Array;
29+
HEAPU32: Uint32Array;
30+
HEAPF32: Float32Array;
31+
HEAPF64: Float64Array;
32+
_malloc(size: number): VoidPtr;
33+
_free(ptr: VoidPtr): void;
34+
print(message: string): void;
35+
printErr(message: string): void;
36+
ccall<T>(ident: string, returnType?: string | null, argTypes?: string[], args?: any[], opts?: any): T;
37+
cwrap<T extends Function>(ident: string, returnType: string, argTypes?: string[], opts?: any): T;
38+
cwrap<T extends Function>(ident: string, ...args: any[]): T;
39+
setValue(ptr: VoidPtr, value: number, type: string, noSafe?: number | boolean): void;
40+
setValue(ptr: Int32Ptr, value: number, type: string, noSafe?: number | boolean): void;
41+
getValue(ptr: number, type: string, noSafe?: number | boolean): number;
42+
UTF8ToString(ptr: CharPtr, maxBytesToRead?: number): string;
43+
UTF8ArrayToString(u8Array: Uint8Array, idx?: number, maxBytesToRead?: number): string;
44+
FS_createPath(parent: string, path: string, canRead?: boolean, canWrite?: boolean): string;
45+
FS_createDataFile(parent: string, name: string, data: TypedArray, canRead: boolean, canWrite: boolean, canOwn?: boolean): string;
46+
removeRunDependency(id: string): void;
47+
addRunDependency(id: string): void;
48+
ready: Promise<unknown>;
49+
preInit?: (() => any)[];
50+
preRun?: (() => any)[];
51+
postRun?: (() => any)[];
52+
onAbort?: {
53+
(error: any): void;
54+
};
55+
onRuntimeInitialized?: () => any;
56+
instantiateWasm: (imports: any, successCallback: Function) => any;
57+
}
58+
declare type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array;
59+
60+
/**
61+
* Allocates a block of memory that can safely contain pointers into the managed heap.
62+
* The result object has get(index) and set(index, value) methods that can be used to retrieve and store managed pointers.
63+
* Once you are done using the root buffer, you must call its release() method.
64+
* For small numbers of roots, it is preferable to use the mono_wasm_new_root and mono_wasm_new_roots APIs instead.
65+
*/
66+
declare function mono_wasm_new_root_buffer(capacity: number, name?: string): WasmRootBuffer;
67+
/**
68+
* Allocates temporary storage for a pointer into the managed heap.
69+
* Pointers stored here will be visible to the GC, ensuring that the object they point to aren't moved or collected.
70+
* If you already have a managed pointer you can pass it as an argument to initialize the temporary storage.
71+
* The result object has get() and set(value) methods, along with a .value property.
72+
* When you are done using the root you must call its .release() method.
73+
*/
74+
declare function mono_wasm_new_root<T extends ManagedPointer | NativePointer>(value?: T | undefined): WasmRoot<T>;
75+
/**
76+
* Releases 1 or more root or root buffer objects.
77+
* Multiple objects may be passed on the argument list.
78+
* 'undefined' may be passed as an argument so it is safe to call this method from finally blocks
79+
* even if you are not sure all of your roots have been created yet.
80+
* @param {... WasmRoot} roots
81+
*/
82+
declare function mono_wasm_release_roots(...args: WasmRoot<any>[]): void;
83+
declare class WasmRootBuffer {
84+
private __count;
85+
private length;
86+
private __offset;
87+
private __offset32;
88+
private __handle;
89+
private __ownsAllocation;
90+
constructor(offset: VoidPtr, capacity: number, ownsAllocation: boolean, name?: string);
91+
_throw_index_out_of_range(): void;
92+
_check_in_range(index: number): void;
93+
get_address(index: number): NativePointer;
94+
get_address_32(index: number): number;
95+
get(index: number): ManagedPointer;
96+
set(index: number, value: ManagedPointer): ManagedPointer;
97+
_unsafe_get(index: number): number;
98+
_unsafe_set(index: number, value: ManagedPointer | NativePointer): void;
99+
clear(): void;
100+
release(): void;
101+
toString(): string;
102+
}
103+
declare class WasmRoot<T extends ManagedPointer | NativePointer> {
104+
private __buffer;
105+
private __index;
106+
constructor(buffer: WasmRootBuffer, index: number);
107+
get_address(): NativePointer;
108+
get_address_32(): number;
109+
get(): T;
110+
set(value: T): T;
111+
get value(): T;
112+
set value(value: T);
113+
valueOf(): T;
114+
clear(): void;
115+
release(): void;
116+
toString(): string;
117+
}
118+
119+
interface MonoObject extends ManagedPointer {
120+
__brandMonoObject: "MonoObject";
121+
}
122+
interface MonoString extends MonoObject {
123+
__brand: "MonoString";
124+
}
125+
interface MonoArray extends MonoObject {
126+
__brand: "MonoArray";
127+
}
128+
declare type MonoConfig = {
129+
isError: false;
130+
assembly_root: string;
131+
assets: AllAssetEntryTypes[];
132+
debug_level?: number;
133+
enable_debugging?: number;
134+
globalization_mode: GlobalizationMode;
135+
diagnostic_tracing?: boolean;
136+
remote_sources?: string[];
137+
environment_variables?: {
138+
[i: string]: string;
139+
};
140+
runtime_options?: string[];
141+
aot_profiler_options?: AOTProfilerOptions;
142+
coverage_profiler_options?: CoverageProfilerOptions;
143+
ignore_pdb_load_errors?: boolean;
144+
};
145+
declare type MonoConfigError = {
146+
isError: true;
147+
message: string;
148+
error: any;
149+
};
150+
declare type AllAssetEntryTypes = AssetEntry | AssemblyEntry | SatelliteAssemblyEntry | VfsEntry | IcuData;
151+
declare type AssetEntry = {
152+
name: string;
153+
behavior: AssetBehaviours;
154+
virtual_path?: string;
155+
culture?: string;
156+
load_remote?: boolean;
157+
is_optional?: boolean;
158+
};
159+
interface AssemblyEntry extends AssetEntry {
160+
name: "assembly";
161+
}
162+
interface SatelliteAssemblyEntry extends AssetEntry {
163+
name: "resource";
164+
culture: string;
165+
}
166+
interface VfsEntry extends AssetEntry {
167+
name: "vfs";
168+
virtual_path: string;
169+
}
170+
interface IcuData extends AssetEntry {
171+
name: "icu";
172+
load_remote: boolean;
173+
}
174+
declare const enum AssetBehaviours {
175+
Resource = "resource",
176+
Assembly = "assembly",
177+
Heap = "heap",
178+
ICU = "icu",
179+
VFS = "vfs"
180+
}
181+
declare const enum GlobalizationMode {
182+
ICU = "icu",
183+
INVARIANT = "invariant",
184+
AUTO = "auto"
185+
}
186+
declare type AOTProfilerOptions = {
187+
write_at?: string;
188+
send_to?: string;
189+
};
190+
declare type CoverageProfilerOptions = {
191+
write_at?: string;
192+
send_to?: string;
193+
};
194+
declare type DotnetModuleConfig = {
195+
disableDotnet6Compatibility?: boolean;
196+
config?: MonoConfig | MonoConfigError;
197+
configSrc?: string;
198+
scriptDirectory?: string;
199+
onConfigLoaded?: () => void;
200+
onDotnetReady?: () => void;
201+
imports?: DotnetModuleConfigImports;
202+
} & Partial<EmscriptenModule>;
203+
declare type DotnetModuleConfigImports = {
204+
require?: (name: string) => any;
205+
fetch?: (url: string) => Promise<Response>;
206+
fs?: {
207+
promises?: {
208+
readFile?: (path: string) => Promise<string | Buffer>;
209+
};
210+
readFileSync?: (path: string, options: any | undefined) => string;
211+
};
212+
crypto?: {
213+
randomBytes?: (size: number) => Buffer;
214+
};
215+
ws?: WebSocket & {
216+
Server: any;
217+
};
218+
path?: {
219+
normalize?: (path: string) => string;
220+
dirname?: (path: string) => string;
221+
};
222+
url?: any;
223+
};
224+
225+
declare function mono_wasm_runtime_ready(): void;
226+
227+
declare function mono_wasm_setenv(name: string, value: string): void;
228+
declare function mono_load_runtime_and_bcl_args(config: MonoConfig | MonoConfigError | undefined): Promise<void>;
229+
declare function mono_wasm_load_data_archive(data: Uint8Array, prefix: string): boolean;
230+
/**
231+
* Loads the mono config file (typically called mono-config.json) asynchroniously
232+
* Note: the run dependencies are so emsdk actually awaits it in order.
233+
*
234+
* @param {string} configFilePath - relative path to the config file
235+
* @throws Will throw an error if the config file loading fails
236+
*/
237+
declare function mono_wasm_load_config(configFilePath: string): Promise<void>;
238+
239+
declare function mono_wasm_load_icu_data(offset: VoidPtr): boolean;
240+
241+
declare function conv_string(mono_obj: MonoString): string | null;
242+
declare function js_string_to_mono_string(string: string): MonoString;
243+
244+
declare function js_to_mono_obj(js_obj: any): MonoObject;
245+
declare function js_typed_array_to_array(js_obj: any): MonoArray;
246+
247+
declare function unbox_mono_obj(mono_obj: MonoObject): any;
248+
declare function mono_array_to_js_array(mono_array: MonoArray): any[] | null;
249+
250+
declare function mono_bind_static_method(fqn: string, signature?: string): Function;
251+
declare function mono_call_assembly_entry_point(assembly: string, args?: any[], signature?: string): number;
252+
253+
declare function mono_wasm_load_bytes_into_heap(bytes: Uint8Array): VoidPtr;
254+
255+
declare type _MemOffset = number | VoidPtr | NativePointer;
256+
declare function setU8(offset: _MemOffset, value: number): void;
257+
declare function setU16(offset: _MemOffset, value: number): void;
258+
declare function setU32(offset: _MemOffset, value: number): void;
259+
declare function setI8(offset: _MemOffset, value: number): void;
260+
declare function setI16(offset: _MemOffset, value: number): void;
261+
declare function setI32(offset: _MemOffset, value: number): void;
262+
declare function setI64(offset: _MemOffset, value: number): void;
263+
declare function setF32(offset: _MemOffset, value: number): void;
264+
declare function setF64(offset: _MemOffset, value: number): void;
265+
declare function getU8(offset: _MemOffset): number;
266+
declare function getU16(offset: _MemOffset): number;
267+
declare function getU32(offset: _MemOffset): number;
268+
declare function getI8(offset: _MemOffset): number;
269+
declare function getI16(offset: _MemOffset): number;
270+
declare function getI32(offset: _MemOffset): number;
271+
declare function getI64(offset: _MemOffset): number;
272+
declare function getF32(offset: _MemOffset): number;
273+
declare function getF64(offset: _MemOffset): number;
274+
275+
declare function mono_run_main_and_exit(main_assembly_name: string, args: string[]): Promise<void>;
276+
declare function mono_run_main(main_assembly_name: string, args: string[]): Promise<number>;
277+
278+
declare const MONO: {
279+
mono_wasm_setenv: typeof mono_wasm_setenv;
280+
mono_wasm_load_bytes_into_heap: typeof mono_wasm_load_bytes_into_heap;
281+
mono_wasm_load_icu_data: typeof mono_wasm_load_icu_data;
282+
mono_wasm_runtime_ready: typeof mono_wasm_runtime_ready;
283+
mono_wasm_load_data_archive: typeof mono_wasm_load_data_archive;
284+
mono_wasm_load_config: typeof mono_wasm_load_config;
285+
mono_load_runtime_and_bcl_args: typeof mono_load_runtime_and_bcl_args;
286+
mono_wasm_new_root_buffer: typeof mono_wasm_new_root_buffer;
287+
mono_wasm_new_root: typeof mono_wasm_new_root;
288+
mono_wasm_release_roots: typeof mono_wasm_release_roots;
289+
mono_run_main: typeof mono_run_main;
290+
mono_run_main_and_exit: typeof mono_run_main_and_exit;
291+
mono_wasm_add_assembly: (name: string, data: VoidPtr, size: number) => number;
292+
mono_wasm_load_runtime: (unused: string, debug_level: number) => void;
293+
config: MonoConfig | MonoConfigError;
294+
loaded_files: string[];
295+
setI8: typeof setI8;
296+
setI16: typeof setI16;
297+
setI32: typeof setI32;
298+
setI64: typeof setI64;
299+
setU8: typeof setU8;
300+
setU16: typeof setU16;
301+
setU32: typeof setU32;
302+
setF32: typeof setF32;
303+
setF64: typeof setF64;
304+
getI8: typeof getI8;
305+
getI16: typeof getI16;
306+
getI32: typeof getI32;
307+
getI64: typeof getI64;
308+
getU8: typeof getU8;
309+
getU16: typeof getU16;
310+
getU32: typeof getU32;
311+
getF32: typeof getF32;
312+
getF64: typeof getF64;
313+
};
314+
declare type MONOType = typeof MONO;
315+
declare const BINDING: {
316+
mono_obj_array_new: (size: number) => MonoArray;
317+
mono_obj_array_set: (array: MonoArray, idx: number, obj: MonoObject) => void;
318+
js_string_to_mono_string: typeof js_string_to_mono_string;
319+
js_typed_array_to_array: typeof js_typed_array_to_array;
320+
js_to_mono_obj: typeof js_to_mono_obj;
321+
mono_array_to_js_array: typeof mono_array_to_js_array;
322+
conv_string: typeof conv_string;
323+
bind_static_method: typeof mono_bind_static_method;
324+
call_assembly_entry_point: typeof mono_call_assembly_entry_point;
325+
unbox_mono_obj: typeof unbox_mono_obj;
326+
};
327+
declare type BINDINGType = typeof BINDING;
328+
interface DotnetPublicAPI {
329+
MONO: typeof MONO;
330+
BINDING: typeof BINDING;
331+
INTERNAL: any;
332+
Module: EmscriptenModule;
333+
RuntimeId: number;
334+
RuntimeBuildInfo: {
335+
ProductVersion: string;
336+
Configuration: string;
337+
};
338+
}
339+
340+
declare function createDotnetRuntime(moduleFactory: DotnetModuleConfig | ((api: DotnetPublicAPI) => DotnetModuleConfig)): Promise<DotnetPublicAPI>;
341+
declare type CreateDotnetRuntimeType = typeof createDotnetRuntime;
342+
declare global {
343+
function getDotnetRuntime(runtimeId: number): DotnetPublicAPI | undefined;
344+
}
345+
346+
export { BINDINGType, CreateDotnetRuntimeType, DotnetModuleConfig, DotnetPublicAPI, EmscriptenModule, MONOType, MonoArray, MonoObject, MonoString, VoidPtr, createDotnetRuntime as default };
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "dotnet-runtime",
3+
"private": true,
4+
"description": ".NET is a developer platform with tools and libraries for building any type of app, including web, mobile, desktop, games, IoT, cloud, and microservices.",
5+
"repository": {
6+
"type": "git",
7+
"url": "[email protected]:dotnet/runtime.git"
8+
},
9+
"version": "1.0.0",
10+
"main": "dotnet.js",
11+
"types": "dotnet.d.ts",
12+
"scripts": {
13+
"rollup": "rollup -c",
14+
"lint": "eslint --no-color --max-warnings=0 ./**/*.ts ./*.js"
15+
},
16+
"keywords": [
17+
"dotnet",
18+
"runtime",
19+
"wasm"
20+
],
21+
"author": "Microsoft",
22+
"license": "MIT",
23+
"devDependencies": {
24+
"@rollup/plugin-typescript": "8.2.5",
25+
"@typescript-eslint/eslint-plugin": "4.31.2",
26+
"@typescript-eslint/parser": "4.31.2",
27+
"eslint": "7.32.0",
28+
"rollup": "2.56.3",
29+
"rollup-plugin-consts": "1.0.2",
30+
"rollup-plugin-dts": "4.0.0",
31+
"rollup-plugin-terser": "7.0.2",
32+
"tslib": "2.3.1",
33+
"typescript": "4.4.3"
34+
}
35+
}

src/Components/Web.JS/dist/Release/blazor.server.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/dist/Release/blazor.webview.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Components/Web.JS/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@microsoft/dotnet-js-interop": "link:../../JSInterop/Microsoft.JSInterop.JS/src",
2525
"@microsoft/signalr": "link:../../SignalR/clients/ts/signalr",
2626
"@microsoft/signalr-protocol-msgpack": "link:../../SignalR/clients/ts/signalr-protocol-msgpack",
27-
"@types/emscripten": "^1.39.5",
27+
"@types/dotnet": "./@types/dotnet",
2828
"@types/jest": "^27.0.1",
2929
"@types/jsdom": "^16.2.13",
3030
"@typescript-eslint/eslint-plugin": "^4.29.3",

0 commit comments

Comments
 (0)