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
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# C/C++ style for the code we own (pkg/html/shim): tabs (width 4, as the Odin code),
# C/C++ style for the code we own (pkg/jsc, pkg/html/shim): tabs (width 4, as the Odin code),
# attached braces, 120 columns, `T *p`. Editors that format on save pick this up; `make fmt` applies it.
BasedOnStyle: LLVM
Language: Cpp
Expand Down
11 changes: 6 additions & 5 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,19 @@ appears in CI, read the code or shrink the CPU set; do not just re-run.
wrappers exist only for **measured** hot natives; cold natives use the generic
`host_native_create` path. The UTF-16→UTF-8 bridge belongs in `pkg/jsc`, not
`environment.odin`.
- `make fmt` (`odin strip-semicolon`) before committing Odin.
- `make fmt` before committing: odinfmt (`odinfmt.json`: tabs, 120 columns, sorted
imports) and `odin strip-semicolon` for Odin, clang-format (`.clang-format`: tabs,
120 columns) for C/C++. odinfmt ships with the OLS extension; pass
`ODINFMT=/path/to/odinfmt` when it is not on PATH.
- **`if` always has braces, on their own lines** — no `if x do y`, no `if x {y}` —
and **a blank line before and after every `if` statement**, unless it opens or
closes a block or a `case` (a comment directly above belongs to the `if`). Applied
by `make fmt`: odinfmt `convert_do`, clang-format `InsertBraces`, then
`scripts/fmt-if.mjs`, which neither formatter covers.
- **Declaration order in a file**, Odin and C alike: imports/includes (sorted), then
constants, then types, then procedures — public ones first, private helpers last.
The formatters sort imports and includes (`odinfmt.json` `sort_imports`,
`.clang-format` `SortIncludes`); nothing reorders declarations, so this is on the
author. Applies to Larva's own packages (`pkg/html`); Lava-synced files keep Lava's
layout so a sync does not fight over it.
The formatters sort imports and includes; nothing reorders declarations, so this is
on the author. Do not retrofit existing files for it alone.

### Contract comments

Expand Down
20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ help:
@printf '%s\n' ' make check-primordials Prototype-pollution ratchet (UPDATE=1 to lower; RAISE=--allow-raise)'
@printf '%s\n' ' make check-jsc Locate JavaScriptCore dev files with install hints'
@printf '%s\n' ' make check-native Verify native build dependencies via pkg-config'
@printf '%s\n' ' make fmt Strip optional semicolons in Odin sources'
@printf '%s\n' ' make fmt Format Odin (odinfmt, strip-semicolon) and C/C++ (clang-format)'
@printf '%s\n' ''
@printf '%s\n' 'Test'
@printf '%s\n' ' make test Odin unit tests + oracle suites (alias of test-all on Unix)'
Expand Down Expand Up @@ -377,20 +377,20 @@ test-lava-nohostfn: build
# clean diff afterwards.
ODINFMT ?= odinfmt

