Skip to content
Draft
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
3 changes: 2 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ console.log(table.toRows());

```html
<script type="module">
import { init } from 'https://cdn.jsdelivr.net/npm/rayforce-wasm@0.2.1/dist/index.js';
import { init } from 'https://cdn.jsdelivr.net/npm/rayforce-wasm@0.3.0/dist/index.js';
const rf = await init();
const result = rf.eval('(sum (til 100))');
console.log(result.toJS()); // 4950
Expand All @@ -139,6 +139,7 @@ console.log(table.toRows());
| DATE | 8 | Int32Array | Days since 2000-01-01 |
| TIME | 9 | Int32Array | Milliseconds since midnight |
| TIMESTAMP | 10 | BigInt64Array | Nanoseconds since 2000-01-01 |
| TIMESPAN | 16 | BigInt64Array | Signed nanoseconds; INT64_MIN is NULL |
| GUID | 11 | - | 128-bit UUID |
| SYM | 12 | BigInt64Array | Interned dictionary-encoded string column |
| STR | 13 | - | Variable-length string (atom or column) |
Expand Down
51 changes: 36 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
RAYFORCE_GITHUB = https://github.com/RayforceDB/rayforce.git
# Pin production builds to a released engine tag. The upstream repository's
# default branch is `dev`, so cloning without a ref can package unreleased code.
RAYFORCE_REF ?= v2.5.13
RAYFORCE_REF ?= v3.0.2
RAYFORCE_COMMIT ?= d5a44cdb4ec33772104f466a2ca177a74b528041
EXEC_DIR = $(shell pwd)
BUILD_DIR = $(EXEC_DIR)/build
DIST_DIR = $(EXEC_DIR)/dist
SRC_DIR = $(EXEC_DIR)/src
OBJ_DIR = $(BUILD_DIR)/obj
RAYFORCE_PATCH = $(EXEC_DIR)/patches/rayforce-v2.5.13-wasm.patch
RAYFORCE_PATCH = $(EXEC_DIR)/patches/rayforce-v3.0.2-wasm.patch

# Rayforce source location: use RAYFORCE_SRC_DIR env var or default to ../rayforce
# v2 layout: public umbrella header in include/, sources nested under src/{core,io,lang,mem,ops,store,table,vec}.
RAYFORCE_SRC_DIR ?= ../rayforce
# Use the pinned nested checkout by default; external source requires an explicit override.
# Core layout: public umbrella header in include/, sources nested under src/{core,io,lang,mem,ops,store,table,vec}.
RAYFORCE_SRC_DIR ?= $(SRC_DIR)/rayforce-repo
RAYFORCE_SRC = $(RAYFORCE_SRC_DIR)/src
RAYFORCE_INC = $(RAYFORCE_SRC_DIR)/include

Expand All @@ -40,9 +41,11 @@ RAYFORCE_VERSION ?= $(shell git -C $(RAYFORCE_SRC_DIR) describe --tags --match '
ifeq ($(strip $(RAYFORCE_VERSION)),)
RAYFORCE_VERSION := $(patsubst v%,%,$(RAYFORCE_REF))
endif
VERSION_MAJOR := $(word 1,$(subst ., ,$(RAYFORCE_VERSION)))
VERSION_MINOR := $(word 2,$(subst ., ,$(RAYFORCE_VERSION)))
VERSION_PATCH := $(word 3,$(subst ., ,$(RAYFORCE_VERSION)))
# Keep numeric C macros valid when an explicitly selected preview has a suffix.
VERSION_NUMERIC := $(firstword $(subst -, ,$(RAYFORCE_VERSION)))
VERSION_MAJOR := $(word 1,$(subst ., ,$(VERSION_NUMERIC)))
VERSION_MINOR := $(word 2,$(subst ., ,$(VERSION_NUMERIC)))
VERSION_PATCH := $(word 3,$(subst ., ,$(VERSION_NUMERIC)))
RAYFORCE_GIT_COMMIT := $(shell git -C $(RAYFORCE_SRC_DIR) rev-parse --short HEAD 2>/dev/null || echo "unknown")
BUILD_DATE := $(shell date -u +%Y-%m-%d)

Expand Down Expand Up @@ -75,6 +78,9 @@ WASM_CFLAGS = -fPIC -Wall -std=$(STD) -O3 -msimd128 \
-ffp-contract=fast -fno-signed-zeros -fno-trapping-math -funroll-loops \
-DSYS_MALLOC $(RAYFORCE_DEFS)

# Match the core codec's strict arithmetic even in the optimized WASM build.
FP_STRICT_store/enc_alp = -fno-fast-math -fno-unsafe-math-optimizations -fno-associative-math -fno-reciprocal-math -fsigned-zeros -ffp-contract=off

# Debug flags for development
# -g : Debug symbols
# -O0 : No optimization (for debugging)
Expand Down Expand Up @@ -145,6 +151,7 @@ EXPORTED_FUNCTIONS = [ \
'_TYPE_CODE_DATE', \
'_TYPE_CODE_TIME', \
'_TYPE_CODE_TIMESTAMP', \
'_TYPE_CODE_TIMESPAN', \
'_TYPE_CODE_GUID', \
'_TYPE_CODE_SYM', \
'_TYPE_CODE_STR', \
Expand Down Expand Up @@ -177,6 +184,11 @@ EXPORTED_FUNCTIONS = [ \
'_init_date', \
'_init_time', \
'_init_timestamp', \
'_init_timespan_parts', \
'_timespan_vec_mark_nulls', \
'_timespan_vec_set_parts', \
'_read_timespan_lo', \
'_read_timespan_hi', \
'_init_symbol_str', \
'_init_string_str', \
'_read_b8', \
Expand Down Expand Up @@ -271,7 +283,8 @@ WASM_MAIN = $(SRC_DIR)/main.c
WASM_MAIN_OBJ = $(OBJ_DIR)/main.o

# All objects for linking
ALL_OBJS = $(CORE_OBJS) $(WASM_MAIN_OBJ)
WASM_PLATFORM_OBJ = $(OBJ_DIR)/platform-stubs.o
ALL_OBJS = $(CORE_OBJS) $(WASM_MAIN_OBJ) $(WASM_PLATFORM_OBJ)

# ============================================================================
# Default Target
Expand All @@ -286,9 +299,14 @@ default: wasm
# Pull rayforce sources from GitHub
# Clone rayforce repo (for CI or fresh setup)
pull:
@echo "⬇️ Cloning rayforce $(RAYFORCE_REF) from GitHub..."
@echo "⬇️ Fetching rayforce $(RAYFORCE_REF), exact commit $(RAYFORCE_COMMIT)..."
@test ! -f $(SRC_DIR)/rayforce-repo/.git || (echo "❌ refusing to replace a linked core worktree"; exit 1)
@rm -rf $(SRC_DIR)/rayforce-repo
@git clone --depth 1 --branch $(RAYFORCE_REF) $(RAYFORCE_GITHUB) $(SRC_DIR)/rayforce-repo
@git init -q $(SRC_DIR)/rayforce-repo
@git -C $(SRC_DIR)/rayforce-repo remote add origin $(RAYFORCE_GITHUB)
@git -C $(SRC_DIR)/rayforce-repo fetch --depth 1 origin $(RAYFORCE_COMMIT)
@git -C $(SRC_DIR)/rayforce-repo checkout -q --detach FETCH_HEAD
@test "$$(git -C $(SRC_DIR)/rayforce-repo rev-parse HEAD)" = "$(RAYFORCE_COMMIT)" || (echo "❌ core checkout does not match RAYFORCE_COMMIT"; exit 1)
@git -C $(SRC_DIR)/rayforce-repo apply $(RAYFORCE_PATCH)
@echo "✅ Rayforce cloned to $(SRC_DIR)/rayforce-repo"
@echo " Build with: RAYFORCE_SRC_DIR=$(SRC_DIR)/rayforce-repo make wasm"
Expand All @@ -315,12 +333,15 @@ $(DIST_DIR):
# obj/lang/eval.o), so the recipe creates the parent dir on demand.
$(OBJ_DIR)/%.o: $(RAYFORCE_SRC)/%.c | $(OBJ_DIR)
@mkdir -p $(@D)
$(CC) -I$(RAYFORCE_INC) -I$(RAYFORCE_SRC) -c $< $(CFLAGS) -o $@
$(CC) -I$(RAYFORCE_INC) -I$(RAYFORCE_SRC) -c $< $(CFLAGS) $(FP_STRICT_$*) -o $@

# Compile WASM main entry point
$(WASM_MAIN_OBJ): $(WASM_MAIN) | $(OBJ_DIR)
$(CC) -I$(RAYFORCE_INC) -I$(RAYFORCE_SRC) -c $< $(CFLAGS) -o $@

$(WASM_PLATFORM_OBJ): $(SRC_DIR)/platform-stubs.c | $(OBJ_DIR)
$(CC) -I$(RAYFORCE_INC) -I$(RAYFORCE_SRC) -c $< $(CFLAGS) -o $@

# Build static library
$(BUILD_DIR)/lib$(TARGET).a: $(CORE_OBJS)
$(AR) rc $@ $(CORE_OBJS)
Expand All @@ -331,7 +352,7 @@ $(BUILD_DIR)/lib$(TARGET).a: $(CORE_OBJS)

# Build optimized WASM release with SDK
wasm: CFLAGS = $(WASM_CFLAGS)
wasm: check-emcc $(DIST_DIR) $(BUILD_DIR)/lib$(TARGET).a $(WASM_MAIN_OBJ)
wasm: check-emcc $(DIST_DIR) $(BUILD_DIR)/lib$(TARGET).a $(WASM_MAIN_OBJ) $(WASM_PLATFORM_OBJ)
$(CC) -I$(SRC_DIR) $(CFLAGS) -o $(DIST_DIR)/$(TARGET).js \
$(ALL_OBJS) \
-s "EXPORTED_FUNCTIONS=$(EXPORTED_FUNCTIONS)" \
Expand All @@ -348,7 +369,7 @@ wasm: check-emcc $(DIST_DIR) $(BUILD_DIR)/lib$(TARGET).a $(WASM_MAIN_OBJ)

# Build debug version with assertions and safety checks
wasm-debug: CFLAGS = $(DEBUG_CFLAGS)
wasm-debug: check-emcc $(DIST_DIR) $(BUILD_DIR)/lib$(TARGET).a $(WASM_MAIN_OBJ)
wasm-debug: check-emcc $(DIST_DIR) $(BUILD_DIR)/lib$(TARGET).a $(WASM_MAIN_OBJ) $(WASM_PLATFORM_OBJ)
$(CC) -I$(SRC_DIR) $(CFLAGS) -o $(DIST_DIR)/$(TARGET)-debug.js \
$(ALL_OBJS) \
-s "EXPORTED_FUNCTIONS=$(EXPORTED_FUNCTIONS)" \
Expand All @@ -359,7 +380,7 @@ wasm-debug: check-emcc $(DIST_DIR) $(BUILD_DIR)/lib$(TARGET).a $(WASM_MAIN_OBJ)

# Build with preloaded examples (standalone version)
wasm-standalone: CFLAGS = $(WASM_CFLAGS)
wasm-standalone: check-emcc $(DIST_DIR) $(BUILD_DIR)/lib$(TARGET).a $(WASM_MAIN_OBJ)
wasm-standalone: check-emcc $(DIST_DIR) $(BUILD_DIR)/lib$(TARGET).a $(WASM_MAIN_OBJ) $(WASM_PLATFORM_OBJ)
@mkdir -p $(BUILD_DIR)/examples
@cp -r $(RAYFORCE_SRC_DIR)/examples/* $(BUILD_DIR)/examples/ 2>/dev/null || true
$(CC) -I$(SRC_DIR) $(CFLAGS) -o $(DIST_DIR)/$(TARGET)-standalone.js \
Expand Down
63 changes: 62 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,67 @@

Full-featured JavaScript SDK for [RayforceDB](https://rayforcedb.com) with zero-copy ArrayBuffer views over native vectors.


## Core and wire compatibility (0.3.0)

This package builds the released core **v3.0.2** (`d5a44cdb4ec33772104f466a2ca177a74b528041`)
with Emscripten 6.0.6. It supports **TIMESPAN (type 16)** and that tag's **wire version 3**.
Current core `dev` emits wire version 4: those frames are deliberately rejected,
not made compatible by changing their version byte. A wire-v4 package requires
building and testing against the matching newer core. This update does not
claim compatibility with arbitrary later core releases.

### Optional immutable dev preview (not an npm release)

A separate, tested preview uses core
`fa9bccfd7f96b9248a39b23994f39a15d001b42c`. That writer emits wire 4 and its
reader accepts wire 3 and 4, including the version-specific error layouts.
It is **not** the released v3.0.2 configuration above and is not a promise of
compatibility with other dev commits. From a fresh checkout with Emscripten
6.0.6 activated:

```sh
make app RAYFORCE_REF=dev-fa9-preview \
RAYFORCE_COMMIT=fa9bccfd7f96b9248a39b23994f39a15d001b42c \
RAYFORCE_VERSION=0.0.0
npm run test:wire4
npm run test:wire4:examples
```

`0.0.0` explicitly labels this experimental core, not a released 3.0.2 binary.
`pull` fetches and verifies the exact commit, not a moving branch. It replaces
the nested core checkout; it refuses to replace a linked Git worktree.
Use separate wrapper checkouts/build outputs for stable and dev builds.
The checked-in native fixtures identify their generating core and include
source generators. Five wasm32 compiler warnings remain in this dev pin;
these checks are not a core full-suite, sanitizer or fault-injection campaign.
This preview does not publish a package or choose the eventual release pin.

A separate inherited SDK issue remains: `Dict.toJS()` decodes SYM keys a second
time and can collapse them to the empty key. Direct named `Dict.get()` read
the native fixture correctly, but that does not fix object conversion. The
dict-conversion repair is part of PR #5, not incorporated into this preview.

Durations are signed **nanoseconds**, not JS dates. `rf.timespan(1n)` creates one
nanosecond; `.value` is exact `bigint`. `.toJS()` returns `bigint` or `null`.
`rf.vector(Types.TIMESPAN, [1n, -1n, null])` exposes an exact `BigInt64Array`;
`at()` and `toJS()` map the reserved `-9223372036854775808n` sentinel to `null`,
while `typedArray` and `at(i, true)` expose the raw bits. Use `set()` for
mutation: it maintains NULL hints, drops stale indexes and handles copy-on-write.
Raw typed-array writes bypass those guarantees. Cached typed-array views can also
become invalid when WASM memory grows; this update does not repair the general
SDK view-refresh behavior. Numeric constructor
arguments must be safe integers; use bigint for larger exact counts.

The WASM build does not provide native TCP/SHM/RDMA or native REPL completion.
Native inline index files contain pointer-width-specific layouts. WASM uses
separate accelerator generations and ignores native inline indexes, which can
be rebuilt; WASM-written inline indexes are likewise ignored by native builds.
This is not general 32/64-bit filesystem-format compatibility. Portable `ser`/`de`
frames are tested separately using checked-in frames generated by the pinned
native core (`test/fixtures/generate-core-frames.c`).


## Features

- 🚀 **Zero-Copy Data Access** - TypedArray views directly over WASM memory
Expand Down Expand Up @@ -35,7 +96,7 @@ console.log(vec.toJS()); // [100, 2, 3, 4, 5]

```html
<script type="module">
import { init } from 'https://cdn.jsdelivr.net/npm/rayforce-wasm@0.2.1/dist/index.js';
import { init } from 'https://cdn.jsdelivr.net/npm/rayforce-wasm@0.3.0/dist/index.js';

