Skip to content

Commit 299966f

Browse files
committed
strings by value
1 parent 1788e52 commit 299966f

File tree

4 files changed

+16
-68
lines changed

4 files changed

+16
-68
lines changed

src/libraries/System.Runtime.InteropServices.JavaScript/src/System.Runtime.InteropServices.JavaScript.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetPlatformIdentifier)' != 'browser'">SR.SystemRuntimeInteropServicesJavaScript_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
1313
<FeatureWasmManagedThreads Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableThreads)' == 'true'">true</FeatureWasmManagedThreads>
1414
<WasmEnableJsInteropByValue Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableJsInteropByValue)' == '' and '$(FeatureWasmManagedThreads)' == 'true'">true</WasmEnableJsInteropByValue>
15+
<WasmEnableJsInteropByValue Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableJsInteropByValue)' == '' and '$(RuntimeFlavor)' == 'CoreCLR'">true</WasmEnableJsInteropByValue>
1516
<WasmEnableJsInteropByValue Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableJsInteropByValue)' == ''">false</WasmEnableJsInteropByValue>
1617
<DefineConstants Condition="'$(FeatureWasmManagedThreads)' == 'true'">$(DefineConstants);FEATURE_WASM_MANAGED_THREADS</DefineConstants>
1718
<DefineConstants Condition="'$(WasmEnableJsInteropByValue)' == 'true'">$(DefineConstants);ENABLE_JS_INTEROP_BY_VALUE</DefineConstants>

src/native/libs/System.Runtime.InteropServices.JavaScript.Native/interop/marshal-to-cs.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import BuildConfiguration from "consts:configuration";
55

6-
import { dotnetBrowserUtilsExports, dotnetApi, dotnetAssert } from "./cross-module";
6+
import { dotnetBrowserUtilsExports, dotnetApi, dotnetAssert, Module } from "./cross-module";
77

