diff options
author | 2023-08-19 00:20:23 -0700 | |
---|---|---|
committer | 2023-08-19 00:20:23 -0700 | |
commit | 86ad0151479c96314233c2d7dfbf7ed83b57feab (patch) | |
tree | 643f3392296fcc8a739a60219d309fb5d78d758a /src/bun.js/javascript.zig | |
parent | db09ed15fd561b89b24b979b986e21a04576f7cc (diff) | |
download | bun-86ad0151479c96314233c2d7dfbf7ed83b57feab.tar.gz bun-86ad0151479c96314233c2d7dfbf7ed83b57feab.tar.zst bun-86ad0151479c96314233c2d7dfbf7ed83b57feab.zip |
Add inline sourcemaps when `--inspect` is enabled (#4213)
* Add inline sourcemaps when --inspect is enabled
* Add some assertions
* Update javascript.zig
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/javascript.zig')
-rw-r--r-- | src/bun.js/javascript.zig | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 1b9520dde..e57702e8b 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -509,6 +509,51 @@ pub const VirtualMachine = struct { return this.rareData().mimeTypeFromString(this.allocator, str); } + const SourceMapHandlerGetter = struct { + vm: *VirtualMachine, + printer: *js_printer.BufferPrinter, + + pub fn get(this: *SourceMapHandlerGetter) js_printer.SourceMapHandler { + if (this.vm.debugger == null) { + return SavedSourceMap.SourceMapHandler.init(&this.vm.source_mappings); + } + + return js_printer.SourceMapHandler.For(SourceMapHandlerGetter, onChunk).init(this); + } + + /// When the inspector is enabled, we want to generate an inline sourcemap. + /// And, for now, we also store it in source_mappings like normal + /// This is hideously expensive memory-wise... + pub fn onChunk(this: *SourceMapHandlerGetter, chunk: SourceMap.Chunk, source: logger.Source) anyerror!void { + var temp_json_buffer = bun.MutableString.initEmpty(bun.default_allocator); + defer temp_json_buffer.deinit(); + temp_json_buffer = try chunk.printSourceMapContents(source, temp_json_buffer, true, true); + const source_map_url_prefix_start = "//# sourceMappingURL=data:application/json;base64,"; + // TODO: do we need to %-encode the path? + const source_url_len = source.path.text.len; + const source_mapping_url = "\n//# sourceURL="; + const prefix_len = source_map_url_prefix_start.len + source_mapping_url.len + source_url_len; + + try this.vm.source_mappings.putMappings(source, chunk.buffer); + const encode_len = bun.base64.encodeLen(temp_json_buffer.list.items); + try this.printer.ctx.buffer.growIfNeeded(encode_len + prefix_len + 2); + this.printer.ctx.buffer.appendAssumeCapacity("\n" ++ source_map_url_prefix_start); + _ = bun.base64.encode(this.printer.ctx.buffer.list.items.ptr[this.printer.ctx.buffer.len()..this.printer.ctx.buffer.list.capacity], temp_json_buffer.list.items); + this.printer.ctx.buffer.list.items.len += encode_len; + this.printer.ctx.buffer.appendAssumeCapacity(source_mapping_url); + // TODO: do we need to %-encode the path? + this.printer.ctx.buffer.appendAssumeCapacity(source.path.text); + try this.printer.ctx.buffer.append("\n"); + } + }; + + pub inline fn sourceMapHandler(this: *VirtualMachine, printer: *js_printer.BufferPrinter) SourceMapHandlerGetter { + return SourceMapHandlerGetter{ + .vm = this, + .printer = printer, + }; + } + pub const GCLevel = enum(u3) { none = 0, mild = 1, |