Skip to content

Commit

Permalink
Add more error handling and diagnostic messages
Browse files Browse the repository at this point in the history
Fix a couple broken tests
  • Loading branch information
kg committed Oct 14, 2020
1 parent e70adaa commit f83e6cf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,10 @@ public static void MarshalTypedArrayInt()
{
Runtime.InvokeJS(@"
var obj = { };
App.call_test_method (""SetTypedArrayInt"", ""o"", [ obj ]);
App.call_test_method (""GetTypedArrayInt"", ""o"", [ obj ]);
App.call_test_method (""SetTypedArrayInt"", [ obj ]);
App.call_test_method (""GetTypedArrayInt"", [ obj ]);
");
Assert.NotEqual(null, HelperMarshal._taInt);
Assert.Equal(15, HelperMarshal._taInt.Length);
Assert.Equal(32, HelperMarshal._taInt[0]);
Assert.Equal(32, HelperMarshal._taInt[HelperMarshal._taInt.Length - 1]);
Expand Down Expand Up @@ -603,8 +604,8 @@ public static void MarshalTypedArrayDouble()
{
Runtime.InvokeJS(@"
var obj = { };
App.call_test_method (""SetTypedArrayDouble"", ""o"", [ obj ]);
App.call_test_method (""GetTypedArrayDouble"", ""o"", [ obj ]);
App.call_test_method (""SetTypedArrayDouble"", [ obj ]);
App.call_test_method (""GetTypedArrayDouble"", [ obj ]);
");
Assert.Equal(18, HelperMarshal._taDouble.Length);
Assert.Equal(3.14d, HelperMarshal._taDouble[0]);
Expand All @@ -616,8 +617,8 @@ public static void TestFunctionSum()
{
HelperMarshal._sumValue = 0;
Runtime.InvokeJS(@"
App.call_test_method (""CreateFunctionSum"", null, [ ]);
App.call_test_method (""CallFunctionSum"", null, [ ]);
App.call_test_method (""CreateFunctionSum"", []);
App.call_test_method (""CallFunctionSum"", []);
");
Assert.Equal(8, HelperMarshal._sumValue);
}
Expand All @@ -627,8 +628,8 @@ public static void TestFunctionApply()
{
HelperMarshal._minValue = 0;
Runtime.InvokeJS(@"
App.call_test_method (""CreateFunctionApply"", null, [ ]);
App.call_test_method (""CallFunctionApply"", null, [ ]);
App.call_test_method (""CreateFunctionApply"", []);
App.call_test_method (""CallFunctionApply"", []);
");
Assert.Equal(2, HelperMarshal._minValue);
}
Expand Down
11 changes: 10 additions & 1 deletion src/mono/wasm/runtime-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,15 @@ var App = {
}
},
call_test_method: function (method_name, args) {
return BINDING.call_static_method("[System.Private.Runtime.InteropServices.JavaScript.Tests]System.Runtime.InteropServices.JavaScript.Tests.HelperMarshal:" + method_name, args);
if (arguments.length > 2)
throw new Error("Invalid number of arguments for call_test_method");

var fqn = "[System.Private.Runtime.InteropServices.JavaScript.Tests]System.Runtime.InteropServices.JavaScript.Tests.HelperMarshal:" + method_name;
try {
return BINDING.call_static_method(fqn, args || []);
} catch (exc) {
console.error("exception thrown in", fqn);
throw exc;
}
}
};
4 changes: 3 additions & 1 deletion src/mono/wasm/runtime/binding_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ var BindingSupportLib = {
},

_verify_args_for_method_call: function (args_marshal, args) {
var has_args = (typeof args === "object") && args.length > 0;
var has_args = args && (typeof args === "object") && args.length > 0;
var has_args_marshal = typeof args_marshal === "string";

if (has_args) {
Expand Down Expand Up @@ -1144,6 +1144,8 @@ var BindingSupportLib = {
// Detect someone accidentally passing the wrong type of value to method
if ((method | 0) !== method)
throw new Error ("method must be an address in the native heap");
if (!method)
throw new Error ("no method specified");

var needs_converter = this._verify_args_for_method_call (args_marshal, args);

Expand Down
12 changes: 11 additions & 1 deletion src/mono/wasm/runtime/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,20 @@ mono_wasm_invoke_js (MonoString *str, int *is_exception)
res = res.toString ();
setValue ($2, 0, "i32");
} catch (e) {
res = e.toString ();
res = e.toString();
setValue ($2, 1, "i32");
if (res === null || res === undefined)
res = "unknown exception";

var stack = e.stack;
if (stack) {
// Some JS runtimes insert the error message at the top of the stack, some don't,
// so normalize it by using the stack as the result if it already contains the error
if (stack.startsWith(res))
res = stack;
else
res += "\n" + stack;
}
}
var buff = Module._malloc((res.length + 1) * 2);
stringToUTF16 (res, buff, (res.length + 1) * 2);
Expand Down

0 comments on commit f83e6cf

Please sign in to comment.