Commit a99423c3 authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

Revert "[test] Refactor assertPromiseResult"

This reverts commit 361bb1a0.

Reason for revert: See https://crbug.com/v8/6981

BUG=v8:6981

Original change's description:
> [test] Refactor assertPromiseResult
>
> This patch introduces assertPromiseFulfills and assertPromiseFulfills as
> a replacement for assertPromiseResult because it’s more JavaScript-y.
>
> BUG=v8:6921
> R=ahaas@chromium.org
>
> Also-By: ahaas@chromium.org
> Change-Id: I2f865dba3992ddf3b58987bf0b376d143edb5c31
> Reviewed-on: https://chromium-review.googlesource.com/718746
> Commit-Queue: Andreas Haas <ahaas@chromium.org>
> Reviewed-by: Andreas Haas <ahaas@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#48578}

Change-Id: Ie760d2422451f16acc616aae001fe9fd18bf5cd4
Reviewed-on: https://chromium-review.googlesource.com/738249Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48936}
parent a74da818
......@@ -132,9 +132,8 @@ var assertContains;
// Assert that a string matches a given regex.
var assertMatches;
// Assert the fulfillment or rejection of a promise.
var assertPromiseFulfills;
var assertPromiseRejects;
// Assert the result of a promise.
var assertPromiseResult;
var promiseTestChain;
var promiseTestCount = 0;
......@@ -531,40 +530,42 @@ var failWithMessage;
}
};
assertPromiseFulfills = (promise) => {
// waitUntilDone is idempotent.
testRunner.waitUntilDone();
++promiseTestCount;
return promise.then((value) => {
if (--promiseTestCount == 0) {
testRunner.notifyDone();
}
return value;
}).catch((error) => {
// Use `setTimeout` to cause the assert to trigger outside of the promise,
// since promises swallow exceptions.
setTimeout(() => {
assertUnreachable("Promise was rejected unexpectedly");
});
});
};
assertPromiseResult = function(promise, success, fail) {
// Use --allow-natives-syntax to use this function. Note that this function
// overwrites {failWithMessage} permanently with %AbortJS.
// We have to patch mjsunit because normal assertion failures just throw
// exceptions which are swallowed in a then clause.
// We use eval here to avoid parsing issues with the natives syntax.
if (!success) success = () => {};
failWithMessage = (msg) => eval("%AbortJS(msg)");
if (!fail) {
fail = result => failWithMessage("assertPromiseResult failed: " + result);
}
var test_promise =
promise.then(
result => {
try {
success(result);
} catch (e) {
failWithMessage(String(e));
}
},
result => {
fail(result);
}
)
.then((x)=> {
if (--promiseTestCount == 0) testRunner.notifyDone();
});
assertPromiseRejects = (promise) => {
if (!promiseTestChain) promiseTestChain = Promise.resolve();
// waitUntilDone is idempotent.
testRunner.waitUntilDone();
++promiseTestCount;
return promise.then(() => {
// Use `setTimeout` to cause the assert to trigger outside of the promise,
// since promises swallow exceptions.
setTimeout(() => {
assertUnreachable("Promise was fulfilled unexpectedly");
});
}).catch((error) => {
if (--promiseTestCount == 0) {
testRunner.notifyDone();
}
return error;
});
return promiseTestChain.then(test_promise);
};
var OptimizationStatusImpl = undefined;
......
......@@ -12,8 +12,9 @@ const bytes = new Uint8Array([
0x08, 0x01, 0x06, 0x00, 0x41, 0x01, 0x40, 0x00, 0x0b
]);
assertPromiseFulfills(WebAssembly.compile(bytes))
.then(module => {
print('promise resolved: ' + module);
new WebAssembly.Instance(module).exports.grow();
});
assertPromiseResult(
WebAssembly.compile(bytes),
module => {
print('promise resolved: ' + module);
new WebAssembly.Instance(module).exports.grow();
});
......@@ -7,22 +7,22 @@
load('test/mjsunit/wasm/wasm-constants.js');
load('test/mjsunit/wasm/wasm-module-builder.js');
const importingModuleBinary1 = (() => {
const builder = new WasmModuleBuilder();
let importingModuleBinary1 = (() => {
var builder = new WasmModuleBuilder();
builder.addImport('', 'f', kSig_i_v);
return new Int8Array(builder.toBuffer());
})();
const importingModuleBinary2 = (() => {
const builder = new WasmModuleBuilder();
let importingModuleBinary2 = (() => {
var builder = new WasmModuleBuilder();
builder.addImport('', 'f', kSig_i_v);
builder.addFunction('g', kSig_v_v)
.addBody([kExprNop]);
return new Int8Array(builder.toBuffer());
})();
const importingModuleBinary3 = (() => {
const builder = new WasmModuleBuilder();
let importingModuleBinary3 = (() => {
var builder = new WasmModuleBuilder();
builder.addImport('', 'f', kSig_i_v);
builder.addImport('', 'f2', kSig_i_v);
builder.addFunction('g', kSig_v_v)
......@@ -30,15 +30,15 @@ const importingModuleBinary3 = (() => {
return new Int8Array(builder.toBuffer());
})();
const importingModuleBinary4 = (() => {
const builder = new WasmModuleBuilder();
let importingModuleBinary4 = (() => {
var builder = new WasmModuleBuilder();
builder.addFunction('g', kSig_v_v)
.addBody([kExprNop]);
return new Int8Array(builder.toBuffer());
})();
const complexImportingModuleBinary1 = (() => {
const builder = new WasmModuleBuilder();
let builder = new WasmModuleBuilder();
builder.addImport('a', 'b', kSig_v_v);
builder.addImportedMemory('c', 'd', 1);
......@@ -50,7 +50,7 @@ const complexImportingModuleBinary1 = (() => {
})();
const complexImportingModuleBinary2 = (() => {
const builder = new WasmModuleBuilder();
let builder = new WasmModuleBuilder();
builder.addImport('a', 'b', kSig_v_v);
builder.addImportedMemory('c', 'd', 1);
......@@ -61,7 +61,7 @@ const complexImportingModuleBinary2 = (() => {
return builder.toBuffer();
})();
const tests = [
var tests = [
importingModuleBinary1,
importingModuleBinary2,
importingModuleBinary3,
......@@ -70,7 +70,9 @@ const tests = [
complexImportingModuleBinary2
];
for (const test of tests) {
assertPromiseFulfills(WebAssembly.compile(test))
.then(m => { assertTrue(m instanceof WebAssembly.Module) });
for (var index in tests) {
assertPromiseResult(
WebAssembly.compile(tests[index]),
m => assertTrue(m instanceof WebAssembly.Module),
assertUnreachable);
}
......@@ -11,4 +11,6 @@ __v_0 = new Uint8Array([
0x00, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x0a,
0x08, 0x01, 0x06, 0x00, 0x41, 0x01, 0x40, 0x00, 0x0b
]);
assertPromiseFulfills(WebAssembly.compile(__v_0));
assertPromiseResult(
WebAssembly.compile(__v_0)
);
......@@ -14,5 +14,7 @@ var bytes = builder.toBuffer();
var m = new WebAssembly.Module(bytes);
assertTrue(m instanceof WebAssembly.Module);
assertPromiseFulfills(WebAssembly.compile(bytes))
.then(async_result => assertTrue(async_result instanceof WebAssembly.Module));
assertPromiseResult(
WebAssembly.compile(bytes)
.then(async_result => assertTrue(async_result instanceof WebAssembly.Module),
assertUnreachable));
......@@ -8,38 +8,40 @@ load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function assertCompiles(buffer) {
return assertPromiseFulfills(WebAssembly.compile(buffer))
.then(module => assertTrue(module instanceof WebAssembly.Module));
return assertPromiseResult(
WebAssembly.compile(buffer),
module => assertTrue(module instanceof WebAssembly.Module),
ex => assertUnreachable);
}
function assertCompileError(buffer) {
return assertPromiseRejects(WebAssembly.compile(buffer))
.then(ex => assertTrue(ex instanceof WebAssembly.CompileError));
return assertPromiseResult(
WebAssembly.compile(buffer), module => assertUnreachable,
ex => assertTrue(ex instanceof WebAssembly.CompileError));
}
let ok_buffer = (() => {
var builder = new WasmModuleBuilder();
builder.addFunction('f', kSig_i_v)
.addBody([kExprI32Const, 42])
.exportAs('f');
return builder.toBuffer();
})();
assertPromiseResult(async function basicCompile() {
let ok_buffer = (() => {
var builder = new WasmModuleBuilder();
builder.addFunction('f', kSig_i_v)
.addBody([kExprI32Const, 42])
.exportAs('f');
return builder.toBuffer();
})();
// The OK buffer validates and can be made into a module.
assertTrue(WebAssembly.validate(ok_buffer));
let ok_module = new WebAssembly.Module(ok_buffer);
assertTrue(ok_module instanceof WebAssembly.Module);
// The OK buffer validates and can be made into a module.
assertTrue(WebAssembly.validate(ok_buffer));
let ok_module = new WebAssembly.Module(ok_buffer);
assertTrue(ok_module instanceof WebAssembly.Module);
// The bad buffer does not validate and cannot be made into a module.
let bad_buffer = new ArrayBuffer(0);
assertFalse(WebAssembly.validate(bad_buffer));
assertThrows(
() => new WebAssembly.Module(bad_buffer), WebAssembly.CompileError);
// The bad buffer does not validate and cannot be made into a module.
let bad_buffer = new ArrayBuffer(0);
assertFalse(WebAssembly.validate(bad_buffer));
assertThrows(
() => new WebAssembly.Module(bad_buffer), WebAssembly.CompileError);
const kNumCompiles = 3;
let kNumCompiles = 3;
// TODO(wasm): Remove assertPromiseFulfills once top-level await is a thing.
assertPromiseFulfills(async function basicCompile() {
// Three compilations of the OK module should succeed.
for (var i = 0; i < kNumCompiles; i++) {
await assertCompiles(ok_buffer);
......@@ -51,7 +53,7 @@ assertPromiseFulfills(async function basicCompile() {
}
}());
assertPromiseFulfills(async function badFunctionInTheMiddle() {
assertPromiseResult(async function badFunctionInTheMiddle() {
// We had an error where an exception was generated by a background task and
// later thrown in a foreground task. The handle to the exception died
// between, since the HandleScope was left.
......
......@@ -71,4 +71,4 @@ async function TestAll() {
await FailSyncInstantiate();
}
assertPromiseFulfills(TestAll());
assertPromiseResult(TestAll());
......@@ -44,8 +44,8 @@ async function AsyncTestOk() {
print('async module compile (ok)...');
%DisallowCodegenFromStrings(false);
let promise = WebAssembly.compile(buffer);
assertPromiseFulfills(promise)
.then(module => assertInstanceof(module, WebAssembly.Module));
assertPromiseResult(
promise, module => assertInstanceof(module, WebAssembly.Module));
}
async function AsyncTestFail() {
......@@ -70,9 +70,8 @@ async function StreamingTestOk() {
return;
}
let promise = WebAssembly.compileStreaming(buffer);
assertPromiseFulfills(promise)
.then(module => assertInstanceof(module, WebAssembly.Module)
);
assertPromiseResult(
promise, module => assertInstanceof(module, WebAssembly.Module));
}
async function StreamingTestFail() {
......@@ -102,4 +101,4 @@ async function RunAll() {
await StreamingTestFail();
}
assertPromiseFulfills(RunAll());
assertPromiseResult(RunAll());
......@@ -23,7 +23,7 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
var buffer = builder.toBuffer();
// Start the compilation but do not wait for the promise to resolve
// with assertPromise*. This should not cause a crash.
// with assertPromiseResult. This should not cause a crash.
WebAssembly.compile(buffer).then(
() => { print("success")},
() => { print("failed"); });
......
......@@ -65,13 +65,12 @@ function CheckInstance(instance) {
print('async module compile...');
let promise = WebAssembly.compile(buffer);
assertPromiseFulfills(promise)
.then(module => CheckInstance(new WebAssembly.Instance(module)));
assertPromiseResult(
promise, module => CheckInstance(new WebAssembly.Instance(module)));
print('async instantiate...');
let instance_promise = WebAssembly.instantiate(buffer);
assertPromiseFulfills(instance_promise)
.then(pair => CheckInstance(pair.instance));
assertPromiseResult(instance_promise, pair => CheckInstance(pair.instance));
})();
// Check that validate works correctly for a module.
......@@ -99,8 +98,10 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
let builder = new WasmModuleBuilder();
builder.addFunction('f', kSig_i_i).addBody([kExprCallFunction, 0]);
let promise = WebAssembly.compile(builder.toBuffer());
assertPromiseRejects(promise)
.then(e => assertInstanceof(e, WebAssembly.CompileError));
assertPromiseResult(
promise, compiled => assertUnreachable(
'should not be able to compile invalid blob.'),
e => assertInstanceof(e, WebAssembly.CompileError));
})();
// Multiple instances tests.
......@@ -115,12 +116,11 @@ assertFalse(WebAssembly.validate(bytes(88, 88, 88, 88, 88, 88, 88, 88)));
(function ManyInstancesAsync() {
print('ManyInstancesAsync...');
let promise = WebAssembly.compile(buffer);
assertPromiseFulfills(promise)
.then(compiled_module => {
let instance_1 = new WebAssembly.Instance(compiled_module);
let instance_2 = new WebAssembly.Instance(compiled_module);
assertTrue(instance_1 != instance_2);
});
assertPromiseResult(promise, compiled_module => {
let instance_1 = new WebAssembly.Instance(compiled_module);
let instance_2 = new WebAssembly.Instance(compiled_module);
assertTrue(instance_1 != instance_2);
});
})();
(function InstancesAreIsolatedFromEachother() {
......
......@@ -27,7 +27,8 @@ function getBuilder() {
(function AsyncTest() {
var builder = getBuilder();
var buffer = builder.toBuffer();
assertPromiseFulfills(WebAssembly.instantiate(buffer))
.then(pair => pair.instance.exports.main())
.then(result => assertEquals(kReturnValue, result));
assertPromiseResult(
WebAssembly.instantiate(buffer)
.then(pair => pair.instance.exports.main(), assertUnreachable)
.then(result => assertEquals(kReturnValue, result), assertUnreachable));
})();
......@@ -2,11 +2,19 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --expose-wasm
// Flags: --expose-wasm --allow-natives-syntax
load('test/mjsunit/wasm/wasm-constants.js');
load('test/mjsunit/wasm/wasm-module-builder.js');
function unexpectedSuccess() {
%AbortJS('unexpected success');
}
function unexpectedFail(error) {
%AbortJS('unexpected fail: ' + error);
}
function assertEq(val, expected) {
assertSame(expected, val);
}
......@@ -765,11 +773,10 @@ assertEq(compile.length, 1);
assertEq(compile.name, 'compile');
function assertCompileError(args, err, msg) {
var error = null;
assertPromiseRejects(compile(...args))
.then(error => {
assertTrue(error instanceof err);
// TODO assertTrue(Boolean(error.message.match(msg)));
});
assertPromiseResult(compile(...args), unexpectedSuccess, error => {
assertTrue(error instanceof err);
// TODO assertTrue(Boolean(error.message.match(msg)));
});
}
assertCompileError([], TypeError, /requires more than 0 arguments/);
assertCompileError(
......@@ -792,8 +799,7 @@ assertCompileError(
function assertCompileSuccess(bytes) {
var module = null;
assertPromiseFulfills(compile(bytes))
.then(m => assertTrue(m instanceof Module));
assertPromiseResult(compile(bytes), m => assertTrue(m instanceof Module));
}
assertCompileSuccess(emptyModuleBinary);
assertCompileSuccess(emptyModuleBinary.buffer);
......@@ -813,11 +819,10 @@ assertEq(instantiate.length, 1);
assertEq(instantiate.name, 'instantiate');
function assertInstantiateError(args, err, msg) {
var error = null;
assertPromiseRejects(instantiate(...args))
.then(error => {
assertTrue(error instanceof err);
// TODO assertTrue(Boolean(error.message.match(msg)));
});
assertPromiseResult(instantiate(...args), unexpectedSuccess, error => {
assertTrue(error instanceof err);
// TODO assertTrue(Boolean(error.message.match(msg)));
});
}
var scratch_memory = new WebAssembly.Memory(new ArrayBuffer(10));
assertInstantiateError([], TypeError, /requires more than 0 arguments/);
......@@ -871,15 +876,14 @@ assertInstantiateError(
function assertInstantiateSuccess(module_or_bytes, imports) {
var result = null;
assertPromiseFulfills(instantiate(module_or_bytes, imports))
.then(result => {
if (module_or_bytes instanceof Module) {
assertTrue(result instanceof Instance);
} else {
assertTrue(result.module instanceof Module);
assertTrue(result.instance instanceof Instance);
}
});
assertPromiseResult(instantiate(module_or_bytes, imports), result => {
if (module_or_bytes instanceof Module) {
assertTrue(result instanceof Instance);
} else {
assertTrue(result.module instanceof Module);
assertTrue(result.instance instanceof Instance);
}
});
}
assertInstantiateSuccess(emptyModule);
assertInstantiateSuccess(emptyModuleBinary);
......
......@@ -103,38 +103,37 @@ load("test/wasm-js/test/harness/wasm-constants.js");
load("test/wasm-js/test/harness/wasm-module-builder.js");
load("test/wasm-js/test/js-api/jsapi.js");
assertPromiseFulfills(last_promise)
.then(_ => {
if (failures.length > 0) {
let unexpected = false;
print("Some tests FAILED:");
for (let i in failures) {
if (known_failures[failures[i]]) {
print(` ${failures[i]} [KNOWN: ${known_failures[failures[i]]}]`);
} else {
print(` ${failures[i]}`);
unexpected = true;
}
}
if (unexpected_successes.length > 0) {
assertPromiseResult(last_promise, _ => {
if (failures.length > 0) {
let unexpected = false;
print("Some tests FAILED:");
for (let i in failures) {
if (known_failures[failures[i]]) {
print(` ${failures[i]} [KNOWN: ${known_failures[failures[i]]}]`);
} else {
print(` ${failures[i]}`);
unexpected = true;
print("");
print("Unexpected successes:");
for(let i in unexpected_successes) {
print(` ${unexpected_successes[i]}`);
}
print("Some tests SUCCEEDED but were known failures. If you've fixed " +
"the bug, please remove the test from the known failures list.")
}
if (unexpected) {
print("\n");
print(" #############################################################");
print(" # #");
print(" # Unexpected outcome. Did you forget to run 'gclient sync'? #");
print(" # #");
print(" #############################################################");
print("\n");
assertUnreachable("Unexpected outcome");
}
if (unexpected_successes.length > 0) {
unexpected = true;
print("");
print("Unexpected successes:");
for(let i in unexpected_successes) {
print(` ${unexpected_successes[i]}`);
}
print("Some tests SUCCEEDED but were known failures. If you've fixed " +
"the bug, please remove the test from the known failures list.")
}
});
if (unexpected) {
print("\n");
print(" #############################################################");
print(" # #");
print(" # Unexpected outcome. Did you forget to run 'gclient sync'? #");
print(" # #");
print(" #############################################################");
print("\n");
assertUnreachable("Unexpected outcome");
}
}
});
......@@ -33,14 +33,14 @@ function toBuffer(binary) {
}
function testErrorPosition(bytes, pos, test_name) {
assertPromiseRejects(WebAssembly.compile(toBuffer(bytes)))
.then(e => {
print(test_name);
assertInstanceof(e, WebAssembly.CompileError);
let regex = new RegExp('@\\+' + pos);
print(e.message);
assertMatches(regex, e.message, 'Error Position');
});
assertPromiseResult(
WebAssembly.compile(toBuffer(bytes)), assertUnreachable, e => {
print(test_name);
assertInstanceof(e, WebAssembly.CompileError);
let regex = new RegExp('@\\+' + pos);
print(e.message);
assertMatches(regex, e.message, 'Error Position');
});
}
(function testInvalidMagic() {
......
......@@ -85,10 +85,9 @@ builder.appendToTable([0]);
let buffer = builder.toBuffer();
// Test async compilation and instantiation.
assertPromiseFulfills(WebAssembly.instantiate(buffer))
.then(pair => {
testTrapLocations(pair.instance, 6);
});
assertPromiseResult(WebAssembly.instantiate(buffer), pair => {
testTrapLocations(pair.instance, 6);
});
// Test sync compilation and instantiation.
testTrapLocations(builder.instantiate(), 4);
......@@ -5,21 +5,29 @@
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
const buffer = (() => {
const builder = new WasmModuleBuilder();
let buffer = (() => {
let builder = new WasmModuleBuilder();
builder.addFunction("f", kSig_i_v)
.addBody([kExprI32Const, 42])
.exportAs("f");
return builder.toBuffer();
})();
const module = new WebAssembly.Module(buffer);
const wrapper = [module];
var module = new WebAssembly.Module(buffer);
var wrapper = [module];
assertThrows(() => {
WebAssembly.instantiateStreaming(wrapper);
}, TypeError);
try {
assertPromiseResult(
WebAssembly.instantiateStreaming(wrapper),
assertUnreachable, assertUnreachable);
} catch (e) {
assertTrue(e instanceof TypeError);
}
assertThrows(() => {
WebAssembly.compileStreaming(wrapper);
}, TypeError);
try {
assertPromiseResult(
WebAssembly.compileStreaming(wrapper),
assertUnreachable, assertUnreachable);
} catch (e) {
assertTrue(e instanceof TypeError);
}
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