@@ -10,55 +10,22 @@ import { showErrorNotification } from '../../BootErrors';
1010import { Platform , System_Array , Pointer , System_Object , System_String , HeapLock , PlatformApi } from '../Platform' ;
1111import { WebAssemblyBootResourceType , WebAssemblyStartOptions } from '../WebAssemblyStartOptions' ;
1212import { Blazor } from '../../GlobalExports' ;
13- import { DotnetModuleConfig , EmscriptenModule , MonoConfig , ModuleAPI , RuntimeAPI , GlobalizationMode } from 'dotnet-runtime' ;
14- import { BINDINGType , MONOType } from 'dotnet-runtime/dotnet-legacy' ;
13+ import { DotnetModuleConfig , MonoConfig , ModuleAPI , RuntimeAPI , GlobalizationMode } from 'dotnet-runtime' ;
1514import { fetchAndInvokeInitializers } from '../../JSInitializers/JSInitializers.WebAssembly' ;
1615import { JSInitializer } from '../../JSInitializers/JSInitializers' ;
17- import { WebRendererId } from '../../Rendering/WebRendererId' ;
1816
1917// initially undefined and only fully initialized after createEmscriptenModuleInstance()
20- export let BINDING : BINDINGType = undefined as any ;
21- export let MONO : MONOType = undefined as any ;
22- export let Module : DotnetModuleConfig & EmscriptenModule = undefined as any ;
2318export let dispatcher : DotNet . ICallDispatcher = undefined as any ;
2419let MONO_INTERNAL : any = undefined as any ;
2520let runtime : RuntimeAPI = undefined as any ;
2621let jsInitializer : JSInitializer ;
2722
28- const uint64HighOrderShift = Math . pow ( 2 , 32 ) ;
29- const maxSafeNumberHighPart = Math . pow ( 2 , 21 ) - 1 ; // The high-order int32 from Number.MAX_SAFE_INTEGER
30-
3123let currentHeapLock : MonoHeapLock | null = null ;
3224
33- // Memory access helpers
34- // The implementations are exactly equivalent to what the global getValue(addr, type) function does,
35- // except without having to parse the 'type' parameter, and with less risk of mistakes at the call site
36- function getValueI16 ( ptr : number ) {
37- return MONO . getI16 ( ptr ) ;
38- }
39- function getValueI32 ( ptr : number ) {
40- return MONO . getI32 ( ptr ) ;
41- }
42- function getValueFloat ( ptr : number ) {
43- return MONO . getF32 ( ptr ) ;
44- }
45-
4625export function getInitializer ( ) {
4726 return jsInitializer ;
4827}
4928
50- function getValueU64 ( ptr : number ) {
51- // There is no Module.HEAPU64, and Module.getValue(..., 'i64') doesn't work because the implementation
52- // treats 'i64' as being the same as 'i32'. Also we must take care to read both halves as unsigned.
53- const heapU32Index = ptr >> 2 ;
54- const highPart = Module . HEAPU32 [ heapU32Index + 1 ] ;
55- if ( highPart > maxSafeNumberHighPart ) {
56- throw new Error ( `Cannot read uint64 with high order part ${ highPart } , because the result would exceed Number.MAX_SAFE_INTEGER.` ) ;
57- }
58-
59- return ( highPart * uint64HighOrderShift ) + Module . HEAPU32 [ heapU32Index ] ;
60- }
61-
6229export const monoPlatform : Platform = {
6330 load : function load ( options : Partial < WebAssemblyStartOptions > , onConfigLoaded ?: ( loadedConfig : MonoConfig ) => void ) {
6431 return createRuntimeInstance ( options , onConfigLoaded ) ;
@@ -77,18 +44,6 @@ export const monoPlatform: Platform = {
7744 }
7845 } ,
7946
80- toUint8Array : function toUint8Array ( array : System_Array < any > ) : Uint8Array {
81- const dataPtr = getArrayDataPointer ( array ) ;
82- const length = getValueI32 ( dataPtr ) ;
83- const uint8Array = new Uint8Array ( length ) ;
84- uint8Array . set ( Module . HEAPU8 . subarray ( dataPtr + 4 , dataPtr + 4 + length ) ) ;
85- return uint8Array ;
86- } ,
87-
88- getArrayLength : function getArrayLength ( array : System_Array < any > ) : number {
89- return getValueI32 ( getArrayDataPointer ( array ) ) ;
90- } ,
91-
9247 getArrayEntryPtr : function getArrayEntryPtr < TPtr extends Pointer > ( array : System_Array < TPtr > , index : number , itemSize : number ) : TPtr {
9348 // First byte is array length, followed by entries
9449 const address = getArrayDataPointer ( array ) + 4 + index * itemSize ;
@@ -97,46 +52,42 @@ export const monoPlatform: Platform = {
9752
9853 getObjectFieldsBaseAddress : function getObjectFieldsBaseAddress ( referenceTypedObject : System_Object ) : Pointer {
9954 // The first two int32 values are internal Mono data
100- return ( referenceTypedObject as any as number + 8 ) as any as Pointer ;
55+ return ( referenceTypedObject as any + 8 ) as any as Pointer ;
10156 } ,
10257
10358 readInt16Field : function readHeapInt16 ( baseAddress : Pointer , fieldOffset ?: number ) : number {
104- return getValueI16 ( ( baseAddress as any as number ) + ( fieldOffset || 0 ) ) ;
59+ return runtime . getHeapI16 ( ( baseAddress as any ) + + ( fieldOffset || 0 ) ) ;
10560 } ,
10661
10762 readInt32Field : function readHeapInt32 ( baseAddress : Pointer , fieldOffset ?: number ) : number {
108- return getValueI32 ( ( baseAddress as unknown as number ) + ( fieldOffset || 0 ) ) ;
63+ return runtime . getHeapI32 ( ( baseAddress as any ) + + ( fieldOffset || 0 ) ) ;
10964 } ,
11065
11166 readUint64Field : function readHeapUint64 ( baseAddress : Pointer , fieldOffset ?: number ) : number {
112- return getValueU64 ( ( baseAddress as unknown as number ) + ( fieldOffset || 0 ) ) ;
113- } ,
114-
115- readFloatField : function readHeapFloat ( baseAddress : Pointer , fieldOffset ?: number ) : number {
116- return getValueFloat ( ( baseAddress as unknown as number ) + ( fieldOffset || 0 ) ) ;
67+ return runtime . getHeapU52 ( ( baseAddress as any ) + + ( fieldOffset || 0 ) ) ;
11768 } ,
11869
119- readObjectField : function readHeapObject < T extends System_Object > ( baseAddress : Pointer , fieldOffset ?: number ) : T {
120- return getValueI32 ( ( baseAddress as unknown as number ) + ( fieldOffset || 0 ) ) as any as T ;
70+ readObjectField : function readObjectField < T extends System_Object > ( baseAddress : Pointer , fieldOffset ?: number ) : T {
71+ return runtime . getHeapU32 ( ( baseAddress as any ) + ( fieldOffset || 0 ) ) as any as T ;
12172 } ,
12273
123- readStringField : function readHeapObject ( baseAddress : Pointer , fieldOffset ?: number , readBoolValueAsString ?: boolean ) : string | null {
124- const fieldValue = getValueI32 ( ( baseAddress as unknown as number ) + ( fieldOffset || 0 ) ) ;
74+ readStringField : function readStringField ( baseAddress : Pointer , fieldOffset ?: number , readBoolValueAsString ?: boolean ) : string | null {
75+ const fieldValue = runtime . getHeapU32 ( ( baseAddress as any ) + ( fieldOffset || 0 ) ) ;
12576 if ( fieldValue === 0 ) {
12677 return null ;
12778 }
12879
12980 if ( readBoolValueAsString ) {
13081 // Some fields are stored as a union of bool | string | null values, but need to read as a string.
13182 // If the stored value is a bool, the behavior we want is empty string ('') for true, or null for false.
132- const unboxedValue = BINDING . unbox_mono_obj ( fieldValue as any as System_Object ) ;
83+
84+ const unboxedValue = MONO_INTERNAL . monoObjectAsBoolOrNullUnsafe ( fieldValue as any as System_Object ) ;
13385 if ( typeof ( unboxedValue ) === 'boolean' ) {
13486 return unboxedValue ? '' : null ;
13587 }
136- return unboxedValue ;
13788 }
13889
139- return BINDING . conv_string ( fieldValue as any as System_String ) ;
90+ return MONO_INTERNAL . monoStringToStringUnsafe ( fieldValue as any as System_String ) ;
14091 } ,
14192
14293 readStructField : function readStructField < T extends Pointer > ( baseAddress : Pointer , fieldOffset ?: number ) : T {
@@ -206,13 +157,12 @@ function prepareRuntimeConfig(options: Partial<WebAssemblyStartOptions>, onConfi
206157 jsInitializer = await fetchAndInvokeInitializers ( options , loadedConfig ) ;
207158 } ;
208159
209- const moduleConfig = ( window [ 'Module' ] || { } ) as typeof Module ;
160+ const moduleConfig = ( window [ 'Module' ] || { } ) as any ;
210161 const dotnetModuleConfig : DotnetModuleConfig = {
211162 ...moduleConfig ,
212163 onConfigLoaded : ( onConfigLoaded as ( config : MonoConfig ) => void | Promise < void > ) ,
213164 onDownloadResourceProgress : setProgress ,
214165 config,
215- disableDotnet6Compatibility : false ,
216166 out : print ,
217167 err : printErr ,
218168 } ;
@@ -251,10 +201,7 @@ async function configureRuntimeInstance(): Promise<PlatformApi> {
251201 throw new Error ( 'The runtime must be loaded it gets configured.' ) ;
252202 }
253203
254- const { MONO : mono , BINDING : binding , Module : module , setModuleImports, INTERNAL : mono_internal , getConfig, invokeLibraryInitializers } = runtime ;
255- Module = module ;
256- BINDING = binding ;
257- MONO = mono ;
204+ const { setModuleImports, INTERNAL : mono_internal , getConfig, invokeLibraryInitializers } = runtime ;
258205 MONO_INTERNAL = mono_internal ;
259206
260207 attachDebuggerHotkey ( getConfig ( ) ) ;
@@ -285,7 +232,7 @@ function setProgress(resourcesLoaded, totalResources) {
285232
286233const suppressMessages = [ 'DEBUGGING ENABLED' ] ;
287234const print = line => ( suppressMessages . indexOf ( line ) < 0 && console . log ( line ) ) ;
288- const printErr = line => {
235+ export const printErr = line => {
289236 // If anything writes to stderr, treat it as a critical exception. The underlying runtime writes
290237 // to stderr if a truly critical problem occurs outside .NET code. Note that .NET unhandled
291238 // exceptions also reach this, but via a different code path - see dotNetCriticalError below.
0 commit comments