Skip to content

Commit 4a2ebf2

Browse files
authored
[wasm] reduce eval in production code (dotnet#71932)
* reduce eval in production code * fix for JSExport nested types * update benchmark to use JSExport/JSImport instead of reflection * fix console benchmark
1 parent cae954a commit 4a2ebf2

File tree

25 files changed

+478
-191
lines changed

25 files changed

+478
-191
lines changed

src/libraries/Common/src/Interop/Browser/Interop.Runtime.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ internal static unsafe partial class Runtime
2525

2626
#region Legacy
2727

28-
[MethodImplAttribute(MethodImplOptions.InternalCall)]
29-
internal static extern string InvokeJS(string str, out int exceptionalResult);
3028
[MethodImplAttribute(MethodImplOptions.InternalCall)]
3129
internal static extern void InvokeJSWithArgsRef(IntPtr jsHandle, in string method, in object?[] parms, out int exceptionalResult, out object result);
3230
[MethodImplAttribute(MethodImplOptions.InternalCall)]

src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/DelegateTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static void InvokeFunction()
1818
HelperMarshal._functionResultValue = 0;
1919
HelperMarshal._i32Value = 0;
2020

21-
Runtime.InvokeJS(@"
21+
Utils.InvokeJS(@"
2222
var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
2323
var res = funcDelegate (10, 20);
2424
App.call_test_method (""InvokeI32"", [ res, res ]);
@@ -34,7 +34,7 @@ public static void InvokeFunctionInLoopUsingConstanceValues()
3434
HelperMarshal._functionResultValue = 0;
3535
HelperMarshal._i32Value = 0;
3636

37-
Runtime.InvokeJS(@"
37+
Utils.InvokeJS(@"
3838
var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
3939
var res = funcDelegate (10, 20);
4040
for (let x = 0; x < 1000; x++)
@@ -53,7 +53,7 @@ public static void InvokeFunctionInLoopUsingIncrementedValues()
5353
{
5454
HelperMarshal._functionResultValue = 0;
5555
HelperMarshal._i32Value = 0;
56-
Runtime.InvokeJS(@"
56+
Utils.InvokeJS(@"
5757
var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
5858
var res = funcDelegate (10, 20);
5959
for (let x = 0; x < 1000; x++)
@@ -73,7 +73,7 @@ public static void InvokeActionTReturnedByInvokingFuncT()
7373
HelperMarshal._functionActionResultValue = 0;
7474
HelperMarshal._functionActionResultValueOfAction = 0;
7575

76-
Runtime.InvokeJS(@"
76+
Utils.InvokeJS(@"
7777
var funcDelegate = App.call_test_method (""CreateFunctionDelegateWithAction"", [ ]);
7878
var actionDelegate = funcDelegate (10, 20);
7979
actionDelegate(30,40);
@@ -88,7 +88,7 @@ public static void InvokeActionIntInt()
8888
{
8989
HelperMarshal._actionResultValue = 0;
9090

91-
Runtime.InvokeJS(@"
91+
Utils.InvokeJS(@"
9292
var actionDelegate = App.call_test_method (""CreateActionDelegate"", [ ]);
9393
actionDelegate(30,40);
9494
");
@@ -100,7 +100,7 @@ public static void InvokeActionIntInt()
100100
public static void InvokeActionFloatIntToIntInt()
101101
{
102102
HelperMarshal._actionResultValue = 0;
103-
var ex = Assert.Throws<JSException>(()=>Runtime.InvokeJS(@"
103+
var ex = Assert.Throws<JSException>(()=>Utils.InvokeJS(@"
104104
var actionDelegate = App.call_test_method (""CreateActionDelegate"", [ ]);
105105
actionDelegate(3.14,40);
106106
"));
@@ -113,7 +113,7 @@ public static void InvokeActionFloatIntToIntInt()
113113
public static void InvokeDelegateMethod()
114114
{
115115
HelperMarshal._delMethodResultValue = string.Empty;
116-
Runtime.InvokeJS(@"
116+
Utils.InvokeJS(@"
117117
var del = App.call_test_method (""CreateDelegateMethod"", [ ]);
118118
del(""Hic sunt dracones"");
119119
");
@@ -125,7 +125,7 @@ public static void InvokeDelegateMethod()
125125
public static void InvokeDelegateMethodReturnString()
126126
{
127127
HelperMarshal._delMethodStringResultValue = string.Empty;
128-
Runtime.InvokeJS(@"
128+
Utils.InvokeJS(@"
129129
var del = App.call_test_method (""CreateDelegateMethodReturnString"", [ ]);
130130
var res = del(""Hic sunt dracones"");
131131
App.call_test_method (""SetTestString1"", [ res ]);
@@ -140,7 +140,7 @@ public static void InvokeDelegateMethodReturnString()
140140
public static void InvokeMultiCastDelegate_VoidString(string creator, string testStr)
141141
{
142142
HelperMarshal._delegateCallResult = string.Empty;
143-
Runtime.InvokeJS($@"
143+
Utils.InvokeJS($@"
144144
var del = App.call_test_method (""{creator}"", [ ]);
145145
del(""{testStr}"");
146146
");
@@ -155,7 +155,7 @@ public static void InvokeMultiCastDelegate_VoidString(string creator, string tes
155155
public static void InvokeDelegate_VoidString(string creator)
156156
{
157157
HelperMarshal._delegateCallResult = string.Empty;
158-
var s = Runtime.InvokeJS($@"
158+
var s = Utils.InvokeJS($@"
159159
var del = App.call_test_method (""{creator}"", [ ]);
160160
del(""Hic sunt dracones"");
161161
");
@@ -178,7 +178,7 @@ public static void InvokeFunctionAcceptingArrayTypes(Function objectPrototype, s
178178
Assert.Equal(10, HelperMarshal._funcActionBufferObjectResultValue.GetObjectProperty("length"));
179179
Assert.Equal($"[object {creator}]", objectPrototype.Call(HelperMarshal._funcActionBufferObjectResultValue));
180180

181-
Runtime.InvokeJS($@"
181+
Utils.InvokeJS($@"
182182
var buffer = new {creator}(50);
183183
var del = App.call_test_method (""CreateFunctionAccepting{creator}"", [ ]);
184184
var setAction = del(buffer);
@@ -253,7 +253,7 @@ public static void EventsAreNotCollected()
253253
var evnti = dispatcher.Invoke("eventFactory", i);
254254
dispatcher.Invoke("fireEvent", evnti);
255255
dispatcher.Invoke("fireEvent", evnt);
256-
Runtime.InvokeJS("if (globalThis.gc) globalThis.gc();");// needs v8 flag --expose-gc
256+
Utils.InvokeJS("if (globalThis.gc) globalThis.gc();");// needs v8 flag --expose-gc
257257
}
258258
}
259259

@@ -307,7 +307,7 @@ public static async Task ResolveJSObjectPromise()
307307
var value = (JSObject)await promise;
308308

309309
Assert.Equal("bar", value.GetObjectProperty("foo"));
310-
Runtime.InvokeJS("if (globalThis.gc) globalThis.gc();");// needs v8 flag --expose-gc
310+
Utils.InvokeJS("if (globalThis.gc) globalThis.gc();");// needs v8 flag --expose-gc
311311
}
312312
}
313313

src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/Http/HttpRequestMessageTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public void ToString_NonDefaultInstanceWithCustomHeaders_DumpAllFields(string ur
374374
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBrowserDomSupported))]
375375
public async Task BlobUri_Marshal_CorrectValues_Browser()
376376
{
377-
Runtime.InvokeJS(@"
377+
Utils.InvokeJS(@"
378378
function typedArrayToURL(typedArray, mimeType) {
379379
return URL.createObjectURL(new Blob([typedArray.buffer], {type: mimeType}))
380380
}
@@ -400,7 +400,7 @@ function typedArrayToURL(typedArray, mimeType) {
400400
[Fact]
401401
public void BlobStringUri_Marshal_CorrectValues()
402402
{
403-
Runtime.InvokeJS(@"
403+
Utils.InvokeJS(@"
404404
function typedArrayToURL(typedArray, mimeType) {
405405
// URL.createObjectURL does not work outside of browser but since this was actual
406406
// test code from https://developer.mozilla.org/en-US/docs/Web/API/Blob
@@ -429,7 +429,7 @@ function typedArrayToURL(typedArray, mimeType) {
429429
[Fact]
430430
public void BlobUri_Marshal_CorrectValues()
431431
{
432-
Runtime.InvokeJS(@"
432+
Utils.InvokeJS(@"
433433
function typedArrayToURL(typedArray, mimeType) {
434434
// URL.createObjectURL does not work outside of browser but since this was actual
435435
// test code from https://developer.mozilla.org/en-US/docs/Web/API/Blob

src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/JavaScriptTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static async Task BagIterator()
8989
var x = new byte[100 + attempt / 100];
9090
if (attempt % 1000 == 0)
9191
{
92-
Runtime.InvokeJS("if (globalThis.gc) globalThis.gc();");// needs v8 flag --expose-gc
92+
Utils.InvokeJS("if (globalThis.gc) globalThis.gc();");// needs v8 flag --expose-gc
9393
GC.Collect();
9494
}
9595
}

0 commit comments

Comments
 (0)