88
import type { BoundMarshalerToCs, JSMarshalerArgument, JSMarshalerArguments, JSMarshalerType, MarshalerToCs, MarshalerToJs, TypedArray } from "./types";
99
import { MarshalerType } from "./types";
@@ -206,22 +206,11 @@ export function marshal_string_to_cs(arg: JSMarshalerArgument, value: string) {
206206

207207
// eslint-disable-next-line @typescript-eslint/no-unused-vars
208208
function _marshal_string_to_cs_impl(arg: JSMarshalerArgument, value: string) {
209-
/* TODO-WASM
210-
if (WasmEnableJsInteropByValue) {
211-
const bufferLen = value.length * 2;
212-
const buffer = malloc(bufferLen);// together with Marshal.FreeHGlobal
213-
stringToUTF16(buffer as any, buffer as any + bufferLen, value);
214-
set_arg_intptr(arg, buffer);
215-
set_arg_length(arg, value.length);
216-
} else {
217-
dotnetAssert.check(!WasmEnableThreads, "Marshaling strings by reference is not supported in multithreaded mode");
218-
const root = get_string_root(arg);
219-
try {
220-
stringToMonoStringRoot(value, root);
221-
} finally {
222-
root.release();
223-
}
224-
}*/
209+
const bufferLen = value.length * 2;
210+
const buffer = Module._malloc(bufferLen);// together with Marshal.FreeHGlobal
211+
dotnetBrowserUtilsExports.stringToUTF16(buffer as any, buffer as any + bufferLen, value);
212+
set_arg_intptr(arg, buffer);
213+
set_arg_length(arg, value.length);
225214
}
226215

227216
function _marshal_null_to_cs(arg: JSMarshalerArgument) {
@@ -457,27 +446,17 @@ export function marshal_array_to_cs_impl(arg: JSMarshalerArgument, value: Array<
457446
if (element_type == MarshalerType.String) {
458447
dotnetAssert.check(Array.isArray(value), "Value is not an Array");
459448
dotnetBrowserUtilsExports.zeroRegion(buffer_ptr, buffer_length);
460-
throw new Error("TODO-WASM");
461-
/*if (!WasmEnableJsInteropByValue) {
462-
dotnetAssert.check(!WasmEnableThreads, "Marshaling strings by reference is not supported in multithreaded mode");
463-
cwraps.SystemInteropJS_RegisterGCRoot(buffer_ptr, buffer_length, "marshal_array_to_cs");
464-
}
465449
for (let index = 0; index < length; index++) {
466450
const element_arg = get_arg(<any>buffer_ptr, index);
467451
marshal_string_to_cs(element_arg, value[index]);
468-
}*/
452+
}
469453
} else if (element_type == MarshalerType.Object) {
470454
dotnetAssert.check(Array.isArray(value), "Value is not an Array");
471455
dotnetBrowserUtilsExports.zeroRegion(buffer_ptr, buffer_length);
472-
throw new Error("TODO-WASM");
473-
/*TODO-WASM if (!WasmEnableJsInteropByValue) {
474-
dotnetAssert.check(!WasmEnableThreads, "Marshaling objects by reference is not supported in multithreaded mode");
475-
cwraps.SystemInteropJS_RegisterGCRoot(buffer_ptr, buffer_length, "marshal_array_to_cs");
476-
}
477456
for (let index = 0; index < length; index++) {
478457
const element_arg = get_arg(<any>buffer_ptr, index);
479458
marshal_cs_object_to_cs(element_arg, value[index]);
480-
}*/
459+
}
481460
} else if (element_type == MarshalerType.JSObject) {
482461
dotnetAssert.check(Array.isArray(value), "Value is not an Array");
483462
dotnetBrowserUtilsExports.zeroRegion(buffer_ptr, buffer_length);

src/native/libs/System.Runtime.InteropServices.JavaScript.Native/interop/marshal-to-js.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import BuildConfiguration from "consts:configuration";
55

6-
import { dotnetLoaderExports, dotnetApi, dotnetAssert, dotnetLogger } from "./cross-module";
6+
import { dotnetBrowserUtilsExports, dotnetLoaderExports, dotnetApi, dotnetAssert, dotnetLogger, Module } from "./cross-module";
77

88
import type { BoundMarshalerToJs, JSHandle, JSMarshalerArgument, JSMarshalerArguments, JSMarshalerType, MarshalerToCs, MarshalerToJs, TypedArray } from "./types";
99
import { GCHandleNull, MarshalerType } from "./types";
@@ -368,27 +368,15 @@ export function SystemInteropJS_ResolveOrRejectPromiseImpl(args: JSMarshalerArgu
368368

369369
// eslint-disable-next-line @typescript-eslint/no-unused-vars
370370
export function marshal_string_to_js(arg: JSMarshalerArgument): string | null {
371-
/*TODO-WASM const type = get_arg_type(arg);
371+
const type = get_arg_type(arg);
372372
if (type == MarshalerType.None) {
373373
return null;
374374
}
375-
if (WasmEnableJsInteropByValue) {
376-
const buffer = get_arg_intptr(arg);
377-
const len = get_arg_length(arg) * 2;
378-
const value = utf16ToString(<any>buffer, <any>buffer + len);
379-
free(buffer as any);
380-
return value;
381-
} else {
382-
dotnetAssert.check(!WasmEnableThreads, "Marshaling strings by reference is not supported in multithreaded mode");
383-
const root = get_string_root(arg);
384-
try {
385-
const value = monoStringToString(root);
386-
return value;
387-
} finally {
388-
root.release();
389-
}
390-
}*/
391-
return "TODO-WASM";
375+
const buffer = get_arg_intptr(arg);
376+
const len = get_arg_length(arg) * 2;
377+
const value = dotnetBrowserUtilsExports.utf16ToString(<any>buffer, <any>buffer + len);
378+
Module._free(buffer as any);
379+
return value;
392380
}
393381

394382
export function marshal_exception_to_js(arg: JSMarshalerArgument): Error | null {
@@ -494,20 +482,12 @@ function _marshal_array_to_js_impl(arg: JSMarshalerArgument, element_type: Marsh
494482
const element_arg = get_arg(<any>buffer_ptr, index);
495483
result[index] = marshal_string_to_js(element_arg);
496484
}
497-
/*TODO-WASM if (!WasmEnableJsInteropByValue) {
498-
dotnetAssert.check(!WasmEnableThreads, "Marshaling string by reference is not supported in multithreaded mode");
499-
cwraps.SystemInteropJS_UnregisterGCRoot(<any>buffer_ptr);
500-
}*/
501485
} else if (element_type == MarshalerType.Object) {
502486
result = new Array(length);
503487
for (let index = 0; index < length; index++) {
504488
const element_arg = get_arg(<any>buffer_ptr, index);
505489
result[index] = _marshal_cs_object_to_js(element_arg);
506490
}
507-
/*TODO-WASM if (!WasmEnableJsInteropByValue) {
508-
dotnetAssert.check(!WasmEnableThreads, "Marshaling objects by reference is not supported in multithreaded mode");
509-
cwraps.SystemInteropJS_UnregisterGCRoot(<any>buffer_ptr);
510-
}*/
511491
} else if (element_type == MarshalerType.JSObject) {
512492
result = new Array(length);
513493
for (let index = 0; index < length; index++) {

src/native/libs/System.Runtime.InteropServices.JavaScript.Native/interop/marshal.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,6 @@ export function set_arg_length(arg: JSMarshalerArgument, size: number): void {
356356
dotnetApi.setHeapI32(<any>arg + JSMarshalerArgumentOffsets.Length, size);
357357
}
358358

359-
/* TODO-WASM
360-
export function get_string_root(arg: JSMarshalerArgument): WasmRoot<MonoString> {
361-
dotnetAssert.check(arg, "Null arg");
362-
throw new Error("TODO-WASM");
363-
//return mono_wasm_new_external_root<MonoString>(<any>arg);
364-
}
365-
366-
export function set_root(arg: JSMarshalerArgument, root: WasmRoot<MonoObject>): void {
367-
dotnetAssert.check(arg, "Null arg");
368-
dotnetApi.setHeapU32(<any>arg + 0, root.get_address());
369-
}*/
370-
371359
export interface IDisposable {
372360
dispose(): void;
373361
get isDisposed(): boolean;

0 commit comments

Comments
 (0)