2016-03-07 10:01:24 +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.
|
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
// Used for encoding f32 and double constants to bits.
|
2020-01-10 14:18:07 +00:00
|
|
|
let byte_view = new Uint8Array(8);
|
|
|
|
let data_view = new DataView(byte_view.buffer);
|
2016-10-06 15:43:10 +00:00
|
|
|
|
2019-02-07 13:06:39 +00:00
|
|
|
// The bytes function receives one of
|
|
|
|
// - several arguments, each of which is either a number or a string of length
|
|
|
|
// 1; if it's a string, the charcode of the contained character is used.
|
|
|
|
// - a single array argument containing the actual arguments
|
|
|
|
// - a single string; the returned buffer will contain the char codes of all
|
|
|
|
// contained characters.
|
|
|
|
function bytes(...input) {
|
|
|
|
if (input.length == 1 && typeof input[0] == 'array') input = input[0];
|
|
|
|
if (input.length == 1 && typeof input[0] == 'string') {
|
|
|
|
let len = input[0].length;
|
|
|
|
let view = new Uint8Array(len);
|
|
|
|
for (let i = 0; i < len; i++) view[i] = input[0].charCodeAt(i);
|
|
|
|
return view.buffer;
|
|
|
|
}
|
|
|
|
let view = new Uint8Array(input.length);
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
|
|
let val = input[i];
|
|
|
|
if (typeof val == 'string') {
|
|
|
|
assertEquals(1, val.length, 'string inputs must have length 1');
|
|
|
|
val = val.charCodeAt(0);
|
|
|
|
}
|
2019-01-30 15:39:14 +00:00
|
|
|
view[i] = val | 0;
|
|
|
|
}
|
2019-02-07 13:06:39 +00:00
|
|
|
return view.buffer;
|
2019-01-30 15:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header declaration constants
|
|
|
|
var kWasmH0 = 0;
|
|
|
|
var kWasmH1 = 0x61;
|
|
|
|
var kWasmH2 = 0x73;
|
|
|
|
var kWasmH3 = 0x6d;
|
|
|
|
|
|
|
|
var kWasmV0 = 0x1;
|
|
|
|
var kWasmV1 = 0;
|
|
|
|
var kWasmV2 = 0;
|
|
|
|
var kWasmV3 = 0;
|
|
|
|
|
|
|
|
var kHeaderSize = 8;
|
|
|
|
var kPageSize = 65536;
|
2020-02-28 10:57:06 +00:00
|
|
|
var kSpecMaxPages = 65536;
|
2019-03-08 09:48:22 +00:00
|
|
|
var kMaxVarInt32Size = 5;
|
|
|
|
var kMaxVarInt64Size = 10;
|
2019-01-30 15:39:14 +00:00
|
|
|
|
|
|
|
let kDeclNoLocals = 0;
|
|
|
|
|
|
|
|
// Section declaration constants
|
|
|
|
let kUnknownSectionCode = 0;
|
|
|
|
let kTypeSectionCode = 1; // Function signature declarations
|
|
|
|
let kImportSectionCode = 2; // Import declarations
|
|
|
|
let kFunctionSectionCode = 3; // Function declarations
|
|
|
|
let kTableSectionCode = 4; // Indirect function table and other tables
|
|
|
|
let kMemorySectionCode = 5; // Memory attributes
|
|
|
|
let kGlobalSectionCode = 6; // Global declarations
|
|
|
|
let kExportSectionCode = 7; // Exports
|
|
|
|
let kStartSectionCode = 8; // Start function declaration
|
|
|
|
let kElementSectionCode = 9; // Elements section
|
|
|
|
let kCodeSectionCode = 10; // Function code
|
|
|
|
let kDataSectionCode = 11; // Data segments
|
2019-02-20 15:39:12 +00:00
|
|
|
let kDataCountSectionCode = 12; // Data segment count (between Element & Code)
|
2020-03-23 17:30:44 +00:00
|
|
|
let kExceptionSectionCode = 13; // Exception section (between Memory & Global)
|
2019-01-30 15:39:14 +00:00
|
|
|
|
|
|
|
// Name section types
|
|
|
|
let kModuleNameCode = 0;
|
|
|
|
let kFunctionNamesCode = 1;
|
|
|
|
let kLocalNamesCode = 2;
|
|
|
|
|
|
|
|
let kWasmFunctionTypeForm = 0x60;
|
|
|
|
let kWasmAnyFunctionTypeForm = 0x70;
|
|
|
|
|
|
|
|
let kHasMaximumFlag = 1;
|
|
|
|
let kSharedHasMaximumFlag = 3;
|
|
|
|
|
|
|
|
// Segment flags
|
|
|
|
let kActiveNoIndex = 0;
|
|
|
|
let kPassive = 1;
|
|
|
|
let kActiveWithIndex = 2;
|
2020-02-17 16:21:02 +00:00
|
|
|
let kDeclarative = 3;
|
2019-09-16 15:40:20 +00:00
|
|
|
let kPassiveWithElements = 5;
|
2020-02-17 16:21:02 +00:00
|
|
|
let kDeclarativeWithElements = 7;
|
2019-01-30 15:39:14 +00:00
|
|
|
|
|
|
|
// Function declaration flags
|
|
|
|
let kDeclFunctionName = 0x01;
|
|
|
|
let kDeclFunctionImport = 0x02;
|
|
|
|
let kDeclFunctionLocals = 0x04;
|
|
|
|
let kDeclFunctionExport = 0x08;
|
|
|
|
|
|
|
|
// Local types
|
|
|
|
let kWasmStmt = 0x40;
|
|
|
|
let kWasmI32 = 0x7f;
|
|
|
|
let kWasmI64 = 0x7e;
|
|
|
|
let kWasmF32 = 0x7d;
|
|
|
|
let kWasmF64 = 0x7c;
|
2019-03-07 14:06:13 +00:00
|
|
|
let kWasmS128 = 0x7b;
|
2020-06-09 15:54:14 +00:00
|
|
|
let kWasmExternRef = 0x6f;
|
2019-01-30 15:39:14 +00:00
|
|
|
let kWasmAnyFunc = 0x70;
|
2019-07-15 08:24:37 +00:00
|
|
|
let kWasmExnRef = 0x68;
|
2019-01-30 15:39:14 +00:00
|
|
|
|
|
|
|
let kExternalFunction = 0;
|
|
|
|
let kExternalTable = 1;
|
|
|
|
let kExternalMemory = 2;
|
|
|
|
let kExternalGlobal = 3;
|
|
|
|
let kExternalException = 4;
|
|
|
|
|
|
|
|
let kTableZero = 0;
|
|
|
|
let kMemoryZero = 0;
|
|
|
|
let kSegmentZero = 0;
|
|
|
|
|
|
|
|
let kExceptionAttribute = 0;
|
|
|
|
|
|
|
|
// Useful signatures
|
|
|
|
let kSig_i_i = makeSig([kWasmI32], [kWasmI32]);
|
|
|
|
let kSig_l_l = makeSig([kWasmI64], [kWasmI64]);
|
|
|
|
let kSig_i_l = makeSig([kWasmI64], [kWasmI32]);
|
|
|
|
let kSig_i_ii = makeSig([kWasmI32, kWasmI32], [kWasmI32]);
|
|
|
|
let kSig_i_iii = makeSig([kWasmI32, kWasmI32, kWasmI32], [kWasmI32]);
|
|
|
|
let kSig_v_iiii = makeSig([kWasmI32, kWasmI32, kWasmI32, kWasmI32], []);
|
|
|
|
let kSig_f_ff = makeSig([kWasmF32, kWasmF32], [kWasmF32]);
|
|
|
|
let kSig_d_dd = makeSig([kWasmF64, kWasmF64], [kWasmF64]);
|
|
|
|
let kSig_l_ll = makeSig([kWasmI64, kWasmI64], [kWasmI64]);
|
|
|
|
let kSig_i_dd = makeSig([kWasmF64, kWasmF64], [kWasmI32]);
|
|
|
|
let kSig_v_v = makeSig([], []);
|
|
|
|
let kSig_i_v = makeSig([], [kWasmI32]);
|
|
|
|
let kSig_l_v = makeSig([], [kWasmI64]);
|
|
|
|
let kSig_f_v = makeSig([], [kWasmF32]);
|
|
|
|
let kSig_d_v = makeSig([], [kWasmF64]);
|
|
|
|
let kSig_v_i = makeSig([kWasmI32], []);
|
|
|
|
let kSig_v_ii = makeSig([kWasmI32, kWasmI32], []);
|
|
|
|
let kSig_v_iii = makeSig([kWasmI32, kWasmI32, kWasmI32], []);
|
|
|
|
let kSig_v_l = makeSig([kWasmI64], []);
|
|
|
|
let kSig_v_d = makeSig([kWasmF64], []);
|
|
|
|
let kSig_v_dd = makeSig([kWasmF64, kWasmF64], []);
|
|
|
|
let kSig_v_ddi = makeSig([kWasmF64, kWasmF64, kWasmI32], []);
|
|
|
|
let kSig_ii_v = makeSig([], [kWasmI32, kWasmI32]);
|
|
|
|
let kSig_iii_v = makeSig([], [kWasmI32, kWasmI32, kWasmI32]);
|
|
|
|
let kSig_ii_i = makeSig([kWasmI32], [kWasmI32, kWasmI32]);
|
|
|
|
let kSig_iii_i = makeSig([kWasmI32], [kWasmI32, kWasmI32, kWasmI32]);
|
|
|
|
let kSig_ii_ii = makeSig([kWasmI32, kWasmI32], [kWasmI32, kWasmI32]);
|
|
|
|
let kSig_iii_ii = makeSig([kWasmI32, kWasmI32], [kWasmI32, kWasmI32, kWasmI32]);
|
|
|
|
|
|
|
|
let kSig_v_f = makeSig([kWasmF32], []);
|
|
|
|
let kSig_f_f = makeSig([kWasmF32], [kWasmF32]);
|
|
|
|
let kSig_f_d = makeSig([kWasmF64], [kWasmF32]);
|
|
|
|
let kSig_d_d = makeSig([kWasmF64], [kWasmF64]);
|
2020-06-09 15:54:14 +00:00
|
|
|
let kSig_r_r = makeSig([kWasmExternRef], [kWasmExternRef]);
|
2019-01-30 15:39:14 +00:00
|
|
|
let kSig_a_a = makeSig([kWasmAnyFunc], [kWasmAnyFunc]);
|
2019-07-15 08:24:37 +00:00
|
|
|
let kSig_e_e = makeSig([kWasmExnRef], [kWasmExnRef]);
|
2020-06-09 15:54:14 +00:00
|
|
|
let kSig_i_r = makeSig([kWasmExternRef], [kWasmI32]);
|
|
|
|
let kSig_v_r = makeSig([kWasmExternRef], []);
|
2019-01-30 15:39:14 +00:00
|
|
|
let kSig_v_a = makeSig([kWasmAnyFunc], []);
|
2019-07-15 08:24:37 +00:00
|
|
|
let kSig_v_e = makeSig([kWasmExnRef], []);
|
2020-06-09 15:54:14 +00:00
|
|
|
let kSig_v_rr = makeSig([kWasmExternRef, kWasmExternRef], []);
|
2019-03-18 11:10:45 +00:00
|
|
|
let kSig_v_aa = makeSig([kWasmAnyFunc, kWasmAnyFunc], []);
|
2020-06-09 15:54:14 +00:00
|
|
|
let kSig_r_v = makeSig([], [kWasmExternRef]);
|
2019-01-30 15:39:14 +00:00
|
|
|
let kSig_a_v = makeSig([], [kWasmAnyFunc]);
|
2019-03-18 10:26:12 +00:00
|
|
|
let kSig_a_i = makeSig([kWasmI32], [kWasmAnyFunc]);
|
2019-07-15 08:24:37 +00:00
|
|
|
let kSig_e_v = makeSig([], [kWasmExnRef]);
|
2020-06-15 23:35:00 +00:00
|
|
|
let kSig_s_i = makeSig([kWasmI32], [kWasmS128]);
|
|
|
|
let kSig_i_s = makeSig([kWasmS128], [kWasmI32]);
|
2019-01-30 15:39:14 +00:00
|
|
|
|
|
|
|
function makeSig(params, results) {
|
|
|
|
return {params: params, results: results};
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeSig_v_x(x) {
|
|
|
|
return makeSig([x], []);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeSig_v_xx(x) {
|
|
|
|
return makeSig([x, x], []);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeSig_r_v(r) {
|
|
|
|
return makeSig([], [r]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeSig_r_x(r, x) {
|
|
|
|
return makeSig([x], [r]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function makeSig_r_xx(r, x) {
|
|
|
|
return makeSig([x, x], [r]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Opcodes
|
|
|
|
let kExprUnreachable = 0x00;
|
|
|
|
let kExprNop = 0x01;
|
|
|
|
let kExprBlock = 0x02;
|
|
|
|
let kExprLoop = 0x03;
|
|
|
|
let kExprIf = 0x04;
|
|
|
|
let kExprElse = 0x05;
|
|
|
|
let kExprTry = 0x06;
|
|
|
|
let kExprCatch = 0x07;
|
|
|
|
let kExprThrow = 0x08;
|
|
|
|
let kExprRethrow = 0x09;
|
|
|
|
let kExprBrOnExn = 0x0a;
|
|
|
|
let kExprEnd = 0x0b;
|
|
|
|
let kExprBr = 0x0c;
|
|
|
|
let kExprBrIf = 0x0d;
|
|
|
|
let kExprBrTable = 0x0e;
|
|
|
|
let kExprReturn = 0x0f;
|
|
|
|
let kExprCallFunction = 0x10;
|
|
|
|
let kExprCallIndirect = 0x11;
|
2019-02-27 18:43:47 +00:00
|
|
|
let kExprReturnCall = 0x12;
|
|
|
|
let kExprReturnCallIndirect = 0x13;
|
2020-08-14 17:17:54 +00:00
|
|
|
let kExprCallRef = 0x14;
|
|
|
|
let kExprReturnCallRef = 0x15;
|
2019-01-30 15:39:14 +00:00
|
|
|
let kExprDrop = 0x1a;
|
|
|
|
let kExprSelect = 0x1b;
|
2020-01-15 14:02:20 +00:00
|
|
|
let kExprSelectWithType = 0x1c;
|
2019-10-08 12:38:48 +00:00
|
|
|
let kExprLocalGet = 0x20;
|
|
|
|
let kExprLocalSet = 0x21;
|
|
|
|
let kExprLocalTee = 0x22;
|
2019-10-08 13:16:30 +00:00
|
|
|
let kExprGlobalGet = 0x23;
|
|
|
|
let kExprGlobalSet = 0x24;
|
2019-07-08 11:49:58 +00:00
|
|
|
let kExprTableGet = 0x25;
|
|
|
|
let kExprTableSet = 0x26;
|
2019-01-30 15:39:14 +00:00
|
|
|
let kExprI32LoadMem = 0x28;
|
|
|
|
let kExprI64LoadMem = 0x29;
|
|
|
|
let kExprF32LoadMem = 0x2a;
|
|
|
|
let kExprF64LoadMem = 0x2b;
|
|
|
|
let kExprI32LoadMem8S = 0x2c;
|
|
|
|
let kExprI32LoadMem8U = 0x2d;
|
|
|
|
let kExprI32LoadMem16S = 0x2e;
|
|
|
|
let kExprI32LoadMem16U = 0x2f;
|
|
|
|
let kExprI64LoadMem8S = 0x30;
|
|
|
|
let kExprI64LoadMem8U = 0x31;
|
|
|
|
let kExprI64LoadMem16S = 0x32;
|
|
|
|
let kExprI64LoadMem16U = 0x33;
|
|
|
|
let kExprI64LoadMem32S = 0x34;
|
|
|
|
let kExprI64LoadMem32U = 0x35;
|
|
|
|
let kExprI32StoreMem = 0x36;
|
|
|
|
let kExprI64StoreMem = 0x37;
|
|
|
|
let kExprF32StoreMem = 0x38;
|
|
|
|
let kExprF64StoreMem = 0x39;
|
|
|
|
let kExprI32StoreMem8 = 0x3a;
|
|
|
|
let kExprI32StoreMem16 = 0x3b;
|
|
|
|
let kExprI64StoreMem8 = 0x3c;
|
|
|
|
let kExprI64StoreMem16 = 0x3d;
|
|
|
|
let kExprI64StoreMem32 = 0x3e;
|
|
|
|
let kExprMemorySize = 0x3f;
|
|
|
|
let kExprMemoryGrow = 0x40;
|
2019-03-04 18:52:03 +00:00
|
|
|
let kExprI32Const = 0x41;
|
|
|
|
let kExprI64Const = 0x42;
|
|
|
|
let kExprF32Const = 0x43;
|
|
|
|
let kExprF64Const = 0x44;
|
2019-01-30 15:39:14 +00:00
|
|
|
let kExprI32Eqz = 0x45;
|
|
|
|
let kExprI32Eq = 0x46;
|
|
|
|
let kExprI32Ne = 0x47;
|
|
|
|
let kExprI32LtS = 0x48;
|
|
|
|
let kExprI32LtU = 0x49;
|
|
|
|
let kExprI32GtS = 0x4a;
|
|
|
|
let kExprI32GtU = 0x4b;
|
|
|
|
let kExprI32LeS = 0x4c;
|
|
|
|
let kExprI32LeU = 0x4d;
|
|
|
|
let kExprI32GeS = 0x4e;
|
|
|
|
let kExprI32GeU = 0x4f;
|
|
|
|
let kExprI64Eqz = 0x50;
|
|
|
|
let kExprI64Eq = 0x51;
|
|
|
|
let kExprI64Ne = 0x52;
|
|
|
|
let kExprI64LtS = 0x53;
|
|
|
|
let kExprI64LtU = 0x54;
|
|
|
|
let kExprI64GtS = 0x55;
|
|
|
|
let kExprI64GtU = 0x56;
|
|
|
|
let kExprI64LeS = 0x57;
|
|
|
|
let kExprI64LeU = 0x58;
|
|
|
|
let kExprI64GeS = 0x59;
|
|
|
|
let kExprI64GeU = 0x5a;
|
|
|
|
let kExprF32Eq = 0x5b;
|
|
|
|
let kExprF32Ne = 0x5c;
|
|
|
|
let kExprF32Lt = 0x5d;
|
|
|
|
let kExprF32Gt = 0x5e;
|
|
|
|
let kExprF32Le = 0x5f;
|
|
|
|
let kExprF32Ge = 0x60;
|
|
|
|
let kExprF64Eq = 0x61;
|
|
|
|
let kExprF64Ne = 0x62;
|
|
|
|
let kExprF64Lt = 0x63;
|
|
|
|
let kExprF64Gt = 0x64;
|
|
|
|
let kExprF64Le = 0x65;
|
|
|
|
let kExprF64Ge = 0x66;
|
|
|
|
let kExprI32Clz = 0x67;
|
|
|
|
let kExprI32Ctz = 0x68;
|
|
|
|
let kExprI32Popcnt = 0x69;
|
|
|
|
let kExprI32Add = 0x6a;
|
|
|
|
let kExprI32Sub = 0x6b;
|
|
|
|
let kExprI32Mul = 0x6c;
|
|
|
|
let kExprI32DivS = 0x6d;
|
|
|
|
let kExprI32DivU = 0x6e;
|
|
|
|
let kExprI32RemS = 0x6f;
|
|
|
|
let kExprI32RemU = 0x70;
|
|
|
|
let kExprI32And = 0x71;
|
|
|
|
let kExprI32Ior = 0x72;
|
|
|
|
let kExprI32Xor = 0x73;
|
|
|
|
let kExprI32Shl = 0x74;
|
|
|
|
let kExprI32ShrS = 0x75;
|
|
|
|
let kExprI32ShrU = 0x76;
|
|
|
|
let kExprI32Rol = 0x77;
|
|
|
|
let kExprI32Ror = 0x78;
|
|
|
|
let kExprI64Clz = 0x79;
|
|
|
|
let kExprI64Ctz = 0x7a;
|
|
|
|
let kExprI64Popcnt = 0x7b;
|
|
|
|
let kExprI64Add = 0x7c;
|
|
|
|
let kExprI64Sub = 0x7d;
|
|
|
|
let kExprI64Mul = 0x7e;
|
|
|
|
let kExprI64DivS = 0x7f;
|
|
|
|
let kExprI64DivU = 0x80;
|
|
|
|
let kExprI64RemS = 0x81;
|
|
|
|
let kExprI64RemU = 0x82;
|
|
|
|
let kExprI64And = 0x83;
|
|
|
|
let kExprI64Ior = 0x84;
|
|
|
|
let kExprI64Xor = 0x85;
|
|
|
|
let kExprI64Shl = 0x86;
|
|
|
|
let kExprI64ShrS = 0x87;
|
|
|
|
let kExprI64ShrU = 0x88;
|
|
|
|
let kExprI64Rol = 0x89;
|
|
|
|
let kExprI64Ror = 0x8a;
|
|
|
|
let kExprF32Abs = 0x8b;
|
|
|
|
let kExprF32Neg = 0x8c;
|
|
|
|
let kExprF32Ceil = 0x8d;
|
|
|
|
let kExprF32Floor = 0x8e;
|
|
|
|
let kExprF32Trunc = 0x8f;
|
|
|
|
let kExprF32NearestInt = 0x90;
|
|
|
|
let kExprF32Sqrt = 0x91;
|
|
|
|
let kExprF32Add = 0x92;
|
|
|
|
let kExprF32Sub = 0x93;
|
|
|
|
let kExprF32Mul = 0x94;
|
|
|
|
let kExprF32Div = 0x95;
|
|
|
|
let kExprF32Min = 0x96;
|
|
|
|
let kExprF32Max = 0x97;
|
|
|
|
let kExprF32CopySign = 0x98;
|
|
|
|
let kExprF64Abs = 0x99;
|
|
|
|
let kExprF64Neg = 0x9a;
|
|
|
|
let kExprF64Ceil = 0x9b;
|
|
|
|
let kExprF64Floor = 0x9c;
|
|
|
|
let kExprF64Trunc = 0x9d;
|
|
|
|
let kExprF64NearestInt = 0x9e;
|
|
|
|
let kExprF64Sqrt = 0x9f;
|
|
|
|
let kExprF64Add = 0xa0;
|
|
|
|
let kExprF64Sub = 0xa1;
|
|
|
|
let kExprF64Mul = 0xa2;
|
|
|
|
let kExprF64Div = 0xa3;
|
|
|
|
let kExprF64Min = 0xa4;
|
|
|
|
let kExprF64Max = 0xa5;
|
|
|
|
let kExprF64CopySign = 0xa6;
|
|
|
|
let kExprI32ConvertI64 = 0xa7;
|
|
|
|
let kExprI32SConvertF32 = 0xa8;
|
|
|
|
let kExprI32UConvertF32 = 0xa9;
|
|
|
|
let kExprI32SConvertF64 = 0xaa;
|
|
|
|
let kExprI32UConvertF64 = 0xab;
|
|
|
|
let kExprI64SConvertI32 = 0xac;
|
|
|
|
let kExprI64UConvertI32 = 0xad;
|
|
|
|
let kExprI64SConvertF32 = 0xae;
|
|
|
|
let kExprI64UConvertF32 = 0xaf;
|
|
|
|
let kExprI64SConvertF64 = 0xb0;
|
|
|
|
let kExprI64UConvertF64 = 0xb1;
|
|
|
|
let kExprF32SConvertI32 = 0xb2;
|
|
|
|
let kExprF32UConvertI32 = 0xb3;
|
|
|
|
let kExprF32SConvertI64 = 0xb4;
|
|
|
|
let kExprF32UConvertI64 = 0xb5;
|
|
|
|
let kExprF32ConvertF64 = 0xb6;
|
|
|
|
let kExprF64SConvertI32 = 0xb7;
|
|
|
|
let kExprF64UConvertI32 = 0xb8;
|
|
|
|
let kExprF64SConvertI64 = 0xb9;
|
|
|
|
let kExprF64UConvertI64 = 0xba;
|
|
|
|
let kExprF64ConvertF32 = 0xbb;
|
|
|
|
let kExprI32ReinterpretF32 = 0xbc;
|
|
|
|
let kExprI64ReinterpretF64 = 0xbd;
|
|
|
|
let kExprF32ReinterpretI32 = 0xbe;
|
|
|
|
let kExprF64ReinterpretI64 = 0xbf;
|
|
|
|
let kExprI32SExtendI8 = 0xc0;
|
|
|
|
let kExprI32SExtendI16 = 0xc1;
|
|
|
|
let kExprI64SExtendI8 = 0xc2;
|
|
|
|
let kExprI64SExtendI16 = 0xc3;
|
|
|
|
let kExprI64SExtendI32 = 0xc4;
|
2019-03-04 18:52:03 +00:00
|
|
|
let kExprRefNull = 0xd0;
|
|
|
|
let kExprRefIsNull = 0xd1;
|
|
|
|
let kExprRefFunc = 0xd2;
|
2019-01-30 15:39:14 +00:00
|
|
|
|
|
|
|
// Prefix opcodes
|
2020-08-14 17:17:54 +00:00
|
|
|
let kGCPrefix = 0xfb;
|
2019-01-30 15:39:14 +00:00
|
|
|
let kNumericPrefix = 0xfc;
|
|
|
|
let kSimdPrefix = 0xfd;
|
|
|
|
let kAtomicPrefix = 0xfe;
|
|
|
|
|
2020-08-14 17:17:54 +00:00
|
|
|
// GC opcodes
|
|
|
|
let kExprRttCanon = 0x30;
|
|
|
|
let kExprRefCast = 0x41;
|
|
|
|
|
2019-01-30 15:39:14 +00:00
|
|
|
// Numeric opcodes.
|
|
|
|
let kExprMemoryInit = 0x08;
|
2019-01-31 18:45:51 +00:00
|
|
|
let kExprDataDrop = 0x09;
|
2019-01-30 15:39:14 +00:00
|
|
|
let kExprMemoryCopy = 0x0a;
|
|
|
|
let kExprMemoryFill = 0x0b;
|
|
|
|
let kExprTableInit = 0x0c;
|
2019-01-31 18:45:51 +00:00
|
|
|
let kExprElemDrop = 0x0d;
|
2019-01-30 15:39:14 +00:00
|
|
|
let kExprTableCopy = 0x0e;
|
2019-05-03 08:06:10 +00:00
|
|
|
let kExprTableGrow = 0x0f;
|
2019-05-03 08:12:32 +00:00
|
|
|
let kExprTableSize = 0x10;
|
2019-05-07 11:00:04 +00:00
|
|
|
let kExprTableFill = 0x11;
|
2019-01-30 15:39:14 +00:00
|
|
|
|
|
|
|
// Atomic opcodes.
|
2019-03-09 00:50:42 +00:00
|
|
|
let kExprAtomicNotify = 0x00;
|
2019-01-30 15:39:14 +00:00
|
|
|
let kExprI32AtomicWait = 0x01;
|
|
|
|
let kExprI64AtomicWait = 0x02;
|
|
|
|
let kExprI32AtomicLoad = 0x10;
|
|
|
|
let kExprI32AtomicLoad8U = 0x12;
|
|
|
|
let kExprI32AtomicLoad16U = 0x13;
|
|
|
|
let kExprI32AtomicStore = 0x17;
|
|
|
|
let kExprI32AtomicStore8U = 0x19;
|
|
|
|
let kExprI32AtomicStore16U = 0x1a;
|
|
|
|
let kExprI32AtomicAdd = 0x1e;
|
|
|
|
let kExprI32AtomicAdd8U = 0x20;
|
|
|
|
let kExprI32AtomicAdd16U = 0x21;
|
|
|
|
let kExprI32AtomicSub = 0x25;
|
|
|
|
let kExprI32AtomicSub8U = 0x27;
|
|
|
|
let kExprI32AtomicSub16U = 0x28;
|
|
|
|
let kExprI32AtomicAnd = 0x2c;
|
|
|
|
let kExprI32AtomicAnd8U = 0x2e;
|
|
|
|
let kExprI32AtomicAnd16U = 0x2f;
|
|
|
|
let kExprI32AtomicOr = 0x33;
|
|
|
|
let kExprI32AtomicOr8U = 0x35;
|
|
|
|
let kExprI32AtomicOr16U = 0x36;
|
|
|
|
let kExprI32AtomicXor = 0x3a;
|
|
|
|
let kExprI32AtomicXor8U = 0x3c;
|
|
|
|
let kExprI32AtomicXor16U = 0x3d;
|
|
|
|
let kExprI32AtomicExchange = 0x41;
|
|
|
|
let kExprI32AtomicExchange8U = 0x43;
|
|
|
|
let kExprI32AtomicExchange16U = 0x44;
|
|
|
|
let kExprI32AtomicCompareExchange = 0x48;
|
|
|
|
let kExprI32AtomicCompareExchange8U = 0x4a;
|
|
|
|
let kExprI32AtomicCompareExchange16U = 0x4b;
|
|
|
|
|
|
|
|
let kExprI64AtomicLoad = 0x11;
|
|
|
|
let kExprI64AtomicLoad8U = 0x14;
|
|
|
|
let kExprI64AtomicLoad16U = 0x15;
|
|
|
|
let kExprI64AtomicLoad32U = 0x16;
|
|
|
|
let kExprI64AtomicStore = 0x18;
|
|
|
|
let kExprI64AtomicStore8U = 0x1b;
|
|
|
|
let kExprI64AtomicStore16U = 0x1c;
|
|
|
|
let kExprI64AtomicStore32U = 0x1d;
|
|
|
|
let kExprI64AtomicAdd = 0x1f;
|
|
|
|
let kExprI64AtomicAdd8U = 0x22;
|
|
|
|
let kExprI64AtomicAdd16U = 0x23;
|
|
|
|
let kExprI64AtomicAdd32U = 0x24;
|
|
|
|
let kExprI64AtomicSub = 0x26;
|
|
|
|
let kExprI64AtomicSub8U = 0x29;
|
|
|
|
let kExprI64AtomicSub16U = 0x2a;
|
|
|
|
let kExprI64AtomicSub32U = 0x2b;
|
|
|
|
let kExprI64AtomicAnd = 0x2d;
|
|
|
|
let kExprI64AtomicAnd8U = 0x30;
|
|
|
|
let kExprI64AtomicAnd16U = 0x31;
|
|
|
|
let kExprI64AtomicAnd32U = 0x32;
|
|
|
|
let kExprI64AtomicOr = 0x34;
|
|
|
|
let kExprI64AtomicOr8U = 0x37;
|
|
|
|
let kExprI64AtomicOr16U = 0x38;
|
|
|
|
let kExprI64AtomicOr32U = 0x39;
|
|
|
|
let kExprI64AtomicXor = 0x3b;
|
|
|
|
let kExprI64AtomicXor8U = 0x3e;
|
|
|
|
let kExprI64AtomicXor16U = 0x3f;
|
|
|
|
let kExprI64AtomicXor32U = 0x40;
|
|
|
|
let kExprI64AtomicExchange = 0x42;
|
|
|
|
let kExprI64AtomicExchange8U = 0x45;
|
|
|
|
let kExprI64AtomicExchange16U = 0x46;
|
|
|
|
let kExprI64AtomicExchange32U = 0x47;
|
|
|
|
let kExprI64AtomicCompareExchange = 0x49
|
|
|
|
let kExprI64AtomicCompareExchange8U = 0x4c;
|
|
|
|
let kExprI64AtomicCompareExchange16U = 0x4d;
|
|
|
|
let kExprI64AtomicCompareExchange32U = 0x4e;
|
|
|
|
|
|
|
|
// Simd opcodes.
|
2019-10-10 23:56:09 +00:00
|
|
|
let kExprS128LoadMem = 0x00;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI16x8Load8x8S = 0x01;
|
|
|
|
let kExprI16x8Load8x8U = 0x02;
|
|
|
|
let kExprI32x4Load16x4S = 0x03;
|
|
|
|
let kExprI32x4Load16x4U = 0x04;
|
|
|
|
let kExprI64x2Load32x2S = 0x05;
|
|
|
|
let kExprI64x2Load32x2U = 0x06;
|
|
|
|
let kExprS8x16LoadSplat = 0x07;
|
|
|
|
let kExprS16x8LoadSplat = 0x08;
|
|
|
|
let kExprS32x4LoadSplat = 0x09;
|
|
|
|
let kExprS64x2LoadSplat = 0x0a;
|
2020-05-01 15:40:22 +00:00
|
|
|
let kExprS128StoreMem = 0x0b;
|
2020-08-18 11:39:30 +00:00
|
|
|
|
2020-07-24 04:12:09 +00:00
|
|
|
let kExprS128Const = 0x0c;
|
2020-05-01 15:40:22 +00:00
|
|
|
let kExprS8x16Shuffle = 0x0d;
|
2020-08-18 11:39:30 +00:00
|
|
|
|
|
|
|
let kExprS8x16Swizzle = 0x0e;
|
2020-05-01 15:40:22 +00:00
|
|
|
let kExprI8x16Splat = 0x0f;
|
|
|
|
let kExprI16x8Splat = 0x10;
|
|
|
|
let kExprI32x4Splat = 0x11;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI64x2Splat = 0x12;
|
2020-05-01 15:40:22 +00:00
|
|
|
let kExprF32x4Splat = 0x13;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprF64x2Splat = 0x14;
|
|
|
|
let kExprI8x16ReplaceLane = 0x17;
|
2020-08-18 22:06:22 +00:00
|
|
|
let kExprI16x8ExtractLaneS = 0x18;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI16x8ReplaceLane = 0x1a;
|
2020-07-24 04:12:09 +00:00
|
|
|
let kExprI32x4ExtractLane = 0x1b;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI32x4ReplaceLane = 0x1c;
|
|
|
|
let kExprI64x2ReplaceLane = 0x1e;
|
|
|
|
let kExprF32x4ReplaceLane = 0x20;
|
|
|
|
let kExprF64x2ReplaceLane = 0x22;
|
|
|
|
let kExprI8x16Eq = 0x23;
|
|
|
|
let kExprI8x16Ne = 0x24;
|
|
|
|
let kExprI8x16LtS = 0x25;
|
2020-05-01 15:40:22 +00:00
|
|
|
let kExprI8x16LtU = 0x26;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI8x16GtS = 0x27;
|
|
|
|
let kExprI8x16GtU = 0x28;
|
|
|
|
let kExprI8x16LeS = 0x29;
|
2020-05-01 15:40:22 +00:00
|
|
|
let kExprI8x16LeU = 0x2a;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI8x16GeS = 0x2b;
|
|
|
|
let kExprI8x16GeU = 0x2c;
|
|
|
|
let kExprI16x8Eq = 0x2d;
|
|
|
|
let kExprI16x8Ne = 0x2e;
|
|
|
|
let kExprI16x8LtS = 0x2f;
|
|
|
|
let kExprI16x8LtU = 0x30;
|
|
|
|
let kExprI16x8GtS = 0x31;
|
|
|
|
let kExprI16x8GtU = 0x32;
|
|
|
|
let kExprI16x8LeS = 0x33;
|
|
|
|
let kExprI16x8LeU = 0x34;
|
|
|
|
let kExprI16x8GeS = 0x35;
|
|
|
|
let kExprI16x8GeU = 0x36;
|
2020-05-01 15:40:22 +00:00
|
|
|
let kExprI32x4Eq = 0x37;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI32x4Ne = 0x38;
|
|
|
|
let kExprI32x4LtS = 0x39;
|
|
|
|
let kExprI32x4LtU = 0x3a;
|
|
|
|
let kExprI32x4GtS = 0x3b;
|
|
|
|
let kExprI32x4GtU = 0x3c;
|
|
|
|
let kExprI32x4LeS = 0x3d;
|
|
|
|
let kExprI32x4LeU = 0x3e;
|
|
|
|
let kExprI32x4GeS = 0x3f;
|
|
|
|
let kExprI32x4GeU = 0x40;
|
|
|
|
let kExprF32x4Eq = 0x41;
|
|
|
|
let kExprF32x4Ne = 0x42;
|
|
|
|
let kExprF32x4Lt = 0x43;
|
|
|
|
let kExprF32x4Gt = 0x44;
|
|
|
|
let kExprF32x4Le = 0x45;
|
|
|
|
let kExprF32x4Ge = 0x46;
|
|
|
|
let kExprF64x2Eq = 0x47;
|
|
|
|
let kExprF64x2Ne = 0x48;
|
|
|
|
let kExprF64x2Lt = 0x49;
|
|
|
|
let kExprF64x2Gt = 0x4a;
|
|
|
|
let kExprF64x2Le = 0x4b;
|
|
|
|
let kExprF64x2Ge = 0x4c;
|
|
|
|
let kExprS128Not = 0x4d;
|
|
|
|
let kExprS128And = 0x4e;
|
|
|
|
let kExprS128AndNot = 0x4f;
|
|
|
|
let kExprS128Or = 0x50;
|
|
|
|
let kExprS128Xor = 0x51;
|
|
|
|
let kExprS128Select = 0x52;
|
|
|
|
let kExprI8x16Abs = 0x60;
|
|
|
|
let kExprI8x16Neg = 0x61;
|
2020-05-21 18:27:47 +00:00
|
|
|
let kExprV8x16AnyTrue = 0x62;
|
|
|
|
let kExprV8x16AllTrue = 0x63;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI8x16SConvertI16x8 = 0x65;
|
|
|
|
let kExprI8x16UConvertI16x8 = 0x66;
|
|
|
|
let kExprI8x16Shl = 0x6b;
|
|
|
|
let kExprI8x16ShrS = 0x6c;
|
|
|
|
let kExprI8x16ShrU = 0x6d;
|
2020-05-01 15:40:22 +00:00
|
|
|
let kExprI8x16Add = 0x6e;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI8x16AddSaturateS = 0x6f;
|
|
|
|
let kExprI8x16AddSaturateU = 0x70;
|
|
|
|
let kExprI8x16Sub = 0x71;
|
|
|
|
let kExprI8x16SubSaturateS = 0x72;
|
|
|
|
let kExprI8x16SubSaturateU = 0x73;
|
|
|
|
let kExprI8x16MinS = 0x76;
|
|
|
|
let kExprI8x16MinU = 0x77;
|
|
|
|
let kExprI8x16MaxS = 0x78;
|
|
|
|
let kExprI8x16MaxU = 0x79;
|
|
|
|
let kExprI8x16RoundingAverageU = 0x7b;
|
|
|
|
let kExprI16x8Abs = 0x80;
|
|
|
|
let kExprI16x8Neg = 0x81;
|
|
|
|
let kExprV16x8AnyTrue = 0x82;
|
|
|
|
let kExprV16x8AllTrue = 0x83;
|
|
|
|
let kExprI16x8SConvertI32x4 = 0x85;
|
|
|
|
let kExprI16x8UConvertI32x4 = 0x86;
|
|
|
|
let kExprI16x8SConvertI8x16Low = 0x87;
|
|
|
|
let kExprI16x8SConvertI8x16High = 0x88;
|
|
|
|
let kExprI16x8UConvertI8x16Low = 0x89;
|
|
|
|
let kExprI16x8UConvertI8x16High = 0x8a;
|
|
|
|
let kExprI16x8Shl = 0x8b;
|
|
|
|
let kExprI16x8ShrS = 0x8c;
|
|
|
|
let kExprI16x8ShrU = 0x8d;
|
|
|
|
let kExprI16x8Add = 0x8e;
|
|
|
|
let kExprI16x8AddSaturateS = 0x8f;
|
|
|
|
let kExprI16x8AddSaturateU = 0x90;
|
|
|
|
let kExprI16x8Sub = 0x91;
|
|
|
|
let kExprI16x8SubSaturateS = 0x92;
|
|
|
|
let kExprI16x8SubSaturateU = 0x93;
|
|
|
|
let kExprI16x8Mul = 0x95;
|
|
|
|
let kExprI16x8MinS = 0x96;
|
|
|
|
let kExprI16x8MinU = 0x97;
|
|
|
|
let kExprI16x8MaxS = 0x98;
|
|
|
|
let kExprI16x8MaxU = 0x99;
|
|
|
|
let kExprI16x8RoundingAverageU = 0x9b;
|
|
|
|
let kExprI32x4Abs = 0xa0;
|
|
|
|
let kExprI32x4Neg = 0xa1;
|
2020-05-21 18:27:47 +00:00
|
|
|
let kExprV32x4AnyTrue = 0xa2;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprV32x4AllTrue = 0xa3;
|
|
|
|
let kExprI32x4SConvertI16x8Low = 0xa7;
|
|
|
|
let kExprI32x4SConvertI16x8High = 0xa8;
|
|
|
|
let kExprI32x4UConvertI16x8Low = 0xa9;
|
|
|
|
let kExprI32x4UConvertI16x8High = 0xaa;
|
|
|
|
let kExprI32x4Shl = 0xab;
|
|
|
|
let kExprI32x4ShrS = 0xac;
|
|
|
|
let kExprI32x4ShrU = 0xad;
|
|
|
|
let kExprI32x4Add = 0xae;
|
|
|
|
let kExprI32x4Sub = 0xb1;
|
|
|
|
let kExprI32x4Mul = 0xb5;
|
|
|
|
let kExprI32x4MinS = 0xb6;
|
|
|
|
let kExprI32x4MinU = 0xb7;
|
|
|
|
let kExprI32x4MaxS = 0xb8;
|
|
|
|
let kExprI32x4MaxU = 0xb9;
|
|
|
|
let kExprI64x2Neg = 0xc1;
|
|
|
|
let kExprI64x2Shl = 0xcb;
|
2020-08-04 17:11:33 +00:00
|
|
|
let kExprI64x2ShrS = 0xcc;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprI64x2ShrU = 0xcd;
|
|
|
|
let kExprI64x2Add = 0xce;
|
|
|
|
let kExprI64x2Sub = 0xd1;
|
|
|
|
let kExprI64x2Mul = 0xd5;
|
|
|
|
let kExprF32x4Abs = 0xe0;
|
|
|
|
let kExprF32x4Neg = 0xe1;
|
|
|
|
let kExprF32x4Sqrt = 0xe3;
|
|
|
|
let kExprF32x4Add = 0xe4;
|
|
|
|
let kExprF32x4Sub = 0xe5;
|
|
|
|
let kExprF32x4Mul = 0xe6;
|
|
|
|
let kExprF32x4Div = 0xe7;
|
2020-05-11 22:07:30 +00:00
|
|
|
let kExprF32x4Min = 0xe8;
|
2020-08-18 11:39:30 +00:00
|
|
|
let kExprF32x4Max = 0xe9;
|
|
|
|
let kExprF64x2Abs = 0xec;
|
|
|
|
let kExprF64x2Neg = 0xed;
|
|
|
|
let kExprF64x2Sqrt = 0xef;
|
|
|
|
let kExprF64x2Add = 0xf0;
|
|
|
|
let kExprF64x2Sub = 0xf1;
|
|
|
|
let kExprF64x2Mul = 0xf2;
|
|
|
|
let kExprF64x2Div = 0xf3;
|
|
|
|
let kExprF64x2Min = 0xf4;
|
|
|
|
let kExprF64x2Max = 0xf5;
|
|
|
|
let kExprI32x4SConvertF32x4 = 0xf8;
|
|
|
|
let kExprI32x4UConvertF32x4 = 0xf9;
|
|
|
|
let kExprF32x4SConvertI32x4 = 0xfa;
|
|
|
|
let kExprF32x4UConvertI32x4 = 0xfb;
|
2019-01-30 15:39:14 +00:00
|
|
|
|
2019-03-18 14:30:34 +00:00
|
|
|
// Compilation hint constants.
|
|
|
|
let kCompilationHintStrategyDefault = 0x00;
|
|
|
|
let kCompilationHintStrategyLazy = 0x01;
|
|
|
|
let kCompilationHintStrategyEager = 0x02;
|
2019-04-25 15:54:16 +00:00
|
|
|
let kCompilationHintStrategyLazyBaselineEagerTopTier = 0x03;
|
2019-03-18 14:30:34 +00:00
|
|
|
let kCompilationHintTierDefault = 0x00;
|
2020-04-29 15:04:46 +00:00
|
|
|
let kCompilationHintTierBaseline = 0x01;
|
|
|
|
let kCompilationHintTierOptimized = 0x02;
|
2019-03-18 14:30:34 +00:00
|
|
|
|
2019-01-30 15:39:14 +00:00
|
|
|
let kTrapUnreachable = 0;
|
|
|
|
let kTrapMemOutOfBounds = 1;
|
|
|
|
let kTrapDivByZero = 2;
|
|
|
|
let kTrapDivUnrepresentable = 3;
|
|
|
|
let kTrapRemByZero = 4;
|
|
|
|
let kTrapFloatUnrepresentable = 5;
|
|
|
|
let kTrapFuncInvalid = 6;
|
|
|
|
let kTrapFuncSigMismatch = 7;
|
|
|
|
let kTrapTypeError = 8;
|
|
|
|
let kTrapUnalignedAccess = 9;
|
|
|
|
let kTrapDataSegmentDropped = 10;
|
|
|
|
let kTrapElemSegmentDropped = 11;
|
2019-02-12 12:03:42 +00:00
|
|
|
let kTrapTableOutOfBounds = 12;
|
2020-06-10 07:22:22 +00:00
|
|
|
let kTrapBrOnExnNull = 13;
|
|
|
|
let kTrapRethrowNull = 14;
|
2019-01-30 15:39:14 +00:00
|
|
|
|
|
|
|
let kTrapMsgs = [
|
|
|
|
"unreachable",
|
|
|
|
"memory access out of bounds",
|
|
|
|
"divide by zero",
|
|
|
|
"divide result unrepresentable",
|
|
|
|
"remainder by zero",
|
|
|
|
"float unrepresentable in integer range",
|
|
|
|
"invalid index into function table",
|
|
|
|
"function signature mismatch",
|
|
|
|
"wasm function signature contains illegal type",
|
|
|
|
"operation does not support unaligned accesses",
|
|
|
|
"data segment has been dropped",
|
2019-02-12 12:03:42 +00:00
|
|
|
"element segment has been dropped",
|
2020-03-23 10:46:51 +00:00
|
|
|
"table access out of bounds",
|
2020-06-10 07:22:22 +00:00
|
|
|
"br_on_exn on null value",
|
|
|
|
"rethrowing null value"
|
2019-01-30 15:39:14 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
function assertTraps(trap, code) {
|
2019-02-01 13:51:23 +00:00
|
|
|
assertThrows(code, WebAssembly.RuntimeError, kTrapMsgs[trap]);
|
2019-01-30 15:39:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 14:06:13 +00:00
|
|
|
class Binary {
|
|
|
|
constructor() {
|
|
|
|
this.length = 0;
|
|
|
|
this.buffer = new Uint8Array(8192);
|
|
|
|
}
|
|
|
|
|
|
|
|
ensure_space(needed) {
|
|
|
|
if (this.buffer.length - this.length >= needed) return;
|
|
|
|
let new_capacity = this.buffer.length * 2;
|
|
|
|
while (new_capacity - this.length < needed) new_capacity *= 2;
|
|
|
|
let new_buffer = new Uint8Array(new_capacity);
|
|
|
|
new_buffer.set(this.buffer);
|
|
|
|
this.buffer = new_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
trunc_buffer() {
|
2019-03-12 15:43:37 +00:00
|
|
|
return new Uint8Array(this.buffer.buffer, 0, this.length);
|
2019-03-07 14:06:13 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 12:12:05 +00:00
|
|
|
reset() {
|
|
|
|
this.length = 0;
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
emit_u8(val) {
|
2019-03-07 14:06:13 +00:00
|
|
|
this.ensure_space(1);
|
|
|
|
this.buffer[this.length++] = val;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit_u16(val) {
|
2019-03-07 14:06:13 +00:00
|
|
|
this.ensure_space(2);
|
|
|
|
this.buffer[this.length++] = val;
|
|
|
|
this.buffer[this.length++] = val >> 8;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit_u32(val) {
|
2019-03-07 14:06:13 +00:00
|
|
|
this.ensure_space(4);
|
|
|
|
this.buffer[this.length++] = val;
|
|
|
|
this.buffer[this.length++] = val >> 8;
|
|
|
|
this.buffer[this.length++] = val >> 16;
|
|
|
|
this.buffer[this.length++] = val >> 24;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 11:19:40 +00:00
|
|
|
emit_leb_u(val, max_len) {
|
2019-03-08 09:48:22 +00:00
|
|
|
this.ensure_space(max_len);
|
|
|
|
for (let i = 0; i < max_len; ++i) {
|
2016-06-21 19:47:51 +00:00
|
|
|
let v = val & 0xff;
|
|
|
|
val = val >>> 7;
|
|
|
|
if (val == 0) {
|
2019-03-07 14:06:13 +00:00
|
|
|
this.buffer[this.length++] = v;
|
2019-03-08 09:48:22 +00:00
|
|
|
return;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2019-03-07 14:06:13 +00:00
|
|
|
this.buffer[this.length++] = v | 0x80;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2019-03-08 09:48:22 +00:00
|
|
|
throw new Error("Leb value exceeds maximum length of " + max_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
emit_u32v(val) {
|
2019-07-30 11:19:40 +00:00
|
|
|
this.emit_leb_u(val, kMaxVarInt32Size);
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 17:05:35 +00:00
|
|
|
emit_u64v(val) {
|
2019-07-30 11:19:40 +00:00
|
|
|
this.emit_leb_u(val, kMaxVarInt64Size);
|
2019-01-29 17:05:35 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
emit_bytes(data) {
|
2019-03-07 14:06:13 +00:00
|
|
|
this.ensure_space(data.length);
|
|
|
|
this.buffer.set(data, this.length);
|
|
|
|
this.length += data.length;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit_string(string) {
|
|
|
|
// When testing illegal names, we pass a byte array directly.
|
|
|
|
if (string instanceof Array) {
|
2016-10-06 15:43:10 +00:00
|
|
|
this.emit_u32v(string.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
this.emit_bytes(string);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is the hacky way to convert a JavaScript string to a UTF8 encoded
|
|
|
|
// string only containing single-byte characters.
|
|
|
|
let string_utf8 = unescape(encodeURIComponent(string));
|
2016-10-06 15:43:10 +00:00
|
|
|
this.emit_u32v(string_utf8.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
for (let i = 0; i < string_utf8.length; i++) {
|
|
|
|
this.emit_u8(string_utf8.charCodeAt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
emit_header() {
|
2019-03-07 14:06:13 +00:00
|
|
|
this.emit_bytes([
|
|
|
|
kWasmH0, kWasmH1, kWasmH2, kWasmH3, kWasmV0, kWasmV1, kWasmV2, kWasmV3
|
|
|
|
]);
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
emit_section(section_code, content_generator) {
|
|
|
|
// Emit section name.
|
2016-09-27 20:46:10 +00:00
|
|
|
this.emit_u8(section_code);
|
2016-06-21 19:47:51 +00:00
|
|
|
// Emit the section to a temporary buffer: its full length isn't know yet.
|
2018-10-26 17:28:37 +00:00
|
|
|
const section = new Binary;
|
2016-06-21 19:47:51 +00:00
|
|
|
content_generator(section);
|
|
|
|
// Emit section length.
|
2016-10-06 15:43:10 +00:00
|
|
|
this.emit_u32v(section.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
// Copy the temporary buffer.
|
2018-11-09 16:54:01 +00:00
|
|
|
// Avoid spread because {section} can be huge.
|
2019-03-07 14:06:13 +00:00
|
|
|
this.emit_bytes(section.trunc_buffer());
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WasmFunctionBuilder {
|
2016-10-06 15:43:10 +00:00
|
|
|
constructor(module, name, type_index) {
|
|
|
|
this.module = module;
|
2016-03-07 10:01:24 +00:00
|
|
|
this.name = name;
|
2016-06-21 19:47:51 +00:00
|
|
|
this.type_index = type_index;
|
2016-10-13 16:17:44 +00:00
|
|
|
this.body = [];
|
2018-10-26 11:05:14 +00:00
|
|
|
this.locals = [];
|
|
|
|
this.local_names = [];
|
2020-01-09 12:11:55 +00:00
|
|
|
this.body_offset = undefined; // Not valid until module is serialized.
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2017-07-02 19:18:57 +00:00
|
|
|
numLocalNames() {
|
|
|
|
let num_local_names = 0;
|
|
|
|
for (let loc_name of this.local_names) {
|
|
|
|
if (loc_name !== undefined) ++num_local_names;
|
|
|
|
}
|
|
|
|
return num_local_names;
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
exportAs(name) {
|
2016-10-17 11:39:04 +00:00
|
|
|
this.module.addExport(name, this.index);
|
2016-03-07 10:01:24 +00:00
|
|
|
return this;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
exportFunc() {
|
2016-10-06 15:43:10 +00:00
|
|
|
this.exportAs(this.name);
|
2016-06-21 19:47:51 +00:00
|
|
|
return this;
|
|
|
|
}
|
2016-03-07 19:32:35 +00:00
|
|
|
|
2019-04-17 17:55:36 +00:00
|
|
|
setCompilationHint(strategy, baselineTier, topTier) {
|
|
|
|
this.module.setCompilationHint(strategy, baselineTier, topTier, this.index);
|
2019-03-18 14:30:34 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
addBody(body) {
|
2017-02-16 14:12:01 +00:00
|
|
|
for (let b of body) {
|
2017-07-26 11:01:21 +00:00
|
|
|
if (typeof b !== 'number' || (b & (~0xFF)) !== 0 )
|
|
|
|
throw new Error('invalid body (entries must be 8 bit numbers): ' + body);
|
2017-02-16 14:12:01 +00:00
|
|
|
}
|
2017-03-31 08:45:15 +00:00
|
|
|
this.body = body.slice();
|
2017-01-15 21:18:53 +00:00
|
|
|
// Automatically add the end for the function block to the body.
|
2017-03-31 08:45:15 +00:00
|
|
|
this.body.push(kExprEnd);
|
2016-03-07 10:01:24 +00:00
|
|
|
return this;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2017-01-25 14:10:04 +00:00
|
|
|
addBodyWithEnd(body) {
|
|
|
|
this.body = body;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-01-08 11:54:54 +00:00
|
|
|
getNumLocals() {
|
|
|
|
let total_locals = 0;
|
2018-10-26 11:05:14 +00:00
|
|
|
for (let l of this.locals) {
|
2018-01-08 11:54:54 +00:00
|
|
|
for (let type of ["i32", "i64", "f32", "f64", "s128"]) {
|
|
|
|
total_locals += l[type + "_count"] || 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return total_locals;
|
|
|
|
}
|
|
|
|
|
2017-07-02 19:18:57 +00:00
|
|
|
addLocals(locals, names) {
|
2018-01-08 11:54:54 +00:00
|
|
|
const old_num_locals = this.getNumLocals();
|
|
|
|
this.locals.push(locals);
|
|
|
|
if (names) {
|
|
|
|
const missing_names = old_num_locals - this.local_names.length;
|
|
|
|
this.local_names.push(...new Array(missing_names), ...names);
|
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
return this;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-10-13 16:17:44 +00:00
|
|
|
|
|
|
|
end() {
|
|
|
|
return this.module;
|
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
class WasmGlobalBuilder {
|
|
|
|
constructor(module, type, mutable) {
|
|
|
|
this.module = module;
|
|
|
|
this.type = type;
|
|
|
|
this.mutable = mutable;
|
|
|
|
this.init = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
exportAs(name) {
|
2016-10-25 09:44:15 +00:00
|
|
|
this.module.exports.push({name: name, kind: kExternalGlobal,
|
|
|
|
index: this.index});
|
2016-10-06 15:43:10 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 12:03:42 +00:00
|
|
|
class WasmTableBuilder {
|
|
|
|
constructor(module, type, initial_size, max_size) {
|
|
|
|
this.module = module;
|
|
|
|
this.type = type;
|
|
|
|
this.initial_size = initial_size;
|
|
|
|
this.has_max = max_size != undefined;
|
|
|
|
this.max_size = max_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
exportAs(name) {
|
|
|
|
this.module.exports.push({name: name, kind: kExternalTable,
|
|
|
|
index: this.index});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
class WasmModuleBuilder {
|
|
|
|
constructor() {
|
|
|
|
this.types = [];
|
2016-03-07 10:01:24 +00:00
|
|
|
this.imports = [];
|
2016-10-06 15:43:10 +00:00
|
|
|
this.exports = [];
|
2016-08-31 00:35:40 +00:00
|
|
|
this.globals = [];
|
2019-02-12 12:03:42 +00:00
|
|
|
this.tables = [];
|
2017-08-01 20:56:39 +00:00
|
|
|
this.exceptions = [];
|
2016-03-07 10:01:24 +00:00
|
|
|
this.functions = [];
|
2019-03-18 14:30:34 +00:00
|
|
|
this.compilation_hints = [];
|
2018-10-10 22:05:40 +00:00
|
|
|
this.element_segments = [];
|
|
|
|
this.data_segments = [];
|
2016-03-07 19:32:35 +00:00
|
|
|
this.explicit = [];
|
2016-10-06 15:43:10 +00:00
|
|
|
this.num_imported_funcs = 0;
|
|
|
|
this.num_imported_globals = 0;
|
2019-02-12 12:03:42 +00:00
|
|
|
this.num_imported_tables = 0;
|
2018-09-11 08:38:24 +00:00
|
|
|
this.num_imported_exceptions = 0;
|
2016-06-22 00:51:58 +00:00
|
|
|
return this;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
addStart(start_index) {
|
2016-03-07 19:32:35 +00:00
|
|
|
this.start_index = start_index;
|
2016-10-13 16:17:44 +00:00
|
|
|
return this;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-07 19:32:35 +00:00
|
|
|
|
2017-09-14 06:14:48 +00:00
|
|
|
addMemory(min, max, exp, shared) {
|
|
|
|
this.memory = {min: min, max: max, exp: exp, shared: shared};
|
2016-03-07 10:01:24 +00:00
|
|
|
return this;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
addExplicitSection(bytes) {
|
|
|
|
this.explicit.push(bytes);
|
|
|
|
return this;
|
|
|
|
}
|
2016-03-07 19:32:35 +00:00
|
|
|
|
2017-01-25 11:53:09 +00:00
|
|
|
stringToBytes(name) {
|
|
|
|
var result = new Binary();
|
|
|
|
result.emit_u32v(name.length);
|
|
|
|
for (var i = 0; i < name.length; i++) {
|
|
|
|
result.emit_u8(name.charCodeAt(i));
|
|
|
|
}
|
2019-03-07 14:06:13 +00:00
|
|
|
return result.trunc_buffer()
|
2017-01-25 11:53:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-18 14:30:34 +00:00
|
|
|
createCustomSection(name, bytes) {
|
2017-01-25 11:53:09 +00:00
|
|
|
name = this.stringToBytes(name);
|
2019-03-07 14:06:13 +00:00
|
|
|
var section = new Binary();
|
|
|
|
section.emit_u8(0);
|
|
|
|
section.emit_u32v(name.length + bytes.length);
|
|
|
|
section.emit_bytes(name);
|
|
|
|
section.emit_bytes(bytes);
|
2019-03-18 14:30:34 +00:00
|
|
|
return section.trunc_buffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
addCustomSection(name, bytes) {
|
|
|
|
this.explicit.push(this.createCustomSection(name, bytes));
|
2017-01-25 11:53:09 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
addType(type) {
|
|
|
|
this.types.push(type);
|
2017-08-03 11:35:15 +00:00
|
|
|
var pl = type.params.length; // should have params
|
|
|
|
var rl = type.results.length; // should have results
|
2016-06-21 19:47:51 +00:00
|
|
|
return this.types.length - 1;
|
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
addGlobal(local_type, mutable) {
|
|
|
|
let glob = new WasmGlobalBuilder(this, local_type, mutable);
|
|
|
|
glob.index = this.globals.length + this.num_imported_globals;
|
|
|
|
this.globals.push(glob);
|
|
|
|
return glob;
|
2016-08-31 00:35:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 12:03:42 +00:00
|
|
|
addTable(type, initial_size, max_size = undefined) {
|
2020-06-10 07:22:22 +00:00
|
|
|
if (type != kWasmExternRef && type != kWasmAnyFunc && type != kWasmExnRef) {
|
2019-10-16 13:34:08 +00:00
|
|
|
throw new Error(
|
2020-06-10 07:22:22 +00:00
|
|
|
'Tables must be of type kWasmExternRef, kWasmAnyFunc or kWasmExnRef');
|
2019-02-12 12:03:42 +00:00
|
|
|
}
|
|
|
|
let table = new WasmTableBuilder(this, type, initial_size, max_size);
|
|
|
|
table.index = this.tables.length + this.num_imported_tables;
|
|
|
|
this.tables.push(table);
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:56:39 +00:00
|
|
|
addException(type) {
|
2018-10-15 14:31:16 +00:00
|
|
|
let type_index = (typeof type) == "number" ? type : this.addType(type);
|
2018-09-11 08:38:24 +00:00
|
|
|
let except_index = this.exceptions.length + this.num_imported_exceptions;
|
2018-10-15 14:31:16 +00:00
|
|
|
this.exceptions.push(type_index);
|
2018-09-11 08:38:24 +00:00
|
|
|
return except_index;
|
2017-08-01 20:56:39 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
addFunction(name, type) {
|
|
|
|
let type_index = (typeof type) == "number" ? type : this.addType(type);
|
2016-10-06 15:43:10 +00:00
|
|
|
let func = new WasmFunctionBuilder(this, name, type_index);
|
|
|
|
func.index = this.functions.length + this.num_imported_funcs;
|
2016-03-07 10:01:24 +00:00
|
|
|
this.functions.push(func);
|
|
|
|
return func;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2018-10-26 11:05:14 +00:00
|
|
|
addImport(module, name, type) {
|
2018-09-11 09:03:27 +00:00
|
|
|
if (this.functions.length != 0) {
|
|
|
|
throw new Error('Imported functions must be declared before local ones');
|
|
|
|
}
|
2016-06-21 19:47:51 +00:00
|
|
|
let type_index = (typeof type) == "number" ? type : this.addType(type);
|
2016-10-06 15:43:10 +00:00
|
|
|
this.imports.push({module: module, name: name, kind: kExternalFunction,
|
|
|
|
type: type_index});
|
|
|
|
return this.num_imported_funcs++;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-08 19:55:36 +00:00
|
|
|
|
2018-10-26 11:05:14 +00:00
|
|
|
addImportedGlobal(module, name, type, mutable = false) {
|
2018-09-11 09:03:27 +00:00
|
|
|
if (this.globals.length != 0) {
|
|
|
|
throw new Error('Imported globals must be declared before local ones');
|
|
|
|
}
|
2016-10-06 15:43:10 +00:00
|
|
|
let o = {module: module, name: name, kind: kExternalGlobal, type: type,
|
2018-04-25 18:12:51 +00:00
|
|
|
mutable: mutable};
|
2016-10-06 15:43:10 +00:00
|
|
|
this.imports.push(o);
|
|
|
|
return this.num_imported_globals++;
|
|
|
|
}
|
|
|
|
|
2018-10-26 11:05:14 +00:00
|
|
|
addImportedMemory(module, name, initial = 0, maximum, shared) {
|
2016-10-25 09:44:15 +00:00
|
|
|
let o = {module: module, name: name, kind: kExternalMemory,
|
2017-09-14 06:14:48 +00:00
|
|
|
initial: initial, maximum: maximum, shared: shared};
|
2016-10-07 09:34:06 +00:00
|
|
|
this.imports.push(o);
|
2016-10-13 16:17:44 +00:00
|
|
|
return this;
|
2016-10-07 09:34:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 14:55:07 +00:00
|
|
|
addImportedTable(module, name, initial, maximum, type) {
|
2019-02-12 12:03:42 +00:00
|
|
|
if (this.tables.length != 0) {
|
|
|
|
throw new Error('Imported tables must be declared before local ones');
|
|
|
|
}
|
2016-10-29 21:06:57 +00:00
|
|
|
let o = {module: module, name: name, kind: kExternalTable, initial: initial,
|
2019-04-30 14:55:07 +00:00
|
|
|
maximum: maximum, type: type || kWasmAnyFunctionTypeForm};
|
2016-10-29 21:06:57 +00:00
|
|
|
this.imports.push(o);
|
2019-02-12 12:03:42 +00:00
|
|
|
return this.num_imported_tables++;
|
2016-10-29 21:06:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:05:14 +00:00
|
|
|
addImportedException(module, name, type) {
|
2018-09-11 08:38:24 +00:00
|
|
|
if (this.exceptions.length != 0) {
|
|
|
|
throw new Error('Imported exceptions must be declared before local ones');
|
|
|
|
}
|
2018-10-15 14:31:16 +00:00
|
|
|
let type_index = (typeof type) == "number" ? type : this.addType(type);
|
|
|
|
let o = {module: module, name: name, kind: kExternalException, type: type_index};
|
2018-09-11 08:38:24 +00:00
|
|
|
this.imports.push(o);
|
|
|
|
return this.num_imported_exceptions++;
|
|
|
|
}
|
|
|
|
|
2016-10-17 11:39:04 +00:00
|
|
|
addExport(name, index) {
|
|
|
|
this.exports.push({name: name, kind: kExternalFunction, index: index});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-10-25 09:44:15 +00:00
|
|
|
addExportOfKind(name, kind, index) {
|
|
|
|
this.exports.push({name: name, kind: kind, index: index});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-04-17 17:55:36 +00:00
|
|
|
setCompilationHint(strategy, baselineTier, topTier, index) {
|
2019-04-03 10:15:58 +00:00
|
|
|
this.compilation_hints[index] = {strategy: strategy, baselineTier:
|
|
|
|
baselineTier, topTier: topTier};
|
2019-03-18 14:30:34 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-10-10 14:41:50 +00:00
|
|
|
addDataSegment(addr, data, is_global = false) {
|
2018-11-12 20:53:47 +00:00
|
|
|
this.data_segments.push(
|
|
|
|
{addr: addr, data: data, is_global: is_global, is_active: true});
|
|
|
|
return this.data_segments.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
addPassiveDataSegment(data) {
|
|
|
|
this.data_segments.push({data: data, is_active: false});
|
2018-10-10 22:05:40 +00:00
|
|
|
return this.data_segments.length - 1;
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2016-10-07 09:34:06 +00:00
|
|
|
exportMemoryAs(name) {
|
|
|
|
this.exports.push({name: name, kind: kExternalMemory, index: 0});
|
|
|
|
}
|
|
|
|
|
2019-05-21 08:01:48 +00:00
|
|
|
addElementSegment(table, base, is_global, array) {
|
2020-02-17 16:21:02 +00:00
|
|
|
this.element_segments.push({
|
|
|
|
table: table,
|
|
|
|
base: base,
|
|
|
|
is_global: is_global,
|
|
|
|
array: array,
|
|
|
|
is_active: true,
|
|
|
|
is_declarative: false
|
|
|
|
});
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-11-12 20:53:47 +00:00
|
|
|
addPassiveElementSegment(array, is_import = false) {
|
2020-02-17 16:21:02 +00:00
|
|
|
this.element_segments.push(
|
|
|
|
{array: array, is_active: false, is_declarative: false});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
addDeclarativeElementSegment(array, is_import = false) {
|
|
|
|
this.element_segments.push(
|
|
|
|
{array: array, is_active: false, is_declarative: true});
|
2018-11-12 20:53:47 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
appendToTable(array) {
|
2017-03-22 13:32:59 +00:00
|
|
|
for (let n of array) {
|
|
|
|
if (typeof n != 'number')
|
|
|
|
throw new Error('invalid table (entries have to be numbers): ' + array);
|
|
|
|
}
|
2019-02-12 12:03:42 +00:00
|
|
|
if (this.tables.length == 0) {
|
|
|
|
this.addTable(kWasmAnyFunc, 0);
|
|
|
|
}
|
2019-05-21 12:22:18 +00:00
|
|
|
// Adjust the table to the correct size.
|
|
|
|
let table = this.tables[0];
|
|
|
|
const base = table.initial_size;
|
|
|
|
const table_size = base + array.length;
|
|
|
|
table.initial_size = table_size;
|
|
|
|
if (table.has_max && table_size > table.max_size) {
|
|
|
|
table.max_size = table_size;
|
|
|
|
}
|
|
|
|
return this.addElementSegment(0, base, false, array);
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-26 12:02:03 +00:00
|
|
|
setTableBounds(min, max = undefined) {
|
2019-02-12 12:03:42 +00:00
|
|
|
if (this.tables.length != 0) {
|
|
|
|
throw new Error("The table bounds of table '0' have already been set.");
|
|
|
|
}
|
|
|
|
this.addTable(kWasmAnyFunc, min, max);
|
2018-10-10 22:05:40 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2017-06-12 09:31:14 +00:00
|
|
|
setName(name) {
|
|
|
|
this.name = name;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-03-12 15:43:37 +00:00
|
|
|
toBuffer(debug = false) {
|
2016-06-21 19:47:51 +00:00
|
|
|
let binary = new Binary;
|
|
|
|
let wasm = this;
|
|
|
|
|
|
|
|
// Add header
|
|
|
|
binary.emit_header();
|
|
|
|
|
|
|
|
// Add type section
|
|
|
|
if (wasm.types.length > 0) {
|
|
|
|
if (debug) print("emitting types @ " + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kTypeSectionCode, section => {
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(wasm.types.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
for (let type of wasm.types) {
|
|
|
|
section.emit_u8(kWasmFunctionTypeForm);
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(type.params.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
for (let param of type.params) {
|
|
|
|
section.emit_u8(param);
|
|
|
|
}
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(type.results.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
for (let result of type.results) {
|
|
|
|
section.emit_u8(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-03-07 10:01:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add imports section
|
2016-03-10 12:36:42 +00:00
|
|
|
if (wasm.imports.length > 0) {
|
2016-06-21 19:47:51 +00:00
|
|
|
if (debug) print("emitting imports @ " + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kImportSectionCode, section => {
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(wasm.imports.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
for (let imp of wasm.imports) {
|
|
|
|
section.emit_string(imp.module);
|
|
|
|
section.emit_string(imp.name || '');
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u8(imp.kind);
|
|
|
|
if (imp.kind == kExternalFunction) {
|
|
|
|
section.emit_u32v(imp.type);
|
|
|
|
} else if (imp.kind == kExternalGlobal) {
|
|
|
|
section.emit_u32v(imp.type);
|
|
|
|
section.emit_u8(imp.mutable);
|
2016-10-07 09:34:06 +00:00
|
|
|
} else if (imp.kind == kExternalMemory) {
|
|
|
|
var has_max = (typeof imp.maximum) != "undefined";
|
2017-09-14 06:14:48 +00:00
|
|
|
var is_shared = (typeof imp.shared) != "undefined";
|
|
|
|
if (is_shared) {
|
|
|
|
section.emit_u8(has_max ? 3 : 2); // flags
|
|
|
|
} else {
|
|
|
|
section.emit_u8(has_max ? 1 : 0); // flags
|
|
|
|
}
|
2016-10-07 09:34:06 +00:00
|
|
|
section.emit_u32v(imp.initial); // initial
|
|
|
|
if (has_max) section.emit_u32v(imp.maximum); // maximum
|
2016-10-29 21:06:57 +00:00
|
|
|
} else if (imp.kind == kExternalTable) {
|
2019-04-30 14:55:07 +00:00
|
|
|
section.emit_u8(imp.type);
|
2016-10-29 21:06:57 +00:00
|
|
|
var has_max = (typeof imp.maximum) != "undefined";
|
|
|
|
section.emit_u8(has_max ? 1 : 0); // flags
|
|
|
|
section.emit_u32v(imp.initial); // initial
|
|
|
|
if (has_max) section.emit_u32v(imp.maximum); // maximum
|
2018-09-11 08:38:24 +00:00
|
|
|
} else if (imp.kind == kExternalException) {
|
2018-10-22 08:39:58 +00:00
|
|
|
section.emit_u32v(kExceptionAttribute);
|
2018-10-15 14:31:16 +00:00
|
|
|
section.emit_u32v(imp.type);
|
2016-10-06 15:43:10 +00:00
|
|
|
} else {
|
|
|
|
throw new Error("unknown/unsupported import kind " + imp.kind);
|
|
|
|
}
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
});
|
2016-03-07 10:01:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 11:18:41 +00:00
|
|
|
// Add functions declarations
|
2016-03-10 12:36:42 +00:00
|
|
|
if (wasm.functions.length > 0) {
|
2016-06-21 19:47:51 +00:00
|
|
|
if (debug) print("emitting function decls @ " + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kFunctionSectionCode, section => {
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(wasm.functions.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
for (let func of wasm.functions) {
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(func.type_index);
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
});
|
2016-04-21 11:18:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 22:05:40 +00:00
|
|
|
// Add table section
|
2019-02-12 12:03:42 +00:00
|
|
|
if (wasm.tables.length > 0) {
|
|
|
|
if (debug) print ("emitting tables @ " + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kTableSectionCode, section => {
|
2019-02-12 12:03:42 +00:00
|
|
|
section.emit_u32v(wasm.tables.length);
|
|
|
|
for (let table of wasm.tables) {
|
|
|
|
section.emit_u8(table.type);
|
|
|
|
section.emit_u8(table.has_max);
|
|
|
|
section.emit_u32v(table.initial_size);
|
|
|
|
if (table.has_max) section.emit_u32v(table.max_size);
|
|
|
|
}
|
2016-06-21 19:47:51 +00:00
|
|
|
});
|
2016-04-21 11:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add memory section
|
2017-04-07 14:37:30 +00:00
|
|
|
if (wasm.memory !== undefined) {
|
2016-06-21 19:47:51 +00:00
|
|
|
if (debug) print("emitting memory @ " + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kMemorySectionCode, section => {
|
|
|
|
section.emit_u8(1); // one memory entry
|
2017-07-26 11:01:21 +00:00
|
|
|
const has_max = wasm.memory.max !== undefined;
|
2017-09-14 06:14:48 +00:00
|
|
|
const is_shared = wasm.memory.shared !== undefined;
|
|
|
|
// Emit flags (bit 0: reszeable max, bit 1: shared memory)
|
|
|
|
if (is_shared) {
|
2019-01-29 17:05:35 +00:00
|
|
|
section.emit_u8(has_max ? kSharedHasMaximumFlag : 2);
|
2017-09-14 06:14:48 +00:00
|
|
|
} else {
|
2019-01-29 17:05:35 +00:00
|
|
|
section.emit_u8(has_max ? kHasMaximumFlag : 0);
|
2017-09-14 06:14:48 +00:00
|
|
|
}
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(wasm.memory.min);
|
2017-07-26 11:01:21 +00:00
|
|
|
if (has_max) section.emit_u32v(wasm.memory.max);
|
2016-06-21 19:47:51 +00:00
|
|
|
});
|
2016-04-21 11:18:41 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 17:30:44 +00:00
|
|
|
// Add event section.
|
|
|
|
if (wasm.exceptions.length > 0) {
|
|
|
|
if (debug) print("emitting events @ " + binary.length);
|
|
|
|
binary.emit_section(kExceptionSectionCode, section => {
|
|
|
|
section.emit_u32v(wasm.exceptions.length);
|
|
|
|
for (let type of wasm.exceptions) {
|
|
|
|
section.emit_u32v(kExceptionAttribute);
|
|
|
|
section.emit_u32v(type);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
// Add global section.
|
|
|
|
if (wasm.globals.length > 0) {
|
|
|
|
if (debug) print ("emitting globals @ " + binary.length);
|
|
|
|
binary.emit_section(kGlobalSectionCode, section => {
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(wasm.globals.length);
|
|
|
|
for (let global of wasm.globals) {
|
|
|
|
section.emit_u8(global.type);
|
|
|
|
section.emit_u8(global.mutable);
|
|
|
|
if ((typeof global.init_index) == "undefined") {
|
|
|
|
// Emit a constant initializer.
|
|
|
|
switch (global.type) {
|
2016-12-21 13:43:00 +00:00
|
|
|
case kWasmI32:
|
2016-09-27 20:46:10 +00:00
|
|
|
section.emit_u8(kExprI32Const);
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(global.init);
|
2016-09-27 20:46:10 +00:00
|
|
|
break;
|
2016-12-21 13:43:00 +00:00
|
|
|
case kWasmI64:
|
2016-09-27 20:46:10 +00:00
|
|
|
section.emit_u8(kExprI64Const);
|
2019-01-29 17:05:35 +00:00
|
|
|
section.emit_u64v(global.init);
|
2016-09-27 20:46:10 +00:00
|
|
|
break;
|
2016-12-21 13:43:00 +00:00
|
|
|
case kWasmF32:
|
2020-06-22 12:54:04 +00:00
|
|
|
section.emit_bytes(wasmF32Const(global.init));
|
2016-09-27 20:46:10 +00:00
|
|
|
break;
|
2016-12-21 13:43:00 +00:00
|
|
|
case kWasmF64:
|
2020-06-22 12:54:04 +00:00
|
|
|
section.emit_bytes(wasmF64Const(global.init));
|
2016-09-27 20:46:10 +00:00
|
|
|
break;
|
2020-07-24 04:12:09 +00:00
|
|
|
case kWasmS128:
|
|
|
|
section.emit_bytes(wasmS128Const(global.init));
|
|
|
|
break;
|
2020-06-09 15:54:14 +00:00
|
|
|
case kWasmExternRef:
|
2020-06-03 12:48:16 +00:00
|
|
|
section.emit_u8(kExprRefNull);
|
2020-06-09 15:54:14 +00:00
|
|
|
section.emit_u8(kWasmExternRef);
|
2020-06-03 12:48:16 +00:00
|
|
|
assertEquals(global.function_index, undefined);
|
|
|
|
break;
|
|
|
|
case kWasmAnyFunc:
|
2019-05-13 11:35:43 +00:00
|
|
|
if (global.function_index !== undefined) {
|
|
|
|
section.emit_u8(kExprRefFunc);
|
|
|
|
section.emit_u32v(global.function_index);
|
|
|
|
} else {
|
|
|
|
section.emit_u8(kExprRefNull);
|
2020-06-03 12:48:16 +00:00
|
|
|
section.emit_u8(kWasmAnyFunc);
|
2019-05-13 11:35:43 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-07-15 08:24:37 +00:00
|
|
|
case kWasmExnRef:
|
2018-12-18 12:07:27 +00:00
|
|
|
section.emit_u8(kExprRefNull);
|
2020-06-03 12:48:16 +00:00
|
|
|
section.emit_u8(kWasmExnRef);
|
2018-12-18 12:07:27 +00:00
|
|
|
break;
|
2016-10-06 15:43:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Emit a global-index initializer.
|
2019-10-08 13:16:30 +00:00
|
|
|
section.emit_u8(kExprGlobalGet);
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(global.init_index);
|
2016-09-27 20:46:10 +00:00
|
|
|
}
|
|
|
|
section.emit_u8(kExprEnd); // end of init expression
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-04-21 11:18:41 +00:00
|
|
|
|
|
|
|
// Add export table.
|
2017-04-07 14:37:30 +00:00
|
|
|
var mem_export = (wasm.memory !== undefined && wasm.memory.exp);
|
2016-10-06 15:43:10 +00:00
|
|
|
var exports_count = wasm.exports.length + (mem_export ? 1 : 0);
|
|
|
|
if (exports_count > 0) {
|
2016-06-21 19:47:51 +00:00
|
|
|
if (debug) print("emitting exports @ " + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kExportSectionCode, section => {
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(exports_count);
|
|
|
|
for (let exp of wasm.exports) {
|
|
|
|
section.emit_string(exp.name);
|
|
|
|
section.emit_u8(exp.kind);
|
|
|
|
section.emit_u32v(exp.index);
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-09-27 20:46:10 +00:00
|
|
|
if (mem_export) {
|
|
|
|
section.emit_string("memory");
|
|
|
|
section.emit_u8(kExternalMemory);
|
|
|
|
section.emit_u8(0);
|
|
|
|
}
|
2016-06-21 19:47:51 +00:00
|
|
|
});
|
2016-04-21 11:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add start function section.
|
2017-04-07 14:37:30 +00:00
|
|
|
if (wasm.start_index !== undefined) {
|
2016-06-21 19:47:51 +00:00
|
|
|
if (debug) print("emitting start function @ " + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kStartSectionCode, section => {
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(wasm.start_index);
|
2016-06-21 19:47:51 +00:00
|
|
|
});
|
2016-04-21 11:18:41 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 22:05:40 +00:00
|
|
|
// Add element segments
|
|
|
|
if (wasm.element_segments.length > 0) {
|
|
|
|
if (debug) print("emitting element segments @ " + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kElementSectionCode, section => {
|
2018-10-10 22:05:40 +00:00
|
|
|
var inits = wasm.element_segments;
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
section.emit_u32v(inits.length);
|
|
|
|
|
|
|
|
for (let init of inits) {
|
2018-11-12 20:53:47 +00:00
|
|
|
if (init.is_active) {
|
2019-03-04 18:52:03 +00:00
|
|
|
// Active segment.
|
2019-04-09 22:42:14 +00:00
|
|
|
if (init.table == 0) {
|
|
|
|
section.emit_u32v(kActiveNoIndex);
|
|
|
|
} else {
|
|
|
|
section.emit_u32v(kActiveWithIndex);
|
|
|
|
section.emit_u32v(init.table);
|
|
|
|
}
|
2018-11-12 20:53:47 +00:00
|
|
|
if (init.is_global) {
|
2019-10-08 13:16:30 +00:00
|
|
|
section.emit_u8(kExprGlobalGet);
|
2018-11-12 20:53:47 +00:00
|
|
|
} else {
|
|
|
|
section.emit_u8(kExprI32Const);
|
|
|
|
}
|
|
|
|
section.emit_u32v(init.base);
|
|
|
|
section.emit_u8(kExprEnd);
|
2019-09-16 15:40:20 +00:00
|
|
|
if (init.table != 0) {
|
|
|
|
section.emit_u8(kExternalFunction);
|
|
|
|
}
|
2019-03-04 18:52:03 +00:00
|
|
|
section.emit_u32v(init.array.length);
|
|
|
|
for (let index of init.array) {
|
|
|
|
section.emit_u32v(index);
|
|
|
|
}
|
2020-02-17 16:21:02 +00:00
|
|
|
} else if (
|
|
|
|
init.is_declarative &&
|
|
|
|
init.array.every(index => index !== null)) {
|
|
|
|
section.emit_u8(kDeclarative);
|
|
|
|
section.emit_u8(kExternalFunction);
|
|
|
|
section.emit_u32v(init.array.length);
|
|
|
|
for (let index of init.array) {
|
|
|
|
section.emit_u32v(index);
|
|
|
|
}
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
} else {
|
2020-02-17 16:21:02 +00:00
|
|
|
// Passive or declarative segment with elements.
|
|
|
|
section.emit_u8(
|
|
|
|
init.is_declarative ? kDeclarativeWithElements :
|
|
|
|
kPassiveWithElements); // flags
|
2019-03-04 18:52:03 +00:00
|
|
|
section.emit_u8(kWasmAnyFunc);
|
|
|
|
section.emit_u32v(init.array.length);
|
|
|
|
for (let index of init.array) {
|
|
|
|
if (index === null) {
|
|
|
|
section.emit_u8(kExprRefNull);
|
2020-06-03 12:48:16 +00:00
|
|
|
section.emit_u8(kWasmAnyFunc);
|
2019-03-04 18:52:03 +00:00
|
|
|
section.emit_u8(kExprEnd);
|
|
|
|
} else {
|
|
|
|
section.emit_u8(kExprRefFunc);
|
|
|
|
section.emit_u32v(index);
|
|
|
|
section.emit_u8(kExprEnd);
|
|
|
|
}
|
|
|
|
}
|
[wasm] Use a Managed<WasmModule> to hold metadata about modules.
This CL refactors the handling of metadata associated with WebAssembly
modules to reduce the duplicate marshalling of data from the C++ world
to the JavaScript world. It does this by wrapping the C++ WasmModule*
object in a Foreign that is rooted from the on-heap WasmCompiledModule
(which is itself just a FixedArray). Upon serialization, the C++ object
is ignored and the original WASM wire bytes are serialized. Upon
deserialization, the C++ object is reconstituted by reparsing the bytes.
This is motivated by increasing complications in implementing the JS
API, in particular WebAssembly.Table, which must perform signature
canonicalization across instances.
Additionally, this CL implements the proper base + offset initialization
behavior for tables.
R=rossberg@chromium.org,bradnelson@chromium.org,mtrofin@chromium.org,yangguo@chromium.org
BUG=v8:5507, chromium:575167, chromium:657316
Review-Url: https://chromiumcodereview.appspot.com/2424623002
Cr-Commit-Position: refs/heads/master@{#40434}
2016-10-19 13:06:44 +00:00
|
|
|
}
|
2016-09-27 20:46:10 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-12 02:18:54 +00:00
|
|
|
// If there are any passive data segments, add the DataCount section.
|
|
|
|
if (wasm.data_segments.some(seg => !seg.is_active)) {
|
|
|
|
binary.emit_section(kDataCountSectionCode, section => {
|
|
|
|
section.emit_u32v(wasm.data_segments.length);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-18 14:30:34 +00:00
|
|
|
// If there are compilation hints add a custom section 'compilationHints'
|
|
|
|
// after the function section and before the code section.
|
|
|
|
if (wasm.compilation_hints.length > 0) {
|
|
|
|
if (debug) print("emitting compilation hints @ " + binary.length);
|
|
|
|
// Build custom section payload.
|
|
|
|
let payloadBinary = new Binary();
|
|
|
|
let implicit_compilation_hints_count = wasm.functions.length;
|
|
|
|
payloadBinary.emit_u32v(implicit_compilation_hints_count);
|
|
|
|
|
|
|
|
// Defaults to the compiler's choice if no better hint was given (0x00).
|
|
|
|
let defaultHintByte = kCompilationHintStrategyDefault |
|
|
|
|
(kCompilationHintTierDefault << 2) |
|
|
|
|
(kCompilationHintTierDefault << 4);
|
|
|
|
|
|
|
|
// Emit hint byte for every function defined in this module.
|
|
|
|
for (let i = 0; i < implicit_compilation_hints_count; i++) {
|
|
|
|
let index = wasm.num_imported_funcs + i;
|
|
|
|
var hintByte;
|
|
|
|
if(index in wasm.compilation_hints) {
|
|
|
|
let hint = wasm.compilation_hints[index];
|
2019-04-03 10:15:58 +00:00
|
|
|
hintByte = hint.strategy | (hint.baselineTier << 2) |
|
|
|
|
(hint.topTier << 4);
|
2019-03-18 14:30:34 +00:00
|
|
|
} else{
|
|
|
|
hintByte = defaultHintByte;
|
|
|
|
}
|
|
|
|
payloadBinary.emit_u8(hintByte);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize as custom section.
|
|
|
|
let name = "compilationHints";
|
|
|
|
let bytes = this.createCustomSection(name, payloadBinary.trunc_buffer());
|
|
|
|
binary.emit_bytes(bytes);
|
|
|
|
}
|
|
|
|
|
2016-04-21 11:18:41 +00:00
|
|
|
// Add function bodies.
|
|
|
|
if (wasm.functions.length > 0) {
|
2016-06-21 19:47:51 +00:00
|
|
|
// emit function bodies
|
|
|
|
if (debug) print("emitting code @ " + binary.length);
|
2020-01-09 12:11:55 +00:00
|
|
|
let section_length = 0;
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kCodeSectionCode, section => {
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(wasm.functions.length);
|
2019-03-08 12:12:05 +00:00
|
|
|
let header = new Binary;
|
2016-06-21 19:47:51 +00:00
|
|
|
for (let func of wasm.functions) {
|
2019-03-08 12:12:05 +00:00
|
|
|
header.reset();
|
2016-06-21 19:47:51 +00:00
|
|
|
// Function body length will be patched later.
|
|
|
|
let local_decls = [];
|
2018-01-08 11:54:54 +00:00
|
|
|
for (let l of func.locals || []) {
|
2016-06-21 19:47:51 +00:00
|
|
|
if (l.i32_count > 0) {
|
2016-12-21 13:43:00 +00:00
|
|
|
local_decls.push({count: l.i32_count, type: kWasmI32});
|
2016-03-07 10:01:24 +00:00
|
|
|
}
|
2016-06-21 19:47:51 +00:00
|
|
|
if (l.i64_count > 0) {
|
2016-12-21 13:43:00 +00:00
|
|
|
local_decls.push({count: l.i64_count, type: kWasmI64});
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
if (l.f32_count > 0) {
|
2016-12-21 13:43:00 +00:00
|
|
|
local_decls.push({count: l.f32_count, type: kWasmF32});
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
if (l.f64_count > 0) {
|
2016-12-21 13:43:00 +00:00
|
|
|
local_decls.push({count: l.f64_count, type: kWasmF64});
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2017-09-11 12:34:35 +00:00
|
|
|
if (l.s128_count > 0) {
|
|
|
|
local_decls.push({count: l.s128_count, type: kWasmS128});
|
|
|
|
}
|
2020-06-09 15:54:14 +00:00
|
|
|
if (l.externref_count > 0) {
|
|
|
|
local_decls.push({count: l.externref_count, type: kWasmExternRef});
|
2018-09-03 10:39:55 +00:00
|
|
|
}
|
2019-01-22 10:29:30 +00:00
|
|
|
if (l.anyfunc_count > 0) {
|
|
|
|
local_decls.push({count: l.anyfunc_count, type: kWasmAnyFunc});
|
|
|
|
}
|
2018-08-30 12:46:48 +00:00
|
|
|
if (l.except_count > 0) {
|
2019-07-15 08:24:37 +00:00
|
|
|
local_decls.push({count: l.except_count, type: kWasmExnRef});
|
2018-08-30 12:46:48 +00:00
|
|
|
}
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
header.emit_u32v(local_decls.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
for (let decl of local_decls) {
|
2016-10-06 15:43:10 +00:00
|
|
|
header.emit_u32v(decl.count);
|
2016-06-21 19:47:51 +00:00
|
|
|
header.emit_u8(decl.type);
|
|
|
|
}
|
|
|
|
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(header.length + func.body.length);
|
2019-03-07 14:06:13 +00:00
|
|
|
section.emit_bytes(header.trunc_buffer());
|
2020-01-09 12:11:55 +00:00
|
|
|
// Set to section offset for now, will update.
|
|
|
|
func.body_offset = section.length;
|
2016-06-21 19:47:51 +00:00
|
|
|
section.emit_bytes(func.body);
|
|
|
|
}
|
2020-01-09 12:11:55 +00:00
|
|
|
section_length = section.length;
|
2016-06-21 19:47:51 +00:00
|
|
|
});
|
2020-01-09 12:11:55 +00:00
|
|
|
for (let func of wasm.functions) {
|
|
|
|
func.body_offset += binary.length - section_length;
|
|
|
|
}
|
2016-03-09 22:50:36 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2016-04-21 11:18:41 +00:00
|
|
|
// Add data segments.
|
2018-10-10 22:05:40 +00:00
|
|
|
if (wasm.data_segments.length > 0) {
|
2016-06-21 19:47:51 +00:00
|
|
|
if (debug) print("emitting data segments @ " + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kDataSectionCode, section => {
|
2018-10-10 22:05:40 +00:00
|
|
|
section.emit_u32v(wasm.data_segments.length);
|
|
|
|
for (let seg of wasm.data_segments) {
|
2018-11-12 20:53:47 +00:00
|
|
|
if (seg.is_active) {
|
|
|
|
section.emit_u8(0); // linear memory index 0 / flags
|
|
|
|
if (seg.is_global) {
|
|
|
|
// initializer is a global variable
|
2019-10-08 13:16:30 +00:00
|
|
|
section.emit_u8(kExprGlobalGet);
|
2018-11-12 20:53:47 +00:00
|
|
|
section.emit_u32v(seg.addr);
|
|
|
|
} else {
|
|
|
|
// initializer is a constant
|
|
|
|
section.emit_u8(kExprI32Const);
|
|
|
|
section.emit_u32v(seg.addr);
|
|
|
|
}
|
|
|
|
section.emit_u8(kExprEnd);
|
2016-10-10 14:41:50 +00:00
|
|
|
} else {
|
2018-11-12 20:53:47 +00:00
|
|
|
section.emit_u8(kPassive); // flags
|
2016-10-10 14:41:50 +00:00
|
|
|
}
|
2016-10-06 15:43:10 +00:00
|
|
|
section.emit_u32v(seg.data.length);
|
2016-06-21 19:47:51 +00:00
|
|
|
section.emit_bytes(seg.data);
|
|
|
|
}
|
|
|
|
});
|
2016-03-07 10:01:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 11:18:41 +00:00
|
|
|
// Add any explicitly added sections
|
2016-06-21 19:47:51 +00:00
|
|
|
for (let exp of wasm.explicit) {
|
|
|
|
if (debug) print("emitting explicit @ " + binary.length);
|
|
|
|
binary.emit_bytes(exp);
|
2016-03-07 19:32:35 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 09:31:14 +00:00
|
|
|
// Add names.
|
2017-07-02 19:18:57 +00:00
|
|
|
let num_function_names = 0;
|
|
|
|
let num_functions_with_local_names = 0;
|
|
|
|
for (let func of wasm.functions) {
|
|
|
|
if (func.name !== undefined) ++num_function_names;
|
|
|
|
if (func.numLocalNames() > 0) ++num_functions_with_local_names;
|
|
|
|
}
|
|
|
|
if (num_function_names > 0 || num_functions_with_local_names > 0 ||
|
|
|
|
wasm.name !== undefined) {
|
2017-04-07 14:37:30 +00:00
|
|
|
if (debug) print('emitting names @ ' + binary.length);
|
2016-09-27 20:46:10 +00:00
|
|
|
binary.emit_section(kUnknownSectionCode, section => {
|
2017-04-07 14:37:30 +00:00
|
|
|
section.emit_string('name');
|
2017-07-02 19:18:57 +00:00
|
|
|
// Emit module name.
|
2017-06-12 09:31:14 +00:00
|
|
|
if (wasm.name !== undefined) {
|
|
|
|
section.emit_section(kModuleNameCode, name_section => {
|
|
|
|
name_section.emit_string(wasm.name);
|
|
|
|
});
|
|
|
|
}
|
2017-07-02 19:18:57 +00:00
|
|
|
// Emit function names.
|
2017-06-12 09:31:14 +00:00
|
|
|
if (num_function_names > 0) {
|
|
|
|
section.emit_section(kFunctionNamesCode, name_section => {
|
|
|
|
name_section.emit_u32v(num_function_names);
|
|
|
|
for (let func of wasm.functions) {
|
|
|
|
if (func.name === undefined) continue;
|
|
|
|
name_section.emit_u32v(func.index);
|
|
|
|
name_section.emit_string(func.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-07-02 19:18:57 +00:00
|
|
|
// Emit local names.
|
|
|
|
if (num_functions_with_local_names > 0) {
|
|
|
|
section.emit_section(kLocalNamesCode, name_section => {
|
|
|
|
name_section.emit_u32v(num_functions_with_local_names);
|
|
|
|
for (let func of wasm.functions) {
|
|
|
|
if (func.numLocalNames() == 0) continue;
|
|
|
|
name_section.emit_u32v(func.index);
|
|
|
|
name_section.emit_u32v(func.numLocalNames());
|
|
|
|
for (let i = 0; i < func.local_names.length; ++i) {
|
|
|
|
if (func.local_names[i] === undefined) continue;
|
|
|
|
name_section.emit_u32v(i);
|
|
|
|
name_section.emit_string(func.local_names[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-06-21 19:47:51 +00:00
|
|
|
});
|
2016-04-21 11:18:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 14:06:13 +00:00
|
|
|
return binary.trunc_buffer();
|
2016-06-21 19:47:51 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2019-03-07 14:06:13 +00:00
|
|
|
toArray(debug = false) {
|
2019-03-12 15:43:37 +00:00
|
|
|
return Array.from(this.toBuffer(debug));
|
2019-01-29 17:05:35 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 16:48:09 +00:00
|
|
|
instantiate(ffi) {
|
2019-03-08 09:48:22 +00:00
|
|
|
let module = this.toModule();
|
2017-01-12 16:48:09 +00:00
|
|
|
let instance = new WebAssembly.Instance(module, ffi);
|
2016-06-21 19:47:51 +00:00
|
|
|
return instance;
|
|
|
|
}
|
2017-07-07 13:52:21 +00:00
|
|
|
|
2018-03-22 10:40:10 +00:00
|
|
|
asyncInstantiate(ffi) {
|
|
|
|
return WebAssembly.instantiate(this.toBuffer(), ffi)
|
|
|
|
.then(({module, instance}) => instance);
|
|
|
|
}
|
|
|
|
|
2017-07-07 13:52:21 +00:00
|
|
|
toModule(debug = false) {
|
2017-07-24 19:21:28 +00:00
|
|
|
return new WebAssembly.Module(this.toBuffer(debug));
|
2017-07-07 13:52:21 +00:00
|
|
|
}
|
2016-03-07 10:01:24 +00:00
|
|
|
}
|
2019-01-29 17:05:35 +00:00
|
|
|
|
2019-07-30 11:19:40 +00:00
|
|
|
function wasmSignedLeb(val, max_len = 5) {
|
|
|
|
let res = [];
|
|
|
|
for (let i = 0; i < max_len; ++i) {
|
|
|
|
let v = val & 0x7f;
|
|
|
|
// If {v} sign-extended from 7 to 32 bits is equal to val, we are done.
|
|
|
|
if (((v << 25) >> 25) == val) {
|
|
|
|
res.push(v);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
res.push(v | 0x80);
|
|
|
|
val = val >> 7;
|
2019-01-29 17:05:35 +00:00
|
|
|
}
|
2019-07-30 11:19:40 +00:00
|
|
|
throw new Error(
|
|
|
|
'Leb value <' + val + '> exceeds maximum length of ' + max_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
function wasmI32Const(val) {
|
|
|
|
return [kExprI32Const, ...wasmSignedLeb(val, 5)];
|
2019-01-29 17:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function wasmF32Const(f) {
|
2020-01-10 14:18:07 +00:00
|
|
|
// Write in little-endian order at offset 0.
|
|
|
|
data_view.setFloat32(0, f, true);
|
2019-03-08 11:23:18 +00:00
|
|
|
return [
|
2020-01-10 14:18:07 +00:00
|
|
|
kExprF32Const, byte_view[0], byte_view[1], byte_view[2], byte_view[3]
|
2019-03-08 11:23:18 +00:00
|
|
|
];
|
2019-01-29 17:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function wasmF64Const(f) {
|
2020-01-10 14:18:07 +00:00
|
|
|
// Write in little-endian order at offset 0.
|
|
|
|
data_view.setFloat64(0, f, true);
|
2019-03-08 11:23:18 +00:00
|
|
|
return [
|
2020-01-10 14:18:07 +00:00
|
|
|
kExprF64Const, byte_view[0], byte_view[1], byte_view[2],
|
|
|
|
byte_view[3], byte_view[4], byte_view[5], byte_view[6], byte_view[7]
|
2019-03-08 11:23:18 +00:00
|
|
|
];
|
2019-01-29 17:05:35 +00:00
|
|
|
}
|
2020-07-24 04:12:09 +00:00
|
|
|
|
|
|
|
function wasmS128Const(f) {
|
|
|
|
// Write in little-endian order at offset 0.
|
|
|
|
return [kSimdPrefix, kExprS128Const, ...f];
|
|
|
|
}
|