Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 133 additions & 27 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,143 @@
on: [push, pull_request]
name: Test
name: CI

on:
push:
branches: [master]
pull_request:

permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
strategy:
matrix:
go-version: [1.25.x, 1.x]
os: [ubuntu-latest]
arch: ["", "386"]
fail-fast: false
runs-on: ${{ matrix.os }}
lint:
name: Lint and module policy
runs-on: ubuntu-latest
env:
CGO_ENABLED: "1"
steps:
- name: Install Go
uses: actions/setup-go@v6
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}
check-latest: true
- name: Checkout code
uses: actions/checkout@v4
go-version: stable
- name: Check formatting
run: diff -u <(echo -n) <(gofmt -d .)
if: ${{ matrix.go-version == '1.x' }}
- name: Run go vet
shell: bash
run: |
out="$(gofmt -l .)"
if [ -n "$out" ]; then
echo "The following files are not gofmt-ed:"
echo "$out"
exit 1
fi
- name: Verify module graph
shell: bash
run: |
test "$(find . -name go.mod -print | wc -l | tr -d ' ')" = "1"
test "$(find . -name go.sum -print | wc -l | tr -d ' ')" = "1"
if go list -m all | grep -q 'github.com/dlclark/regexp2'; then
echo 'github.com/dlclark/regexp2 must not enter the module graph'
exit 1
fi
- name: Vet
run: go vet ./...
- name: Run staticcheck
- name: Staticcheck
uses: dominikh/staticcheck-action@v1.4.1
with:
version: "2026.2"
install-go: false
cache-key: ${{ matrix.go-version }}
if: ${{ matrix.go-version == '1.x' }}
- name: Checkout tc39 tests

test:
name: Test ${{ matrix.os }} / Go ${{ matrix.go }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go: ["1.22.12", stable]
env:
CGO_ENABLED: "1"
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go }}
- name: Test
run: go test -count=1 ./...

linux-386:
name: Test linux/386
runs-on: ubuntu-latest
env:
CGO_ENABLED: "1"
GOARCH: "386"
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: "1.22.12"
- name: Install 32-bit C toolchain
run: sudo apt-get update && sudo apt-get install -y gcc-multilib
- name: Test
run: go test -count=1 ./...

race:
name: Race
runs-on: ubuntu-latest
env:
CGO_ENABLED: "1"
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: stable
- name: Test with race detector
run: go test -race -count=1 ./...

no-cgo:
name: No-cgo compile
runs-on: ubuntu-latest
env:
CGO_ENABLED: "0"
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: "1.22.12"
- name: Compile packages and tests
run: go test -run '^$' ./...

nodejs-types:
name: NodeJS type definitions
runs-on: ubuntu-latest
defaults:
run:
working-directory: nodejs
steps:
- uses: actions/checkout@v5
- uses: actions/setup-node@v6
with:
node-version: "22"
cache: npm
cache-dependency-path: nodejs/package-lock.json
- name: Test workspaces
run: |
npm ci
npm test --workspaces

tc39:
name: TC39
runs-on: ubuntu-latest
env:
CGO_ENABLED: "1"
steps:
- uses: actions/checkout@v5
- uses: actions/setup-go@v6
with:
go-version: stable
- name: Checkout TC39 tests
run: ./.tc39_test262_checkout.sh
- name: Run tests
env:
GOARCH: ${{ matrix.arch }}
run: go test -vet=off ./...
- name: Run test suite
run: go test -count=1 ./...
45 changes: 45 additions & 0 deletions BENCHMARK.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Production bundle benchmark

This benchmark records cold parse, compile, execute, and load costs for
deterministic minified JavaScript generated by `makeProductionBundle`. Every
generated module contains a lookbehind expression, so the compile benchmark
also exercises the PCRE2 fallback rather than measuring parser-only code.

Environment: Apple M1 Max, darwin/arm64, Go 1.22.12, cgo enabled,
`go-pcre2-lite v0.1.6`. Each reported result ran for three iterations.
The compiler reuses identical regular-expression literals within one Program
by `(pattern, flags)`, while each runtime RegExp object keeps independent
mutable state such as `lastIndex`:

