diff --git a/src/objects/objects.cc b/src/objects/objects.cc index 7703b9cf86..0cd74334d8 100644 --- a/src/objects/objects.cc +++ b/src/objects/objects.cc @@ -4656,8 +4656,26 @@ bool Script::GetPositionInfo(int position, PositionInfo* info, // directly. if (type() == Script::TYPE_WASM) { DCHECK_LE(0, position); - return WasmModuleObject::cast(wasm_module_object()) - .GetPositionInfo(static_cast(position), info); + wasm::NativeModule* native_module = wasm_native_module(); + const wasm::WasmModule* module = native_module->module(); + if (source_mapping_url().IsString()) { + if (module->functions.size() == 0) return false; + info->line = 0; + info->column = position; + info->line_start = module->functions[0].code.offset(); + info->line_end = module->functions.back().code.end_offset(); + return true; + } + int func_index = GetContainingWasmFunction(module, position); + if (func_index < 0) return false; + + const wasm::WasmFunction& function = module->functions[func_index]; + + info->line = func_index; + info->column = position - function.code.offset(); + info->line_start = function.code.offset(); + info->line_end = function.code.end_offset(); + return true; } if (line_ends().IsUndefined()) { diff --git a/src/wasm/wasm-module.cc b/src/wasm/wasm-module.cc index cc6d92cb8a..033f12ae24 100644 --- a/src/wasm/wasm-module.cc +++ b/src/wasm/wasm-module.cc @@ -67,6 +67,32 @@ int GetWasmFunctionOffset(const WasmModule* module, uint32_t func_index) { return static_cast(functions[func_index].code.offset()); } +// static +int GetContainingWasmFunction(const WasmModule* module, uint32_t byte_offset) { + const std::vector& functions = module->functions; + + // Binary search for a function containing the given position. + int left = 0; // inclusive + int right = static_cast(functions.size()); // exclusive + if (right == 0) return false; + while (right - left > 1) { + int mid = left + (right - left) / 2; + if (functions[mid].code.offset() <= byte_offset) { + left = mid; + } else { + right = mid; + } + } + // If the found function does not contains the given position, return -1. + const WasmFunction& func = functions[left]; + if (byte_offset < func.code.offset() || + byte_offset >= func.code.end_offset()) { + return -1; + } + + return left; +} + // static v8::debug::WasmDisassembly DisassembleWasmFunction( const WasmModule* module, const ModuleWireBytes& wire_bytes, diff --git a/src/wasm/wasm-module.h b/src/wasm/wasm-module.h index 64223dff07..600020219b 100644 --- a/src/wasm/wasm-module.h +++ b/src/wasm/wasm-module.h @@ -250,6 +250,11 @@ int GetExportWrapperIndex(const WasmModule* module, const FunctionSig* sig, // Returns -1 if the function index is invalid. int GetWasmFunctionOffset(const WasmModule* module, uint32_t func_index); +// Returns the function containing the given byte offset. +// Returns -1 if the byte offset is not contained in any function of this +// module. +int GetContainingWasmFunction(const WasmModule* module, uint32_t byte_offset); + // Compute the disassembly of a wasm function. // Returns the disassembly string and a list of // entries, mapping wasm byte offsets to line and column in the disassembly. diff --git a/src/wasm/wasm-objects.cc b/src/wasm/wasm-objects.cc index a93e52170b..7619f3c11a 100644 --- a/src/wasm/wasm-objects.cc +++ b/src/wasm/wasm-objects.cc @@ -258,9 +258,10 @@ bool WasmModuleObject::SetBreakPoint(Handle module_object, Isolate* isolate = module_object->GetIsolate(); // Find the function for this breakpoint. - int func_index = module_object->GetContainingFunction(*position); + const WasmModule* module = module_object->module(); + int func_index = GetContainingWasmFunction(module, *position); if (func_index < 0) return false; - const WasmFunction& func = module_object->module()->functions[func_index]; + const WasmFunction& func = module->functions[func_index]; int offset_in_func = *position - func.code.offset(); // According to the current design, we should only be called with valid @@ -403,9 +404,10 @@ void WasmModuleObject::SetBreakpointsOnNewInstance( int position = breakpoint_info->source_position(); // Find the function for this breakpoint, and set the breakpoint. - int func_index = module_object->GetContainingFunction(position); + const WasmModule* module = module_object->module(); + int func_index = GetContainingWasmFunction(module, position); DCHECK_LE(0, func_index); - const WasmFunction& func = module_object->module()->functions[func_index]; + const WasmFunction& func = module->functions[func_index]; int offset_in_func = position - func.code.offset(); WasmDebugInfo::SetBreakpoint(debug_info, func_index, offset_in_func); } @@ -689,53 +691,6 @@ Vector WasmModuleObject::GetRawFunctionName( return Vector::cast(name); } -int WasmModuleObject::GetContainingFunction(uint32_t byte_offset) { - const std::vector& functions = module()->functions; - - // Binary search for a function containing the given position. - int left = 0; // inclusive - int right = static_cast(functions.size()); // exclusive - if (right == 0) return false; - while (right - left > 1) { - int mid = left + (right - left) / 2; - if (functions[mid].code.offset() <= byte_offset) { - left = mid; - } else { - right = mid; - } - } - // If the found function does not contains the given position, return -1. - const WasmFunction& func = functions[left]; - if (byte_offset < func.code.offset() || - byte_offset >= func.code.end_offset()) { - return -1; - } - - return left; -} - -bool WasmModuleObject::GetPositionInfo(uint32_t position, - Script::PositionInfo* info) { - if (script().source_mapping_url().IsString()) { - if (module()->functions.size() == 0) return false; - info->line = 0; - info->column = position; - info->line_start = module()->functions[0].code.offset(); - info->line_end = module()->functions.back().code.end_offset(); - return true; - } - int func_index = GetContainingFunction(position); - if (func_index < 0) return false; - - const WasmFunction& function = module()->functions[func_index]; - - info->line = func_index; - info->column = position - function.code.offset(); - info->line_start = function.code.offset(); - info->line_end = function.code.end_offset(); - return true; -} - Handle WasmTableObject::New(Isolate* isolate, wasm::ValueType type, uint32_t initial, bool has_maximum, diff --git a/src/wasm/wasm-objects.h b/src/wasm/wasm-objects.h index fdd925a945..a79fcf856c 100644 --- a/src/wasm/wasm-objects.h +++ b/src/wasm/wasm-objects.h @@ -12,7 +12,6 @@ #include "src/debug/debug.h" #include "src/heap/heap.h" #include "src/objects/objects.h" -#include "src/objects/script.h" #include "src/wasm/value-type.h" // Has to be the last include (doesn't have include guards) @@ -198,16 +197,6 @@ class WasmModuleObject : public JSObject { // Does not allocate, hence gc-safe. Vector GetRawFunctionName(uint32_t func_index); - // Returns the function containing the given byte offset. - // Returns -1 if the byte offset is not contained in any function of this - // module. - int GetContainingFunction(uint32_t byte_offset); - - // Translate from byte offset in the module to function number and byte offset - // within that function, encoded as line and column in the position info. - // Returns true if the position is valid inside this module, false otherwise. - bool GetPositionInfo(uint32_t position, Script::PositionInfo* info); - // Get the source position from a given function index and byte offset, // for either asm.js or pure Wasm modules. static int GetSourcePosition(Handle, uint32_t func_index,