1// Copyright 2015 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5// Flags: --expose-wasm 6 7function bytes() { 8 var buffer = new ArrayBuffer(arguments.length); 9 var view = new Uint8Array(buffer); 10 for (var i = 0; i < arguments.length; i++) { 11 var val = arguments[i]; 12 if ((typeof val) == "string") val = val.charCodeAt(0); 13 view[i] = val | 0; 14 } 15 return buffer; 16} 17 18// Section declaration constants 19var kDeclMemory = 0x00; 20var kDeclSignatures = 0x01; 21var kDeclFunctions = 0x02; 22var kDeclGlobals = 0x03; 23var kDeclDataSegments = 0x04; 24var kDeclFunctionTable = 0x05; 25var kDeclEnd = 0x06; 26 27// Function declaration flags 28var kDeclFunctionName = 0x01; 29var kDeclFunctionImport = 0x02; 30var kDeclFunctionLocals = 0x04; 31var kDeclFunctionExport = 0x08; 32 33// Local types 34var kAstStmt = 0; 35var kAstI32 = 1; 36var kAstI64 = 2; 37var kAstF32 = 3; 38var kAstF64 = 4; 39 40// Opcodes 41var kExprNop = 0x00; 42var kExprBlock = 0x01; 43var kExprLoop = 0x02; 44var kExprIf = 0x03; 45var kExprIfElse = 0x04; 46var kExprSelect = 0x05; 47var kExprBr = 0x06; 48var kExprBrIf = 0x07; 49var kExprTableSwitch = 0x08; 50var kExprReturn = 0x14; 51var kExprUnreachable = 0x15; 52 53var kExprI8Const = 0x09; 54var kExprI32Const = 0x0a; 55var kExprI64Const = 0x0b; 56var kExprF64Const = 0x0c; 57var kExprF32Const = 0x0d; 58var kExprGetLocal = 0x0e; 59var kExprSetLocal = 0x0f; 60var kExprLoadGlobal = 0x10; 61var kExprStoreGlobal = 0x11; 62var kExprCallFunction = 0x12; 63var kExprCallIndirect = 0x13; 64 65var kExprI32LoadMem8S = 0x20; 66var kExprI32LoadMem8U = 0x21; 67var kExprI32LoadMem16S = 0x22; 68var kExprI32LoadMem16U = 0x23; 69var kExprI64LoadMem8S = 0x24; 70var kExprI64LoadMem8U = 0x25; 71var kExprI64LoadMem16S = 0x26; 72var kExprI64LoadMem16U = 0x27; 73var kExprI64LoadMem32S = 0x28; 74var kExprI64LoadMem32U = 0x29; 75var kExprI32LoadMem = 0x2a; 76var kExprI64LoadMem = 0x2b; 77var kExprF32LoadMem = 0x2c; 78var kExprF64LoadMem = 0x2d; 79 80var kExprI32StoreMem8 = 0x2e; 81var kExprI32StoreMem16 = 0x2f; 82var kExprI64StoreMem8 = 0x30; 83var kExprI64StoreMem16 = 0x31; 84var kExprI64StoreMem32 = 0x32; 85var kExprI32StoreMem = 0x33; 86var kExprI64StoreMem = 0x34; 87var kExprF32StoreMem = 0x35; 88var kExprF64StoreMem = 0x36; 89 90var kExprMemorySize = 0x3b; 91var kExprGrowMemory = 0x39; 92 93var kExprI32Add = 0x40; 94var kExprI32Sub = 0x41; 95var kExprI32Mul = 0x42; 96var kExprI32DivS = 0x43; 97var kExprI32DivU = 0x44; 98var kExprI32RemS = 0x45; 99var kExprI32RemU = 0x46; 100var kExprI32And = 0x47; 101var kExprI32Ior = 0x48; 102var kExprI32Xor = 0x49; 103var kExprI32Shl = 0x4a; 104var kExprI32ShrU = 0x4b; 105var kExprI32ShrS = 0x4c; 106var kExprI32Eq = 0x4d; 107var kExprI32Ne = 0x4e; 108var kExprI32LtS = 0x4f; 109var kExprI32LeS = 0x50; 110var kExprI32LtU = 0x51; 111var kExprI32LeU = 0x52; 112var kExprI32GtS = 0x53; 113var kExprI32GeS = 0x54; 114var kExprI32GtU = 0x55; 115var kExprI32GeU = 0x56; 116var kExprI32Clz = 0x57; 117var kExprI32Ctz = 0x58; 118var kExprI32Popcnt = 0x59; 119var kExprBoolNot = 0x5a; 120var kExprI64Add = 0x5b; 121var kExprI64Sub = 0x5c; 122var kExprI64Mul = 0x5d; 123var kExprI64DivS = 0x5e; 124var kExprI64DivU = 0x5f; 125var kExprI64RemS = 0x60; 126var kExprI64RemU = 0x61; 127var kExprI64And = 0x62; 128var kExprI64Ior = 0x63; 129var kExprI64Xor = 0x64; 130var kExprI64Shl = 0x65; 131var kExprI64ShrU = 0x66; 132var kExprI64ShrS = 0x67; 133var kExprI64Eq = 0x68; 134var kExprI64Ne = 0x69; 135var kExprI64LtS = 0x6a; 136var kExprI64LeS = 0x6b; 137var kExprI64LtU = 0x6c; 138var kExprI64LeU = 0x6d; 139var kExprI64GtS = 0x6e; 140var kExprI64GeS = 0x6f; 141var kExprI64GtU = 0x70; 142var kExprI64GeU = 0x71; 143var kExprI64Clz = 0x72; 144var kExprI64Ctz = 0x73; 145var kExprI64Popcnt = 0x74; 146var kExprF32Add = 0x75; 147var kExprF32Sub = 0x76; 148var kExprF32Mul = 0x77; 149var kExprF32Div = 0x78; 150var kExprF32Min = 0x79; 151var kExprF32Max = 0x7a; 152var kExprF32Abs = 0x7b; 153var kExprF32Neg = 0x7c; 154var kExprF32CopySign = 0x7d; 155var kExprF32Ceil = 0x7e; 156var kExprF32Floor = 0x7f; 157var kExprF32Trunc = 0x80; 158var kExprF32NearestInt = 0x81; 159var kExprF32Sqrt = 0x82; 160var kExprF32Eq = 0x83; 161var kExprF32Ne = 0x84; 162var kExprF32Lt = 0x85; 163var kExprF32Le = 0x86; 164var kExprF32Gt = 0x87; 165var kExprF32Ge = 0x88; 166var kExprF64Add = 0x89; 167var kExprF64Sub = 0x8a; 168var kExprF64Mul = 0x8b; 169var kExprF64Div = 0x8c; 170var kExprF64Min = 0x8d; 171var kExprF64Max = 0x8e; 172var kExprF64Abs = 0x8f; 173var kExprF64Neg = 0x90; 174var kExprF64CopySign = 0x91; 175var kExprF64Ceil = 0x92; 176var kExprF64Floor = 0x93; 177var kExprF64Trunc = 0x94; 178var kExprF64NearestInt = 0x95; 179var kExprF64Sqrt = 0x96; 180var kExprF64Eq = 0x97; 181var kExprF64Ne = 0x98; 182var kExprF64Lt = 0x99; 183var kExprF64Le = 0x9a; 184var kExprF64Gt = 0x9b; 185var kExprF64Ge = 0x9c; 186var kExprI32SConvertF32 = 0x9d; 187var kExprI32SConvertF64 = 0x9e; 188var kExprI32UConvertF32 = 0x9f; 189var kExprI32UConvertF64 = 0xa0; 190var kExprI32ConvertI64 = 0xa1; 191var kExprI64SConvertF32 = 0xa2; 192var kExprI64SConvertF64 = 0xa3; 193var kExprI64UConvertF32 = 0xa4; 194var kExprI64UConvertF64 = 0xa5; 195var kExprI64SConvertI32 = 0xa6; 196var kExprI64UConvertI32 = 0xa7; 197var kExprF32SConvertI32 = 0xa8; 198var kExprF32UConvertI32 = 0xa9; 199var kExprF32SConvertI64 = 0xaa; 200var kExprF32UConvertI64 = 0xab; 201var kExprF32ConvertF64 = 0xac; 202var kExprF32ReinterpretI32 = 0xad; 203var kExprF64SConvertI32 = 0xae; 204var kExprF64UConvertI32 = 0xaf; 205var kExprF64SConvertI64 = 0xb0; 206var kExprF64UConvertI64 = 0xb1; 207var kExprF64ConvertF32 = 0xb2; 208var kExprF64ReinterpretI64 = 0xb3; 209var kExprI32ReinterpretF32 = 0xb4; 210var kExprI64ReinterpretF64 = 0xb5; 211 212var kTrapUnreachable = 0; 213var kTrapMemOutOfBounds = 1; 214var kTrapDivByZero = 2; 215var kTrapDivUnrepresentable = 3; 216var kTrapRemByZero = 4; 217var kTrapFloatUnrepresentable = 5; 218var kTrapFuncInvalid = 6; 219var kTrapFuncSigMismatch = 7; 220 221var kTrapMsgs = [ 222 "unreachable", 223 "memory access out of bounds", 224 "divide by zero", 225 "divide result unrepresentable", 226 "remainder by zero", 227 "integer result unrepresentable", 228 "invalid function", 229 "function signature mismatch" 230]; 231 232function assertTraps(trap, code) { 233 var threwException = true; 234 try { 235 if (typeof code === 'function') { 236 code(); 237 } else { 238 eval(code); 239 } 240 threwException = false; 241 } catch (e) { 242 assertEquals("string", typeof e); 243 assertEquals(kTrapMsgs[trap], e); 244 // Success. 245 return; 246 } 247 throw new MjsUnitAssertionError("Did not trap, expected: " + kTrapMsgs[trap]); 248} 249