Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ public static void FunctionSum()
public static void FunctionMath()
{
JSObject math = (JSObject)Runtime.GetGlobalObject("Math");
Assert.NotNull(math);
Assert.True(math != null, "math != null");

Function mathMax = (Function)math.GetObjectProperty("max");
Assert.NotNull(mathMax);
Assert.True(mathMax != null, "mathMax != null");

var maxValue = (int)mathMax.Apply(null, new object[] { 5, 6, 2, 3, 7 });
Assert.Equal(7, maxValue);
Expand All @@ -107,13 +107,47 @@ public static void FunctionMath()
Assert.Equal(7, maxValue);

Function mathMin = (Function)((JSObject)Runtime.GetGlobalObject("Math")).GetObjectProperty("min");
Assert.NotNull(mathMin);
Assert.True(mathMin != null, "mathMin != null");

var minValue = (int)mathMin.Apply(null, new object[] { 5, 6, 2, 3, 7 });
Assert.Equal(2, minValue);

minValue = (int)mathMin.Call(null, 5, 6, 2, 3, 7);
Assert.Equal(2, minValue);
}

private static string GetBigTestString() {
var expectedSb = new System.Text.StringBuilder();
expectedSb.Append("start<<<");
for (int i = 0; i < 4096000; i++)
expectedSb.Append(i % 10);
expectedSb.Append(">>>end");
return expectedSb.ToString();
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/42693")]
[Fact]
public static void InvokeJSMarshalsStrings() {
var invokeResult = Runtime.InvokeJS("\"a\0bc\"");
Assert.Equal("a\0bc", invokeResult);

var expected = GetBigTestString();
invokeResult = Runtime.InvokeJS("\"" + expected + "\"");
Assert.Equal(expected, invokeResult);
}

[Fact]
public static void MarshalStringParametersFromJS() {
HelperMarshal._stringResource = null;
Runtime.InvokeJS("App.call_test_method(\"InvokeString\", [\"a\0bc\"])");
Assert.Equal("a\0bc", HelperMarshal._stringResource);

var expected = GetBigTestString();
for (var i = 0; i < 10; i++) {
HelperMarshal._stringResource = null;
Runtime.InvokeJS("App.call_test_method(\"InvokeString\", [\"" + expected + "\"])");
Assert.Equal(expected, HelperMarshal._stringResource);
}
}
}
}
19 changes: 18 additions & 1 deletion src/mono/wasm/runtime/binding_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var BindingSupportLib = {
this.find_method = Module.cwrap ('mono_wasm_assembly_find_method', 'number', ['number', 'string', 'number']);
this.invoke_method = Module.cwrap ('mono_wasm_invoke_method', 'number', ['number', 'number', 'number', 'number']);
this.mono_string_get_utf8 = Module.cwrap ('mono_wasm_string_get_utf8', 'number', ['number']);
this.js_string_to_mono_string = Module.cwrap ('mono_wasm_string_from_js', 'number', ['string']);
this.mono_wasm_string_from_utf16 = Module.cwrap ('mono_wasm_string_from_utf16', 'number', ['number', 'number']);
this.mono_get_obj_type = Module.cwrap ('mono_wasm_get_obj_type', 'number', ['number']);
this.mono_unbox_int = Module.cwrap ('mono_unbox_int', 'number', ['number']);
this.mono_unbox_float = Module.cwrap ('mono_wasm_unbox_float', 'number', ['number']);
Expand Down Expand Up @@ -140,6 +140,23 @@ var BindingSupportLib = {
return this.call_method (this.is_simple_array, null, "mi", [ ele ]);
},

js_string_to_mono_string: function (string) {
if (string === null || typeof string === "undefined")
return 0;

var buffer = Module._malloc (string.length * 2);
if (!buffer)
throw new Error ("out of memory");

var buffer16 = (buffer / 2) | 0;
for (var i = 0; i < string.length; i++)
Module.HEAP16[buffer16 + i] = string.charCodeAt (i);

var result = this.mono_wasm_string_from_utf16 (buffer, string.length);
Module._free (buffer);
return result;
},

mono_array_to_js_array: function (mono_array) {
if (mono_array == 0)
return null;
Expand Down
11 changes: 11 additions & 0 deletions src/mono/wasm/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,17 @@ mono_wasm_string_from_js (const char *str)
return NULL;
}

EMSCRIPTEN_KEEPALIVE MonoString *
mono_wasm_string_from_utf16 (const mono_unichar2 * chars, int length)
{
assert (length >= 0);

if (chars)
return mono_string_new_utf16 (root_domain, chars, length);
else
return NULL;
}

static int
class_is_task (MonoClass *klass)
{
Expand Down