Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/wasm/wasm-function-name-table.h" |
| 6 | |
| 7 | #include "src/wasm/wasm-module.h" |
| 8 | |
| 9 | namespace v8 { |
| 10 | namespace internal { |
| 11 | namespace wasm { |
| 12 | |
| 13 | // Build an array with all function names. If there are N functions in the |
| 14 | // module, then the first (kIntSize * (N+1)) bytes are integer entries. |
| 15 | // The first integer entry encodes the number of functions in the module. |
| 16 | // The entries 1 to N contain offsets into the second part of this array. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 17 | // If a function is unnamed (not to be confused with an empty name), then the |
| 18 | // integer entry is the negative offset of the next function name. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 19 | // After these N+1 integer entries, the second part begins, which holds a |
| 20 | // concatenation of all function names. |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 21 | Handle<ByteArray> BuildFunctionNamesTable(Isolate* isolate, |
| 22 | const WasmModule* module) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 23 | uint64_t func_names_length = 0; |
| 24 | for (auto& func : module->functions) func_names_length += func.name_length; |
| 25 | int num_funcs_int = static_cast<int>(module->functions.size()); |
| 26 | int current_offset = (num_funcs_int + 1) * kIntSize; |
| 27 | uint64_t total_array_length = current_offset + func_names_length; |
| 28 | int total_array_length_int = static_cast<int>(total_array_length); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 29 | // Check for overflow. |
| 30 | CHECK(total_array_length_int == total_array_length && num_funcs_int >= 0 && |
| 31 | num_funcs_int == module->functions.size()); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 32 | Handle<ByteArray> func_names_array = |
| 33 | isolate->factory()->NewByteArray(total_array_length_int, TENURED); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 34 | func_names_array->set_int(0, num_funcs_int); |
| 35 | int func_index = 0; |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 36 | for (const WasmFunction& fun : module->functions) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 37 | WasmName name = module->GetNameOrNull(&fun); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 38 | if (name.start() == nullptr) { |
| 39 | func_names_array->set_int(func_index + 1, -current_offset); |
| 40 | } else { |
| 41 | func_names_array->copy_in(current_offset, |
| 42 | reinterpret_cast<const byte*>(name.start()), |
| 43 | name.length()); |
| 44 | func_names_array->set_int(func_index + 1, current_offset); |
| 45 | current_offset += name.length(); |
| 46 | } |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 47 | ++func_index; |
| 48 | } |
| 49 | return func_names_array; |
| 50 | } |
| 51 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 52 | MaybeHandle<String> GetWasmFunctionNameFromTable( |
| 53 | Handle<ByteArray> func_names_array, uint32_t func_index) { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 54 | uint32_t num_funcs = static_cast<uint32_t>(func_names_array->get_int(0)); |
| 55 | DCHECK(static_cast<int>(num_funcs) >= 0); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 56 | Factory* factory = func_names_array->GetIsolate()->factory(); |
| 57 | if (func_index >= num_funcs) return {}; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 58 | int offset = func_names_array->get_int(func_index + 1); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 59 | if (offset < 0) return {}; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 60 | int next_offset = func_index == num_funcs - 1 |
| 61 | ? func_names_array->length() |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 62 | : abs(func_names_array->get_int(func_index + 2)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 63 | ScopedVector<byte> buffer(next_offset - offset); |
| 64 | func_names_array->copy_out(offset, buffer.start(), next_offset - offset); |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 65 | if (!unibrow::Utf8::Validate(buffer.start(), buffer.length())) return {}; |
| 66 | return factory->NewStringFromUtf8(Vector<const char>::cast(buffer)); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } // namespace wasm |
| 70 | } // namespace internal |
| 71 | } // namespace v8 |