Commit ee646748 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Fix test utils and tests

Add a check to appendToTable to catch illegal input, and fix a test
case triggering this check.
Also removing unused variables and fix indentation.

R=ahaas@chromium.org

Change-Id: I0eaa48ab95ef710530a3cfbe94ed4dd419618cda
Reviewed-on: https://chromium-review.googlesource.com/458436
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44027}
parent 1fe5f0e3
...@@ -406,19 +406,18 @@ var failWithMessage; ...@@ -406,19 +406,18 @@ var failWithMessage;
assertThrows = function assertThrows(code, type_opt, cause_opt) { assertThrows = function assertThrows(code, type_opt, cause_opt) {
var threwException = true;
try { try {
if (typeof code === 'function') { if (typeof code === 'function') {
code(); code();
} else { } else {
eval(code); eval(code);
} }
threwException = false;
} catch (e) { } catch (e) {
if (typeof type_opt === 'function') { if (typeof type_opt === 'function') {
assertInstanceof(e, type_opt); assertInstanceof(e, type_opt);
} else if (type_opt !== void 0) { } else if (type_opt !== void 0) {
failWithMessage("invalid use of assertThrows, maybe you want assertThrowsEquals"); failWithMessage(
'invalid use of assertThrows, maybe you want assertThrowsEquals');
} }
if (arguments.length >= 3) { if (arguments.length >= 3) {
assertEquals(e.message, cause_opt); assertEquals(e.message, cause_opt);
...@@ -426,7 +425,7 @@ var failWithMessage; ...@@ -426,7 +425,7 @@ var failWithMessage;
// Success. // Success.
return; return;
} }
failWithMessage("Did not throw exception"); failWithMessage('Did not throw exception');
}; };
......
...@@ -74,7 +74,7 @@ module = (function () { ...@@ -74,7 +74,7 @@ module = (function () {
kExprCallIndirect, sig_i_ii, kTableZero kExprCallIndirect, sig_i_ii, kTableZero
]) ])
.exportFunc(); .exportFunc();
builder.appendToTable([mul.index, add.index, popcnt.index, main.index]); builder.appendToTable([mul, add.index, popcnt.index, main.index]);
return builder.instantiate({q: {mul: function(a, b) { return a * b | 0; }}}); return builder.instantiate({q: {mul: function(a, b) { return a * b | 0; }}});
})(); })();
......
...@@ -339,36 +339,34 @@ let kTrapMsgs = [ ...@@ -339,36 +339,34 @@ let kTrapMsgs = [
]; ];
function assertTraps(trap, code) { function assertTraps(trap, code) {
var threwException = true; try {
try { if (typeof code === 'function') {
if (typeof code === 'function') { code();
code(); } else {
} else { eval(code);
eval(code);
}
threwException = false;
} catch (e) {
assertEquals("object", typeof e);
assertEquals(kTrapMsgs[trap], e.message);
// Success.
return;
} }
throw new MjsUnitAssertionError("Did not trap, expected: " + kTrapMsgs[trap]); } catch (e) {
assertEquals('object', typeof e);
assertEquals(kTrapMsgs[trap], e.message);
// Success.
return;
}
throw new MjsUnitAssertionError('Did not trap, expected: ' + kTrapMsgs[trap]);
} }
function assertWasmThrows(value, code) { function assertWasmThrows(value, code) {
assertEquals("number", typeof(value)); assertEquals('number', typeof value);
try { try {
if (typeof code === 'function') { if (typeof code === 'function') {
code(); code();
} else { } else {
eval(code); eval(code);
}
} catch (e) {
assertEquals("number", typeof e);
assertEquals(value, e);
// Success.
return;
} }
throw new MjsUnitAssertionError("Did not throw at all, expected: " + value); } catch (e) {
assertEquals('number', typeof e);
assertEquals(value, e);
// Success.
return;
}
throw new MjsUnitAssertionError('Did not throw, expected: ' + value);
} }
...@@ -98,7 +98,8 @@ class WasmFunctionBuilder { ...@@ -98,7 +98,8 @@ class WasmFunctionBuilder {
addBody(body) { addBody(body) {
for (let b of body) { for (let b of body) {
if (typeof b != 'number') throw new Error('invalid body: ' + body); if (typeof b != 'number')
throw new Error('invalid body (entries have to be numbers): ' + body);
} }
this.body = body; this.body = body;
// Automatically add the end for the function block to the body. // Automatically add the end for the function block to the body.
...@@ -264,6 +265,10 @@ class WasmModuleBuilder { ...@@ -264,6 +265,10 @@ class WasmModuleBuilder {
} }
appendToTable(array) { appendToTable(array) {
for (let n of array) {
if (typeof n != 'number')
throw new Error('invalid table (entries have to be numbers): ' + array);
}
return this.addFunctionTableInit(this.function_table.length, false, array); return this.addFunctionTableInit(this.function_table.length, false, array);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment