Welcome to mirror list, hosted at ThFree Co, Russian Federation.

marshal-to-js.ts « runtime « wasm « mono « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 47d240f1f1353ff9fe2d043eff4021bdd9b72227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import { PromiseControl, promise_control_symbol, create_cancelable_promise } from "./cancelable-promise";
import cwraps from "./cwraps";
import { _lookup_js_owned_object, mono_wasm_get_jsobj_from_js_handle, mono_wasm_get_js_handle, setup_managed_proxy } from "./gc-handles";
import { Module, runtimeHelpers } from "./imports";
import {
    ManagedObject, JSMarshalerArgument, ManagedError, JSMarshalerArguments, MarshalerToCs, MarshalerToJs, JSMarshalerType,
    get_arg_gc_handle, get_arg_js_handle, get_arg_type, get_arg_i32, get_arg_f64, get_arg_i52, get_arg_i16, get_arg_u8, get_arg_f32,
    get_arg_b8, get_arg_date, get_arg_length, set_js_handle, get_arg, set_arg_type,
    get_signature_arg2_type, get_signature_arg1_type, get_signature_type, cs_to_js_marshalers, js_to_cs_marshalers,
    get_signature_res_type, JavaScriptMarshalerArgSize, set_gc_handle, is_args_exception, get_arg_u16, array_element_size, get_string_root, ArraySegment, Span, MemoryViewType, get_signature_arg3_type, MarshalerType, get_arg_i64_big, get_arg_intptr, get_arg_element_type
} from "./marshal";
import { conv_string, conv_string_root } from "./strings";
import { mono_assert, JSHandleNull, GCHandleNull } from "./types";
import { TypedArray } from "./types/emscripten";

