mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
14
.vscode/launch.json
vendored
14
.vscode/launch.json
vendored
@@ -11,6 +11,20 @@
|
||||
// "cwd": "${workspaceFolder}",
|
||||
// "console": "internalConsole"
|
||||
// },
|
||||
{
|
||||
"type": "lldb",
|
||||
"request": "launch",
|
||||
"name": "Eval",
|
||||
"program": "${workspaceFolder}/build/debug/macos-x86_64/spjs",
|
||||
"args": [
|
||||
"./simple.css",
|
||||
"--resolve=dev",
|
||||
"--outdir=outcss",
|
||||
"--public-url=https://localhost:9000/"
|
||||
],
|
||||
"cwd": "${workspaceFolder}/src/test/fixtures",
|
||||
"console": "internalConsole"
|
||||
},
|
||||
|
||||
{
|
||||
"type": "lldb",
|
||||
|
||||
17
build.zig
17
build.zig
@@ -27,7 +27,6 @@ pub fn build(b: *std.build.Builder) void {
|
||||
var cwd_buf = [_]u8{0} ** 4096;
|
||||
var cwd = std.os.getcwd(&cwd_buf) catch unreachable;
|
||||
var exe: *std.build.LibExeObjStep = undefined;
|
||||
var javascript: *std.build.LibExeObjStep = undefined;
|
||||
|
||||
var output_dir_buf = std.mem.zeroes([4096]u8);
|
||||
var bin_label = if (mode == std.builtin.Mode.Debug) "/debug/" else "/";
|
||||
@@ -90,7 +89,7 @@ pub fn build(b: *std.build.Builder) void {
|
||||
}
|
||||
// exe.setLibCFile("libc.txt");
|
||||
exe.linkLibC();
|
||||
|
||||
exe.linkLibCpp();
|
||||
exe.addPackage(.{
|
||||
.name = "clap",
|
||||
.path = .{ .path = "src/deps/zig-clap/clap.zig" },
|
||||
@@ -130,6 +129,20 @@ pub fn build(b: *std.build.Builder) void {
|
||||
// exe.addSystemIncludeDir(sys);
|
||||
// }
|
||||
addPicoHTTP(exe, cwd);
|
||||
var javascript = b.addExecutable("spjs", "src/main_javascript.zig");
|
||||
javascript.packages = exe.packages;
|
||||
javascript.setOutputDir(output_dir);
|
||||
javascript.setBuildMode(mode);
|
||||
javascript.linkLibC();
|
||||
javascript.linkLibCpp();
|
||||
|
||||
if (target.getOsTag() == .macos) {
|
||||
javascript.linkFramework("JavaScriptCore");
|
||||
exe.linkFramework("JavascriptCore");
|
||||
}
|
||||
|
||||
javascript.strip = false;
|
||||
javascript.install();
|
||||
}
|
||||
|
||||
exe.install();
|
||||
|
||||
7
src/api/schema.d.ts
vendored
7
src/api/schema.d.ts
vendored
@@ -55,13 +55,16 @@ type uint32 = number;
|
||||
}
|
||||
export enum Platform {
|
||||
browser = 1,
|
||||
node = 2
|
||||
node = 2,
|
||||
speedy = 3
|
||||
}
|
||||
export const PlatformKeys = {
|
||||
1: "browser",
|
||||
browser: "browser",
|
||||
2: "node",
|
||||
node: "node"
|
||||
node: "node",
|
||||
3: "speedy",
|
||||
speedy: "speedy"
|
||||
}
|
||||
export enum JSXRuntime {
|
||||
automatic = 1,
|
||||
|
||||
@@ -53,14 +53,18 @@ const ResolveModeKeys = {
|
||||
const Platform = {
|
||||
"1": 1,
|
||||
"2": 2,
|
||||
"3": 3,
|
||||
"browser": 1,
|
||||
"node": 2
|
||||
"node": 2,
|
||||
"speedy": 3
|
||||
};
|
||||
const PlatformKeys = {
|
||||
"1": "browser",
|
||||
"2": "node",
|
||||
"3": "speedy",
|
||||
"browser": "browser",
|
||||
"node": "node"
|
||||
"node": "node",
|
||||
"speedy": "speedy"
|
||||
};
|
||||
const JSXRuntime = {
|
||||
"1": 1,
|
||||
|
||||
@@ -20,6 +20,7 @@ smol ResolveMode {
|
||||
smol Platform {
|
||||
browser = 1;
|
||||
node = 2;
|
||||
speedy = 3;
|
||||
}
|
||||
|
||||
smol JSXRuntime {
|
||||
|
||||
2828
src/api/schema.zig
2828
src/api/schema.zig
File diff suppressed because it is too large
Load Diff
@@ -131,7 +131,7 @@ pub const ScanResult = struct {
|
||||
|
||||
pub fn NewBundler(cache_files: bool) type {
|
||||
return struct {
|
||||
const Linker = if (cache_files) linker.Linker else linker.ServeLinker;
|
||||
pub const Linker = if (cache_files) linker.Linker else linker.ServeLinker;
|
||||
pub const Resolver = if (cache_files) _resolver.Resolver else _resolver.ResolverUncached;
|
||||
|
||||
const ThisBundler = @This();
|
||||
@@ -164,8 +164,6 @@ pub fn NewBundler(cache_files: bool) type {
|
||||
) !ThisBundler {
|
||||
js_ast.Expr.Data.Store.create(allocator);
|
||||
js_ast.Stmt.Data.Store.create(allocator);
|
||||
js_ast.Expr.Data.Store.reset();
|
||||
js_ast.Stmt.Data.Store.reset();
|
||||
var fs = try Fs.FileSystem.init1(allocator, opts.absolute_working_dir, opts.serve orelse false);
|
||||
const bundle_options = try options.BundleOptions.fromApi(
|
||||
allocator,
|
||||
@@ -941,7 +939,14 @@ pub fn NewBundler(cache_files: bool) type {
|
||||
};
|
||||
},
|
||||
else => {
|
||||
var result = bundler.parse(allocator, file_path, loader, resolve_result.dirname_fd, file_descriptor, filepath_hash) orelse {
|
||||
var result = bundler.parse(
|
||||
allocator,
|
||||
file_path,
|
||||
loader,
|
||||
resolve_result.dirname_fd,
|
||||
file_descriptor,
|
||||
filepath_hash,
|
||||
) orelse {
|
||||
bundler.resetStore();
|
||||
return BuildResolveResultPair{
|
||||
.written = 0,
|
||||
@@ -1241,9 +1246,9 @@ pub fn NewBundler(cache_files: bool) type {
|
||||
jsx.parse = loader.isJSX();
|
||||
var opts = js_parser.Parser.Options.init(jsx, loader);
|
||||
opts.enable_bundling = false;
|
||||
opts.transform_require_to_import = true;
|
||||
opts.transform_require_to_import = bundler.options.platform != .speedy;
|
||||
opts.can_import_from_bundle = bundler.options.node_modules_bundle != null;
|
||||
opts.features.hot_module_reloading = bundler.options.hot_module_reloading;
|
||||
opts.features.hot_module_reloading = bundler.options.hot_module_reloading and bundler.options.platform != .speedy;
|
||||
opts.features.react_fast_refresh = opts.features.hot_module_reloading and jsx.parse and bundler.options.jsx.supports_fast_refresh;
|
||||
opts.filepath_hash_for_hmr = file_hash orelse 0;
|
||||
const value = (bundler.resolver.caches.js.parse(allocator, opts, bundler.options.define, bundler.log, &source) catch null) orelse return null;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
usingnamespace @import("global.zig");
|
||||
usingnamespace @import("global.zig");
|
||||
|
||||
usingnamespace @import("./http.zig");
|
||||
const std = @import("std");
|
||||
|
||||
@@ -234,6 +234,13 @@ pub fn HashMap(
|
||||
return self.unmanaged.putAssumeCapacityNoClobber(key, value);
|
||||
}
|
||||
|
||||
/// Asserts there is enough capacity to store the new key-value pair.
|
||||
/// Asserts that it does not clobber any existing data.
|
||||
/// To detect if a put would clobber existing data, see `getOrPutAssumeCapacity`.
|
||||
pub fn putAssumeCapacityNoClobberWithHash(self: *Self, key: K, hash: u64, value: V) void {
|
||||
return self.unmanaged.putAssumeCapacityNoClobberWithHash(key, hash, value);
|
||||
}
|
||||
|
||||
/// Inserts a new `Entry` into the hash map, returning the previous one, if any.
|
||||
pub fn fetchPut(self: *Self, key: K, value: V) !?Entry {
|
||||
return self.unmanaged.fetchPut(self.allocator, key, value);
|
||||
@@ -537,6 +544,12 @@ pub fn HashMapUnmanaged(
|
||||
assert(!self.contains(key));
|
||||
|
||||
const hash = hashFn(key);
|
||||
putAssumeCapacityNoClobberWithHash(self, key, hash, value);
|
||||
}
|
||||
|
||||
/// Insert an entry in the map. Assumes it is not already present,
|
||||
/// and that no allocation is needed.
|
||||
pub fn putAssumeCapacityNoClobberWithHash(self: *Self, key: K, hash: u64, value: V) void {
|
||||
const mask = self.capacity() - 1;
|
||||
var idx = @truncate(usize, hash & mask);
|
||||
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
const generic = opaque {};
|
||||
pub const Private = c_void;
|
||||
pub const struct_OpaqueJSContextGroup = opaque {};
|
||||
pub const JSContextGroupRef = ?*const struct_OpaqueJSContextGroup;
|
||||
pub const struct_OpaqueJSContext = opaque {};
|
||||
pub const JSContextRef = ?*const struct_OpaqueJSContext;
|
||||
pub const JSGlobalContextRef = ?*struct_OpaqueJSContext;
|
||||
pub const struct_OpaqueJSString = opaque {};
|
||||
pub const struct_OpaqueJSString = generic;
|
||||
pub const JSStringRef = ?*struct_OpaqueJSString;
|
||||
pub const struct_OpaqueJSClass = opaque {};
|
||||
pub const struct_OpaqueJSClass = generic;
|
||||
pub const JSClassRef = ?*struct_OpaqueJSClass;
|
||||
pub const struct_OpaqueJSPropertyNameArray = opaque {};
|
||||
pub const struct_OpaqueJSPropertyNameArray = generic;
|
||||
pub const JSPropertyNameArrayRef = ?*struct_OpaqueJSPropertyNameArray;
|
||||
pub const struct_OpaqueJSPropertyNameAccumulator = opaque {};
|
||||
pub const struct_OpaqueJSPropertyNameAccumulator = generic;
|
||||
pub const JSPropertyNameAccumulatorRef = ?*struct_OpaqueJSPropertyNameAccumulator;
|
||||
pub const JSTypedArrayBytesDeallocator = ?fn (?*c_void, ?*c_void) callconv(.C) void;
|
||||
pub const struct_OpaqueJSValue = opaque {};
|
||||
pub const JSValueRef = ?*const struct_OpaqueJSValue;
|
||||
pub const struct_OpaqueJSValue = generic;
|
||||
pub const JSValueRef = ?*struct_OpaqueJSValue;
|
||||
pub const JSObjectRef = ?*struct_OpaqueJSValue;
|
||||
pub extern fn JSEvaluateScript(ctx: JSContextRef, script: JSStringRef, thisObject: JSObjectRef, sourceURL: JSStringRef, startingLineNumber: c_int, exception: [*c]JSValueRef) JSValueRef;
|
||||
pub extern fn JSCheckScriptSyntax(ctx: JSContextRef, script: JSStringRef, sourceURL: JSStringRef, startingLineNumber: c_int, exception: [*c]JSValueRef) bool;
|
||||
@@ -115,7 +117,15 @@ pub const JSObjectGetPropertyCallback = ?fn (JSContextRef, JSObjectRef, JSString
|
||||
pub const JSObjectSetPropertyCallback = ?fn (JSContextRef, JSObjectRef, JSStringRef, JSValueRef, [*c]JSValueRef) callconv(.C) bool;
|
||||
pub const JSObjectDeletePropertyCallback = ?fn (JSContextRef, JSObjectRef, JSStringRef, [*c]JSValueRef) callconv(.C) bool;
|
||||
pub const JSObjectGetPropertyNamesCallback = ?fn (JSContextRef, JSObjectRef, JSPropertyNameAccumulatorRef) callconv(.C) void;
|
||||
pub const JSObjectCallAsFunctionCallback = ?fn (ctx: JSContextRef, function: JSObjectRef, thisObject: JSObjectRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) callconv(.C) JSValueRef;
|
||||
|
||||
pub const JSObjectCallAsFunctionCallback = ?fn (
|
||||
ctx: JSContextRef,
|
||||
function: JSObjectRef,
|
||||
thisObject: JSObjectRef,
|
||||
argumentCount: usize,
|
||||
arguments: [*c]const JSValueRef,
|
||||
exception: [*c]JSValueRef,
|
||||
) callconv(.C) JSValueRef;
|
||||
pub const JSObjectCallAsConstructorCallback = ?fn (JSContextRef, JSObjectRef, usize, [*c]const JSValueRef, [*c]JSValueRef) callconv(.C) JSObjectRef;
|
||||
pub const JSObjectHasInstanceCallback = ?fn (JSContextRef, JSObjectRef, JSValueRef, [*c]JSValueRef) callconv(.C) bool;
|
||||
pub const JSObjectConvertToTypeCallback = ?fn (JSContextRef, JSObjectRef, JSType, [*c]JSValueRef) callconv(.C) JSValueRef;
|
||||
@@ -150,54 +160,54 @@ pub const JSClassDefinition = extern struct {
|
||||
convertToType: JSObjectConvertToTypeCallback,
|
||||
};
|
||||
pub extern const kJSClassDefinitionEmpty: JSClassDefinition;
|
||||
pub extern fn JSClassCreate(definition: [*c]const JSClassDefinition) JSClassRef;
|
||||
pub extern fn JSClassRetain(jsClass: JSClassRef) JSClassRef;
|
||||
pub extern fn JSClassRelease(jsClass: JSClassRef) void;
|
||||
pub extern fn JSObjectMake(ctx: JSContextRef, jsClass: JSClassRef, data: ?*c_void) JSObjectRef;
|
||||
pub extern fn JSObjectMakeFunctionWithCallback(ctx: JSContextRef, name: JSStringRef, callAsFunction: JSObjectCallAsFunctionCallback) JSObjectRef;
|
||||
pub extern fn JSObjectMakeConstructor(ctx: JSContextRef, jsClass: JSClassRef, callAsConstructor: JSObjectCallAsConstructorCallback) JSObjectRef;
|
||||
pub extern fn JSObjectMakeArray(ctx: JSContextRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern fn JSObjectMakeDate(ctx: JSContextRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern fn JSObjectMakeError(ctx: JSContextRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern fn JSObjectMakeRegExp(ctx: JSContextRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern fn JSObjectMakeDeferredPromise(ctx: JSContextRef, resolve: [*c]JSObjectRef, reject: [*c]JSObjectRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern fn JSObjectMakeFunction(ctx: JSContextRef, name: JSStringRef, parameterCount: c_uint, parameterNames: [*c]const JSStringRef, body: JSStringRef, sourceURL: JSStringRef, startingLineNumber: c_int, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern fn JSObjectGetPrototype(ctx: JSContextRef, object: JSObjectRef) JSValueRef;
|
||||
pub extern fn JSObjectSetPrototype(ctx: JSContextRef, object: JSObjectRef, value: JSValueRef) void;
|
||||
pub extern fn JSObjectHasProperty(ctx: JSContextRef, object: JSObjectRef, propertyName: JSStringRef) bool;
|
||||
pub extern fn JSObjectGetProperty(ctx: JSContextRef, object: JSObjectRef, propertyName: JSStringRef, exception: [*c]JSValueRef) JSValueRef;
|
||||
pub extern fn JSObjectSetProperty(ctx: JSContextRef, object: JSObjectRef, propertyName: JSStringRef, value: JSValueRef, attributes: JSPropertyAttributes, exception: [*c]JSValueRef) void;
|
||||
pub extern fn JSObjectDeleteProperty(ctx: JSContextRef, object: JSObjectRef, propertyName: JSStringRef, exception: [*c]JSValueRef) bool;
|
||||
pub extern fn JSObjectHasPropertyForKey(ctx: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, exception: [*c]JSValueRef) bool;
|
||||
pub extern fn JSObjectGetPropertyForKey(ctx: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, exception: [*c]JSValueRef) JSValueRef;
|
||||
pub extern fn JSObjectSetPropertyForKey(ctx: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, value: JSValueRef, attributes: JSPropertyAttributes, exception: [*c]JSValueRef) void;
|
||||
pub extern fn JSObjectDeletePropertyForKey(ctx: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, exception: [*c]JSValueRef) bool;
|
||||
pub extern fn JSObjectGetPropertyAtIndex(ctx: JSContextRef, object: JSObjectRef, propertyIndex: c_uint, exception: [*c]JSValueRef) JSValueRef;
|
||||
pub extern fn JSObjectSetPropertyAtIndex(ctx: JSContextRef, object: JSObjectRef, propertyIndex: c_uint, value: JSValueRef, exception: [*c]JSValueRef) void;
|
||||
pub extern fn JSObjectGetPrivate(object: JSObjectRef) ?*c_void;
|
||||
pub extern fn JSObjectSetPrivate(object: JSObjectRef, data: ?*c_void) bool;
|
||||
pub extern fn JSObjectIsFunction(ctx: JSContextRef, object: JSObjectRef) bool;
|
||||
pub extern fn JSObjectCallAsFunction(ctx: JSContextRef, object: JSObjectRef, thisObject: JSObjectRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSValueRef;
|
||||
pub extern fn JSObjectIsConstructor(ctx: JSContextRef, object: JSObjectRef) bool;
|
||||
pub extern fn JSObjectCallAsConstructor(ctx: JSContextRef, object: JSObjectRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern fn JSObjectCopyPropertyNames(ctx: JSContextRef, object: JSObjectRef) JSPropertyNameArrayRef;
|
||||
pub extern fn JSPropertyNameArrayRetain(array: JSPropertyNameArrayRef) JSPropertyNameArrayRef;
|
||||
pub extern fn JSPropertyNameArrayRelease(array: JSPropertyNameArrayRef) void;
|
||||
pub extern fn JSPropertyNameArrayGetCount(array: JSPropertyNameArrayRef) usize;
|
||||
pub extern fn JSPropertyNameArrayGetNameAtIndex(array: JSPropertyNameArrayRef, index: usize) JSStringRef;
|
||||
pub extern fn JSPropertyNameAccumulatorAddName(accumulator: JSPropertyNameAccumulatorRef, propertyName: JSStringRef) void;
|
||||
pub extern fn JSContextGroupCreate() JSContextGroupRef;
|
||||
pub extern fn JSContextGroupRetain(group: JSContextGroupRef) JSContextGroupRef;
|
||||
pub extern fn JSContextGroupRelease(group: JSContextGroupRef) void;
|
||||
pub extern fn JSGlobalContextCreate(globalObjectClass: JSClassRef) JSGlobalContextRef;
|
||||
pub extern fn JSGlobalContextCreateInGroup(group: JSContextGroupRef, globalObjectClass: JSClassRef) JSGlobalContextRef;
|
||||
pub extern fn JSGlobalContextRetain(ctx: JSGlobalContextRef) JSGlobalContextRef;
|
||||
pub extern fn JSGlobalContextRelease(ctx: JSGlobalContextRef) void;
|
||||
pub extern fn JSContextGetGlobalObject(ctx: JSContextRef) JSObjectRef;
|
||||
pub extern fn JSContextGetGroup(ctx: JSContextRef) JSContextGroupRef;
|
||||
pub extern fn JSContextGetGlobalContext(ctx: JSContextRef) JSGlobalContextRef;
|
||||
pub extern fn JSGlobalContextCopyName(ctx: JSGlobalContextRef) JSStringRef;
|
||||
pub extern fn JSGlobalContextSetName(ctx: JSGlobalContextRef, name: JSStringRef) void;
|
||||
pub extern "c" fn JSClassCreate(definition: [*c]const JSClassDefinition) JSClassRef;
|
||||
pub extern "c" fn JSClassRetain(jsClass: JSClassRef) JSClassRef;
|
||||
pub extern "c" fn JSClassRelease(jsClass: JSClassRef) void;
|
||||
pub extern "c" fn JSObjectMake(ctx: JSContextRef, jsClass: JSClassRef, data: ?*c_void) JSObjectRef;
|
||||
pub extern "c" fn JSObjectMakeFunctionWithCallback(ctx: JSContextRef, name: JSStringRef, callAsFunction: JSObjectCallAsFunctionCallback) JSObjectRef;
|
||||
pub extern "c" fn JSObjectMakeConstructor(ctx: JSContextRef, jsClass: JSClassRef, callAsConstructor: JSObjectCallAsConstructorCallback) JSObjectRef;
|
||||
pub extern "c" fn JSObjectMakeArray(ctx: JSContextRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern "c" fn JSObjectMakeDate(ctx: JSContextRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern "c" fn JSObjectMakeError(ctx: JSContextRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern "c" fn JSObjectMakeRegExp(ctx: JSContextRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern "c" fn JSObjectMakeDeferredPromise(ctx: JSContextRef, resolve: [*c]JSObjectRef, reject: [*c]JSObjectRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern "c" fn JSObjectMakeFunction(ctx: JSContextRef, name: JSStringRef, parameterCount: c_uint, parameterNames: [*c]const JSStringRef, body: JSStringRef, sourceURL: JSStringRef, startingLineNumber: c_int, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern "c" fn JSObjectGetPrototype(ctx: JSContextRef, object: JSObjectRef) JSValueRef;
|
||||
pub extern "c" fn JSObjectSetPrototype(ctx: JSContextRef, object: JSObjectRef, value: JSValueRef) void;
|
||||
pub extern "c" fn JSObjectHasProperty(ctx: JSContextRef, object: JSObjectRef, propertyName: JSStringRef) bool;
|
||||
pub extern "c" fn JSObjectGetProperty(ctx: JSContextRef, object: JSObjectRef, propertyName: JSStringRef, exception: [*c]JSValueRef) JSValueRef;
|
||||
pub extern "c" fn JSObjectSetProperty(ctx: JSContextRef, object: JSObjectRef, propertyName: JSStringRef, value: JSValueRef, attributes: c_uint, exception: [*c]JSValueRef) void;
|
||||
pub extern "c" fn JSObjectDeleteProperty(ctx: JSContextRef, object: JSObjectRef, propertyName: JSStringRef, exception: [*c]JSValueRef) bool;
|
||||
pub extern "c" fn JSObjectHasPropertyForKey(ctx: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, exception: [*c]JSValueRef) bool;
|
||||
pub extern "c" fn JSObjectGetPropertyForKey(ctx: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, exception: [*c]JSValueRef) JSValueRef;
|
||||
pub extern "c" fn JSObjectSetPropertyForKey(ctx: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, value: JSValueRef, attributes: JSPropertyAttributes, exception: [*c]JSValueRef) void;
|
||||
pub extern "c" fn JSObjectDeletePropertyForKey(ctx: JSContextRef, object: JSObjectRef, propertyKey: JSValueRef, exception: [*c]JSValueRef) bool;
|
||||
pub extern "c" fn JSObjectGetPropertyAtIndex(ctx: JSContextRef, object: JSObjectRef, propertyIndex: c_uint, exception: [*c]JSValueRef) JSValueRef;
|
||||
pub extern "c" fn JSObjectSetPropertyAtIndex(ctx: JSContextRef, object: JSObjectRef, propertyIndex: c_uint, value: JSValueRef, exception: [*c]JSValueRef) void;
|
||||
pub extern "c" fn JSObjectGetPrivate(object: JSObjectRef) ?*c_void;
|
||||
pub extern "c" fn JSObjectSetPrivate(object: JSObjectRef, data: ?*c_void) bool;
|
||||
pub extern "c" fn JSObjectIsFunction(ctx: JSContextRef, object: JSObjectRef) bool;
|
||||
pub extern "c" fn JSObjectCallAsFunction(ctx: JSContextRef, object: JSObjectRef, thisObject: JSObjectRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSValueRef;
|
||||
pub extern "c" fn JSObjectIsConstructor(ctx: JSContextRef, object: JSObjectRef) bool;
|
||||
pub extern "c" fn JSObjectCallAsConstructor(ctx: JSContextRef, object: JSObjectRef, argumentCount: usize, arguments: [*c]const JSValueRef, exception: [*c]JSValueRef) JSObjectRef;
|
||||
pub extern "c" fn JSObjectCopyPropertyNames(ctx: JSContextRef, object: JSObjectRef) JSPropertyNameArrayRef;
|
||||
pub extern "c" fn JSPropertyNameArrayRetain(array: JSPropertyNameArrayRef) JSPropertyNameArrayRef;
|
||||
pub extern "c" fn JSPropertyNameArrayRelease(array: JSPropertyNameArrayRef) void;
|
||||
pub extern "c" fn JSPropertyNameArrayGetCount(array: JSPropertyNameArrayRef) usize;
|
||||
pub extern "c" fn JSPropertyNameArrayGetNameAtIndex(array: JSPropertyNameArrayRef, index: usize) JSStringRef;
|
||||
pub extern "c" fn JSPropertyNameAccumulatorAddName(accumulator: JSPropertyNameAccumulatorRef, propertyName: JSStringRef) void;
|
||||
pub extern "c" fn JSContextGroupCreate() JSContextGroupRef;
|
||||
pub extern "c" fn JSContextGroupRetain(group: JSContextGroupRef) JSContextGroupRef;
|
||||
pub extern "c" fn JSContextGroupRelease(group: JSContextGroupRef) void;
|
||||
pub extern "c" fn JSGlobalContextCreate(globalObjectClass: JSClassRef) JSGlobalContextRef;
|
||||
pub extern "c" fn JSGlobalContextCreateInGroup(group: JSContextGroupRef, globalObjectClass: JSClassRef) JSGlobalContextRef;
|
||||
pub extern "c" fn JSGlobalContextRetain(ctx: JSGlobalContextRef) JSGlobalContextRef;
|
||||
pub extern "c" fn JSGlobalContextRelease(ctx: JSGlobalContextRef) void;
|
||||
pub extern "c" fn JSContextGetGlobalObject(ctx: JSContextRef) JSObjectRef;
|
||||
pub extern "c" fn JSContextGetGroup(ctx: JSContextRef) JSContextGroupRef;
|
||||
pub extern "c" fn JSContextGetGlobalContext(ctx: JSContextRef) JSGlobalContextRef;
|
||||
pub extern "c" fn JSGlobalContextCopyName(ctx: JSGlobalContextRef) JSStringRef;
|
||||
pub extern "c" fn JSGlobalContextSetName(ctx: JSGlobalContextRef, name: JSStringRef) void;
|
||||
pub const JSChar = c_ushort;
|
||||
pub extern fn JSStringCreateWithCharacters(chars: [*c]const JSChar, numChars: usize) JSStringRef;
|
||||
pub extern fn JSStringCreateWithUTF8CString(string: [*c]const u8) JSStringRef;
|
||||
@@ -235,4 +245,4 @@ pub const OpaqueJSValue = struct_OpaqueJSValue;
|
||||
// StringImpl::createWithoutCopying
|
||||
// https://github.com/WebKit/webkit/blob/main/Source/JavaScriptCore/API/JSStringRef.cpp#L62
|
||||
pub extern fn JSStringCreateWithCharactersNoCopy(string: [*c]const JSChar, numChars: size_t) JSStringRef;
|
||||
|
||||
const size_t = usize;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
374
src/main_javascript.zig
Normal file
374
src/main_javascript.zig
Normal file
@@ -0,0 +1,374 @@
|
||||
const std = @import("std");
|
||||
const lex = @import("js_lexer.zig");
|
||||
const logger = @import("logger.zig");
|
||||
const alloc = @import("alloc.zig");
|
||||
const options = @import("options.zig");
|
||||
const js_parser = @import("js_parser.zig");
|
||||
const json_parser = @import("json_parser.zig");
|
||||
const js_printer = @import("js_printer.zig");
|
||||
const js_ast = @import("js_ast.zig");
|
||||
const linker = @import("linker.zig");
|
||||
usingnamespace @import("ast/base.zig");
|
||||
usingnamespace @import("defines.zig");
|
||||
usingnamespace @import("global.zig");
|
||||
const panicky = @import("panic_handler.zig");
|
||||
pub const MainPanicHandler = panicky.NewPanicHandler(panicky.default_panic);
|
||||
const Api = @import("api/schema.zig").Api;
|
||||
const resolve_path = @import("./resolver/resolve_path.zig");
|
||||
const clap = @import("clap");
|
||||
const bundler = @import("bundler.zig");
|
||||
const fs = @import("fs.zig");
|
||||
const NodeModuleBundle = @import("./node_module_bundle.zig").NodeModuleBundle;
|
||||
const js = @import("javascript/jsc/javascript.zig");
|
||||
const allocators = @import("allocators.zig");
|
||||
pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn {
|
||||
if (MainPanicHandler.Singleton) |singleton| {
|
||||
MainPanicHandler.handle_panic(msg, error_return_trace);
|
||||
} else {
|
||||
panicky.default_panic(msg, error_return_trace);
|
||||
}
|
||||
}
|
||||
const constStrToU8 = allocators.constStrToU8;
|
||||
pub fn main() anyerror!void {
|
||||
// The memory allocator makes a massive difference.
|
||||
// std.heap.raw_c_allocator and std.heap.c_allocator perform similarly.
|
||||
// std.heap.GeneralPurposeAllocator makes this about 3x _slower_ than esbuild.
|
||||
// var root_alloc = std.heap.ArenaAllocator.init(std.heap.raw_c_allocator);
|
||||
// var root_alloc_ = &root_alloc.allocator;
|
||||
try alloc.setup(std.heap.c_allocator);
|
||||
var stdout = std.io.getStdOut();
|
||||
// var stdout = std.io.bufferedWriter(stdout_file.writer());
|
||||
var stderr = std.io.getStdErr();
|
||||
// var stderr = std.io.bufferedWriter(stderr_file.writer());
|
||||
var output_source = Output.Source.init(stdout, stderr);
|
||||
// defer stdout.flush() catch {};
|
||||
// defer stderr.flush() catch {};
|
||||
Output.Source.set(&output_source);
|
||||
Output.enable_ansi_colors = stderr.isTty();
|
||||
defer Output.flush();
|
||||
try Cli.start(
|
||||
std.heap.c_allocator,
|
||||
stdout,
|
||||
stderr,
|
||||
);
|
||||
}
|
||||
|
||||
pub const Cli = struct {
|
||||
const LoaderMatcher = strings.ExactSizeMatcher(4);
|
||||
pub fn ColonListType(comptime t: type, value_resolver: anytype) type {
|
||||
return struct {
|
||||
pub fn init(allocator: *std.mem.Allocator, count: usize) !@This() {
|
||||
var keys = try allocator.alloc(string, count);
|
||||
var values = try allocator.alloc(t, count);
|
||||
|
||||
return @This(){ .keys = keys, .values = values };
|
||||
}
|
||||
keys: []string,
|
||||
values: []t,
|
||||
|
||||
pub fn load(self: *@This(), input: []const string) !void {
|
||||
for (input) |str, i| {
|
||||
// Support either ":" or "=" as the separator, preferring whichever is first.
|
||||
// ":" is less confusing IMO because that syntax is used with flags
|
||||
// but "=" is what esbuild uses and I want this to be somewhat familiar for people using esbuild
|
||||
const midpoint = std.math.min(strings.indexOfChar(str, ':') orelse std.math.maxInt(usize), strings.indexOfChar(str, '=') orelse std.math.maxInt(usize));
|
||||
if (midpoint == std.math.maxInt(usize)) {
|
||||
return error.InvalidSeparator;
|
||||
}
|
||||
|
||||
self.keys[i] = str[0..midpoint];
|
||||
self.values[i] = try value_resolver(str[midpoint + 1 .. str.len]);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve(allocator: *std.mem.Allocator, input: []const string) !@This() {
|
||||
var list = try init(allocator, input.len);
|
||||
try list.load(input);
|
||||
return list;
|
||||
}
|
||||
};
|
||||
}
|
||||
pub const LoaderColonList = ColonListType(Api.Loader, Arguments.loader_resolver);
|
||||
pub const DefineColonList = ColonListType(string, Arguments.noop_resolver);
|
||||
|
||||
pub const Arguments = struct {
|
||||
pub fn loader_resolver(in: string) !Api.Loader {
|
||||
const Matcher = strings.ExactSizeMatcher(4);
|
||||
switch (Matcher.match(in)) {
|
||||
Matcher.case("jsx") => return Api.Loader.jsx,
|
||||
Matcher.case("js") => return Api.Loader.js,
|
||||
Matcher.case("ts") => return Api.Loader.ts,
|
||||
Matcher.case("tsx") => return Api.Loader.tsx,
|
||||
Matcher.case("css") => return Api.Loader.css,
|
||||
Matcher.case("file") => return Api.Loader.file,
|
||||
Matcher.case("json") => return Api.Loader.json,
|
||||
else => {
|
||||
return error.InvalidLoader;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn noop_resolver(in: string) !string {
|
||||
return in;
|
||||
}
|
||||
|
||||
pub fn fileReadError(err: anyerror, stderr: anytype, filename: string, kind: string) noreturn {
|
||||
stderr.writer().print("Error reading file \"{s}\" for {s}: {s}", .{ filename, kind, @errorName(err) }) catch {};
|
||||
std.process.exit(1);
|
||||
}
|
||||
|
||||
pub fn readFile(
|
||||
allocator: *std.mem.Allocator,
|
||||
cwd: string,
|
||||
filename: string,
|
||||
) ![]u8 {
|
||||
var paths = [_]string{ cwd, filename };
|
||||
const outpath = try std.fs.path.resolve(allocator, &paths);
|
||||
defer allocator.free(outpath);
|
||||
var file = try std.fs.openFileAbsolute(outpath, std.fs.File.OpenFlags{ .read = true, .write = false });
|
||||
defer file.close();
|
||||
const stats = try file.stat();
|
||||
return try file.readToEndAlloc(allocator, stats.size);
|
||||
}
|
||||
|
||||
pub fn parse(allocator: *std.mem.Allocator, stdout: anytype, stderr: anytype) !Api.TransformOptions {
|
||||
@setEvalBranchQuota(9999);
|
||||
const params = comptime [_]clap.Param(clap.Help){
|
||||
clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
|
||||
clap.parseParam("-r, --resolve <STR> Determine import/require behavior. \"disable\" ignores. \"dev\" bundles node_modules and builds everything else as independent entry points") catch unreachable,
|
||||
clap.parseParam("-d, --define <STR>... Substitute K:V while parsing, e.g. --define process.env.NODE_ENV:development") catch unreachable,
|
||||
clap.parseParam("-l, --loader <STR>... Parse files with .ext:loader, e.g. --loader .js:jsx. Valid loaders: jsx, js, json, tsx (not implemented yet), ts (not implemented yet), css (not implemented yet)") catch unreachable,
|
||||
clap.parseParam("-o, --outdir <STR> Save output to directory (default: \"out\" if none provided and multiple entry points passed)") catch unreachable,
|
||||
clap.parseParam("-e, --external <STR>... Exclude module from transpilation (can use * wildcards). ex: -e react") catch unreachable,
|
||||
clap.parseParam("-i, --inject <STR>... Inject module at the top of every file") catch unreachable,
|
||||
clap.parseParam("--cwd <STR> Absolute path to resolve entry points from. Defaults to cwd") catch unreachable,
|
||||
clap.parseParam("--public-url <STR> Rewrite import paths to start with --public-url. Useful for web browsers.") catch unreachable,
|
||||
clap.parseParam("--serve Start a local dev server. This also sets resolve to \"lazy\".") catch unreachable,
|
||||
clap.parseParam("--public-dir <STR> Top-level directory for .html files, fonts, images, or anything external. Only relevant with --serve. Defaults to \"<cwd>/public\", to match create-react-app and Next.js") catch unreachable,
|
||||
clap.parseParam("--jsx-factory <STR> Changes the function called when compiling JSX elements using the classic JSX runtime") catch unreachable,
|
||||
clap.parseParam("--jsx-fragment <STR> Changes the function called when compiling JSX fragments using the classic JSX runtime") catch unreachable,
|
||||
clap.parseParam("--jsx-import-source <STR> Declares the module specifier to be used for importing the jsx and jsxs factory functions. Default: \"react\"") catch unreachable,
|
||||
clap.parseParam("--jsx-runtime <STR> \"automatic\" (default) or \"classic\"") catch unreachable,
|
||||
clap.parseParam("--jsx-production Use jsx instead of jsxDEV (default) for the automatic runtime") catch unreachable,
|
||||
clap.parseParam("--extension-order <STR>... defaults to: .tsx,.ts,.jsx,.js,.json ") catch unreachable,
|
||||
clap.parseParam("--disable-react-fast-refresh Disable React Fast Refresh. Enabled if --serve is set and --jsx-production is not set. Otherwise, it's a noop.") catch unreachable,
|
||||
clap.parseParam("--tsconfig-override <STR> Load tsconfig from path instead of cwd/tsconfig.json") catch unreachable,
|
||||
clap.parseParam("--platform <STR> \"browser\" or \"node\". Defaults to \"browser\"") catch unreachable,
|
||||
clap.parseParam("--main-fields <STR>... Main fields to lookup in package.json. Defaults to --platform dependent") catch unreachable,
|
||||
clap.parseParam("--scan Instead of bundling or transpiling, print a list of every file imported by an entry point, recursively") catch unreachable,
|
||||
clap.parseParam("--new-jsb Generate a new node_modules.jsb file from node_modules and entry point(s)") catch unreachable,
|
||||
clap.parseParam("--jsb <STR> Use a Speedy JavaScript Bundle (default: \"./node_modules.jsb\" if exists)") catch unreachable,
|
||||
// clap.parseParam("--no-jsb Use a Speedy JavaScript Bundle (default: \"./node_modules.jsb\" if exists)") catch unreachable,
|
||||
clap.parseParam("<POS>... Entry points to use") catch unreachable,
|
||||
};
|
||||
|
||||
var diag = clap.Diagnostic{};
|
||||
|
||||
var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| {
|
||||
// Report useful error and exit
|
||||
diag.report(stderr.writer(), err) catch {};
|
||||
return err;
|
||||
};
|
||||
|
||||
if (args.flag("--help")) {
|
||||
try clap.help(stderr.writer(), ¶ms);
|
||||
std.process.exit(1);
|
||||
}
|
||||
|
||||
var cwd_paths = [_]string{args.option("--cwd") orelse try std.process.getCwdAlloc(allocator)};
|
||||
var cwd = try std.fs.path.resolve(allocator, &cwd_paths);
|
||||
var tsconfig_override = if (args.option("--tsconfig-override")) |ts| (Arguments.readFile(allocator, cwd, ts) catch |err| fileReadError(err, stderr, ts, "tsconfig.json")) else null;
|
||||
var public_url = args.option("--public-url");
|
||||
var defines_tuple = try DefineColonList.resolve(allocator, args.options("--define"));
|
||||
var loader_tuple = try LoaderColonList.resolve(allocator, args.options("--define"));
|
||||
|
||||
var define_keys = defines_tuple.keys;
|
||||
var define_values = defines_tuple.values;
|
||||
var loader_keys = loader_tuple.keys;
|
||||
var loader_values = loader_tuple.values;
|
||||
var entry_points = args.positionals();
|
||||
var inject = args.options("--inject");
|
||||
var output_dir = args.option("--outdir");
|
||||
const serve = false;
|
||||
|
||||
var write = entry_points.len > 1 or output_dir != null;
|
||||
if (write and output_dir == null) {
|
||||
var _paths = [_]string{ cwd, "out" };
|
||||
output_dir = try std.fs.path.resolve(allocator, &_paths);
|
||||
}
|
||||
var externals = std.mem.zeroes([][]u8);
|
||||
if (args.options("--external").len > 0) {
|
||||
externals = try allocator.alloc([]u8, args.options("--external").len);
|
||||
for (args.options("--external")) |external, i| {
|
||||
externals[i] = constStrToU8(external);
|
||||
}
|
||||
}
|
||||
|
||||
var jsx_factory = args.option("--jsx-factory");
|
||||
var jsx_fragment = args.option("--jsx-fragment");
|
||||
var jsx_import_source = args.option("--jsx-import-source");
|
||||
var jsx_runtime = args.option("--jsx-runtime");
|
||||
var jsx_production = args.flag("--jsx-production");
|
||||
var react_fast_refresh = false;
|
||||
|
||||
if (serve or args.flag("--new-jsb")) {
|
||||
react_fast_refresh = true;
|
||||
if (args.flag("--disable-react-fast-refresh") or jsx_production) {
|
||||
react_fast_refresh = false;
|
||||
}
|
||||
}
|
||||
|
||||
var main_fields = args.options("--main-fields");
|
||||
|
||||
var node_modules_bundle_path = args.option("--jsb") orelse brk: {
|
||||
if (args.flag("--new-jsb")) {
|
||||
break :brk null;
|
||||
}
|
||||
|
||||
const node_modules_bundle_path_absolute = resolve_path.joinAbs(cwd, .auto, "node_modules.jsb");
|
||||
std.fs.accessAbsolute(node_modules_bundle_path_absolute, .{}) catch |err| {
|
||||
break :brk null;
|
||||
};
|
||||
break :brk try allocator.dupe(u8, node_modules_bundle_path_absolute);
|
||||
};
|
||||
|
||||
if (args.flag("--new-jsb")) {
|
||||
node_modules_bundle_path = null;
|
||||
}
|
||||
|
||||
const PlatformMatcher = strings.ExactSizeMatcher(8);
|
||||
const ResoveMatcher = strings.ExactSizeMatcher(8);
|
||||
|
||||
var resolve = Api.ResolveMode.lazy;
|
||||
if (args.option("--resolve")) |_resolve| {
|
||||
switch (PlatformMatcher.match(_resolve)) {
|
||||
PlatformMatcher.case("disable") => {
|
||||
resolve = Api.ResolveMode.disable;
|
||||
},
|
||||
PlatformMatcher.case("bundle") => {
|
||||
resolve = Api.ResolveMode.bundle;
|
||||
},
|
||||
PlatformMatcher.case("dev") => {
|
||||
resolve = Api.ResolveMode.dev;
|
||||
},
|
||||
PlatformMatcher.case("lazy") => {
|
||||
resolve = Api.ResolveMode.lazy;
|
||||
},
|
||||
else => {
|
||||
diag.name.long = "--resolve";
|
||||
diag.arg = _resolve;
|
||||
try diag.report(stderr.writer(), error.InvalidResolveOption);
|
||||
std.process.exit(1);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var platform: ?Api.Platform = null;
|
||||
|
||||
if (args.option("--platform")) |_platform| {
|
||||
switch (PlatformMatcher.match(_platform)) {
|
||||
PlatformMatcher.case("browser") => {
|
||||
platform = Api.Platform.browser;
|
||||
},
|
||||
PlatformMatcher.case("node") => {
|
||||
platform = Api.Platform.node;
|
||||
},
|
||||
else => {
|
||||
diag.name.long = "--platform";
|
||||
diag.arg = _platform;
|
||||
try diag.report(stderr.writer(), error.InvalidPlatform);
|
||||
std.process.exit(1);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var jsx: ?Api.Jsx = null;
|
||||
if (jsx_factory != null or
|
||||
jsx_fragment != null or
|
||||
jsx_import_source != null or
|
||||
jsx_runtime != null or
|
||||
jsx_production or react_fast_refresh)
|
||||
{
|
||||
var default_factory = "".*;
|
||||
var default_fragment = "".*;
|
||||
var default_import_source = "".*;
|
||||
jsx = Api.Jsx{
|
||||
.factory = constStrToU8(jsx_factory orelse &default_factory),
|
||||
.fragment = constStrToU8(jsx_fragment orelse &default_fragment),
|
||||
.import_source = constStrToU8(jsx_import_source orelse &default_import_source),
|
||||
.runtime = if (jsx_runtime != null) try resolve_jsx_runtime(jsx_runtime.?) else Api.JsxRuntime.automatic,
|
||||
.development = !jsx_production,
|
||||
.react_fast_refresh = react_fast_refresh,
|
||||
};
|
||||
}
|
||||
|
||||
if (entry_points.len == 0) {
|
||||
try clap.help(stderr.writer(), ¶ms);
|
||||
try diag.report(stderr.writer(), error.MissingEntryPoint);
|
||||
std.process.exit(1);
|
||||
}
|
||||
|
||||
return Api.TransformOptions{
|
||||
.jsx = jsx,
|
||||
.output_dir = output_dir,
|
||||
.resolve = resolve,
|
||||
.external = externals,
|
||||
.absolute_working_dir = cwd,
|
||||
.tsconfig_override = tsconfig_override,
|
||||
.public_url = public_url,
|
||||
.define = .{
|
||||
.keys = define_keys,
|
||||
.values = define_values,
|
||||
},
|
||||
.loaders = .{
|
||||
.extensions = loader_keys,
|
||||
.loaders = loader_values,
|
||||
},
|
||||
.node_modules_bundle_path = node_modules_bundle_path,
|
||||
.public_dir = if (args.option("--public-dir")) |public_dir| allocator.dupe(u8, public_dir) catch unreachable else null,
|
||||
.write = write,
|
||||
.platform = .speedy,
|
||||
.serve = serve,
|
||||
.inject = inject,
|
||||
.entry_points = entry_points,
|
||||
.extension_order = args.options("--extension-order"),
|
||||
.main_fields = args.options("--main-fields"),
|
||||
.only_scan_dependencies = if (args.flag("--scan")) Api.ScanDependencyMode.all else Api.ScanDependencyMode._none,
|
||||
.generate_node_module_bundle = if (args.flag("--new-jsb")) true else false,
|
||||
};
|
||||
}
|
||||
};
|
||||
pub fn resolve_jsx_runtime(str: string) !Api.JsxRuntime {
|
||||
if (strings.eql(str, "automatic")) {
|
||||
return Api.JsxRuntime.automatic;
|
||||
} else if (strings.eql(str, "fallback")) {
|
||||
return Api.JsxRuntime.classic;
|
||||
} else {
|
||||
return error.InvalidJSXRuntime;
|
||||
}
|
||||
}
|
||||
pub fn printScanResults(scan_results: bundler.ScanResult.Summary, allocator: *std.mem.Allocator) !void {
|
||||
var stdout = std.io.getStdOut();
|
||||
const print_start = std.time.nanoTimestamp();
|
||||
try std.json.stringify(scan_results.list(), .{}, stdout.writer());
|
||||
Output.printError("\nJSON printing took: {d}\n", .{std.time.nanoTimestamp() - print_start});
|
||||
}
|
||||
pub fn startTransform(allocator: *std.mem.Allocator, args: Api.TransformOptions, log: *logger.Log) anyerror!void {}
|
||||
pub fn start(allocator: *std.mem.Allocator, stdout: anytype, stderr: anytype) anyerror!void {
|
||||
const start_time = std.time.nanoTimestamp();
|
||||
var log = logger.Log.init(allocator);
|
||||
var panicker = MainPanicHandler.init(&log);
|
||||
MainPanicHandler.Singleton = &panicker;
|
||||
|
||||
// var args = try Arguments.parse(alloc.static, stdout, stderr);
|
||||
// var serve_bundler = try bundler.ServeBundler.init(allocator, &log, args);
|
||||
// var res = try serve_bundler.buildFile(&log, allocator, args.entry_points[0], std.fs.path.extension(args.entry_points[0]));
|
||||
|
||||
// var results = try bundler.Bundler.bundle(allocator, &log, args);
|
||||
// var file = results.output_files[0];
|
||||
var vm = try js.VirtualMachine.init(allocator);
|
||||
|
||||
_ = try vm.evalUtf8("test.js", "console.log(Number(10.1));"[0.. :0]);
|
||||
Output.print("Done", .{});
|
||||
}
|
||||
};
|
||||
@@ -286,6 +286,7 @@ pub const Platform = enum {
|
||||
return switch (plat orelse api.Api.Platform._none) {
|
||||
.node => .node,
|
||||
.browser => .browser,
|
||||
.speedy => .speedy,
|
||||
else => .browser,
|
||||
};
|
||||
}
|
||||
@@ -674,12 +675,25 @@ pub const BundleOptions = struct {
|
||||
}
|
||||
|
||||
if (transform.platform) |plat| {
|
||||
opts.platform = if (plat == .browser) .browser else .node;
|
||||
opts.platform = switch (plat) {
|
||||
.speedy => speedy,
|
||||
.neutral => .neutral,
|
||||
.browser => .browser,
|
||||
.node => .node,
|
||||
};
|
||||
opts.main_fields = Platform.DefaultMainFields.get(opts.platform);
|
||||
}
|
||||
|
||||
if (opts.platform == .node) {
|
||||
opts.import_path_format = .relative_nodejs;
|
||||
switch (opts.platform) {
|
||||
.node => {
|
||||
opts.import_path_format = .relative_nodejs;
|
||||
},
|
||||
.speedy => {
|
||||
// If we're doing SSR, we want all the URLs to be the same as what it would be in the browser
|
||||
// If we're not doing SSR, we want all the import paths to be absolute
|
||||
opts.import_path_format = if (opts.import_path_format == .absolute_url) .absolute_url else .absolute_path;
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
|
||||
if (transform.main_fields.len > 0) {
|
||||
|
||||
@@ -431,11 +431,10 @@ pub const CodepointIterator = struct {
|
||||
inline fn nextCodepointSlice(it: *CodepointIterator) []const u8 {
|
||||
@setRuntimeSafety(false);
|
||||
|
||||
const cp_len = utf8ByteSequenceLength(it.source.contents[it.current]);
|
||||
it.end = it.current;
|
||||
it.current += cp_len;
|
||||
const cp_len = utf8ByteSequenceLength(it.bytes[it.i]);
|
||||
it.i += cp_len;
|
||||
|
||||
return if (!(it.current > it.source.contents.len)) it.source.contents[it.current - cp_len .. it.current] else "";
|
||||
return if (!(it.i > it.bytes.len)) it.bytes[it.i - cp_len .. it.i] else "";
|
||||
}
|
||||
|
||||
pub fn nextCodepoint(it: *CodepointIterator) CodePoint {
|
||||
|
||||
@@ -139,7 +139,7 @@ pub const WTFStringMutable = struct {
|
||||
self.list.appendAssumeCapacity(char);
|
||||
}
|
||||
pub inline fn append(self: *WTFStringMutable, str: []const u8) !void {
|
||||
self.growIfNeeded(str.len);
|
||||
try self.growIfNeeded(str.len);
|
||||
|
||||
var iter = strings.CodepointIterator{ .bytes = str, .i = 0 };
|
||||
while (true) {
|
||||
@@ -148,12 +148,12 @@ pub const WTFStringMutable = struct {
|
||||
return;
|
||||
},
|
||||
0...0xFFFF => {
|
||||
try self.list.append(@intCast(u16, iter.c));
|
||||
try self.list.append(self.allocator, @intCast(u16, iter.c));
|
||||
},
|
||||
else => {
|
||||
const c = iter.c - 0x10000;
|
||||
try self.list.append(@intCast(u16, 0xD800 + ((c >> 10) & 0x3FF)));
|
||||
try self.list.append(@intCast(u16, 0xDC00 + (c & 0x3FF)));
|
||||
try self.list.append(self.allocator, @intCast(u16, 0xD800 + ((c >> 10) & 0x3FF)));
|
||||
try self.list.append(self.allocator, @intCast(u16, 0xDC00 + (c & 0x3FF)));
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -186,15 +186,15 @@ pub const WTFStringMutable = struct {
|
||||
return @intCast(i32, self.list.items.len);
|
||||
}
|
||||
|
||||
pub fn toOwnedSlice(self: *WTFStringMutable) string {
|
||||
pub fn toOwnedSlice(self: *WTFStringMutable) []u16 {
|
||||
return self.list.toOwnedSlice(self.allocator);
|
||||
}
|
||||
|
||||
pub fn toOwnedSliceLeaky(self: *WTFStringMutable) string {
|
||||
pub fn toOwnedSliceLeaky(self: *WTFStringMutable) []u16 {
|
||||
return self.list.items;
|
||||
}
|
||||
|
||||
pub fn toOwnedSliceLength(self: *WTFStringMutable, length: usize) string {
|
||||
pub fn toOwnedSliceLength(self: *WTFStringMutable, length: usize) {
|
||||
self.list.shrinkAndFree(self.allocator, length);
|
||||
return self.list.toOwnedSlice(self.allocator);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user