2016-02-15 08:58:44 +00:00
|
|
|
// Copyright 2016 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
// Flags: --expose-wasm
|
|
|
|
|
2021-06-01 12:46:36 +00:00
|
|
|
d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
|
2016-02-15 08:58:44 +00:00
|
|
|
|
|
|
|
function instantiate(sig, body) {
|
2016-03-07 19:32:35 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
var func = builder.addFunction("", sig)
|
|
|
|
.addBody(body);
|
|
|
|
|
|
|
|
builder.addStart(func.index);
|
|
|
|
|
|
|
|
return builder.instantiate();
|
2016-02-15 08:58:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function assertVerifies(sig, body) {
|
|
|
|
var module = instantiate(sig, body);
|
|
|
|
assertFalse(module === undefined);
|
|
|
|
assertFalse(module === null);
|
|
|
|
assertFalse(module === 0);
|
|
|
|
assertEquals("object", typeof module);
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
assertVerifies(kSig_v_v, [kExprNop]);
|
2016-02-15 08:58:44 +00:00
|
|
|
|
[wasm] Use pending exceptions consistently
In our internal code, we should only use pending exceptions. They will
be converted to scheduled exceptions on the API boundary.
Hence, the ErrorThrower just sets a pending exception; it should never
have to think about scheduled exceptions. The new
ScheduledErrorThrower inherits from ErrorThrower and reschedules any
pending exceptions in its destructor (turning them into scheduled
exceptions).
In some situations, there might already be a scheduled exception, e.g.
when calling other API methods (v8::Value::Get). In this case, the
ErrorThrower should also not set another pending exception. For the
reasons mentioned above, this can only be handled in the
ScheduledErrorThrower, which is used the API methods.
This fixes one DCHECK failure and one TODO about scheduled exceptions
if no instance can be created, because the start function throws.
R=mtrofin@chromium.org, mstarzinger@chromium.org
BUG=v8:6232,chromium:736256
Change-Id: I4905be04c565df9495de18fb26adbb5c05d193d2
Reviewed-on: https://chromium-review.googlesource.com/548641
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: Mircea Trofin <mtrofin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46314}
2017-06-29 08:16:29 +00:00
|
|
|
// Arguments aren't allowed to start functions.
|
2019-10-08 12:38:48 +00:00
|
|
|
assertThrows(() => {instantiate(kSig_i_i, [kExprLocalGet, 0]);});
|
|
|
|
assertThrows(() => {instantiate(kSig_i_ii, [kExprLocalGet, 0]);});
|
|
|
|
assertThrows(() => {instantiate(kSig_i_dd, [kExprLocalGet, 0]);});
|
2017-01-09 13:57:26 +00:00
|
|
|
assertThrows(() => {instantiate(kSig_i_v, [kExprI32Const, 0]);});
|
2016-02-15 08:58:44 +00:00
|
|
|
|
|
|
|
(function testInvalidIndex() {
|
2016-03-07 19:32:35 +00:00
|
|
|
print("testInvalidIndex");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([kExprNop]);
|
|
|
|
|
|
|
|
builder.addStart(func.index + 1);
|
|
|
|
|
2017-04-10 12:13:36 +00:00
|
|
|
assertThrows(
|
|
|
|
() => builder.instantiate(), WebAssembly.CompileError,
|
2019-02-01 13:51:23 +00:00
|
|
|
'WebAssembly.Module(): ' +
|
2022-07-18 18:36:30 +00:00
|
|
|
'function index 1 out of bounds (1 entry) @+20');
|
2016-02-15 08:58:44 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
(function testTwoStartFuncs() {
|
2016-03-07 19:32:35 +00:00
|
|
|
print("testTwoStartFuncs");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([kExprNop]);
|
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
builder.addExplicitSection([kStartSectionCode, 0]);
|
|
|
|
builder.addExplicitSection([kStartSectionCode, 0]);
|
2016-03-07 19:32:35 +00:00
|
|
|
|
2017-04-10 12:13:36 +00:00
|
|
|
assertThrows(
|
|
|
|
() => builder.instantiate(), WebAssembly.CompileError,
|
2019-02-01 13:51:23 +00:00
|
|
|
'WebAssembly.Module(): unexpected section <Start> @+27');
|
2016-02-15 08:58:44 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
2016-11-08 12:36:21 +00:00
|
|
|
(function testRun1() {
|
|
|
|
print("testRun1");
|
2016-03-07 19:32:35 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
builder.addMemory(12, 12, true);
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
2017-01-09 13:57:26 +00:00
|
|
|
.addBody([kExprI32Const, 0, kExprI32Const, 55, kExprI32StoreMem, 0, 0]);
|
2016-11-08 12:36:21 +00:00
|
|
|
|
|
|
|
builder.addStart(func.index);
|
|
|
|
|
|
|
|
var module = builder.instantiate();
|
|
|
|
var memory = module.exports.memory.buffer;
|
|
|
|
var view = new Int8Array(memory);
|
2017-01-09 13:57:26 +00:00
|
|
|
assertEquals(55, view[0]);
|
2016-11-08 12:36:21 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function testRun2() {
|
|
|
|
print("testRun2");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
builder.addMemory(12, 12, true);
|
|
|
|
|
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
2017-01-09 13:57:26 +00:00
|
|
|
.addBody([kExprI32Const, 0, kExprI32Const, 22, kExprI32Const, 55, kExprI32Add, kExprI32StoreMem, 0, 0]);
|
2016-03-07 19:32:35 +00:00
|
|
|
|
|
|
|
builder.addStart(func.index);
|
|
|
|
|
|
|
|
var module = builder.instantiate();
|
2016-09-27 20:46:10 +00:00
|
|
|
var memory = module.exports.memory.buffer;
|
2016-02-15 08:58:44 +00:00
|
|
|
var view = new Int8Array(memory);
|
|
|
|
assertEquals(77, view[0]);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testStartFFI() {
|
2016-03-07 19:32:35 +00:00
|
|
|
print("testStartFFI");
|
2016-02-15 08:58:44 +00:00
|
|
|
var ranned = false;
|
2016-12-20 15:32:56 +00:00
|
|
|
var ffi = {gak: {foo : function() {
|
2016-02-15 08:58:44 +00:00
|
|
|
print("we ranned at stert!");
|
|
|
|
ranned = true;
|
2016-12-20 15:32:56 +00:00
|
|
|
}}};
|
2016-03-07 19:32:35 +00:00
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
2016-06-21 19:47:51 +00:00
|
|
|
var sig_index = builder.addType(kSig_v_v);
|
2016-03-07 19:32:35 +00:00
|
|
|
|
2016-12-20 15:32:56 +00:00
|
|
|
builder.addImport("gak", "foo", sig_index);
|
2016-03-07 19:32:35 +00:00
|
|
|
var func = builder.addFunction("", sig_index)
|
2016-09-27 20:46:10 +00:00
|
|
|
.addBody([kExprCallFunction, 0]);
|
2016-03-07 19:32:35 +00:00
|
|
|
|
|
|
|
builder.addStart(func.index);
|
|
|
|
|
|
|
|
var module = builder.instantiate(ffi);
|
2016-02-15 08:58:44 +00:00
|
|
|
assertTrue(ranned);
|
|
|
|
})();
|
[wasm] Use pending exceptions consistently
In our internal code, we should only use pending exceptions. They will
be converted to scheduled exceptions on the API boundary.
Hence, the ErrorThrower just sets a pending exception; it should never
have to think about scheduled exceptions. The new
ScheduledErrorThrower inherits from ErrorThrower and reschedules any
pending exceptions in its destructor (turning them into scheduled
exceptions).
In some situations, there might already be a scheduled exception, e.g.
when calling other API methods (v8::Value::Get). In this case, the
ErrorThrower should also not set another pending exception. For the
reasons mentioned above, this can only be handled in the
ScheduledErrorThrower, which is used the API methods.
This fixes one DCHECK failure and one TODO about scheduled exceptions
if no instance can be created, because the start function throws.
R=mtrofin@chromium.org, mstarzinger@chromium.org
BUG=v8:6232,chromium:736256
Change-Id: I4905be04c565df9495de18fb26adbb5c05d193d2
Reviewed-on: https://chromium-review.googlesource.com/548641
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: Mircea Trofin <mtrofin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46314}
2017-06-29 08:16:29 +00:00
|
|
|
|
|
|
|
(function testStartFunctionThrowsExplicitly() {
|
|
|
|
print('testStartFunctionThrowsExplicitly');
|
|
|
|
let error = new Error('my explicit error');
|
2018-06-06 05:51:02 +00:00
|
|
|
var ffi = {
|
|
|
|
foo: {
|
|
|
|
throw_fn: function() {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
[wasm] Use pending exceptions consistently
In our internal code, we should only use pending exceptions. They will
be converted to scheduled exceptions on the API boundary.
Hence, the ErrorThrower just sets a pending exception; it should never
have to think about scheduled exceptions. The new
ScheduledErrorThrower inherits from ErrorThrower and reschedules any
pending exceptions in its destructor (turning them into scheduled
exceptions).
In some situations, there might already be a scheduled exception, e.g.
when calling other API methods (v8::Value::Get). In this case, the
ErrorThrower should also not set another pending exception. For the
reasons mentioned above, this can only be handled in the
ScheduledErrorThrower, which is used the API methods.
This fixes one DCHECK failure and one TODO about scheduled exceptions
if no instance can be created, because the start function throws.
R=mtrofin@chromium.org, mstarzinger@chromium.org
BUG=v8:6232,chromium:736256
Change-Id: I4905be04c565df9495de18fb26adbb5c05d193d2
Reviewed-on: https://chromium-review.googlesource.com/548641
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: Mircea Trofin <mtrofin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46314}
2017-06-29 08:16:29 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2018-06-06 05:51:02 +00:00
|
|
|
builder.addImport('foo', 'throw_fn', kSig_v_v);
|
[wasm] Use pending exceptions consistently
In our internal code, we should only use pending exceptions. They will
be converted to scheduled exceptions on the API boundary.
Hence, the ErrorThrower just sets a pending exception; it should never
have to think about scheduled exceptions. The new
ScheduledErrorThrower inherits from ErrorThrower and reschedules any
pending exceptions in its destructor (turning them into scheduled
exceptions).
In some situations, there might already be a scheduled exception, e.g.
when calling other API methods (v8::Value::Get). In this case, the
ErrorThrower should also not set another pending exception. For the
reasons mentioned above, this can only be handled in the
ScheduledErrorThrower, which is used the API methods.
This fixes one DCHECK failure and one TODO about scheduled exceptions
if no instance can be created, because the start function throws.
R=mtrofin@chromium.org, mstarzinger@chromium.org
BUG=v8:6232,chromium:736256
Change-Id: I4905be04c565df9495de18fb26adbb5c05d193d2
Reviewed-on: https://chromium-review.googlesource.com/548641
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: Mircea Trofin <mtrofin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46314}
2017-06-29 08:16:29 +00:00
|
|
|
let func = builder.addFunction('', kSig_v_v).addBody([kExprCallFunction, 0]);
|
|
|
|
builder.addStart(func.index);
|
|
|
|
|
|
|
|
assertThrowsEquals(() => builder.instantiate(ffi), error);
|
2018-06-08 11:51:33 +00:00
|
|
|
assertPromiseResult(builder.asyncInstantiate(ffi), assertUnreachable,
|
|
|
|
e => assertSame(e, error));
|
|
|
|
assertPromiseResult(WebAssembly.instantiate(builder.toModule(), ffi),
|
|
|
|
assertUnreachable, e => assertSame(e, error));
|
[wasm] Use pending exceptions consistently
In our internal code, we should only use pending exceptions. They will
be converted to scheduled exceptions on the API boundary.
Hence, the ErrorThrower just sets a pending exception; it should never
have to think about scheduled exceptions. The new
ScheduledErrorThrower inherits from ErrorThrower and reschedules any
pending exceptions in its destructor (turning them into scheduled
exceptions).
In some situations, there might already be a scheduled exception, e.g.
when calling other API methods (v8::Value::Get). In this case, the
ErrorThrower should also not set another pending exception. For the
reasons mentioned above, this can only be handled in the
ScheduledErrorThrower, which is used the API methods.
This fixes one DCHECK failure and one TODO about scheduled exceptions
if no instance can be created, because the start function throws.
R=mtrofin@chromium.org, mstarzinger@chromium.org
BUG=v8:6232,chromium:736256
Change-Id: I4905be04c565df9495de18fb26adbb5c05d193d2
Reviewed-on: https://chromium-review.googlesource.com/548641
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: Mircea Trofin <mtrofin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46314}
2017-06-29 08:16:29 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function testStartFunctionThrowsImplicitly() {
|
|
|
|
print("testStartFunctionThrowsImplicitly");
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
let func = builder.addFunction('', kSig_v_v).addBody([kExprUnreachable]);
|
|
|
|
builder.addStart(func.index);
|
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => builder.instantiate(), WebAssembly.RuntimeError, /unreachable/);
|
2019-02-06 13:09:43 +00:00
|
|
|
assertThrowsAsync(builder.asyncInstantiate(), WebAssembly.RuntimeError);
|
|
|
|
assertThrowsAsync(
|
|
|
|
WebAssembly.instantiate(builder.toModule()), WebAssembly.RuntimeError);
|
[wasm] Use pending exceptions consistently
In our internal code, we should only use pending exceptions. They will
be converted to scheduled exceptions on the API boundary.
Hence, the ErrorThrower just sets a pending exception; it should never
have to think about scheduled exceptions. The new
ScheduledErrorThrower inherits from ErrorThrower and reschedules any
pending exceptions in its destructor (turning them into scheduled
exceptions).
In some situations, there might already be a scheduled exception, e.g.
when calling other API methods (v8::Value::Get). In this case, the
ErrorThrower should also not set another pending exception. For the
reasons mentioned above, this can only be handled in the
ScheduledErrorThrower, which is used the API methods.
This fixes one DCHECK failure and one TODO about scheduled exceptions
if no instance can be created, because the start function throws.
R=mtrofin@chromium.org, mstarzinger@chromium.org
BUG=v8:6232,chromium:736256
Change-Id: I4905be04c565df9495de18fb26adbb5c05d193d2
Reviewed-on: https://chromium-review.googlesource.com/548641
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: Mircea Trofin <mtrofin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#46314}
2017-06-29 08:16:29 +00:00
|
|
|
})();
|