diff options
author | 2022-07-04 08:42:29 -0700 | |
---|---|---|
committer | 2022-07-04 08:42:29 -0700 | |
commit | cf5939c425ac72fef23448d0dcd68ae0aa59ef73 (patch) | |
tree | 14043a0c10db5530179fcd2c9ea1bba0d3ad91cc /src/bun.js | |
parent | bbcc08dbb44dd28f7141e6f50a7cb5edfb8b4987 (diff) | |
download | bun-cf5939c425ac72fef23448d0dcd68ae0aa59ef73.tar.gz bun-cf5939c425ac72fef23448d0dcd68ae0aa59ef73.tar.zst bun-cf5939c425ac72fef23448d0dcd68ae0aa59ef73.zip |
[jsc] Make JSC own the memory for source code strings
Diffstat (limited to 'src/bun.js')
-rw-r--r-- | src/bun.js/bindings/ZigSourceProvider.cpp | 14 | ||||
-rw-r--r-- | src/bun.js/javascript.zig | 45 |
2 files changed, 40 insertions, 19 deletions
diff --git a/src/bun.js/bindings/ZigSourceProvider.cpp b/src/bun.js/bindings/ZigSourceProvider.cpp index 4e426b449..c2959a8fa 100644 --- a/src/bun.js/bindings/ZigSourceProvider.cpp +++ b/src/bun.js/bindings/ZigSourceProvider.cpp @@ -36,13 +36,23 @@ Ref<SourceProvider> SourceProvider::create(ResolvedSource resolvedSource) JSC::SourceProviderSourceType sourceType = JSC::SourceProviderSourceType::Module; + // JSC owns the memory + if (resolvedSource.hash == 1) { + Ref<WTF::StringImpl> stringImpl_ = WTF::StringImpl::create(resolvedSource.source_code.ptr, resolvedSource.source_code.len); + return adoptRef(*new SourceProvider( + resolvedSource, WTFMove(stringImpl_), + JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(toString(resolvedSource.source_url))), + toStringNotConst(resolvedSource.source_url), TextPosition(), + sourceType)); + } + if (allocator) { Ref<WTF::ExternalStringImpl> stringImpl_ = WTF::ExternalStringImpl::create( resolvedSource.source_code.ptr, resolvedSource.source_code.len, allocator, RefString__free); return adoptRef(*new SourceProvider( - resolvedSource, stringImpl_, + resolvedSource, WTFMove(stringImpl_), JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(toString(resolvedSource.source_url))), toStringNotConst(resolvedSource.source_url), TextPosition(), sourceType)); @@ -50,7 +60,7 @@ Ref<SourceProvider> SourceProvider::create(ResolvedSource resolvedSource) Ref<WTF::ExternalStringImpl> stringImpl_ = WTF::ExternalStringImpl::createStatic( resolvedSource.source_code.ptr, resolvedSource.source_code.len); return adoptRef(*new SourceProvider( - resolvedSource, stringImpl_, + resolvedSource, WTFMove(stringImpl_), JSC::SourceOrigin(WTF::URL::fileURLWithFileSystemPath(toString(resolvedSource.source_url))), toStringNotConst(resolvedSource.source_url), TextPosition(), sourceType)); diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig index 5c4f2ae68..0908505a3 100644 --- a/src/bun.js/javascript.zig +++ b/src/bun.js/javascript.zig @@ -103,7 +103,6 @@ pub const GlobalClasses = [_]type{ Fetch.Class, js_ast.Macro.JSNode.BunJSXCallbackFunction, - WebCore.Performance.Class, WebCore.Crypto.Class, WebCore.Crypto.Prototype, @@ -188,7 +187,8 @@ pub const SavedSourceMap = struct { pub const Value = TaggedPointerUnion(.{ MappingList, SavedMappings }); pub const HashTable = std.HashMap(u64, *anyopaque, IdentityContext(u64), 80); - map: HashTable, + /// This is a pointer to the map located on the VirtualMachine struct + map: *HashTable, pub fn onSourceMapChunk(this: *SavedSourceMap, chunk: SourceMap.Chunk, source: logger.Source) anyerror!void { try this.putMappings(source, chunk.buffer); @@ -305,6 +305,13 @@ pub const VirtualMachine = struct { timer: Bun.Timer = Bun.Timer{}, uws_event_loop: ?*uws.Loop = null, + /// Do not access this field directly + /// It exists in the VirtualMachine struct so that + /// we don't accidentally make a stack copy of it + /// only use it through + /// source_mappings + saved_source_map_table: SavedSourceMap.HashTable = undefined, + arena: *Arena = undefined, has_loaded: bool = false, @@ -512,13 +519,15 @@ pub const VirtualMachine = struct { .flush_list = std.ArrayList(string).init(allocator), .blobs = if (_args.serve orelse false) try Blob.Group.init(allocator) else null, .origin = bundler.options.origin, - .source_mappings = SavedSourceMap{ .map = SavedSourceMap.HashTable.init(allocator) }, + .saved_source_map_table = SavedSourceMap.HashTable.init(allocator), + .source_mappings = undefined, .macros = MacroMap.init(allocator), .macro_entry_points = @TypeOf(VirtualMachine.vm.macro_entry_points).init(allocator), .origin_timer = std.time.Timer.start() catch @panic("Please don't mess with timers."), .ref_strings = JSC.RefString.Map.init(allocator), .file_blobs = JSC.WebCore.Blob.Store.Map.init(allocator), }; + VirtualMachine.vm.source_mappings = .{ .map = &VirtualMachine.vm.saved_source_map_table }; VirtualMachine.vm.regular_event_loop.tasks = EventLoop.Queue.init( default_allocator, ); @@ -1065,18 +1074,18 @@ pub const VirtualMachine = struct { jsc_vm.bundler.linker.import_counter = 0; var printer = source_code_printer.?.*; - var written: usize = undefined; + defer source_code_printer.?.* = printer; printer.ctx.reset(); - { - defer source_code_printer.?.* = printer; - written = try jsc_vm.bundler.printWithSourceMap( - parse_result, - @TypeOf(&printer), - &printer, - .esm_ascii, - SavedSourceMap.SourceMapHandler.init(&jsc_vm.source_mappings), - ); - } + + var written: usize = 0; + + written = try jsc_vm.bundler.printWithSourceMap( + parse_result, + @TypeOf(&printer), + &printer, + .esm_ascii, + SavedSourceMap.SourceMapHandler.init(&jsc_vm.source_mappings), + ); if (written == 0) { return error.PrintingErrorWriteFailed; @@ -1088,10 +1097,11 @@ pub const VirtualMachine = struct { return ResolvedSource{ .allocator = null, - .source_code = ZigString.init(jsc_vm.allocator.dupe(u8, printer.ctx.written) catch unreachable), + .source_code = ZigString.init(printer.ctx.writtenWithoutTrailingZero()), .specifier = ZigString.init(specifier), .source_url = ZigString.init(path.text), - .hash = 0, + // TODO: change hash to a bitfield + .hash = 1, }; }, // provideFetch() should be called @@ -1261,7 +1271,8 @@ pub const VirtualMachine = struct { globalObject: *JSGlobalObject, microtask: *Microtask, ) void { - std.debug.assert(VirtualMachine.vm_loaded); + if (comptime Environment.allow_assert) + std.debug.assert(VirtualMachine.vm_loaded); var vm_ = globalObject.bunVM(); if (vm_.global == globalObject) { |