Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: src/wasm/wasm-function-name-table.cc

Issue 1970503004: [wasm] Differentiate unnamed and empty names (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@add-utf8-check
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 6
7 #include "src/wasm/utf8.h" 7 #include "src/wasm/utf8.h"
8 #include "src/wasm/wasm-module.h" 8 #include "src/wasm/wasm-module.h"
9 9
10 namespace v8 { 10 namespace v8 {
11 namespace internal { 11 namespace internal {
12 namespace wasm { 12 namespace wasm {
13 13
14 // Build an array with all function names. If there are N functions in the 14 // Build an array with all function names. If there are N functions in the
15 // module, then the first (kIntSize * (N+1)) bytes are integer entries. 15 // module, then the first (kIntSize * (N+1)) bytes are integer entries.
16 // The first integer entry encodes the number of functions in the module. 16 // The first integer entry encodes the number of functions in the module.
17 // The entries 1 to N contain offsets into the second part of this array. 17 // The entries 1 to N contain offsets into the second part of this array.
18 // If a function is unnamed (not to be confused with an empty name), then the
19 // integer entry is the negative offset of the next function name.
titzer 2016/05/12 09:05:51 Why does it have to be the negative offset of the
Clemens Hammacher 2016/05/12 09:45:57 The negative offset of the next function name allo
titzer 2016/05/12 09:54:36 ok
18 // After these N+1 integer entries, the second part begins, which holds a 20 // After these N+1 integer entries, the second part begins, which holds a
19 // concatenation of all function names. 21 // concatenation of all function names.
20 // 22 //
21 // Returns undefined if the array length would not fit in an integer value 23 // Returns undefined if the array length would not fit in an integer value.
22 Handle<Object> BuildFunctionNamesTable(Isolate* isolate, WasmModule* module) { 24 Handle<Object> BuildFunctionNamesTable(Isolate* isolate, WasmModule* module) {
23 uint64_t func_names_length = 0; 25 uint64_t func_names_length = 0;
24 for (auto& func : module->functions) func_names_length += func.name_length; 26 for (auto& func : module->functions) func_names_length += func.name_length;
25 int num_funcs_int = static_cast<int>(module->functions.size()); 27 int num_funcs_int = static_cast<int>(module->functions.size());
26 int current_offset = (num_funcs_int + 1) * kIntSize; 28 int current_offset = (num_funcs_int + 1) * kIntSize;
27 uint64_t total_array_length = current_offset + func_names_length; 29 uint64_t total_array_length = current_offset + func_names_length;
28 int total_array_length_int = static_cast<int>(total_array_length); 30 int total_array_length_int = static_cast<int>(total_array_length);
29 // Check for overflow. Just skip function names if it happens. 31 // Check for overflow. Just skip function names if it happens.
30 if (total_array_length_int != total_array_length || num_funcs_int < 0 || 32 if (total_array_length_int != total_array_length || num_funcs_int < 0 ||
31 num_funcs_int != module->functions.size()) 33 num_funcs_int != module->functions.size())
32 return isolate->factory()->undefined_value(); 34 return isolate->factory()->undefined_value();
33 Handle<ByteArray> func_names_array = 35 Handle<ByteArray> func_names_array =
34 isolate->factory()->NewByteArray(total_array_length_int, TENURED); 36 isolate->factory()->NewByteArray(total_array_length_int, TENURED);
35 if (func_names_array.is_null()) return isolate->factory()->undefined_value(); 37 if (func_names_array.is_null()) return isolate->factory()->undefined_value();
36 func_names_array->set_int(0, num_funcs_int); 38 func_names_array->set_int(0, num_funcs_int);
37 int func_index = 0; 39 int func_index = 0;
38 for (WasmFunction& fun : module->functions) { 40 for (WasmFunction& fun : module->functions) {
39 WasmName name = module->GetNameOrNull(&fun); 41 WasmName name = module->GetNameOrNull(&fun);
40 func_names_array->copy_in(current_offset, 42 if (name.start() == nullptr) {
41 reinterpret_cast<const byte*>(name.start()), 43 func_names_array->set_int(func_index + 1, -current_offset);
42 name.length()); 44 } else {
43 func_names_array->set_int(func_index + 1, current_offset); 45 func_names_array->copy_in(current_offset,
44 current_offset += name.length(); 46 reinterpret_cast<const byte*>(name.start()),
47 name.length());
48 func_names_array->set_int(func_index + 1, current_offset);
49 current_offset += name.length();
50 }
45 ++func_index; 51 ++func_index;
46 } 52 }
47 return func_names_array; 53 return func_names_array;
48 } 54 }
49 55
56 // Extract the function name for the given function index from the function name
57 // table.
58 //
59 // Returns undefined if the function index is invalid or the respective function
60 // name is no valid UTF-8 string; returns null for unnamed functions (not to be
titzer 2016/05/12 09:05:51 s/no valid/not a valid/ Why not just return undef
Clemens Hammacher 2016/05/12 09:45:57 I am not the JavaScript expert, but I think undefi
titzer 2016/05/12 09:54:36 I think it's OK to use undefined for both here: th
61 // confused with empty names).
50 Handle<Object> GetWasmFunctionNameFromTable(Handle<ByteArray> func_names_array, 62 Handle<Object> GetWasmFunctionNameFromTable(Handle<ByteArray> func_names_array,
51 uint32_t func_index) { 63 uint32_t func_index) {
52 uint32_t num_funcs = static_cast<uint32_t>(func_names_array->get_int(0)); 64 uint32_t num_funcs = static_cast<uint32_t>(func_names_array->get_int(0));
53 DCHECK(static_cast<int>(num_funcs) >= 0); 65 DCHECK(static_cast<int>(num_funcs) >= 0);
54 auto undefined = [&func_names_array]() -> Handle<Object> { 66 Factory* factory = func_names_array->GetIsolate()->factory();
55 return func_names_array->GetIsolate()->factory()->undefined_value(); 67 if (func_index >= num_funcs) return factory->undefined_value();
56 };
57 if (func_index >= num_funcs) return undefined();
58 int offset = func_names_array->get_int(func_index + 1); 68 int offset = func_names_array->get_int(func_index + 1);
69 if (offset < 0) return factory->null_value();
59 int next_offset = func_index == num_funcs - 1 70 int next_offset = func_index == num_funcs - 1
60 ? func_names_array->length() 71 ? func_names_array->length()
61 : func_names_array->get_int(func_index + 2); 72 : abs(func_names_array->get_int(func_index + 2));
62 ScopedVector<byte> buffer(next_offset - offset); 73 ScopedVector<byte> buffer(next_offset - offset);
63 func_names_array->copy_out(offset, buffer.start(), next_offset - offset); 74 func_names_array->copy_out(offset, buffer.start(), next_offset - offset);
64 if (!IsValidUtf8(buffer.start(), buffer.length())) return undefined(); 75 if (!IsValidUtf8(buffer.start(), buffer.length()))
65 MaybeHandle<Object> maybe_name = 76 return factory->undefined_value();
66 func_names_array->GetIsolate()->factory()->NewStringFromUtf8( 77 Handle<Object> maybe_name;
67 Vector<const char>::cast(buffer)); 78 if (!factory->NewStringFromUtf8(Vector<const char>::cast(buffer))
68 return maybe_name.is_null() ? undefined() : maybe_name.ToHandleChecked(); 79 .ToHandle(&maybe_name))
80 maybe_name = factory->undefined_value();
81 return maybe_name;
69 } 82 }
70 83
71 } // namespace wasm 84 } // namespace wasm
72 } // namespace internal 85 } // namespace internal
73 } // namespace v8 86 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698