Skip to content

Commit

Permalink
[WASM] Enabled strict mode on all JS files (#54011)
Browse files Browse the repository at this point in the history
"use strict" for all wasm JavaScript

Co-authored-by: Daniel Genkin <t-dgenkin@microsoft.com>
Co-authored-by: Pavel Savara <pavelsavara@microsoft.com>
  • Loading branch information
3 people committed Sep 14, 2021
1 parent f2ce707 commit 55622ab
Show file tree
Hide file tree
Showing 12 changed files with 135 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void InvokeFunctionInLoopUsingConstanceValues()
Runtime.InvokeJS(@"
var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
var res = funcDelegate (10, 20);
for (x = 0; x < 1000; x++)
for (let x = 0; x < 1000; x++)
{
res = funcDelegate (10, 20);
}
Expand All @@ -55,7 +55,7 @@ public static void InvokeFunctionInLoopUsingIncrementedValues()
Runtime.InvokeJS(@"
var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
var res = funcDelegate (10, 20);
for (x = 0; x < 1000; x++)
for (let x = 0; x < 1000; x++)
{
res = funcDelegate (x, x);
}
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/mbr/browser/runtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";
var Module = {
config: null,

Expand Down
2 changes: 2 additions & 0 deletions src/mono/sample/wasm/browser-bench/runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";
var Module = {
config: null,

Expand Down
2 changes: 2 additions & 0 deletions src/mono/sample/wasm/browser-profile/runtime.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";
var Module = {
is_testing: false,
config: null,
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/browser/runtime.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";
var Module = {

config: null,
Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/debugger/tests/debugger-test/other.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";

function big_array_js_test (len) {
var big = new Array(len);
for (let i=0; i < len; i ++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";

var Module = {
config: null,

Expand Down
143 changes: 70 additions & 73 deletions src/mono/wasm/runtime-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// -*- mode: js; js-indent-level: 4; -*-
//
// Run runtime tests under a JS shell or a browser
//
"use strict";

//glue code to deal with the differences between chrome, ch, d8, jsc and sm.
var is_browser = typeof window != "undefined";
Expand All @@ -13,9 +16,36 @@ if (typeof (console) === "undefined") {
clear: function () { }
};
}

globalThis.testConsole = console;

//define arguments for later
var allRuntimeArguments = null;
try {
if (is_browser) {
// We expect to be run by tests/runtime/run.js which passes in the arguments using http parameters
const url = new URL(decodeURI(window.location));
allRuntimeArguments = [];
for (let param of url.searchParams) {
if (param[0] == "arg") {
allRuntimeArguments.push(param[1]);
}
}

} else if (typeof arguments !== "undefined" && typeof arguments !== "null") {
allRuntimeArguments = arguments;
} else if (typeof process !== 'undefined' && typeof process.argv !== "undefined") {
allRuntimeArguments = process.argv.slice(2);
} else if (typeof scriptArgs !== "undefined") {
allRuntimeArguments = scriptArgs;
} else if (typeof WScript !== "undefined" && WScript.Arguments) {
allRuntimeArguments = WScript.Arguments;
} else {
allRuntimeArguments = [];
}
} catch (e) {
console.log(e);
}

function proxyMethod (prefix, func, asJson) {
return function() {
const args = [...arguments];
Expand Down Expand Up @@ -66,15 +96,6 @@ if (is_browser) {
consoleWebSocket.onerror = function(event) {
console.log(`websocket error: ${event}`);
};

// We expect to be run by tests/runtime/run.js which passes in the arguments using http parameters
var url = new URL (decodeURI (window.location));
arguments = [];
for (var v of url.searchParams) {
if (v [0] == "arg") {
arguments.push (v [1]);
}
}
}
//proxyJson(console.log);

Expand Down Expand Up @@ -103,30 +124,8 @@ if (typeof performance == 'undefined') {
}
}

try {
if (typeof arguments == "undefined")
arguments = WScript.Arguments;
load = WScript.LoadScriptFile;
read = WScript.LoadBinaryFile;
} catch (e) {
}

try {
if (typeof arguments == "undefined") {
if (typeof scriptArgs !== "undefined")
arguments = scriptArgs;
}
} catch (e) {
}

if (arguments === undefined)
arguments = [];

//end of all the nice shell glue code.

// set up a global variable to be accessed in App.init
var testArguments = arguments;

function test_exit (exit_code) {
if (is_browser) {
// Notify the selenium script
Expand Down Expand Up @@ -156,44 +155,42 @@ function inspect_object (o) {
}

// Preprocess arguments
var args = testArguments;
console.info("Arguments: " + testArguments);
profilers = [];
setenv = {};
runtime_args = [];
enable_gc = true;
enable_zoneinfo = false;
working_dir='/';
while (args !== undefined && args.length > 0) {
if (args [0].startsWith ("--profile=")) {
var arg = args [0].substring ("--profile=".length);
console.info("Arguments: " + allRuntimeArguments);
var profilers = [];
var setenv = {};
var runtime_args = [];
var enable_gc = true;
var enable_zoneinfo = false;
var working_dir='/';
while (allRuntimeArguments !== undefined && allRuntimeArguments.length > 0) {
if (allRuntimeArguments [0].startsWith ("--profile=")) {
var arg = allRuntimeArguments [0].substring ("--profile=".length);

profilers.push (arg);

args = args.slice (1);
} else if (args [0].startsWith ("--setenv=")) {
var arg = args [0].substring ("--setenv=".length);
allRuntimeArguments = allRuntimeArguments.slice (1);
} else if (allRuntimeArguments [0].startsWith ("--setenv=")) {
var arg = allRuntimeArguments [0].substring ("--setenv=".length);
var parts = arg.split ('=');
if (parts.length < 2)
fail_exec ("Error: malformed argument: '" + args [0]);
setenv [parts [0]] = arg.substring (parts [0].length + 1);
args = args.slice (1);
} else if (args [0].startsWith ("--runtime-arg=")) {
var arg = args [0].substring ("--runtime-arg=".length);
runtime_args.push (arg);
args = args.slice (1);
} else if (args [0] == "--disable-on-demand-gc") {
if (parts.length != 2)
fail_exec ("Error: malformed argument: '" + allRuntimeArguments [0]);
setenv [parts [0]] = parts [1];
allRuntimeArguments = allRuntimeArguments.slice (1);
} else if (allRuntimeArguments [0].startsWith ("--runtime-arg=")) {
var arg = allRuntimeArguments [0].substring ("--runtime-arg=".length);
runtime_args = allRuntimeArguments.push (arg);
allRuntimeArguments = allRuntimeArguments.slice (1);
} else if (allRuntimeArguments [0] == "--disable-on-demand-gc") {
enable_gc = false;
args = args.slice (1);
} else if (args [0].startsWith ("--working-dir=")) {
var arg = args [0].substring ("--working-dir=".length);
allRuntimeArguments = allRuntimeArguments.slice (1);
} else if (allRuntimeArguments [0].startsWith ("--working-dir=")) {
var arg = allRuntimeArguments [0].substring ("--working-dir=".length);
working_dir = arg;
args = args.slice (1);
allRuntimeArguments = allRuntimeArguments.slice (1);
} else {
break;
}
}
testArguments = args;

// cheap way to let the testing infrastructure know we're running in a browser context (or not)
setenv["IsBrowserDomSupported"] = is_browser.toString().toLowerCase();
Expand Down Expand Up @@ -316,17 +313,17 @@ var App = {
init ("");
}

if (args.length == 0) {
if (allRuntimeArguments.length == 0) {
fail_exec ("Missing required --run argument");
return;
}

if (args[0] == "--regression") {
if (allRuntimeArguments[0] == "--regression") {
var exec_regression = Module.cwrap ('mono_wasm_exec_regression', 'number', ['number', 'string'])

var res = 0;
try {
res = exec_regression (10, args[1]);
res = exec_regression (10, allRuntimeArguments[1]);
Module.print ("REGRESSION RESULT: " + res);
} catch (e) {
Module.print ("ABORT: " + e);
Expand All @@ -343,23 +340,23 @@ var App = {
if (runtime_args.length > 0)
MONO.mono_wasm_set_runtime_options (runtime_args);

if (args[0] == "--run") {
if (allRuntimeArguments[0] == "--run") {
// Run an exe
if (args.length == 1) {
if (allRuntimeArguments.length == 1) {
fail_exec ("Error: Missing main executable argument.");
return;
}

main_assembly_name = args[1];
var app_args = args.slice (2);
var main_assembly_name = allRuntimeArguments[1];
var app_args = allRuntimeArguments.slice (2);

var main_argc = args.length - 2 + 1;
var main_argc = allRuntimeArguments.length - 2 + 1;
var main_argv = Module._malloc (main_argc * 4);
aindex = 0;
Module.setValue (main_argv + (aindex * 4), wasm_strdup (args [1]), "i32")
var aindex = 0;
Module.setValue (main_argv + (aindex * 4), wasm_strdup (allRuntimeArguments [1]), "i32")
aindex += 1;
for (var i = 2; i < args.length; ++i) {
Module.setValue (main_argv + (aindex * 4), wasm_strdup (args [i]), "i32");
for (var i = 2; i < allRuntimeArguments.length; ++i) {
Module.setValue (main_argv + (aindex * 4), wasm_strdup (allRuntimeArguments [i]), "i32");
aindex += 1;
}
wasm_set_main_args (main_argc, main_argv);
Expand All @@ -381,7 +378,7 @@ var App = {
}

} else {
fail_exec ("Unhandled argument: " + args [0]);
fail_exec ("Unhandled argument: " + allRuntimeArguments [0]);
}
},
call_test_method: function (method_name, args, signature) {
Expand Down
18 changes: 10 additions & 8 deletions src/mono/wasm/runtime/binding_support.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";

var BindingSupportLib = {
$BINDING__postset: 'BINDING.export_functions (Module);',
$BINDING: {
Expand Down Expand Up @@ -776,6 +778,7 @@ var BindingSupportLib = {
case typeof js_obj === "undefined":
return 0;
case typeof js_obj === "number": {
let result = null;
if ((js_obj | 0) === js_obj)
result = this._box_js_int (js_obj);
else if ((js_obj >>> 0) === js_obj)
Expand Down Expand Up @@ -1033,7 +1036,7 @@ var BindingSupportLib = {
},

_create_named_function: function (name, argumentNames, body, closure) {
var result = null, keys = null, closureArgumentList = null, closureArgumentNames = null;
var result = null, closureArgumentList = null, closureArgumentNames = null;

if (closure) {
closureArgumentNames = Object.keys (closure);
Expand Down Expand Up @@ -1135,7 +1138,7 @@ var BindingSupportLib = {

var conv = primitiveConverters.get (key);
if (!conv)
throw new Error ("Unknown parameter type " + type);
throw new Error ("Unknown parameter type " + key);

var localStep = Object.create (conv.steps[0]);
localStep.size = conv.size;
Expand Down Expand Up @@ -1185,7 +1188,7 @@ var BindingSupportLib = {

// worst-case allocation size instead of allocating dynamically, plus padding
var bufferSizeBytes = converter.size + (args_marshal.length * 4) + 16;
var rootBufferSize = args_marshal.length;

// ensure the indirect values are 8-byte aligned so that aligned loads and stores will work
var indirectBaseOffset = ((((args_marshal.length * 4) + 7) / 8) | 0) * 8;

Expand Down Expand Up @@ -1286,7 +1289,7 @@ var BindingSupportLib = {

body.push(");");

bodyJs = body.join ("\r\n");
var bodyJs = body.join ("\r\n");
try {
compiledVariadicFunction = this._create_named_function("variadic_converter_" + converterName, argumentNames, bodyJs, closure);
converter.compiled_variadic_function = compiledVariadicFunction;
Expand Down Expand Up @@ -1473,10 +1476,10 @@ var BindingSupportLib = {
) {
this._handle_exception_for_call (converter, buffer, resultRoot, exceptionRoot, argsRootBuffer);

let result = resultRoot.value;

if (is_result_marshaled)
result = this._unbox_mono_obj_root (resultRoot);
else
result = resultRoot.value;

this._teardown_after_call (converter, buffer, resultRoot, exceptionRoot, argsRootBuffer);
return result;
Expand Down Expand Up @@ -1615,7 +1618,7 @@ var BindingSupportLib = {
"return result;"
);

bodyJs = body.join ("\r\n");
var bodyJs = body.join ("\r\n");

if (friendly_name) {
var escapeRE = /[^A-Za-z0-9_]/g;
Expand Down Expand Up @@ -1865,7 +1868,6 @@ var BindingSupportLib = {
js_obj[property] = js_value;
result = true;
}

}
return BINDING._box_js_bool (result);
} finally {
Expand Down
4 changes: 3 additions & 1 deletion src/mono/wasm/runtime/dotnet_support.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

"use strict";

var DotNetSupportLib = {
$DOTNET: {
conv_string: function (mono_obj) {
Expand Down Expand Up @@ -46,7 +48,7 @@ var DotNetSupportLib = {
var funcNameJsString = DOTNET.conv_string(functionName);
var argsJsonJsString = argsJson && DOTNET.conv_string (argsJson);

var dotNetExports = globaThis.DotNet;
var dotNetExports = globalThis.DotNet;
if (!dotNetExports) {
throw new Error('The Microsoft.JSInterop.js library is not loaded.');
}
Expand Down
Loading

0 comments on commit 55622ab

Please sign in to comment.