11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci// Used for encoding f32 and double constants to bits. 61cb0ef41Sopenharmony_cilet byte_view = new Uint8Array(8); 71cb0ef41Sopenharmony_cilet data_view = new DataView(byte_view.buffer); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci// The bytes function receives one of 101cb0ef41Sopenharmony_ci// - several arguments, each of which is either a number or a string of length 111cb0ef41Sopenharmony_ci// 1; if it's a string, the charcode of the contained character is used. 121cb0ef41Sopenharmony_ci// - a single array argument containing the actual arguments 131cb0ef41Sopenharmony_ci// - a single string; the returned buffer will contain the char codes of all 141cb0ef41Sopenharmony_ci// contained characters. 151cb0ef41Sopenharmony_cifunction bytes(...input) { 161cb0ef41Sopenharmony_ci if (input.length == 1 && typeof input[0] == 'array') input = input[0]; 171cb0ef41Sopenharmony_ci if (input.length == 1 && typeof input[0] == 'string') { 181cb0ef41Sopenharmony_ci let len = input[0].length; 191cb0ef41Sopenharmony_ci let view = new Uint8Array(len); 201cb0ef41Sopenharmony_ci for (let i = 0; i < len; i++) view[i] = input[0].charCodeAt(i); 211cb0ef41Sopenharmony_ci return view.buffer; 221cb0ef41Sopenharmony_ci } 231cb0ef41Sopenharmony_ci let view = new Uint8Array(input.length); 241cb0ef41Sopenharmony_ci for (let i = 0; i < input.length; i++) { 251cb0ef41Sopenharmony_ci let val = input[i]; 261cb0ef41Sopenharmony_ci if (typeof val == 'string') { 271cb0ef41Sopenharmony_ci assertEquals(1, val.length, 'string inputs must have length 1'); 281cb0ef41Sopenharmony_ci val = val.charCodeAt(0); 291cb0ef41Sopenharmony_ci } 301cb0ef41Sopenharmony_ci view[i] = val | 0; 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci return view.buffer; 331cb0ef41Sopenharmony_ci} 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci// Header declaration constants 361cb0ef41Sopenharmony_civar kWasmH0 = 0; 371cb0ef41Sopenharmony_civar kWasmH1 = 0x61; 381cb0ef41Sopenharmony_civar kWasmH2 = 0x73; 391cb0ef41Sopenharmony_civar kWasmH3 = 0x6d; 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_civar kWasmV0 = 0x1; 421cb0ef41Sopenharmony_civar kWasmV1 = 0; 431cb0ef41Sopenharmony_civar kWasmV2 = 0; 441cb0ef41Sopenharmony_civar kWasmV3 = 0; 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_civar kHeaderSize = 8; 471cb0ef41Sopenharmony_civar kPageSize = 65536; 481cb0ef41Sopenharmony_civar kSpecMaxPages = 65535; 491cb0ef41Sopenharmony_civar kMaxVarInt32Size = 5; 501cb0ef41Sopenharmony_civar kMaxVarInt64Size = 10; 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_cilet kDeclNoLocals = 0; 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci// Section declaration constants 551cb0ef41Sopenharmony_cilet kUnknownSectionCode = 0; 561cb0ef41Sopenharmony_cilet kTypeSectionCode = 1; // Function signature declarations 571cb0ef41Sopenharmony_cilet kImportSectionCode = 2; // Import declarations 581cb0ef41Sopenharmony_cilet kFunctionSectionCode = 3; // Function declarations 591cb0ef41Sopenharmony_cilet kTableSectionCode = 4; // Indirect function table and other tables 601cb0ef41Sopenharmony_cilet kMemorySectionCode = 5; // Memory attributes 611cb0ef41Sopenharmony_cilet kGlobalSectionCode = 6; // Global declarations 621cb0ef41Sopenharmony_cilet kExportSectionCode = 7; // Exports 631cb0ef41Sopenharmony_cilet kStartSectionCode = 8; // Start function declaration 641cb0ef41Sopenharmony_cilet kElementSectionCode = 9; // Elements section 651cb0ef41Sopenharmony_cilet kCodeSectionCode = 10; // Function code 661cb0ef41Sopenharmony_cilet kDataSectionCode = 11; // Data segments 671cb0ef41Sopenharmony_cilet kDataCountSectionCode = 12; // Data segment count (between Element & Code) 681cb0ef41Sopenharmony_cilet kTagSectionCode = 13; // Tag section (between Memory & Global) 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci// Name section types 711cb0ef41Sopenharmony_cilet kModuleNameCode = 0; 721cb0ef41Sopenharmony_cilet kFunctionNamesCode = 1; 731cb0ef41Sopenharmony_cilet kLocalNamesCode = 2; 741cb0ef41Sopenharmony_ci 751cb0ef41Sopenharmony_cilet kWasmFunctionTypeForm = 0x60; 761cb0ef41Sopenharmony_cilet kWasmAnyFunctionTypeForm = 0x70; 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_cilet kHasMaximumFlag = 1; 791cb0ef41Sopenharmony_cilet kSharedHasMaximumFlag = 3; 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci// Segment flags 821cb0ef41Sopenharmony_cilet kActiveNoIndex = 0; 831cb0ef41Sopenharmony_cilet kPassive = 1; 841cb0ef41Sopenharmony_cilet kActiveWithIndex = 2; 851cb0ef41Sopenharmony_cilet kPassiveWithElements = 5; 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci// Function declaration flags 881cb0ef41Sopenharmony_cilet kDeclFunctionName = 0x01; 891cb0ef41Sopenharmony_cilet kDeclFunctionImport = 0x02; 901cb0ef41Sopenharmony_cilet kDeclFunctionLocals = 0x04; 911cb0ef41Sopenharmony_cilet kDeclFunctionExport = 0x08; 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci// Local types 941cb0ef41Sopenharmony_cilet kWasmStmt = 0x40; 951cb0ef41Sopenharmony_cilet kWasmI32 = 0x7f; 961cb0ef41Sopenharmony_cilet kWasmI64 = 0x7e; 971cb0ef41Sopenharmony_cilet kWasmF32 = 0x7d; 981cb0ef41Sopenharmony_cilet kWasmF64 = 0x7c; 991cb0ef41Sopenharmony_cilet kWasmS128 = 0x7b; 1001cb0ef41Sopenharmony_cilet kWasmAnyRef = 0x6f; 1011cb0ef41Sopenharmony_cilet kWasmAnyFunc = 0x70; 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_cilet kExternalFunction = 0; 1041cb0ef41Sopenharmony_cilet kExternalTable = 1; 1051cb0ef41Sopenharmony_cilet kExternalMemory = 2; 1061cb0ef41Sopenharmony_cilet kExternalGlobal = 3; 1071cb0ef41Sopenharmony_cilet kExternalTag = 4; 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_cilet kTableZero = 0; 1101cb0ef41Sopenharmony_cilet kMemoryZero = 0; 1111cb0ef41Sopenharmony_cilet kSegmentZero = 0; 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_cilet kTagAttribute = 0; 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ci// Useful signatures 1161cb0ef41Sopenharmony_cilet kSig_i_i = makeSig([kWasmI32], [kWasmI32]); 1171cb0ef41Sopenharmony_cilet kSig_l_l = makeSig([kWasmI64], [kWasmI64]); 1181cb0ef41Sopenharmony_cilet kSig_i_l = makeSig([kWasmI64], [kWasmI32]); 1191cb0ef41Sopenharmony_cilet kSig_i_ii = makeSig([kWasmI32, kWasmI32], [kWasmI32]); 1201cb0ef41Sopenharmony_cilet kSig_i_iii = makeSig([kWasmI32, kWasmI32, kWasmI32], [kWasmI32]); 1211cb0ef41Sopenharmony_cilet kSig_v_iiii = makeSig([kWasmI32, kWasmI32, kWasmI32, kWasmI32], []); 1221cb0ef41Sopenharmony_cilet kSig_f_ff = makeSig([kWasmF32, kWasmF32], [kWasmF32]); 1231cb0ef41Sopenharmony_cilet kSig_d_dd = makeSig([kWasmF64, kWasmF64], [kWasmF64]); 1241cb0ef41Sopenharmony_cilet kSig_l_ll = makeSig([kWasmI64, kWasmI64], [kWasmI64]); 1251cb0ef41Sopenharmony_cilet kSig_i_dd = makeSig([kWasmF64, kWasmF64], [kWasmI32]); 1261cb0ef41Sopenharmony_cilet kSig_v_v = makeSig([], []); 1271cb0ef41Sopenharmony_cilet kSig_i_v = makeSig([], [kWasmI32]); 1281cb0ef41Sopenharmony_cilet kSig_l_v = makeSig([], [kWasmI64]); 1291cb0ef41Sopenharmony_cilet kSig_f_v = makeSig([], [kWasmF32]); 1301cb0ef41Sopenharmony_cilet kSig_d_v = makeSig([], [kWasmF64]); 1311cb0ef41Sopenharmony_cilet kSig_v_i = makeSig([kWasmI32], []); 1321cb0ef41Sopenharmony_cilet kSig_v_ii = makeSig([kWasmI32, kWasmI32], []); 1331cb0ef41Sopenharmony_cilet kSig_v_iii = makeSig([kWasmI32, kWasmI32, kWasmI32], []); 1341cb0ef41Sopenharmony_cilet kSig_v_l = makeSig([kWasmI64], []); 1351cb0ef41Sopenharmony_cilet kSig_v_d = makeSig([kWasmF64], []); 1361cb0ef41Sopenharmony_cilet kSig_v_dd = makeSig([kWasmF64, kWasmF64], []); 1371cb0ef41Sopenharmony_cilet kSig_v_ddi = makeSig([kWasmF64, kWasmF64, kWasmI32], []); 1381cb0ef41Sopenharmony_cilet kSig_ii_v = makeSig([], [kWasmI32, kWasmI32]); 1391cb0ef41Sopenharmony_cilet kSig_iii_v = makeSig([], [kWasmI32, kWasmI32, kWasmI32]); 1401cb0ef41Sopenharmony_cilet kSig_ii_i = makeSig([kWasmI32], [kWasmI32, kWasmI32]); 1411cb0ef41Sopenharmony_cilet kSig_iii_i = makeSig([kWasmI32], [kWasmI32, kWasmI32, kWasmI32]); 1421cb0ef41Sopenharmony_cilet kSig_ii_ii = makeSig([kWasmI32, kWasmI32], [kWasmI32, kWasmI32]); 1431cb0ef41Sopenharmony_cilet kSig_iii_ii = makeSig([kWasmI32, kWasmI32], [kWasmI32, kWasmI32, kWasmI32]); 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_cilet kSig_v_f = makeSig([kWasmF32], []); 1461cb0ef41Sopenharmony_cilet kSig_f_f = makeSig([kWasmF32], [kWasmF32]); 1471cb0ef41Sopenharmony_cilet kSig_f_d = makeSig([kWasmF64], [kWasmF32]); 1481cb0ef41Sopenharmony_cilet kSig_d_d = makeSig([kWasmF64], [kWasmF64]); 1491cb0ef41Sopenharmony_cilet kSig_r_r = makeSig([kWasmAnyRef], [kWasmAnyRef]); 1501cb0ef41Sopenharmony_cilet kSig_a_a = makeSig([kWasmAnyFunc], [kWasmAnyFunc]); 1511cb0ef41Sopenharmony_cilet kSig_i_r = makeSig([kWasmAnyRef], [kWasmI32]); 1521cb0ef41Sopenharmony_cilet kSig_v_r = makeSig([kWasmAnyRef], []); 1531cb0ef41Sopenharmony_cilet kSig_v_a = makeSig([kWasmAnyFunc], []); 1541cb0ef41Sopenharmony_cilet kSig_v_rr = makeSig([kWasmAnyRef, kWasmAnyRef], []); 1551cb0ef41Sopenharmony_cilet kSig_v_aa = makeSig([kWasmAnyFunc, kWasmAnyFunc], []); 1561cb0ef41Sopenharmony_cilet kSig_r_v = makeSig([], [kWasmAnyRef]); 1571cb0ef41Sopenharmony_cilet kSig_a_v = makeSig([], [kWasmAnyFunc]); 1581cb0ef41Sopenharmony_cilet kSig_a_i = makeSig([kWasmI32], [kWasmAnyFunc]); 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_cifunction makeSig(params, results) { 1611cb0ef41Sopenharmony_ci return {params: params, results: results}; 1621cb0ef41Sopenharmony_ci} 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_cifunction makeSig_v_x(x) { 1651cb0ef41Sopenharmony_ci return makeSig([x], []); 1661cb0ef41Sopenharmony_ci} 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_cifunction makeSig_v_xx(x) { 1691cb0ef41Sopenharmony_ci return makeSig([x, x], []); 1701cb0ef41Sopenharmony_ci} 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_cifunction makeSig_r_v(r) { 1731cb0ef41Sopenharmony_ci return makeSig([], [r]); 1741cb0ef41Sopenharmony_ci} 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_cifunction makeSig_r_x(r, x) { 1771cb0ef41Sopenharmony_ci return makeSig([x], [r]); 1781cb0ef41Sopenharmony_ci} 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_cifunction makeSig_r_xx(r, x) { 1811cb0ef41Sopenharmony_ci return makeSig([x, x], [r]); 1821cb0ef41Sopenharmony_ci} 1831cb0ef41Sopenharmony_ci 1841cb0ef41Sopenharmony_ci// Opcodes 1851cb0ef41Sopenharmony_cilet kExprUnreachable = 0x00; 1861cb0ef41Sopenharmony_cilet kExprNop = 0x01; 1871cb0ef41Sopenharmony_cilet kExprBlock = 0x02; 1881cb0ef41Sopenharmony_cilet kExprLoop = 0x03; 1891cb0ef41Sopenharmony_cilet kExprIf = 0x04; 1901cb0ef41Sopenharmony_cilet kExprElse = 0x05; 1911cb0ef41Sopenharmony_cilet kExprTry = 0x06; 1921cb0ef41Sopenharmony_cilet kExprCatch = 0x07; 1931cb0ef41Sopenharmony_cilet kExprCatchAll = 0x19; 1941cb0ef41Sopenharmony_cilet kExprThrow = 0x08; 1951cb0ef41Sopenharmony_cilet kExprRethrow = 0x09; 1961cb0ef41Sopenharmony_cilet kExprBrOnExn = 0x0a; 1971cb0ef41Sopenharmony_cilet kExprEnd = 0x0b; 1981cb0ef41Sopenharmony_cilet kExprBr = 0x0c; 1991cb0ef41Sopenharmony_cilet kExprBrIf = 0x0d; 2001cb0ef41Sopenharmony_cilet kExprBrTable = 0x0e; 2011cb0ef41Sopenharmony_cilet kExprReturn = 0x0f; 2021cb0ef41Sopenharmony_cilet kExprCallFunction = 0x10; 2031cb0ef41Sopenharmony_cilet kExprCallIndirect = 0x11; 2041cb0ef41Sopenharmony_cilet kExprReturnCall = 0x12; 2051cb0ef41Sopenharmony_cilet kExprReturnCallIndirect = 0x13; 2061cb0ef41Sopenharmony_cilet kExprDrop = 0x1a; 2071cb0ef41Sopenharmony_cilet kExprSelect = 0x1b; 2081cb0ef41Sopenharmony_cilet kExprLocalGet = 0x20; 2091cb0ef41Sopenharmony_cilet kExprLocalSet = 0x21; 2101cb0ef41Sopenharmony_cilet kExprLocalTee = 0x22; 2111cb0ef41Sopenharmony_cilet kExprGlobalGet = 0x23; 2121cb0ef41Sopenharmony_cilet kExprGlobalSet = 0x24; 2131cb0ef41Sopenharmony_cilet kExprTableGet = 0x25; 2141cb0ef41Sopenharmony_cilet kExprTableSet = 0x26; 2151cb0ef41Sopenharmony_cilet kExprI32LoadMem = 0x28; 2161cb0ef41Sopenharmony_cilet kExprI64LoadMem = 0x29; 2171cb0ef41Sopenharmony_cilet kExprF32LoadMem = 0x2a; 2181cb0ef41Sopenharmony_cilet kExprF64LoadMem = 0x2b; 2191cb0ef41Sopenharmony_cilet kExprI32LoadMem8S = 0x2c; 2201cb0ef41Sopenharmony_cilet kExprI32LoadMem8U = 0x2d; 2211cb0ef41Sopenharmony_cilet kExprI32LoadMem16S = 0x2e; 2221cb0ef41Sopenharmony_cilet kExprI32LoadMem16U = 0x2f; 2231cb0ef41Sopenharmony_cilet kExprI64LoadMem8S = 0x30; 2241cb0ef41Sopenharmony_cilet kExprI64LoadMem8U = 0x31; 2251cb0ef41Sopenharmony_cilet kExprI64LoadMem16S = 0x32; 2261cb0ef41Sopenharmony_cilet kExprI64LoadMem16U = 0x33; 2271cb0ef41Sopenharmony_cilet kExprI64LoadMem32S = 0x34; 2281cb0ef41Sopenharmony_cilet kExprI64LoadMem32U = 0x35; 2291cb0ef41Sopenharmony_cilet kExprI32StoreMem = 0x36; 2301cb0ef41Sopenharmony_cilet kExprI64StoreMem = 0x37; 2311cb0ef41Sopenharmony_cilet kExprF32StoreMem = 0x38; 2321cb0ef41Sopenharmony_cilet kExprF64StoreMem = 0x39; 2331cb0ef41Sopenharmony_cilet kExprI32StoreMem8 = 0x3a; 2341cb0ef41Sopenharmony_cilet kExprI32StoreMem16 = 0x3b; 2351cb0ef41Sopenharmony_cilet kExprI64StoreMem8 = 0x3c; 2361cb0ef41Sopenharmony_cilet kExprI64StoreMem16 = 0x3d; 2371cb0ef41Sopenharmony_cilet kExprI64StoreMem32 = 0x3e; 2381cb0ef41Sopenharmony_cilet kExprMemorySize = 0x3f; 2391cb0ef41Sopenharmony_cilet kExprMemoryGrow = 0x40; 2401cb0ef41Sopenharmony_cilet kExprI32Const = 0x41; 2411cb0ef41Sopenharmony_cilet kExprI64Const = 0x42; 2421cb0ef41Sopenharmony_cilet kExprF32Const = 0x43; 2431cb0ef41Sopenharmony_cilet kExprF64Const = 0x44; 2441cb0ef41Sopenharmony_cilet kExprI32Eqz = 0x45; 2451cb0ef41Sopenharmony_cilet kExprI32Eq = 0x46; 2461cb0ef41Sopenharmony_cilet kExprI32Ne = 0x47; 2471cb0ef41Sopenharmony_cilet kExprI32LtS = 0x48; 2481cb0ef41Sopenharmony_cilet kExprI32LtU = 0x49; 2491cb0ef41Sopenharmony_cilet kExprI32GtS = 0x4a; 2501cb0ef41Sopenharmony_cilet kExprI32GtU = 0x4b; 2511cb0ef41Sopenharmony_cilet kExprI32LeS = 0x4c; 2521cb0ef41Sopenharmony_cilet kExprI32LeU = 0x4d; 2531cb0ef41Sopenharmony_cilet kExprI32GeS = 0x4e; 2541cb0ef41Sopenharmony_cilet kExprI32GeU = 0x4f; 2551cb0ef41Sopenharmony_cilet kExprI64Eqz = 0x50; 2561cb0ef41Sopenharmony_cilet kExprI64Eq = 0x51; 2571cb0ef41Sopenharmony_cilet kExprI64Ne = 0x52; 2581cb0ef41Sopenharmony_cilet kExprI64LtS = 0x53; 2591cb0ef41Sopenharmony_cilet kExprI64LtU = 0x54; 2601cb0ef41Sopenharmony_cilet kExprI64GtS = 0x55; 2611cb0ef41Sopenharmony_cilet kExprI64GtU = 0x56; 2621cb0ef41Sopenharmony_cilet kExprI64LeS = 0x57; 2631cb0ef41Sopenharmony_cilet kExprI64LeU = 0x58; 2641cb0ef41Sopenharmony_cilet kExprI64GeS = 0x59; 2651cb0ef41Sopenharmony_cilet kExprI64GeU = 0x5a; 2661cb0ef41Sopenharmony_cilet kExprF32Eq = 0x5b; 2671cb0ef41Sopenharmony_cilet kExprF32Ne = 0x5c; 2681cb0ef41Sopenharmony_cilet kExprF32Lt = 0x5d; 2691cb0ef41Sopenharmony_cilet kExprF32Gt = 0x5e; 2701cb0ef41Sopenharmony_cilet kExprF32Le = 0x5f; 2711cb0ef41Sopenharmony_cilet kExprF32Ge = 0x60; 2721cb0ef41Sopenharmony_cilet kExprF64Eq = 0x61; 2731cb0ef41Sopenharmony_cilet kExprF64Ne = 0x62; 2741cb0ef41Sopenharmony_cilet kExprF64Lt = 0x63; 2751cb0ef41Sopenharmony_cilet kExprF64Gt = 0x64; 2761cb0ef41Sopenharmony_cilet kExprF64Le = 0x65; 2771cb0ef41Sopenharmony_cilet kExprF64Ge = 0x66; 2781cb0ef41Sopenharmony_cilet kExprI32Clz = 0x67; 2791cb0ef41Sopenharmony_cilet kExprI32Ctz = 0x68; 2801cb0ef41Sopenharmony_cilet kExprI32Popcnt = 0x69; 2811cb0ef41Sopenharmony_cilet kExprI32Add = 0x6a; 2821cb0ef41Sopenharmony_cilet kExprI32Sub = 0x6b; 2831cb0ef41Sopenharmony_cilet kExprI32Mul = 0x6c; 2841cb0ef41Sopenharmony_cilet kExprI32DivS = 0x6d; 2851cb0ef41Sopenharmony_cilet kExprI32DivU = 0x6e; 2861cb0ef41Sopenharmony_cilet kExprI32RemS = 0x6f; 2871cb0ef41Sopenharmony_cilet kExprI32RemU = 0x70; 2881cb0ef41Sopenharmony_cilet kExprI32And = 0x71; 2891cb0ef41Sopenharmony_cilet kExprI32Ior = 0x72; 2901cb0ef41Sopenharmony_cilet kExprI32Xor = 0x73; 2911cb0ef41Sopenharmony_cilet kExprI32Shl = 0x74; 2921cb0ef41Sopenharmony_cilet kExprI32ShrS = 0x75; 2931cb0ef41Sopenharmony_cilet kExprI32ShrU = 0x76; 2941cb0ef41Sopenharmony_cilet kExprI32Rol = 0x77; 2951cb0ef41Sopenharmony_cilet kExprI32Ror = 0x78; 2961cb0ef41Sopenharmony_cilet kExprI64Clz = 0x79; 2971cb0ef41Sopenharmony_cilet kExprI64Ctz = 0x7a; 2981cb0ef41Sopenharmony_cilet kExprI64Popcnt = 0x7b; 2991cb0ef41Sopenharmony_cilet kExprI64Add = 0x7c; 3001cb0ef41Sopenharmony_cilet kExprI64Sub = 0x7d; 3011cb0ef41Sopenharmony_cilet kExprI64Mul = 0x7e; 3021cb0ef41Sopenharmony_cilet kExprI64DivS = 0x7f; 3031cb0ef41Sopenharmony_cilet kExprI64DivU = 0x80; 3041cb0ef41Sopenharmony_cilet kExprI64RemS = 0x81; 3051cb0ef41Sopenharmony_cilet kExprI64RemU = 0x82; 3061cb0ef41Sopenharmony_cilet kExprI64And = 0x83; 3071cb0ef41Sopenharmony_cilet kExprI64Ior = 0x84; 3081cb0ef41Sopenharmony_cilet kExprI64Xor = 0x85; 3091cb0ef41Sopenharmony_cilet kExprI64Shl = 0x86; 3101cb0ef41Sopenharmony_cilet kExprI64ShrS = 0x87; 3111cb0ef41Sopenharmony_cilet kExprI64ShrU = 0x88; 3121cb0ef41Sopenharmony_cilet kExprI64Rol = 0x89; 3131cb0ef41Sopenharmony_cilet kExprI64Ror = 0x8a; 3141cb0ef41Sopenharmony_cilet kExprF32Abs = 0x8b; 3151cb0ef41Sopenharmony_cilet kExprF32Neg = 0x8c; 3161cb0ef41Sopenharmony_cilet kExprF32Ceil = 0x8d; 3171cb0ef41Sopenharmony_cilet kExprF32Floor = 0x8e; 3181cb0ef41Sopenharmony_cilet kExprF32Trunc = 0x8f; 3191cb0ef41Sopenharmony_cilet kExprF32NearestInt = 0x90; 3201cb0ef41Sopenharmony_cilet kExprF32Sqrt = 0x91; 3211cb0ef41Sopenharmony_cilet kExprF32Add = 0x92; 3221cb0ef41Sopenharmony_cilet kExprF32Sub = 0x93; 3231cb0ef41Sopenharmony_cilet kExprF32Mul = 0x94; 3241cb0ef41Sopenharmony_cilet kExprF32Div = 0x95; 3251cb0ef41Sopenharmony_cilet kExprF32Min = 0x96; 3261cb0ef41Sopenharmony_cilet kExprF32Max = 0x97; 3271cb0ef41Sopenharmony_cilet kExprF32CopySign = 0x98; 3281cb0ef41Sopenharmony_cilet kExprF64Abs = 0x99; 3291cb0ef41Sopenharmony_cilet kExprF64Neg = 0x9a; 3301cb0ef41Sopenharmony_cilet kExprF64Ceil = 0x9b; 3311cb0ef41Sopenharmony_cilet kExprF64Floor = 0x9c; 3321cb0ef41Sopenharmony_cilet kExprF64Trunc = 0x9d; 3331cb0ef41Sopenharmony_cilet kExprF64NearestInt = 0x9e; 3341cb0ef41Sopenharmony_cilet kExprF64Sqrt = 0x9f; 3351cb0ef41Sopenharmony_cilet kExprF64Add = 0xa0; 3361cb0ef41Sopenharmony_cilet kExprF64Sub = 0xa1; 3371cb0ef41Sopenharmony_cilet kExprF64Mul = 0xa2; 3381cb0ef41Sopenharmony_cilet kExprF64Div = 0xa3; 3391cb0ef41Sopenharmony_cilet kExprF64Min = 0xa4; 3401cb0ef41Sopenharmony_cilet kExprF64Max = 0xa5; 3411cb0ef41Sopenharmony_cilet kExprF64CopySign = 0xa6; 3421cb0ef41Sopenharmony_cilet kExprI32ConvertI64 = 0xa7; 3431cb0ef41Sopenharmony_cilet kExprI32SConvertF32 = 0xa8; 3441cb0ef41Sopenharmony_cilet kExprI32UConvertF32 = 0xa9; 3451cb0ef41Sopenharmony_cilet kExprI32SConvertF64 = 0xaa; 3461cb0ef41Sopenharmony_cilet kExprI32UConvertF64 = 0xab; 3471cb0ef41Sopenharmony_cilet kExprI64SConvertI32 = 0xac; 3481cb0ef41Sopenharmony_cilet kExprI64UConvertI32 = 0xad; 3491cb0ef41Sopenharmony_cilet kExprI64SConvertF32 = 0xae; 3501cb0ef41Sopenharmony_cilet kExprI64UConvertF32 = 0xaf; 3511cb0ef41Sopenharmony_cilet kExprI64SConvertF64 = 0xb0; 3521cb0ef41Sopenharmony_cilet kExprI64UConvertF64 = 0xb1; 3531cb0ef41Sopenharmony_cilet kExprF32SConvertI32 = 0xb2; 3541cb0ef41Sopenharmony_cilet kExprF32UConvertI32 = 0xb3; 3551cb0ef41Sopenharmony_cilet kExprF32SConvertI64 = 0xb4; 3561cb0ef41Sopenharmony_cilet kExprF32UConvertI64 = 0xb5; 3571cb0ef41Sopenharmony_cilet kExprF32ConvertF64 = 0xb6; 3581cb0ef41Sopenharmony_cilet kExprF64SConvertI32 = 0xb7; 3591cb0ef41Sopenharmony_cilet kExprF64UConvertI32 = 0xb8; 3601cb0ef41Sopenharmony_cilet kExprF64SConvertI64 = 0xb9; 3611cb0ef41Sopenharmony_cilet kExprF64UConvertI64 = 0xba; 3621cb0ef41Sopenharmony_cilet kExprF64ConvertF32 = 0xbb; 3631cb0ef41Sopenharmony_cilet kExprI32ReinterpretF32 = 0xbc; 3641cb0ef41Sopenharmony_cilet kExprI64ReinterpretF64 = 0xbd; 3651cb0ef41Sopenharmony_cilet kExprF32ReinterpretI32 = 0xbe; 3661cb0ef41Sopenharmony_cilet kExprF64ReinterpretI64 = 0xbf; 3671cb0ef41Sopenharmony_cilet kExprI32SExtendI8 = 0xc0; 3681cb0ef41Sopenharmony_cilet kExprI32SExtendI16 = 0xc1; 3691cb0ef41Sopenharmony_cilet kExprI64SExtendI8 = 0xc2; 3701cb0ef41Sopenharmony_cilet kExprI64SExtendI16 = 0xc3; 3711cb0ef41Sopenharmony_cilet kExprI64SExtendI32 = 0xc4; 3721cb0ef41Sopenharmony_cilet kExprRefNull = 0xd0; 3731cb0ef41Sopenharmony_cilet kExprRefIsNull = 0xd1; 3741cb0ef41Sopenharmony_cilet kExprRefFunc = 0xd2; 3751cb0ef41Sopenharmony_ci 3761cb0ef41Sopenharmony_ci// Prefix opcodes 3771cb0ef41Sopenharmony_cilet kNumericPrefix = 0xfc; 3781cb0ef41Sopenharmony_cilet kSimdPrefix = 0xfd; 3791cb0ef41Sopenharmony_cilet kAtomicPrefix = 0xfe; 3801cb0ef41Sopenharmony_ci 3811cb0ef41Sopenharmony_ci// Numeric opcodes. 3821cb0ef41Sopenharmony_cilet kExprMemoryInit = 0x08; 3831cb0ef41Sopenharmony_cilet kExprDataDrop = 0x09; 3841cb0ef41Sopenharmony_cilet kExprMemoryCopy = 0x0a; 3851cb0ef41Sopenharmony_cilet kExprMemoryFill = 0x0b; 3861cb0ef41Sopenharmony_cilet kExprTableInit = 0x0c; 3871cb0ef41Sopenharmony_cilet kExprElemDrop = 0x0d; 3881cb0ef41Sopenharmony_cilet kExprTableCopy = 0x0e; 3891cb0ef41Sopenharmony_cilet kExprTableGrow = 0x0f; 3901cb0ef41Sopenharmony_cilet kExprTableSize = 0x10; 3911cb0ef41Sopenharmony_cilet kExprTableFill = 0x11; 3921cb0ef41Sopenharmony_ci 3931cb0ef41Sopenharmony_ci// Atomic opcodes. 3941cb0ef41Sopenharmony_cilet kExprAtomicNotify = 0x00; 3951cb0ef41Sopenharmony_cilet kExprI32AtomicWait = 0x01; 3961cb0ef41Sopenharmony_cilet kExprI64AtomicWait = 0x02; 3971cb0ef41Sopenharmony_cilet kExprI32AtomicLoad = 0x10; 3981cb0ef41Sopenharmony_cilet kExprI32AtomicLoad8U = 0x12; 3991cb0ef41Sopenharmony_cilet kExprI32AtomicLoad16U = 0x13; 4001cb0ef41Sopenharmony_cilet kExprI32AtomicStore = 0x17; 4011cb0ef41Sopenharmony_cilet kExprI32AtomicStore8U = 0x19; 4021cb0ef41Sopenharmony_cilet kExprI32AtomicStore16U = 0x1a; 4031cb0ef41Sopenharmony_cilet kExprI32AtomicAdd = 0x1e; 4041cb0ef41Sopenharmony_cilet kExprI32AtomicAdd8U = 0x20; 4051cb0ef41Sopenharmony_cilet kExprI32AtomicAdd16U = 0x21; 4061cb0ef41Sopenharmony_cilet kExprI32AtomicSub = 0x25; 4071cb0ef41Sopenharmony_cilet kExprI32AtomicSub8U = 0x27; 4081cb0ef41Sopenharmony_cilet kExprI32AtomicSub16U = 0x28; 4091cb0ef41Sopenharmony_cilet kExprI32AtomicAnd = 0x2c; 4101cb0ef41Sopenharmony_cilet kExprI32AtomicAnd8U = 0x2e; 4111cb0ef41Sopenharmony_cilet kExprI32AtomicAnd16U = 0x2f; 4121cb0ef41Sopenharmony_cilet kExprI32AtomicOr = 0x33; 4131cb0ef41Sopenharmony_cilet kExprI32AtomicOr8U = 0x35; 4141cb0ef41Sopenharmony_cilet kExprI32AtomicOr16U = 0x36; 4151cb0ef41Sopenharmony_cilet kExprI32AtomicXor = 0x3a; 4161cb0ef41Sopenharmony_cilet kExprI32AtomicXor8U = 0x3c; 4171cb0ef41Sopenharmony_cilet kExprI32AtomicXor16U = 0x3d; 4181cb0ef41Sopenharmony_cilet kExprI32AtomicExchange = 0x41; 4191cb0ef41Sopenharmony_cilet kExprI32AtomicExchange8U = 0x43; 4201cb0ef41Sopenharmony_cilet kExprI32AtomicExchange16U = 0x44; 4211cb0ef41Sopenharmony_cilet kExprI32AtomicCompareExchange = 0x48; 4221cb0ef41Sopenharmony_cilet kExprI32AtomicCompareExchange8U = 0x4a; 4231cb0ef41Sopenharmony_cilet kExprI32AtomicCompareExchange16U = 0x4b; 4241cb0ef41Sopenharmony_ci 4251cb0ef41Sopenharmony_cilet kExprI64AtomicLoad = 0x11; 4261cb0ef41Sopenharmony_cilet kExprI64AtomicLoad8U = 0x14; 4271cb0ef41Sopenharmony_cilet kExprI64AtomicLoad16U = 0x15; 4281cb0ef41Sopenharmony_cilet kExprI64AtomicLoad32U = 0x16; 4291cb0ef41Sopenharmony_cilet kExprI64AtomicStore = 0x18; 4301cb0ef41Sopenharmony_cilet kExprI64AtomicStore8U = 0x1b; 4311cb0ef41Sopenharmony_cilet kExprI64AtomicStore16U = 0x1c; 4321cb0ef41Sopenharmony_cilet kExprI64AtomicStore32U = 0x1d; 4331cb0ef41Sopenharmony_cilet kExprI64AtomicAdd = 0x1f; 4341cb0ef41Sopenharmony_cilet kExprI64AtomicAdd8U = 0x22; 4351cb0ef41Sopenharmony_cilet kExprI64AtomicAdd16U = 0x23; 4361cb0ef41Sopenharmony_cilet kExprI64AtomicAdd32U = 0x24; 4371cb0ef41Sopenharmony_cilet kExprI64AtomicSub = 0x26; 4381cb0ef41Sopenharmony_cilet kExprI64AtomicSub8U = 0x29; 4391cb0ef41Sopenharmony_cilet kExprI64AtomicSub16U = 0x2a; 4401cb0ef41Sopenharmony_cilet kExprI64AtomicSub32U = 0x2b; 4411cb0ef41Sopenharmony_cilet kExprI64AtomicAnd = 0x2d; 4421cb0ef41Sopenharmony_cilet kExprI64AtomicAnd8U = 0x30; 4431cb0ef41Sopenharmony_cilet kExprI64AtomicAnd16U = 0x31; 4441cb0ef41Sopenharmony_cilet kExprI64AtomicAnd32U = 0x32; 4451cb0ef41Sopenharmony_cilet kExprI64AtomicOr = 0x34; 4461cb0ef41Sopenharmony_cilet kExprI64AtomicOr8U = 0x37; 4471cb0ef41Sopenharmony_cilet kExprI64AtomicOr16U = 0x38; 4481cb0ef41Sopenharmony_cilet kExprI64AtomicOr32U = 0x39; 4491cb0ef41Sopenharmony_cilet kExprI64AtomicXor = 0x3b; 4501cb0ef41Sopenharmony_cilet kExprI64AtomicXor8U = 0x3e; 4511cb0ef41Sopenharmony_cilet kExprI64AtomicXor16U = 0x3f; 4521cb0ef41Sopenharmony_cilet kExprI64AtomicXor32U = 0x40; 4531cb0ef41Sopenharmony_cilet kExprI64AtomicExchange = 0x42; 4541cb0ef41Sopenharmony_cilet kExprI64AtomicExchange8U = 0x45; 4551cb0ef41Sopenharmony_cilet kExprI64AtomicExchange16U = 0x46; 4561cb0ef41Sopenharmony_cilet kExprI64AtomicExchange32U = 0x47; 4571cb0ef41Sopenharmony_cilet kExprI64AtomicCompareExchange = 0x49 4581cb0ef41Sopenharmony_cilet kExprI64AtomicCompareExchange8U = 0x4c; 4591cb0ef41Sopenharmony_cilet kExprI64AtomicCompareExchange16U = 0x4d; 4601cb0ef41Sopenharmony_cilet kExprI64AtomicCompareExchange32U = 0x4e; 4611cb0ef41Sopenharmony_ci 4621cb0ef41Sopenharmony_ci// Simd opcodes. 4631cb0ef41Sopenharmony_cilet kExprS128LoadMem = 0x00; 4641cb0ef41Sopenharmony_cilet kExprS128StoreMem = 0x01; 4651cb0ef41Sopenharmony_cilet kExprI32x4Splat = 0x0c; 4661cb0ef41Sopenharmony_cilet kExprI32x4Eq = 0x2c; 4671cb0ef41Sopenharmony_cilet kExprS1x4AllTrue = 0x75; 4681cb0ef41Sopenharmony_cilet kExprF32x4Min = 0x9e; 4691cb0ef41Sopenharmony_ci 4701cb0ef41Sopenharmony_ciclass Binary { 4711cb0ef41Sopenharmony_ci constructor() { 4721cb0ef41Sopenharmony_ci this.length = 0; 4731cb0ef41Sopenharmony_ci this.buffer = new Uint8Array(8192); 4741cb0ef41Sopenharmony_ci } 4751cb0ef41Sopenharmony_ci 4761cb0ef41Sopenharmony_ci ensure_space(needed) { 4771cb0ef41Sopenharmony_ci if (this.buffer.length - this.length >= needed) return; 4781cb0ef41Sopenharmony_ci let new_capacity = this.buffer.length * 2; 4791cb0ef41Sopenharmony_ci while (new_capacity - this.length < needed) new_capacity *= 2; 4801cb0ef41Sopenharmony_ci let new_buffer = new Uint8Array(new_capacity); 4811cb0ef41Sopenharmony_ci new_buffer.set(this.buffer); 4821cb0ef41Sopenharmony_ci this.buffer = new_buffer; 4831cb0ef41Sopenharmony_ci } 4841cb0ef41Sopenharmony_ci 4851cb0ef41Sopenharmony_ci trunc_buffer() { 4861cb0ef41Sopenharmony_ci return new Uint8Array(this.buffer.buffer, 0, this.length); 4871cb0ef41Sopenharmony_ci } 4881cb0ef41Sopenharmony_ci 4891cb0ef41Sopenharmony_ci reset() { 4901cb0ef41Sopenharmony_ci this.length = 0; 4911cb0ef41Sopenharmony_ci } 4921cb0ef41Sopenharmony_ci 4931cb0ef41Sopenharmony_ci emit_u8(val) { 4941cb0ef41Sopenharmony_ci this.ensure_space(1); 4951cb0ef41Sopenharmony_ci this.buffer[this.length++] = val; 4961cb0ef41Sopenharmony_ci } 4971cb0ef41Sopenharmony_ci 4981cb0ef41Sopenharmony_ci emit_u16(val) { 4991cb0ef41Sopenharmony_ci this.ensure_space(2); 5001cb0ef41Sopenharmony_ci this.buffer[this.length++] = val; 5011cb0ef41Sopenharmony_ci this.buffer[this.length++] = val >> 8; 5021cb0ef41Sopenharmony_ci } 5031cb0ef41Sopenharmony_ci 5041cb0ef41Sopenharmony_ci emit_u32(val) { 5051cb0ef41Sopenharmony_ci this.ensure_space(4); 5061cb0ef41Sopenharmony_ci this.buffer[this.length++] = val; 5071cb0ef41Sopenharmony_ci this.buffer[this.length++] = val >> 8; 5081cb0ef41Sopenharmony_ci this.buffer[this.length++] = val >> 16; 5091cb0ef41Sopenharmony_ci this.buffer[this.length++] = val >> 24; 5101cb0ef41Sopenharmony_ci } 5111cb0ef41Sopenharmony_ci 5121cb0ef41Sopenharmony_ci emit_leb_u(val, max_len) { 5131cb0ef41Sopenharmony_ci this.ensure_space(max_len); 5141cb0ef41Sopenharmony_ci for (let i = 0; i < max_len; ++i) { 5151cb0ef41Sopenharmony_ci let v = val & 0xff; 5161cb0ef41Sopenharmony_ci val = val >>> 7; 5171cb0ef41Sopenharmony_ci if (val == 0) { 5181cb0ef41Sopenharmony_ci this.buffer[this.length++] = v; 5191cb0ef41Sopenharmony_ci return; 5201cb0ef41Sopenharmony_ci } 5211cb0ef41Sopenharmony_ci this.buffer[this.length++] = v | 0x80; 5221cb0ef41Sopenharmony_ci } 5231cb0ef41Sopenharmony_ci throw new Error("Leb value exceeds maximum length of " + max_len); 5241cb0ef41Sopenharmony_ci } 5251cb0ef41Sopenharmony_ci 5261cb0ef41Sopenharmony_ci emit_u32v(val) { 5271cb0ef41Sopenharmony_ci this.emit_leb_u(val, kMaxVarInt32Size); 5281cb0ef41Sopenharmony_ci } 5291cb0ef41Sopenharmony_ci 5301cb0ef41Sopenharmony_ci emit_u64v(val) { 5311cb0ef41Sopenharmony_ci this.emit_leb_u(val, kMaxVarInt64Size); 5321cb0ef41Sopenharmony_ci } 5331cb0ef41Sopenharmony_ci 5341cb0ef41Sopenharmony_ci emit_bytes(data) { 5351cb0ef41Sopenharmony_ci this.ensure_space(data.length); 5361cb0ef41Sopenharmony_ci this.buffer.set(data, this.length); 5371cb0ef41Sopenharmony_ci this.length += data.length; 5381cb0ef41Sopenharmony_ci } 5391cb0ef41Sopenharmony_ci 5401cb0ef41Sopenharmony_ci emit_string(string) { 5411cb0ef41Sopenharmony_ci // When testing illegal names, we pass a byte array directly. 5421cb0ef41Sopenharmony_ci if (string instanceof Array) { 5431cb0ef41Sopenharmony_ci this.emit_u32v(string.length); 5441cb0ef41Sopenharmony_ci this.emit_bytes(string); 5451cb0ef41Sopenharmony_ci return; 5461cb0ef41Sopenharmony_ci } 5471cb0ef41Sopenharmony_ci 5481cb0ef41Sopenharmony_ci // This is the hacky way to convert a JavaScript string to a UTF8 encoded 5491cb0ef41Sopenharmony_ci // string only containing single-byte characters. 5501cb0ef41Sopenharmony_ci let string_utf8 = unescape(encodeURIComponent(string)); 5511cb0ef41Sopenharmony_ci this.emit_u32v(string_utf8.length); 5521cb0ef41Sopenharmony_ci for (let i = 0; i < string_utf8.length; i++) { 5531cb0ef41Sopenharmony_ci this.emit_u8(string_utf8.charCodeAt(i)); 5541cb0ef41Sopenharmony_ci } 5551cb0ef41Sopenharmony_ci } 5561cb0ef41Sopenharmony_ci 5571cb0ef41Sopenharmony_ci emit_header() { 5581cb0ef41Sopenharmony_ci this.emit_bytes([ 5591cb0ef41Sopenharmony_ci kWasmH0, kWasmH1, kWasmH2, kWasmH3, kWasmV0, kWasmV1, kWasmV2, kWasmV3 5601cb0ef41Sopenharmony_ci ]); 5611cb0ef41Sopenharmony_ci } 5621cb0ef41Sopenharmony_ci 5631cb0ef41Sopenharmony_ci emit_section(section_code, content_generator) { 5641cb0ef41Sopenharmony_ci // Emit section name. 5651cb0ef41Sopenharmony_ci this.emit_u8(section_code); 5661cb0ef41Sopenharmony_ci // Emit the section to a temporary buffer: its full length isn't know yet. 5671cb0ef41Sopenharmony_ci const section = new Binary; 5681cb0ef41Sopenharmony_ci content_generator(section); 5691cb0ef41Sopenharmony_ci // Emit section length. 5701cb0ef41Sopenharmony_ci this.emit_u32v(section.length); 5711cb0ef41Sopenharmony_ci // Copy the temporary buffer. 5721cb0ef41Sopenharmony_ci // Avoid spread because {section} can be huge. 5731cb0ef41Sopenharmony_ci this.emit_bytes(section.trunc_buffer()); 5741cb0ef41Sopenharmony_ci } 5751cb0ef41Sopenharmony_ci} 5761cb0ef41Sopenharmony_ci 5771cb0ef41Sopenharmony_ciclass WasmFunctionBuilder { 5781cb0ef41Sopenharmony_ci constructor(module, name, type_index) { 5791cb0ef41Sopenharmony_ci this.module = module; 5801cb0ef41Sopenharmony_ci this.name = name; 5811cb0ef41Sopenharmony_ci this.type_index = type_index; 5821cb0ef41Sopenharmony_ci this.body = []; 5831cb0ef41Sopenharmony_ci this.locals = []; 5841cb0ef41Sopenharmony_ci this.local_names = []; 5851cb0ef41Sopenharmony_ci } 5861cb0ef41Sopenharmony_ci 5871cb0ef41Sopenharmony_ci numLocalNames() { 5881cb0ef41Sopenharmony_ci let num_local_names = 0; 5891cb0ef41Sopenharmony_ci for (let loc_name of this.local_names) { 5901cb0ef41Sopenharmony_ci if (loc_name !== undefined) ++num_local_names; 5911cb0ef41Sopenharmony_ci } 5921cb0ef41Sopenharmony_ci return num_local_names; 5931cb0ef41Sopenharmony_ci } 5941cb0ef41Sopenharmony_ci 5951cb0ef41Sopenharmony_ci exportAs(name) { 5961cb0ef41Sopenharmony_ci this.module.addExport(name, this.index); 5971cb0ef41Sopenharmony_ci return this; 5981cb0ef41Sopenharmony_ci } 5991cb0ef41Sopenharmony_ci 6001cb0ef41Sopenharmony_ci exportFunc() { 6011cb0ef41Sopenharmony_ci this.exportAs(this.name); 6021cb0ef41Sopenharmony_ci return this; 6031cb0ef41Sopenharmony_ci } 6041cb0ef41Sopenharmony_ci 6051cb0ef41Sopenharmony_ci addBody(body) { 6061cb0ef41Sopenharmony_ci for (let b of body) { 6071cb0ef41Sopenharmony_ci if (typeof b !== 'number' || (b & (~0xFF)) !== 0 ) 6081cb0ef41Sopenharmony_ci throw new Error('invalid body (entries must be 8 bit numbers): ' + body); 6091cb0ef41Sopenharmony_ci } 6101cb0ef41Sopenharmony_ci this.body = body.slice(); 6111cb0ef41Sopenharmony_ci // Automatically add the end for the function block to the body. 6121cb0ef41Sopenharmony_ci this.body.push(kExprEnd); 6131cb0ef41Sopenharmony_ci return this; 6141cb0ef41Sopenharmony_ci } 6151cb0ef41Sopenharmony_ci 6161cb0ef41Sopenharmony_ci addBodyWithEnd(body) { 6171cb0ef41Sopenharmony_ci this.body = body; 6181cb0ef41Sopenharmony_ci return this; 6191cb0ef41Sopenharmony_ci } 6201cb0ef41Sopenharmony_ci 6211cb0ef41Sopenharmony_ci getNumLocals() { 6221cb0ef41Sopenharmony_ci let total_locals = 0; 6231cb0ef41Sopenharmony_ci for (let l of this.locals) { 6241cb0ef41Sopenharmony_ci for (let type of ["i32", "i64", "f32", "f64", "s128"]) { 6251cb0ef41Sopenharmony_ci total_locals += l[type + "_count"] || 0; 6261cb0ef41Sopenharmony_ci } 6271cb0ef41Sopenharmony_ci } 6281cb0ef41Sopenharmony_ci return total_locals; 6291cb0ef41Sopenharmony_ci } 6301cb0ef41Sopenharmony_ci 6311cb0ef41Sopenharmony_ci addLocals(locals, names) { 6321cb0ef41Sopenharmony_ci const old_num_locals = this.getNumLocals(); 6331cb0ef41Sopenharmony_ci this.locals.push(locals); 6341cb0ef41Sopenharmony_ci if (names) { 6351cb0ef41Sopenharmony_ci const missing_names = old_num_locals - this.local_names.length; 6361cb0ef41Sopenharmony_ci this.local_names.push(...new Array(missing_names), ...names); 6371cb0ef41Sopenharmony_ci } 6381cb0ef41Sopenharmony_ci return this; 6391cb0ef41Sopenharmony_ci } 6401cb0ef41Sopenharmony_ci 6411cb0ef41Sopenharmony_ci end() { 6421cb0ef41Sopenharmony_ci return this.module; 6431cb0ef41Sopenharmony_ci } 6441cb0ef41Sopenharmony_ci} 6451cb0ef41Sopenharmony_ci 6461cb0ef41Sopenharmony_ciclass WasmGlobalBuilder { 6471cb0ef41Sopenharmony_ci constructor(module, type, mutable) { 6481cb0ef41Sopenharmony_ci this.module = module; 6491cb0ef41Sopenharmony_ci this.type = type; 6501cb0ef41Sopenharmony_ci this.mutable = mutable; 6511cb0ef41Sopenharmony_ci this.init = 0; 6521cb0ef41Sopenharmony_ci } 6531cb0ef41Sopenharmony_ci 6541cb0ef41Sopenharmony_ci exportAs(name) { 6551cb0ef41Sopenharmony_ci this.module.exports.push({name: name, kind: kExternalGlobal, 6561cb0ef41Sopenharmony_ci index: this.index}); 6571cb0ef41Sopenharmony_ci return this; 6581cb0ef41Sopenharmony_ci } 6591cb0ef41Sopenharmony_ci} 6601cb0ef41Sopenharmony_ci 6611cb0ef41Sopenharmony_ciclass WasmTableBuilder { 6621cb0ef41Sopenharmony_ci constructor(module, type, initial_size, max_size) { 6631cb0ef41Sopenharmony_ci this.module = module; 6641cb0ef41Sopenharmony_ci this.type = type; 6651cb0ef41Sopenharmony_ci this.initial_size = initial_size; 6661cb0ef41Sopenharmony_ci this.has_max = max_size != undefined; 6671cb0ef41Sopenharmony_ci this.max_size = max_size; 6681cb0ef41Sopenharmony_ci } 6691cb0ef41Sopenharmony_ci 6701cb0ef41Sopenharmony_ci exportAs(name) { 6711cb0ef41Sopenharmony_ci this.module.exports.push({name: name, kind: kExternalTable, 6721cb0ef41Sopenharmony_ci index: this.index}); 6731cb0ef41Sopenharmony_ci return this; 6741cb0ef41Sopenharmony_ci } 6751cb0ef41Sopenharmony_ci} 6761cb0ef41Sopenharmony_ci 6771cb0ef41Sopenharmony_ciclass WasmModuleBuilder { 6781cb0ef41Sopenharmony_ci constructor() { 6791cb0ef41Sopenharmony_ci this.types = []; 6801cb0ef41Sopenharmony_ci this.imports = []; 6811cb0ef41Sopenharmony_ci this.exports = []; 6821cb0ef41Sopenharmony_ci this.globals = []; 6831cb0ef41Sopenharmony_ci this.tables = []; 6841cb0ef41Sopenharmony_ci this.tags = []; 6851cb0ef41Sopenharmony_ci this.functions = []; 6861cb0ef41Sopenharmony_ci this.element_segments = []; 6871cb0ef41Sopenharmony_ci this.data_segments = []; 6881cb0ef41Sopenharmony_ci this.explicit = []; 6891cb0ef41Sopenharmony_ci this.num_imported_funcs = 0; 6901cb0ef41Sopenharmony_ci this.num_imported_globals = 0; 6911cb0ef41Sopenharmony_ci this.num_imported_tables = 0; 6921cb0ef41Sopenharmony_ci this.num_imported_tags = 0; 6931cb0ef41Sopenharmony_ci return this; 6941cb0ef41Sopenharmony_ci } 6951cb0ef41Sopenharmony_ci 6961cb0ef41Sopenharmony_ci addStart(start_index) { 6971cb0ef41Sopenharmony_ci this.start_index = start_index; 6981cb0ef41Sopenharmony_ci return this; 6991cb0ef41Sopenharmony_ci } 7001cb0ef41Sopenharmony_ci 7011cb0ef41Sopenharmony_ci addMemory(min, max, exp, shared) { 7021cb0ef41Sopenharmony_ci this.memory = {min: min, max: max, exp: exp, shared: shared}; 7031cb0ef41Sopenharmony_ci return this; 7041cb0ef41Sopenharmony_ci } 7051cb0ef41Sopenharmony_ci 7061cb0ef41Sopenharmony_ci addExplicitSection(bytes) { 7071cb0ef41Sopenharmony_ci this.explicit.push(bytes); 7081cb0ef41Sopenharmony_ci return this; 7091cb0ef41Sopenharmony_ci } 7101cb0ef41Sopenharmony_ci 7111cb0ef41Sopenharmony_ci stringToBytes(name) { 7121cb0ef41Sopenharmony_ci var result = new Binary(); 7131cb0ef41Sopenharmony_ci result.emit_string(name); 7141cb0ef41Sopenharmony_ci return result.trunc_buffer() 7151cb0ef41Sopenharmony_ci } 7161cb0ef41Sopenharmony_ci 7171cb0ef41Sopenharmony_ci createCustomSection(name, bytes) { 7181cb0ef41Sopenharmony_ci name = this.stringToBytes(name); 7191cb0ef41Sopenharmony_ci var section = new Binary(); 7201cb0ef41Sopenharmony_ci section.emit_u8(kUnknownSectionCode); 7211cb0ef41Sopenharmony_ci section.emit_u32v(name.length + bytes.length); 7221cb0ef41Sopenharmony_ci section.emit_bytes(name); 7231cb0ef41Sopenharmony_ci section.emit_bytes(bytes); 7241cb0ef41Sopenharmony_ci return section.trunc_buffer(); 7251cb0ef41Sopenharmony_ci } 7261cb0ef41Sopenharmony_ci 7271cb0ef41Sopenharmony_ci addCustomSection(name, bytes) { 7281cb0ef41Sopenharmony_ci this.explicit.push(this.createCustomSection(name, bytes)); 7291cb0ef41Sopenharmony_ci } 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ci addType(type) { 7321cb0ef41Sopenharmony_ci this.types.push(type); 7331cb0ef41Sopenharmony_ci var pl = type.params.length; // should have params 7341cb0ef41Sopenharmony_ci var rl = type.results.length; // should have results 7351cb0ef41Sopenharmony_ci return this.types.length - 1; 7361cb0ef41Sopenharmony_ci } 7371cb0ef41Sopenharmony_ci 7381cb0ef41Sopenharmony_ci addGlobal(local_type, mutable) { 7391cb0ef41Sopenharmony_ci let glob = new WasmGlobalBuilder(this, local_type, mutable); 7401cb0ef41Sopenharmony_ci glob.index = this.globals.length + this.num_imported_globals; 7411cb0ef41Sopenharmony_ci this.globals.push(glob); 7421cb0ef41Sopenharmony_ci return glob; 7431cb0ef41Sopenharmony_ci } 7441cb0ef41Sopenharmony_ci 7451cb0ef41Sopenharmony_ci addTable(type, initial_size, max_size = undefined) { 7461cb0ef41Sopenharmony_ci if (type != kWasmAnyRef && type != kWasmAnyFunc) { 7471cb0ef41Sopenharmony_ci throw new Error('Tables must be of type kWasmAnyRef or kWasmAnyFunc'); 7481cb0ef41Sopenharmony_ci } 7491cb0ef41Sopenharmony_ci let table = new WasmTableBuilder(this, type, initial_size, max_size); 7501cb0ef41Sopenharmony_ci table.index = this.tables.length + this.num_imported_tables; 7511cb0ef41Sopenharmony_ci this.tables.push(table); 7521cb0ef41Sopenharmony_ci return table; 7531cb0ef41Sopenharmony_ci } 7541cb0ef41Sopenharmony_ci 7551cb0ef41Sopenharmony_ci addTag(type) { 7561cb0ef41Sopenharmony_ci let type_index = (typeof type) == "number" ? type : this.addType(type); 7571cb0ef41Sopenharmony_ci let except_index = this.tags.length + this.num_imported_tags; 7581cb0ef41Sopenharmony_ci this.tags.push(type_index); 7591cb0ef41Sopenharmony_ci return except_index; 7601cb0ef41Sopenharmony_ci } 7611cb0ef41Sopenharmony_ci 7621cb0ef41Sopenharmony_ci addFunction(name, type) { 7631cb0ef41Sopenharmony_ci let type_index = (typeof type) == "number" ? type : this.addType(type); 7641cb0ef41Sopenharmony_ci let func = new WasmFunctionBuilder(this, name, type_index); 7651cb0ef41Sopenharmony_ci func.index = this.functions.length + this.num_imported_funcs; 7661cb0ef41Sopenharmony_ci this.functions.push(func); 7671cb0ef41Sopenharmony_ci return func; 7681cb0ef41Sopenharmony_ci } 7691cb0ef41Sopenharmony_ci 7701cb0ef41Sopenharmony_ci addImport(module, name, type) { 7711cb0ef41Sopenharmony_ci if (this.functions.length != 0) { 7721cb0ef41Sopenharmony_ci throw new Error('Imported functions must be declared before local ones'); 7731cb0ef41Sopenharmony_ci } 7741cb0ef41Sopenharmony_ci let type_index = (typeof type) == "number" ? type : this.addType(type); 7751cb0ef41Sopenharmony_ci this.imports.push({module: module, name: name, kind: kExternalFunction, 7761cb0ef41Sopenharmony_ci type: type_index}); 7771cb0ef41Sopenharmony_ci return this.num_imported_funcs++; 7781cb0ef41Sopenharmony_ci } 7791cb0ef41Sopenharmony_ci 7801cb0ef41Sopenharmony_ci addImportedGlobal(module, name, type, mutable = false) { 7811cb0ef41Sopenharmony_ci if (this.globals.length != 0) { 7821cb0ef41Sopenharmony_ci throw new Error('Imported globals must be declared before local ones'); 7831cb0ef41Sopenharmony_ci } 7841cb0ef41Sopenharmony_ci let o = {module: module, name: name, kind: kExternalGlobal, type: type, 7851cb0ef41Sopenharmony_ci mutable: mutable}; 7861cb0ef41Sopenharmony_ci this.imports.push(o); 7871cb0ef41Sopenharmony_ci return this.num_imported_globals++; 7881cb0ef41Sopenharmony_ci } 7891cb0ef41Sopenharmony_ci 7901cb0ef41Sopenharmony_ci addImportedMemory(module, name, initial = 0, maximum, shared) { 7911cb0ef41Sopenharmony_ci let o = {module: module, name: name, kind: kExternalMemory, 7921cb0ef41Sopenharmony_ci initial: initial, maximum: maximum, shared: shared}; 7931cb0ef41Sopenharmony_ci this.imports.push(o); 7941cb0ef41Sopenharmony_ci return this; 7951cb0ef41Sopenharmony_ci } 7961cb0ef41Sopenharmony_ci 7971cb0ef41Sopenharmony_ci addImportedTable(module, name, initial, maximum, type) { 7981cb0ef41Sopenharmony_ci if (this.tables.length != 0) { 7991cb0ef41Sopenharmony_ci throw new Error('Imported tables must be declared before local ones'); 8001cb0ef41Sopenharmony_ci } 8011cb0ef41Sopenharmony_ci let o = {module: module, name: name, kind: kExternalTable, initial: initial, 8021cb0ef41Sopenharmony_ci maximum: maximum, type: type || kWasmAnyFunctionTypeForm}; 8031cb0ef41Sopenharmony_ci this.imports.push(o); 8041cb0ef41Sopenharmony_ci return this.num_imported_tables++; 8051cb0ef41Sopenharmony_ci } 8061cb0ef41Sopenharmony_ci 8071cb0ef41Sopenharmony_ci addImportedTag(module, name, type) { 8081cb0ef41Sopenharmony_ci if (this.tags.length != 0) { 8091cb0ef41Sopenharmony_ci throw new Error('Imported tags must be declared before local ones'); 8101cb0ef41Sopenharmony_ci } 8111cb0ef41Sopenharmony_ci let type_index = (typeof type) == "number" ? type : this.addType(type); 8121cb0ef41Sopenharmony_ci let o = {module: module, name: name, kind: kExternalTag, type: type_index}; 8131cb0ef41Sopenharmony_ci this.imports.push(o); 8141cb0ef41Sopenharmony_ci return this.num_imported_tags++; 8151cb0ef41Sopenharmony_ci } 8161cb0ef41Sopenharmony_ci 8171cb0ef41Sopenharmony_ci addExport(name, index) { 8181cb0ef41Sopenharmony_ci this.exports.push({name: name, kind: kExternalFunction, index: index}); 8191cb0ef41Sopenharmony_ci return this; 8201cb0ef41Sopenharmony_ci } 8211cb0ef41Sopenharmony_ci 8221cb0ef41Sopenharmony_ci addExportOfKind(name, kind, index) { 8231cb0ef41Sopenharmony_ci this.exports.push({name: name, kind: kind, index: index}); 8241cb0ef41Sopenharmony_ci return this; 8251cb0ef41Sopenharmony_ci } 8261cb0ef41Sopenharmony_ci 8271cb0ef41Sopenharmony_ci addDataSegment(addr, data, is_global = false) { 8281cb0ef41Sopenharmony_ci this.data_segments.push( 8291cb0ef41Sopenharmony_ci {addr: addr, data: data, is_global: is_global, is_active: true}); 8301cb0ef41Sopenharmony_ci return this.data_segments.length - 1; 8311cb0ef41Sopenharmony_ci } 8321cb0ef41Sopenharmony_ci 8331cb0ef41Sopenharmony_ci addPassiveDataSegment(data) { 8341cb0ef41Sopenharmony_ci this.data_segments.push({data: data, is_active: false}); 8351cb0ef41Sopenharmony_ci return this.data_segments.length - 1; 8361cb0ef41Sopenharmony_ci } 8371cb0ef41Sopenharmony_ci 8381cb0ef41Sopenharmony_ci exportMemoryAs(name) { 8391cb0ef41Sopenharmony_ci this.exports.push({name: name, kind: kExternalMemory, index: 0}); 8401cb0ef41Sopenharmony_ci } 8411cb0ef41Sopenharmony_ci 8421cb0ef41Sopenharmony_ci addElementSegment(table, base, is_global, array) { 8431cb0ef41Sopenharmony_ci this.element_segments.push({table: table, base: base, is_global: is_global, 8441cb0ef41Sopenharmony_ci array: array, is_active: true}); 8451cb0ef41Sopenharmony_ci return this; 8461cb0ef41Sopenharmony_ci } 8471cb0ef41Sopenharmony_ci 8481cb0ef41Sopenharmony_ci addPassiveElementSegment(array, is_import = false) { 8491cb0ef41Sopenharmony_ci this.element_segments.push({array: array, is_active: false}); 8501cb0ef41Sopenharmony_ci return this; 8511cb0ef41Sopenharmony_ci } 8521cb0ef41Sopenharmony_ci 8531cb0ef41Sopenharmony_ci appendToTable(array) { 8541cb0ef41Sopenharmony_ci for (let n of array) { 8551cb0ef41Sopenharmony_ci if (typeof n != 'number') 8561cb0ef41Sopenharmony_ci throw new Error('invalid table (entries have to be numbers): ' + array); 8571cb0ef41Sopenharmony_ci } 8581cb0ef41Sopenharmony_ci if (this.tables.length == 0) { 8591cb0ef41Sopenharmony_ci this.addTable(kWasmAnyFunc, 0); 8601cb0ef41Sopenharmony_ci } 8611cb0ef41Sopenharmony_ci // Adjust the table to the correct size. 8621cb0ef41Sopenharmony_ci let table = this.tables[0]; 8631cb0ef41Sopenharmony_ci const base = table.initial_size; 8641cb0ef41Sopenharmony_ci const table_size = base + array.length; 8651cb0ef41Sopenharmony_ci table.initial_size = table_size; 8661cb0ef41Sopenharmony_ci if (table.has_max && table_size > table.max_size) { 8671cb0ef41Sopenharmony_ci table.max_size = table_size; 8681cb0ef41Sopenharmony_ci } 8691cb0ef41Sopenharmony_ci return this.addElementSegment(0, base, false, array); 8701cb0ef41Sopenharmony_ci } 8711cb0ef41Sopenharmony_ci 8721cb0ef41Sopenharmony_ci setTableBounds(min, max = undefined) { 8731cb0ef41Sopenharmony_ci if (this.tables.length != 0) { 8741cb0ef41Sopenharmony_ci throw new Error("The table bounds of table '0' have already been set."); 8751cb0ef41Sopenharmony_ci } 8761cb0ef41Sopenharmony_ci this.addTable(kWasmAnyFunc, min, max); 8771cb0ef41Sopenharmony_ci return this; 8781cb0ef41Sopenharmony_ci } 8791cb0ef41Sopenharmony_ci 8801cb0ef41Sopenharmony_ci setName(name) { 8811cb0ef41Sopenharmony_ci this.name = name; 8821cb0ef41Sopenharmony_ci return this; 8831cb0ef41Sopenharmony_ci } 8841cb0ef41Sopenharmony_ci 8851cb0ef41Sopenharmony_ci toBuffer(debug = false) { 8861cb0ef41Sopenharmony_ci let binary = new Binary; 8871cb0ef41Sopenharmony_ci let wasm = this; 8881cb0ef41Sopenharmony_ci 8891cb0ef41Sopenharmony_ci // Add header 8901cb0ef41Sopenharmony_ci binary.emit_header(); 8911cb0ef41Sopenharmony_ci 8921cb0ef41Sopenharmony_ci // Add type section 8931cb0ef41Sopenharmony_ci if (wasm.types.length > 0) { 8941cb0ef41Sopenharmony_ci if (debug) print("emitting types @ " + binary.length); 8951cb0ef41Sopenharmony_ci binary.emit_section(kTypeSectionCode, section => { 8961cb0ef41Sopenharmony_ci section.emit_u32v(wasm.types.length); 8971cb0ef41Sopenharmony_ci for (let type of wasm.types) { 8981cb0ef41Sopenharmony_ci section.emit_u8(kWasmFunctionTypeForm); 8991cb0ef41Sopenharmony_ci section.emit_u32v(type.params.length); 9001cb0ef41Sopenharmony_ci for (let param of type.params) { 9011cb0ef41Sopenharmony_ci section.emit_u8(param); 9021cb0ef41Sopenharmony_ci } 9031cb0ef41Sopenharmony_ci section.emit_u32v(type.results.length); 9041cb0ef41Sopenharmony_ci for (let result of type.results) { 9051cb0ef41Sopenharmony_ci section.emit_u8(result); 9061cb0ef41Sopenharmony_ci } 9071cb0ef41Sopenharmony_ci } 9081cb0ef41Sopenharmony_ci }); 9091cb0ef41Sopenharmony_ci } 9101cb0ef41Sopenharmony_ci 9111cb0ef41Sopenharmony_ci // Add imports section 9121cb0ef41Sopenharmony_ci if (wasm.imports.length > 0) { 9131cb0ef41Sopenharmony_ci if (debug) print("emitting imports @ " + binary.length); 9141cb0ef41Sopenharmony_ci binary.emit_section(kImportSectionCode, section => { 9151cb0ef41Sopenharmony_ci section.emit_u32v(wasm.imports.length); 9161cb0ef41Sopenharmony_ci for (let imp of wasm.imports) { 9171cb0ef41Sopenharmony_ci section.emit_string(imp.module); 9181cb0ef41Sopenharmony_ci section.emit_string(imp.name || ''); 9191cb0ef41Sopenharmony_ci section.emit_u8(imp.kind); 9201cb0ef41Sopenharmony_ci if (imp.kind == kExternalFunction) { 9211cb0ef41Sopenharmony_ci section.emit_u32v(imp.type); 9221cb0ef41Sopenharmony_ci } else if (imp.kind == kExternalGlobal) { 9231cb0ef41Sopenharmony_ci section.emit_u32v(imp.type); 9241cb0ef41Sopenharmony_ci section.emit_u8(imp.mutable); 9251cb0ef41Sopenharmony_ci } else if (imp.kind == kExternalMemory) { 9261cb0ef41Sopenharmony_ci var has_max = (typeof imp.maximum) != "undefined"; 9271cb0ef41Sopenharmony_ci var is_shared = (typeof imp.shared) != "undefined"; 9281cb0ef41Sopenharmony_ci if (is_shared) { 9291cb0ef41Sopenharmony_ci section.emit_u8(has_max ? 3 : 2); // flags 9301cb0ef41Sopenharmony_ci } else { 9311cb0ef41Sopenharmony_ci section.emit_u8(has_max ? 1 : 0); // flags 9321cb0ef41Sopenharmony_ci } 9331cb0ef41Sopenharmony_ci section.emit_u32v(imp.initial); // initial 9341cb0ef41Sopenharmony_ci if (has_max) section.emit_u32v(imp.maximum); // maximum 9351cb0ef41Sopenharmony_ci } else if (imp.kind == kExternalTable) { 9361cb0ef41Sopenharmony_ci section.emit_u8(imp.type); 9371cb0ef41Sopenharmony_ci var has_max = (typeof imp.maximum) != "undefined"; 9381cb0ef41Sopenharmony_ci section.emit_u8(has_max ? 1 : 0); // flags 9391cb0ef41Sopenharmony_ci section.emit_u32v(imp.initial); // initial 9401cb0ef41Sopenharmony_ci if (has_max) section.emit_u32v(imp.maximum); // maximum 9411cb0ef41Sopenharmony_ci } else if (imp.kind == kExternalTag) { 9421cb0ef41Sopenharmony_ci section.emit_u32v(kTagAttribute); 9431cb0ef41Sopenharmony_ci section.emit_u32v(imp.type); 9441cb0ef41Sopenharmony_ci } else { 9451cb0ef41Sopenharmony_ci throw new Error("unknown/unsupported import kind " + imp.kind); 9461cb0ef41Sopenharmony_ci } 9471cb0ef41Sopenharmony_ci } 9481cb0ef41Sopenharmony_ci }); 9491cb0ef41Sopenharmony_ci } 9501cb0ef41Sopenharmony_ci 9511cb0ef41Sopenharmony_ci // Add functions declarations 9521cb0ef41Sopenharmony_ci if (wasm.functions.length > 0) { 9531cb0ef41Sopenharmony_ci if (debug) print("emitting function decls @ " + binary.length); 9541cb0ef41Sopenharmony_ci binary.emit_section(kFunctionSectionCode, section => { 9551cb0ef41Sopenharmony_ci section.emit_u32v(wasm.functions.length); 9561cb0ef41Sopenharmony_ci for (let func of wasm.functions) { 9571cb0ef41Sopenharmony_ci section.emit_u32v(func.type_index); 9581cb0ef41Sopenharmony_ci } 9591cb0ef41Sopenharmony_ci }); 9601cb0ef41Sopenharmony_ci } 9611cb0ef41Sopenharmony_ci 9621cb0ef41Sopenharmony_ci // Add table section 9631cb0ef41Sopenharmony_ci if (wasm.tables.length > 0) { 9641cb0ef41Sopenharmony_ci if (debug) print ("emitting tables @ " + binary.length); 9651cb0ef41Sopenharmony_ci binary.emit_section(kTableSectionCode, section => { 9661cb0ef41Sopenharmony_ci section.emit_u32v(wasm.tables.length); 9671cb0ef41Sopenharmony_ci for (let table of wasm.tables) { 9681cb0ef41Sopenharmony_ci section.emit_u8(table.type); 9691cb0ef41Sopenharmony_ci section.emit_u8(table.has_max); 9701cb0ef41Sopenharmony_ci section.emit_u32v(table.initial_size); 9711cb0ef41Sopenharmony_ci if (table.has_max) section.emit_u32v(table.max_size); 9721cb0ef41Sopenharmony_ci } 9731cb0ef41Sopenharmony_ci }); 9741cb0ef41Sopenharmony_ci } 9751cb0ef41Sopenharmony_ci 9761cb0ef41Sopenharmony_ci // Add memory section 9771cb0ef41Sopenharmony_ci if (wasm.memory !== undefined) { 9781cb0ef41Sopenharmony_ci if (debug) print("emitting memory @ " + binary.length); 9791cb0ef41Sopenharmony_ci binary.emit_section(kMemorySectionCode, section => { 9801cb0ef41Sopenharmony_ci section.emit_u8(1); // one memory entry 9811cb0ef41Sopenharmony_ci const has_max = wasm.memory.max !== undefined; 9821cb0ef41Sopenharmony_ci const is_shared = wasm.memory.shared !== undefined; 9831cb0ef41Sopenharmony_ci // Emit flags (bit 0: reszeable max, bit 1: shared memory) 9841cb0ef41Sopenharmony_ci if (is_shared) { 9851cb0ef41Sopenharmony_ci section.emit_u8(has_max ? kSharedHasMaximumFlag : 2); 9861cb0ef41Sopenharmony_ci } else { 9871cb0ef41Sopenharmony_ci section.emit_u8(has_max ? kHasMaximumFlag : 0); 9881cb0ef41Sopenharmony_ci } 9891cb0ef41Sopenharmony_ci section.emit_u32v(wasm.memory.min); 9901cb0ef41Sopenharmony_ci if (has_max) section.emit_u32v(wasm.memory.max); 9911cb0ef41Sopenharmony_ci }); 9921cb0ef41Sopenharmony_ci } 9931cb0ef41Sopenharmony_ci 9941cb0ef41Sopenharmony_ci // Add global section. 9951cb0ef41Sopenharmony_ci if (wasm.globals.length > 0) { 9961cb0ef41Sopenharmony_ci if (debug) print ("emitting globals @ " + binary.length); 9971cb0ef41Sopenharmony_ci binary.emit_section(kGlobalSectionCode, section => { 9981cb0ef41Sopenharmony_ci section.emit_u32v(wasm.globals.length); 9991cb0ef41Sopenharmony_ci for (let global of wasm.globals) { 10001cb0ef41Sopenharmony_ci section.emit_u8(global.type); 10011cb0ef41Sopenharmony_ci section.emit_u8(global.mutable); 10021cb0ef41Sopenharmony_ci if ((typeof global.init_index) == "undefined") { 10031cb0ef41Sopenharmony_ci // Emit a constant initializer. 10041cb0ef41Sopenharmony_ci switch (global.type) { 10051cb0ef41Sopenharmony_ci case kWasmI32: 10061cb0ef41Sopenharmony_ci section.emit_u8(kExprI32Const); 10071cb0ef41Sopenharmony_ci section.emit_u32v(global.init); 10081cb0ef41Sopenharmony_ci break; 10091cb0ef41Sopenharmony_ci case kWasmI64: 10101cb0ef41Sopenharmony_ci section.emit_u8(kExprI64Const); 10111cb0ef41Sopenharmony_ci section.emit_u64v(global.init); 10121cb0ef41Sopenharmony_ci break; 10131cb0ef41Sopenharmony_ci case kWasmF32: 10141cb0ef41Sopenharmony_ci section.emit_bytes(wasmF32Const(global.init)); 10151cb0ef41Sopenharmony_ci break; 10161cb0ef41Sopenharmony_ci case kWasmF64: 10171cb0ef41Sopenharmony_ci section.emit_bytes(wasmF64Const(global.init)); 10181cb0ef41Sopenharmony_ci break; 10191cb0ef41Sopenharmony_ci case kWasmAnyFunc: 10201cb0ef41Sopenharmony_ci case kWasmAnyRef: 10211cb0ef41Sopenharmony_ci if (global.function_index !== undefined) { 10221cb0ef41Sopenharmony_ci section.emit_u8(kExprRefFunc); 10231cb0ef41Sopenharmony_ci section.emit_u32v(global.function_index); 10241cb0ef41Sopenharmony_ci } else { 10251cb0ef41Sopenharmony_ci section.emit_u8(kExprRefNull); 10261cb0ef41Sopenharmony_ci } 10271cb0ef41Sopenharmony_ci break; 10281cb0ef41Sopenharmony_ci } 10291cb0ef41Sopenharmony_ci } else { 10301cb0ef41Sopenharmony_ci // Emit a global-index initializer. 10311cb0ef41Sopenharmony_ci section.emit_u8(kExprGlobalGet); 10321cb0ef41Sopenharmony_ci section.emit_u32v(global.init_index); 10331cb0ef41Sopenharmony_ci } 10341cb0ef41Sopenharmony_ci section.emit_u8(kExprEnd); // end of init expression 10351cb0ef41Sopenharmony_ci } 10361cb0ef41Sopenharmony_ci }); 10371cb0ef41Sopenharmony_ci } 10381cb0ef41Sopenharmony_ci 10391cb0ef41Sopenharmony_ci // Add tags. 10401cb0ef41Sopenharmony_ci if (wasm.tags.length > 0) { 10411cb0ef41Sopenharmony_ci if (debug) print("emitting tags @ " + binary.length); 10421cb0ef41Sopenharmony_ci binary.emit_section(kTagSectionCode, section => { 10431cb0ef41Sopenharmony_ci section.emit_u32v(wasm.tags.length); 10441cb0ef41Sopenharmony_ci for (let type of wasm.tags) { 10451cb0ef41Sopenharmony_ci section.emit_u32v(kTagAttribute); 10461cb0ef41Sopenharmony_ci section.emit_u32v(type); 10471cb0ef41Sopenharmony_ci } 10481cb0ef41Sopenharmony_ci }); 10491cb0ef41Sopenharmony_ci } 10501cb0ef41Sopenharmony_ci 10511cb0ef41Sopenharmony_ci // Add export table. 10521cb0ef41Sopenharmony_ci var mem_export = (wasm.memory !== undefined && wasm.memory.exp); 10531cb0ef41Sopenharmony_ci var exports_count = wasm.exports.length + (mem_export ? 1 : 0); 10541cb0ef41Sopenharmony_ci if (exports_count > 0) { 10551cb0ef41Sopenharmony_ci if (debug) print("emitting exports @ " + binary.length); 10561cb0ef41Sopenharmony_ci binary.emit_section(kExportSectionCode, section => { 10571cb0ef41Sopenharmony_ci section.emit_u32v(exports_count); 10581cb0ef41Sopenharmony_ci for (let exp of wasm.exports) { 10591cb0ef41Sopenharmony_ci section.emit_string(exp.name); 10601cb0ef41Sopenharmony_ci section.emit_u8(exp.kind); 10611cb0ef41Sopenharmony_ci section.emit_u32v(exp.index); 10621cb0ef41Sopenharmony_ci } 10631cb0ef41Sopenharmony_ci if (mem_export) { 10641cb0ef41Sopenharmony_ci section.emit_string("memory"); 10651cb0ef41Sopenharmony_ci section.emit_u8(kExternalMemory); 10661cb0ef41Sopenharmony_ci section.emit_u8(0); 10671cb0ef41Sopenharmony_ci } 10681cb0ef41Sopenharmony_ci }); 10691cb0ef41Sopenharmony_ci } 10701cb0ef41Sopenharmony_ci 10711cb0ef41Sopenharmony_ci // Add start function section. 10721cb0ef41Sopenharmony_ci if (wasm.start_index !== undefined) { 10731cb0ef41Sopenharmony_ci if (debug) print("emitting start function @ " + binary.length); 10741cb0ef41Sopenharmony_ci binary.emit_section(kStartSectionCode, section => { 10751cb0ef41Sopenharmony_ci section.emit_u32v(wasm.start_index); 10761cb0ef41Sopenharmony_ci }); 10771cb0ef41Sopenharmony_ci } 10781cb0ef41Sopenharmony_ci 10791cb0ef41Sopenharmony_ci // Add element segments 10801cb0ef41Sopenharmony_ci if (wasm.element_segments.length > 0) { 10811cb0ef41Sopenharmony_ci if (debug) print("emitting element segments @ " + binary.length); 10821cb0ef41Sopenharmony_ci binary.emit_section(kElementSectionCode, section => { 10831cb0ef41Sopenharmony_ci var inits = wasm.element_segments; 10841cb0ef41Sopenharmony_ci section.emit_u32v(inits.length); 10851cb0ef41Sopenharmony_ci 10861cb0ef41Sopenharmony_ci for (let init of inits) { 10871cb0ef41Sopenharmony_ci if (init.is_active) { 10881cb0ef41Sopenharmony_ci // Active segment. 10891cb0ef41Sopenharmony_ci if (init.table == 0) { 10901cb0ef41Sopenharmony_ci section.emit_u32v(kActiveNoIndex); 10911cb0ef41Sopenharmony_ci } else { 10921cb0ef41Sopenharmony_ci section.emit_u32v(kActiveWithIndex); 10931cb0ef41Sopenharmony_ci section.emit_u32v(init.table); 10941cb0ef41Sopenharmony_ci } 10951cb0ef41Sopenharmony_ci if (init.is_global) { 10961cb0ef41Sopenharmony_ci section.emit_u8(kExprGlobalGet); 10971cb0ef41Sopenharmony_ci } else { 10981cb0ef41Sopenharmony_ci section.emit_u8(kExprI32Const); 10991cb0ef41Sopenharmony_ci } 11001cb0ef41Sopenharmony_ci section.emit_u32v(init.base); 11011cb0ef41Sopenharmony_ci section.emit_u8(kExprEnd); 11021cb0ef41Sopenharmony_ci if (init.table != 0) { 11031cb0ef41Sopenharmony_ci section.emit_u8(kExternalFunction); 11041cb0ef41Sopenharmony_ci } 11051cb0ef41Sopenharmony_ci section.emit_u32v(init.array.length); 11061cb0ef41Sopenharmony_ci for (let index of init.array) { 11071cb0ef41Sopenharmony_ci section.emit_u32v(index); 11081cb0ef41Sopenharmony_ci } 11091cb0ef41Sopenharmony_ci } else { 11101cb0ef41Sopenharmony_ci // Passive segment. 11111cb0ef41Sopenharmony_ci section.emit_u8(kPassiveWithElements); // flags 11121cb0ef41Sopenharmony_ci section.emit_u8(kWasmAnyFunc); 11131cb0ef41Sopenharmony_ci section.emit_u32v(init.array.length); 11141cb0ef41Sopenharmony_ci for (let index of init.array) { 11151cb0ef41Sopenharmony_ci if (index === null) { 11161cb0ef41Sopenharmony_ci section.emit_u8(kExprRefNull); 11171cb0ef41Sopenharmony_ci section.emit_u8(kExprEnd); 11181cb0ef41Sopenharmony_ci } else { 11191cb0ef41Sopenharmony_ci section.emit_u8(kExprRefFunc); 11201cb0ef41Sopenharmony_ci section.emit_u32v(index); 11211cb0ef41Sopenharmony_ci section.emit_u8(kExprEnd); 11221cb0ef41Sopenharmony_ci } 11231cb0ef41Sopenharmony_ci } 11241cb0ef41Sopenharmony_ci } 11251cb0ef41Sopenharmony_ci } 11261cb0ef41Sopenharmony_ci }); 11271cb0ef41Sopenharmony_ci } 11281cb0ef41Sopenharmony_ci 11291cb0ef41Sopenharmony_ci // If there are any passive data segments, add the DataCount section. 11301cb0ef41Sopenharmony_ci if (wasm.data_segments.some(seg => !seg.is_active)) { 11311cb0ef41Sopenharmony_ci binary.emit_section(kDataCountSectionCode, section => { 11321cb0ef41Sopenharmony_ci section.emit_u32v(wasm.data_segments.length); 11331cb0ef41Sopenharmony_ci }); 11341cb0ef41Sopenharmony_ci } 11351cb0ef41Sopenharmony_ci 11361cb0ef41Sopenharmony_ci // Add function bodies. 11371cb0ef41Sopenharmony_ci if (wasm.functions.length > 0) { 11381cb0ef41Sopenharmony_ci // emit function bodies 11391cb0ef41Sopenharmony_ci if (debug) print("emitting code @ " + binary.length); 11401cb0ef41Sopenharmony_ci binary.emit_section(kCodeSectionCode, section => { 11411cb0ef41Sopenharmony_ci section.emit_u32v(wasm.functions.length); 11421cb0ef41Sopenharmony_ci let header = new Binary; 11431cb0ef41Sopenharmony_ci for (let func of wasm.functions) { 11441cb0ef41Sopenharmony_ci header.reset(); 11451cb0ef41Sopenharmony_ci // Function body length will be patched later. 11461cb0ef41Sopenharmony_ci let local_decls = []; 11471cb0ef41Sopenharmony_ci for (let l of func.locals || []) { 11481cb0ef41Sopenharmony_ci if (l.i32_count > 0) { 11491cb0ef41Sopenharmony_ci local_decls.push({count: l.i32_count, type: kWasmI32}); 11501cb0ef41Sopenharmony_ci } 11511cb0ef41Sopenharmony_ci if (l.i64_count > 0) { 11521cb0ef41Sopenharmony_ci local_decls.push({count: l.i64_count, type: kWasmI64}); 11531cb0ef41Sopenharmony_ci } 11541cb0ef41Sopenharmony_ci if (l.f32_count > 0) { 11551cb0ef41Sopenharmony_ci local_decls.push({count: l.f32_count, type: kWasmF32}); 11561cb0ef41Sopenharmony_ci } 11571cb0ef41Sopenharmony_ci if (l.f64_count > 0) { 11581cb0ef41Sopenharmony_ci local_decls.push({count: l.f64_count, type: kWasmF64}); 11591cb0ef41Sopenharmony_ci } 11601cb0ef41Sopenharmony_ci if (l.s128_count > 0) { 11611cb0ef41Sopenharmony_ci local_decls.push({count: l.s128_count, type: kWasmS128}); 11621cb0ef41Sopenharmony_ci } 11631cb0ef41Sopenharmony_ci if (l.anyref_count > 0) { 11641cb0ef41Sopenharmony_ci local_decls.push({count: l.anyref_count, type: kWasmAnyRef}); 11651cb0ef41Sopenharmony_ci } 11661cb0ef41Sopenharmony_ci if (l.anyfunc_count > 0) { 11671cb0ef41Sopenharmony_ci local_decls.push({count: l.anyfunc_count, type: kWasmAnyFunc}); 11681cb0ef41Sopenharmony_ci } 11691cb0ef41Sopenharmony_ci } 11701cb0ef41Sopenharmony_ci 11711cb0ef41Sopenharmony_ci header.emit_u32v(local_decls.length); 11721cb0ef41Sopenharmony_ci for (let decl of local_decls) { 11731cb0ef41Sopenharmony_ci header.emit_u32v(decl.count); 11741cb0ef41Sopenharmony_ci header.emit_u8(decl.type); 11751cb0ef41Sopenharmony_ci } 11761cb0ef41Sopenharmony_ci 11771cb0ef41Sopenharmony_ci section.emit_u32v(header.length + func.body.length); 11781cb0ef41Sopenharmony_ci section.emit_bytes(header.trunc_buffer()); 11791cb0ef41Sopenharmony_ci section.emit_bytes(func.body); 11801cb0ef41Sopenharmony_ci } 11811cb0ef41Sopenharmony_ci }); 11821cb0ef41Sopenharmony_ci } 11831cb0ef41Sopenharmony_ci 11841cb0ef41Sopenharmony_ci // Add data segments. 11851cb0ef41Sopenharmony_ci if (wasm.data_segments.length > 0) { 11861cb0ef41Sopenharmony_ci if (debug) print("emitting data segments @ " + binary.length); 11871cb0ef41Sopenharmony_ci binary.emit_section(kDataSectionCode, section => { 11881cb0ef41Sopenharmony_ci section.emit_u32v(wasm.data_segments.length); 11891cb0ef41Sopenharmony_ci for (let seg of wasm.data_segments) { 11901cb0ef41Sopenharmony_ci if (seg.is_active) { 11911cb0ef41Sopenharmony_ci section.emit_u8(0); // linear memory index 0 / flags 11921cb0ef41Sopenharmony_ci if (seg.is_global) { 11931cb0ef41Sopenharmony_ci // initializer is a global variable 11941cb0ef41Sopenharmony_ci section.emit_u8(kExprGlobalGet); 11951cb0ef41Sopenharmony_ci section.emit_u32v(seg.addr); 11961cb0ef41Sopenharmony_ci } else { 11971cb0ef41Sopenharmony_ci // initializer is a constant 11981cb0ef41Sopenharmony_ci section.emit_u8(kExprI32Const); 11991cb0ef41Sopenharmony_ci section.emit_u32v(seg.addr); 12001cb0ef41Sopenharmony_ci } 12011cb0ef41Sopenharmony_ci section.emit_u8(kExprEnd); 12021cb0ef41Sopenharmony_ci } else { 12031cb0ef41Sopenharmony_ci section.emit_u8(kPassive); // flags 12041cb0ef41Sopenharmony_ci } 12051cb0ef41Sopenharmony_ci section.emit_u32v(seg.data.length); 12061cb0ef41Sopenharmony_ci section.emit_bytes(seg.data); 12071cb0ef41Sopenharmony_ci } 12081cb0ef41Sopenharmony_ci }); 12091cb0ef41Sopenharmony_ci } 12101cb0ef41Sopenharmony_ci 12111cb0ef41Sopenharmony_ci // Add any explicitly added sections 12121cb0ef41Sopenharmony_ci for (let exp of wasm.explicit) { 12131cb0ef41Sopenharmony_ci if (debug) print("emitting explicit @ " + binary.length); 12141cb0ef41Sopenharmony_ci binary.emit_bytes(exp); 12151cb0ef41Sopenharmony_ci } 12161cb0ef41Sopenharmony_ci 12171cb0ef41Sopenharmony_ci // Add names. 12181cb0ef41Sopenharmony_ci let num_function_names = 0; 12191cb0ef41Sopenharmony_ci let num_functions_with_local_names = 0; 12201cb0ef41Sopenharmony_ci for (let func of wasm.functions) { 12211cb0ef41Sopenharmony_ci if (func.name !== undefined) ++num_function_names; 12221cb0ef41Sopenharmony_ci if (func.numLocalNames() > 0) ++num_functions_with_local_names; 12231cb0ef41Sopenharmony_ci } 12241cb0ef41Sopenharmony_ci if (num_function_names > 0 || num_functions_with_local_names > 0 || 12251cb0ef41Sopenharmony_ci wasm.name !== undefined) { 12261cb0ef41Sopenharmony_ci if (debug) print('emitting names @ ' + binary.length); 12271cb0ef41Sopenharmony_ci binary.emit_section(kUnknownSectionCode, section => { 12281cb0ef41Sopenharmony_ci section.emit_string('name'); 12291cb0ef41Sopenharmony_ci // Emit module name. 12301cb0ef41Sopenharmony_ci if (wasm.name !== undefined) { 12311cb0ef41Sopenharmony_ci section.emit_section(kModuleNameCode, name_section => { 12321cb0ef41Sopenharmony_ci name_section.emit_string(wasm.name); 12331cb0ef41Sopenharmony_ci }); 12341cb0ef41Sopenharmony_ci } 12351cb0ef41Sopenharmony_ci // Emit function names. 12361cb0ef41Sopenharmony_ci if (num_function_names > 0) { 12371cb0ef41Sopenharmony_ci section.emit_section(kFunctionNamesCode, name_section => { 12381cb0ef41Sopenharmony_ci name_section.emit_u32v(num_function_names); 12391cb0ef41Sopenharmony_ci for (let func of wasm.functions) { 12401cb0ef41Sopenharmony_ci if (func.name === undefined) continue; 12411cb0ef41Sopenharmony_ci name_section.emit_u32v(func.index); 12421cb0ef41Sopenharmony_ci name_section.emit_string(func.name); 12431cb0ef41Sopenharmony_ci } 12441cb0ef41Sopenharmony_ci }); 12451cb0ef41Sopenharmony_ci } 12461cb0ef41Sopenharmony_ci // Emit local names. 12471cb0ef41Sopenharmony_ci if (num_functions_with_local_names > 0) { 12481cb0ef41Sopenharmony_ci section.emit_section(kLocalNamesCode, name_section => { 12491cb0ef41Sopenharmony_ci name_section.emit_u32v(num_functions_with_local_names); 12501cb0ef41Sopenharmony_ci for (let func of wasm.functions) { 12511cb0ef41Sopenharmony_ci if (func.numLocalNames() == 0) continue; 12521cb0ef41Sopenharmony_ci name_section.emit_u32v(func.index); 12531cb0ef41Sopenharmony_ci name_section.emit_u32v(func.numLocalNames()); 12541cb0ef41Sopenharmony_ci for (let i = 0; i < func.local_names.length; ++i) { 12551cb0ef41Sopenharmony_ci if (func.local_names[i] === undefined) continue; 12561cb0ef41Sopenharmony_ci name_section.emit_u32v(i); 12571cb0ef41Sopenharmony_ci name_section.emit_string(func.local_names[i]); 12581cb0ef41Sopenharmony_ci } 12591cb0ef41Sopenharmony_ci } 12601cb0ef41Sopenharmony_ci }); 12611cb0ef41Sopenharmony_ci } 12621cb0ef41Sopenharmony_ci }); 12631cb0ef41Sopenharmony_ci } 12641cb0ef41Sopenharmony_ci 12651cb0ef41Sopenharmony_ci return binary.trunc_buffer(); 12661cb0ef41Sopenharmony_ci } 12671cb0ef41Sopenharmony_ci 12681cb0ef41Sopenharmony_ci toArray(debug = false) { 12691cb0ef41Sopenharmony_ci return Array.from(this.toBuffer(debug)); 12701cb0ef41Sopenharmony_ci } 12711cb0ef41Sopenharmony_ci 12721cb0ef41Sopenharmony_ci instantiate(ffi) { 12731cb0ef41Sopenharmony_ci let module = this.toModule(); 12741cb0ef41Sopenharmony_ci let instance = new WebAssembly.Instance(module, ffi); 12751cb0ef41Sopenharmony_ci return instance; 12761cb0ef41Sopenharmony_ci } 12771cb0ef41Sopenharmony_ci 12781cb0ef41Sopenharmony_ci asyncInstantiate(ffi) { 12791cb0ef41Sopenharmony_ci return WebAssembly.instantiate(this.toBuffer(), ffi) 12801cb0ef41Sopenharmony_ci .then(({module, instance}) => instance); 12811cb0ef41Sopenharmony_ci } 12821cb0ef41Sopenharmony_ci 12831cb0ef41Sopenharmony_ci toModule(debug = false) { 12841cb0ef41Sopenharmony_ci return new WebAssembly.Module(this.toBuffer(debug)); 12851cb0ef41Sopenharmony_ci } 12861cb0ef41Sopenharmony_ci} 12871cb0ef41Sopenharmony_ci 12881cb0ef41Sopenharmony_cifunction wasmSignedLeb(val, max_len = 5) { 12891cb0ef41Sopenharmony_ci let res = []; 12901cb0ef41Sopenharmony_ci for (let i = 0; i < max_len; ++i) { 12911cb0ef41Sopenharmony_ci let v = val & 0x7f; 12921cb0ef41Sopenharmony_ci // If {v} sign-extended from 7 to 32 bits is equal to val, we are done. 12931cb0ef41Sopenharmony_ci if (((v << 25) >> 25) == val) { 12941cb0ef41Sopenharmony_ci res.push(v); 12951cb0ef41Sopenharmony_ci return res; 12961cb0ef41Sopenharmony_ci } 12971cb0ef41Sopenharmony_ci res.push(v | 0x80); 12981cb0ef41Sopenharmony_ci val = val >> 7; 12991cb0ef41Sopenharmony_ci } 13001cb0ef41Sopenharmony_ci throw new Error( 13011cb0ef41Sopenharmony_ci 'Leb value <' + val + '> exceeds maximum length of ' + max_len); 13021cb0ef41Sopenharmony_ci} 13031cb0ef41Sopenharmony_ci 13041cb0ef41Sopenharmony_cifunction wasmI32Const(val) { 13051cb0ef41Sopenharmony_ci return [kExprI32Const, ...wasmSignedLeb(val, 5)]; 13061cb0ef41Sopenharmony_ci} 13071cb0ef41Sopenharmony_ci 13081cb0ef41Sopenharmony_cifunction wasmF32Const(f) { 13091cb0ef41Sopenharmony_ci // Write in little-endian order at offset 0. 13101cb0ef41Sopenharmony_ci data_view.setFloat32(0, f, true); 13111cb0ef41Sopenharmony_ci return [ 13121cb0ef41Sopenharmony_ci kExprF32Const, byte_view[0], byte_view[1], byte_view[2], byte_view[3] 13131cb0ef41Sopenharmony_ci ]; 13141cb0ef41Sopenharmony_ci} 13151cb0ef41Sopenharmony_ci 13161cb0ef41Sopenharmony_cifunction wasmF64Const(f) { 13171cb0ef41Sopenharmony_ci // Write in little-endian order at offset 0. 13181cb0ef41Sopenharmony_ci data_view.setFloat64(0, f, true); 13191cb0ef41Sopenharmony_ci return [ 13201cb0ef41Sopenharmony_ci kExprF64Const, byte_view[0], byte_view[1], byte_view[2], 13211cb0ef41Sopenharmony_ci byte_view[3], byte_view[4], byte_view[5], byte_view[6], byte_view[7] 13221cb0ef41Sopenharmony_ci ]; 13231cb0ef41Sopenharmony_ci} 1324