```sh
go test -run '^$' \
-bench 'Benchmark(Parse|Compile|Execute|Load)ProductionBundle$' \
-benchtime=3x -count=1 -benchmem
```

| Operation | 5 MiB | 20 MiB | Time ratio | Allocated bytes (5 / 20 MiB) |
|---|---:|---:|---:|---:|
| Parse | 133.1 ms | 529.3 ms | 3.98x | 73.8 MB / 295.5 MB |
| Compile | 249.0 ms | 968.7 ms | 3.89x | 204.0 MB / 816.1 MB |
| Execute compiled program | 19.96 ms | 94.39 ms | 4.73x | 25.6 MB / 103.8 MB |
| Cold load (compile + execute) | 265.9 ms | 1.086 s | 4.08x | 229.6 MB / 919.9 MB |

A 4x source-size increase produces approximately 4.0x parse, compile, and
cold-load time in this workload. The observed complexity is therefore linear,
`Theta(n)`, with cache and garbage-collector effects accounting for the modest
ratio variation. This is an empirical result for this source shape, not a
worst-case guarantee for every JavaScript or regular expression.

The external-bundle fixture also compiled Yakit's 28.09 MiB minified
`main.94ece1a4.js` successfully. Its cold compile result was 1.752 s,
1.364 GB allocated, 23,136,763 allocations, and 16.81 MB/s:

```sh
GOJA_PRODUCTION_BUNDLE=/path/to/main.min.js \
go test -run '^$' -bench '^BenchmarkCompileExternalProductionBundle$' \
-benchtime=3x -count=1 -benchmem
```

`B/op` is cumulative allocation traffic, not peak resident memory. These tests
consume minified JavaScript source; a gzip or Brotli transport artifact must be
decompressed before it can be parsed.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Changelog

## v0.1.0

First supported Yaklang hard-fork release.

- Freeze Goja at upstream commit `8f1c0696a37b221d3b14dc4c2e826cc22a6b723d`.
- Integrate goja_nodejs commit `1f56ff5bcf1444844c756986ed5fc03069aec1ff`
into the root module under `nodejs/`.
- Change the module path to `github.com/yaklang/goja` and target Go 1.22.12.
- Remove `github.com/dlclark/regexp2` completely from source and module graphs.
- Use Go RE2 as the fast path and `go-pcre2-lite` v0.1.6 with embedded PCRE2
10.47 as the only bounded backtracking fallback.
- Restore ECMAScript lookbehind capture and backreference behavior through the
PCRE2 callout compatibility layer without skipping Test262 cases.
- Preserve JavaScript UTF-16 indexing and isolated-surrogate behavior.
- Cache identical regexp literals within compiled Programs while preserving
independent RegExp object state.
- Add Linux, macOS, Windows, 386, race, no-cgo compilation, static analysis,
Test262, and nodejs type-definition CI coverage.
- Document the Go 1.22 weak-collection compatibility tradeoff and production
bundle benchmark results.
89 changes: 89 additions & 0 deletions FORK.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Yaklang Goja fork

This repository is a hard fork maintained for Yaklang. It combines Goja and
goja_nodejs sources in one module, targets Yaklang's Go toolchain, and
uses a bounded PCRE2 fallback without depending on `github.com/dlclark/regexp2`.

## Maintenance and licensing

The baselines below are intentionally frozen. This repository does not merge or
rebase upstream branches after the fork. Security fixes or useful language
features may be reviewed and ported as independent Yaklang changes, with their
source commit recorded, but they are not automatic upstream synchronisations.

The upstream Goja and goja_nodejs code remains under its original MIT license.
Yaklang's modifications are also released under the MIT license. See `LICENSE`
and `NOTICE` for attribution and redistribution terms.

## Upstream baselines

- Goja: `dop251/goja` commit `8f1c0696a37b221d3b14dc4c2e826cc22a6b723d`
(2026-08-26).
- goja_nodejs: `dop251/goja_nodejs` commit
`1f56ff5bcf1444844c756986ed5fc03069aec1ff` (2026-02-12), integrated as a
squashed git subtree under `nodejs/`.

