Add RuntimeHook API for instrumentation#697
Open
Asafrose wants to merge 1 commit intodop251:masterfrom
Open
Conversation
Add a minimal, low-overhead hook interface that enables building debuggers, profilers, tracers, and coverage tools on top of goja. The API consists of: - RuntimeHook interface with hooks for key execution points - BaseRuntimeHook for convenient embedding (implement only needed hooks) - HookResult enum for controlling execution (continue/pause) - Runtime methods: SetRuntimeHook, GetRuntimeHook, Resume, Scopes, VMState, LoadedScripts, FindPCsForLine - StackFrame methods: PC, SourceCode Hooks provided: - OnInstruction: called before each VM instruction (enables breakpoints, stepping) - OnFunctionEnter: called when entering a JS function - OnFunctionExit: called when exiting a JS function - OnException: called when an exception is thrown - OnPromiseReaction: called when a promise reaction is enqueued - OnVariableSet: called when a variable is assigned Design principles: - Zero overhead when no hook attached (single nil check in hot path) - Minimal API surface to reduce maintenance burden - Uses sync.Cond for pause/resume (no polling) - All hooks receive *Runtime to access full state Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a minimal, low-overhead hook interface that enables building debuggers, profilers, tracers, and coverage tools on top of goja.
Motivation
Currently, there's no way to instrument goja's execution for debugging or profiling purposes. Users who want to build development tools (debuggers, step-through execution, breakpoints, profilers, coverage analyzers) have no supported mechanism to hook into the runtime.
This PR addresses that by providing a clean, minimal API that:
API Overview
RuntimeHook Interface
BaseRuntimeHook
For convenience, users can embed
BaseRuntimeHookand only override the hooks they need:HookResult
Hooks that control execution flow return
HookResult:HookResultContinue- continue execution normallyHookResultPause- pause execution untilRuntime.Resume()is calledRuntime Methods
SetRuntimeHook(hook RuntimeHook)- attach a hookGetRuntimeHook() RuntimeHook- get current hookResume() error- resume paused executionScopes() []Scope- get current scope chain with variablesVMState() VMState- get low-level VM state (PC, SP, call depth, etc.)LoadedScripts() []LoadedScript- list loaded scriptsFindPCsForLine(filename string, line int) []int- find PCs for breakpointsStackFrame Methods
PC() int- get program counterSourceCode() string- get source code for the frameDesign Decisions
Single interface vs separate callbacks: Chose a single interface to match goja's existing patterns (e.g.,
FieldNameMapper) and provide better type safety.Pause mechanism: Uses
sync.Condrather than channels to avoid allocation overhead and enable efficient wait/signal semantics.Hook placement:
OnInstructionis in the main VM loop but guarded by a nil check, ensuring zero overhead when not debugging.No eval/setVariable: Intentionally omitted runtime eval and variable mutation to keep the API minimal and avoid potential security concerns. Debuggers can be built externally using the provided introspection APIs.
Performance
When no hook is attached, the only overhead is a single nil pointer check per instruction:
This compiles to a single comparison and conditional jump, with no allocation or function call overhead.
Testing
Comprehensive tests are included covering:
Example Usage
Checklist