const rf = await init();
console.log(rf.eval('(sum (til 100))').toJS()); // 4950
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <h1>⚡ RayforceDB via CDN</h1>

<!-- Load RayforceDB from CDN -->
<script type="module">
import { init } from 'https://unpkg.com/rayforce-wasm@0.2.1/dist/index.js';
import { init } from 'https://unpkg.com/rayforce-wasm@0.3.0/dist/index.js';

window.runDemo = async function() {
const output = document.getElementById('output');
Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rayforce-wasm",
"version": "0.2.1",
"version": "0.3.0",
"description": "RayforceDB WASM SDK - High-performance columnar database in WebAssembly",
"type": "module",
"main": "dist/index.js",
Expand All @@ -25,10 +25,13 @@
"scripts": {
"build": "make app",
"build:dev": "make dev",
"test": "node test.mjs && node test-contract.mjs",
"test": "node test.mjs && node test-contract.mjs && node test-current-core.mjs && npm run test:types",
"test:examples": "node test-examples.mjs",
"prepublishOnly": "npm run build && npm test && npm run test:examples",
"serve": "python3 -m http.server 8080"
"serve": "python3 -m http.server 8080",
"test:types": "tsc --noEmit --strict --target ES2020 --module NodeNext --moduleResolution NodeNext test/typed-timespan.ts",
"test:wire4": "node test-wire4.mjs && node test-contract.mjs && node test-timespan-allocation-unit.mjs && npm run test:types",
"test:wire4:examples": "node test-wire4-examples.mjs"
},
"keywords": [
"rayforce",
Expand All @@ -52,6 +55,7 @@
"access": "public"
},
"devDependencies": {
"playwright": "^1.57.0"
"playwright": "^1.57.0",
"typescript": "5.9.3"
}
}
87 changes: 87 additions & 0 deletions patches/rayforce-v3.0.2-wasm.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
diff --git a/include/rayforce.h b/include/rayforce.h
--- a/include/rayforce.h
+++ b/include/rayforce.h
@@ -180,7 +180,11 @@
* bytes. _idx_pad (8-15) is NULL for numeric columns, but
* holds str_pool for RAY_STR and sym_domain for RAY_SYM
* (attach_finalize preserves it there). See ops/idxop.h. */
- struct { union ray_t* index; union ray_t* _idx_pad; };
+ struct { union ray_t* index;
+#if UINTPTR_MAX == UINT32_MAX
+ uint8_t _index_ptr_pad[4]; /* keep the second aux slot at byte 8 */
+#endif
+ union ray_t* _idx_pad; };
/* RAY_ATTR_HAS_LINK (vectors, RAY_I32/RAY_I64 only): bytes
* 8-15 hold an int64 sym ID naming the target table.
* link_lo[8] aliases bytes 0-7. See ops/linkop.h. */
diff --git a/src/ops/idxop.h b/src/ops/idxop.h
--- a/src/ops/idxop.h
+++ b/src/ops/idxop.h
@@ -145,6 +145,9 @@
int64_t n_keys; /* number of non-null rows indexed */
int64_t n_groups; /* number of distinct keys */
int64_t order_sym; /* optional column symbol ordered within each group */
+#if UINTPTR_MAX == UINT32_MAX
+ uint8_t _wasm32_pad[16]; /* four pointer high halves absent on wasm32 */
+#endif
} hash;
struct { /* RAY_IDX_SORT */
ray_t* perm; /* RAY_I64 vec, perm[i] = row id at sorted pos i */
@@ -246,6 +249,7 @@
* contiguous 32-byte-aligned ray_t blocks mmap'd in place (no serialization).
* For the trailing child blocks to stay 32-aligned, the index payload must be a
* 32-byte multiple — enforce it so the layout invariant can't silently break. */
+_Static_assert(sizeof(((ray_index_t*)0)->u.hash) == 64, "index hash payload must reserve 64 bytes");
_Static_assert(sizeof(ray_index_t) % 32 == 0,
"ray_index_t must be a 32-byte multiple for in-place mmap layout");

diff --git a/src/ops/idxop.c b/src/ops/idxop.c
--- a/src/ops/idxop.c
+++ b/src/ops/idxop.c
@@ -1528,16 +1528,28 @@
* simply ignored (column loads unindexed) rather than mis-decoded.
* Generation 1 = the CSR grouped hash layout (table/gkeys/offs/rows);
* generation-0 regions carried the retired chain layout. */
+#if defined(RAY_OS_WASM)
+#define RAY_IDX_FORMAT_MAJOR ((uint8_t)129)
+#else
#define RAY_IDX_FORMAT_MAJOR ((uint8_t)1)
+#endif
/* Generation 2: the same layout with the hash `table`, `offs` and `rows`
* stored narrow (see hx_get). Written only for indexes that carry a narrow
* array, so a binary that knows generation 1 alone loads such a column
* unindexed instead of misreading it; generation-1 regions read as before. */
+#if defined(RAY_OS_WASM)
+#define RAY_IDX_FORMAT_NARROW ((uint8_t)130)
+#else
#define RAY_IDX_FORMAT_NARROW ((uint8_t)2)
+#endif
/* Generation 3: a hash table whose home slots are the top hash bits
* (RAY_MARK_HASH_HIGH, groups in hash order); arrays narrow or I64 as in
* generation 2. Older binaries load such a column unindexed. */
+#if defined(RAY_OS_WASM)
+#define RAY_IDX_FORMAT_HASH_HIGH ((uint8_t)131)
+#else
#define RAY_IDX_FORMAT_HASH_HIGH ((uint8_t)3)
+#endif

/* Hash index arrays whose values are group ids, positions or row ids
* (`table`, `offs`, `rows`) fit 32 bits on any column below 2^32 rows; they
diff --git a/src/io/csv.c b/src/io/csv.c
--- a/src/io/csv.c
+++ b/src/io/csv.c
@@ -3868,12 +3868,14 @@
wrote_any = true;
/* The chunk's bytes are done with: drop them from the mapping so a
* long file does not pin its whole length in resident memory. */
+#if !defined(RAY_OS_WASM)
if (next_offset > chunk_offset) {
size_t ps = (size_t)sysconf(_SC_PAGESIZE);
size_t a = (chunk_offset + ps - 1) & ~(ps - 1);
size_t b = next_offset & ~(ps - 1);
if (b > a) madvise((void*)(buf + a), b - a, MADV_DONTNEED);
}
+#endif
if (cnt == 0) break;
chunk_offset = next_offset;
}
3 changes: 2 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {

export * from './rayforce.sdk.js';

export declare const version: '0.2.1';
export declare const version: '0.3.0';

export interface InitOptions {
/** URL/path to the Emscripten module loader. Defaults to ./rayforce.js. */
Expand Down Expand Up @@ -37,6 +37,7 @@ export declare const create: {
readonly date: (value: number | Date) => ReturnType<RayforceSDK['date']>;
readonly time: (value: number | Date) => ReturnType<RayforceSDK['time']>;
readonly timestamp: (value: number | bigint | Date) => ReturnType<RayforceSDK['timestamp']>;
readonly timespan: (value: number | bigint | null) => import('./rayforce.sdk.js').RayTimespan;
};

declare const rayforce: {
Expand Down
Loading