These provenance points must remain in this document for the lifetime of the
fork. Any selectively ported upstream change must be listed in the release
notes and must rerun the full root-module test matrix.

## Module and toolchain policy

- Module path: `github.com/yaklang/goja`.
- Minimum Go version: 1.22.12, matching Yaklang.
- NodeJS packages share the root `go.mod`; `nodejs/` intentionally has no
nested module files.
- The production module graph must not contain `github.com/dlclark/regexp2`.

## Regular-expression backend

The standard library `regexp` engine remains the fast path for compatible
patterns. `github.com/VillanCh/go-pcre2-lite` provides the only backtracking
backend for lookaround, backreferences, non-zero search starts, and other
ECMAScript cases that RE2 cannot handle. PCRE2 10.47 is embedded in that module,
JIT is disabled, and match/depth limits bound catastrophic backtracking.

The PCRE2 binding uses its 8-bit UTF mode, while JavaScript strings are UTF-16.
To preserve lone-surrogate behavior without a second regex engine, this fork
maps isolated surrogate code units one-to-one into U+10F800..U+10FFFF before
matching and rewrites corresponding pattern escapes. Indexes remain UTF-16 code
unit indexes. The reserved mapping range is the one intentional edge-case
tradeoff: a source string containing both an isolated surrogate and an actual
scalar in that final supplementary private-use range cannot be represented
injectively by the 8-bit backend.

`CGO_ENABLED=1` is required when a script needs the PCRE2 fallback. A no-cgo
build is kept compilable for dependency analysis and tooling, but fallback-only
patterns return a compile error at runtime.

### Regular-expression changes from upstream

- Removed every source and module dependency on `github.com/dlclark/regexp2`.
- Kept Go's RE2 engine as the zero-cgo fast path for compatible expressions.
- Added `go-pcre2-lite` v0.1.6 with embedded PCRE2 10.47 as the only
backtracking engine; no system PCRE2 installation is used and JIT is disabled.
- Added PCRE2 callout-based ECMAScript lookbehind handling for right-to-left
captures, variable-length alternatives, nested assertions, and
internal/external/forward/mutual backreferences.
- Preserved UTF-16 indexes and isolated surrogate behavior over PCRE2's 8-bit
UTF interface using the mapping described above.
- Preserved PCRE2 match and depth limits, so the fallback remains bounded for
adversarial backtracking inputs.
- Cached identical regexp literals per compiled Program by `(pattern, flags)`;
each JavaScript RegExp object still has independent mutable state such as
`lastIndex`.

The compatibility implementation is primarily in `regexp.go`,
`regexp_pcre2.go`, `regexp_nocgo.go`, `builtin_regexp.go`, and the compiler's
regexp-literal cache. The lower-level ECMAScript lookbehind bridge is maintained
in `github.com/VillanCh/go-pcre2-lite/regexp2/ecma_lookbehind.go`.

## Go 1.22 weak collections

The latest upstream implementation uses Go 1.25's `weak` package and
`runtime.AddCleanup`. Go 1.22 cannot reproduce those garbage-collector hooks.
This fork stores WeakMap and WeakSet entries on their key objects under a
per-collection ID. Observable JavaScript operations remain compatible, but a
value can stay reachable while its key stays reachable after the collection
itself has been collected. Revisit this compatibility layer when Yaklang raises
its minimum Go version to 1.25 or newer.
27 changes: 27 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Yaklang Goja hard fork
======================

This repository contains a hard fork of:

goja
Copyright (c) 2016 Dmitry Panov
Copyright (c) 2012 Robert Krimen
Upstream: https://github.com/dop251/goja

and incorporates code from:

goja_nodejs
Copyright (c) the goja_nodejs contributors
Upstream: https://github.com/dop251/goja_nodejs

Yaklang-specific modifications and integration work are Copyright (c) the
Yaklang contributors.

The original code and Yaklang modifications are distributed under the MIT
License in LICENSE. The integrated nodejs subtree also retains its upstream
license at nodejs/LICENSE.

The frozen source baselines, maintenance policy, dependency policy, and a
technical description of the Yaklang changes are recorded in FORK.md. This
fork is independently maintained by Yaklang and is not an upstream-tracking
mirror.
Loading
Loading