Commit 8c7a413c authored by clemensh's avatar clemensh Committed by Commit bot

[wasm] Remove three fields from wasm object

Use information in the WasmCompiledModule instead.

R=titzer@chromium.org,mtrofin@chromium.org

Review-Url: https://codereview.chromium.org/2396043002
Cr-Commit-Position: refs/heads/master@{#40062}
parent 550364fb
......@@ -1104,7 +1104,7 @@ FunctionOffsetsResult DecodeWasmFunctionOffsets(
uint32_t functions_count = decoder.consume_u32v("functions count");
// Take care of invalid input here.
if (functions_count < static_cast<unsigned>(code_section.length()) / 2)
table.reserve(functions_count);
table.reserve(num_imported_functions + functions_count);
int section_offset = static_cast<int>(code_section.start() - module_start);
DCHECK_LE(0, section_offset);
for (uint32_t i = 0; i < functions_count && decoder.ok(); ++i) {
......
......@@ -31,12 +31,11 @@ ByteArray *GetOrCreateFunctionOffsetTable(Handle<WasmDebugInfo> debug_info) {
FunctionOffsetsResult function_offsets;
{
DisallowHeapAllocation no_gc;
Handle<JSObject> wasm_object(debug_info->wasm_object(), isolate);
uint32_t num_imported_functions =
wasm::GetNumImportedFunctions(wasm_object);
SeqOneByteString *wasm_bytes =
wasm::GetWasmBytes(debug_info->wasm_object());
static_cast<uint32_t>(wasm::GetNumImportedFunctions(wasm_object));
Handle<SeqOneByteString> wasm_bytes = wasm::GetWasmBytes(wasm_object);
DisallowHeapAllocation no_gc;
const byte *bytes_start = wasm_bytes->GetChars();
const byte *bytes_end = bytes_start + wasm_bytes->length();
function_offsets = wasm::DecodeWasmFunctionOffsets(bytes_start, bytes_end,
......@@ -72,8 +71,8 @@ std::pair<int, int> GetFunctionOffsetAndLength(Handle<WasmDebugInfo> debug_info,
Vector<const uint8_t> GetFunctionBytes(Handle<WasmDebugInfo> debug_info,
int func_index) {
SeqOneByteString *module_bytes =
wasm::GetWasmBytes(debug_info->wasm_object());
Handle<JSObject> wasm_object(debug_info->wasm_object());
Handle<SeqOneByteString> module_bytes = wasm::GetWasmBytes(wasm_object);
std::pair<int, int> offset_and_length =
GetFunctionOffsetAndLength(debug_info, func_index);
return Vector<const uint8_t>(
......@@ -90,7 +89,7 @@ Handle<WasmDebugInfo> WasmDebugInfo::New(Handle<JSObject> wasm) {
factory->NewFixedArray(kWasmDebugInfoNumEntries, TENURED);
arr->set(kWasmDebugInfoWasmObj, *wasm);
int hash = 0;
Handle<SeqOneByteString> wasm_bytes(GetWasmBytes(*wasm), isolate);
Handle<SeqOneByteString> wasm_bytes = GetWasmBytes(wasm);
{
DisallowHeapAllocation no_gc;
hash = StringHasher::HashSequentialString(
......@@ -130,7 +129,8 @@ Script *WasmDebugInfo::GetFunctionScript(Handle<WasmDebugInfo> debug_info,
Object *scripts_obj = debug_info->get(kWasmDebugInfoFunctionScripts);
Handle<FixedArray> scripts;
if (scripts_obj->IsUndefined(isolate)) {
int num_functions = wasm::GetNumberOfFunctions(debug_info->wasm_object());
Handle<JSObject> wasm_object(debug_info->wasm_object(), isolate);
int num_functions = wasm::GetNumberOfFunctions(wasm_object);
scripts = isolate->factory()->NewFixedArray(num_functions, TENURED);
debug_info->set(kWasmDebugInfoFunctionScripts, *scripts);
} else {
......
......@@ -55,12 +55,7 @@ enum WasmInstanceObjectFields {
kWasmModuleCodeTable,
kWasmMemArrayBuffer,
kWasmGlobalsArrayBuffer,
// TODO(clemensh): Remove function name array, extract names from module
// bytes.
kWasmFunctionNamesArray,
kWasmModuleBytesString,
kWasmDebugInfo,
kWasmNumImportedFunctions,
kWasmModuleInternalFieldCount
};
......@@ -352,6 +347,7 @@ Address GetGlobalStartAddressFromCodeTemplate(Object* undefined,
}
Handle<FixedArray> EncodeImports(Factory* factory, const WasmModule* module) {
// TODO(wasm): Encode this in one big FixedArray.
Handle<FixedArray> ret = factory->NewFixedArray(
static_cast<int>(module->import_table.size()), TENURED);
......@@ -852,10 +848,20 @@ Object* GetOwningWasmInstance(Code* code) {
return cell->value();
}
uint32_t GetNumImportedFunctions(Handle<JSObject> wasm_object) {
return static_cast<uint32_t>(
Smi::cast(wasm_object->GetInternalField(kWasmNumImportedFunctions))
->value());
int GetNumImportedFunctions(Handle<JSObject> wasm_object) {
// TODO(wasm): Cache this number if it ever becomes a performance problem.
DCHECK(IsWasmObject(*wasm_object));
Object* compiled_module = wasm_object->GetInternalField(kWasmCompiledModule);
Handle<FixedArray> imports =
WasmCompiledModule::cast(compiled_module)->imports();
int num_imports = imports->length();
int num_imported_functions = 0;
for (int i = 0; i < num_imports; ++i) {
FixedArray* encoded_import = FixedArray::cast(imports->get(i));
int kind = Smi::cast(encoded_import->get(kImportKind))->value();
if (kind == kExternalFunction) ++num_imported_functions;
}
return num_imported_functions;
}
WasmModule::WasmModule(byte* module_start)
......@@ -1306,26 +1312,6 @@ class WasmInstanceBuilder {
//--------------------------------------------------------------------------
ProcessInits(globals);
//--------------------------------------------------------------------------
// Set up the debug support for the new instance.
//--------------------------------------------------------------------------
// TODO(clemensh): avoid referencing this stuff from the instance, use it
// off the compiled module instead. See the following 3 assignments:
if (compiled_module_->has_module_bytes()) {
instance->SetInternalField(kWasmModuleBytesString,
compiled_module_->ptr_to_module_bytes());
}
if (compiled_module_->has_function_names()) {
instance->SetInternalField(kWasmFunctionNamesArray,
compiled_module_->ptr_to_function_names());
}
{
Handle<Object> handle = factory->NewNumber(num_imported_functions);
instance->SetInternalField(kWasmNumImportedFunctions, *handle);
}
//--------------------------------------------------------------------------
// Set up the runtime support for the new instance.
//--------------------------------------------------------------------------
......@@ -1934,15 +1920,14 @@ void WasmCompiledModule::PrintInstancesChain() {
Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm,
uint32_t func_index) {
if (!wasm->IsUndefined(isolate)) {
Handle<ByteArray> func_names_arr_obj(
ByteArray::cast(Handle<JSObject>::cast(wasm)->GetInternalField(
kWasmFunctionNamesArray)),
isolate);
DCHECK(IsWasmObject(*wasm));
WasmCompiledModule* compiled_module = WasmCompiledModule::cast(
Handle<JSObject>::cast(wasm)->GetInternalField(kWasmCompiledModule));
Handle<ByteArray> func_names = compiled_module->function_names();
// TODO(clemens): Extract this from the module bytes; skip whole function
// name table.
Handle<Object> name;
if (GetWasmFunctionNameFromTable(func_names_arr_obj, func_index)
.ToHandle(&name)) {
if (GetWasmFunctionNameFromTable(func_names, func_index).ToHandle(&name)) {
return name;
}
}
......@@ -1971,7 +1956,6 @@ bool IsWasmObject(Object* object) {
Object* mem = obj->GetInternalField(kWasmMemArrayBuffer);
if (!obj->GetInternalField(kWasmModuleCodeTable)->IsFixedArray() ||
!(mem->IsUndefined(isolate) || mem->IsJSArrayBuffer()) ||
!obj->GetInternalField(kWasmFunctionNamesArray)->IsByteArray() ||
!WasmCompiledModule::IsWasmCompiledModule(
obj->GetInternalField(kWasmCompiledModule))) {
return false;
......@@ -1981,8 +1965,10 @@ bool IsWasmObject(Object* object) {
return true;
}
SeqOneByteString* GetWasmBytes(JSObject* wasm) {
return SeqOneByteString::cast(wasm->GetInternalField(kWasmModuleBytesString));
Handle<SeqOneByteString> GetWasmBytes(Handle<JSObject> wasm) {
DCHECK(IsWasmObject(*wasm));
Object* compiled_module = wasm->GetInternalField(kWasmCompiledModule);
return WasmCompiledModule::cast(compiled_module)->module_bytes();
}
Handle<WasmDebugInfo> GetDebugInfo(Handle<JSObject> wasm) {
......@@ -2061,10 +2047,13 @@ void PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size,
}
}
int GetNumberOfFunctions(JSObject* wasm) {
Object* func_names_obj = wasm->GetInternalField(kWasmFunctionNamesArray);
int GetNumberOfFunctions(Handle<JSObject> wasm) {
DCHECK(IsWasmObject(*wasm));
WasmCompiledModule* compiled_module =
WasmCompiledModule::cast(wasm->GetInternalField(kWasmCompiledModule));
ByteArray* func_names_arr = compiled_module->ptr_to_function_names();
// TODO(clemensh): this looks inside an array constructed elsewhere. Refactor.
return ByteArray::cast(func_names_obj)->get_int(0);
return func_names_arr->get_int(0);
}
Handle<JSObject> CreateCompiledModuleObject(Isolate* isolate,
......
......@@ -478,14 +478,14 @@ Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm,
uint32_t func_index);
// Return the binary source bytes of a wasm module.
SeqOneByteString* GetWasmBytes(JSObject* wasm);
Handle<SeqOneByteString> GetWasmBytes(Handle<JSObject> wasm);
// Get the debug info associated with the given wasm object.
// If no debug info exists yet, it is created automatically.
Handle<WasmDebugInfo> GetDebugInfo(Handle<JSObject> wasm);
// Return the number of functions in the given wasm object.
int GetNumberOfFunctions(JSObject* wasm);
int GetNumberOfFunctions(Handle<JSObject> wasm);
// Create and export JSFunction
Handle<JSFunction> WrapExportCodeAsJSFunction(Isolate* isolate,
......@@ -530,7 +530,7 @@ V8_EXPORT_PRIVATE bool ValidateModuleBytes(Isolate* isolate, const byte* start,
ModuleOrigin origin);
// Get the number of imported functions for a WASM instance.
uint32_t GetNumImportedFunctions(Handle<JSObject> wasm_object);
int GetNumImportedFunctions(Handle<JSObject> wasm_object);
// Assumed to be called with a code object associated to a wasm module instance.
// Intended to be called from runtime functions.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment