fix RuntimeType.cs
[mono-project.git] / sdks / wasm / corebindings.c
blob693ae582c06728dd8322a5763ea721af695bbdf6
1 #include <emscripten.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdint.h>
6 #include <assert.h>
8 #include <mono/jit/jit.h>
10 //JS funcs
11 extern MonoObject* mono_wasm_invoke_js_with_args (int js_handle, MonoString *method, MonoArray *args, int *is_exception);
12 extern MonoObject* mono_wasm_get_object_property (int js_handle, MonoString *propertyName, int *is_exception);
13 extern MonoObject* mono_wasm_get_by_index (int js_handle, int property_index, int *is_exception);
14 extern MonoObject* mono_wasm_set_object_property (int js_handle, MonoString *propertyName, MonoObject *value, int createIfNotExist, int hasOwnProperty, int *is_exception);
15 extern MonoObject* mono_wasm_set_by_index (int js_handle, int property_index, MonoObject *value, int *is_exception);
16 extern MonoObject* mono_wasm_get_global_object (MonoString *global_name, int *is_exception);
17 extern void* mono_wasm_release_handle (int js_handle, int *is_exception);
18 extern void* mono_wasm_release_object (int js_handle, int *is_exception);
19 extern MonoObject* mono_wasm_new_object (int js_handle, MonoArray *args, int *is_exception);
20 extern MonoObject* mono_wasm_new (MonoString *core_name, MonoArray *args, int *is_exception);
21 extern int mono_wasm_bind_core_object (int js_handle, int gc_handle, int *is_exception);
22 extern int mono_wasm_bind_host_object (int js_handle, int gc_handle, int *is_exception);
23 extern MonoObject* mono_wasm_typed_array_to_array (int js_handle, int *is_exception);
24 extern MonoObject* mono_wasm_typed_array_copy_to (int js_handle, int ptr, int begin, int end, int bytes_per_element, int *is_exception);
25 extern MonoObject* mono_wasm_typed_array_from (int ptr, int begin, int end, int bytes_per_element, int type, int *is_exception);
26 extern MonoObject* mono_wasm_typed_array_copy_from (int js_handle, int ptr, int begin, int end, int bytes_per_element, int *is_exception);
28 void core_initialize_internals ()
30 mono_add_internal_call ("WebAssembly.Runtime::InvokeJSWithArgs", mono_wasm_invoke_js_with_args);
31 mono_add_internal_call ("WebAssembly.Runtime::GetObjectProperty", mono_wasm_get_object_property);
32 mono_add_internal_call ("WebAssembly.Runtime::GetByIndex", mono_wasm_get_by_index);
33 mono_add_internal_call ("WebAssembly.Runtime::SetObjectProperty", mono_wasm_set_object_property);
34 mono_add_internal_call ("WebAssembly.Runtime::SetByIndex", mono_wasm_set_by_index);
35 mono_add_internal_call ("WebAssembly.Runtime::GetGlobalObject", mono_wasm_get_global_object);
36 mono_add_internal_call ("WebAssembly.Runtime::ReleaseHandle", mono_wasm_release_handle);
37 mono_add_internal_call ("WebAssembly.Runtime::ReleaseObject", mono_wasm_release_object);
38 mono_add_internal_call ("WebAssembly.Runtime::NewObjectJS", mono_wasm_new_object);
39 mono_add_internal_call ("WebAssembly.Runtime::BindCoreObject", mono_wasm_bind_core_object);
40 mono_add_internal_call ("WebAssembly.Runtime::BindHostObject", mono_wasm_bind_host_object);
41 mono_add_internal_call ("WebAssembly.Runtime::New", mono_wasm_new);
42 mono_add_internal_call ("WebAssembly.Runtime::TypedArrayToArray", mono_wasm_typed_array_to_array);
43 mono_add_internal_call ("WebAssembly.Runtime::TypedArrayCopyTo", mono_wasm_typed_array_copy_to);
44 mono_add_internal_call ("WebAssembly.Runtime::TypedArrayFrom", mono_wasm_typed_array_from);
45 mono_add_internal_call ("WebAssembly.Runtime::TypedArrayCopyFrom", mono_wasm_typed_array_copy_from);
49 // Int8Array | int8_t | byte or SByte (signed byte)
50 // Uint8Array | uint8_t | byte or Byte (unsigned byte)
51 // Uint8ClampedArray| uint8_t | byte or Byte (unsigned byte)
52 // Int16Array | int16_t | short (signed short)
53 // Uint16Array | uint16_t | ushort (unsigned short)
54 // Int32Array | int32_t | int (signed integer)
55 // Uint32Array | uint32_t | uint (unsigned integer)
56 // Float32Array | float | float
57 // Float64Array | double | double
58 // typed array marshalling
59 #define MARSHAL_ARRAY_BYTE 11
60 #define MARSHAL_ARRAY_UBYTE 12
61 #define MARSHAL_ARRAY_SHORT 13
62 #define MARSHAL_ARRAY_USHORT 14
63 #define MARSHAL_ARRAY_INT 15
64 #define MARSHAL_ARRAY_UINT 16
65 #define MARSHAL_ARRAY_FLOAT 17
66 #define MARSHAL_ARRAY_DOUBLE 18
68 EMSCRIPTEN_KEEPALIVE MonoArray*
69 mono_wasm_typed_array_new (char *arr, int length, int size, int type)
71 MonoClass *typeClass = mono_get_byte_class(); // default is Byte
72 switch (type) {
73 case MARSHAL_ARRAY_BYTE:
74 typeClass = mono_get_sbyte_class();
75 break;
76 case MARSHAL_ARRAY_SHORT:
77 typeClass = mono_get_int16_class();
78 break;
79 case MARSHAL_ARRAY_USHORT:
80 typeClass = mono_get_uint16_class();
81 break;
82 case MARSHAL_ARRAY_INT:
83 typeClass = mono_get_int32_class();
84 break;
85 case MARSHAL_ARRAY_UINT:
86 typeClass = mono_get_uint32_class();
87 break;
88 case MARSHAL_ARRAY_FLOAT:
89 typeClass = mono_get_single_class();
90 break;
91 case MARSHAL_ARRAY_DOUBLE:
92 typeClass = mono_get_double_class();
93 break;
96 MonoArray *buffer;
98 buffer = mono_array_new (mono_get_root_domain(), typeClass, length);
99 memcpy(mono_array_addr_with_size(buffer, sizeof(char), 0), arr, length * size);
101 return buffer;
104 EMSCRIPTEN_KEEPALIVE int
105 mono_wasm_unbox_enum (MonoObject *obj)
107 if (!obj)
108 return 0;
110 MonoType *type = mono_class_get_type (mono_object_get_class(obj));
112 void *ptr = mono_object_unbox (obj);
113 switch (mono_type_get_type(mono_type_get_underlying_type (type))) {
114 case MONO_TYPE_I1:
115 case MONO_TYPE_U1:
116 return *(unsigned char*)ptr;
117 case MONO_TYPE_I2:
118 return *(short*)ptr;
119 case MONO_TYPE_U2:
120 return *(unsigned short*)ptr;
121 case MONO_TYPE_I4:
122 return *(int*)ptr;
123 case MONO_TYPE_U4:
124 return *(unsigned int*)ptr;
125 // WASM doesn't support returning longs to JS
126 // case MONO_TYPE_I8:
127 // case MONO_TYPE_U8:
128 default:
129 printf ("Invalid type %d to mono_unbox_enum\n", mono_type_get_type(mono_type_get_underlying_type (type)));
130 return 0;