Skip to content

Commit 832f131

Browse files
committed
Convert to official Fabrice Bellard's quickjs.
1 parent c9e3218 commit 832f131

5 files changed

Lines changed: 123 additions & 47 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/.zig-cache/
22
/zig-cache/
33
/zig-out/
4+
/zig-pkg/
45
gen

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Rob Blanckaert
3+
Copyright (c) 2026 AllYourCodebase contributors
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
# QuickJS-NG
1+
# QuickJS
22

3-
This is [QuickJS-NG](https://github.com/quickjs-ng/quickjs) packaged for [Zig](https://ziglang.org).
3+
This is [QuickJS](https://github.com/bellard/quickjs) packaged for [Zig](https://ziglang.org).
4+
5+
# Credits
6+
7+
Converted from [zig's quickjs-ng](https://github.com/allyourcodebase/quickjs-ng)

build.zig

Lines changed: 109 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,111 @@
11
const std = @import("std");
2+
const Io = std.Io;
23

3-
fn addDefines(c: *std.Build.Step.Compile, b: *std.Build) void {
4+
const PortableAddCSourceFilesOptions = if (@hasDecl(std.Build.Module, "AddCSourceFilesOptions"))
5+
std.Build.Module.AddCSourceFilesOptions else std.Build.Step.Compile.AddCSourceFilesOptions;
6+
7+
pub fn portableAddCSourceFiles(c: *std.Build.Step.Compile, options: PortableAddCSourceFilesOptions) void {
8+
if (@hasDecl(std.Build.Step.Compile, "addCSourceFiles")) {
9+
c.addCSourceFiles(options);
10+
} else {
11+
c.root_module.addCSourceFiles(options);
12+
}
13+
}
14+
15+
pub fn portableLinkSystemLibrary(c: *std.Build.Step.Compile, name: []const u8) void {
16+
if (@hasDecl(std.Build.Step.Compile, "linkSystemLibrary")) {
17+
c.linkSystemLibrary(name);
18+
} else {
19+
c.root_module.linkSystemLibrary(name, .{});
20+
}
21+
}
22+
23+
pub fn portableLinkLibC(c: *std.Build.Step.Compile) void {
24+
if (@hasDecl(std.Build.Step.Compile, "linkLibC")) {
25+
c.linkLibC();
26+
} else {
27+
c.root_module.link_libc = true;
28+
}
29+
}
30+
31+
pub fn portableLinkLibrary(c: *std.Build.Step.Compile, library: *std.Build.Step.Compile) void {
32+
if (@hasDecl(std.Build.Step.Compile, "linkLibrary")) {
33+
c.linkLibrary(library);
34+
} else {
35+
c.root_module.linkLibrary(library);
36+
}
37+
}
38+
39+
fn addDefines(c: *std.Build.Step.Compile, b: *std.Build, version: []const u8) void {
440
c.root_module.addCMacro("CONFIG_BIGNUM", "1");
541
c.root_module.addCMacro("_GNU_SOURCE", "1");
42+
var buf: [256]u8 = undefined;
43+
const version_str = std.fmt.bufPrint(&buf, "\"{s}\"", .{ version })
44+
catch @panic("could not format version");
45+
c.root_module.addCMacro("CONFIG_VERSION", version_str);
646
_ = b;
747
}
848

949
fn addStdLib(c: *std.Build.Step.Compile, cflags: []const []const u8, root: *std.Build.Dependency) void {
1050
if (c.rootModuleTarget().os.tag == .wasi) {
1151
c.root_module.addCMacro("_WASI_EMULATED_PROCESS_CLOCKS", "1");
1252
c.root_module.addCMacro("_WASI_EMULATED_SIGNAL", "1");
13-
c.linkSystemLibrary("wasi-emulated-process-clocks");
14-
c.linkSystemLibrary("wasi-emulated-signal");
53+
portableLinkSystemLibrary(c, "wasi-emulated-process-clocks");
54+
portableLinkSystemLibrary(c, "wasi-emulated-signal");
1555
}
16-
c.addCSourceFiles(.{ .files = &.{"quickjs-libc.c"}, .flags = cflags, .root = root.path(".") });
56+
portableAddCSourceFiles(c, .{ .files = &.{"quickjs-libc.c"}, .flags = cflags, .root = root.path(".") });
57+
}
58+
59+
var buffer: [256]u8 = undefined;
60+
61+
pub fn getVersion(b: *std.Build, csrc: *std.Build.Dependency) []const u8 {
62+
const version_path = csrc.path("VERSION").getPath(b);
63+
var file = std.fs.cwd().openFile(version_path, .{})
64+
catch @panic("fail to read VERSION file");
65+
defer file.close();
66+
67+
const reader = file.reader();
68+
69+
const first_line = reader.readUntilDelimiterOrEofAlloc(
70+
b.allocator,
71+
'\n',
72+
128,
73+
) catch @panic("fail to read VERSION file");
74+
75+
if (first_line) |_| {} else {
76+
@panic("fail to read VERSION file");
77+
}
78+
79+
return first_line.?;
80+
}
81+
82+
pub fn getVersionIo(b: *std.Build, csrc: *std.Build.Dependency) []const u8 {
83+
var threaded: std.Io.Threaded = .init(b.allocator, .{});
84+
defer threaded.deinit();
85+
const io = threaded.io();
86+
87+
const version_path = csrc.path("VERSION").getPath(b);
88+
var file = std.Io.Dir.cwd().openFile(io, version_path, .{})
89+
catch @panic("fail to read VERSION file");
90+
defer file.close(io);
91+
92+
var reader = file.reader(io, &buffer);
93+
94+
const first_line = reader.interface.takeDelimiterExclusive('\n')
95+
catch @panic("fail to read VERSION file");
96+
97+
return first_line;
1798
}
1899

19100
pub fn build(b: *std.Build) void {
20101
const target = b.standardTargetOptions(.{});
21102
const optimize = b.standardOptimizeOption(.{});
22103
const include_stdlib = b.option(bool, "stdlib", "include stdlib in library") orelse true;
23104

24-
const csrc = b.dependency("quickjs-ng", .{});
105+
const csrc = b.dependency("quickjs", .{});
106+
107+
const version = if (@hasDecl(std, "Io")) getVersionIo(b, csrc) else getVersion(b, csrc);
108+
defer b.allocator.free(version);
25109

26110
const cflags = &.{
27111
"-Wno-implicit-fallthrough",
@@ -40,7 +124,7 @@ pub fn build(b: *std.Build) void {
40124
"libregexp.c",
41125
"libunicode.c",
42126
"cutils.c",
43-
"xsum.c",
127+
"dtoa.c",
44128
};
45129

46130
const libquickjs = b.addLibrary(.{
@@ -51,16 +135,16 @@ pub fn build(b: *std.Build) void {
51135
.optimize = optimize,
52136
}),
53137
});
54-
libquickjs.addCSourceFiles(.{
138+
portableAddCSourceFiles(libquickjs, .{
55139
.files = libquickjs_source,
56140
.flags = cflags,
57141
.root = csrc.path("."),
58142
});
59-
addDefines(libquickjs, b);
143+
addDefines(libquickjs, b, version);
60144
if (include_stdlib) {
61145
addStdLib(libquickjs, cflags, csrc);
62146
}
63-
libquickjs.linkLibC();
147+
portableLinkLibC(libquickjs);
64148
if (target.result.os.tag == .windows) {
65149
libquickjs.stack_size = 8388608;
66150
}
@@ -73,13 +157,13 @@ pub fn build(b: *std.Build) void {
73157
.optimize = optimize,
74158
}),
75159
});
76-
qjsc.addCSourceFiles(.{
160+
portableAddCSourceFiles(qjsc, .{
77161
.files = &.{"qjsc.c"},
78162
.flags = cflags,
79163
.root = csrc.path("."),
80164
});
81-
qjsc.linkLibrary(libquickjs);
82-
addDefines(qjsc, b);
165+
portableLinkLibrary(qjsc, libquickjs);
166+
addDefines(qjsc, b, version);
83167
if (!include_stdlib) {
84168
addStdLib(qjsc, cflags, csrc);
85169
}
@@ -97,71 +181,58 @@ pub fn build(b: *std.Build) void {
97181
qjsc_host.stack_size = 8388608;
98182
}
99183

100-
qjsc_host.addCSourceFiles(.{
184+
portableAddCSourceFiles(qjsc_host, .{
101185
.files = &.{"qjsc.c"},
102186
.flags = cflags,
103187
.root = csrc.path("."),
104188
});
105-
qjsc_host.addCSourceFiles(.{
189+
portableAddCSourceFiles(qjsc_host, .{
106190
.files = libquickjs_source,
107191
.flags = cflags,
108192
.root = csrc.path("."),
109193
});
110194
addStdLib(qjsc_host, cflags, csrc);
111-
addDefines(qjsc_host, b);
112-
qjsc_host.linkLibC();
195+
addDefines(qjsc_host, b, version);
196+
portableLinkLibC(qjsc_host);
113197

114198
const header = b.addTranslateC(.{
115-
.root_source_file = csrc.path("quickjs.h"),
199+
.root_source_file = csrc.path("quickjs-libc.h"),
116200
.target = target,
117201
.optimize = optimize,
118202
});
119-
_ = b.addModule("quickjs-ng", .{ .root_source_file = header.getOutput() });
203+
_ = b.addModule("quickjs", .{ .root_source_file = header.getOutput() });
120204

121205
const gen_repl = b.addRunArtifact(qjsc_host);
122-
gen_repl.addArg("-N");
123-
gen_repl.addArg("qjsc_repl");
206+
gen_repl.addArg("-s");
207+
gen_repl.addArg("-c");
124208
gen_repl.addArg("-o");
125209
const gen_repl_out = gen_repl.addOutputFileArg("repl.c");
126210
gen_repl.addArg("-m");
127211
gen_repl.addFileArg(csrc.path("repl.js"));
128212

129-
const gen_standalone = b.addRunArtifact(qjsc_host);
130-
gen_standalone.addArg("-N");
131-
gen_standalone.addArg("qjsc_standalone");
132-
gen_standalone.addArg("-o");
133-
const gen_standalone_out = gen_standalone.addOutputFileArg("standalone.c");
134-
gen_standalone.addArg("-m");
135-
gen_standalone.addFileArg(csrc.path("standalone.js"));
136-
137213
const qjs = b.addExecutable(.{
138214
.name = "qjs",
139215
.root_module = b.createModule(.{
140216
.target = target,
141217
.optimize = optimize,
142218
}),
143219
});
144-
qjs.addCSourceFiles(.{
220+
qjs.root_module.addCMacro("CONFIG_VERSION", "1.0.0");
221+
portableAddCSourceFiles(qjs, .{
145222
.files = &.{"qjs.c"},
146223
.flags = cflags,
147224
.root = csrc.path("."),
148225
});
149-
qjs.addCSourceFiles(.{
226+
portableAddCSourceFiles(qjs, .{
150227
.files = &.{"repl.c"},
151228
.root = gen_repl_out.dirname(),
152229
.flags = cflags,
153230
});
154-
qjs.addCSourceFiles(.{
155-
.files = &.{"standalone.c"},
156-
.root = gen_standalone_out.dirname(),
157-
.flags = cflags,
158-
});
159231
if (!include_stdlib) {
160232
addStdLib(qjs, cflags, csrc);
161233
}
162-
qjs.linkLibrary(libquickjs);
163-
addDefines(qjs, b);
234+
portableLinkLibrary(qjs, libquickjs);
235+
addDefines(qjs, b, version);
164236
qjs.step.dependOn(&gen_repl.step);
165-
qjs.step.dependOn(&gen_standalone.step);
166237
b.installArtifact(qjs);
167238
}

build.zig.zon

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
.{
2-
.name = .quickjs_ng,
3-
.fingerprint = 0xb2bca9faa72a0432,
4-
.version = "0.10.1",
2+
.name = .quickjs,
3+
.fingerprint = 0x23b15228d5af0966,
4+
.version = "1.0.0",
55
.minimum_zig_version = "0.14.0",
66
.dependencies = .{
7-
.@"quickjs-ng" = .{
8-
.url = "git+https://github.com/quickjs-ng/quickjs/?ref=v0.10.1#5299e09100b97a5dd0ea0e73fa5caa4aa0b2d97c",
9-
.hash = "N-V-__8AAJ6ZOwAqRvY64qRhZ9KrT8YuUvtVQxBECJxJwPyd",
7+
.@"quickjs" = .{
8+
.url = "https://github.com/bellard/quickjs/archive/f1139494d18a2053630c5ed3384a42bb70db3c53.tar.gz",
9+
.hash = "N-V-__8AAFP3MACLqx3IPYUe7MVJki-nUn2Jtimo4Tx6u2s_",
1010
},
1111
},
1212
.paths = .{

0 commit comments

Comments
 (0)