From 449c8b5f362835d75e5f98ccf89f99df5d7a2e88 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sun, 24 Oct 2021 06:18:42 -0700 Subject: [internal] Tweak build system to better support unit tests in Zig --- .vscode/launch.json | 33 +++++++++++-- .vscode/settings.json | 21 ++++++-- Dockerfile-zig | 52 ++++++++++++++++++++ Dockerfile.zig | 52 -------------------- Makefile | 2 +- build.zig | 14 ++++-- integration/scripts/snippets.json | 23 +++++++++ misctools/gen-unicode-table.js | 56 ++++++++++++++++++++++ packages/bun-framework-next/package.json | 2 +- packages/bun-framework-next/renderDocument.tsx | 2 +- packages/bun-framework-next/server.development.tsx | 1 - packages/bun-macro-relay/bun-macro-relay.tsx | 11 +++-- 12 files changed, 198 insertions(+), 71 deletions(-) create mode 100644 Dockerfile-zig delete mode 100644 Dockerfile.zig create mode 100644 integration/scripts/snippets.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 74ec3c759..12b0881ea 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -37,6 +37,15 @@ "cwd": "/tmp/", "console": "internalConsole" }, + { + "type": "lldb", + "request": "launch", + "name": "bun routes", + "program": "${workspaceFolder}/packages/debug-bun-cli-darwin-x64/bin/bun-debug", + "args": [], + "cwd": "${workspaceFolder}/integration/apps/routing", + "console": "internalConsole" + }, { "type": "lldb", "request": "launch", @@ -174,10 +183,28 @@ { "type": "lldb", "request": "launch", - "name": "Fixture serve", + "name": "Dazzle serve", "program": "${workspaceFolder}/packages/debug-bun-cli-darwin-x64/bin/bun-debug", - "args": [], - "cwd": "${workspaceFolder}/src/test/fixtures", + "args": ["--origin=http://localhost:5000", "--disable-hmr"], + "cwd": "/Users/jarred/Build/lattice/apps/dazzle", + "console": "internalConsole" + }, + { + "type": "lldb", + "request": "launch", + "name": "Bun", + "program": "${workspaceFolder}/packages/debug-bun-cli-darwin-x64/bin/bun-debug", + "args": ["bun", "--use=next"], + "cwd": "/Users/jarred/Build/lattice/apps/dazzle", + "console": "internalConsole" + }, + { + "type": "lldb", + "request": "launch", + "name": "unicode check", + "args": ["--disable-hmr"], + "program": "${workspaceFolder}/packages/debug-bun-cli-darwin-x64/bin/bun-debug", + "cwd": "/Users/jarred/Build/app994", "console": "internalConsole" }, diff --git a/.vscode/settings.json b/.vscode/settings.json index 117340f28..c0970369d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,15 +3,16 @@ "search.quickOpen.includeSymbols": false, "search.seedWithNearestWord": true, "search.smartCase": true, - "search.exclude": { - "src/javascript/jsc/WebKit/**/*": true - }, + "search.exclude": {}, "search.followSymlinks": false, "search.useIgnoreFiles": true, "zig.buildOnSave": false, "[zig]": { "editor.defaultFormatter": "tiehuis.zig" }, + "lldb.verboseLogging": true, + "zig.beforeDebugCmd": "make build-unit ${file} ${filter} ${bin}", + "zig.testCmd": "make test ${file} ${filter} ${bin}", "files.exclude": { "**/.git": true, "**/.svn": true, @@ -19,8 +20,20 @@ "**/CVS": true, "**/.DS_Store": true, "**/Thumbs.db": true, + "**/*.xcworkspacedata": true, + "**/*.xcscheme": true, + "**/*.pem": true, + "**/*.xcodeproj": true, + "integration/snapshots": true, - "integration/snapshots-no-hmr": true + "integration/snapshots-no-hmr": true, + "src/javascript/jsc/WebKit": true, + "src/deps/libarchive": true, + "src/deps/mimalloc": true, + "src/deps/s2n-tls": true, + "src/deps/openssl": true, + "src/deps/zlib": true, + "integration/snippets/package-json-exports/_node_modules_copy": true }, "C_Cpp.files.exclude": { "**/.vscode": true, diff --git a/Dockerfile-zig b/Dockerfile-zig new file mode 100644 index 000000000..57598be49 --- /dev/null +++ b/Dockerfile-zig @@ -0,0 +1,52 @@ +FROM ubuntu:latest + +RUN apt-get update && apt-get install --no-install-recommends -y wget gnupg2 curl lsb-release wget software-properties-common +RUN curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - + +RUN wget https://apt.llvm.org/llvm.sh --no-check-certificate +RUN chmod +x llvm.sh +RUN ./llvm.sh 12 + +RUN apt-get update && apt-get install --no-install-recommends -y \ + ca-certificates \ + curl \ + gnupg2 \ + software-properties-common \ + cmake \ + build-essential \ + git \ + libssl-dev \ + ruby \ + liblld-12-dev \ + libclang-12-dev \ + nodejs \ + gcc \ + g++ \ + npm \ + clang-12 \ + clang-format-12 \ + libc++-12-dev \ + libc++abi-12-dev \ + lld-12 \ + libicu-dev + +RUN update-alternatives --install /usr/bin/ld ld /usr/bin/lld-12 90 && \ + update-alternatives --install /usr/bin/cc cc /usr/bin/clang-12 90 && \ + update-alternatives --install /usr/bin/cpp cpp /usr/bin/clang++-12 90 && \ + update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-12 90 + + +ENV CC=clang-12 +ENV CXX=clang++-12 + +# Compile zig +RUN mkdir -p /home/ubuntu/zig; cd /home/ubuntu; git clone https://github.com/jarred-sumner/zig.git; cd /home/ubuntu/zig && git checkout jarred/zig-sloppy-with-small-structs && cmake . -DCMAKE_BUILD_TYPE=Release && make -j$(nproc) + +ENV PATH="/home/ubuntu/zig:$PATH" + +RUN npm install -g esbuild + + + + + diff --git a/Dockerfile.zig b/Dockerfile.zig deleted file mode 100644 index 57598be49..000000000 --- a/Dockerfile.zig +++ /dev/null @@ -1,52 +0,0 @@ -FROM ubuntu:latest - -RUN apt-get update && apt-get install --no-install-recommends -y wget gnupg2 curl lsb-release wget software-properties-common -RUN curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - - -RUN wget https://apt.llvm.org/llvm.sh --no-check-certificate -RUN chmod +x llvm.sh -RUN ./llvm.sh 12 - -RUN apt-get update && apt-get install --no-install-recommends -y \ - ca-certificates \ - curl \ - gnupg2 \ - software-properties-common \ - cmake \ - build-essential \ - git \ - libssl-dev \ - ruby \ - liblld-12-dev \ - libclang-12-dev \ - nodejs \ - gcc \ - g++ \ - npm \ - clang-12 \ - clang-format-12 \ - libc++-12-dev \ - libc++abi-12-dev \ - lld-12 \ - libicu-dev - -RUN update-alternatives --install /usr/bin/ld ld /usr/bin/lld-12 90 && \ - update-alternatives --install /usr/bin/cc cc /usr/bin/clang-12 90 && \ - update-alternatives --install /usr/bin/cpp cpp /usr/bin/clang++-12 90 && \ - update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-12 90 - - -ENV CC=clang-12 -ENV CXX=clang++-12 - -# Compile zig -RUN mkdir -p /home/ubuntu/zig; cd /home/ubuntu; git clone https://github.com/jarred-sumner/zig.git; cd /home/ubuntu/zig && git checkout jarred/zig-sloppy-with-small-structs && cmake . -DCMAKE_BUILD_TYPE=Release && make -j$(nproc) - -ENV PATH="/home/ubuntu/zig:$PATH" - -RUN npm install -g esbuild - - - - - diff --git a/Makefile b/Makefile index 382084245..9c4f4254d 100644 --- a/Makefile +++ b/Makefile @@ -703,7 +703,7 @@ build-unit: -femit-bin=zig-out/bin/$(testname) \ -fcompiler-rt \ -lc -lc++ \ - --cache-dir /tmp/zig-cache-bun-$(testname)-$(basename $(firstword $(testfilter))) \ + --cache-dir /tmp/zig-cache-bun-$(testname)-$(basename $(lastword $(testfilter))) \ -fallow-shlib-undefined \ -L$(LIBCRYPTO_PREFIX_DIR)/lib \ -lcrypto -lssl \ diff --git a/build.zig b/build.zig index e116bd3d9..b03781330 100644 --- a/build.zig +++ b/build.zig @@ -128,13 +128,19 @@ pub fn build(b: *std.build.Builder) !void { exe.setOutputDir(output_dir); var cwd_dir = std.fs.cwd(); - - const runtime_hash = std.hash.Wyhash.hash(0, @embedFile("./src/runtime.out.js")); + var runtime_out_file = try std.fs.cwd().openFile("src/runtime.out.js", .{ .read = true }); + const runtime_hash = std.hash.Wyhash.hash( + 0, + try runtime_out_file.readToEndAlloc(b.allocator, try runtime_out_file.getEndPos()), + ); const runtime_version_file = std.fs.cwd().openFile("src/runtime.version", .{ .write = true }) catch unreachable; runtime_version_file.writer().print("{x}", .{runtime_hash}) catch unreachable; defer runtime_version_file.close(); - - const fallback_hash = std.hash.Wyhash.hash(0, @embedFile("./src/fallback.out.js")); + var fallback_out_file = try std.fs.cwd().openFile("src/fallback.out.js", .{ .read = true }); + const fallback_hash = std.hash.Wyhash.hash( + 0, + try fallback_out_file.readToEndAlloc(b.allocator, try fallback_out_file.getEndPos()), + ); const fallback_version_file = std.fs.cwd().openFile("src/fallback.version", .{ .write = true }) catch unreachable; fallback_version_file.writer().print("{x}", .{fallback_hash}) catch unreachable; defer fallback_version_file.close(); diff --git a/integration/scripts/snippets.json b/integration/scripts/snippets.json new file mode 100644 index 000000000..a2a044e6d --- /dev/null +++ b/integration/scripts/snippets.json @@ -0,0 +1,23 @@ +[ + "/cjs-transform-shouldnt-have-static-imports-in-cjs-function.js", + "/bundled-entry-point.js", + "/export.js", + "/type-only-imports.ts", + "/global-is-remapped-to-globalThis.js", + "/multiple-imports.js", + "/ts-fallback-rewrite-works.js", + "/tsx-fallback-rewrite-works.js", + "/lodash-regexp.js", + "/unicode-identifiers.js", + "/string-escapes.js", + "/package-json-exports/index.js", + "/array-args-with-default-values.js", + "/forbid-in-is-correct.js", + "/code-simplification-neql-define.js", + "/spread_with_key.tsx", + "/styledcomponents-output.js", + "/void-shouldnt-delete-call-expressions.js", + "/custom-emotion-jsx/file.jsx", + "/react-context-value-func.tsx", + "/latin1-chars-in-regexp.js" +] diff --git a/misctools/gen-unicode-table.js b/misctools/gen-unicode-table.js index 6ee02f5e8..cd46ac31c 100644 --- a/misctools/gen-unicode-table.js +++ b/misctools/gen-unicode-table.js @@ -117,6 +117,52 @@ function generateRangeTable(codePoints) { return lines.join("\n"); } +function generateBigSwitchStatement(codePoints) { + let lines = []; + let index = 0; + let latinOffset = 0; + + while (latinOffset < codePoints.length && codePoints[latinOffset] <= 0xff) { + latinOffset++; + } + + lines.push(`return switch(codepoint) {`); + + // 16-bit code points + while (index < codePoints.length && codePoints[index] < 0x1000) { + let start = codePoints[index]; + index++; + while ( + index < codePoints.length && + codePoints[index] < 0x1000 && + codePoints[index] === codePoints[index - 1] + 1 + ) { + index++; + } + let end = codePoints[index - 1]; + lines.push(`0x${start.toString(16)}...0x${end.toString(16)},`); + } + + // 32-bit code points + while (index < codePoints.length) { + let start = codePoints[index]; + index++; + while ( + index < codePoints.length && + codePoints[index] === codePoints[index - 1] + 1 + ) { + index++; + } + let end = codePoints[index - 1]; + lines.push(` 0x${start.toString(16)}...0x${end.toString(16)},`); + } + + lines.push(` => true, + else => false +};`); + return lines.join("\n"); +} + fs.writeFileSync( path.join(__dirname, "..", "src", "js_lexer", "unicode.zig"), `// This file was automatically generated by ${path.basename( @@ -134,5 +180,15 @@ pub const id_continue = ${generateRangeTable(idContinueES5OrESNext)} pub const printable_id_start = ${generateRangeTable(idStartESNext)} pub const printable_id_continue = ${generateRangeTable(idContinueESNext)} + +pub fn isIdentifierStart(comptime Codepoint: type, codepoint: Codepoint) bool{ + ${generateBigSwitchStatement(idStartES5OrESNext)} +} + +pub fn isIdentifierContinue(comptime Codepoint: type, codepoint: Codepoint) bool{ + ${generateBigSwitchStatement(idContinueES5OrESNext)} +} + + ` ); diff --git a/packages/bun-framework-next/package.json b/packages/bun-framework-next/package.json index e9222b390..fe99ede42 100644 --- a/packages/bun-framework-next/package.json +++ b/packages/bun-framework-next/package.json @@ -32,7 +32,7 @@ ".env": "NEXT_PUBLIC_", "defaults": { "process.env.__NEXT_TRAILING_SLASH": "false", - "process.env.NODE_ENV": "\"development\"", + "process.env.NODE_ENV": "'development'", "process.env.__NEXT_ROUTER_BASEPATH": "''", "process.env.__NEXT_SCROLL_RESTORATION": "false", "process.env.__NEXT_I18N_SUPPORT": "false", diff --git a/packages/bun-framework-next/renderDocument.tsx b/packages/bun-framework-next/renderDocument.tsx index aa6c85fbf..463395863 100644 --- a/packages/bun-framework-next/renderDocument.tsx +++ b/packages/bun-framework-next/renderDocument.tsx @@ -391,7 +391,7 @@ export async function render({ var query = Object.assign({}, route.query); // These are reversed in our Router versus Next.js...mostly due to personal preference. - const pathname = "/" + route.name; + const pathname = route.name; var asPath = route.pathname; const pages = {}; diff --git a/packages/bun-framework-next/server.development.tsx b/packages/bun-framework-next/server.development.tsx index 29c40c6be..d2f8a5d1e 100644 --- a/packages/bun-framework-next/server.development.tsx +++ b/packages/bun-framework-next/server.development.tsx @@ -52,7 +52,6 @@ addEventListener("fetch", async (event: FetchEvent) => { appRoute = null; } const appStylesheets = (Bun.getImportedStyles() as string[]).slice(); - event.respondWith( render({ route, diff --git a/packages/bun-macro-relay/bun-macro-relay.tsx b/packages/bun-macro-relay/bun-macro-relay.tsx index 6c12c1f45..0dc42eace 100644 --- a/packages/bun-macro-relay/bun-macro-relay.tsx +++ b/packages/bun-macro-relay/bun-macro-relay.tsx @@ -10,12 +10,15 @@ import { parse, print } from "graphql"; // 6. Replace the TemplateLiteral with the default identifier from the injected import let artifactDirectory: string = `__generated__`; -if (process.env.RELAY_ARTIFACT_DIRECTORY) { - artifactDirectory = process.env.RELAY_ARTIFACT_DIRECTORY; +const { RELAY_ARTIFACT_DIRECTORY, BUN_MACRO_RELAY_ARTIFACT_DIRECTORY } = + Bun.env; + +if (RELAY_ARTIFACT_DIRECTORY) { + artifactDirectory = RELAY_ARTIFACT_DIRECTORY; } -if (process.env.BUN_MACRO_RELAY_ARTIFACT_DIRECTORY) { - artifactDirectory = process.env.BUN_MACRO_RELAY_ARTIFACT_DIRECTORY; +if (BUN_MACRO_RELAY_ARTIFACT_DIRECTORY) { + artifactDirectory = BUN_MACRO_RELAY_ARTIFACT_DIRECTORY; } // TODO: platform-independent path cleaning -- cgit v1.2.3