✓
Passing This code compiles and runs correctly.
Code
// TEST: the JS expression lowering must carry two Zig-spelled Koru forms it
// used to pass through raw:
//
// - `var name: f64 = …` — a TYPED declaration inside a kernel op body
// (koru_body mode). Emitted verbatim it reached `node` as
// `var nx: f64 = 0.0;` — a syntax error, not a wrong answer.
// - `if (c) a else b` — the inline conditional EXPRESSION, in koru_body
// (kernel op) AND koru_expr (insert field) modes. The koru_expr path
// reserved-word-mangled `if`/`else` to `if$`/`else$`.
//
// kind>0.5 rows gain 100, others gain 1; the third row's x is itself an
// inline-if insert field. Both targets print the same three rows.
import std/io
import std/store
import std/kernel
std/store:new(world, capacity: 8) { x: f64, kind: f64 }
std/store:insert(world) { x: 0.0, kind: 0.0 }
std/store:insert(world) { x: 1.0, kind: 1.0 }
std/store:insert(world) { x: (if (1.0 > 0.0) @as(f64, 5.0) else @as(f64, 9.0)), kind: 1.0 }
std/kernel:init(world) {}
| kernel k |> std/kernel:self {
var bonus: f64 = 0.0;
bonus = (if (k.kind > 0.5) @as(f64, 100.0) else @as(f64, 1.0));
k.x += bonus;
}
std/store:query(world)
! query r |> std/io:print.ln("x {{ r.x:f }}")
Actual
x 1
x 101
x 105
Expected output
✓ Zig✓ JavaScriptx 1
x 101
x 105
Emitted Zig source
// Access compiler flags via the per-user compiler_env module
const CompilerEnv = @import("compiler_env").CompilerEnv;
pub const panic = if (@import("builtin").mode == .Debug)
@import("std").debug.FullPanic(@import("std").debug.defaultPanic)
else
@import("std").debug.simple_panic;
const __koru_bare = struct {
extern fn posix_memalign(memptr: *?*anyopaque, alignment: usize, size: usize) c_int;
extern fn free(ptr: ?*anyopaque) void;
fn bareAlloc(_: *anyopaque, len: usize, alignment: @import("std").mem.Alignment, _: usize) ?[*]u8 {
var p: ?*anyopaque = null;
const a = @max(alignment.toByteUnits(), @sizeOf(usize));
if (posix_memalign(&p, a, len) != 0) return null;
return @ptrCast(p);
}
fn bareResize(_: *anyopaque, _: []u8, _: @import("std").mem.Alignment, _: usize, _: usize) bool { return false; }
fn bareRemap(_: *anyopaque, _: []u8, _: @import("std").mem.Alignment, _: usize, _: usize) ?[*]u8 { return null; }
fn bareFree(_: *anyopaque, memory: []u8, _: @import("std").mem.Alignment, _: usize) void { free(@ptrCast(memory.ptr)); }
const vtable = @import("std").mem.Allocator.VTable{ .alloc = bareAlloc, .resize = bareResize, .remap = bareRemap, .free = bareFree };
const allocator = @import("std").mem.Allocator{ .ptr = undefined, .vtable = &vtable };
};
const __koru_backing = if (@import("builtin").link_libc) @import("std").heap.c_allocator else if (@import("builtin").os.tag == .freestanding and !@import("builtin").cpu.arch.isWasm()) __koru_bare.allocator else @import("std").heap.page_allocator;
var __koru_leak_count: @import("std").atomic.Value(usize) = .init(0);
var __koru_par_tasks: @import("std").atomic.Value(usize) = .init(0);
const __koru_par_CAP: usize = 128;
threadlocal var __koru_par_depth: usize = 0;
const __koru_par_DEPTH_CAP: usize = 7;
var __koru_par_pool: @import("std").Thread.Pool = undefined;
var __koru_par_pool_ready: bool = false;
var __koru_par_pool_mtx: @import("std").Thread.Mutex = .{};
fn __koru_par_pool_ensure() *@import("std").Thread.Pool {
__koru_par_pool_mtx.lock();
defer __koru_par_pool_mtx.unlock();
if (!__koru_par_pool_ready) {
@import("std").Thread.Pool.init(&__koru_par_pool, .{ .allocator = @import("std").heap.smp_allocator }) catch @panic("koru: thread pool init failed");
__koru_par_pool_ready = true;
}
return &__koru_par_pool;
}
fn __koru_alloc(ctx: *anyopaque, len: usize, alignment: @import("std").mem.Alignment, ret_addr: usize) ?[*]u8 {
_ = ctx;
const r = __koru_backing.rawAlloc(len, alignment, ret_addr);
if (comptime @import("builtin").mode == .Debug) {
if (r != null) _ = __koru_leak_count.fetchAdd(1, .monotonic);
}
return r;
}
fn __koru_resize(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, new_len: usize, ret_addr: usize) bool {
_ = ctx;
return __koru_backing.rawResize(memory, alignment, new_len, ret_addr);
}
fn __koru_remap(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, new_len: usize, ret_addr: usize) ?[*]u8 {
_ = ctx;
return __koru_backing.rawRemap(memory, alignment, new_len, ret_addr);
}
fn __koru_free(ctx: *anyopaque, memory: []u8, alignment: @import("std").mem.Alignment, ret_addr: usize) void {
_ = ctx;
__koru_backing.rawFree(memory, alignment, ret_addr);
if (comptime @import("builtin").mode == .Debug) {
_ = __koru_leak_count.fetchSub(1, .monotonic);
}
}
const __koru_vtable = @import("std").mem.Allocator.VTable{ .alloc = __koru_alloc, .resize = __koru_resize, .remap = __koru_remap, .free = __koru_free };
pub fn koru_allocator() @import("std").mem.Allocator {
return .{ .ptr = undefined, .vtable = &__koru_vtable };
}
pub inline fn __koru_intcast(comptime T: type, x: anytype) T {
if (comptime (@import("builtin").mode == .Debug or @import("builtin").mode == .ReleaseSafe))
return @as(T, @intCast(x));
const dst = @typeInfo(T);
const src = @typeInfo(@TypeOf(x));
if (comptime (dst == .int and src == .int and dst.int.bits == src.int.bits and dst.int.signedness != src.int.signedness))
return @as(T, @bitCast(x));
return @as(T, @intCast(x));
}
fn __KoruPadI(comptime T: type) type {
const V = switch (@typeInfo(T)) { .comptime_int => i64, .comptime_float => f64, else => T };
return struct {
v: V,
width: usize,
pub fn format(self: @This(), w: *@import("std").Io.Writer) @import("std").Io.Writer.Error!void {
const rendered = @import("std").fmt.count("{d}", .{self.v});
const pad = self.width -| rendered;
for (0..pad) |_| try w.writeByte(' ');
try w.print("{d}", .{self.v});
}
};
}
pub fn __koru_pad_i(width: usize, value: anytype) __KoruPadI(@TypeOf(value)) {
return .{ .v = value, .width = width };
}
pub fn koru_leak_check() void {
if (comptime @import("builtin").mode != .Debug) return;
if (__koru_leak_count.load(.acquire) == 0) return;
if (comptime @import("builtin").target.os.tag == .freestanding) {
if (comptime @import("builtin").cpu.arch == .wasm32 or @import("builtin").cpu.arch == .wasm64) {
@panic("KORU LEAK CHECK FAILED: the produced program leaked");
} else {
const __klc = struct { extern var stdout: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; };
var __lb: [128]u8 = undefined;
const __lm = "KORU LEAK CHECK FAILED: allocations still outstanding at end of run: ";
@memcpy(__lb[0..__lm.len], __lm);
var __ln: usize = __lm.len;
var __lv = __koru_leak_count.load(.acquire);
var __ld: [20]u8 = undefined;
var __lk: usize = 0;
while (__lv > 0) : (__lk += 1) { __ld[__lk] = @intCast('0' + __lv % 10); __lv /= 10; }
for (0..__lk) |__li| { __lb[__ln] = __ld[__lk - 1 - __li]; __ln += 1; }
__lb[__ln] = '\n'; __ln += 1; __lb[__ln] = 0;
_ = __klc.fputs(@as([*:0]const u8, @ptrCast(&__lb)), __klc.stdout);
@trap();
}
} else {
@import("std").debug.print("KORU LEAK CHECK FAILED: the produced program leaked (trace above)\n", .{});
@import("std").process.exit(1);
}
}
pub const main_module = struct {
// std/store: plural store 'world' created (SoA cell + insert/query/write/take/stripe units); fields x: f64, kind: f64
// >>> FLOW: tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:21 ~input:__store_insert_world()
pub fn flow0() void {
_ = main_module.__store_insert_world_event.handler(.{ .x = 0.0, .kind = 0.0, .__site_line = 21 });
}
// >>> FLOW: tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:22 ~input:__store_insert_world()
pub fn flow1() void {
_ = main_module.__store_insert_world_event.handler(.{ .x = 1.0, .kind = 1.0, .__site_line = 22 });
}
// >>> FLOW: tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:23 ~input:__store_insert_world()
pub fn flow2() void {
_ = main_module.__store_insert_world_event.handler(.{ .x = (if (1.0 > 0.0) @as(f64, 5.0) else @as(f64, 9.0)), .kind = 1.0, .__site_line = 23 });
}
// >>> FLOW: tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:25 ~input:kernel_init_1954844754()
pub fn flow3() void {
const result_0 = main_module.kernel_init_1954844754_event.handler(.{ });
const _auto_6 = result_0.kernel;
_ = &_auto_6;
}
// >>> FLOW: tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:32 ~input:__store_sweeprun_world_L33_390_120_kernel_body_typed_var_inline_if_input_a9ed08()
pub fn flow4() void {
_ = main_module.__store_sweeprun_world_L33_390_120_kernel_body_typed_var_inline_if_input_a9ed08_event.handler(.{ });
}
pub fn koru_start_flow() void {
const result_0 = koru_koru.start_event.handler(.{ });
const result_0_done = result_0.done;
_ = &result_0_done;
}
pub fn koru_end_flow() void {
const result_0 = koru_koru.end_event.handler(.{ });
const result_0_done = result_0.done;
_ = &result_0_done;
}
const __KoruStoreT_world = struct {
x: [8]f64 = undefined,
kind: [8]f64 = undefined,
len: usize = 0,
};
var __koru_store_world: __KoruStoreT_world = .{};
const __KoruStoreRow_world = struct { x: f64, kind: f64 };
pub const __store_insert_world_event = struct {
pub const Input = struct {
x: f64,
kind: f64,
__site_line: i64,
};
pub const Output = void;
pub inline fn handler(__koru_event_input: @This().Input) @This().Output {
return __koru_handler_impl(__koru_event_input.x, __koru_event_input.kind, __koru_event_input.__site_line);
}
fn __koru_handler_impl(__koru_p_0: f64, __koru_p_1: f64, __koru_p_2: i64) @This().Output {
const __koru_event_input: @This().Input = .{ .x = __koru_p_0, .kind = __koru_p_1, .__site_line = __koru_p_2 };
// >>> PROC: __store_insert_world [tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:19]
const x = __koru_event_input.x;
const kind = __koru_event_input.kind;
const __site_line = __koru_event_input.__site_line;
_ = &x;
_ = &kind;
_ = &__site_line;
_ = &__koru_event_input;
if (__koru_store_world.len >= 8) @panic("std/store: store 'world' is full (capacity 8) - declared capacity and the `| full` branch are pinned at 690_011");
const __koru_new_row = __koru_store_world.len;
__koru_store_world.x[__koru_new_row] = x;
__koru_store_world.kind[__koru_new_row] = kind;
__koru_store_world.len += 1;
return;
}
};
pub const __store_apply_world_event = struct {
pub const Input = struct {
row: usize,
field: i64,
value_0: f64,
value_1: f64,
};
pub const Output = union(enum(u8)) {
x: f64,
kind: f64,
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> PROC: __store_apply_world [tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:19]
const row = __koru_event_input.row;
const field = __koru_event_input.field;
const value_0 = __koru_event_input.value_0;
const value_1 = __koru_event_input.value_1;
_ = &row;
_ = &field;
_ = &value_0;
_ = &value_1;
_ = &__koru_event_input;
const __koru_r: usize = row;
return switch (field) {
0 => blk: { __koru_store_world.x[__koru_r] = value_0; break :blk .{ .x = value_0 }; },
1 => blk: { __koru_store_world.kind[__koru_r] = value_1; break :blk .{ .kind = value_1 }; },
else => unreachable,
};
}
};
var __koru_pool: @import("std").Thread.Pool = undefined;
var __koru_pool_ready: bool = false;
fn __koru_pool_ensure() *@import("std").Thread.Pool {
if (!__koru_pool_ready) {
@import("std").Thread.Pool.init(&__koru_pool, .{ .allocator = @import("std").heap.smp_allocator }) catch @panic("koru: thread pool init failed");
__koru_pool_ready = true;
}
return &__koru_pool;
}
pub const kernel_init_1954844754_event = struct {
pub const Input = struct {
};
pub const Output = union(enum(u8)) {
kernel: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> PROC: kernel_init_1954844754 [tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:25]
_ = &__koru_event_input;
const __koru_par_kernel_init_1954844754_data_0 = struct {
fn run(lo: usize, hi: usize) void {
for (lo..hi) |__koru_i| {
var bonus: f64 = 0.0;
bonus = (if (__koru_store_world.kind[__koru_i] > 0.5) @as(f64, 100.0) else @as(f64, 1.0));
__koru_store_world.x[__koru_i] += bonus;
}
}
};
{
const __koru_n = __koru_store_world.len;
if (__koru_n >= 65536) {
const __koru_p = __koru_pool_ensure();
const __koru_nc = @max(@as(usize, 1), @min(@import("std").Thread.getCpuCount() catch 1, (__koru_n + 4095) / 4096));
var __koru_wg: @import("std").Thread.WaitGroup = .{};
for (0..__koru_nc) |__koru_t| {
const __koru_lo = __koru_t * __koru_n / __koru_nc;
const __koru_hi = if (__koru_t + 1 == __koru_nc) __koru_n else (__koru_t + 1) * __koru_n / __koru_nc;
if (__koru_hi <= __koru_lo) break;
__koru_p.spawnWg(&__koru_wg, __koru_par_kernel_init_1954844754_data_0.run, .{ __koru_lo, __koru_hi });
}
__koru_wg.wait();
} else {
for (0..__koru_store_world.len) |i| {
var bonus: f64 = 0.0;
bonus = (if (__koru_store_world.kind[i] > 0.5) @as(f64, 100.0) else @as(f64, 1.0));
__koru_store_world.x[i] += bonus;
}
}
}
return .{ .kernel = .{} };
}
};
pub const __store_sweepbody_world_L33_390_120_kernel_body_typed_var_inline_if_input_a9ed08_event = struct {
pub const Input = struct {
__koru_srf_r_L33_x: f64,
__koru_sdix_r_L33: usize,
};
pub const Output = void;
pub inline fn handler(__koru_event_input: @This().Input) @This().Output {
return __koru_handler_impl(__koru_event_input.__koru_srf_r_L33_x, __koru_event_input.__koru_sdix_r_L33);
}
fn __koru_handler_impl(__koru_p_0: f64, __koru_p_1: usize) @This().Output {
const __koru_event_input: @This().Input = .{ .__koru_srf_r_L33_x = __koru_p_0, .__koru_sdix_r_L33 = __koru_p_1 };
// >>> SUBFLOW: tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:32
const __koru_srf_r_L33_x = __koru_event_input.__koru_srf_r_L33_x;
const __koru_sdix_r_L33 = __koru_event_input.__koru_sdix_r_L33;
_ = &__koru_srf_r_L33_x;
_ = &__koru_sdix_r_L33;
_ = &__koru_event_input;
(struct { fn __kout(__fd: i32, __b: []const u8) void { if (@import("builtin").os.tag == .freestanding) { const __kc = struct { extern var stdout: ?*anyopaque; extern var stderr: ?*anyopaque; extern fn fputs(__s: [*:0]const u8, __st: ?*anyopaque) c_int; }; var __kt: [4096]u8 = undefined; for (0..(__b.len + __kt.len - 2) / (__kt.len - 1)) |__ki| { const __kn = @min(__kt.len - 1, __b.len - __ki * (__kt.len - 1)); @memcpy(__kt[0..__kn], __b[__ki * (__kt.len - 1)..][0..__kn]); __kt[__kn] = 0; @import("std").mem.doNotOptimizeAway(__kc.fputs(@as([*:0]const u8, @ptrCast(&__kt)), if (__fd == 2) __kc.stderr else __kc.stdout)); } } else { @import("std").mem.doNotOptimizeAway(@import("std").posix.write(__fd, __b) catch @as(usize, 0)); } } fn __kw(comptime __f: []const u8, __a: anytype) void { var __kb: [65536]u8 = undefined; const __ks = @import("std").fmt.bufPrint(&__kb, __f, __a) catch __kb[0..0]; __kout(1, __ks); } }).__kw("x {}\n", .{__koru_srf_r_L33_x});
}
};
pub const __store_sweeprun_world_L33_390_120_kernel_body_typed_var_inline_if_input_a9ed08_event = struct {
pub const Input = struct {
};
pub const Output = void;
pub fn handler(__koru_event_input: @This().Input) @This().Output {
// >>> PROC: __store_sweeprun_world_L33_390_120_kernel_body_typed_var_inline_if_input_a9ed08 [tests/regression/300_ADVANCED_FEATURES/390_KERNEL/390_120_kernel_body_typed_var_inline_if/input.k:32]
_ = &__koru_event_input;
{
for (0..main_module.__koru_store_world.len) |__koru_si| {
const __koru_srf_r_L33_x = main_module.__koru_store_world.x[__koru_si];
_ = &__koru_srf_r_L33_x;
main_module.__store_sweepbody_world_L33_390_120_kernel_body_typed_var_inline_if_input_a9ed08_event.handler(.{ .__koru_srf_r_L33_x = __koru_srf_r_L33_x, .__koru_sdix_r_L33 = __koru_si });
}
}
return;
}
};
};
pub const koru_koru = struct {
pub const start_event = struct {
pub const Input = struct {
};
pub const Output = union(enum(u8)) {
done: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
_ = &__koru_event_input;
return .{ .done = .{} };
}
};
pub const end_event = struct {
pub const Input = struct {
};
pub const Output = union(enum(u8)) {
done: struct {
},
};
pub fn handler(__koru_event_input: @This().Input) @This().Output {
_ = &__koru_event_input;
return .{ .done = .{} };
}
};
};
pub fn main() void {
main_module.koru_start_flow();
main_module.flow0();
main_module.flow1();
main_module.flow2();
main_module.flow3();
main_module.flow4();
main_module.koru_end_flow();
if (comptime @import("builtin").mode == .Debug) koru_leak_check();
}
test {
@import("std").testing.refAllDeclsRecursive(@This());
}
Flows
flow ~new click a branch to expand · @labels scroll to their anchor
new (world, capacity: 8, source: x: f64, kind: f64)
flow ~insert click a branch to expand · @labels scroll to their anchor
insert (world, source: x: 0.0, kind: 0.0)
flow ~insert click a branch to expand · @labels scroll to their anchor
insert (world, source: x: 1.0, kind: 1.0)
flow ~insert click a branch to expand · @labels scroll to their anchor
insert (world, source: x: (if (1.0 > 0.0) @as(f64, 5.0) else @as(f64, 9.0)), kind: 1.0)
flow ~init click a branch to expand · @labels scroll to their anchor
init (expr: world, source: )
flow ~query click a branch to expand · @labels scroll to their anchor
query (world)
Test Configuration
MUST_RUN LANGUAGES: zig js