export function initialize_marshalers_to_js(): void {
    if (cs_to_js_marshalers.size == 0) {
        cs_to_js_marshalers.set(MarshalerType.Array, _marshal_array_to_js);
        cs_to_js_marshalers.set(MarshalerType.Span, _marshal_span_to_js);
        cs_to_js_marshalers.set(MarshalerType.ArraySegment, _marshal_array_segment_to_js);
        cs_to_js_marshalers.set(MarshalerType.Boolean, _marshal_bool_to_js);
        cs_to_js_marshalers.set(MarshalerType.Byte, _marshal_byte_to_js);
        cs_to_js_marshalers.set(MarshalerType.Char, _marshal_char_to_js);
        cs_to_js_marshalers.set(MarshalerType.Int16, _marshal_int16_to_js);
        cs_to_js_marshalers.set(MarshalerType.Int32, _marshal_int32_to_js);
        cs_to_js_marshalers.set(MarshalerType.Int52, _marshal_int52_to_js);
        cs_to_js_marshalers.set(MarshalerType.BigInt64, _marshal_bigint64_to_js);
        cs_to_js_marshalers.set(MarshalerType.Single, _marshal_float_to_js);
        cs_to_js_marshalers.set(MarshalerType.IntPtr, _marshal_intptr_to_js);
        cs_to_js_marshalers.set(MarshalerType.Double, _marshal_double_to_js);
        cs_to_js_marshalers.set(MarshalerType.String, _marshal_string_to_js);
        cs_to_js_marshalers.set(MarshalerType.Exception, marshal_exception_to_js);
        cs_to_js_marshalers.set(MarshalerType.JSException, marshal_exception_to_js);
        cs_to_js_marshalers.set(MarshalerType.JSObject, _marshal_js_object_to_js);
        cs_to_js_marshalers.set(MarshalerType.Object, _marshal_cs_object_to_js);
        cs_to_js_marshalers.set(MarshalerType.DateTime, _marshal_datetime_to_js);
        cs_to_js_marshalers.set(MarshalerType.DateTimeOffset, _marshal_datetime_to_js);
        cs_to_js_marshalers.set(MarshalerType.Task, _marshal_task_to_js);
        cs_to_js_marshalers.set(MarshalerType.Action, _marshal_delegate_to_js);
        cs_to_js_marshalers.set(MarshalerType.Function, _marshal_delegate_to_js);
        cs_to_js_marshalers.set(MarshalerType.None, _marshal_null_to_js);
        cs_to_js_marshalers.set(MarshalerType.Void, _marshal_null_to_js);
        cs_to_js_marshalers.set(MarshalerType.Discard, _marshal_null_to_js);
    }
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function generate_arg_marshal_to_js(sig: JSMarshalerType, index: number, arg_offset: number, sig_offset: number, jsname: string, closure: any): {
    converters: string,
    call_body: string,
    marshaler_type: MarshalerType
} {
    let converters = "";
    let converter_types = "";
    let call_body = "";
    const converter_name = "converter" + index;
    let converter_name_arg1 = "null";
    let converter_name_arg2 = "null";
    let converter_name_arg3 = "null";
    let converter_name_res = "null";

    let marshaler_type = get_signature_type(sig);
    if (marshaler_type === MarshalerType.None || marshaler_type === MarshalerType.Void) {
        return {
            converters,
            call_body,
            marshaler_type
        };
    }

    const marshaler_type_res = get_signature_res_type(sig);
    if (marshaler_type_res !== MarshalerType.None) {
        const converter = cs_to_js_marshalers.get(marshaler_type_res);
        mono_assert(converter && typeof converter === "function", () => `Unknow converter for type ${marshaler_type_res} at ${index}`);

        if (marshaler_type != MarshalerType.Nullable) {
            converter_name_res = "converter" + index + "_res";
            converters += ", " + converter_name_res;
            converter_types += " " + MarshalerType[marshaler_type_res];
            closure[converter_name_res] = converter;
        } else {
            marshaler_type = marshaler_type_res;
        }
    }

    const marshaler_type_arg1 = get_signature_arg1_type(sig);
    if (marshaler_type_arg1 !== MarshalerType.None) {
        const converter = js_to_cs_marshalers.get(marshaler_type_arg1);
        mono_assert(converter && typeof converter === "function", () => `Unknow converter for type ${marshaler_type_arg1} at ${index}`);

        converter_name_arg1 = "converter" + index + "_arg1";
        converters += ", " + converter_name_arg1;
        converter_types += " " + MarshalerType[marshaler_type_arg1];
        closure[converter_name_arg1] = converter;
    }

    const marshaler_type_arg2 = get_signature_arg2_type(sig);
    if (marshaler_type_arg2 !== MarshalerType.None) {
        const converter = js_to_cs_marshalers.get(marshaler_type_arg2);
        mono_assert(converter && typeof converter === "function", () => `Unknow converter for type ${marshaler_type_arg2} at ${index}`);

        converter_name_arg2 = "converter" + index + "_arg2";
        converters += ", " + converter_name_arg2;
        converter_types += " " + MarshalerType[marshaler_type_arg2];
        closure[converter_name_arg2] = converter;
    }

    const marshaler_type_arg3 = get_signature_arg3_type(sig);
    if (marshaler_type_arg3 !== MarshalerType.None) {
        const converter = js_to_cs_marshalers.get(marshaler_type_arg3);
        mono_assert(converter && typeof converter === "function", () => `Unknow converter for type ${marshaler_type_arg3} at ${index}`);

        converter_name_arg3 = "converter" + index + "_arg3";
        converters += ", " + converter_name_arg3;
        converter_types += " " + MarshalerType[marshaler_type_arg3];
        closure[converter_name_arg3] = converter;
    }

    const converter = cs_to_js_marshalers.get(marshaler_type);
    mono_assert(converter && typeof converter === "function", () => `Unknow converter for type ${marshaler_type} at ${index} `);

    converters += ", " + converter_name;
    converter_types += " " + MarshalerType[marshaler_type];
    closure[converter_name] = converter;

    if (marshaler_type == MarshalerType.Task) {
        call_body = `  const ${jsname} = ${converter_name}(args + ${arg_offset}, signature + ${sig_offset}, ${converter_name_res}); // ${converter_types} \n`;
    } else if (marshaler_type == MarshalerType.Action || marshaler_type == MarshalerType.Function) {
        call_body = `  const ${jsname} = ${converter_name}(args + ${arg_offset}, signature + ${sig_offset}, ${converter_name_res}, ${converter_name_arg1}, ${converter_name_arg2}, ${converter_name_arg3}); // ${converter_types} \n`;
    } else {
        call_body = `  const ${jsname} = ${converter_name}(args + ${arg_offset}, signature + ${sig_offset}); // ${converter_types} \n`;
    }

    return {
        converters,
        call_body,
        marshaler_type
    };
}

function _marshal_bool_to_js(arg: JSMarshalerArgument): boolean | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_b8(arg);
}

function _marshal_byte_to_js(arg: JSMarshalerArgument): number | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_u8(arg);
}

function _marshal_char_to_js(arg: JSMarshalerArgument): number | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_u16(arg);
}

function _marshal_int16_to_js(arg: JSMarshalerArgument): number | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_i16(arg);
}

function _marshal_int32_to_js(arg: JSMarshalerArgument): number | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_i32(arg);
}

