11// Licensed to the .NET Foundation under one or more agreements. 
22// The .NET Foundation licenses this file to you under the MIT license. 
33
4+ import  MonoWasmThreads  from  "consts:monoWasmThreads" ; 
5+ import  WasmEnableLegacyJsInterop  from  "consts:wasmEnableLegacyJsInterop" ; 
6+ 
47import  type  { 
58    MonoArray ,  MonoAssembly ,  MonoClass , 
69    MonoMethod ,  MonoObject , 
710    MonoType ,  MonoObjectRef ,  MonoStringRef ,  JSMarshalerArguments 
811}  from  "./types/internal" ; 
912import  type  {  VoidPtr ,  CharPtrPtr ,  Int32Ptr ,  CharPtr ,  ManagedPointer  }  from  "./types/emscripten" ; 
10- import  WasmEnableLegacyJsInterop  from  "consts:WasmEnableLegacyJsInterop" ; 
11- import  {  disableLegacyJsInterop ,  Module  }  from  "./globals" ; 
13+ import  {  linkerDisableLegacyJsInterop ,  linkerEnableAotProfiler ,  linkerEnableBrowserProfiler ,  Module  }  from  "./globals" ; 
1214import  {  mono_log_error  }  from  "./logging" ; 
1315
14- type  SigLine  =  [ lazy : boolean ,  name : string ,  returnType : string  |  null ,  argTypes ?: string [ ] ,  opts ?: any ] ; 
16+ type  SigLine  =  [ lazyOrSkip : boolean   |   ( ( )   =>   boolean ) ,  name : string ,  returnType : string  |  null ,  argTypes ?: string [ ] ,  opts ?: any ] ; 
1517
1618const  legacy_interop_cwraps : SigLine [ ]  =  WasmEnableLegacyJsInterop  ? [ 
1719    [ true ,  "mono_wasm_array_get_ref" ,  "void" ,  [ "number" ,  "number" ,  "number" ] ] , 
@@ -29,6 +31,17 @@ const legacy_interop_cwraps: SigLine[] = WasmEnableLegacyJsInterop ? [
2931    [ true ,  "mono_wasm_array_length_ref" ,  "number" ,  [ "number" ] ] , 
3032]  : [ ] ; 
3133
34+ const  diagnostics_cwraps : SigLine [ ]  =  MonoWasmThreads  ? [ 
35+     // MONO.diagnostics 
36+     [ true ,  "mono_wasm_event_pipe_enable" ,  "bool" ,  [ "string" ,  "number" ,  "number" ,  "string" ,  "bool" ,  "number" ] ] , 
37+     [ true ,  "mono_wasm_event_pipe_session_start_streaming" ,  "bool" ,  [ "number" ] ] , 
38+     [ true ,  "mono_wasm_event_pipe_session_disable" ,  "bool" ,  [ "number" ] ] , 
39+     [ true ,  "mono_wasm_diagnostic_server_create_thread" ,  "bool" ,  [ "string" ,  "number" ] ] , 
40+     [ true ,  "mono_wasm_diagnostic_server_thread_attach_to_runtime" ,  "void" ,  [ ] ] , 
41+     [ true ,  "mono_wasm_diagnostic_server_post_resume_runtime" ,  "void" ,  [ ] ] , 
42+     [ true ,  "mono_wasm_diagnostic_server_create_stream" ,  "number" ,  [ ] ] , 
43+ ]  : [ ] ; 
44+ 
3245// when the method is assigned/cached at usage, instead of being invoked directly from cwraps, it can't be marked lazy, because it would be re-bound on each call 
3346const  fn_signatures : SigLine [ ]  =  [ 
3447    // MONO 
@@ -61,22 +74,14 @@ const fn_signatures: SigLine[] = [
6174    [ true ,  "mono_wasm_assembly_get_entry_point" ,  "number" ,  [ "number" ,  "number" ] ] , 
6275    [ true ,  "mono_wasm_class_get_type" ,  "number" ,  [ "number" ] ] , 
6376
64-     // MONO.diagnostics 
65-     [ true ,  "mono_wasm_event_pipe_enable" ,  "bool" ,  [ "string" ,  "number" ,  "number" ,  "string" ,  "bool" ,  "number" ] ] , 
66-     [ true ,  "mono_wasm_event_pipe_session_start_streaming" ,  "bool" ,  [ "number" ] ] , 
67-     [ true ,  "mono_wasm_event_pipe_session_disable" ,  "bool" ,  [ "number" ] ] , 
68-     [ true ,  "mono_wasm_diagnostic_server_create_thread" ,  "bool" ,  [ "string" ,  "number" ] ] , 
69-     [ true ,  "mono_wasm_diagnostic_server_thread_attach_to_runtime" ,  "void" ,  [ ] ] , 
70-     [ true ,  "mono_wasm_diagnostic_server_post_resume_runtime" ,  "void" ,  [ ] ] , 
71-     [ true ,  "mono_wasm_diagnostic_server_create_stream" ,  "number" ,  [ ] ] , 
72- 
7377    //INTERNAL 
7478    [ false ,  "mono_wasm_exit" ,  "void" ,  [ "number" ] ] , 
7579    [ true ,  "mono_wasm_getenv" ,  "number" ,  [ "string" ] ] , 
7680    [ true ,  "mono_wasm_set_main_args" ,  "void" ,  [ "number" ,  "number" ] ] , 
7781    [ false ,  "mono_wasm_enable_on_demand_gc" ,  "void" ,  [ "number" ] ] , 
7882    // These two need to be lazy because they may be missing 
79-     [ true ,  "mono_wasm_profiler_init_aot" ,  "void" ,  [ "string" ] ] , 
83+     [ ( )  =>  ! linkerEnableAotProfiler ,  "mono_wasm_profiler_init_aot" ,  "void" ,  [ "string" ] ] , 
84+     [ ( )  =>  ! linkerEnableBrowserProfiler ,  "mono_wasm_profiler_init_aot" ,  "void" ,  [ "string" ] ] , 
8085    [ true ,  "mono_wasm_profiler_init_browser" ,  "void" ,  [ "number" ] ] , 
8186    [ false ,  "mono_wasm_exec_regression" ,  "number" ,  [ "number" ,  "string" ] ] , 
8287    [ false ,  "mono_wasm_invoke_method_bound" ,  "number" ,  [ "number" ,  "number" ,  "number" ] ] , 
@@ -132,6 +137,7 @@ const fn_signatures: SigLine[] = [
132137    [ true ,  "mono_jiterp_get_opcode_info" ,  "number" ,  [ "number" ,  "number" ] ] , 
133138    [ true ,  "mono_wasm_is_zero_page_reserved" ,  "number" ,  [ ] ] , 
134139    [ true ,  "mono_jiterp_is_special_interface" ,  "number" ,  [ "number" ] ] , 
140+     ...diagnostics_cwraps , 
135141    ...legacy_interop_cwraps 
136142] ; 
137143
@@ -152,6 +158,22 @@ export interface t_LegacyCwraps {
152158    mono_wasm_array_length_ref ( array : MonoObjectRef ) : number ; 
153159} 
154160
161+ export  interface  t_DiagnosticsCwraps  { 
162+     // MONO.diagnostics 
163+     mono_wasm_event_pipe_enable ( outputPath : string  |  null ,  stream : VoidPtr ,  bufferSizeInMB : number ,  providers : string ,  rundownRequested : boolean ,  outSessionId : VoidPtr ) : boolean ; 
164+     mono_wasm_event_pipe_session_start_streaming ( sessionId : number ) : boolean ; 
165+     mono_wasm_event_pipe_session_disable ( sessionId : number ) : boolean ; 
166+     mono_wasm_diagnostic_server_create_thread ( websocketURL : string ,  threadIdOutPtr : VoidPtr ) : boolean ; 
167+     mono_wasm_diagnostic_server_thread_attach_to_runtime ( ) : void ; 
168+     mono_wasm_diagnostic_server_post_resume_runtime ( ) : void ; 
169+     mono_wasm_diagnostic_server_create_stream ( ) : VoidPtr ; 
170+ } 
171+ 
172+ export  interface  t_ProfilerCwraps  { 
173+     mono_wasm_profiler_init_aot ( desc : string ) : void ; 
174+     mono_wasm_profiler_init_browser ( desc : string ) : void ; 
175+ } 
176+ 
155177export  interface  t_Cwraps  { 
156178    // MONO 
157179    mono_wasm_register_root ( start : VoidPtr ,  size : number ,  name : string ) : number ; 
@@ -182,23 +204,11 @@ export interface t_Cwraps {
182204    mono_wasm_assembly_get_entry_point ( assembly : MonoAssembly ,  idx : number ) : MonoMethod ; 
183205    mono_wasm_intern_string_ref ( strRef : MonoStringRef ) : void ; 
184206
185- 
186-     // MONO.diagnostics 
187-     mono_wasm_event_pipe_enable ( outputPath : string  |  null ,  stream : VoidPtr ,  bufferSizeInMB : number ,  providers : string ,  rundownRequested : boolean ,  outSessionId : VoidPtr ) : boolean ; 
188-     mono_wasm_event_pipe_session_start_streaming ( sessionId : number ) : boolean ; 
189-     mono_wasm_event_pipe_session_disable ( sessionId : number ) : boolean ; 
190-     mono_wasm_diagnostic_server_create_thread ( websocketURL : string ,  threadIdOutPtr : VoidPtr ) : boolean ; 
191-     mono_wasm_diagnostic_server_thread_attach_to_runtime ( ) : void ; 
192-     mono_wasm_diagnostic_server_post_resume_runtime ( ) : void ; 
193-     mono_wasm_diagnostic_server_create_stream ( ) : VoidPtr ; 
194- 
195207    //INTERNAL 
196208    mono_wasm_exit ( exit_code : number ) : number ; 
197209    mono_wasm_getenv ( name : string ) : CharPtr ; 
198210    mono_wasm_enable_on_demand_gc ( enable : number ) : void ; 
199211    mono_wasm_set_main_args ( argc : number ,  argv : VoidPtr ) : void ; 
200-     mono_wasm_profiler_init_aot ( desc : string ) : void ; 
201-     mono_wasm_profiler_init_browser ( desc : string ) : void ; 
202212    mono_wasm_exec_regression ( verbose_level : number ,  image : string ) : number ; 
203213    mono_wasm_invoke_method_bound ( method : MonoMethod ,  args : JSMarshalerArguments ,  fail : MonoStringRef ) : number ; 
204214    mono_wasm_write_managed_pointer_unsafe ( destination : VoidPtr  |  MonoObjectRef ,  pointer : ManagedPointer ) : void ; 
@@ -265,6 +275,8 @@ const wrapped_c_functions: t_Cwraps = <any>{};
265275
266276export  default  wrapped_c_functions ; 
267277export  const  legacy_c_functions : t_LegacyCwraps  &  t_Cwraps  =  wrapped_c_functions  as  any ; 
278+ export  const  diagnostics_c_functions : t_DiagnosticsCwraps  &  t_Cwraps  =  wrapped_c_functions  as  any ; 
279+ export  const  profiler_c_functions : t_ProfilerCwraps  &  t_Cwraps  =  wrapped_c_functions  as  any ; 
268280
269281// see src/mono/wasm/driver.c I52_ERROR_xxx 
270282export  const  enum  I52Error  { 
@@ -275,7 +287,7 @@ export const enum I52Error {
275287
276288const  fastCwrapTypes  =  [ "void" ,  "number" ,  null ] ; 
277289
278- function  cwrap ( name : string ,  returnType : string  |  null ,  argTypes : string [ ]  |  undefined ,  opts : any ,   throwOnError :  boolean ) : Function  { 
290+ function  cwrap ( name : string ,  returnType : string  |  null ,  argTypes : string [ ]  |  undefined ,  opts : any ) : Function  { 
279291    // Attempt to bypass emscripten's generated wrapper if it is safe to do so 
280292    let  fce  = 
281293        // Special cwrap options disable the fast path 
@@ -301,29 +313,29 @@ function cwrap(name: string, returnType: string | null, argTypes: string[] | und
301313
302314    if  ( typeof  ( fce )  !==  "function" )  { 
303315        const  msg  =  `cwrap ${ name }  ; 
304-         if  ( throwOnError ) 
305-             throw  new  Error ( msg ) ; 
306-         else 
307-             mono_log_error ( ""  +  msg ) ; 
316+         throw  new  Error ( msg ) ; 
308317    } 
309318    return  fce ; 
310319} 
311320
312321export  function  init_c_exports ( ) : void { 
313-     const  lfns  =  WasmEnableLegacyJsInterop  &&  ! disableLegacyJsInterop  ? legacy_interop_cwraps  : [ ] ; 
322+     const  lfns  =  WasmEnableLegacyJsInterop  &&  ! linkerDisableLegacyJsInterop  ? legacy_interop_cwraps  : [ ] ; 
314323    const  fns  =  [ ...fn_signatures ,  ...lfns ] ; 
315324    for  ( const  sig  of  fns )  { 
316325        const  wf : any  =  wrapped_c_functions ; 
317-         const  [ lazy ,  name ,  returnType ,  argTypes ,  opts ]  =  sig ; 
318-         if  ( lazy )  { 
326+         const  [ lazyOrSkip ,  name ,  returnType ,  argTypes ,  opts ]  =  sig ; 
327+         const  maybeSkip  =  typeof  lazyOrSkip  ===  "function" ; 
328+         if  ( lazyOrSkip  ===  true  ||  maybeSkip )  { 
319329            // lazy init on first run 
320330            wf [ name ]  =  function  ( ...args : any [ ] )  { 
321-                 const  fce  =  cwrap ( name ,  returnType ,  argTypes ,  opts ,  true ) ; 
331+                 const  isNotSkipped  =  ! maybeSkip  ||  ! lazyOrSkip ( ) ; 
332+                 mono_assert ( isNotSkipped ,  ( )  =>  `cwrap ${ name }  ) ; 
333+                 const  fce  =  cwrap ( name ,  returnType ,  argTypes ,  opts ) ; 
322334                wf [ name ]  =  fce ; 
323335                return  fce ( ...args ) ; 
324336            } ; 
325337        }  else  { 
326-             const  fce  =  cwrap ( name ,  returnType ,  argTypes ,  opts ,   false ) ; 
338+             const  fce  =  cwrap ( name ,  returnType ,  argTypes ,  opts ) ; 
327339            wf [ name ]  =  fce ; 
328340        } 
329341    } 
0 commit comments