| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "test/unittests/test-utils.h" | 5 #include "test/unittests/test-utils.h" |
| 6 | 6 |
| 7 #include "src/v8.h" | 7 #include "src/v8.h" |
| 8 | 8 |
| 9 #include "src/wasm/ast-decoder.h" | 9 #include "src/wasm/ast-decoder.h" |
| 10 #include "src/wasm/encoder.h" | 10 #include "src/wasm/encoder.h" |
| 11 | 11 |
| 12 namespace v8 { | 12 namespace v8 { |
| 13 namespace internal { | 13 namespace internal { |
| 14 namespace wasm { | 14 namespace wasm { |
| 15 | 15 |
| 16 class EncoderTest : public TestWithZone { | 16 class EncoderTest : public TestWithZone { |
| 17 protected: | 17 protected: |
| 18 void AddLocal(WasmFunctionBuilder* f, LocalType type) { | 18 void AddLocal(WasmFunctionBuilder* f, LocalType type) { |
| 19 uint16_t index = f->AddLocal(type); | 19 uint16_t index = f->AddLocal(type); |
| 20 const std::vector<uint8_t>& out_index = UnsignedLEB128From(index); | 20 f->EmitGetLocal(index); |
| 21 std::vector<uint8_t> code; | |
| 22 code.push_back(kExprGetLocal); | |
| 23 for (size_t i = 0; i < out_index.size(); i++) { | |
| 24 code.push_back(out_index.at(i)); | |
| 25 } | |
| 26 uint32_t local_indices[] = {1}; | |
| 27 f->EmitCode(&code[0], static_cast<uint32_t>(code.size()), local_indices, 1); | |
| 28 } | |
| 29 | |
| 30 void CheckReadValue(uint8_t* leb_value, uint32_t expected_result, | |
| 31 int expected_length, | |
| 32 ReadUnsignedLEB128ErrorCode expected_error_code) { | |
| 33 int length; | |
| 34 uint32_t result; | |
| 35 ReadUnsignedLEB128ErrorCode error_code = | |
| 36 ReadUnsignedLEB128Operand(leb_value, leb_value + 5, &length, &result); | |
| 37 CHECK_EQ(error_code, expected_error_code); | |
| 38 if (error_code == 0) { | |
| 39 CHECK_EQ(result, expected_result); | |
| 40 CHECK_EQ(length, expected_length); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 void CheckWriteValue(uint32_t input, int length, uint8_t* vals) { | |
| 45 const std::vector<uint8_t> result = UnsignedLEB128From(input); | |
| 46 CHECK_EQ(result.size(), length); | |
| 47 for (int i = 0; i < length; i++) { | |
| 48 CHECK_EQ(result.at(i), vals[i]); | |
| 49 } | |
| 50 } | 21 } |
| 51 }; | 22 }; |
| 52 | 23 |
| 53 | 24 |
| 54 TEST_F(EncoderTest, Function_Builder_Variable_Indexing) { | 25 TEST_F(EncoderTest, Function_Builder_Variable_Indexing) { |
| 55 base::AccountingAllocator allocator; | 26 base::AccountingAllocator allocator; |
| 56 Zone zone(&allocator); | 27 Zone zone(&allocator); |
| 57 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); | 28 WasmModuleBuilder* builder = new (&zone) WasmModuleBuilder(&zone); |
| 58 uint16_t f_index = builder->AddFunction(); | 29 uint16_t f_index = builder->AddFunction(); |
| 59 WasmFunctionBuilder* function = builder->FunctionAt(f_index); | 30 WasmFunctionBuilder* function = builder->FunctionAt(f_index); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 CHECK_EQ(body[offset++], i); | 151 CHECK_EQ(body[offset++], i); |
| 181 } | 152 } |
| 182 // GetLocal with two-byte indices. | 153 // GetLocal with two-byte indices. |
| 183 for (int i = 128; i < 200; ++i) { | 154 for (int i = 128; i < 200; ++i) { |
| 184 CHECK_EQ(body[offset++], kExprGetLocal); | 155 CHECK_EQ(body[offset++], kExprGetLocal); |
| 185 CHECK_EQ(body[offset++], (i & 0x7f) | 0x80); | 156 CHECK_EQ(body[offset++], (i & 0x7f) | 0x80); |
| 186 CHECK_EQ(body[offset++], (i >> 7) & 0x7f); | 157 CHECK_EQ(body[offset++], (i >> 7) & 0x7f); |
| 187 } | 158 } |
| 188 CHECK_EQ(offset, 479); | 159 CHECK_EQ(offset, 479); |
| 189 } | 160 } |
| 190 | |
| 191 TEST_F(EncoderTest, LEB_Functions) { | |
| 192 byte leb_value[5] = {0, 0, 0, 0, 0}; | |
| 193 CheckReadValue(leb_value, 0, 1, kNoError); | |
| 194 CheckWriteValue(0, 1, leb_value); | |
| 195 leb_value[0] = 23; | |
| 196 CheckReadValue(leb_value, 23, 1, kNoError); | |
| 197 CheckWriteValue(23, 1, leb_value); | |
| 198 leb_value[0] = 0x80; | |
| 199 leb_value[1] = 0x01; | |
| 200 CheckReadValue(leb_value, 128, 2, kNoError); | |
| 201 CheckWriteValue(128, 2, leb_value); | |
| 202 leb_value[0] = 0x80; | |
| 203 leb_value[1] = 0x80; | |
| 204 leb_value[2] = 0x80; | |
| 205 leb_value[3] = 0x80; | |
| 206 leb_value[4] = 0x01; | |
| 207 CheckReadValue(leb_value, 0x10000000, 5, kNoError); | |
| 208 CheckWriteValue(0x10000000, 5, leb_value); | |
| 209 leb_value[0] = 0x80; | |
| 210 leb_value[1] = 0x80; | |
| 211 leb_value[2] = 0x80; | |
| 212 leb_value[3] = 0x80; | |
| 213 leb_value[4] = 0x80; | |
| 214 CheckReadValue(leb_value, -1, -1, kInvalidLEB128); | |
| 215 } | |
| 216 } // namespace wasm | 161 } // namespace wasm |
| 217 } // namespace internal | 162 } // namespace internal |
| 218 } // namespace v8 | 163 } // namespace v8 |
| OLD | NEW |