aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-08-20 19:34:33 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-20 19:34:33 -0700
commit3de9ce5f30ad3a2a5615acb62a25cecdd2ddef33 (patch)
treeedbfe3f14bc8f09497aeea2b88d60d9a070404f4 /src
parent65280853acf2385eae124ef4870af2751ad662df (diff)
downloadbun-3de9ce5f30ad3a2a5615acb62a25cecdd2ddef33.tar.gz
bun-3de9ce5f30ad3a2a5615acb62a25cecdd2ddef33.tar.zst
bun-3de9ce5f30ad3a2a5615acb62a25cecdd2ddef33.zip
Implement `--inspect-brk` (#4222)
* Implement `--inspect-brk` * Bump WebKit --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src')
m---------src/bun.js/WebKit0
-rw-r--r--src/bun.js/bindings/BunDebugger.cpp41
-rw-r--r--src/bun.js/javascript.zig6
-rw-r--r--src/bun.js/module_loader.zig2
-rw-r--r--src/bundler.zig2
-rw-r--r--src/cli.zig16
-rw-r--r--src/cli/test_command.zig1
-rw-r--r--src/js_parser.zig14
-rw-r--r--src/runtime.zig2
9 files changed, 72 insertions, 12 deletions
diff --git a/src/bun.js/WebKit b/src/bun.js/WebKit
-Subproject fc705595a2e10361a64f1dab56527d7b112e75c
+Subproject fd79ce3120a692f4aed314c3da3dd452b4aa865
diff --git a/src/bun.js/bindings/BunDebugger.cpp b/src/bun.js/bindings/BunDebugger.cpp
index 41001a8f2..440a5125b 100644
--- a/src/bun.js/bindings/BunDebugger.cpp
+++ b/src/bun.js/bindings/BunDebugger.cpp
@@ -23,14 +23,40 @@ static WebCore::ScriptExecutionContext* debuggerScriptExecutionContext = nullptr
static WTF::Lock inspectorConnectionsLock = WTF::Lock();
static WTF::HashMap<ScriptExecutionContextIdentifier, Vector<BunInspectorConnection*, 8>>* inspectorConnections = nullptr;
+static bool waitingForConnection = false;
+extern "C" void Debugger__didConnect();
+
+class BunJSGlobalObjectDebuggable final : public JSC::JSGlobalObjectDebuggable {
+public:
+ using Base = JSC::JSGlobalObjectDebuggable;
+
+ BunJSGlobalObjectDebuggable(JSC::JSGlobalObject& globalObject)
+ : Base(globalObject)
+ {
+ }
+
+ ~BunJSGlobalObjectDebuggable() final
+ {
+ }
+
+ void pauseWaitingForAutomaticInspection() override
+ {
+ }
+ void unpauseForInitializedInspector() override
+ {
+ if (waitingForConnection) {
+ waitingForConnection = false;
+ Debugger__didConnect();
+ }
+ }
+};
+
enum class ConnectionStatus : int32_t {
Pending = 0,
Connected = 1,
Disconnecting = 2,
Disconnected = 3,
};
-static bool waitingForConnection = false;
-extern "C" void Debugger__didConnect();
class BunInspectorConnection : public Inspector::FrontendChannel {
@@ -81,15 +107,9 @@ public:
Bun__eventLoop__incrementRefConcurrently(reinterpret_cast<Zig::GlobalObject*>(globalObject)->bunVM(), 1);
}
globalObject->setInspectable(true);
-
auto& inspector = globalObject->inspectorDebuggable();
inspector.setInspectable(true);
-
- inspector.connect(*connection);
- if (waitingForConnection) {
- waitingForConnection = false;
- Debugger__didConnect();
- }
+ globalObject->inspectorController().connectFrontend(*connection, true, waitingForConnection);
Inspector::JSGlobalObjectDebugger* debugger = reinterpret_cast<Inspector::JSGlobalObjectDebugger*>(globalObject->debugger());
if (debugger) {
@@ -99,7 +119,6 @@ public:
}
connection->receiveMessagesOnInspectorThread(context, reinterpret_cast<Zig::GlobalObject*>(globalObject));
-
break;
}
default: {
@@ -415,6 +434,8 @@ extern "C" void Bun__ensureDebugger(ScriptExecutionContextIdentifier scriptId, b
auto* globalObject = ScriptExecutionContext::getScriptExecutionContext(scriptId)->jsGlobalObject();
globalObject->m_inspectorController = makeUnique<Inspector::JSGlobalObjectInspectorController>(*globalObject, Bun::BunInjectedScriptHost::create());
+ globalObject->m_inspectorDebuggable = makeUnique<BunJSGlobalObjectDebuggable>(*globalObject);
+
globalObject->setInspectable(true);
auto& inspector = globalObject->inspectorDebuggable();
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index 98e61f3b0..07d3f64cf 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -748,6 +748,8 @@ pub const VirtualMachine = struct {
next_debugger_id: u64 = 1,
poll_ref: JSC.PollRef = .{},
wait_for_connection: bool = false,
+ set_breakpoint_on_first_line: bool = false,
+
const debug = Output.scoped(.DEBUGGER, false);
extern "C" fn Bun__createJSDebugger(*JSC.JSGlobalObject) u32;
@@ -784,6 +786,8 @@ pub const VirtualMachine = struct {
Bun__ensureDebugger(debugger.script_execution_context_id, debugger.wait_for_connection);
while (debugger.wait_for_connection) {
this.eventLoop().tick();
+ if (debugger.wait_for_connection)
+ this.eventLoop().autoTickActive();
}
}
@@ -1136,6 +1140,7 @@ pub const VirtualMachine = struct {
this.debugger = Debugger{
.path_or_port = debugger.enable.path_or_port,
.wait_for_connection = debugger.enable.wait_for_connection,
+ .set_breakpoint_on_first_line = debugger.enable.set_breakpoint_on_first_line,
};
},
}
@@ -1870,6 +1875,7 @@ pub const VirtualMachine = struct {
pub fn reloadEntryPoint(this: *VirtualMachine, entry_path: []const u8) !*JSInternalPromise {
this.has_loaded = false;
this.main = entry_path;
+ this.main_hash = bun.JSC.Watcher.getHash(entry_path);
try this.entry_point.generate(
this.allocator,
diff --git a/src/bun.js/module_loader.zig b/src/bun.js/module_loader.zig
index be10b9722..dfb9077e2 100644
--- a/src/bun.js/module_loader.zig
+++ b/src/bun.js/module_loader.zig
@@ -403,6 +403,7 @@ pub const RuntimeTranspilerStore = struct {
vm.main.len == path.text.len and
vm.main_hash == hash and
strings.eqlLong(vm.main, path.text, false),
+ .set_breakpoint_on_first_line = vm.debugger != null and vm.debugger.?.set_breakpoint_on_first_line and strings.eqlLong(vm.main, path.text, false),
};
defer {
@@ -1439,6 +1440,7 @@ pub const ModuleLoader = struct {
.dont_bundle_twice = true,
.allow_commonjs = true,
.inject_jest_globals = jsc_vm.bundler.options.rewrite_jest_for_tests and is_main,
+ .set_breakpoint_on_first_line = is_main and jsc_vm.debugger != null and jsc_vm.debugger.?.set_breakpoint_on_first_line,
};
defer {
if (should_close_input_file_fd and input_file_fd != 0) {
diff --git a/src/bundler.zig b/src/bundler.zig
index b5912a96f..ce7d9fc42 100644
--- a/src/bundler.zig
+++ b/src/bundler.zig
@@ -1188,6 +1188,7 @@ pub const Bundler = struct {
virtual_source: ?*const logger.Source = null,
replace_exports: runtime.Runtime.Features.ReplaceableExport.Map = .{},
inject_jest_globals: bool = false,
+ set_breakpoint_on_first_line: bool = false,
dont_bundle_twice: bool = false,
allow_commonjs: bool = false,
@@ -1302,6 +1303,7 @@ pub const Bundler = struct {
var opts = js_parser.Parser.Options.init(jsx, loader);
opts.legacy_transform_require_to_import = bundler.options.allow_runtime and !bundler.options.target.isBun();
opts.features.allow_runtime = bundler.options.allow_runtime;
+ opts.features.set_breakpoint_on_first_line = this_parse.set_breakpoint_on_first_line;
opts.features.trim_unused_imports = bundler.options.trim_unused_imports orelse loader.isTypeScript();
opts.features.should_fold_typescript_constant_expressions = loader.isTypeScript() or target.isBun() or bundler.options.minify_syntax;
opts.features.dynamic_require = target.isBun();
diff --git a/src/cli.zig b/src/cli.zig
index 88b6aa057..0fb618afc 100644
--- a/src/cli.zig
+++ b/src/cli.zig
@@ -161,7 +161,8 @@ pub const Arguments = struct {
clap.parseParam("--no-macros Disable macros from being executed in the bundler, transpiler and runtime") catch unreachable,
clap.parseParam("--target <STR> The intended execution environment for the bundle. \"browser\", \"bun\" or \"node\"") catch unreachable,
clap.parseParam("--inspect <STR>? Activate Bun's Debugger") catch unreachable,
- clap.parseParam("--inspect-wait <STR>? Activate Bun's Debugger and wait for a connection before executing") catch unreachable,
+ clap.parseParam("--inspect-wait <STR>? Activate Bun's Debugger, wait for a connection before executing") catch unreachable,
+ clap.parseParam("--inspect-brk <STR>? Activate Bun's Debugger, set breakpoint on first line of code and wait") catch unreachable,
clap.parseParam("<POS>... ") catch unreachable,
};
@@ -530,6 +531,18 @@ pub const Arguments = struct {
.path_or_port = inspect_flag,
.wait_for_connection = true,
} };
+ } else if (args.option("--inspect-brk")) |inspect_flag| {
+ ctx.runtime_options.debugger = if (inspect_flag.len == 0)
+ Command.Debugger{ .enable = .{
+ .wait_for_connection = true,
+ .set_breakpoint_on_first_line = true,
+ } }
+ else
+ Command.Debugger{ .enable = .{
+ .path_or_port = inspect_flag,
+ .wait_for_connection = true,
+ .set_breakpoint_on_first_line = true,
+ } };
}
}
@@ -969,6 +982,7 @@ pub const Command = struct {
enable: struct {
path_or_port: []const u8 = "",
wait_for_connection: bool = false,
+ set_breakpoint_on_first_line: bool = false,
},
};
diff --git a/src/cli/test_command.zig b/src/cli/test_command.zig
index 5686659c8..5bf48d7d9 100644
--- a/src/cli/test_command.zig
+++ b/src/cli/test_command.zig
@@ -924,7 +924,6 @@ pub const TestCommand = struct {
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#grouping-log-lines
const file_prefix = if (Output.is_github_action) "::group::" else "";
- vm.main_hash = @as(u32, @truncate(bun.hash(file_path)));
var repeat_count = reporter.repeat_count;
var repeat_index: u32 = 0;
while (repeat_index < repeat_count) : (repeat_index += 1) {
diff --git a/src/js_parser.zig b/src/js_parser.zig
index bb767d875..ebe2a01c9 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -2950,6 +2950,20 @@ pub const Parser = struct {
before.deinit();
}
+ // --inspect-brk
+ if (p.options.features.set_breakpoint_on_first_line) {
+ var debugger_stmts = try p.allocator.alloc(Stmt, 1);
+ debugger_stmts[0] = Stmt{
+ .data = .{ .s_debugger = .{} },
+ .loc = logger.Loc.Empty,
+ };
+ before.append(
+ js_ast.Part{
+ .stmts = debugger_stmts,
+ },
+ ) catch unreachable;
+ }
+
if (p.options.bundle) {
// allocate an empty part for the bundle
before.append(
diff --git a/src/runtime.zig b/src/runtime.zig
index 790f5b197..2c22f392e 100644
--- a/src/runtime.zig
+++ b/src/runtime.zig
@@ -290,6 +290,8 @@ pub const Runtime = struct {
minify_syntax: bool = false,
minify_identifiers: bool = false,
+ set_breakpoint_on_first_line: bool = false,
+
/// Instead of jsx("div", {}, void 0)
/// ->
/// {