Skip to content

Commit

Permalink
[wasm] reduce legacy interop usage (#72021)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed Jul 28, 2022
1 parent 07ebba1 commit d7c8bc1
Show file tree
Hide file tree
Showing 51 changed files with 1,950 additions and 1,957 deletions.
4 changes: 0 additions & 4 deletions src/libraries/Common/src/Interop/Browser/Interop.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ internal static unsafe partial class Runtime
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void CreateCSOwnedObjectRef(in string className, in object[] parms, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void TypedArrayCopyToRef(IntPtr jsHandle, int arrayPtr, int begin, int end, int bytesPerElement, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void TypedArrayFromRef(int arrayPtr, int begin, int end, int bytesPerElement, int type, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void TypedArrayCopyFromRef(IntPtr jsHandle, int arrayPtr, int begin, int end, int bytesPerElement, out int exceptionalResult, out object result);

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public static async Task Iterator()
{
throw new Exception($"At attempt={attempt}, index={index}: {ex.Message}", ex);
}
await Task.Yield();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ public static void MarshalArrayBuffer()
Assert.Equal(16, HelperMarshal._byteBuffer.Length);
}




[Fact]
public static void MarshalStringToCS()
{
Expand Down Expand Up @@ -279,7 +276,7 @@ public static void BindStaticMethod()
{
HelperMarshal._intValue = 0;
Utils.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (200);
");

Expand All @@ -291,7 +288,7 @@ public static void BindIntPtrStaticMethod()
{
HelperMarshal._intPtrValue = IntPtr.Zero;
Utils.InvokeJS(@$"
var invoke_int_ptr = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeIntPtr"");
var invoke_int_ptr = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeIntPtr"");
invoke_int_ptr (42);
");
Assert.Equal(42, (int)HelperMarshal._intPtrValue);
Expand All @@ -302,25 +299,14 @@ public static void MarshalIntPtrToJS()
{
HelperMarshal._marshaledIntPtrValue = IntPtr.Zero;
Utils.InvokeJS(@$"
var invokeMarshalIntPtr = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeMarshalIntPtr"");
var invokeMarshalIntPtr = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeMarshalIntPtr"");
var r = invokeMarshalIntPtr ();
if (r != 42) throw `Invalid int_ptr value`;
");
Assert.Equal(42, (int)HelperMarshal._marshaledIntPtrValue);
}

[Fact]
public static void InvokeStaticMethod()
{
HelperMarshal._intValue = 0;
Utils.InvokeJS(@$"
INTERNAL.call_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"", [ 300 ]);
");

Assert.Equal(300, HelperMarshal._intValue);
}

[Fact]
public static void ResolveMethod()
{
Expand Down Expand Up @@ -407,6 +393,17 @@ public static void MarshalTypedArray()
Assert.Equal(16, HelperMarshal._byteBuffer.Length);
}

[Fact]
public static void MarshalUri()
{
HelperMarshal._blobURI = null;
Utils.InvokeJS(@"
App.call_test_method (""SetBlobAsUri"", [ ""https://dotnet.microsoft.com/en-us/"" ]);
");

Assert.NotNull(HelperMarshal._blobURI);
}

private static void RunMarshalTypedArrayJS(string type)
{
Utils.InvokeJS(@"
Expand All @@ -426,8 +423,6 @@ public static void MarshalTypedArrayByte()
Assert.Equal("hic sunt dracones", System.Text.Encoding.Default.GetString(HelperMarshal._taByte));
}



[Fact]
public static void TestFunctionSum()
{
Expand Down Expand Up @@ -455,7 +450,7 @@ public static void BoundStaticMethodMissingArgs()
{
HelperMarshal._intValue = 1;
var ex = Assert.Throws<JSException>(() => Utils.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int ();
"));
Assert.Contains("Value is not an integer: undefined (undefined)", ex.Message);
Expand All @@ -467,7 +462,7 @@ public static void BoundStaticMethodExtraArgs()
{
HelperMarshal._intValue = 0;
Utils.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (200, 400);
");
Assert.Equal(200, HelperMarshal._intValue);
Expand All @@ -479,7 +474,7 @@ public static void RangeCheckInt()
HelperMarshal._intValue = 0;
// no numbers bigger than 32 bits
var ex = Assert.Throws<JSException>(() => Utils.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (Number.MAX_SAFE_INTEGER);
"));
Assert.Contains("Overflow: value 9007199254740991 is out of -2147483648 2147483647 range", ex.Message);
Expand All @@ -492,7 +487,7 @@ public static void IntegerCheckInt()
HelperMarshal._intValue = 0;
// no floating point rounding
var ex = Assert.Throws<JSException>(() => Utils.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (3.14);
"));
Assert.Contains("Value is not an integer: 3.14 (number)", ex.Message);
Expand All @@ -505,7 +500,7 @@ public static void TypeCheckInt()
HelperMarshal._intValue = 0;
// no string conversion
var ex = Assert.Throws<JSException>(() => Utils.InvokeJS(@$"
var invoke_int = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
var invoke_int = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeInt"");
invoke_int (""200"");
"));
Assert.Contains("Value is not an integer: 200 (string)", ex.Message);
Expand All @@ -517,7 +512,7 @@ public static void PassUintArgument()
{
HelperMarshal._uintValue = 0;
Utils.InvokeJS(@$"
var invoke_uint = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeUInt"");
var invoke_uint = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeUInt"");
invoke_uint (0xFFFFFFFE);
");

Expand All @@ -530,9 +525,9 @@ public static void ReturnUintEnum()
HelperMarshal._uintValue = 0;
HelperMarshal._enumValue = TestEnum.BigValue;
Utils.InvokeJS(@$"
var get_value = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}GetEnumValue"");
var get_value = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}GetEnumValue"");
var e = get_value ();
var invoke_uint = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeUInt"");
var invoke_uint = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}InvokeUInt"");
invoke_uint (e);
");
Assert.Equal((uint)TestEnum.BigValue, HelperMarshal._uintValue);
Expand All @@ -543,7 +538,7 @@ public static void PassUintEnumByValue()
{
HelperMarshal._enumValue = TestEnum.Zero;
Utils.InvokeJS(@$"
var set_enum = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""j"");
var set_enum = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""j"");
set_enum (0xFFFFFFFE);
");
Assert.Equal(TestEnum.BigValue, HelperMarshal._enumValue);
Expand All @@ -555,7 +550,7 @@ public static void PassUintEnumByNameIsNotImplemented()
HelperMarshal._enumValue = TestEnum.Zero;
var exc = Assert.Throws<JSException>(() =>
Utils.InvokeJS(@$"
var set_enum = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""j"");
var set_enum = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}SetEnumValue"", ""j"");
set_enum (""BigValue"");
")
);
Expand All @@ -567,7 +562,7 @@ public static void CannotUnboxUint64()
{
var exc = Assert.Throws<JSException>(() =>
Utils.InvokeJS(@$"
var get_u64 = INTERNAL.mono_bind_static_method (""{HelperMarshal.INTEROP_CLASS}GetUInt64"", """");
var get_u64 = BINDING.bind_static_method (""{HelperMarshal.INTEROP_CLASS}GetUInt64"", """");
var u64 = get_u64();
")
);
Expand Down Expand Up @@ -679,8 +674,8 @@ public static void InternedStringReturnValuesWork()
HelperMarshal._stringResource = HelperMarshal._stringResource2 = null;
var fqn = "[System.Private.Runtime.InteropServices.JavaScript.Tests]System.Runtime.InteropServices.JavaScript.Tests.HelperMarshal:StoreArgumentAndReturnLiteral";
Utils.InvokeJS(
$"var a = INTERNAL.mono_bind_static_method('{fqn}')('test');\r\n" +
$"var b = INTERNAL.mono_bind_static_method('{fqn}')(a);\r\n" +
$"var a = BINDING.bind_static_method('{fqn}')('test');\r\n" +
$"var b = BINDING.bind_static_method('{fqn}')(a);\r\n" +
"App.call_test_method ('InvokeString2', [ b ]);"
);
Assert.Equal("s: 1 length: 1", HelperMarshal._stringResource);
Expand Down Expand Up @@ -721,7 +716,7 @@ private static async Task<bool> MarshalTask(string helperMethodName, string help
@"globalThis.__test_promise_completed = false; " +
@"globalThis.__test_promise_resolved = false; " +
@"globalThis.__test_promise_failed = false; " +
$@"var t = App.call_test_method ('{helperMethodName}', [ {helperMethodArgs} ], 'i'); " +
$@"var t = App.call_test_method ('{helperMethodName}', [ {helperMethodArgs} ]); " +
"t.then(result => { globalThis.__test_promise_resolved = true; " + resolvedBody + " })" +
" .catch(e => { globalThis.__test_promise_failed = true; })" +
" .finally(result => { globalThis.__test_promise_completed = true; }); " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\JavaScriptImports.Generated.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\JavaScriptExports.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\JavaScriptImports.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\LegacyExports.cs" />

<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Runtime.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Array.cs" />
Expand Down
Loading

0 comments on commit d7c8bc1

Please sign in to comment.