-
Notifications
You must be signed in to change notification settings - Fork 5.1k
[browser][wasm][tests] JavaScript Interop Marshal tests #38917
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
19d0071
[browser][wasm][tests] JavaScript Interop Marshal tests
kjpou1 cd2e3ea
Add primitive and string marshal tests
kjpou1 8b1c4af
Add tests for object identity across marshaling calls for JS object a…
kjpou1 b120694
Add tests and code cleanup
kjpou1 4c15f56
Add marshal of js function tests
kjpou1 1f78463
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 ef21c10
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 b7aa4af
Add delegate marshaling test
kjpou1 edaf9dd
Fix License text
kjpou1 f8aabce
More tests
kjpou1 59fdd58
Fix an error where `mono_method_resolve` is called before BINDING obj…
kjpou1 5d13100
Add more marshal tests
kjpou1 45da7aa
Add more tests
kjpou1 582e954
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 8ca0c83
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 56a5c16
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 7d1e81f
Address review comments
kjpou1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
336 changes: 336 additions & 0 deletions
336
...teropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/HelperMarshal.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,336 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Runtime.InteropServices.JavaScript; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace System.Runtime.InteropServices.JavaScript.Tests | ||
{ | ||
public static class HelperMarshal | ||
{ | ||
internal const string INTEROP_CLASS = "[System.Runtime.InteropServices.JavaScript.Tests]System.Runtime.InteropServices.JavaScript.Tests.HelperMarshal:"; | ||
internal static int _i32Value; | ||
private static void InvokeI32(int a, int b) | ||
{ | ||
_i32Value = a + b; | ||
} | ||
|
||
internal static float _f32Value; | ||
private static void InvokeFloat(float f) | ||
{ | ||
_f32Value = f; | ||
} | ||
|
||
internal static double _f64Value; | ||
private static void InvokeDouble(double d) | ||
{ | ||
_f64Value = d; | ||
} | ||
|
||
internal static long _i64Value; | ||
private static void InvokeLong(long l) | ||
{ | ||
_i64Value = l; | ||
} | ||
|
||
internal static byte[] _byteBuffer; | ||
private static void MarshalArrayBuffer(ArrayBuffer buffer) | ||
{ | ||
using (var bytes = new Uint8Array(buffer)) | ||
_byteBuffer = bytes.ToArray(); | ||
} | ||
|
||
private static void MarshalByteBuffer(Uint8Array buffer) | ||
{ | ||
_byteBuffer = buffer.ToArray(); | ||
} | ||
|
||
internal static int[] _intBuffer; | ||
kjpou1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static void MarshalArrayBufferToInt32Array(ArrayBuffer buffer) | ||
{ | ||
using (var ints = new Int32Array(buffer)) | ||
_intBuffer = ints.ToArray(); | ||
} | ||
|
||
internal static string _stringResource; | ||
private static void InvokeString(string s) | ||
{ | ||
_stringResource = s; | ||
} | ||
|
||
internal static string _marshalledString; | ||
kjpou1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static string InvokeMarshalString() | ||
{ | ||
_marshalledString = "Hic Sunt Dracones"; | ||
return _marshalledString; | ||
} | ||
|
||
internal static object _object1; | ||
kjpou1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static object InvokeObj1(object obj) | ||
{ | ||
_object1 = obj; | ||
return obj; | ||
} | ||
|
||
internal static object _object2; | ||
private static object InvokeObj2(object obj) | ||
{ | ||
_object2 = obj; | ||
return obj; | ||
} | ||
|
||
internal static object _marshalledObject; | ||
private static object InvokeMarshalObj() | ||
{ | ||
_marshalledObject = new object(); | ||
return _marshalledObject; | ||
} | ||
|
||
internal static int _valOne, _valTwo; | ||
private static void ManipulateObject(JSObject obj) | ||
{ | ||
_valOne = (int)obj.Invoke("inc"); | ||
_valTwo = (int)obj.Invoke("add", 20); | ||
} | ||
|
||
internal static object[] _jsObjects; | ||
private static void MinipulateObjTypes(JSObject obj) | ||
{ | ||
_jsObjects = new object[4]; | ||
_jsObjects[0] = obj.Invoke("return_int"); | ||
_jsObjects[1] = obj.Invoke("return_double"); | ||
_jsObjects[2] = obj.Invoke("return_string"); | ||
_jsObjects[3] = obj.Invoke("return_bool"); | ||
} | ||
|
||
internal static int _jsAddFunctionResult; | ||
private static void UseFunction(JSObject obj) | ||
{ | ||
_jsAddFunctionResult = (int)obj.Invoke("call", null, 10, 20); | ||
} | ||
|
||
internal static int _jsAddAsFunctionResult; | ||
private static void UseAsFunction(Function func) | ||
{ | ||
_jsAddAsFunctionResult = (int)func.Call(null, 20, 30); | ||
} | ||
|
||
internal static int _functionResultValue; | ||
private static Func<int, int, int> CreateFunctionDelegate() | ||
{ | ||
return (a, b) => | ||
{ | ||
_functionResultValue = a + b; | ||
return _functionResultValue; | ||
}; | ||
} | ||
|
||
internal static int _intValue; | ||
private static void InvokeInt(int value) | ||
{ | ||
_intValue = value; | ||
} | ||
|
||
internal static IntPtr _intPtrValue; | ||
private static void InvokeIntPtr(IntPtr i) | ||
{ | ||
_intPtrValue = i; | ||
} | ||
|
||
internal static IntPtr _marshaledIntPtrValue; | ||
private static IntPtr InvokeMarshalIntPtr() | ||
{ | ||
_marshaledIntPtrValue = (IntPtr)42; | ||
return _marshaledIntPtrValue; | ||
} | ||
|
||
internal static object[] _jsProperties; | ||
private static void RetrieveObjectProperties(JSObject obj) | ||
{ | ||
_jsProperties = new object[4]; | ||
_jsProperties[0] = obj.GetObjectProperty("myInt"); | ||
_jsProperties[1] = obj.GetObjectProperty("myDouble"); | ||
_jsProperties[2] = obj.GetObjectProperty("myString"); | ||
_jsProperties[3] = obj.GetObjectProperty("myBoolean"); | ||
} | ||
|
||
private static void PopulateObjectProperties(JSObject obj, bool createIfNotExist) | ||
{ | ||
_jsProperties = new object[4]; | ||
obj.SetObjectProperty("myInt", 100, createIfNotExist); | ||
obj.SetObjectProperty("myDouble", 4.5, createIfNotExist); | ||
obj.SetObjectProperty("myString", "qwerty", createIfNotExist); | ||
obj.SetObjectProperty("myBoolean", true, createIfNotExist); | ||
} | ||
|
||
private static void MarshalByteBufferToInts(ArrayBuffer buffer) | ||
{ | ||
using (var bytes = new Uint8Array(buffer)) | ||
{ | ||
var byteBuffer = bytes.ToArray(); | ||
_intBuffer = new int[bytes.Length / sizeof(int)]; | ||
for (int i = 0; i < bytes.Length; i += sizeof(int)) | ||
_intBuffer[i / sizeof(int)] = BitConverter.ToInt32(byteBuffer, i); | ||
} | ||
} | ||
|
||
private static void MarshalInt32Array(Int32Array buffer) | ||
{ | ||
_intBuffer = buffer.ToArray(); | ||
} | ||
|
||
internal static float[] _floatBuffer; | ||
private static void MarshalFloat32Array(Float32Array buffer) | ||
{ | ||
_floatBuffer = buffer.ToArray(); | ||
} | ||
private static void MarshalArrayBufferToFloat32Array(ArrayBuffer buffer) | ||
{ | ||
using (var floats = new Float32Array(buffer)) | ||
_floatBuffer = floats.ToArray(); | ||
} | ||
|
||
internal static double[] _doubleBuffer; | ||
private static void MarshalFloat64Array(Float64Array buffer) | ||
{ | ||
_doubleBuffer = buffer.ToArray(); | ||
} | ||
|
||
private static void MarshalArrayBufferToFloat64Array(ArrayBuffer buffer) | ||
{ | ||
using (var doubles = new Float64Array(buffer)) | ||
_doubleBuffer = doubles.ToArray(); | ||
} | ||
|
||
private static void MarshalByteBufferToDoubles(ArrayBuffer buffer) | ||
kjpou1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
using (var doubles = new Float64Array(buffer)) | ||
_doubleBuffer = doubles.ToArray(); | ||
} | ||
|
||
private static void SetTypedArraySByte(JSObject obj) | ||
{ | ||
sbyte[] buffer = Enumerable.Repeat((sbyte)0x20, 11).ToArray(); | ||
obj.SetObjectProperty("typedArray", Int8Array.From(buffer)); | ||
} | ||
|
||
internal static sbyte[] _taSByte; | ||
private static void GetTypedArraySByte(JSObject obj) | ||
{ | ||
_taSByte = ((Int8Array)obj.GetObjectProperty("typedArray")).ToArray(); | ||
} | ||
|
||
private static void SetTypedArrayByte(JSObject obj) | ||
{ | ||
var dragons = "hic sunt dracones"; | ||
byte[] buffer = System.Text.Encoding.ASCII.GetBytes(dragons); | ||
obj.SetObjectProperty("dracones", Uint8Array.From(buffer)); | ||
} | ||
|
||
internal static byte[] _taByte; | ||
private static void GetTypedArrayByte(JSObject obj) | ||
{ | ||
_taByte = ((Uint8Array)obj.GetObjectProperty("dracones")).ToArray(); | ||
} | ||
|
||
private static void SetTypedArrayShort(JSObject obj) | ||
{ | ||
short[] buffer = Enumerable.Repeat((short)0x20, 13).ToArray(); | ||
obj.SetObjectProperty("typedArray", Int16Array.From(buffer)); | ||
} | ||
|
||
internal static short[] _taShort; | ||
private static void GetTypedArrayShort(JSObject obj) | ||
{ | ||
_taShort = ((Int16Array)obj.GetObjectProperty("typedArray")).ToArray(); | ||
} | ||
|
||
private static void SetTypedArrayUShort(JSObject obj) | ||
{ | ||
ushort[] buffer = Enumerable.Repeat((ushort)0x20, 14).ToArray(); | ||
obj.SetObjectProperty("typedArray", Uint16Array.From(buffer)); | ||
} | ||
|
||
internal static ushort[] _taUShort; | ||
private static void GetTypedArrayUShort(JSObject obj) | ||
{ | ||
_taUShort = ((Uint16Array)obj.GetObjectProperty("typedArray")).ToArray(); | ||
} | ||
|
||
private static void SetTypedArrayInt(JSObject obj) | ||
{ | ||
int[] buffer = Enumerable.Repeat((int)0x20, 15).ToArray(); | ||
obj.SetObjectProperty("typedArray", Int32Array.From(buffer)); | ||
} | ||
|
||
internal static int[] _taInt; | ||
private static void GetTypedArrayInt(JSObject obj) | ||
{ | ||
_taInt = ((Int32Array)obj.GetObjectProperty("typedArray")).ToArray(); | ||
} | ||
|
||
public static void SetTypedArrayUInt(JSObject obj) | ||
{ | ||
uint[] buffer = Enumerable.Repeat((uint)0x20, 16).ToArray(); | ||
obj.SetObjectProperty("typedArray", Uint32Array.From(buffer)); | ||
} | ||
|
||
internal static uint[] _taUInt; | ||
private static void GetTypedArrayUInt(JSObject obj) | ||
{ | ||
_taUInt = ((Uint32Array)obj.GetObjectProperty("typedArray")).ToArray(); | ||
} | ||
|
||
private static void SetTypedArrayFloat(JSObject obj) | ||
{ | ||
float[] buffer = Enumerable.Repeat(3.14f, 17).ToArray(); | ||
obj.SetObjectProperty("typedArray", Float32Array.From(buffer)); | ||
} | ||
|
||
internal static float[] _taFloat; | ||
private static void GetTypedArrayFloat(JSObject obj) | ||
{ | ||
_taFloat = ((Float32Array)obj.GetObjectProperty("typedArray")).ToArray(); | ||
} | ||
|
||
kjpou1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static void SetTypedArrayDouble(JSObject obj) | ||
{ | ||
double[] buffer = Enumerable.Repeat(3.14d, 18).ToArray(); | ||
obj.SetObjectProperty("typedArray", Float64Array.From(buffer)); | ||
} | ||
|
||
internal static double[] _taDouble; | ||
private static void GetTypedArrayDouble(JSObject obj) | ||
{ | ||
_taDouble = ((Float64Array)obj.GetObjectProperty("typedArray")).ToArray(); | ||
} | ||
|
||
private static Function _sumFunction; | ||
private static void CreateFunctionSum() | ||
{ | ||
_sumFunction = new Function("a", "b", "return a + b"); | ||
} | ||
|
||
internal static int _sumValue = 0; | ||
private static void CallFunctionSum() | ||
{ | ||
_sumValue = (int)_sumFunction.Call(null, 3, 5); | ||
} | ||
|
||
private static Function _mathMinFunction; | ||
private static void CreateFunctionApply() | ||
{ | ||
var math = (JSObject)Runtime.GetGlobalObject("Math"); | ||
_mathMinFunction = (Function)math.GetObjectProperty("min"); | ||
|
||
} | ||
|
||
internal static int _minValue = 0; | ||
private static void CallFunctionApply() | ||
{ | ||
_minValue = (int)_mathMinFunction.Apply(null, new object[] { 5, 6, 2, 3, 7 }); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.