function _marshal_int52_to_js(arg: JSMarshalerArgument): number | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_i52(arg);
}

function _marshal_bigint64_to_js(arg: JSMarshalerArgument): bigint | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_i64_big(arg);
}

function _marshal_float_to_js(arg: JSMarshalerArgument): number | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_f32(arg);
}

function _marshal_double_to_js(arg: JSMarshalerArgument): number | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_f64(arg);
}

function _marshal_intptr_to_js(arg: JSMarshalerArgument): number | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    return get_arg_intptr(arg);
}

function _marshal_null_to_js(): null {
    return null;
}

function _marshal_datetime_to_js(arg: JSMarshalerArgument): Date | null {
    const type = get_arg_type(arg);
    if (type === MarshalerType.None) {
        return null;
    }
    return get_arg_date(arg);
}

function _marshal_delegate_to_js(arg: JSMarshalerArgument, _?: JSMarshalerType, res_converter?: MarshalerToJs, arg1_converter?: MarshalerToCs, arg2_converter?: MarshalerToCs, arg3_converter?: MarshalerToCs): Function | null {
    const type = get_arg_type(arg);
    if (type === MarshalerType.None) {
        return null;
    }

    const anyModule = Module as any;
    const gc_handle = get_arg_gc_handle(arg);
    let result = _lookup_js_owned_object(gc_handle);
    if (result === null || result === undefined) {
        // this will create new Function for the C# delegate
        result = (arg1_js: any, arg2_js: any, arg3_js: any) => {

            const sp = anyModule.stackSave();
            try {
                const args = anyModule.stackAlloc(JavaScriptMarshalerArgSize * 5);
                const exc = get_arg(args, 0);
                set_arg_type(exc, MarshalerType.None);
                const res = get_arg(args, 1);
                set_arg_type(res, MarshalerType.None);
                set_gc_handle(res, <any>gc_handle);
                const arg1 = get_arg(args, 2);
                const arg2 = get_arg(args, 3);
                const arg3 = get_arg(args, 4);

                if (arg1_converter) {
                    arg1_converter(arg1, arg1_js);
                }
                if (arg2_converter) {
                    arg2_converter(arg2, arg2_js);
                }
                if (arg3_converter) {
                    arg3_converter(arg3, arg3_js);
                }

                const fail = cwraps.mono_wasm_invoke_method_bound(runtimeHelpers.call_delegate, args);
                if (fail) throw new Error("ERR23: Unexpected error: " + conv_string(fail));
                if (is_args_exception(args)) throw marshal_exception_to_js(exc);

                if (res_converter) {
                    return res_converter(res);
                }

            } finally {
                anyModule.stackRestore(sp);
            }
        };

        setup_managed_proxy(result, gc_handle);
    }

    return result;
}

function _marshal_task_to_js(arg: JSMarshalerArgument, _?: JSMarshalerType, res_converter?: MarshalerToJs): Promise<any> | null {
    const type = get_arg_type(arg);
    if (type === MarshalerType.None) {
        return null;
    }

    if (type !== MarshalerType.Task) {

        if (!res_converter) {
            // when we arrived here from _marshal_cs_object_to_js
            res_converter = cs_to_js_marshalers.get(type);
        }
        mono_assert(res_converter, () => `Unknow sub_converter for type ${MarshalerType[type]} `);

        // this is already resolved
        const val = res_converter(arg);
        return new Promise((resolve) => resolve(val));
    }

    const js_handle = get_arg_js_handle(arg);
    // console.log("_marshal_task_to_js A" + js_handle);
    if (js_handle == JSHandleNull) {
        // this is already resolved void
        return new Promise((resolve) => resolve(undefined));
    }
    const promise = mono_wasm_get_jsobj_from_js_handle(js_handle);
    mono_assert(!!promise, () => `ERR28: promise not found for js_handle: ${js_handle} `);
    const promise_control = promise[promise_control_symbol] as PromiseControl;
    mono_assert(!!promise_control, () => `ERR27: promise_control not found for js_handle: ${js_handle} `);

    const orig_resolve = promise_control.resolve;
    promise_control.resolve = (argInner: JSMarshalerArgument) => {
        // console.log("_marshal_task_to_js R" + js_handle);
        const type = get_arg_type(argInner);
        if (type === MarshalerType.None) {
            orig_resolve(null);
            return;
        }

        if (!res_converter) {
            // when we arrived here from _marshal_cs_object_to_js
            res_converter = cs_to_js_marshalers.get(type);
        }
        mono_assert(res_converter, () => `Unknow sub_converter for type ${MarshalerType[type]}`);

        const js_value = res_converter!(argInner);
        orig_resolve(js_value);
    };

    return promise;
}

