| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 "src/wasm/wasm-function-name-table.h" | 5 #include "src/wasm/wasm-function-name-table.h" |
| 6 #include "src/wasm/wasm-module.h" | 6 #include "src/wasm/wasm-module.h" |
| 7 | 7 |
| 8 #include "test/cctest/cctest.h" | 8 #include "test/cctest/cctest.h" |
| 9 | 9 |
| 10 using namespace v8::internal; | 10 using namespace v8::internal; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 exp_.length(), exp_.start(), found_.length(), found_.start()); \ | 23 exp_.length(), exp_.start(), found_.length(), found_.start()); \ |
| 24 } \ | 24 } \ |
| 25 } while (0) | 25 } while (0) |
| 26 | 26 |
| 27 void testFunctionNameTable(Vector<Vector<const char>> names) { | 27 void testFunctionNameTable(Vector<Vector<const char>> names) { |
| 28 Isolate *isolate = CcTest::InitIsolateOnce(); | 28 Isolate *isolate = CcTest::InitIsolateOnce(); |
| 29 HandleAndZoneScope scope; | 29 HandleAndZoneScope scope; |
| 30 | 30 |
| 31 WasmModule module; | 31 WasmModule module; |
| 32 std::vector<char> all_names; | 32 std::vector<char> all_names; |
| 33 // No name should have offset 0, because that encodes unnamed functions. |
| 34 // In real wasm binary, offset 0 is impossible anyway. |
| 35 all_names.push_back('\0'); |
| 33 | 36 |
| 34 uint32_t func_index = 0; | 37 uint32_t func_index = 0; |
| 35 for (Vector<const char> name : names) { | 38 for (Vector<const char> name : names) { |
| 36 size_t name_offset = all_names.size(); | 39 size_t name_offset = name.start() ? all_names.size() : 0; |
| 37 all_names.insert(all_names.end(), name.start(), | 40 all_names.insert(all_names.end(), name.start(), |
| 38 name.start() + name.length()); | 41 name.start() + name.length()); |
| 39 // Make every second function name null-terminated. | 42 // Make every second function name null-terminated. |
| 40 if (func_index % 2) all_names.push_back('\0'); | 43 if (func_index % 2) all_names.push_back('\0'); |
| 41 module.functions.push_back( | 44 module.functions.push_back( |
| 42 {nullptr, 0, 0, static_cast<uint32_t>(name_offset), | 45 {nullptr, 0, 0, static_cast<uint32_t>(name_offset), |
| 43 static_cast<uint32_t>(name.length()), 0, 0, 0, 0, 0, 0, false, false}); | 46 static_cast<uint32_t>(name.length()), 0, 0, 0, 0, 0, 0, false, false}); |
| 44 ++func_index; | 47 ++func_index; |
| 45 } | 48 } |
| 46 | 49 |
| 47 module.module_start = reinterpret_cast<byte *>(all_names.data()); | 50 module.module_start = reinterpret_cast<byte *>(all_names.data()); |
| 48 module.module_end = module.module_start + all_names.size(); | 51 module.module_end = module.module_start + all_names.size(); |
| 49 | 52 |
| 50 Handle<Object> wasm_function_name_table = | 53 Handle<Object> wasm_function_name_table = |
| 51 BuildFunctionNamesTable(isolate, &module); | 54 BuildFunctionNamesTable(isolate, &module); |
| 52 CHECK(wasm_function_name_table->IsByteArray()); | 55 CHECK(wasm_function_name_table->IsByteArray()); |
| 53 | 56 |
| 54 func_index = 0; | 57 func_index = 0; |
| 55 for (Vector<const char> name : names) { | 58 for (Vector<const char> name : names) { |
| 56 Handle<Object> string_obj = GetWasmFunctionNameFromTable( | 59 Handle<Object> string_obj = GetWasmFunctionNameFromTable( |
| 57 Handle<ByteArray>::cast(wasm_function_name_table), func_index); | 60 Handle<ByteArray>::cast(wasm_function_name_table), func_index); |
| 58 CHECK(!string_obj.is_null()); | 61 CHECK(!string_obj.is_null()); |
| 59 CHECK(string_obj->IsString()); | 62 if (name.start()) { |
| 60 Handle<String> string = Handle<String>::cast(string_obj); | 63 CHECK(string_obj->IsString()); |
| 61 CHECK(string->IsUtf8EqualTo(name)); | 64 Handle<String> string = Handle<String>::cast(string_obj); |
| 65 CHECK(string->IsUtf8EqualTo(name)); |
| 66 } else { |
| 67 CHECK(string_obj->IsNull()); |
| 68 } |
| 62 ++func_index; | 69 ++func_index; |
| 63 } | 70 } |
| 64 } | 71 } |
| 65 | 72 |
| 66 void testFunctionNameTable(Vector<const char *> names) { | 73 void testFunctionNameTable(Vector<const char *> names) { |
| 67 std::vector<Vector<const char>> names_vec; | 74 std::vector<Vector<const char>> names_vec; |
| 68 for (const char *name : names) names_vec.push_back(CStrVector(name)); | 75 for (const char *name : names) |
| 76 names_vec.push_back(name ? CStrVector(name) : Vector<const char>()); |
| 69 testFunctionNameTable(Vector<Vector<const char>>( | 77 testFunctionNameTable(Vector<Vector<const char>>( |
| 70 names_vec.data(), static_cast<int>(names_vec.size()))); | 78 names_vec.data(), static_cast<int>(names_vec.size()))); |
| 71 } | 79 } |
| 72 | 80 |
| 73 } // namespace | 81 } // namespace |
| 74 | 82 |
| 75 TEST(NoFunctions) { testFunctionNameTable(Vector<Vector<const char>>()); } | 83 TEST(NoFunctions) { testFunctionNameTable(Vector<Vector<const char>>()); } |
| 76 | 84 |
| 77 TEST(OneFunctions) { | 85 TEST(OneFunctions) { |
| 78 const char *names[] = {"foo"}; | 86 const char *names[] = {"foo"}; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 101 | 109 |
| 102 TEST(ThreeUnnamedFunctions) { | 110 TEST(ThreeUnnamedFunctions) { |
| 103 const char *names[] = {"", "", ""}; | 111 const char *names[] = {"", "", ""}; |
| 104 testFunctionNameTable(ArrayVector(names)); | 112 testFunctionNameTable(ArrayVector(names)); |
| 105 } | 113 } |
| 106 | 114 |
| 107 TEST(UTF8Names) { | 115 TEST(UTF8Names) { |
| 108 const char *names[] = {"↱fun↰", "↺", "alpha:α beta:β"}; | 116 const char *names[] = {"↱fun↰", "↺", "alpha:α beta:β"}; |
| 109 testFunctionNameTable(ArrayVector(names)); | 117 testFunctionNameTable(ArrayVector(names)); |
| 110 } | 118 } |
| 119 |
| 120 TEST(UnnamesVsEmptyNames) { |
| 121 const char *names[] = {"", nullptr, nullptr, ""}; |
| 122 testFunctionNameTable(ArrayVector(names)); |
| 123 } |
| OLD | NEW |