1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
// META: global=window,dedicatedworker,jsshell
// META: script=/wasm/jsapi/wasm-module-builder.js
// Test for invalid wrappers
test(() => {
assert_throws_js(TypeError, () => WebAssembly.promising({}),
"Argument 0 must be a function");
assert_throws_js(TypeError, () => WebAssembly.promising(() => {}),
"Argument 0 must be a WebAssembly exported function");
assert_throws_js(TypeError, () => WebAssembly.Suspending(() => {}),
"WebAssembly.Suspending must be invoked with 'new'");
assert_throws_js(TypeError, () => new WebAssembly.Suspending({}),
"Argument 0 must be a function");
function asmModule() {
"use asm";
function x(v) {
v = v | 0;
}
return x;
}
assert_throws_js(TypeError, () => WebAssembly.promising(asmModule()),
"Argument 0 must be a WebAssembly exported function");
},"Valid use of API");
test(() => {
let builder = new WasmModuleBuilder();
builder.addGlobal(kWasmI32, true).exportAs('g');
builder.addFunction("test", kSig_i_v)
.addBody([
kExprI32Const, 42,
kExprGlobalSet, 0,
kExprI32Const, 0
]).exportFunc();
let instance = builder.instantiate();
let wrapper = WebAssembly.promising(instance.exports.test);
wrapper();
assert_equals(42, instance.exports.g.value);
},"Promising function always entered");
promise_test(async () => {
let builder = new WasmModuleBuilder();
let import_index = builder.addImport('m', 'import', kSig_i_v);
builder.addFunction("test", kSig_i_i)
.addBody([
kExprCallFunction, import_index, // suspend
]).exportFunc();
let js_import = () => 42;
let instance = builder.instantiate({
m: {
import: js_import
}
});
let wrapped_export = WebAssembly.promising(instance.exports.test);
let export_promise = wrapped_export();
assert_true(export_promise instanceof Promise);
assert_equals(await export_promise, 42);
}, "Always get a Promise");
promise_test(async () => {
let builder = new WasmModuleBuilder();
let import_index = builder.addImport('m', 'import', kSig_i_i);
builder.addFunction("test", kSig_i_i)
.addBody([
kExprLocalGet, 0,
kExprCallFunction, import_index, // suspend
]).exportFunc();
let js_import = new WebAssembly.Suspending(() => Promise.resolve(42));
let instance = builder.instantiate({
m: {
import: js_import
}
});
let wrapped_export = WebAssembly.promising(instance.exports.test);
let export_promise = wrapped_export();
assert_true(export_promise instanceof Promise);
assert_equals(await export_promise, 42);
}, "Suspend once");
promise_test(async () => {
let builder = new WasmModuleBuilder();
builder.addGlobal(kWasmI32, true).exportAs('g');
let import_index = builder.addImport('m', 'import', kSig_i_v);
// void test() {
// for (i = 0; i < 5; ++i) {
// g = g + await import();
// }
// }
builder.addFunction("test", kSig_v_i)
.addLocals({
i32_count: 1
})
.addBody([
kExprI32Const, 5,
kExprLocalSet, 1,
kExprLoop, kWasmStmt,
kExprCallFunction, import_index, // suspend
kExprGlobalGet, 0,
kExprI32Add,
kExprGlobalSet, 0,
kExprLocalGet, 1,
kExprI32Const, 1,
kExprI32Sub,
kExprLocalTee, 1,
kExprBrIf, 0,
kExprEnd,
]).exportFunc();
let i = 0;
function js_import() {
return Promise.resolve(++i);
};
let wasm_js_import = new WebAssembly.Suspending(js_import);
let instance = builder.instantiate({
m: {
import: wasm_js_import
}
});
let wrapped_export = WebAssembly.promising(instance.exports.test);
let export_promise = wrapped_export();
assert_equals(instance.exports.g.value, 0);
assert_true(export_promise instanceof Promise);
await export_promise;
assert_equals(instance.exports.g.value, 15);
}, "Suspend/resume in a loop");
promise_test(async () => {
let builder = new WasmModuleBuilder();
let import_index = builder.addImport('m', 'import', kSig_i_v);
builder.addFunction("test", kSig_i_v)
.addBody([
kExprCallFunction, import_index, // suspend
]).exportFunc();
let js_import = new WebAssembly.Suspending(() => Promise.resolve(42));
let instance = builder.instantiate({
m: {
import: js_import
}
});
let wrapped_export = WebAssembly.promising(instance.exports.test);
assert_equals(await wrapped_export(), 42);
// Also try with a JS function with a mismatching arity.
js_import = new WebAssembly.Suspending((unused) => Promise.resolve(42));
instance = builder.instantiate({
m: {
import: js_import
}
});
wrapped_export = WebAssembly.promising(instance.exports.test);
assert_equals(await wrapped_export(), 42);
// Also try with a proxy.
js_import = new WebAssembly.Suspending(new Proxy(() => Promise.resolve(42), {}));
instance = builder.instantiate({
m: {
import: js_import
}
});
wrapped_export = WebAssembly.promising(instance.exports.test);
assert_equals(await wrapped_export(), 42);
},"Suspending with mismatched args and via Proxy");
function recordAbeforeB() {
let AbeforeB = [];
let setA = () => {
AbeforeB.push("A")
}
let setB = () => {
AbeforeB.push("B")
}
let isAbeforeB = () =>
AbeforeB[0] == "A" && AbeforeB[1] == "B";
let isBbeforeA = () =>
AbeforeB[0] == "B" && AbeforeB[1] == "A";
return {
setA: setA,
setB: setB,
isAbeforeB: isAbeforeB,
isBbeforeA: isBbeforeA,
}
}
promise_test(async () => {
let builder = new WasmModuleBuilder();
let AbeforeB = recordAbeforeB();
let import42_index = builder.addImport('m', 'import42', kSig_i_i);
let importSetA_index = builder.addImport('m', 'setA', kSig_v_v);
builder.addFunction("test", kSig_i_i)
.addBody([
kExprLocalGet, 0,
kExprCallFunction, import42_index, // suspend?
kExprCallFunction, importSetA_index
]).exportFunc();
let import42 = new WebAssembly.Suspending(() => Promise.resolve(42));
let instance = builder.instantiate({
m: {
import42: import42,
setA: AbeforeB.setA
}
});
let wrapped_export = WebAssembly.promising(instance.exports.test);
let exported_promise = wrapped_export();
AbeforeB.setB();
assert_equals(await exported_promise, 42);
assert_true(AbeforeB.isBbeforeA());
}, "Make sure we actually suspend");
promise_test(async () => {
let builder = new WasmModuleBuilder();
let AbeforeB = recordAbeforeB();
let import42_index = builder.addImport('m', 'import42', kSig_i_i);
let importSetA_index = builder.addImport('m', 'setA', kSig_v_v);
builder.addFunction("test", kSig_i_i)
.addBody([
kExprLocalGet, 0,
kExprCallFunction, import42_index, // suspend?
kExprCallFunction, importSetA_index
]).exportFunc();
let import42 = new WebAssembly.Suspending(() => 42);
let instance = builder.instantiate({
m: {
import42: import42,
setA: AbeforeB.setA
}
});
let wrapped_export = WebAssembly.promising(instance.exports.test);
let exported_promise = wrapped_export();
AbeforeB.setB();
assert_equals(await exported_promise, 42);
assert_true(AbeforeB.isBbeforeA());
}, "Do suspend even if the import's return value is not a Promise by wrapping it with Promise.resolve");
promise_test(async (t) => {
let tag = new WebAssembly.Tag({
parameters: ['i32']
});
let builder = new WasmModuleBuilder();
let import_index = builder.addImport('m', 'import', kSig_i_i);
let tag_index = builder.addImportedTag('m', 'tag', kSig_v_i);
builder.addFunction("test", kSig_i_i)
.addBody([
kExprTry, kWasmI32,
kExprLocalGet, 0,
kExprCallFunction, import_index,
kExprCatch, tag_index,
kExprEnd
]).exportFunc();
function js_import(unused) {
return Promise.reject(new WebAssembly.Exception(tag, [42]));
};
let wasm_js_import = new WebAssembly.Suspending(js_import);
let instance = builder.instantiate({
m: {
import: wasm_js_import,
tag: tag
}
});
let wrapped_export = WebAssembly.promising(instance.exports.test);
let export_promise = wrapped_export();
assert_true(export_promise instanceof Promise);
assert_equals(await export_promise, 42);
}, "Catch rejected promise");
async function TestSandwich(suspend) {
// Set up a 'sandwich' scenario. The call chain looks like:
// top (wasm) -> outer (js) -> bottom (wasm) -> inner (js)
// If 'suspend' is true, the inner JS function returns a Promise, which
// suspends the bottom wasm function, which returns a Promise, which suspends
// the top wasm function, which returns a Promise. The inner Promise
// resolves first, which resumes the bottom continuation. Then the outer
// promise resolves which resumes the top continuation.
// If 'suspend' is false, the bottom JS function returns a regular value and
// no computation is suspended.
let builder = new WasmModuleBuilder();
let inner_index = builder.addImport('m', 'inner', kSig_i_i);
let outer_index = builder.addImport('m', 'outer', kSig_i_i);
builder.addFunction("top", kSig_i_i)
.addBody([
kExprLocalGet, 0,
kExprCallFunction, outer_index
]).exportFunc();
builder.addFunction("bottom", kSig_i_i)
.addBody([
kExprLocalGet, 0,
kExprCallFunction, inner_index
]).exportFunc();
let inner = new WebAssembly.Suspending(() => suspend ? Promise.resolve(42) : 43);
let export_inner;
let outer = new WebAssembly.Suspending(() => export_inner());
let instance = builder.instantiate({
m: {
inner,
outer
}
});
export_inner = WebAssembly.promising(instance.exports.bottom);
let export_top = WebAssembly.promising(instance.exports.top);
let result = export_top();
assert_true(result instanceof Promise);
if (suspend)
assert_equals(await result, 42);
else
assert_equals(await result, 43);
}
promise_test(async () => {
TestSandwich(true);
}, "Test sandwich with suspension");
promise_test(async () => {
TestSandwich(false);
}, "Test sandwich with no suspension");
test(() => {
// Check that a promising function with no return is allowed.
let builder = new WasmModuleBuilder();
builder.addFunction("export", kSig_v_v).addBody([]).exportFunc();
let instance = builder.instantiate();
let export_wrapper = WebAssembly.promising(instance.exports.export);
assert_true(export_wrapper instanceof Function);
},"Promising with no return");
promise_test(async () => {
let builder1 = new WasmModuleBuilder();
let import_index = builder1.addImport('m', 'import', kSig_i_v);
builder1.addFunction("f", kSig_i_v)
.addBody([
kExprCallFunction, import_index, // suspend
kExprI32Const, 1,
kExprI32Add,
]).exportFunc();
let js_import = new WebAssembly.Suspending(() => Promise.resolve(1));
let instance1 = builder1.instantiate({
m: {
import: js_import
}
});
let builder2 = new WasmModuleBuilder();
import_index = builder2.addImport('m', 'import', kSig_i_v);
builder2.addFunction("main", kSig_i_v)
.addBody([
kExprCallFunction, import_index,
kExprI32Const, 1,
kExprI32Add,
]).exportFunc();
let instance2 = builder2.instantiate({
m: {
import: instance1.exports.f
}
});
let wrapped_export = WebAssembly.promising(instance2.exports.main);
assert_equals(await wrapped_export(), 3);
},"Suspend two modules");
|