[wasm] Small cleanups

Changes:
- Simplify GetRefTypeName.
- Simplify WasmModuleDebug::GetWasmValue.
- Fix some signature issues in tests.

Change-Id: I61b9a48c0fbce0bc9cc74771412bdb8977880697
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3468344
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79137}
This commit is contained in:
Manos Koukoutos 2022-02-17 11:00:14 +00:00 committed by V8 LUCI CQ
parent d571cf7c2f
commit 6909711b88
4 changed files with 28 additions and 84 deletions

View File

@ -771,71 +771,23 @@ Handle<String> WasmSimd128ToString(Isolate* isolate, wasm::Simd128 s128) {
Handle<String> GetRefTypeName(Isolate* isolate, wasm::ValueType type,
wasm::NativeModule* module) {
bool is_nullable = type.kind() == wasm::kOptRef;
const char* null_str = is_nullable ? " null" : "";
// This length only needs to be enough for generated names like
// "(ref null $type12345)". For names coming from the name section,
// we'll dynamically allocate an appropriately sized vector.
base::EmbeddedVector<char, 32> type_name;
size_t len;
DCHECK(type.is_object_reference());
std::ostringstream name;
if (type.heap_type().is_generic()) {
const char* generic_name = "";
wasm::HeapType::Representation heap_rep = type.heap_representation();
switch (heap_rep) {
case wasm::HeapType::kFunc:
generic_name = "func";
break;
case wasm::HeapType::kExtern:
generic_name = "extern";
break;
case wasm::HeapType::kEq:
generic_name = "eq";
break;
case wasm::HeapType::kI31:
generic_name = "i31";
break;
case wasm::HeapType::kData:
generic_name = "data";
break;
case wasm::HeapType::kArray:
generic_name = "array";
break;
case wasm::HeapType::kAny:
generic_name = "any";
break;
default:
UNREACHABLE();
}
len = SNPrintF(type_name, "(ref%s %s)", null_str, generic_name);
name << type.name();
} else {
int type_index = type.ref_index();
name << "(ref " << (type.is_nullable() ? "null " : "") << "$";
wasm::ModuleWireBytes module_wire_bytes(module->wire_bytes());
base::Vector<const char> name_vec = module_wire_bytes.GetNameOrNull(
module->GetDebugInfo()->GetTypeName(type_index));
if (name_vec.empty()) {
len = SNPrintF(type_name, "(ref%s $type%u)", null_str, type_index);
base::Vector<const char> module_name = module_wire_bytes.GetNameOrNull(
module->GetDebugInfo()->GetTypeName(type.ref_index()));
if (module_name.empty()) {
name << "type" << type.ref_index();
} else {
size_t required_length =
name_vec.size() + // length of provided name
7 + // length of "(ref $)"
(is_nullable ? 5 : 0); // length of " null" (optional)
base::Vector<char> long_type_name =
base::Vector<char>::New(required_length);
len = SNPrintF(long_type_name, "(ref%s $", null_str);
base::Vector<char> suffix =
long_type_name.SubVector(len, long_type_name.size());
// StrNCpy requires that there is room for an assumed trailing \0...
DCHECK_EQ(suffix.size(), name_vec.size() + 1);
base::StrNCpy(suffix, name_vec.data(), name_vec.size());
// ...but we actually write ')' into that byte.
long_type_name[required_length - 1] = ')';
Handle<String> result =
isolate->factory()->InternalizeString(long_type_name);
long_type_name.Dispose();
return result;
name.write(module_name.begin(), module_name.size());
}
name << ")";
}
return isolate->factory()->InternalizeString(type_name.SubVector(0, len));
return isolate->factory()->InternalizeString(base::VectorOf(name.str()));
}
} // namespace
@ -1017,13 +969,6 @@ Handle<WasmValueObject> WasmValueObject::New(
break;
}
case wasm::kOptRef:
if (value.type().is_reference_to(wasm::HeapType::kExtern)) {
t = isolate->factory()->InternalizeString(
base::StaticCharVector("externref"));
v = value.to_ref();
break;
}
V8_FALLTHROUGH;
case wasm::kRef: {
t = GetRefTypeName(isolate, value.type(), module_object->native_module());
Handle<Object> ref = value.to_ref();
@ -1031,11 +976,12 @@ Handle<WasmValueObject> WasmValueObject::New(
v = StructProxy::Create(isolate, value, module_object);
} else if (ref->IsWasmArray()) {
v = ArrayProxy::Create(isolate, value, module_object);
} else if (ref->IsJSFunction() || ref->IsSmi() || ref->IsNull()) {
v = ref;
} else if (ref->IsWasmInternalFunction()) {
v = handle(Handle<WasmInternalFunction>::cast(ref)->external(),
isolate);
} else if (ref->IsJSFunction() || ref->IsSmi() || ref->IsNull() ||
value.type().is_reference_to(wasm::HeapType::kExtern)) {
v = ref;
} else {
// Fail gracefully.
base::EmbeddedVector<char, 64> error;

View File

@ -389,21 +389,21 @@ bool WasmModuleDebug::GetWasmValue(const wasm::WasmValue& wasm_value,
uint8_t* buffer, uint32_t buffer_size,
uint32_t* size) {
switch (wasm_value.type().kind()) {
case wasm::kWasmI32.kind():
case wasm::kI32:
return StoreValue(wasm_value.to_i32(), buffer, buffer_size, size);
case wasm::kWasmI64.kind():
case wasm::kI64:
return StoreValue(wasm_value.to_i64(), buffer, buffer_size, size);
case wasm::kWasmF32.kind():
case wasm::kF32:
return StoreValue(wasm_value.to_f32(), buffer, buffer_size, size);
case wasm::kWasmF64.kind():
case wasm::kF64:
return StoreValue(wasm_value.to_f64(), buffer, buffer_size, size);
case wasm::kWasmS128.kind():
case wasm::kS128:
return StoreValue(wasm_value.to_s128(), buffer, buffer_size, size);
case wasm::kWasmVoid.kind():
case wasm::kWasmExternRef.kind():
case wasm::kWasmBottom.kind():
default:
case wasm::kRef:
case wasm::kOptRef:
case wasm::kRtt:
case wasm::kVoid:
case wasm::kBottom:
// Not supported
return false;
}

View File

@ -102,7 +102,7 @@ class TestSignatures {
FunctionSig* d_dd() { return &sig_d_dd; }
FunctionSig* e_v() { return &sig_e_v; }
FunctionSig* a_v() { return &sig_c_v; }
FunctionSig* c_v() { return &sig_c_v; }
FunctionSig* e_e() { return &sig_e_e; }
FunctionSig* c_c() { return &sig_c_c; }

View File

@ -353,7 +353,7 @@ TEST_F(FunctionBodyDecoderTest, Int32Const1) {
TEST_F(FunctionBodyDecoderTest, RefFunc) {
builder.AddFunction(sigs.v_ii());
builder.AddFunction(sigs.ii_v());
ExpectValidates(sigs.a_v(), {kExprRefFunc, 1});
ExpectValidates(sigs.c_v(), {kExprRefFunc, 1});
}
TEST_F(FunctionBodyDecoderTest, EmptyFunction) {
@ -3394,15 +3394,13 @@ TEST_F(FunctionBodyDecoderTest, DeclarativeElemDrop) {
}
TEST_F(FunctionBodyDecoderTest, RefFuncDeclared) {
builder.InitializeTable(wasm::kWasmVoid);
byte function_index = builder.AddFunction(sigs.v_i());
ExpectValidates(sigs.a_v(), {WASM_REF_FUNC(function_index)});
ExpectValidates(sigs.c_v(), {WASM_REF_FUNC(function_index)});
}
TEST_F(FunctionBodyDecoderTest, RefFuncUndeclared) {
builder.InitializeTable(wasm::kWasmVoid);
byte function_index = builder.AddFunction(sigs.v_i(), false);
ExpectFailure(sigs.a_v(), {WASM_REF_FUNC(function_index)});
ExpectFailure(sigs.c_v(), {WASM_REF_FUNC(function_index)});
}
TEST_F(FunctionBodyDecoderTest, ElemSegmentIndexUnsigned) {