# Odin: odinfmt with odinfmt.json (tabs, 120 columns, sorted imports), then
# strip-semicolon. odinfmt ships with the OLS editor extension and is not on PATH by
# default; point ODINFMT at it. C/C++: clang-format with .clang-format; vendored
# picohttpparser opts out (its own .clang-format).
fmt:
@if command -v $(ODINFMT) >/dev/null 2>&1; then \
for d in cmd/lava pkg/jsc pkg/runtime pkg/std/sqlite pkg/html; do $(ODINFMT) -w $$d >/dev/null; done; \
else echo 'make fmt: odinfmt not found; set ODINFMT=/path/to/odinfmt'; fi
$(ODIN) strip-semicolon cmd/lava -collection:lava=.
$(ODIN) strip-semicolon pkg/jsc -no-entry-point
# Larva's own C (.clang-format). pkg/jsc's C++ comes from Lava and is left as Lava
# formats it, so a sync does not fight over whitespace.
clang-format -i pkg/html/shim/*.c
# Odin in pkg/html: imports sorted, odinfmt.json there. odinfmt ships with the OLS editor
# extension, not on PATH by default; point ODINFMT at it. Lava-synced packages are left
# to strip-semicolon above, so a sync does not fight over whitespace.
@if command -v $(ODINFMT) >/dev/null 2>&1; then $(ODINFMT) -w pkg/html; \
else echo 'make fmt: odinfmt not found; set ODINFMT=/path/to/odinfmt to format pkg/html'; fi
clang-format -i pkg/jsc/*.cpp pkg/html/shim/*.c
# Multi-line blocks after if/else/for and blank lines around if: no formatter does
# either (scripts/fmt-if.mjs). Run after odinfmt, whose convert_do makes the braces.
node scripts/fmt-if.mjs pkg/html
node scripts/fmt-if.mjs cmd/lava pkg/jsc pkg/runtime pkg/std/sqlite pkg/html

# `rm` isn't a Windows command and can't take the $(RUNSCRIPT) prefix (bash would treat
# `rm` as a script), so invoke the shell explicitly: $(SHELL) is Git Bash on Windows and
Expand Down
12 changes: 7 additions & 5 deletions cmd/lava/buffer_reentrancy_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ buffer_natives_detach_during_coercion :: proc(t: ^testing.T) {
jsc.lava_jsc_init()
gctx := jsc.JSGlobalContextCreate(nil)
testing.expect(t, gctx != nil, "could not create a JSC global context")
if gctx == nil do return

if gctx == nil {
return
}

defer jsc.JSGlobalContextRelease(gctx)
ctx := cast(jsc.JSContextRef)gctx

Expand All @@ -65,6 +69,7 @@ buffer_natives_detach_during_coercion :: proc(t: ^testing.T) {
ctx,
reentrancy_eval(ctx, "typeof ArrayBuffer.prototype.transfer === 'function'"),
)

if !has_transfer {
// Older JSC without the detach primitive: nothing to exercise.
return
Expand Down Expand Up @@ -112,10 +117,7 @@ buffer_natives_detach_during_coercion :: proc(t: ^testing.T) {
// bytes; the call must be a no-op (undefined return, no write).
{
view, _ := reentrancy_setup(ctx)
evil2 := reentrancy_eval(
ctx,
"({ valueOf: function () { globalThis.__ab.transfer(); return 2; } })",
)
evil2 := reentrancy_eval(ctx, "({ valueOf: function () { globalThis.__ab.transfer(); return 2; } })")
args := [2]jsc.JSValueRef{view, evil2}
r := lava.buffer_swap_cb(ctx, nil, nil, 2, raw_data(args[:]), &exc)
testing.expect(t, jsc.JSValueIsUndefined(ctx, r), "swapInPlace must no-op on a detached target")
Expand Down
8 changes: 6 additions & 2 deletions cmd/lava/dns_alloc_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "base:runtime"
import "core:mem"
import "core:sync"
import "core:testing"
import eventloop "lava:pkg/runtime/eventloop"
import lava "lava:pkg/runtime"
import eventloop "lava:pkg/runtime/eventloop"

// Guards the localized dns/fetch allocator fix (Dns_Lookup_Request.allocator). The
// hostname is cloned inside the native dns lookup callback (a `proc "c"` where context
Expand Down Expand Up @@ -83,11 +83,15 @@ watched_allocator_proc :: proc(
mem.Allocator_Error,
) {
w := cast(^Watched_Allocator)data

if mode == .Alloc || mode == .Alloc_Non_Zeroed {
for p in WATCHED_PROCS {
if loc.procedure == p do sync.atomic_add(&w.hits, 1)
if loc.procedure == p {
sync.atomic_add(&w.hits, 1)
}
}
}

return w.backing.procedure(w.backing.data, mode, size, alignment, old_memory, old_size, loc)
}

Expand Down
50 changes: 42 additions & 8 deletions cmd/lava/errors_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ node_error_factory :: proc(t: ^testing.T) {
gclass := lava.make_global_class()
gctx := jsc.JSGlobalContextCreate(gclass)
testing.expect(t, gctx != nil, "could not create a JSC global context")

if gctx == nil {
jsc.JSClassRelease(gclass)
return
}

defer jsc.JSClassRelease(gclass)
defer jsc.JSGlobalContextRelease(gctx)
ctx := cast(jsc.JSContextRef)gctx
Expand Down Expand Up @@ -81,7 +83,11 @@ node_error_received_types :: proc(t: ^testing.T) {
jsc.lava_jsc_init()
gctx := jsc.JSGlobalContextCreate(nil)
testing.expect(t, gctx != nil, "could not create a JSC global context")
if gctx == nil do return

if gctx == nil {
return
}

defer jsc.JSGlobalContextRelease(gctx)
ctx := cast(jsc.JSContextRef)gctx

Expand All @@ -108,13 +114,18 @@ node_error_received_types :: proc(t: ^testing.T) {
for c in cases {
val := eval_value(ctx, c.expr)
e := lava.err_invalid_arg_type(ctx, "x", "string", val)

if !testing.expectf(t, jsc.JSValueIsObject(ctx, e), "%s: error must be an object", c.expr) {
continue
}

msg, allocated := lava.value_to_string(ctx, lava.get_named(ctx, cast(jsc.JSObjectRef)e, "message"))
want := fmt.tprintf("The \"x\" argument must be of type string. Received %s", c.want)
testing.expectf(t, msg == want, "%s: got %q want %q", c.expr, msg, want)
if allocated do delete(msg)

if allocated {
delete(msg)
}
}
}

Expand All @@ -131,7 +142,11 @@ expect_error_shape :: proc(
want_name, want_message, want_code: string,
) {
testing.expect(t, jsc.JSValueIsObject(ctx, err), "error value must be an object")
if !jsc.JSValueIsObject(ctx, err) do return

if !jsc.JSValueIsObject(ctx, err) {
return
}

obj := cast(jsc.JSObjectRef)err

global := jsc.JSContextGetGlobalObject(ctx)
Expand All @@ -143,6 +158,7 @@ expect_error_shape :: proc(
check_string_prop(t, ctx, obj, "message", want_message)

code_v := lava.get_named(ctx, obj, "code")

if want_code == "" {
testing.expect(t, jsc.JSValueIsUndefined(ctx, code_v), "plain error should have no code")
} else {
Expand All @@ -154,19 +170,29 @@ expect_error_shape :: proc(
check_string_prop :: proc(t: ^testing.T, ctx: jsc.JSContextRef, obj: jsc.JSObjectRef, prop, want: string) {
v := lava.get_named(ctx, obj, prop)
got, allocated := lava.value_to_string(ctx, v)
defer if allocated do delete(got)
defer if allocated {
delete(got)
}
testing.expectf(t, got == want, "%s: got %q want %q", prop, got, want)
}

@(private = "file")
eval_truthy :: proc(ctx: jsc.JSContextRef, expr: string) -> bool {
c_expr, err := strings.clone_to_cstring(expr, context.temp_allocator)
if err != nil do return false

if err != nil {
return false
}

src := jsc.JSStringCreateWithUTF8CString(c_expr)
defer jsc.JSStringRelease(src)
exc: jsc.JSValueRef
v := jsc.JSEvaluateScript(ctx, src, nil, nil, 1, &exc)
if exc != nil || v == nil do return false

if exc != nil || v == nil {
return false
}

return jsc.JSValueToBoolean(ctx, v)
}

Expand All @@ -175,11 +201,19 @@ eval_truthy :: proc(ctx: jsc.JSContextRef, expr: string) -> bool {
@(private = "file")
eval_value :: proc(ctx: jsc.JSContextRef, expr: string) -> jsc.JSValueRef {
c_expr, err := strings.clone_to_cstring(expr, context.temp_allocator)
if err != nil do return jsc.JSValueMakeUndefined(ctx)

if err != nil {
return jsc.JSValueMakeUndefined(ctx)
}

src := jsc.JSStringCreateWithUTF8CString(c_expr)
defer jsc.JSStringRelease(src)
exc: jsc.JSValueRef
v := jsc.JSEvaluateScript(ctx, src, nil, nil, 1, &exc)
if exc != nil || v == nil do return jsc.JSValueMakeUndefined(ctx)

if exc != nil || v == nil {
return jsc.JSValueMakeUndefined(ctx)
}

return v
}
2 changes: 1 addition & 1 deletion cmd/lava/eval_report_hook_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main

import "core:strings"
import "core:testing"
import eventloop "lava:pkg/runtime/eventloop"
import lava "lava:pkg/runtime"
import eventloop "lava:pkg/runtime/eventloop"

// Pins eval's `report` hook (runtime.odin, Result_Hook): it fires exactly once per eval,
// with the Result eval returns, and BEFORE eval's deferred teardown — the loop is not
Expand Down
22 changes: 18 additions & 4 deletions cmd/lava/fd_census_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,29 @@ RUNNER_THREADS :: #config(ODIN_TEST_THREADS, 0)
// skipped — when RUNNER_THREADS != 1.
fd_census :: proc(timerfds_only := false) -> int {
handle, err := os.open("/proc/self/fd")
if err != nil do return -1

if err != nil {
return -1
}

defer os.close(handle)
infos, read_err := os.read_directory(handle, -1, context.temp_allocator)
if read_err != nil do return -1
if !timerfds_only do return len(infos)

if read_err != nil {
return -1
}

if !timerfds_only {
return len(infos)
}

n := 0
for fi in infos {
target, lerr := os.read_link(fi.fullpath, context.temp_allocator)
if lerr == nil && strings.contains(target, "timerfd") do n += 1

if lerr == nil && strings.contains(target, "timerfd") {
n += 1
}
}
return n
}
12 changes: 10 additions & 2 deletions cmd/lava/fetch_url_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ parse_http_url_cases :: proc(t: ^testing.T) {
for c in cases {
host, port, path, scheme, ok := lava_runtime.parse_http_url(c.url)
testing.expectf(t, ok == c.ok, "%s: ok=%v want %v", c.url, ok, c.ok)
if !c.ok do continue

if !c.ok {
continue
}

testing.expectf(t, host == c.host, "%s: host=%q want %q", c.url, host, c.host)
testing.expectf(t, port == c.port, "%s: port=%d want %d", c.url, port, c.port)
testing.expectf(t, path == c.path, "%s: path=%q want %q", c.url, path, c.path)
Expand Down Expand Up @@ -78,7 +82,11 @@ fetch_parse_chunk_size_cases :: proc(t: ^testing.T) {
for c in cases {
size, ok := lava_runtime.fetch_parse_chunk_size(c.text)
testing.expectf(t, ok == c.ok, "%q: ok=%v want %v", c.text, ok, c.ok)
if !c.ok do continue

if !c.ok {
continue
}

testing.expectf(t, size == c.size, "%q: size=%d want %d", c.text, size, c.size)
}
}
15 changes: 10 additions & 5 deletions cmd/lava/fs_write_payload_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ fs_write_payload_rejects_a_window_resized_by_its_own_getter :: proc(t: ^testing.
jsc.lava_jsc_init()
gctx := jsc.JSGlobalContextCreate(nil)
testing.expect(t, gctx != nil, "could not create a JSC global context")
if gctx == nil do return

if gctx == nil {
return
}

defer jsc.JSGlobalContextRelease(gctx)
ctx := cast(jsc.JSContextRef)gctx

Expand All @@ -57,7 +61,10 @@ fs_write_payload_rejects_a_window_resized_by_its_own_getter :: proc(t: ^testing.
ctx,
payload_eval(ctx, "new ArrayBuffer(8, { maxByteLength: 8 }).resizable === true"),
)
if !resizable do return

if !resizable {
return
}

// A genuine DataView over a 4096-byte resizable buffer, carrying an own byteOffset
// getter that shrinks the backing to 8 bytes and an own byteLength that keeps
Expand Down Expand Up @@ -85,9 +92,7 @@ fs_write_payload_rejects_a_window_resized_by_its_own_getter :: proc(t: ^testing.

// The buffer is 8 bytes live by the time the pointer is taken. Anything longer is
// memory past the allocation — which is what shipped, at 4096.
live := int(
jsc.JSValueToNumber(ctx, payload_eval(ctx, "globalThis.__ab.byteLength"), nil),
)
live := int(jsc.JSValueToNumber(ctx, payload_eval(ctx, "globalThis.__ab.byteLength"), nil))
testing.expectf(t, live == 8, "the getter should have resized the backing to 8, got %d", live)
testing.expectf(
t,
Expand Down
Loading
Loading