export function mono_wasm_marshal_promise(args: JSMarshalerArguments): void {
    const exc = get_arg(args, 0);
    const res = get_arg(args, 1);
    const arg_handle = get_arg(args, 2);
    const arg_value = get_arg(args, 3);

    const exc_type = get_arg_type(exc);
    const value_type = get_arg_type(arg_value);
    const js_handle = get_arg_js_handle(arg_handle);

    if (js_handle === JSHandleNull) {
        const { promise, promise_control } = create_cancelable_promise();
        const js_handle = mono_wasm_get_js_handle(promise)!;
        set_js_handle(res, js_handle);

        if (exc_type !== MarshalerType.None) {
            // this is already failed task
            const reason = marshal_exception_to_js(exc);
            promise_control.reject(reason);
        }
        else if (value_type !== MarshalerType.Task) {
            // this is already resolved task
            const sub_converter = cs_to_js_marshalers.get(value_type);
            mono_assert(sub_converter, () => `Unknow sub_converter for type ${MarshalerType[value_type]} `);
            const data = sub_converter(arg_value);
            promise_control.resolve(data);
        }
    } else {
        // resolve existing promise
        const promise = mono_wasm_get_jsobj_from_js_handle(js_handle);
        mono_assert(!!promise, () => `ERR25: promise not found for js_handle: ${js_handle} `);
        const promise_control = promise[promise_control_symbol] as PromiseControl;
        mono_assert(!!promise_control, () => `ERR26: promise_control not found for js_handle: ${js_handle} `);

        if (exc_type !== MarshalerType.None) {
            const reason = marshal_exception_to_js(exc);
            promise_control.reject(reason);
        }
        else if (value_type !== MarshalerType.Task) {
            // here we assume that resolve was wrapped with sub_converter inside _marshal_task_to_js
            promise_control.resolve(arg_value);
        }
    }
    set_arg_type(res, MarshalerType.Task);
    set_arg_type(exc, MarshalerType.None);
}

function _marshal_string_to_js(arg: JSMarshalerArgument): string | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    const root = get_string_root(arg);
    try {
        const value = conv_string_root(root);
        return value;
    } finally {
        root.release();
    }
}

export function marshal_exception_to_js(arg: JSMarshalerArgument): Error | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    if (type == MarshalerType.JSException) {
        // this is JSException roundtrip
        const js_handle = get_arg_js_handle(arg);
        const js_obj = mono_wasm_get_jsobj_from_js_handle(js_handle);
        return js_obj;
    }

    const gc_handle = get_arg_gc_handle(arg);
    let result = _lookup_js_owned_object(gc_handle);
    if (result === null || result === undefined) {
        // this will create new ManagedError
        const message = _marshal_string_to_js(arg);
        result = new ManagedError(message!);

        setup_managed_proxy(result, gc_handle);
    }

    return result;
}

function _marshal_js_object_to_js(arg: JSMarshalerArgument): any {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    const js_handle = get_arg_js_handle(arg);
    const js_obj = mono_wasm_get_jsobj_from_js_handle(js_handle);
    return js_obj;
}

function _marshal_cs_object_to_js(arg: JSMarshalerArgument): any {
    const marshaler_type = get_arg_type(arg);
    if (marshaler_type == MarshalerType.None) {
        return null;
    }
    if (marshaler_type == MarshalerType.JSObject) {
        const js_handle = get_arg_js_handle(arg);
        const js_obj = mono_wasm_get_jsobj_from_js_handle(js_handle);
        return js_obj;
    }

    if (marshaler_type == MarshalerType.Array) {
        const element_type = get_arg_element_type(arg);
        return _marshal_array_to_js_impl(arg, element_type);
    }

    if (marshaler_type == MarshalerType.Object) {
        const gc_handle = get_arg_gc_handle(arg);
        if (gc_handle === GCHandleNull) {
            return null;
        }

        // see if we have js owned instance for this gc_handle already
        let result = _lookup_js_owned_object(gc_handle);

        // If the JS object for this gc_handle was already collected (or was never created)
        if (!result) {
            result = new ManagedObject();
            setup_managed_proxy(result, gc_handle);
        }

        return result;
    }

    // other types
    const converter = cs_to_js_marshalers.get(marshaler_type);
    mono_assert(converter, () => `Unknow converter for type ${MarshalerType[marshaler_type]}`);
    return converter(arg);
}

