Skip to content
Open
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
5 changes: 3 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ Typed arrays: `let nums: array<i32> = [1, 2, 3];`

---

## Standard Library (42 modules)
## Standard Library (43 modules)

Import with `@stdlib/` prefix:
```hemlock
Expand Down Expand Up @@ -589,6 +589,7 @@ import { TcpStream, UdpSocket } from "@stdlib/net";
| `logging` | Logger with levels |
| `math` | sin, cos, sqrt, pow, rand, PI, E |
| `net` | TcpListener, TcpStream, UdpSocket |
| `onnx` | ONNX Runtime inference (local ML models) |
| `os` | platform, arch, cpu_count, hostname |
| `path` | File path manipulation |
| `process` | fork, exec, wait, kill |
Expand Down Expand Up @@ -979,7 +980,7 @@ make parity
- Manual memory management with `talloc()` and `sizeof()`
- Async/await with true pthread parallelism
- Atomic operations for lock-free concurrent programming
- 42 stdlib modules (+ arena, assert, semver, toml, retry, iter, random, shell, termios, vector)
- 43 stdlib modules (+ arena, assert, semver, toml, retry, iter, random, shell, termios, vector, onnx)
- FFI for C interop with `export extern fn` for reusable library wrappers
- FFI struct support in compiler (pass C structs by value)
- FFI pointer helpers (`ptr_null`, `ptr_read_*`, `ptr_write_*`)
Expand Down
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ ifeq ($(shell uname),Darwin)
else
HAS_LIBWEBSOCKETS := 0
endif

# On macOS, check for ONNX Runtime
BREW_ONNXRUNTIME := $(shell brew --prefix onnxruntime 2>/dev/null)
ifneq ($(BREW_ONNXRUNTIME),)
HAS_ONNXRUNTIME := $(shell test -f $(BREW_ONNXRUNTIME)/lib/libonnxruntime.dylib && echo 1 || echo 0)
else
HAS_ONNXRUNTIME := 0
endif
else
# On Linux, use pkg-config if available
LIBFFI_CFLAGS := $(shell pkg-config --cflags libffi 2>/dev/null)
Expand All @@ -48,6 +56,9 @@ else

# Check if libwebsockets is available on Linux
HAS_LIBWEBSOCKETS := $(shell pkg-config --exists libwebsockets 2>/dev/null && echo 1 || (test -f /usr/include/libwebsockets.h && echo 1 || echo 0))

# Check if ONNX Runtime is available on Linux
HAS_ONNXRUNTIME := $(shell pkg-config --exists libonnxruntime 2>/dev/null && echo 1 || (test -f /usr/local/include/onnxruntime_c_api.h && echo 1 || (test -f /usr/include/onnxruntime/core/session/onnxruntime_c_api.h && echo 1 || echo 0)))
endif

# Base libraries (always required)
Expand Down Expand Up @@ -177,6 +188,17 @@ else
$(CC) -shared -fPIC -o stdlib/c/libffi_struct_test.so stdlib/c/ffi_struct_test.c
endif
@echo "✓ libffi_struct_test built successfully"
ifeq ($(HAS_ONNXRUNTIME),1)
@echo "Building stdlib/c/libhemlock_onnx..."
ifeq ($(shell uname),Darwin)
$(CC) -shared -fPIC -I$(BREW_ONNXRUNTIME)/include -o stdlib/c/libhemlock_onnx.dylib stdlib/c/onnx_wrapper.c -L$(BREW_ONNXRUNTIME)/lib -lonnxruntime
else
$(CC) -shared -fPIC -o stdlib/c/libhemlock_onnx.so stdlib/c/onnx_wrapper.c -lonnxruntime
endif
@echo "✓ libhemlock_onnx built successfully"
else
@echo "⊘ Skipping libhemlock_onnx (libonnxruntime not installed)"
endif

# Clean stdlib builds
.PHONY: stdlib-clean
Expand Down
35 changes: 34 additions & 1 deletion stdlib/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,45 @@ statically linked into the Hemlock interpreter. No separate FFI wrapper is neede

The `lws_wrapper.so` is primarily used for WebSocket functionality.

## ONNX Runtime Wrapper

### Purpose

`onnx_wrapper.c` provides a flat C interface to ONNX Runtime's `OrtApi` struct-of-function-pointers,
enabling Hemlock's FFI system to call ONNX Runtime functions directly.

### Requirements

```bash
# From GitHub releases
wget https://github.com/microsoft/onnxruntime/releases/download/v1.17.0/onnxruntime-linux-x64-1.17.0.tgz
tar xzf onnxruntime-linux-x64-1.17.0.tgz
sudo cp lib/libonnxruntime.so* /usr/local/lib/
sudo cp -r include/* /usr/local/include/
sudo ldconfig

# macOS
brew install onnxruntime
```

### Building

```bash
# From hemlock root directory
make stdlib
```

This compiles `onnx_wrapper.c` → `libhemlock_onnx.so`

### Usage

Used by `@stdlib/onnx` (`stdlib/onnx.hml`) for local ML inference.

## Future C Modules

Planned FFI wrappers:
- OpenSSL wrapper for crypto functions
- zlib wrapper for compression
- SQLite wrapper for embedded database

## Design Philosophy

Expand Down
Loading
Loading