mirror of
https://github.com/oven-sh/bun
synced 2026-02-16 22:01:47 +00:00
Compare commits
1 Commits
claude/fix
...
claude/sou
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e48578c5cd |
@@ -66,6 +66,43 @@ pub fn resolveEmbeddedFile(vm: *VirtualMachine, input_path: []const u8, extname:
|
||||
return bun.path.joinAbs(bun.fs.FileSystem.instance.fs.tmpdirPath(), .auto, tmpfilename);
|
||||
}
|
||||
|
||||
/// Temporary shim to convert ModuleResult back to ResolvedSource
|
||||
/// TODO: Remove this once C++ side is fully converted to ModuleResult
|
||||
fn moduleResultToResolvedSource(result: jsc.ModuleResult) ResolvedSource {
|
||||
return switch (result.tag) {
|
||||
.transpiled => .{
|
||||
.allocator = null,
|
||||
.source_code = result.value.transpiled.source_code,
|
||||
.source_url = result.value.transpiled.source_url,
|
||||
.is_commonjs_module = result.value.transpiled.flags.is_commonjs,
|
||||
.already_bundled = result.value.transpiled.flags.is_already_bundled,
|
||||
.bytecode_cache = result.value.transpiled.bytecode_cache,
|
||||
.bytecode_cache_size = result.value.transpiled.bytecode_cache_len,
|
||||
.tag = if (result.value.transpiled.flags.from_package_json_type_module) .package_json_type_module else .javascript,
|
||||
},
|
||||
.special => .{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.empty,
|
||||
.jsvalue_for_export = result.value.special.jsvalue,
|
||||
.tag = switch (result.value.special.tag) {
|
||||
.exports_object => .exports_object,
|
||||
.export_default_object => .export_default_object,
|
||||
.custom_extension => .javascript, // TODO: handle custom_extension properly
|
||||
},
|
||||
},
|
||||
.builtin => .{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.empty,
|
||||
.tag = @enumFromInt(result.value.builtin_id),
|
||||
},
|
||||
.err => .{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.empty,
|
||||
.tag = .javascript,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
pub const AsyncModule = struct {
|
||||
// This is all the state used by the printer to print the module
|
||||
parse_result: ParseResult,
|
||||
@@ -425,26 +462,15 @@ pub const AsyncModule = struct {
|
||||
defer log.deinit();
|
||||
var errorable: jsc.ErrorableResolvedSource = undefined;
|
||||
this.poll_ref.unref(jsc_vm);
|
||||
outer: {
|
||||
errorable = jsc.ErrorableResolvedSource.ok(this.resumeLoadingModule(&log) catch |err| {
|
||||
switch (err) {
|
||||
error.JSError => {
|
||||
errorable = .err(error.JSError, this.globalThis.takeError(error.JSError));
|
||||
break :outer;
|
||||
},
|
||||
else => {
|
||||
VirtualMachine.processFetchLog(
|
||||
this.globalThis,
|
||||
bun.String.init(this.specifier),
|
||||
bun.String.init(this.referrer),
|
||||
&log,
|
||||
&errorable,
|
||||
err,
|
||||
);
|
||||
break :outer;
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
const module_result = this.resumeLoadingModule(&log);
|
||||
|
||||
// Convert ModuleResult to ErrorableResolvedSource for now
|
||||
if (module_result.tag == .err) {
|
||||
errorable = jsc.ErrorableResolvedSource.err(error.JSError, module_result.value.err.exception);
|
||||
} else {
|
||||
const resolved_source = moduleResultToResolvedSource(module_result);
|
||||
errorable = jsc.ErrorableResolvedSource.ok(resolved_source);
|
||||
}
|
||||
|
||||
var spec = bun.String.init(ZigString.init(this.specifier).withEncoding());
|
||||
@@ -463,7 +489,7 @@ pub const AsyncModule = struct {
|
||||
pub fn fulfill(
|
||||
globalThis: *JSGlobalObject,
|
||||
promise: JSValue,
|
||||
resolved_source: *ResolvedSource,
|
||||
module_result: *jsc.ModuleResult,
|
||||
err: ?anyerror,
|
||||
specifier_: bun.String,
|
||||
referrer_: bun.String,
|
||||
@@ -480,15 +506,10 @@ pub const AsyncModule = struct {
|
||||
scope.deinit();
|
||||
}
|
||||
|
||||
// TODO: Convert to use ModuleResult directly
|
||||
// For now, we still need to convert to ErrorableResolvedSource for C++ compatibility
|
||||
var errorable: jsc.ErrorableResolvedSource = undefined;
|
||||
if (err) |e| {
|
||||
defer {
|
||||
if (resolved_source.source_code_needs_deref) {
|
||||
resolved_source.source_code_needs_deref = false;
|
||||
resolved_source.source_code.deref();
|
||||
}
|
||||
}
|
||||
|
||||
if (e == error.JSError) {
|
||||
errorable = jsc.ErrorableResolvedSource.err(error.JSError, globalThis.takeError(error.JSError));
|
||||
} else {
|
||||
@@ -502,7 +523,10 @@ pub const AsyncModule = struct {
|
||||
);
|
||||
}
|
||||
} else {
|
||||
errorable = jsc.ErrorableResolvedSource.ok(resolved_source.*);
|
||||
// Convert ModuleResult to ResolvedSource for now
|
||||
// This is a temporary shim until C++ side is updated
|
||||
const resolved_source = moduleResultToResolvedSource(module_result.*);
|
||||
errorable = jsc.ErrorableResolvedSource.ok(resolved_source);
|
||||
}
|
||||
log.deinit();
|
||||
|
||||
@@ -704,7 +728,7 @@ pub const AsyncModule = struct {
|
||||
promise.rejectAsHandled(globalThis, error_instance);
|
||||
}
|
||||
|
||||
pub fn resumeLoadingModule(this: *AsyncModule, log: *logger.Log) !ResolvedSource {
|
||||
pub fn resumeLoadingModule(this: *AsyncModule, log: *logger.Log) jsc.ModuleResult {
|
||||
debug("resumeLoadingModule: {s}", .{this.specifier});
|
||||
var parse_result = this.parse_result;
|
||||
const path = this.path;
|
||||
@@ -725,14 +749,21 @@ pub const AsyncModule = struct {
|
||||
|
||||
// We _must_ link because:
|
||||
// - node_modules bundle won't be properly
|
||||
try jsc_vm.transpiler.linker.link(
|
||||
jsc_vm.transpiler.linker.link(
|
||||
path,
|
||||
&parse_result,
|
||||
jsc_vm.origin,
|
||||
.absolute_path,
|
||||
false,
|
||||
true,
|
||||
);
|
||||
) catch |err| {
|
||||
const exception = log.toJS(this.globalThis, jsc_vm.allocator, "Link error") catch JSValue.jsUndefined();
|
||||
_ = err; // autofix
|
||||
return jsc.ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = .{ .exception = exception } },
|
||||
};
|
||||
};
|
||||
this.parse_result = parse_result;
|
||||
|
||||
var printer = VirtualMachine.source_code_printer.?.*;
|
||||
@@ -741,13 +772,20 @@ pub const AsyncModule = struct {
|
||||
{
|
||||
var mapper = jsc_vm.sourceMapHandler(&printer);
|
||||
defer VirtualMachine.source_code_printer.?.* = printer;
|
||||
_ = try jsc_vm.transpiler.printWithSourceMap(
|
||||
_ = jsc_vm.transpiler.printWithSourceMap(
|
||||
parse_result,
|
||||
@TypeOf(&printer),
|
||||
&printer,
|
||||
.esm_ascii,
|
||||
mapper.get(),
|
||||
);
|
||||
) catch |err| {
|
||||
const exception = log.toJS(this.globalThis, jsc_vm.allocator, "Print error") catch JSValue.jsUndefined();
|
||||
_ = err; // autofix
|
||||
return jsc.ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = .{ .exception = exception } },
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
if (comptime Environment.dump_source) {
|
||||
@@ -773,15 +811,19 @@ pub const AsyncModule = struct {
|
||||
|
||||
resolved_source.is_commonjs_module = parse_result.ast.has_commonjs_export_names or parse_result.ast.exports_kind == .cjs;
|
||||
|
||||
// TODO: Convert refCountedResolvedSource to return ModuleResult
|
||||
return resolved_source;
|
||||
}
|
||||
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.cloneLatin1(printer.ctx.getWritten()),
|
||||
.specifier = String.init(specifier),
|
||||
.source_url = String.init(path.text),
|
||||
.is_commonjs_module = parse_result.ast.has_commonjs_export_names or parse_result.ast.exports_kind == .cjs,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.cloneLatin1(printer.ctx.getWritten()),
|
||||
.source_url = String.init(path.text),
|
||||
.flags = .{
|
||||
.is_commonjs = parse_result.ast.has_commonjs_export_names or parse_result.ast.exports_kind == .cjs,
|
||||
},
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -799,7 +841,7 @@ pub const AsyncModule = struct {
|
||||
extern "c" fn Bun__onFulfillAsyncModule(
|
||||
globalObject: *JSGlobalObject,
|
||||
promiseValue: JSValue,
|
||||
res: *jsc.ErrorableResolvedSource,
|
||||
res: *jsc.ModuleResult,
|
||||
specifier: *bun.String,
|
||||
referrer: *bun.String,
|
||||
) void;
|
||||
@@ -831,17 +873,18 @@ pub fn transpileSourceCode(
|
||||
source_code_printer: *js_printer.BufferPrinter,
|
||||
globalObject: ?*JSGlobalObject,
|
||||
comptime flags: FetchFlags,
|
||||
) !ResolvedSource {
|
||||
) jsc.ModuleResult {
|
||||
const disable_transpilying = comptime flags.disableTranspiling();
|
||||
|
||||
if (comptime disable_transpilying) {
|
||||
if (!(loader.isJavaScriptLike() or loader == .toml or loader == .yaml or loader == .text or loader == .json or loader == .jsonc)) {
|
||||
// Don't print "export default <file path>"
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.empty,
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.empty,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
} },
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1024,7 +1067,12 @@ pub fn transpileSourceCode(
|
||||
}
|
||||
|
||||
give_back_arena = false;
|
||||
return error.ParseError;
|
||||
const global = globalObject orelse jsc_vm.global;
|
||||
const exception = log.toJS(global, jsc_vm.allocator, "Parse error") catch JSValue.jsUndefined();
|
||||
return jsc.ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = .{ .exception = exception } },
|
||||
};
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -1070,63 +1118,72 @@ pub fn transpileSourceCode(
|
||||
|
||||
if (jsc_vm.transpiler.log.errors > 0) {
|
||||
give_back_arena = false;
|
||||
return error.ParseError;
|
||||
const global = globalObject orelse jsc_vm.global;
|
||||
const exception = jsc_vm.transpiler.log.toJS(global, jsc_vm.allocator, "Parse error") catch JSValue.jsUndefined();
|
||||
return jsc.ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = .{ .exception = exception } },
|
||||
};
|
||||
}
|
||||
|
||||
if (loader == .json) {
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.cloneUTF8(source.contents),
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.tag = ResolvedSource.Tag.json_for_object_loader,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.cloneUTF8(source.contents),
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
if (comptime disable_transpilying) {
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = switch (comptime flags) {
|
||||
.print_source_and_clone => bun.String.init(jsc_vm.allocator.dupe(u8, source.contents) catch unreachable),
|
||||
.print_source => bun.String.init(source.contents),
|
||||
else => @compileError("unreachable"),
|
||||
},
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = switch (comptime flags) {
|
||||
.print_source_and_clone => bun.String.init(jsc_vm.allocator.dupe(u8, source.contents) catch unreachable),
|
||||
.print_source => bun.String.init(source.contents),
|
||||
else => @compileError("unreachable"),
|
||||
},
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
if (loader == .json or loader == .jsonc or loader == .toml or loader == .yaml) {
|
||||
if (parse_result.empty) {
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.jsvalue_for_export = JSValue.createEmptyObject(jsc_vm.global, 0),
|
||||
.tag = .exports_object,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .special,
|
||||
.value = .{ .special = .{
|
||||
.tag = .exports_object,
|
||||
.jsvalue = JSValue.createEmptyObject(jsc_vm.global, 0),
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.jsvalue_for_export = parse_result.ast.parts.at(0).stmts[0].data.s_expr.value.toJS(allocator, globalObject orelse jsc_vm.global) catch |e| panic("Unexpected JS error: {s}", .{@errorName(e)}),
|
||||
.tag = .exports_object,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .special,
|
||||
.value = .{ .special = .{
|
||||
.tag = .exports_object,
|
||||
.jsvalue = parse_result.ast.parts.at(0).stmts[0].data.s_expr.value.toJS(allocator, globalObject orelse jsc_vm.global) catch |e| panic("Unexpected JS error: {s}", .{@errorName(e)}),
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
if (parse_result.already_bundled != .none) {
|
||||
const bytecode_slice = parse_result.already_bundled.bytecodeSlice();
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.cloneLatin1(source.contents),
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.already_bundled = true,
|
||||
.bytecode_cache = if (bytecode_slice.len > 0) bytecode_slice.ptr else null,
|
||||
.bytecode_cache_size = bytecode_slice.len,
|
||||
.is_commonjs_module = parse_result.already_bundled.isCommonJS(),
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.cloneLatin1(source.contents),
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.bytecode_cache = if (bytecode_slice.len > 0) bytecode_slice.ptr else null,
|
||||
.bytecode_cache_len = bytecode_slice.len,
|
||||
.flags = .{
|
||||
.is_commonjs = parse_result.already_bundled.isCommonJS(),
|
||||
.is_already_bundled = true,
|
||||
},
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1136,13 +1193,15 @@ pub fn transpileSourceCode(
|
||||
break :brk strings.eqlComptime(ext, ".cjs") or strings.eqlComptime(ext, ".cts");
|
||||
};
|
||||
if (was_cjs) {
|
||||
return .{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.static("(function(){})"),
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.is_commonjs_module = true,
|
||||
.tag = .javascript,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.static("(function(){})"),
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.flags = .{
|
||||
.is_commonjs = true,
|
||||
},
|
||||
} },
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1157,37 +1216,42 @@ pub fn transpileSourceCode(
|
||||
dumpSourceString(jsc_vm, specifier, entry.output_code.byteSlice());
|
||||
}
|
||||
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = switch (entry.output_code) {
|
||||
.string => entry.output_code.string,
|
||||
.utf8 => brk: {
|
||||
const result = bun.String.cloneUTF8(entry.output_code.utf8);
|
||||
cache.output_code_allocator.free(entry.output_code.utf8);
|
||||
entry.output_code.utf8 = "";
|
||||
break :brk result;
|
||||
},
|
||||
},
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.is_commonjs_module = entry.metadata.module_type == .cjs,
|
||||
.tag = brk: {
|
||||
if (entry.metadata.module_type == .cjs and source.path.isFile()) {
|
||||
const actual_package_json: *PackageJSON = package_json orelse brk2: {
|
||||
// this should already be cached virtually always so it's fine to do this
|
||||
const dir_info = (jsc_vm.transpiler.resolver.readDirInfo(source.path.name.dir) catch null) orelse
|
||||
break :brk .javascript;
|
||||
const from_package_json_type_module = brk: {
|
||||
if (entry.metadata.module_type == .cjs and source.path.isFile()) {
|
||||
const actual_package_json: *PackageJSON = package_json orelse brk2: {
|
||||
// this should already be cached virtually always so it's fine to do this
|
||||
const dir_info = (jsc_vm.transpiler.resolver.readDirInfo(source.path.name.dir) catch null) orelse
|
||||
break :brk false;
|
||||
|
||||
break :brk2 dir_info.package_json orelse dir_info.enclosing_package_json;
|
||||
} orelse break :brk .javascript;
|
||||
break :brk2 dir_info.package_json orelse dir_info.enclosing_package_json;
|
||||
} orelse break :brk false;
|
||||
|
||||
if (actual_package_json.module_type == .esm) {
|
||||
break :brk ResolvedSource.Tag.package_json_type_module;
|
||||
}
|
||||
if (actual_package_json.module_type == .esm) {
|
||||
break :brk true;
|
||||
}
|
||||
}
|
||||
|
||||
break :brk ResolvedSource.Tag.javascript;
|
||||
},
|
||||
break :brk false;
|
||||
};
|
||||
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = switch (entry.output_code) {
|
||||
.string => entry.output_code.string,
|
||||
.utf8 => brk: {
|
||||
const result = bun.String.cloneUTF8(entry.output_code.utf8);
|
||||
cache.output_code_allocator.free(entry.output_code.utf8);
|
||||
entry.output_code.utf8 = "";
|
||||
break :brk result;
|
||||
},
|
||||
},
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.flags = .{
|
||||
.is_commonjs = entry.metadata.module_type == .cjs,
|
||||
.from_package_json_type_module = from_package_json_type_module,
|
||||
},
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1206,7 +1270,14 @@ pub fn transpileSourceCode(
|
||||
|
||||
if (parse_result.pending_imports.len > 0) {
|
||||
if (promise_ptr == null) {
|
||||
return error.UnexpectedPendingResolution;
|
||||
const global = globalObject orelse jsc_vm.global;
|
||||
const msg_str = std.fmt.allocPrint(jsc_vm.allocator, "Unexpected pending import in \"{}\". To automatically install npm packages with Bun, please use an import statement instead of require() or dynamic import().", .{input_specifier}) catch "Unexpected pending import";
|
||||
defer if (msg_str.ptr != "Unexpected pending import".ptr) jsc_vm.allocator.free(msg_str);
|
||||
const exception = JSValue.createTypeError(global, bun.String.init(msg_str), .{});
|
||||
return jsc.ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = .{ .exception = exception } },
|
||||
};
|
||||
}
|
||||
|
||||
if (source.contents_is_recycled) {
|
||||
@@ -1232,7 +1303,15 @@ pub fn transpileSourceCode(
|
||||
},
|
||||
);
|
||||
give_back_arena = false;
|
||||
return error.AsyncModule;
|
||||
// Special case: async module loading in progress
|
||||
// This signals to the caller that module loading is async
|
||||
// The promise will be fulfilled later via AsyncModule.fulfill()
|
||||
const global = globalObject.?;
|
||||
const exception = JSValue.createTypeError(global, bun.String.static("AsyncModule"), .{});
|
||||
return jsc.ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = .{ .exception = exception } },
|
||||
};
|
||||
}
|
||||
|
||||
if (!jsc_vm.macro_mode)
|
||||
@@ -1267,40 +1346,39 @@ pub fn transpileSourceCode(
|
||||
if (jsc_vm.isWatcherEnabled()) {
|
||||
var resolved_source = jsc_vm.refCountedResolvedSource(printer.ctx.written, input_specifier, path.text, null, false);
|
||||
resolved_source.is_commonjs_module = parse_result.ast.has_commonjs_export_names or parse_result.ast.exports_kind == .cjs;
|
||||
// TODO: Convert refCountedResolvedSource to return ModuleResult
|
||||
return resolved_source;
|
||||
}
|
||||
|
||||
// Pass along package.json type "module" if set.
|
||||
const tag: ResolvedSource.Tag = switch (loader) {
|
||||
.json, .jsonc => .json_for_object_loader,
|
||||
// Determine if from package.json type module
|
||||
const from_package_json_type_module = switch (loader) {
|
||||
.json, .jsonc => false,
|
||||
.js, .jsx, .ts, .tsx => brk: {
|
||||
const module_type_ = if (package_json) |pkg| pkg.module_type else module_type;
|
||||
|
||||
break :brk switch (module_type_) {
|
||||
.esm => .package_json_type_module,
|
||||
.cjs => .package_json_type_commonjs,
|
||||
else => .javascript,
|
||||
};
|
||||
break :brk module_type_ == .esm;
|
||||
},
|
||||
else => .javascript,
|
||||
else => false,
|
||||
};
|
||||
|
||||
return .{
|
||||
.allocator = null,
|
||||
.source_code = brk: {
|
||||
const written = printer.ctx.getWritten();
|
||||
const result = cache.output_code orelse bun.String.cloneLatin1(written);
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = brk: {
|
||||
const written = printer.ctx.getWritten();
|
||||
const result = cache.output_code orelse bun.String.cloneLatin1(written);
|
||||
|
||||
if (written.len > 1024 * 1024 * 2 or jsc_vm.smol) {
|
||||
printer.ctx.buffer.deinit();
|
||||
}
|
||||
if (written.len > 1024 * 1024 * 2 or jsc_vm.smol) {
|
||||
printer.ctx.buffer.deinit();
|
||||
}
|
||||
|
||||
break :brk result;
|
||||
},
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.is_commonjs_module = parse_result.ast.has_commonjs_export_names or parse_result.ast.exports_kind == .cjs,
|
||||
.tag = tag,
|
||||
break :brk result;
|
||||
},
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.flags = .{
|
||||
.is_commonjs = parse_result.ast.has_commonjs_export_names or parse_result.ast.exports_kind == .cjs,
|
||||
.from_package_json_type_module = from_package_json_type_module,
|
||||
},
|
||||
} },
|
||||
};
|
||||
},
|
||||
// provideFetch() should be called
|
||||
@@ -1361,12 +1439,12 @@ pub fn transpileSourceCode(
|
||||
);
|
||||
}
|
||||
}
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.static(@embedFile("../js/wasi-runner.js")),
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.tag = .esm,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.static(@embedFile("../js/wasi-runner.js")),
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1420,48 +1498,58 @@ pub fn transpileSourceCode(
|
||||
;
|
||||
};
|
||||
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.cloneUTF8(sqlite_module_source_code_string),
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.tag = .esm,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.cloneUTF8(sqlite_module_source_code_string),
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
} },
|
||||
};
|
||||
},
|
||||
|
||||
.html => {
|
||||
if (flags.disableTranspiling()) {
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.empty,
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.tag = .esm,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.empty,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
if (globalObject == null) {
|
||||
return error.NotSupported;
|
||||
const exception = JSValue.createTypeError(jsc_vm.global, bun.String.static("HTML bundles require a global object"), .{});
|
||||
return jsc.ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = .{ .exception = exception } },
|
||||
};
|
||||
}
|
||||
|
||||
const html_bundle = try jsc.API.HTMLBundle.init(globalObject.?, path.text);
|
||||
return ResolvedSource{
|
||||
.allocator = &jsc_vm.allocator,
|
||||
.jsvalue_for_export = html_bundle.toJS(globalObject.?),
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.tag = .export_default_object,
|
||||
const html_bundle = jsc.API.HTMLBundle.init(globalObject.?, path.text) catch |err| {
|
||||
const exception = JSValue.createTypeError(globalObject.?, bun.String.init(std.fmt.allocPrint(jsc_vm.allocator, "Failed to load HTML bundle: {}", .{err}) catch "Failed to load HTML bundle"), .{});
|
||||
return jsc.ModuleResult{
|
||||
.tag = .err,
|
||||
.value = .{ .err = .{ .exception = exception } },
|
||||
};
|
||||
};
|
||||
return jsc.ModuleResult{
|
||||
.tag = .special,
|
||||
.value = .{ .special = .{
|
||||
.tag = .export_default_object,
|
||||
.jsvalue = html_bundle.toJS(globalObject.?),
|
||||
} },
|
||||
};
|
||||
},
|
||||
|
||||
else => {
|
||||
if (flags.disableTranspiling()) {
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.empty,
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.tag = .esm,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.empty,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1527,12 +1615,12 @@ pub fn transpileSourceCode(
|
||||
break :brk try bun.String.createUTF8ForJS(globalObject.?, path.text);
|
||||
};
|
||||
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.jsvalue_for_export = value,
|
||||
.specifier = input_specifier,
|
||||
.source_url = input_specifier.createIfDifferent(path.text),
|
||||
.tag = .export_default_object,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .special,
|
||||
.value = .{ .special = .{
|
||||
.tag = .export_default_object,
|
||||
.jsvalue = value,
|
||||
} },
|
||||
};
|
||||
},
|
||||
}
|
||||
@@ -1796,39 +1884,37 @@ pub export fn Bun__transpileFile(
|
||||
defer jsc_vm.module_loader.resetArena(jsc_vm);
|
||||
|
||||
var promise: ?*jsc.JSInternalPromise = null;
|
||||
ret.* = jsc.ErrorableResolvedSource.ok(
|
||||
ModuleLoader.transpileSourceCode(
|
||||
jsc_vm,
|
||||
lr.specifier,
|
||||
referrer_slice.slice(),
|
||||
specifier_ptr.*,
|
||||
lr.path,
|
||||
synchronous_loader,
|
||||
module_type,
|
||||
&log,
|
||||
lr.virtual_source,
|
||||
if (allow_promise) &promise else null,
|
||||
VirtualMachine.source_code_printer.?,
|
||||
globalObject,
|
||||
FetchFlags.transpile,
|
||||
) catch |err| {
|
||||
switch (err) {
|
||||
error.AsyncModule => {
|
||||
bun.assert(promise != null);
|
||||
return promise;
|
||||
},
|
||||
error.PluginError => return null,
|
||||
error.JSError => {
|
||||
ret.* = jsc.ErrorableResolvedSource.err(error.JSError, globalObject.takeError(error.JSError));
|
||||
return null;
|
||||
},
|
||||
else => {
|
||||
VirtualMachine.processFetchLog(globalObject, specifier_ptr.*, referrer.*, &log, ret, err);
|
||||
return null;
|
||||
},
|
||||
}
|
||||
},
|
||||
const module_result = ModuleLoader.transpileSourceCode(
|
||||
jsc_vm,
|
||||
lr.specifier,
|
||||
referrer_slice.slice(),
|
||||
specifier_ptr.*,
|
||||
lr.path,
|
||||
synchronous_loader,
|
||||
module_type,
|
||||
&log,
|
||||
lr.virtual_source,
|
||||
if (allow_promise) &promise else null,
|
||||
VirtualMachine.source_code_printer.?,
|
||||
globalObject,
|
||||
FetchFlags.transpile,
|
||||
);
|
||||
|
||||
// Handle ModuleResult
|
||||
if (module_result.tag == .err) {
|
||||
// Check if it's an AsyncModule error (special case)
|
||||
const err_msg = module_result.value.err.exception.toString(globalObject);
|
||||
defer err_msg.deref();
|
||||
if (err_msg.eqlComptime("AsyncModule")) {
|
||||
bun.assert(promise != null);
|
||||
return promise;
|
||||
}
|
||||
ret.* = jsc.ErrorableResolvedSource.err(error.JSError, module_result.value.err.exception);
|
||||
return null;
|
||||
}
|
||||
|
||||
const resolved_source = moduleResultToResolvedSource(module_result);
|
||||
ret.* = jsc.ErrorableResolvedSource.ok(resolved_source);
|
||||
return promise;
|
||||
}
|
||||
|
||||
@@ -1855,16 +1941,15 @@ export fn Bun__runVirtualModule(globalObject: *JSGlobalObject, specifier_ptr: *c
|
||||
} orelse return .zero;
|
||||
}
|
||||
|
||||
fn getHardcodedModule(jsc_vm: *VirtualMachine, specifier: bun.String, hardcoded: HardcodedModule) ?ResolvedSource {
|
||||
fn getHardcodedModule(jsc_vm: *VirtualMachine, specifier: bun.String, hardcoded: HardcodedModule) ?jsc.ModuleResult {
|
||||
analytics.Features.builtin_modules.insert(hardcoded);
|
||||
return switch (hardcoded) {
|
||||
.@"bun:main" => .{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.cloneUTF8(jsc_vm.entry_point.source.contents),
|
||||
.specifier = specifier,
|
||||
.source_url = specifier,
|
||||
.tag = .esm,
|
||||
.source_code_needs_deref = true,
|
||||
.@"bun:main" => jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.cloneUTF8(jsc_vm.entry_point.source.contents),
|
||||
.source_url = specifier,
|
||||
} },
|
||||
},
|
||||
.@"bun:internal-for-testing" => {
|
||||
if (!Environment.isDebug) {
|
||||
@@ -1873,17 +1958,18 @@ fn getHardcodedModule(jsc_vm: *VirtualMachine, specifier: bun.String, hardcoded:
|
||||
}
|
||||
return jsSyntheticModule(.@"bun:internal-for-testing", specifier);
|
||||
},
|
||||
.@"bun:wrap" => .{
|
||||
.allocator = null,
|
||||
.source_code = String.init(Runtime.Runtime.sourceCode()),
|
||||
.specifier = specifier,
|
||||
.source_url = specifier,
|
||||
.@"bun:wrap" => jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = String.init(Runtime.Runtime.sourceCode()),
|
||||
.source_url = specifier,
|
||||
} },
|
||||
},
|
||||
inline else => |tag| jsSyntheticModule(@field(ResolvedSource.Tag, @tagName(tag)), specifier),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn fetchBuiltinModule(jsc_vm: *VirtualMachine, specifier: bun.String) !?ResolvedSource {
|
||||
pub fn fetchBuiltinModule(jsc_vm: *VirtualMachine, specifier: bun.String) ?jsc.ModuleResult {
|
||||
if (HardcodedModule.map.getWithEql(specifier, bun.String.eqlComptime)) |hardcoded| {
|
||||
return getHardcodedModule(jsc_vm, specifier, hardcoded);
|
||||
}
|
||||
@@ -1892,11 +1978,12 @@ pub fn fetchBuiltinModule(jsc_vm: *VirtualMachine, specifier: bun.String) !?Reso
|
||||
const spec = specifier.toUTF8(bun.default_allocator);
|
||||
defer spec.deinit();
|
||||
if (jsc_vm.macro_entry_points.get(MacroEntryPoint.generateIDFromSpecifier(spec.slice()))) |entry| {
|
||||
return .{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.cloneUTF8(entry.source.contents),
|
||||
.specifier = specifier,
|
||||
.source_url = specifier.dupeRef(),
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.cloneUTF8(entry.source.contents),
|
||||
.source_url = specifier.dupeRef(),
|
||||
} },
|
||||
};
|
||||
}
|
||||
} else if (jsc_vm.standalone_module_graph) |graph| {
|
||||
@@ -1913,24 +2000,27 @@ pub fn fetchBuiltinModule(jsc_vm: *VirtualMachine, specifier: bun.String) !?Reso
|
||||
\\export const __esModule = true;
|
||||
\\export default db;
|
||||
;
|
||||
return .{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.static(code),
|
||||
.specifier = specifier,
|
||||
.source_url = specifier.dupeRef(),
|
||||
.source_code_needs_deref = false,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.static(code),
|
||||
.source_url = specifier.dupeRef(),
|
||||
} },
|
||||
};
|
||||
}
|
||||
|
||||
return .{
|
||||
.allocator = null,
|
||||
.source_code = file.toWTFString(),
|
||||
.specifier = specifier,
|
||||
.source_url = specifier.dupeRef(),
|
||||
.source_code_needs_deref = false,
|
||||
.bytecode_cache = if (file.bytecode.len > 0) file.bytecode.ptr else null,
|
||||
.bytecode_cache_size = file.bytecode.len,
|
||||
.is_commonjs_module = file.module_format == .cjs,
|
||||
return jsc.ModuleResult{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = file.toWTFString(),
|
||||
.source_url = specifier.dupeRef(),
|
||||
.bytecode_cache = if (file.bytecode.len > 0) file.bytecode.ptr else null,
|
||||
.bytecode_cache_len = file.bytecode.len,
|
||||
.flags = .{
|
||||
.is_commonjs = file.module_format == .cjs,
|
||||
.is_already_bundled = file.bytecode.len > 0,
|
||||
},
|
||||
} },
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1977,47 +2067,39 @@ export fn Bun__transpileVirtualModule(
|
||||
defer log.deinit();
|
||||
defer jsc_vm.module_loader.resetArena(jsc_vm);
|
||||
|
||||
ret.* = jsc.ErrorableResolvedSource.ok(
|
||||
ModuleLoader.transpileSourceCode(
|
||||
jsc_vm,
|
||||
specifier_slice.slice(),
|
||||
referrer_slice.slice(),
|
||||
specifier_ptr.*,
|
||||
path,
|
||||
loader,
|
||||
.unknown,
|
||||
&log,
|
||||
&virtual_source,
|
||||
null,
|
||||
VirtualMachine.source_code_printer.?,
|
||||
globalObject,
|
||||
FetchFlags.transpile,
|
||||
) catch |err| {
|
||||
switch (err) {
|
||||
error.PluginError => return true,
|
||||
error.JSError => {
|
||||
ret.* = jsc.ErrorableResolvedSource.err(error.JSError, globalObject.takeError(error.JSError));
|
||||
return true;
|
||||
},
|
||||
else => {
|
||||
VirtualMachine.processFetchLog(globalObject, specifier_ptr.*, referrer_ptr.*, &log, ret, err);
|
||||
return true;
|
||||
},
|
||||
}
|
||||
},
|
||||
const module_result = ModuleLoader.transpileSourceCode(
|
||||
jsc_vm,
|
||||
specifier_slice.slice(),
|
||||
referrer_slice.slice(),
|
||||
specifier_ptr.*,
|
||||
path,
|
||||
loader,
|
||||
.unknown,
|
||||
&log,
|
||||
&virtual_source,
|
||||
null,
|
||||
VirtualMachine.source_code_printer.?,
|
||||
globalObject,
|
||||
FetchFlags.transpile,
|
||||
);
|
||||
|
||||
if (module_result.tag == .err) {
|
||||
ret.* = jsc.ErrorableResolvedSource.err(error.JSError, module_result.value.err.exception);
|
||||
} else {
|
||||
const resolved_source = moduleResultToResolvedSource(module_result);
|
||||
ret.* = jsc.ErrorableResolvedSource.ok(resolved_source);
|
||||
}
|
||||
analytics.Features.virtual_modules += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline fn jsSyntheticModule(name: ResolvedSource.Tag, specifier: String) ResolvedSource {
|
||||
return ResolvedSource{
|
||||
.allocator = null,
|
||||
.source_code = bun.String.empty,
|
||||
.specifier = specifier,
|
||||
.source_url = bun.String.static(@tagName(name)),
|
||||
.tag = name,
|
||||
.source_code_needs_deref = false,
|
||||
inline fn jsSyntheticModule(name: ResolvedSource.Tag, specifier: String) jsc.ModuleResult {
|
||||
// Synthetic modules are builtin modules identified by their tag
|
||||
// Convert the tag name to a builtin ID (for now, use a simple hash)
|
||||
const builtin_id = @intFromEnum(name);
|
||||
return jsc.ModuleResult{
|
||||
.tag = .builtin,
|
||||
.value = .{ .builtin_id = builtin_id },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2179,14 +2261,16 @@ pub const RuntimeTranspilerStore = struct {
|
||||
|
||||
// NOTE: DirInfo should already be cached since module loading happens
|
||||
// after module resolution, so this should be cheap
|
||||
var resolved_source = ResolvedSource{};
|
||||
// Determine module type flags from package.json
|
||||
var module_flags: jsc.TranspiledSource.Flags = .{};
|
||||
if (package_json) |pkg| {
|
||||
switch (pkg.module_type) {
|
||||
.cjs => {
|
||||
resolved_source.tag = .package_json_type_commonjs;
|
||||
resolved_source.is_commonjs_module = true;
|
||||
module_flags.is_commonjs = true;
|
||||
},
|
||||
.esm => {
|
||||
module_flags.from_package_json_type_module = true;
|
||||
},
|
||||
.esm => resolved_source.tag = .package_json_type_module,
|
||||
.unknown => {},
|
||||
}
|
||||
}
|
||||
@@ -2204,7 +2288,18 @@ pub const RuntimeTranspilerStore = struct {
|
||||
.fetcher = TranspilerJob.Fetcher{
|
||||
.file = {},
|
||||
},
|
||||
.resolved_source = resolved_source,
|
||||
.module_result = .{
|
||||
.tag = .transpiled,
|
||||
.value = .{ .transpiled = .{
|
||||
.source_code = bun.String.empty,
|
||||
.source_url = bun.String.empty,
|
||||
.flags = module_flags,
|
||||
} },
|
||||
},
|
||||
.resolved_source = .{
|
||||
.tag = if (module_flags.from_package_json_type_module) .package_json_type_module else if (module_flags.is_commonjs) .package_json_type_commonjs else .javascript,
|
||||
.is_commonjs_module = module_flags.is_commonjs,
|
||||
},
|
||||
};
|
||||
if (comptime Environment.allow_assert)
|
||||
debug("transpile({s}, {s}, async)", .{ path.text, @tagName(job.loader) });
|
||||
@@ -2225,6 +2320,9 @@ pub const RuntimeTranspilerStore = struct {
|
||||
generation_number: u32 = 0,
|
||||
log: logger.Log,
|
||||
parse_error: ?anyerror = null,
|
||||
module_result: jsc.ModuleResult = .{ .tag = .transpiled, .value = .{ .transpiled = .{ .source_code = bun.String.empty, .source_url = bun.String.empty } } },
|
||||
// TODO(Phase 3): Fully convert TranspilerJob to use ModuleResult throughout
|
||||
// For now, we keep a compatibility shim for the worker thread code
|
||||
resolved_source: ResolvedSource = ResolvedSource{},
|
||||
work_task: jsc.WorkPoolTask = .{ .callback = runFromWorkerThread },
|
||||
next: ?*TranspilerJob = null,
|
||||
@@ -3098,6 +3196,9 @@ const jsc = bun.jsc;
|
||||
const JSGlobalObject = bun.jsc.JSGlobalObject;
|
||||
const JSValue = bun.jsc.JSValue;
|
||||
const ResolvedSource = bun.jsc.ResolvedSource;
|
||||
const ModuleResult = bun.jsc.ModuleResult;
|
||||
const TranspiledSource = bun.jsc.TranspiledSource;
|
||||
const SpecialModule = bun.jsc.SpecialModule;
|
||||
const VirtualMachine = bun.jsc.VirtualMachine;
|
||||
const ZigString = bun.jsc.ZigString;
|
||||
const Bun = jsc.API.Bun;
|
||||
|
||||
@@ -1863,7 +1863,7 @@ pub fn drainMicrotasks(this: *VirtualMachine) void {
|
||||
this.eventLoop().drainMicrotasks() catch {}; // TODO: properly propagate exception upwards
|
||||
}
|
||||
|
||||
pub fn processFetchLog(globalThis: *JSGlobalObject, specifier: bun.String, referrer: bun.String, log: *logger.Log, ret: *ErrorableResolvedSource, err: anyerror) void {
|
||||
fn processFetchLog(globalThis: *JSGlobalObject, specifier: bun.String, referrer: bun.String, log: *logger.Log, ret: *ErrorableResolvedSource, err: anyerror) void {
|
||||
switch (log.msgs.items.len) {
|
||||
0 => {
|
||||
const msg: logger.Msg = brk: {
|
||||
|
||||
114
src/bun.js/bindings/BunSourceProvider.cpp
Normal file
114
src/bun.js/bindings/BunSourceProvider.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "BunSourceProvider.h"
|
||||
#include "ZigGlobalObject.h"
|
||||
#include "BunString.h"
|
||||
#include "JavaScriptCore/SourceCode.h"
|
||||
#include "JavaScriptCore/CachedBytecode.h"
|
||||
#include "wtf/URL.h"
|
||||
#include "wtf/text/StringImpl.h"
|
||||
#include "headers-handwritten.h"
|
||||
|
||||
// Verify struct sizes match between C++ and Zig
|
||||
static_assert(sizeof(TranspiledSourceFlags) == sizeof(uint32_t), "TranspiledSourceFlags must be 32 bits");
|
||||
static_assert(alignof(TranspiledSource) == 8, "TranspiledSource must be 8-byte aligned");
|
||||
|
||||
namespace Zig {
|
||||
|
||||
extern "C" void Bun__addSourceProviderSourceMap(void* bun_vm, JSC::SourceProvider* provider, BunString* specifier);
|
||||
|
||||
Ref<BunSourceProvider> BunSourceProvider::create(
|
||||
Zig::GlobalObject* globalObject,
|
||||
Ref<WTF::StringImpl>&& source,
|
||||
const JSC::SourceOrigin& origin,
|
||||
String&& sourceURL,
|
||||
RefPtr<JSC::CachedBytecode>&& bytecode,
|
||||
JSC::SourceProviderSourceType sourceType)
|
||||
{
|
||||
return adoptRef(*new BunSourceProvider(
|
||||
globalObject,
|
||||
WTFMove(source),
|
||||
origin,
|
||||
WTFMove(sourceURL),
|
||||
WTFMove(bytecode),
|
||||
sourceType));
|
||||
}
|
||||
|
||||
BunSourceProvider::BunSourceProvider(
|
||||
Zig::GlobalObject* globalObject,
|
||||
Ref<WTF::StringImpl>&& source,
|
||||
const JSC::SourceOrigin& origin,
|
||||
String&& sourceURL,
|
||||
RefPtr<JSC::CachedBytecode>&& bytecode,
|
||||
JSC::SourceProviderSourceType sourceType)
|
||||
: JSC::SourceProvider(origin, WTF::URL(), sourceType, false)
|
||||
, m_source(WTFMove(source))
|
||||
, m_cachedBytecode(WTFMove(bytecode))
|
||||
, m_globalObject(globalObject)
|
||||
, m_hash(0)
|
||||
{
|
||||
// Set the source URL
|
||||
this->setSourceURLDirectly(sourceURL);
|
||||
|
||||
// Compute hash for the source
|
||||
m_hash = m_source->hash();
|
||||
}
|
||||
|
||||
BunSourceProvider::~BunSourceProvider()
|
||||
{
|
||||
// Sourcemap cleanup happens automatically via the global object's sourcemap registry
|
||||
// No manual cleanup needed here
|
||||
}
|
||||
|
||||
// C bridge function to create a SourceProvider from TranspiledSource
|
||||
extern "C" JSC::SourceProvider* Bun__createSourceProvider(
|
||||
Zig::GlobalObject* globalObject,
|
||||
const TranspiledSource* source)
|
||||
{
|
||||
// Convert source code to WTF::String
|
||||
auto sourceString = source->source_code.toWTFString(BunString::ZeroCopy);
|
||||
auto sourceURL = source->source_url.toWTFString(BunString::ZeroCopy);
|
||||
|
||||
bool isCommonJS = source->flags.is_commonjs;
|
||||
auto sourceType = isCommonJS
|
||||
? JSC::SourceProviderSourceType::Program
|
||||
: JSC::SourceProviderSourceType::Module;
|
||||
|
||||
// Create SourceOrigin from URL
|
||||
auto origin = JSC::SourceOrigin(WTF::URL(sourceURL));
|
||||
|
||||
// Handle bytecode if present
|
||||
RefPtr<JSC::CachedBytecode> bytecode;
|
||||
if (source->bytecode_cache && source->bytecode_cache_len > 0) {
|
||||
bytecode = JSC::CachedBytecode::create(
|
||||
std::span<uint8_t>(source->bytecode_cache, source->bytecode_cache_len),
|
||||
[](const void* ptr) {
|
||||
// Free using mimalloc (Bun's allocator)
|
||||
extern "C" void mi_free(void*);
|
||||
mi_free(const_cast<void*>(ptr));
|
||||
},
|
||||
{}
|
||||
);
|
||||
}
|
||||
|
||||
auto provider = BunSourceProvider::create(
|
||||
globalObject,
|
||||
*sourceString.impl(),
|
||||
origin,
|
||||
WTFMove(sourceURL),
|
||||
WTFMove(bytecode),
|
||||
sourceType
|
||||
);
|
||||
|
||||
// Register sourcemap if this is already bundled code
|
||||
if (source->flags.is_already_bundled) {
|
||||
auto specifier = source->source_url;
|
||||
Bun__addSourceProviderSourceMap(
|
||||
globalObject->bunVM(),
|
||||
provider.ptr(),
|
||||
const_cast<BunString*>(&specifier)
|
||||
);
|
||||
}
|
||||
|
||||
return &provider.leakRef();
|
||||
}
|
||||
|
||||
} // namespace Zig
|
||||
50
src/bun.js/bindings/BunSourceProvider.h
Normal file
50
src/bun.js/bindings/BunSourceProvider.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include "root.h"
|
||||
#include "headers-handwritten.h"
|
||||
#include "ZigGlobalObject.h"
|
||||
#include "JavaScriptCore/SourceProvider.h"
|
||||
#include "JavaScriptCore/SourceOrigin.h"
|
||||
#include "JavaScriptCore/CachedBytecode.h"
|
||||
|
||||
namespace Zig {
|
||||
|
||||
// Forward declarations
|
||||
struct TranspiledSource;
|
||||
|
||||
/// New SourceProvider for transpiled Bun modules
|
||||
/// Simpler than the old ZigSourceProvider - no stored ResolvedSource
|
||||
class BunSourceProvider final : public JSC::SourceProvider {
|
||||
public:
|
||||
static Ref<BunSourceProvider> create(
|
||||
Zig::GlobalObject* globalObject,
|
||||
Ref<WTF::StringImpl>&& source,
|
||||
const JSC::SourceOrigin& origin,
|
||||
String&& sourceURL,
|
||||
RefPtr<JSC::CachedBytecode>&& bytecode,
|
||||
JSC::SourceProviderSourceType sourceType);
|
||||
|
||||
virtual ~BunSourceProvider();
|
||||
|
||||
// Required overrides from SourceProvider
|
||||
StringView source() const final { return m_source.get(); }
|
||||
unsigned hash() const final { return m_hash; }
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode() const final { return m_cachedBytecode; }
|
||||
|
||||
private:
|
||||
BunSourceProvider(
|
||||
Zig::GlobalObject* globalObject,
|
||||
Ref<WTF::StringImpl>&& source,
|
||||
const JSC::SourceOrigin& origin,
|
||||
String&& sourceURL,
|
||||
RefPtr<JSC::CachedBytecode>&& bytecode,
|
||||
JSC::SourceProviderSourceType sourceType);
|
||||
|
||||
// Simplified members (vs old ZigSourceProvider)
|
||||
Ref<WTF::StringImpl> m_source;
|
||||
RefPtr<JSC::CachedBytecode> m_cachedBytecode;
|
||||
Zig::GlobalObject* m_globalObject; // For sourcemap cleanup only
|
||||
unsigned m_hash;
|
||||
};
|
||||
|
||||
} // namespace Zig
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "root.h"
|
||||
#include "ZigSourceProvider.h"
|
||||
#include "BunSourceProvider.h"
|
||||
#include <JavaScriptCore/ControlFlowProfiler.h>
|
||||
|
||||
using namespace JSC;
|
||||
|
||||
@@ -260,9 +260,8 @@ JSC::EncodedJSValue builtinLoader(JSC::JSGlobalObject* globalObject, JSC::CallFr
|
||||
BunString specifierBunString = Bun::toString(specifierWtfString);
|
||||
BunString empty = BunStringEmpty;
|
||||
JSC::VM& vm = globalObject->vm();
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
memset(&res, 0, sizeof res);
|
||||
|
||||
JSValue result = fetchCommonJSModuleNonBuiltin<true>(
|
||||
global->bunVM(),
|
||||
|
||||
@@ -66,7 +66,7 @@
|
||||
|
||||
#include <JavaScriptCore/JSMapInlines.h>
|
||||
#include <JavaScriptCore/GetterSetter.h>
|
||||
#include "ZigSourceProvider.h"
|
||||
#include "BunSourceProvider.h"
|
||||
#include <JavaScriptCore/FunctionPrototype.h>
|
||||
#include "JSCommonJSModule.h"
|
||||
#include <JavaScriptCore/JSModuleNamespaceObject.h>
|
||||
@@ -1311,14 +1311,13 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionRequireNativeModule, (JSGlobalObject * lexica
|
||||
JSValue specifierValue = callframe->argument(0);
|
||||
WTF::String specifier = specifierValue.toWTFString(globalObject);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
memset(&res, 0, sizeof res);
|
||||
BunString specifierStr = Bun::toString(specifier);
|
||||
auto result = fetchBuiltinModuleWithoutResolution(globalObject, &specifierStr, &res);
|
||||
RETURN_IF_EXCEPTION(throwScope, {});
|
||||
if (result) {
|
||||
if (res.success)
|
||||
if (res.tag != ModuleResultTag_err)
|
||||
return JSC::JSValue::encode(result);
|
||||
}
|
||||
throwScope.assertNoExceptionExceptTermination();
|
||||
|
||||
@@ -87,11 +87,11 @@ public:
|
||||
|
||||
static JSC::Structure* createStructure(JSC::JSGlobalObject* globalObject);
|
||||
|
||||
void evaluate(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, ResolvedSource& resolvedSource, bool isBuiltIn);
|
||||
void evaluateWithPotentiallyOverriddenCompile(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, JSValue keyJSString, ResolvedSource& resolvedSource);
|
||||
inline void evaluate(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, ResolvedSource& resolvedSource)
|
||||
void evaluate(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, JSC::SourceProvider* provider, bool isBuiltIn, bool fromPackageJSONTypeModule);
|
||||
void evaluateWithPotentiallyOverriddenCompile(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, JSValue keyJSString, JSC::SourceProvider* provider, bool fromPackageJSONTypeModule);
|
||||
inline void evaluate(Zig::GlobalObject* globalObject, const WTF::String& sourceURL, JSC::SourceProvider* provider, bool fromPackageJSONTypeModule)
|
||||
{
|
||||
return evaluate(globalObject, sourceURL, resolvedSource, false);
|
||||
return evaluate(globalObject, sourceURL, provider, false, fromPackageJSONTypeModule);
|
||||
}
|
||||
|
||||
static JSCommonJSModule* create(JSC::VM& vm, JSC::Structure* structure,
|
||||
@@ -112,7 +112,8 @@ public:
|
||||
static JSCommonJSModule* create(
|
||||
Zig::GlobalObject* globalObject,
|
||||
const WTF::String& key,
|
||||
ResolvedSource resolvedSource);
|
||||
JSC::SourceProvider* provider,
|
||||
bool fromPackageJSONTypeModule);
|
||||
|
||||
static JSObject* createBoundRequireFunction(VM& vm, JSGlobalObject* lexicalGlobalObject, const WTF::String& pathString);
|
||||
|
||||
@@ -166,15 +167,17 @@ JSC::Structure* createCommonJSModuleStructure(
|
||||
std::optional<JSC::SourceCode> createCommonJSModule(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierValue,
|
||||
ResolvedSource& source,
|
||||
bool isBuiltIn);
|
||||
JSC::SourceProvider* provider,
|
||||
bool isBuiltIn,
|
||||
bool fromPackageJSONTypeModule);
|
||||
|
||||
inline std::optional<JSC::SourceCode> createCommonJSModule(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierValue,
|
||||
ResolvedSource& source)
|
||||
JSC::SourceProvider* provider,
|
||||
bool fromPackageJSONTypeModule)
|
||||
{
|
||||
return createCommonJSModule(globalObject, specifierValue, source, false);
|
||||
return createCommonJSModule(globalObject, specifierValue, provider, false, fromPackageJSONTypeModule);
|
||||
}
|
||||
|
||||
class RequireResolveFunctionPrototype final : public JSC::JSNonFinalObject {
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <JavaScriptCore/JSInternalPromise.h>
|
||||
#include <JavaScriptCore/JSInternalFieldObjectImpl.h>
|
||||
|
||||
#include "ZigSourceProvider.h"
|
||||
#include "BunSourceProvider.h"
|
||||
|
||||
#include <JavaScriptCore/JSSourceCode.h>
|
||||
#include <JavaScriptCore/JSString.h>
|
||||
@@ -46,23 +46,6 @@ using namespace JSC;
|
||||
using namespace Zig;
|
||||
using namespace WebCore;
|
||||
|
||||
class ResolvedSourceCodeHolder {
|
||||
public:
|
||||
ResolvedSourceCodeHolder(ErrorableResolvedSource* res_)
|
||||
: res(res_)
|
||||
{
|
||||
}
|
||||
|
||||
~ResolvedSourceCodeHolder()
|
||||
{
|
||||
if (res->success && res->result.value.source_code.tag == BunStringTag::WTFStringImpl && res->result.value.needsDeref) {
|
||||
res->result.value.needsDeref = false;
|
||||
res->result.value.source_code.impl.wtf->deref();
|
||||
}
|
||||
}
|
||||
|
||||
ErrorableResolvedSource* res;
|
||||
};
|
||||
|
||||
extern "C" BunLoaderType Bun__getDefaultLoader(JSC::JSGlobalObject*, BunString* specifier);
|
||||
|
||||
@@ -331,7 +314,7 @@ template<bool allowPromise>
|
||||
static JSValue handleVirtualModuleResult(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSValue virtualModuleResult,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
bool wasModuleMock = false,
|
||||
@@ -341,7 +324,6 @@ static JSValue handleVirtualModuleResult(
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
auto onLoadResult = handleOnLoadResult(globalObject, virtualModuleResult, specifier, wasModuleMock);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
ResolvedSourceCodeHolder sourceCodeHolder(res);
|
||||
|
||||
const auto reject = [&](JSC::JSValue exception) -> JSValue {
|
||||
if constexpr (allowPromise) {
|
||||
@@ -353,7 +335,6 @@ static JSValue handleVirtualModuleResult(
|
||||
};
|
||||
|
||||
const auto resolve = [&](JSValue code) -> JSValue {
|
||||
res->success = true;
|
||||
if constexpr (allowPromise) {
|
||||
scope.release();
|
||||
return resolvedInternalPromise(globalObject, code);
|
||||
@@ -372,8 +353,6 @@ static JSValue handleVirtualModuleResult(
|
||||
}
|
||||
}
|
||||
|
||||
res->success = true;
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
scope.release();
|
||||
return resolvedInternalPromise(globalObject, code);
|
||||
@@ -384,13 +363,13 @@ static JSValue handleVirtualModuleResult(
|
||||
|
||||
switch (onLoadResult.type) {
|
||||
case OnLoadResultTypeCode: {
|
||||
Bun__transpileVirtualModule(globalObject, specifier, referrer, &onLoadResult.value.sourceText.string, onLoadResult.value.sourceText.loader, res);
|
||||
if (!res->success) {
|
||||
RELEASE_AND_RETURN(scope, reject(JSValue::decode(res->result.err.value)));
|
||||
bool success = Bun__transpileVirtualModule(globalObject, specifier, referrer, &onLoadResult.value.sourceText.string, onLoadResult.value.sourceText.loader, res);
|
||||
if (!success || res->tag == ModuleResultTag_err) {
|
||||
RELEASE_AND_RETURN(scope, reject(JSValue::decode(res->value.err.exception)));
|
||||
}
|
||||
|
||||
auto provider = Zig::SourceProvider::create(globalObject, res->result.value);
|
||||
return resolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(provider)));
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &res->value.transpiled);
|
||||
return resolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(adoptRef(*provider))));
|
||||
}
|
||||
case OnLoadResultTypeError: {
|
||||
RELEASE_AND_RETURN(scope, reject(onLoadResult.value.error));
|
||||
@@ -457,17 +436,16 @@ static JSValue handleVirtualModuleResult(
|
||||
extern "C" void Bun__onFulfillAsyncModule(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::EncodedJSValue encodedPromiseValue,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* result,
|
||||
BunString* specifier,
|
||||
BunString* referrer)
|
||||
{
|
||||
ResolvedSourceCodeHolder sourceCodeHolder(res);
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
JSC::JSInternalPromise* promise = jsCast<JSC::JSInternalPromise*>(JSC::JSValue::decode(encodedPromiseValue));
|
||||
|
||||
if (!res->success) {
|
||||
RELEASE_AND_RETURN(scope, promise->reject(globalObject, JSValue::decode(res->result.err.value)));
|
||||
if (result->tag == ModuleResultTag_err) {
|
||||
RELEASE_AND_RETURN(scope, promise->reject(globalObject, JSValue::decode(result->value.err.exception)));
|
||||
}
|
||||
|
||||
auto specifierValue = Bun::toJS(globalObject, *specifier);
|
||||
@@ -492,25 +470,44 @@ extern "C" void Bun__onFulfillAsyncModule(
|
||||
}
|
||||
}
|
||||
|
||||
if (res->result.value.isCommonJSModule) {
|
||||
auto created = Bun::createCommonJSModule(jsCast<Zig::GlobalObject*>(globalObject), specifierValue, res->result.value);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
JSSourceCode* code = JSSourceCode::create(vm, WTFMove(created.value()));
|
||||
promise->resolve(globalObject, code);
|
||||
scope.assertNoExceptionExceptTermination();
|
||||
} else {
|
||||
auto* exception = scope.exception();
|
||||
if (!vm.isTerminationException(exception)) {
|
||||
scope.clearException();
|
||||
promise->reject(globalObject, exception);
|
||||
switch (result->tag) {
|
||||
case ModuleResultTag_transpiled: {
|
||||
auto& transpiled = result->value.transpiled;
|
||||
bool isCommonJS = transpiled.flags.is_commonjs;
|
||||
bool fromPackageJSONTypeModule = transpiled.flags.from_package_json_type_module;
|
||||
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &transpiled);
|
||||
|
||||
if (isCommonJS) {
|
||||
auto created = Bun::createCommonJSModule(jsCast<Zig::GlobalObject*>(globalObject), specifierValue, provider, fromPackageJSONTypeModule);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
JSSourceCode* code = JSSourceCode::create(vm, WTFMove(created.value()));
|
||||
promise->resolve(globalObject, code);
|
||||
scope.assertNoExceptionExceptTermination();
|
||||
} else {
|
||||
auto* exception = scope.exception();
|
||||
if (!vm.isTerminationException(exception)) {
|
||||
scope.clearException();
|
||||
promise->reject(globalObject, exception);
|
||||
scope.assertNoExceptionExceptTermination();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
promise->resolve(globalObject, JSC::JSSourceCode::create(vm, JSC::SourceCode(adoptRef(*provider))));
|
||||
scope.assertNoExceptionExceptTermination();
|
||||
}
|
||||
} else {
|
||||
auto&& provider = Zig::SourceProvider::create(jsDynamicCast<Zig::GlobalObject*>(globalObject), res->result.value);
|
||||
promise->resolve(globalObject, JSC::JSSourceCode::create(vm, JSC::SourceCode(provider)));
|
||||
scope.assertNoExceptionExceptTermination();
|
||||
break;
|
||||
}
|
||||
case ModuleResultTag_special:
|
||||
case ModuleResultTag_builtin:
|
||||
// These shouldn't happen in async module loading
|
||||
ASSERT_NOT_REACHED();
|
||||
break;
|
||||
case ModuleResultTag_err:
|
||||
// Already handled above
|
||||
ASSERT_NOT_REACHED();
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
// the module has since been deleted from the registry.
|
||||
@@ -521,47 +518,28 @@ extern "C" void Bun__onFulfillAsyncModule(
|
||||
JSValue fetchBuiltinModuleWithoutResolution(
|
||||
Zig::GlobalObject* globalObject,
|
||||
BunString* specifier,
|
||||
ErrorableResolvedSource* res)
|
||||
ModuleResult* res)
|
||||
{
|
||||
void* bunVM = globalObject->bunVM();
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
BunString referrer = BunStringEmpty;
|
||||
if (Bun__fetchBuiltinModule(bunVM, globalObject, specifier, &referrer, res)) {
|
||||
if (!res->success) {
|
||||
return {};
|
||||
switch (res->tag) {
|
||||
case ModuleResultTag_special: {
|
||||
// Special modules that are JSValues (bun, process, etc)
|
||||
return JSValue::decode(res->value.special.jsvalue);
|
||||
}
|
||||
|
||||
auto tag = res->result.value.tag;
|
||||
switch (tag) {
|
||||
// require("bun")
|
||||
case SyntheticModuleType::BunObject: {
|
||||
return globalObject->bunObject();
|
||||
}
|
||||
// require("module"), require("node:module")
|
||||
case SyntheticModuleType::NodeModule: {
|
||||
return globalObject->m_nodeModuleConstructor.getInitializedOnMainThread(globalObject);
|
||||
}
|
||||
// require("process"), require("node:process")
|
||||
case SyntheticModuleType::NodeProcess: {
|
||||
return globalObject->processObject();
|
||||
}
|
||||
|
||||
case SyntheticModuleType::ESM: {
|
||||
res->success = false;
|
||||
case ModuleResultTag_builtin: {
|
||||
// Builtin ES modules - return sentinel to indicate ESM
|
||||
RELEASE_AND_RETURN(scope, jsNumber(-1));
|
||||
}
|
||||
|
||||
default: {
|
||||
if (tag & SyntheticModuleType::InternalModuleRegistryFlag) {
|
||||
constexpr auto mask = (SyntheticModuleType::InternalModuleRegistryFlag - 1);
|
||||
auto result = globalObject->internalModuleRegistry()->requireId(globalObject, vm, static_cast<InternalModuleRegistry::Field>(tag & mask));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
return result;
|
||||
} else {
|
||||
res->success = false;
|
||||
RELEASE_AND_RETURN(scope, jsNumber(-1));
|
||||
}
|
||||
case ModuleResultTag_transpiled: {
|
||||
// Shouldn't happen for builtins typically, but return empty
|
||||
return {};
|
||||
}
|
||||
case ModuleResultTag_err: {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -575,39 +553,23 @@ JSValue resolveAndFetchBuiltinModule(
|
||||
void* bunVM = globalObject->bunVM();
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
memset(&res, 0, sizeof res);
|
||||
if (Bun__resolveAndFetchBuiltinModule(bunVM, specifier, &res)) {
|
||||
ASSERT(res.success);
|
||||
|
||||
auto tag = res.result.value.tag;
|
||||
switch (tag) {
|
||||
// require("bun")
|
||||
case SyntheticModuleType::BunObject: {
|
||||
return globalObject->bunObject();
|
||||
switch (res.tag) {
|
||||
case ModuleResultTag_special: {
|
||||
// Special modules that are JSValues (bun, process, etc)
|
||||
return JSValue::decode(res.value.special.jsvalue);
|
||||
}
|
||||
// require("module"), require("node:module")
|
||||
case SyntheticModuleType::NodeModule: {
|
||||
return globalObject->m_nodeModuleConstructor.getInitializedOnMainThread(globalObject);
|
||||
}
|
||||
// require("process"), require("node:process")
|
||||
case SyntheticModuleType::NodeProcess: {
|
||||
return globalObject->processObject();
|
||||
}
|
||||
|
||||
case SyntheticModuleType::ESM: {
|
||||
case ModuleResultTag_builtin: {
|
||||
// Builtin ES modules
|
||||
return {};
|
||||
}
|
||||
|
||||
default: {
|
||||
if (tag & SyntheticModuleType::InternalModuleRegistryFlag) {
|
||||
constexpr auto mask = (SyntheticModuleType::InternalModuleRegistryFlag - 1);
|
||||
auto result = globalObject->internalModuleRegistry()->requireId(globalObject, vm, static_cast<InternalModuleRegistry::Field>(tag & mask));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
return result;
|
||||
}
|
||||
|
||||
case ModuleResultTag_transpiled: {
|
||||
// Shouldn't happen for builtins typically, but return empty
|
||||
return {};
|
||||
}
|
||||
case ModuleResultTag_err: {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
@@ -651,12 +613,10 @@ JSValue fetchCommonJSModule(
|
||||
void* bunVM = globalObject->bunVM();
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
ErrorableResolvedSource resValue;
|
||||
resValue.success = false;
|
||||
memset(&resValue.result, 0, sizeof resValue.result);
|
||||
ModuleResult resValue;
|
||||
memset(&resValue, 0, sizeof resValue);
|
||||
|
||||
ErrorableResolvedSource* res = &resValue;
|
||||
ResolvedSourceCodeHolder sourceCodeHolder(res);
|
||||
ModuleResult* res = &resValue;
|
||||
|
||||
BunString specifier = Bun::toString(specifierWtfString);
|
||||
|
||||
@@ -688,8 +648,8 @@ JSValue fetchCommonJSModule(
|
||||
RELEASE_AND_RETURN(scope, JSValue {});
|
||||
}
|
||||
case JSPromise::Status::Fulfilled: {
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
if (res->tag == ModuleResultTag_err) {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
if (!wasModuleMock) {
|
||||
@@ -706,7 +666,7 @@ JSValue fetchCommonJSModule(
|
||||
auto builtin = fetchBuiltinModuleWithoutResolution(globalObject, &specifier, res);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
if (builtin) {
|
||||
if (!res->success) {
|
||||
if (res->tag == ModuleResultTag_err || res->tag == ModuleResultTag_builtin) {
|
||||
RELEASE_AND_RETURN(scope, builtin);
|
||||
}
|
||||
target->setExportsObject(builtin);
|
||||
@@ -739,8 +699,8 @@ JSValue fetchCommonJSModule(
|
||||
RELEASE_AND_RETURN(scope, JSValue {});
|
||||
}
|
||||
case JSPromise::Status::Fulfilled: {
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
if (res->tag == ModuleResultTag_err) {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
if (!wasModuleMock) {
|
||||
@@ -787,75 +747,80 @@ JSValue fetchCommonJSModuleNonBuiltin(
|
||||
JSC::JSValue specifierValue,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
JSCommonJSModule* target,
|
||||
String specifierWtfString,
|
||||
BunLoaderType forceLoaderType,
|
||||
JSC::ThrowScope& scope)
|
||||
{
|
||||
Bun__transpileFile(bunVM, globalObject, specifier, referrer, typeAttribute, res, false, !isExtension, forceLoaderType);
|
||||
if (res->success && res->result.value.isCommonJSModule) {
|
||||
if constexpr (isExtension) {
|
||||
target->evaluateWithPotentiallyOverriddenCompile(globalObject, specifierWtfString, specifierValue, res->result.value);
|
||||
|
||||
switch (res->tag) {
|
||||
case ModuleResultTag_transpiled: {
|
||||
auto& transpiled = res->value.transpiled;
|
||||
bool isCommonJS = transpiled.flags.is_commonjs;
|
||||
|
||||
if (isCommonJS) {
|
||||
// Create SourceProvider and evaluate as CommonJS
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &transpiled);
|
||||
bool fromPackageJSONTypeModule = transpiled.flags.from_package_json_type_module;
|
||||
|
||||
if constexpr (isExtension) {
|
||||
target->evaluateWithPotentiallyOverriddenCompile(globalObject, specifierWtfString, specifierValue, provider, fromPackageJSONTypeModule);
|
||||
} else {
|
||||
// Use the overload that matches: (globalObject, sourceURL, provider, isBuiltIn, fromPackageJSONTypeModule)
|
||||
target->evaluate(globalObject, specifierWtfString, provider, false, fromPackageJSONTypeModule);
|
||||
}
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
} else {
|
||||
target->evaluate(globalObject, specifierWtfString, res->result.value);
|
||||
// ESM - provide fetch and return sentinel
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &transpiled);
|
||||
globalObject->moduleLoader()->provideFetch(globalObject, specifierValue, JSC::SourceCode(adoptRef(*provider)));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, jsNumber(-1));
|
||||
}
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
}
|
||||
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
case ModuleResultTag_special: {
|
||||
auto& special = res->value.special;
|
||||
JSC::JSValue value = JSC::JSValue::decode(special.jsvalue);
|
||||
|
||||
// The JSONForObjectLoader tag is source code returned from Bun that needs
|
||||
// to go through the JSON parser in JSC.
|
||||
//
|
||||
// We don't use JSON.parse directly in JS because we want the top-level keys of the JSON
|
||||
// object to be accessible as named imports.
|
||||
//
|
||||
// We don't use Bun's JSON parser because JSON.parse is faster and
|
||||
// handles stack overflow better.
|
||||
//
|
||||
// When parsing tsconfig.*.json or jsconfig.*.json, we go through Bun's JSON
|
||||
// parser instead to support comments and trailing commas.
|
||||
if (res->result.value.tag == SyntheticModuleType::JSONForObjectLoader) {
|
||||
WTF::String jsonSource = res->result.value.source_code.toWTFString(BunString::NonNull);
|
||||
JSC::JSValue value = JSC::JSONParseWithException(globalObject, jsonSource);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
|
||||
target->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), value, 0);
|
||||
target->hasEvaluated = true;
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
|
||||
}
|
||||
// TOML and JSONC may go through here
|
||||
else if (res->result.value.tag == SyntheticModuleType::ExportsObject || res->result.value.tag == SyntheticModuleType::ExportDefaultObject) {
|
||||
JSC::JSValue value = JSC::JSValue::decode(res->result.value.jsvalue_for_export);
|
||||
if (!value) {
|
||||
JSC::throwException(globalObject, scope, JSC::createSyntaxError(globalObject, "Failed to parse Object"_s));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
|
||||
if (special.tag == SpecialModuleTag_custom_extension) {
|
||||
if constexpr (isExtension) {
|
||||
ASSERT_NOT_REACHED();
|
||||
JSC::throwException(globalObject, scope, JSC::createSyntaxError(globalObject, "Recursive extension. This is a bug in Bun"_s));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
evaluateCommonJSCustomExtension(globalObject, target, specifierWtfString, specifierValue, value);
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
}
|
||||
|
||||
// ExportsObject and ExportDefaultObject cases
|
||||
target->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), value, 0);
|
||||
target->hasEvaluated = true;
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
} else if (res->result.value.tag == SyntheticModuleType::CommonJSCustomExtension) {
|
||||
if constexpr (isExtension) {
|
||||
ASSERT_NOT_REACHED();
|
||||
JSC::throwException(globalObject, scope, JSC::createSyntaxError(globalObject, "Recursive extension. This is a bug in Bun"_s));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
evaluateCommonJSCustomExtension(globalObject, target, specifierWtfString, specifierValue, JSC::JSValue::decode(res->result.value.cjsCustomExtension));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, target);
|
||||
}
|
||||
|
||||
auto&& provider = Zig::SourceProvider::create(globalObject, res->result.value);
|
||||
globalObject->moduleLoader()->provideFetch(globalObject, specifierValue, JSC::SourceCode(provider));
|
||||
RETURN_IF_EXCEPTION(scope, {});
|
||||
RELEASE_AND_RETURN(scope, jsNumber(-1));
|
||||
case ModuleResultTag_builtin: {
|
||||
// Builtins shouldn't go through this path
|
||||
ASSERT_NOT_REACHED();
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
|
||||
case ModuleResultTag_err: {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
}
|
||||
|
||||
RELEASE_AND_RETURN(scope, {});
|
||||
}
|
||||
|
||||
// Explicit instantiations of fetchCommonJSModuleNonBuiltin
|
||||
@@ -867,7 +832,7 @@ template JSValue fetchCommonJSModuleNonBuiltin<true>(
|
||||
JSC::JSValue specifierValue,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
JSCommonJSModule* target,
|
||||
String specifierWtfString,
|
||||
BunLoaderType forceLoaderType,
|
||||
@@ -880,7 +845,7 @@ template JSValue fetchCommonJSModuleNonBuiltin<false>(
|
||||
JSC::JSValue specifierValue,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
JSCommonJSModule* target,
|
||||
String specifierWtfString,
|
||||
BunLoaderType forceLoaderType,
|
||||
@@ -892,7 +857,7 @@ template<bool allowPromise>
|
||||
static JSValue fetchESMSourceCode(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute)
|
||||
@@ -900,7 +865,6 @@ static JSValue fetchESMSourceCode(
|
||||
void* bunVM = globalObject->bunVM();
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
auto scope = DECLARE_THROW_SCOPE(vm);
|
||||
ResolvedSourceCodeHolder sourceCodeHolder(res);
|
||||
|
||||
const auto reject = [&](JSC::JSValue exception) -> JSValue {
|
||||
if constexpr (allowPromise) {
|
||||
@@ -944,59 +908,63 @@ static JSValue fetchESMSourceCode(
|
||||
}
|
||||
|
||||
if (Bun__fetchBuiltinModule(bunVM, globalObject, specifier, referrer, res)) {
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
if (res->tag == ModuleResultTag_err) {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, reject(exception));
|
||||
}
|
||||
|
||||
// This can happen if it's a `bun build --compile`'d CommonJS file
|
||||
if (res->result.value.isCommonJSModule) {
|
||||
auto created = Bun::createCommonJSModule(globalObject, specifierJS, res->result.value);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(created.value()))));
|
||||
// Handle transpiled builtins (e.g., `bun build --compile`'d CommonJS file)
|
||||
if (res->tag == ModuleResultTag_transpiled) {
|
||||
auto& transpiled = res->value.transpiled;
|
||||
bool isCommonJS = transpiled.flags.is_commonjs;
|
||||
bool fromPackageJSONTypeModule = transpiled.flags.from_package_json_type_module;
|
||||
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &transpiled);
|
||||
|
||||
if (isCommonJS) {
|
||||
auto created = Bun::createCommonJSModule(globalObject, specifierJS, provider, fromPackageJSONTypeModule);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(created.value()))));
|
||||
}
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, rejectedInternalPromise(globalObject, exception));
|
||||
} else {
|
||||
scope.release();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, rejectedInternalPromise(globalObject, exception));
|
||||
} else {
|
||||
scope.release();
|
||||
return {};
|
||||
}
|
||||
// ES Module
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, JSC::SourceCode(adoptRef(*provider)))));
|
||||
}
|
||||
|
||||
auto moduleKey = specifier->toWTFString(BunString::ZeroCopy);
|
||||
|
||||
auto tag = res->result.value.tag;
|
||||
switch (tag) {
|
||||
case SyntheticModuleType::ESM: {
|
||||
auto&& provider = Zig::SourceProvider::create(globalObject, res->result.value, JSC::SourceProviderSourceType::Module, true);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, JSC::SourceCode(provider))));
|
||||
// Handle special modules (bun, process, etc) - these return JSValues
|
||||
if (res->tag == ModuleResultTag_special) {
|
||||
auto moduleKey = specifier->toWTFString(BunString::ZeroCopy);
|
||||
auto source = JSC::SourceCode(JSC::SyntheticSourceProvider::create(
|
||||
generateInternalModuleSourceCode(globalObject, static_cast<InternalModuleRegistry::Field>(0)),
|
||||
JSC::SourceOrigin(URL(makeString("builtins://"_s, moduleKey))),
|
||||
moduleKey));
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(source))));
|
||||
}
|
||||
|
||||
#define CASE(str, name) \
|
||||
case (SyntheticModuleType::name): { \
|
||||
auto source = JSC::SourceCode(JSC::SyntheticSourceProvider::create(generateNativeModule_##name, JSC::SourceOrigin(), WTFMove(moduleKey))); \
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(source)))); \
|
||||
}
|
||||
BUN_FOREACH_ESM_NATIVE_MODULE(CASE)
|
||||
#undef CASE
|
||||
|
||||
// CommonJS modules from src/js/*
|
||||
default: {
|
||||
if (tag & SyntheticModuleType::InternalModuleRegistryFlag) {
|
||||
constexpr auto mask = (SyntheticModuleType::InternalModuleRegistryFlag - 1);
|
||||
auto source = JSC::SourceCode(JSC::SyntheticSourceProvider::create(generateInternalModuleSourceCode(globalObject, static_cast<InternalModuleRegistry::Field>(tag & mask)), JSC::SourceOrigin(URL(makeString("builtins://"_s, moduleKey))), moduleKey));
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(source))));
|
||||
} else {
|
||||
auto&& provider = Zig::SourceProvider::create(globalObject, res->result.value, JSC::SourceProviderSourceType::Module, true);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(provider))));
|
||||
}
|
||||
}
|
||||
// Handle builtin ES modules
|
||||
if (res->tag == ModuleResultTag_builtin) {
|
||||
// For builtin ES modules, Zig returns the module name in result.builtin
|
||||
// We need to generate the appropriate source code
|
||||
auto moduleKey = specifier->toWTFString(BunString::ZeroCopy);
|
||||
// Return synthetic source for builtin ESM
|
||||
auto source = JSC::SourceCode(JSC::SyntheticSourceProvider::create(
|
||||
generateInternalModuleSourceCode(globalObject, static_cast<InternalModuleRegistry::Field>(0)),
|
||||
JSC::SourceOrigin(URL(makeString("builtin://"_s, moduleKey))),
|
||||
moduleKey));
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(source))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1018,100 +986,79 @@ static JSValue fetchESMSourceCode(
|
||||
Bun__transpileFile(bunVM, globalObject, specifier, referrer, typeAttribute, res, false, false, BunLoaderTypeNone);
|
||||
}
|
||||
|
||||
if (res->success && res->result.value.isCommonJSModule) {
|
||||
auto created = Bun::createCommonJSModule(globalObject, specifierJS, res->result.value);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(created.value()))));
|
||||
switch (res->tag) {
|
||||
case ModuleResultTag_transpiled: {
|
||||
auto& transpiled = res->value.transpiled;
|
||||
bool isCommonJS = transpiled.flags.is_commonjs;
|
||||
bool fromPackageJSONTypeModule = transpiled.flags.from_package_json_type_module;
|
||||
|
||||
auto* provider = Bun__createSourceProvider(globalObject, &transpiled);
|
||||
|
||||
if (isCommonJS) {
|
||||
auto created = Bun::createCommonJSModule(globalObject, specifierJS, provider, fromPackageJSONTypeModule);
|
||||
EXCEPTION_ASSERT(created.has_value() == !scope.exception());
|
||||
if (created.has_value()) {
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(vm, WTFMove(created.value()))));
|
||||
}
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, rejectedInternalPromise(globalObject, exception));
|
||||
} else {
|
||||
scope.release();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
if constexpr (allowPromise) {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, rejectedInternalPromise(globalObject, exception));
|
||||
} else {
|
||||
scope.release();
|
||||
return {};
|
||||
}
|
||||
// ES Module
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(adoptRef(*provider)))));
|
||||
}
|
||||
|
||||
if (!res->success) {
|
||||
throwException(scope, res->result.err, globalObject);
|
||||
case ModuleResultTag_special: {
|
||||
auto& special = res->value.special;
|
||||
JSC::JSValue value = JSC::JSValue::decode(special.jsvalue);
|
||||
|
||||
if (!value) {
|
||||
RELEASE_AND_RETURN(scope, reject(JSC::createSyntaxError(globalObject, "Failed to parse Object"_s)));
|
||||
}
|
||||
|
||||
// Use appropriate generator based on special module tag
|
||||
JSC::SyntheticSourceProvider::SyntheticSourceGenerator function;
|
||||
if (special.tag == SpecialModuleTag_export_default_object) {
|
||||
function = generateJSValueExportDefaultObjectSourceCode(globalObject, value);
|
||||
} else {
|
||||
function = generateJSValueModuleSourceCode(globalObject, value);
|
||||
}
|
||||
|
||||
auto source = JSC::SourceCode(
|
||||
JSC::SyntheticSourceProvider::create(WTFMove(function),
|
||||
JSC::SourceOrigin(), specifier->toWTFString(BunString::ZeroCopy)));
|
||||
JSC::ensureStillAliveHere(value);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(globalObject->vm(), WTFMove(source))));
|
||||
}
|
||||
|
||||
case ModuleResultTag_builtin: {
|
||||
// Shouldn't typically happen in this path (handled above)
|
||||
ASSERT_NOT_REACHED();
|
||||
return {};
|
||||
}
|
||||
|
||||
case ModuleResultTag_err: {
|
||||
throwException(globalObject, scope, JSValue::decode(res->value.err.exception));
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, reject(exception));
|
||||
}
|
||||
|
||||
// The JSONForObjectLoader tag is source code returned from Bun that needs
|
||||
// to go through the JSON parser in JSC.
|
||||
//
|
||||
// We don't use JSON.parse directly in JS because we want the top-level keys of the JSON
|
||||
// object to be accessible as named imports.
|
||||
//
|
||||
// We don't use Bun's JSON parser because JSON.parse is faster and
|
||||
// handles stack overflow better.
|
||||
//
|
||||
// When parsing tsconfig.*.json or jsconfig.*.json, we go through Bun's JSON
|
||||
// parser instead to support comments and trailing commas.
|
||||
if (res->result.value.tag == SyntheticModuleType::JSONForObjectLoader) {
|
||||
WTF::String jsonSource = res->result.value.source_code.toWTFString(BunString::NonNull);
|
||||
JSC::JSValue value = JSC::JSONParseWithException(globalObject, jsonSource);
|
||||
if (scope.exception()) [[unlikely]] {
|
||||
auto* exception = scope.exception();
|
||||
scope.clearException();
|
||||
RELEASE_AND_RETURN(scope, reject(exception));
|
||||
}
|
||||
|
||||
// JSON can become strings, null, numbers, booleans so we must handle "export default 123"
|
||||
auto function = generateJSValueModuleSourceCode(
|
||||
globalObject,
|
||||
value);
|
||||
auto source = JSC::SourceCode(
|
||||
JSC::SyntheticSourceProvider::create(WTFMove(function),
|
||||
JSC::SourceOrigin(), specifier->toWTFString(BunString::ZeroCopy)));
|
||||
JSC::ensureStillAliveHere(value);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(globalObject->vm(), WTFMove(source))));
|
||||
}
|
||||
// TOML and JSONC may go through here
|
||||
else if (res->result.value.tag == SyntheticModuleType::ExportsObject) {
|
||||
JSC::JSValue value = JSC::JSValue::decode(res->result.value.jsvalue_for_export);
|
||||
if (!value) {
|
||||
RELEASE_AND_RETURN(scope, reject(JSC::createSyntaxError(globalObject, "Failed to parse Object"_s)));
|
||||
}
|
||||
|
||||
// JSON can become strings, null, numbers, booleans so we must handle "export default 123"
|
||||
auto function = generateJSValueModuleSourceCode(
|
||||
globalObject,
|
||||
value);
|
||||
auto source = JSC::SourceCode(
|
||||
JSC::SyntheticSourceProvider::create(WTFMove(function),
|
||||
JSC::SourceOrigin(), specifier->toWTFString(BunString::ZeroCopy)));
|
||||
JSC::ensureStillAliveHere(value);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(globalObject->vm(), WTFMove(source))));
|
||||
} else if (res->result.value.tag == SyntheticModuleType::ExportDefaultObject) {
|
||||
JSC::JSValue value = JSC::JSValue::decode(res->result.value.jsvalue_for_export);
|
||||
if (!value) {
|
||||
RELEASE_AND_RETURN(scope, reject(JSC::createSyntaxError(globalObject, "Failed to parse Object"_s)));
|
||||
}
|
||||
|
||||
// JSON can become strings, null, numbers, booleans so we must handle "export default 123"
|
||||
auto function = generateJSValueExportDefaultObjectSourceCode(
|
||||
globalObject,
|
||||
value);
|
||||
auto source = JSC::SourceCode(
|
||||
JSC::SyntheticSourceProvider::create(WTFMove(function),
|
||||
JSC::SourceOrigin(), specifier->toWTFString(BunString::ZeroCopy)));
|
||||
JSC::ensureStillAliveHere(value);
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSSourceCode::create(globalObject->vm(), WTFMove(source))));
|
||||
}
|
||||
|
||||
RELEASE_AND_RETURN(scope, rejectOrResolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(Zig::SourceProvider::create(globalObject, res->result.value)))));
|
||||
return {};
|
||||
}
|
||||
|
||||
JSValue fetchESMSourceCodeSync(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute)
|
||||
@@ -1122,7 +1069,7 @@ JSValue fetchESMSourceCodeSync(
|
||||
JSValue fetchESMSourceCodeAsync(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSC::JSString* specifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute)
|
||||
@@ -1136,9 +1083,8 @@ using namespace Bun;
|
||||
BUN_DEFINE_HOST_FUNCTION(jsFunctionOnLoadObjectResultResolve, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
|
||||
{
|
||||
auto& vm = JSC::getVM(globalObject);
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
ModuleResult res;
|
||||
memset(&res, 0, sizeof res);
|
||||
JSC::JSValue objectResult = callFrame->argument(0);
|
||||
PendingVirtualModuleResult* pendingModule = JSC::jsCast<PendingVirtualModuleResult*>(callFrame->argument(1));
|
||||
JSC::JSValue specifierString = pendingModule->internalField(0).get();
|
||||
@@ -1154,7 +1100,7 @@ BUN_DEFINE_HOST_FUNCTION(jsFunctionOnLoadObjectResultResolve, (JSC::JSGlobalObje
|
||||
bool wasModuleMock = pendingModule->wasModuleMock;
|
||||
|
||||
JSC::JSValue result = handleVirtualModuleResult<false>(static_cast<Zig::GlobalObject*>(globalObject), objectResult, &res, &specifier, &referrer, wasModuleMock);
|
||||
if (!scope.exception() && !res.success) [[unlikely]] {
|
||||
if (!scope.exception() && res.tag == ModuleResultTag_err) [[unlikely]] {
|
||||
throwException(globalObject, scope, result);
|
||||
}
|
||||
if (scope.exception()) [[unlikely]] {
|
||||
|
||||
@@ -93,7 +93,7 @@ public:
|
||||
JSValue fetchESMSourceCodeSync(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSString* spceifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute);
|
||||
@@ -101,7 +101,7 @@ JSValue fetchESMSourceCodeSync(
|
||||
JSValue fetchESMSourceCodeAsync(
|
||||
Zig::GlobalObject* globalObject,
|
||||
JSString* spceifierJS,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute);
|
||||
@@ -123,7 +123,7 @@ JSValue fetchCommonJSModuleNonBuiltin(
|
||||
JSC::JSValue specifierValue,
|
||||
BunString* referrer,
|
||||
BunString* typeAttribute,
|
||||
ErrorableResolvedSource* res,
|
||||
ModuleResult* res,
|
||||
JSCommonJSModule* target,
|
||||
String specifierWtfString,
|
||||
BunLoaderType forceLoaderType,
|
||||
@@ -136,6 +136,6 @@ JSValue resolveAndFetchBuiltinModule(
|
||||
JSValue fetchBuiltinModuleWithoutResolution(
|
||||
Zig::GlobalObject* globalObject,
|
||||
BunString* specifier,
|
||||
ErrorableResolvedSource* res);
|
||||
ModuleResult* res);
|
||||
|
||||
} // namespace Bun
|
||||
|
||||
29
src/bun.js/bindings/ModuleResult.zig
Normal file
29
src/bun.js/bindings/ModuleResult.zig
Normal file
@@ -0,0 +1,29 @@
|
||||
/// Tagged union return type from transpiler
|
||||
const bun = @import("bun");
|
||||
const JSValue = bun.jsc.JSValue;
|
||||
const TranspiledSource = @import("./TranspiledSource.zig").TranspiledSource;
|
||||
const SpecialModule = @import("./SpecialModule.zig").SpecialModule;
|
||||
|
||||
/// Tagged union return type from transpiler
|
||||
pub const ModuleResult = extern struct {
|
||||
tag: Tag,
|
||||
value: extern union {
|
||||
transpiled: TranspiledSource,
|
||||
special: SpecialModule,
|
||||
builtin_id: u32,
|
||||
err: ErrorResult,
|
||||
},
|
||||
|
||||
pub const Tag = enum(u8) {
|
||||
transpiled,
|
||||
special,
|
||||
builtin,
|
||||
err,
|
||||
};
|
||||
};
|
||||
|
||||
/// Error information from transpilation/module loading
|
||||
pub const ErrorResult = extern struct {
|
||||
/// Error value (JSValue) - already an exception
|
||||
exception: JSValue,
|
||||
};
|
||||
@@ -1,36 +0,0 @@
|
||||
pub const ResolvedSource = extern struct {
|
||||
/// Specifier's lifetime is the caller from C++
|
||||
/// https://github.com/oven-sh/bun/issues/9521
|
||||
specifier: bun.String = bun.String.empty,
|
||||
source_code: bun.String = bun.String.empty,
|
||||
|
||||
/// source_url is eventually deref'd on success
|
||||
source_url: bun.String = bun.String.empty,
|
||||
|
||||
is_commonjs_module: bool = false,
|
||||
|
||||
/// When .tag is .common_js_custom_extension, this is special-cased to hold
|
||||
/// the JSFunction extension. It is kept alive by
|
||||
/// - This structure is stored on the stack
|
||||
/// - There is a JSC::Strong reference to it
|
||||
cjs_custom_extension_index: JSValue = .zero,
|
||||
|
||||
allocator: ?*anyopaque = null,
|
||||
|
||||
jsvalue_for_export: JSValue = .zero,
|
||||
|
||||
tag: Tag = .javascript,
|
||||
|
||||
/// This is for source_code
|
||||
source_code_needs_deref: bool = true,
|
||||
already_bundled: bool = false,
|
||||
bytecode_cache: ?[*]u8 = null,
|
||||
bytecode_cache_size: usize = 0,
|
||||
|
||||
pub const Tag = @import("ResolvedSourceTag").ResolvedSourceTag;
|
||||
};
|
||||
|
||||
const bun = @import("bun");
|
||||
|
||||
const jsc = bun.jsc;
|
||||
const JSValue = jsc.JSValue;
|
||||
20
src/bun.js/bindings/SpecialModule.zig
Normal file
20
src/bun.js/bindings/SpecialModule.zig
Normal file
@@ -0,0 +1,20 @@
|
||||
/// For special cases that need JSValue handling
|
||||
/// Main thread only (contains JSValue)
|
||||
const bun = @import("bun");
|
||||
const JSValue = bun.jsc.JSValue;
|
||||
|
||||
/// For special cases that need JSValue handling
|
||||
/// Main thread only
|
||||
pub const SpecialModule = extern struct {
|
||||
tag: Tag,
|
||||
jsvalue: JSValue,
|
||||
|
||||
pub const Tag = enum(u8) {
|
||||
/// Return exports object directly
|
||||
exports_object,
|
||||
/// Return default export only
|
||||
export_default_object,
|
||||
/// Call custom require.extensions handler
|
||||
custom_extension,
|
||||
};
|
||||
};
|
||||
29
src/bun.js/bindings/TranspiledSource.zig
Normal file
29
src/bun.js/bindings/TranspiledSource.zig
Normal file
@@ -0,0 +1,29 @@
|
||||
/// Minimal POD struct for transpiled source code
|
||||
/// Can be safely created on worker threads (no JSValue fields)
|
||||
/// Thread-safe transpilation result
|
||||
const bun = @import("bun");
|
||||
|
||||
/// Minimal POD struct for transpiled source code
|
||||
/// Can be safely created on worker threads
|
||||
pub const TranspiledSource = extern struct {
|
||||
/// Transpiled source code (Latin1 or UTF16)
|
||||
/// Ownership transfers to C++ on return
|
||||
source_code: bun.String,
|
||||
|
||||
/// Module specifier for debugging/sourcemaps
|
||||
source_url: bun.String,
|
||||
|
||||
/// Optional bytecode cache (for bun build --compile)
|
||||
bytecode_cache: ?[*]u8 = null,
|
||||
bytecode_cache_len: usize = 0,
|
||||
|
||||
/// Packed flags
|
||||
flags: Flags = .{},
|
||||
|
||||
pub const Flags = packed struct(u32) {
|
||||
is_commonjs: bool = false,
|
||||
is_already_bundled: bool = false,
|
||||
from_package_json_type_module: bool = false,
|
||||
_padding: u29 = 0,
|
||||
};
|
||||
};
|
||||
@@ -153,7 +153,7 @@
|
||||
#include "webcrypto/JSCryptoKey.h"
|
||||
#include "webcrypto/JSSubtleCrypto.h"
|
||||
#include "ZigGeneratedClasses.h"
|
||||
#include "ZigSourceProvider.h"
|
||||
#include "BunSourceProvider.h"
|
||||
#include "UtilInspect.h"
|
||||
#include "Base64Helpers.h"
|
||||
#include "wtf/text/OrdinalNumber.h"
|
||||
@@ -577,11 +577,10 @@ JSC_DEFINE_HOST_FUNCTION(functionFulfillModuleSync,
|
||||
}
|
||||
|
||||
auto specifier = Bun::toString(moduleKey);
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
ModuleResult res;
|
||||
// zero-initialize entire result union. zeroed BunString has BunStringTag::Dead, and zeroed
|
||||
// EncodedJSValues are empty, which our code should be handling
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
memset(&res, 0, sizeof res);
|
||||
|
||||
JSValue result = Bun::fetchESMSourceCodeSync(
|
||||
globalObject,
|
||||
@@ -3309,11 +3308,10 @@ JSC::JSInternalPromise* GlobalObject::moduleLoaderFetch(JSGlobalObject* globalOb
|
||||
|
||||
auto source = Bun::toString(sourceString);
|
||||
auto typeAttribute = Bun::toString(typeAttributeString);
|
||||
ErrorableResolvedSource res;
|
||||
res.success = false;
|
||||
ModuleResult res;
|
||||
// zero-initialize entire result union. zeroed BunString has BunStringTag::Dead, and zeroed
|
||||
// EncodedJSValues are empty, which our code should be handling
|
||||
memset(&res.result, 0, sizeof res.result);
|
||||
memset(&res, 0, sizeof res);
|
||||
|
||||
JSValue result = Bun::fetchESMSourceCodeAsync(
|
||||
static_cast<Zig::GlobalObject*>(globalObject),
|
||||
|
||||
@@ -1,368 +0,0 @@
|
||||
#include "root.h"
|
||||
|
||||
#include "helpers.h"
|
||||
|
||||
#include "ZigSourceProvider.h"
|
||||
|
||||
#include <JavaScriptCore/BytecodeCacheError.h>
|
||||
#include "ZigGlobalObject.h"
|
||||
#include "wtf/Assertions.h"
|
||||
|
||||
#include <JavaScriptCore/Completion.h>
|
||||
#include <wtf/Scope.h>
|
||||
#include <wtf/text/StringHash.h>
|
||||
#include <sys/stat.h>
|
||||
#include <JavaScriptCore/SourceCodeKey.h>
|
||||
#include <mimalloc.h>
|
||||
#include <JavaScriptCore/CodeCache.h>
|
||||
|
||||
namespace Zig {
|
||||
|
||||
using Base = JSC::SourceProvider;
|
||||
using BytecodeCacheGenerator = JSC::BytecodeCacheGenerator;
|
||||
using UnlinkedFunctionExecutable = JSC::UnlinkedFunctionExecutable;
|
||||
using CachedBytecode = JSC::CachedBytecode;
|
||||
using UnlinkedFunctionCodeBlock = JSC::UnlinkedFunctionCodeBlock;
|
||||
using SourceCode = JSC::SourceCode;
|
||||
using CodeSpecializationKind = JSC::CodeSpecializationKind;
|
||||
using SourceOrigin = JSC::SourceOrigin;
|
||||
using String = WTF::String;
|
||||
using SourceProviderSourceType = JSC::SourceProviderSourceType;
|
||||
|
||||
SourceOrigin toSourceOrigin(const String& sourceURL, bool isBuiltin)
|
||||
{
|
||||
|
||||
ASSERT_WITH_MESSAGE(!sourceURL.startsWith("file://"_s), "specifier should not already be a file URL");
|
||||
|
||||
if (isBuiltin) {
|
||||
if (sourceURL.startsWith("node:"_s)) {
|
||||
return SourceOrigin(WTF::URL(makeString("builtin://node/"_s, sourceURL.substring(5))));
|
||||
} else if (sourceURL.startsWith("bun:"_s)) {
|
||||
return SourceOrigin(WTF::URL(makeString("builtin://bun/"_s, sourceURL.substring(4))));
|
||||
} else {
|
||||
return SourceOrigin(WTF::URL(makeString("builtin://"_s, sourceURL)));
|
||||
}
|
||||
}
|
||||
return SourceOrigin(WTF::URL::fileURLWithFileSystemPath(sourceURL));
|
||||
}
|
||||
|
||||
extern "C" int ByteRangeMapping__getSourceID(void* mappings, BunString sourceURL);
|
||||
extern "C" void* ByteRangeMapping__find(BunString sourceURL);
|
||||
void* sourceMappingForSourceURL(const WTF::String& sourceURL)
|
||||
{
|
||||
return ByteRangeMapping__find(Bun::toString(sourceURL));
|
||||
}
|
||||
|
||||
extern "C" void ByteRangeMapping__generate(BunString sourceURL, BunString code, int sourceID);
|
||||
|
||||
JSC::SourceID sourceIDForSourceURL(const WTF::String& sourceURL)
|
||||
{
|
||||
void* mappings = ByteRangeMapping__find(Bun::toString(sourceURL));
|
||||
if (!mappings) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ByteRangeMapping__getSourceID(mappings, Bun::toString(sourceURL));
|
||||
}
|
||||
|
||||
extern "C" bool BunTest__shouldGenerateCodeCoverage(BunString sourceURL);
|
||||
extern "C" void Bun__addSourceProviderSourceMap(void* bun_vm, SourceProvider* opaque_source_provider, BunString* specifier);
|
||||
extern "C" void Bun__removeSourceProviderSourceMap(void* bun_vm, SourceProvider* opaque_source_provider, BunString* specifier);
|
||||
|
||||
Ref<SourceProvider> SourceProvider::create(
|
||||
Zig::GlobalObject* globalObject,
|
||||
ResolvedSource& resolvedSource,
|
||||
JSC::SourceProviderSourceType sourceType,
|
||||
bool isBuiltin)
|
||||
{
|
||||
auto string = resolvedSource.source_code.toWTFString(BunString::ZeroCopy);
|
||||
auto sourceURLString = resolvedSource.source_url.toWTFString(BunString::ZeroCopy);
|
||||
|
||||
bool isCodeCoverageEnabled = !!globalObject->vm().controlFlowProfiler();
|
||||
|
||||
bool shouldGenerateCodeCoverage = isCodeCoverageEnabled && !isBuiltin && BunTest__shouldGenerateCodeCoverage(resolvedSource.source_url);
|
||||
|
||||
if (resolvedSource.needsDeref && !isBuiltin) {
|
||||
resolvedSource.needsDeref = false;
|
||||
resolvedSource.source_code.deref();
|
||||
// Do not deref either source_url or specifier
|
||||
// Specifier's lifetime is the JSValue, mostly
|
||||
// source_url is owned by the string above
|
||||
// https://github.com/oven-sh/bun/issues/9521
|
||||
}
|
||||
|
||||
const auto getProvider = [&]() -> Ref<SourceProvider> {
|
||||
if (resolvedSource.bytecode_cache != nullptr) {
|
||||
const auto destructorPtr = [](const void* ptr) {
|
||||
mi_free(const_cast<void*>(ptr));
|
||||
};
|
||||
const auto destructorNoOp = [](const void* ptr) {
|
||||
// no-op, for bun build --compile.
|
||||
};
|
||||
const auto destructor = resolvedSource.needsDeref ? destructorPtr : destructorNoOp;
|
||||
|
||||
Ref<JSC::CachedBytecode> bytecode = JSC::CachedBytecode::create(std::span<uint8_t>(resolvedSource.bytecode_cache, resolvedSource.bytecode_cache_size), destructor, {});
|
||||
auto provider = adoptRef(*new SourceProvider(
|
||||
globalObject->isThreadLocalDefaultGlobalObject ? globalObject : nullptr,
|
||||
resolvedSource,
|
||||
string.isNull() ? *StringImpl::empty() : *string.impl(),
|
||||
JSC::SourceTaintedOrigin::Untainted,
|
||||
toSourceOrigin(sourceURLString, isBuiltin),
|
||||
sourceURLString.impl(), TextPosition(),
|
||||
sourceType));
|
||||
provider->m_cachedBytecode = WTFMove(bytecode);
|
||||
return provider;
|
||||
}
|
||||
|
||||
return adoptRef(*new SourceProvider(
|
||||
globalObject->isThreadLocalDefaultGlobalObject ? globalObject : nullptr,
|
||||
resolvedSource,
|
||||
string.isNull() ? *StringImpl::empty() : *string.impl(),
|
||||
JSC::SourceTaintedOrigin::Untainted,
|
||||
toSourceOrigin(sourceURLString, isBuiltin),
|
||||
sourceURLString.impl(), TextPosition(),
|
||||
sourceType));
|
||||
};
|
||||
|
||||
auto provider = getProvider();
|
||||
|
||||
if (shouldGenerateCodeCoverage) {
|
||||
ByteRangeMapping__generate(Bun::toString(provider->sourceURL()), Bun::toString(provider->source().toStringWithoutCopying()), provider->asID());
|
||||
}
|
||||
|
||||
if (resolvedSource.already_bundled) {
|
||||
Bun__addSourceProviderSourceMap(globalObject->bunVM(), provider.ptr(), &resolvedSource.source_url);
|
||||
}
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
StringView SourceProvider::source() const
|
||||
{
|
||||
return StringView(m_source.get());
|
||||
}
|
||||
|
||||
SourceProvider::~SourceProvider()
|
||||
{
|
||||
if (m_resolvedSource.already_bundled) {
|
||||
BunString str = Bun::toString(sourceURL());
|
||||
Bun__removeSourceProviderSourceMap(m_globalObject->bunVM(), this, &str);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" void CachedBytecode__deref(JSC::CachedBytecode* cachedBytecode)
|
||||
{
|
||||
cachedBytecode->deref();
|
||||
}
|
||||
|
||||
static JSC::VM& getVMForBytecodeCache()
|
||||
{
|
||||
static thread_local JSC::VM* vmForBytecodeCache = nullptr;
|
||||
if (!vmForBytecodeCache) {
|
||||
const auto heapSize = JSC::HeapType::Small;
|
||||
auto vmPtr = JSC::VM::tryCreate(heapSize);
|
||||
vmPtr->refSuppressingSaferCPPChecking();
|
||||
vmForBytecodeCache = vmPtr.get();
|
||||
vmPtr->heap.acquireAccess();
|
||||
}
|
||||
return *vmForBytecodeCache;
|
||||
}
|
||||
|
||||
extern "C" bool generateCachedModuleByteCodeFromSourceCode(BunString* sourceProviderURL, const Latin1Character* inputSourceCode, size_t inputSourceCodeSize, const uint8_t** outputByteCode, size_t* outputByteCodeSize, JSC::CachedBytecode** cachedBytecodePtr)
|
||||
{
|
||||
std::span<const Latin1Character> sourceCodeSpan(inputSourceCode, inputSourceCodeSize);
|
||||
JSC::SourceCode sourceCode = JSC::makeSource(WTF::String(sourceCodeSpan), toSourceOrigin(sourceProviderURL->toWTFString(), false), JSC::SourceTaintedOrigin::Untainted);
|
||||
|
||||
JSC::VM& vm = getVMForBytecodeCache();
|
||||
|
||||
JSC::JSLockHolder locker(vm);
|
||||
LexicallyScopedFeatures lexicallyScopedFeatures = StrictModeLexicallyScopedFeature;
|
||||
JSParserScriptMode scriptMode = JSParserScriptMode::Module;
|
||||
EvalContextType evalContextType = EvalContextType::None;
|
||||
|
||||
ParserError parserError;
|
||||
UnlinkedModuleProgramCodeBlock* unlinkedCodeBlock = JSC::recursivelyGenerateUnlinkedCodeBlockForModuleProgram(vm, sourceCode, lexicallyScopedFeatures, scriptMode, {}, parserError, evalContextType);
|
||||
if (parserError.isValid())
|
||||
return false;
|
||||
if (!unlinkedCodeBlock)
|
||||
return false;
|
||||
|
||||
auto key = JSC::sourceCodeKeyForSerializedModule(vm, sourceCode);
|
||||
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode = JSC::encodeCodeBlock(vm, key, unlinkedCodeBlock);
|
||||
if (!cachedBytecode)
|
||||
return false;
|
||||
|
||||
cachedBytecode->ref();
|
||||
*cachedBytecodePtr = cachedBytecode.get();
|
||||
*outputByteCode = cachedBytecode->span().data();
|
||||
*outputByteCodeSize = cachedBytecode->span().size();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C" bool generateCachedCommonJSProgramByteCodeFromSourceCode(BunString* sourceProviderURL, const Latin1Character* inputSourceCode, size_t inputSourceCodeSize, const uint8_t** outputByteCode, size_t* outputByteCodeSize, JSC::CachedBytecode** cachedBytecodePtr)
|
||||
{
|
||||
std::span<const Latin1Character> sourceCodeSpan(inputSourceCode, inputSourceCodeSize);
|
||||
|
||||
JSC::SourceCode sourceCode = JSC::makeSource(WTF::String(sourceCodeSpan), toSourceOrigin(sourceProviderURL->toWTFString(), false), JSC::SourceTaintedOrigin::Untainted);
|
||||
JSC::VM& vm = getVMForBytecodeCache();
|
||||
|
||||
JSC::JSLockHolder locker(vm);
|
||||
LexicallyScopedFeatures lexicallyScopedFeatures = NoLexicallyScopedFeatures;
|
||||
JSParserScriptMode scriptMode = JSParserScriptMode::Classic;
|
||||
EvalContextType evalContextType = EvalContextType::None;
|
||||
|
||||
ParserError parserError;
|
||||
UnlinkedProgramCodeBlock* unlinkedCodeBlock = JSC::recursivelyGenerateUnlinkedCodeBlockForProgram(vm, sourceCode, lexicallyScopedFeatures, scriptMode, {}, parserError, evalContextType);
|
||||
if (parserError.isValid())
|
||||
return false;
|
||||
if (!unlinkedCodeBlock)
|
||||
return false;
|
||||
|
||||
auto key = JSC::sourceCodeKeyForSerializedProgram(vm, sourceCode);
|
||||
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode = JSC::encodeCodeBlock(vm, key, unlinkedCodeBlock);
|
||||
if (!cachedBytecode)
|
||||
return false;
|
||||
|
||||
cachedBytecode->ref();
|
||||
*cachedBytecodePtr = cachedBytecode.get();
|
||||
*outputByteCode = cachedBytecode->span().data();
|
||||
*outputByteCodeSize = cachedBytecode->span().size();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
unsigned SourceProvider::hash() const
|
||||
{
|
||||
if (m_hash) {
|
||||
return m_hash;
|
||||
}
|
||||
|
||||
return m_source->hash();
|
||||
}
|
||||
|
||||
void SourceProvider::freeSourceCode()
|
||||
{
|
||||
}
|
||||
|
||||
void SourceProvider::updateCache(const UnlinkedFunctionExecutable* executable, const SourceCode&,
|
||||
CodeSpecializationKind kind,
|
||||
const UnlinkedFunctionCodeBlock* codeBlock)
|
||||
{
|
||||
// if (!m_resolvedSource.bytecodecache_fd || !m_cachedBytecode)
|
||||
return;
|
||||
|
||||
JSC::BytecodeCacheError error;
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode = JSC::encodeFunctionCodeBlock(executable->vm(), codeBlock, error);
|
||||
if (cachedBytecode && !error.isValid())
|
||||
m_cachedBytecode->addFunctionUpdate(executable, kind, *cachedBytecode);
|
||||
}
|
||||
|
||||
void SourceProvider::cacheBytecode(const BytecodeCacheGenerator& generator)
|
||||
{
|
||||
// if (!m_resolvedSource.bytecodecache_fd)
|
||||
return;
|
||||
|
||||
if (!m_cachedBytecode)
|
||||
m_cachedBytecode = JSC::CachedBytecode::create();
|
||||
auto update = generator();
|
||||
if (update)
|
||||
m_cachedBytecode->addGlobalUpdate(*update);
|
||||
}
|
||||
|
||||
void SourceProvider::commitCachedBytecode()
|
||||
{
|
||||
// if (!m_resolvedSource.bytecodecache_fd || !m_cachedBytecode || !m_cachedBytecode->hasUpdates())
|
||||
return;
|
||||
|
||||
// auto clearBytecode = WTF::makeScopeExit([&] { m_cachedBytecode = nullptr; });
|
||||
// const auto fd = m_resolvedSource.bytecodecache_fd;
|
||||
|
||||
// auto fileSize = FileSystem::fileSize(fd);
|
||||
// if (!fileSize)
|
||||
// return;
|
||||
|
||||
// size_t cacheFileSize;
|
||||
// if (!WTF::convertSafely(*fileSize, cacheFileSize) || cacheFileSize != m_cachedBytecode->size()) {
|
||||
// // The bytecode cache has already been updated
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (!FileSystem::truncateFile(fd, m_cachedBytecode->sizeForUpdate()))
|
||||
// return;
|
||||
|
||||
// m_cachedBytecode->commitUpdates([&](off_t offset, const void* data, size_t size) {
|
||||
// long long result = FileSystem::seekFile(fd, offset, FileSystem::FileSeekOrigin::Beginning);
|
||||
// ASSERT_UNUSED(result, result != -1);
|
||||
// size_t bytesWritten = static_cast<size_t>(FileSystem::writeToFile(fd, data, size));
|
||||
// ASSERT_UNUSED(bytesWritten, bytesWritten == size);
|
||||
// });
|
||||
}
|
||||
|
||||
bool SourceProvider::isBytecodeCacheEnabled() const
|
||||
{
|
||||
// return m_resolvedSource.bytecodecache_fd > 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
void SourceProvider::readOrGenerateByteCodeCache(JSC::VM& vm, const JSC::SourceCode& sourceCode)
|
||||
{
|
||||
// auto status = this->readCache(vm, sourceCode);
|
||||
// switch (status) {
|
||||
// case -1: {
|
||||
// m_resolvedSource.bytecodecache_fd = 0;
|
||||
// break;
|
||||
// }
|
||||
// case 0: {
|
||||
// JSC::BytecodeCacheError err;
|
||||
// m_cachedBytecode = JSC::generateModuleBytecode(vm, sourceCode, m_resolvedSource.bytecodecache_fd, err);
|
||||
|
||||
// if (err.isValid()) {
|
||||
// m_resolvedSource.bytecodecache_fd = 0;
|
||||
// m_cachedBytecode = JSC::CachedBytecode::create();
|
||||
// }
|
||||
// }
|
||||
// // TODO: read the bytecode into a JSC::SourceCode object here
|
||||
// case 1: {
|
||||
// }
|
||||
// }
|
||||
}
|
||||
int SourceProvider::readCache(JSC::VM& vm, const JSC::SourceCode& sourceCode)
|
||||
{
|
||||
return -1;
|
||||
// if (m_resolvedSource.bytecodecache_fd == 0)
|
||||
// return -1;
|
||||
// if (!FileSystem::isHandleValid(m_resolvedSource.bytecodecache_fd))
|
||||
// return -1;
|
||||
// const auto fd = m_resolvedSource.bytecodecache_fd;
|
||||
|
||||
// bool success;
|
||||
// FileSystem::MappedFileData mappedFile(fd, FileSystem::MappedFileMode::Shared, success);
|
||||
// if (!success)
|
||||
// return -1;
|
||||
|
||||
// const uint8_t* fileData = reinterpret_cast<const uint8_t*>(mappedFile.data());
|
||||
// unsigned fileTotalSize = mappedFile.size();
|
||||
// if (fileTotalSize == 0)
|
||||
// return 0;
|
||||
|
||||
// Ref<JSC::CachedBytecode> cachedBytecode = JSC::CachedBytecode::create(WTFMove(mappedFile));
|
||||
// // auto key = JSC::sourceCodeKeyForSerializedModule(vm, sourceCode);
|
||||
// // if (isCachedBytecodeStillValid(vm, cachedBytecode.copyRef(), key,
|
||||
// // JSC::SourceCodeType::ModuleType)) {
|
||||
// m_cachedBytecode = WTFMove(cachedBytecode);
|
||||
// return 1;
|
||||
// } else {
|
||||
// FileSystem::truncateFile(fd, 0);
|
||||
// return 0;
|
||||
// }
|
||||
}
|
||||
|
||||
extern "C" BunString ZigSourceProvider__getSourceSlice(SourceProvider* provider)
|
||||
{
|
||||
return Bun::toStringView(provider->source());
|
||||
}
|
||||
|
||||
}; // namespace Zig
|
||||
@@ -1,80 +0,0 @@
|
||||
#include "headers.h"
|
||||
#include "root.h"
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace JSC {
|
||||
class Structure;
|
||||
class Identifier;
|
||||
class SourceCodeKey;
|
||||
class SourceProvider;
|
||||
} // namespace JSC
|
||||
|
||||
#include <JavaScriptCore/CachedBytecode.h>
|
||||
#include <JavaScriptCore/JSGlobalObject.h>
|
||||
#include <JavaScriptCore/JSTypeInfo.h>
|
||||
#include <JavaScriptCore/SourceProvider.h>
|
||||
#include <JavaScriptCore/Structure.h>
|
||||
|
||||
namespace Zig {
|
||||
|
||||
class GlobalObject;
|
||||
|
||||
void forEachSourceProvider(WTF::Function<void(JSC::SourceID)>);
|
||||
JSC::SourceID sourceIDForSourceURL(const WTF::String& sourceURL);
|
||||
void* sourceMappingForSourceURL(const WTF::String& sourceURL);
|
||||
JSC::SourceOrigin toSourceOrigin(const String& sourceURL, bool isBuiltin);
|
||||
class SourceProvider final : public JSC::SourceProvider {
|
||||
WTF_DEPRECATED_MAKE_FAST_ALLOCATED(SourceProvider);
|
||||
using Base = JSC::SourceProvider;
|
||||
using BytecodeCacheGenerator = JSC::BytecodeCacheGenerator;
|
||||
using UnlinkedFunctionExecutable = JSC::UnlinkedFunctionExecutable;
|
||||
using CachedBytecode = JSC::CachedBytecode;
|
||||
using UnlinkedFunctionCodeBlock = JSC::UnlinkedFunctionCodeBlock;
|
||||
using SourceCode = JSC::SourceCode;
|
||||
using CodeSpecializationKind = JSC::CodeSpecializationKind;
|
||||
using SourceOrigin = JSC::SourceOrigin;
|
||||
|
||||
public:
|
||||
static Ref<SourceProvider> create(
|
||||
Zig::GlobalObject*,
|
||||
ResolvedSource& resolvedSource,
|
||||
JSC::SourceProviderSourceType sourceType = JSC::SourceProviderSourceType::Module,
|
||||
bool isBuiltIn = false);
|
||||
~SourceProvider();
|
||||
unsigned hash() const override;
|
||||
StringView source() const override;
|
||||
|
||||
RefPtr<JSC::CachedBytecode> cachedBytecode() const final
|
||||
{
|
||||
return m_cachedBytecode.copyRef();
|
||||
};
|
||||
|
||||
void updateCache(const UnlinkedFunctionExecutable* executable, const SourceCode&, CodeSpecializationKind kind, const UnlinkedFunctionCodeBlock* codeBlock);
|
||||
void cacheBytecode(const BytecodeCacheGenerator& generator);
|
||||
void commitCachedBytecode();
|
||||
bool isBytecodeCacheEnabled() const;
|
||||
void readOrGenerateByteCodeCache(JSC::VM& vm, const JSC::SourceCode& sourceCode);
|
||||
ResolvedSource m_resolvedSource;
|
||||
int readCache(JSC::VM& vm, const JSC::SourceCode& sourceCode);
|
||||
void freeSourceCode();
|
||||
|
||||
private:
|
||||
SourceProvider(Zig::GlobalObject* globalObject, ResolvedSource resolvedSource, Ref<WTF::StringImpl>&& sourceImpl,
|
||||
JSC::SourceTaintedOrigin taintedness,
|
||||
const SourceOrigin& sourceOrigin, WTF::String&& sourceURL,
|
||||
const TextPosition& startPosition, JSC::SourceProviderSourceType sourceType)
|
||||
: Base(sourceOrigin, WTFMove(sourceURL), String(), taintedness, startPosition, sourceType)
|
||||
, m_globalObject(globalObject)
|
||||
, m_source(sourceImpl)
|
||||
{
|
||||
m_resolvedSource = resolvedSource;
|
||||
}
|
||||
|
||||
Zig::GlobalObject* m_globalObject;
|
||||
RefPtr<JSC::CachedBytecode> m_cachedBytecode;
|
||||
Ref<WTF::StringImpl> m_source;
|
||||
unsigned m_hash = 0;
|
||||
};
|
||||
|
||||
} // namespace Zig
|
||||
@@ -105,29 +105,6 @@ typedef struct ErrorableString {
|
||||
ErrorableStringResult result;
|
||||
bool success;
|
||||
} ErrorableString;
|
||||
typedef struct ResolvedSource {
|
||||
BunString specifier;
|
||||
BunString source_code;
|
||||
BunString source_url;
|
||||
bool isCommonJSModule;
|
||||
JSC::EncodedJSValue cjsCustomExtension;
|
||||
void* allocator;
|
||||
JSC::EncodedJSValue jsvalue_for_export;
|
||||
uint32_t tag;
|
||||
bool needsDeref;
|
||||
bool already_bundled;
|
||||
uint8_t* bytecode_cache;
|
||||
size_t bytecode_cache_size;
|
||||
} ResolvedSource;
|
||||
static const uint32_t ResolvedSourceTagPackageJSONTypeModule = 1;
|
||||
typedef union ErrorableResolvedSourceResult {
|
||||
ResolvedSource value;
|
||||
ZigErrorType err;
|
||||
} ErrorableResolvedSourceResult;
|
||||
typedef struct ErrorableResolvedSource {
|
||||
ErrorableResolvedSourceResult result;
|
||||
bool success;
|
||||
} ErrorableResolvedSource;
|
||||
|
||||
typedef struct SystemError {
|
||||
int errno_;
|
||||
@@ -342,6 +319,54 @@ typedef struct {
|
||||
|
||||
#include "SyntheticModuleType.h"
|
||||
|
||||
// New module result types for refactored SourceProvider
|
||||
typedef struct TranspiledSourceFlags {
|
||||
uint32_t is_commonjs : 1;
|
||||
uint32_t is_already_bundled : 1;
|
||||
uint32_t from_package_json_type_module : 1;
|
||||
uint32_t _padding : 29;
|
||||
} TranspiledSourceFlags;
|
||||
|
||||
typedef struct TranspiledSource {
|
||||
BunString source_code;
|
||||
BunString source_url;
|
||||
uint8_t* bytecode_cache;
|
||||
size_t bytecode_cache_len;
|
||||
TranspiledSourceFlags flags;
|
||||
} TranspiledSource;
|
||||
|
||||
typedef uint8_t SpecialModuleTag;
|
||||
const SpecialModuleTag SpecialModuleTag_exports_object = 0;
|
||||
const SpecialModuleTag SpecialModuleTag_export_default_object = 1;
|
||||
const SpecialModuleTag SpecialModuleTag_custom_extension = 2;
|
||||
|
||||
typedef struct SpecialModule {
|
||||
SpecialModuleTag tag;
|
||||
JSC::EncodedJSValue jsvalue;
|
||||
} SpecialModule;
|
||||
|
||||
typedef struct ErrorResult {
|
||||
JSC::EncodedJSValue exception;
|
||||
} ErrorResult;
|
||||
|
||||
typedef uint8_t ModuleResultTag;
|
||||
const ModuleResultTag ModuleResultTag_transpiled = 0;
|
||||
const ModuleResultTag ModuleResultTag_special = 1;
|
||||
const ModuleResultTag ModuleResultTag_builtin = 2;
|
||||
const ModuleResultTag ModuleResultTag_err = 3;
|
||||
|
||||
typedef union ModuleResultValue {
|
||||
TranspiledSource transpiled;
|
||||
SpecialModule special;
|
||||
uint32_t builtin_id;
|
||||
ErrorResult err;
|
||||
} ModuleResultValue;
|
||||
|
||||
typedef struct ModuleResult {
|
||||
ModuleResultTag tag;
|
||||
ModuleResultValue value;
|
||||
} ModuleResult;
|
||||
|
||||
extern "C" const char* Bun__userAgent;
|
||||
|
||||
extern "C" ZigErrorCode Zig_ErrorCodeParserError;
|
||||
@@ -354,7 +379,7 @@ extern "C" bool Bun__transpileVirtualModule(
|
||||
const BunString* referrer,
|
||||
ZigString* sourceCode,
|
||||
BunLoaderType loader,
|
||||
ErrorableResolvedSource* result);
|
||||
ModuleResult* result);
|
||||
|
||||
extern "C" JSC::EncodedJSValue Bun__runVirtualModule(
|
||||
JSC::JSGlobalObject* global,
|
||||
@@ -366,7 +391,7 @@ extern "C" JSC::JSInternalPromise* Bun__transpileFile(
|
||||
BunString* specifier,
|
||||
BunString* referrer,
|
||||
const BunString* typeAttribute,
|
||||
ErrorableResolvedSource* result,
|
||||
ModuleResult* result,
|
||||
bool allowPromise,
|
||||
bool isCommonJSRequire,
|
||||
BunLoaderType forceLoaderType);
|
||||
@@ -376,11 +401,11 @@ extern "C" bool Bun__fetchBuiltinModule(
|
||||
JSC::JSGlobalObject* global,
|
||||
const BunString* specifier,
|
||||
const BunString* referrer,
|
||||
ErrorableResolvedSource* result);
|
||||
ModuleResult* result);
|
||||
extern "C" bool Bun__resolveAndFetchBuiltinModule(
|
||||
void* bunVM,
|
||||
const BunString* specifier,
|
||||
ErrorableResolvedSource* result);
|
||||
ModuleResult* result);
|
||||
|
||||
// Used in process.version
|
||||
extern "C" const char* Bun__version;
|
||||
|
||||
@@ -89,7 +89,9 @@ pub const RegularExpression = @import("./bindings/RegularExpression.zig").Regula
|
||||
|
||||
// JavaScript-related
|
||||
pub const Errorable = @import("./bindings/Errorable.zig").Errorable;
|
||||
pub const ResolvedSource = @import("./bindings/ResolvedSource.zig").ResolvedSource;
|
||||
pub const ModuleResult = @import("./bindings/ModuleResult.zig").ModuleResult;
|
||||
pub const TranspiledSource = @import("./bindings/TranspiledSource.zig").TranspiledSource;
|
||||
pub const SpecialModule = @import("./bindings/SpecialModule.zig").SpecialModule;
|
||||
pub const ErrorCode = @import("./bindings/ErrorCode.zig").ErrorCode;
|
||||
pub const JSErrorCode = @import("./bindings/JSErrorCode.zig").JSErrorCode;
|
||||
pub const ZigErrorType = @import("./bindings/ZigErrorType.zig").ZigErrorType;
|
||||
@@ -102,7 +104,10 @@ pub const EventType = @import("./bindings/EventType.zig").EventType;
|
||||
pub const JSRuntimeType = @import("./bindings/JSRuntimeType.zig").JSRuntimeType;
|
||||
pub const ZigStackFrameCode = @import("./bindings/ZigStackFrameCode.zig").ZigStackFrameCode;
|
||||
|
||||
pub const ErrorableResolvedSource = Errorable(ResolvedSource);
|
||||
// Note: ResolvedSource has been replaced by ModuleResult
|
||||
// For backward compatibility during migration, ErrorableResolvedSource is aliased to ErrorableModuleResult
|
||||
pub const ErrorableModuleResult = Errorable(ModuleResult);
|
||||
pub const ErrorableResolvedSource = ErrorableModuleResult; // Temporary alias for migration
|
||||
pub const ErrorableZigString = Errorable(ZigString);
|
||||
pub const ErrorableJSValue = Errorable(JSValue);
|
||||
pub const ErrorableString = Errorable(bun.String);
|
||||
|
||||
@@ -44,7 +44,7 @@
|
||||
#endif
|
||||
|
||||
#include "JSDOMConvertBase.h"
|
||||
#include "ZigSourceProvider.h"
|
||||
#include "BunSourceProvider.h"
|
||||
#include "mimalloc.h"
|
||||
|
||||
#include <JavaScriptCore/ControlFlowProfiler.h>
|
||||
|
||||
Reference in New Issue
Block a user