function _marshal_array_to_js(arg: JSMarshalerArgument, sig?: JSMarshalerType): Array<any> | TypedArray | null {
    mono_assert(!!sig, "Expected valid sig paramater");
    const element_type = get_signature_arg1_type(sig);
    return _marshal_array_to_js_impl(arg, element_type);
}

function _marshal_array_to_js_impl(arg: JSMarshalerArgument, element_type: MarshalerType): Array<any> | TypedArray | null {
    const type = get_arg_type(arg);
    if (type == MarshalerType.None) {
        return null;
    }
    const elementSize = array_element_size(element_type);
    mono_assert(elementSize != -1, () => `Element type ${MarshalerType[element_type]} not supported`);
    const buffer_ptr = get_arg_intptr(arg);
    const length = get_arg_length(arg);
    let result: Array<any> | TypedArray | null = null;
    if (element_type == MarshalerType.String) {
        result = new Array(length);
        for (let index = 0; index < length; index++) {
            const element_arg = get_arg(<any>buffer_ptr, index);
            result[index] = _marshal_string_to_js(element_arg);
        }
        cwraps.mono_wasm_deregister_root(<any>buffer_ptr);
    }
    else if (element_type == MarshalerType.Object) {
        result = new Array(length);
        for (let index = 0; index < length; index++) {
            const element_arg = get_arg(<any>buffer_ptr, index);
            result[index] = _marshal_cs_object_to_js(element_arg);
        }
        cwraps.mono_wasm_deregister_root(<any>buffer_ptr);
    }
    else if (element_type == MarshalerType.JSObject) {
        result = new Array(length);
        for (let index = 0; index < length; index++) {
            const element_arg = get_arg(<any>buffer_ptr, index);
            result[index] = _marshal_js_object_to_js(element_arg);
        }
    }
    else if (element_type == MarshalerType.Byte) {
        const sourceView = Module.HEAPU8.subarray(<any>buffer_ptr, buffer_ptr + length);
        result = sourceView.slice();//copy
    }
    else if (element_type == MarshalerType.Int32) {
        const sourceView = Module.HEAP32.subarray(buffer_ptr >> 2, (buffer_ptr >> 2) + length);
        result = sourceView.slice();//copy
    }
    else if (element_type == MarshalerType.Double) {
        const sourceView = Module.HEAPF64.subarray(buffer_ptr >> 3, (buffer_ptr >> 3) + length);
        result = sourceView.slice();//copy
    }
    else {
        throw new Error(`NotImplementedException ${MarshalerType[element_type]} `);
    }
    Module._free(<any>buffer_ptr);
    return result;
}

function _marshal_span_to_js(arg: JSMarshalerArgument, sig?: JSMarshalerType): Span {
    mono_assert(!!sig, "Expected valid sig paramater");

    const element_type = get_signature_arg1_type(sig);
    const buffer_ptr = get_arg_intptr(arg);
    const length = get_arg_length(arg);
    let result: Span | null = null;
    if (element_type == MarshalerType.Byte) {
        result = new Span(<any>buffer_ptr, length, MemoryViewType.Byte);
    }
    else if (element_type == MarshalerType.Int32) {
        result = new Span(<any>buffer_ptr, length, MemoryViewType.Int32);
    }
    else if (element_type == MarshalerType.Double) {
        result = new Span(<any>buffer_ptr, length, MemoryViewType.Double);
    }
    else {
        throw new Error(`NotImplementedException ${MarshalerType[element_type]} `);
    }
    return result;
}

function _marshal_array_segment_to_js(arg: JSMarshalerArgument, sig?: JSMarshalerType): ArraySegment {
    mono_assert(!!sig, "Expected valid sig paramater");

    const element_type = get_signature_arg1_type(sig);
    const buffer_ptr = get_arg_intptr(arg);
    const length = get_arg_length(arg);
    let result: ArraySegment | null = null;
    if (element_type == MarshalerType.Byte) {
        result = new ArraySegment(<any>buffer_ptr, length, MemoryViewType.Byte);
    }
    else if (element_type == MarshalerType.Int32) {
        result = new ArraySegment(<any>buffer_ptr, length, MemoryViewType.Int32);
    }
    else if (element_type == MarshalerType.Double) {
        result = new ArraySegment(<any>buffer_ptr, length, MemoryViewType.Double);
    }
    else {
        throw new Error(`NotImplementedException ${MarshalerType[element_type]} `);
    }
    const gc_handle = get_arg_gc_handle(arg);
    setup_managed_proxy(result, gc_handle);

    return result;
}