Commit de52d865 authored by clemensh's avatar clemensh Committed by Commit bot

[wasm] Move and refactor position to location translation

The GetPositionInfo function only operates on WasmCompiledModule, so it
should be a method of that class.
This CL also splits the method in two, such that I can reuse the
GetContainingFunction method for breakpoint support.

R=titzer@chromium.org
BUG=chromium:613110

Review-Url: https://codereview.chromium.org/2521293002
Cr-Commit-Position: refs/heads/master@{#41191}
parent 339b0a09
......@@ -13474,8 +13474,8 @@ bool Script::GetPositionInfo(int position, PositionInfo* info,
Handle<WasmCompiledModule> compiled_module(
WasmCompiledModule::cast(wasm_compiled_module()));
DCHECK_LE(0, position);
return wasm::GetPositionInfo(compiled_module,
static_cast<uint32_t>(position), info);
return compiled_module->GetPositionInfo(static_cast<uint32_t>(position),
info);
}
if (line_ends()->IsUndefined(GetIsolate())) {
......
......@@ -767,35 +767,6 @@ int wasm::GetFunctionCodeOffset(Handle<WasmCompiledModule> compiled_module,
return GetFunctionOffsetAndLength(compiled_module, func_index).first;
}
bool wasm::GetPositionInfo(Handle<WasmCompiledModule> compiled_module,
uint32_t position, Script::PositionInfo* info) {
std::vector<WasmFunction>& functions = compiled_module->module()->functions;
// Binary search for a function containing the given position.
int left = 0; // inclusive
int right = static_cast<int>(functions.size()); // exclusive
if (right == 0) return false;
while (right - left > 1) {
int mid = left + (right - left) / 2;
if (functions[mid].code_start_offset <= position) {
left = mid;
} else {
right = mid;
}
}
// If the found entry does not contains the given position, return false.
WasmFunction& func = functions[left];
if (position < func.code_start_offset || position >= func.code_end_offset) {
return false;
}
info->line = left;
info->column = position - func.code_start_offset;
info->line_start = func.code_start_offset;
info->line_end = func.code_end_offset;
return true;
}
WasmModule::WasmModule(Zone* owned, const byte* module_start)
: owned_zone(owned),
module_start(module_start),
......
......@@ -413,11 +413,6 @@ V8_EXPORT_PRIVATE bool ValidateModuleBytes(Isolate* isolate, const byte* start,
int GetFunctionCodeOffset(Handle<WasmCompiledModule> compiled_module,
int func_index);
// 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.
bool GetPositionInfo(Handle<WasmCompiledModule> compiled_module,
uint32_t position, Script::PositionInfo* info);
// Assumed to be called with a code object associated to a wasm module instance.
// Intended to be called from runtime functions.
// Returns nullptr on failing to get owning instance.
......
......@@ -385,3 +385,42 @@ int WasmCompiledModule::GetFunctionOffset(uint32_t func_index) const {
functions[func_index].code_start_offset);
return static_cast<int>(functions[func_index].code_start_offset);
}
int WasmCompiledModule::GetContainingFunction(uint32_t byte_offset) const {
std::vector<WasmFunction>& functions = module()->functions;
// Binary search for a function containing the given position.
int left = 0; // inclusive
int right = static_cast<int>(functions.size()); // exclusive
if (right == 0) return false;
while (right - left > 1) {
int mid = left + (right - left) / 2;
if (functions[mid].code_start_offset <= byte_offset) {
left = mid;
} else {
right = mid;
}
}
// If the found function does not contains the given position, return -1.
WasmFunction& func = functions[left];
if (byte_offset < func.code_start_offset ||
byte_offset >= func.code_end_offset) {
return -1;
}
return left;
}
bool WasmCompiledModule::GetPositionInfo(uint32_t position,
Script::PositionInfo* info) {
int func_index = GetContainingFunction(position);
if (func_index < 0) return false;
WasmFunction& function = module()->functions[func_index];
info->line = func_index;
info->column = position - function.code_start_offset;
info->line_start = function.code_start_offset;
info->line_end = function.code_end_offset;
return true;
}
......@@ -276,6 +276,16 @@ class WasmCompiledModule : public FixedArray {
// Returns -1 if the function index is invalid.
int GetFunctionOffset(uint32_t func_index) const;
// 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) const;
// 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);
private:
void InitId();
......
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