1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:28:12 +00:00

LibJS: Add file & line number to bytecode VM stack traces :^)

This works by adding source start/end offset to every bytecode
instruction. In the future we can make this more efficient by keeping
a map of bytecode ranges to source ranges in the Executable instead,
but let's just get traces working first.

Co-Authored-By: Andrew Kaster <akaster@serenityos.org>
This commit is contained in:
Andreas Kling 2023-09-01 16:53:55 +02:00
parent 0b66656ca9
commit 1c06111cbd
16 changed files with 157 additions and 26 deletions

View file

@ -8,7 +8,9 @@
#include <AK/Forward.h>
#include <AK/Span.h>
#include <LibJS/Bytecode/Executable.h>
#include <LibJS/Forward.h>
#include <LibJS/SourceRange.h>
#define ENUMERATE_BYTECODE_OPS(O) \
O(Add) \
@ -137,6 +139,10 @@ public:
ThrowCompletionOr<void> execute(Bytecode::Interpreter&) const;
static void destroy(Instruction&);
// FIXME: Find a better way to organize this information
void set_source_record(SourceRecord rec) { m_source_record = rec; }
SourceRecord source_record() const { return m_source_record; }
protected:
explicit Instruction(Type type)
: m_type(type)
@ -144,13 +150,15 @@ protected:
}
private:
SourceRecord m_source_record {};
Type m_type {};
};
class InstructionStreamIterator {
public:
explicit InstructionStreamIterator(ReadonlyBytes bytes)
InstructionStreamIterator(ReadonlyBytes bytes, Executable const* executable = nullptr)
: m_bytes(bytes)
, m_executable(executable)
{
}
@ -170,11 +178,15 @@ public:
m_offset += dereference().length();
}
UnrealizedSourceRange source_range() const;
RefPtr<SourceCode> source_code() const;
private:
Instruction const& dereference() const { return *reinterpret_cast<Instruction const*>(m_bytes.data() + offset()); }
ReadonlyBytes m_bytes;
size_t m_offset { 0 };
Executable const* m_executable { nullptr };
};
}