Skip to content

Commit 3c86317

Browse files
tannevaledclaude
andauthored
NDArray: the go-ndarray playground's Ruby API, on go-ndarray v0.11.0 (#813)
The go-ndarray playground shows each pipeline as Go | Ruby | NumPy and runs the Ruby column in the browser through cmd/wasm. This implements the API its code generator emits (ruby-ndarray-contract.md): - creation: arange(stop | start, stop | start, stop, step), linspace, eye(n, m, k), zeros()/ones() (0-d with no shape, or a shape Array), full(value, *shape) - views: reshape (with -1), T, squeeze(*axes), expand_dims, dup/copy (copy of the data), and NumPy basic indexing through a[...]: Integer, true for ':', a Range (exclusive, inclusive, endless, beginless) or an Enumerator::ArithmeticSequence ((0..) % 2, (-1..) % -1, (5...1) % -2) - elementwise: square, ** (scalar, array with broadcasting, and 2 ** a through NDArray#coerce), clip, NDArray.maximum/minimum - reductions: sum/mean/max/min/prod/argmax/argmin with (axis, keepdims:), cumsum - products and joins: NDArray.outer, NDArray.concatenate/stack(axis:) - p/inspect/to_s print go-ndarray's NumPy-style Repr Every method now checks its arguments: a wrong count raises ArgumentError, a wrong type TypeError, an index or axis out of range IndexError, other go-ndarray errors ArgumentError with go-ndarray's message. NDArray.arange(5) and NDArray.full() used to raise "ArgumentError: arange: runtime error: index out of range [1] with length 1", a recovered Go panic. TestNativeFaultBecomesRubyError relied on that bug; it now plants its own faulting native. go-ndarray v0.3.0 -> v0.11.0 (and go-fft v0.1.14 -> v0.23.0 with it), outside the grouped Renovate PR #792. The README no longer says the browser target does not build (it has since #684), in three places; readme_wasm_lane_test.go now catches that wording across line breaks. web/smoke.mjs runs an NDArray program in the JS host. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4872e01 commit 3c86317

12 files changed

Lines changed: 1122 additions & 137 deletions

‎README.md‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ no libruby.
1717
examples ([what that counts](#runtime-conformance--rubyspec)) — a large and growing
1818
subset of the language, and **not** a drop-in replacement for CRuby. Read
1919
[What does not work yet](#what-does-not-work-yet) before you depend on it; the
20-
embedding API is currently **one function**, and the browser WebAssembly target
21-
does not build today.
20+
embedding API is currently **one function**.
2221

2322
This repository is the interpreter: a compiler that lowers Ruby to bytecode, and
2423
a stack VM (mruby/YARV lineage) that runs it. The Ruby **front-end** (lexer,
@@ -496,13 +495,22 @@ JRuby**):
496495
reference object), `instance_variable_get`/`set`.
497496
- **Math:** the `Math` module (`sqrt`/`cbrt`/`exp`/`log`/`log2`/`log10`, the
498497
trig and hyperbolic functions, `atan2`/`hypot`/`pow`) with `Math::PI`/`Math::E`.
499-
- **NDArray:** a NumPy-style n-dimensional array — `zeros`/`ones`/`full`/`arange`/
500-
`from`, element-wise `+ - * /` with scalar broadcasting, ufuncs
501-
(`sqrt`/`exp`/`log`/`sin`/`cos`/`abs`), reductions (`sum`/`mean`/`max`/`min`/
502-
`prod`/`argmax`/`argmin`), `matmul`/`dot`, `transpose`/`reshape`/`flatten`,
503-
`shape`/`to_a`/`[]` — binding the pure-Go
498+
- **NDArray:** a NumPy-style n-dimensional array (float64), binding the pure-Go
504499
[go-ndarray](https://github.com/go-ndarray/ndarray) library, **no cgo / no
505-
NumPy**.
500+
NumPy** — the API the go-ndarray playground emits for its Ruby column. Creation
501+
`zeros`/`ones`/`full`/`arange`/`linspace`/`eye`/`from`; views `reshape` (with
502+
`-1`)/`transpose`/`T`/`flatten`/`squeeze`/`expand_dims`, and `dup`/`copy` (a
503+
copy of the data); NumPy basic indexing through `a[...]` — an Integer, `true`
504+
for `:`, a Range (`1...3`, `1..`, `...3`; inclusive ranges too) or an
505+
ArithmeticSequence (`(0..) % 2`, `(-1..) % -1` for `::-1`); element-wise
506+
`+ - * / **` with broadcasting (a number on either side),
507+
`sqrt`/`exp`/`log`/`sin`/`cos`/`abs`/`square`/`clip`,
508+
`NDArray.maximum`/`minimum`; reductions `sum`/`mean`/`max`/`min`/`prod`/
509+
`argmax`/`argmin` (whole array, or `(axis, keepdims: true)`) and `cumsum`;
510+
`matmul`/`dot`/`NDArray.outer`, `NDArray.concatenate`/`stack` (`axis:`);
511+
`shape`/`ndim`/`size`/`to_a` (flat), and `p a` prints NumPy's repr
512+
(`array([[0., 1.], ...])`). Every argument is checked: a wrong count or type
513+
raises `ArgumentError`/`TypeError`, an index or axis out of range `IndexError`.
506514
- **Image:** a scikit-image-style image processor — `Image.new`/`load`/`save`,
507515
pixel `get`/`set`, filters (`gaussian_blur`/`box_blur`/`median`/`sharpen`),
508516
edges (`sobel`/`prewitt`/`scharr`/`laplacian`/`canny`), morphology
@@ -615,11 +623,11 @@ throughput tracks the underlying driver rather than the interpreter:
615623
`eval`/`require` calls (if any) would raise in the closed binary, since there
616624
is no front-end left to compile source at runtime.
617625

618-
- **WebAssembly:** the **WASI** target (`GOOS=wasip1 GOARCH=wasm`) builds and runs,
619-
and is gated in CI. The **browser** target (`GOOS=js GOARCH=wasm`) does **not**
620-
currently build — the playground under `cmd/wasm`, the `JS` bridge and
621-
`rbgo build --closed --target wasm` are all blocked behind one compile error in
622-
`internal/vm`. See [WebAssembly](#webassembly) and *What does not work yet*.
626+
- **WebAssembly:** both targets build and are gated in CI: **WASI**
627+
(`GOOS=wasip1 GOARCH=wasm`) is built and run under wazero, and the **browser**
628+
target (`GOOS=js GOARCH=wasm`, the playground under `cmd/wasm` and the `JS`
629+
bridge) is built and run in node by `web/smoke.mjs`. See
630+
[WebAssembly](#webassembly).
623631

624632
### What does not work yet
625633

@@ -689,7 +697,7 @@ Otherwise:
689697
| **linux, macOS, windows** | the full suite under `-race`. The **100 % coverage gate** is enforced on linux and macOS only — a few POSIX-only paths (opening `/dev/null` as a character device) are unreachable on windows, so its coverage is structurally below 100 % |
690698
| **amd64, arm64** | `go test ./...` on native runners |
691699
| **wasip1/wasm** | built with `CGO_ENABLED=0 GOOS=wasip1 GOARCH=wasm` and **run** under [wazero](https://wazero.io), a pure-Go runtime — verified here: `wazero run rbgo.wasm -e 'puts (1..10).sum'` prints `55` |
692-
| **js/wasm (browser)** | `CGO_ENABLED=0 GOOS=js GOARCH=wasm go build ./...` plus `go vet` over `./cmd/wasm ./cmd/rbgo ./internal/vm` (`wasm (js, browser)` in `ci.yml`). Added with #684, which fixed the three `syscall` constants the `js` port does not define |
700+
| **js/wasm (browser)** | `CGO_ENABLED=0 GOOS=js GOARCH=wasm go build ./...` plus `go vet` over `./cmd/wasm ./cmd/rbgo ./internal/vm`, then the playground module is **run** in node by `web/smoke.mjs` (`wasm (js, browser)` in `ci.yml`). Added with #684, which fixed the three `syscall` constants the `js` port does not define |
693701

694702
**Not on the pull-request path** — validated on a nightly schedule instead:
695703
`riscv64`, `loong64`, `ppc64le` and `s390x`, under QEMU
@@ -884,10 +892,16 @@ $ wazero run rbgo.wasm -e 'puts (1..10).sum'
884892
55
885893
```
886894

887-
**The browser target (`GOOS=js GOARCH=wasm`) does not currently build** — see
888-
*What does not work yet*. The playground under `./cmd/wasm`, the `JS` bridge and
889-
`rbgo build --closed --target wasm` are all blocked behind that compile error, so
890-
treat the browser story as unavailable until it is fixed.
895+
**The browser target (`GOOS=js GOARCH=wasm`) builds and is gated in CI** (since
896+
#684). The `wasm (js, browser)` lane builds `./...` for js/wasm, builds the
897+
playground module from `./cmd/wasm`, and runs it in node with Go's own
898+
`wasm_exec.js` (`web/smoke.mjs`), asserting that Ruby — including an `NDArray`
899+
program — actually ran:
900+
901+
```console
902+
$ CGO_ENABLED=0 GOOS=js GOARCH=wasm go build -o web/rbgo.wasm ./cmd/wasm
903+
$ node web/smoke.mjs web/rbgo.wasm
904+
```
891905

892906
On any wasm target the gem backends that need real sockets or OS facilities are
893907
compiled out (`grpc`, `nats`, `kafka`, `mysql2`, `mongo`/`bson`, `arrow`,

‎go.mod‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ require (
1111
github.com/go-composites/bag v0.0.0-20261001005725-fcf324b93f95
1212
github.com/go-composites/result v0.0.0-20260927170344-f2c7344faeef
1313
github.com/go-composites/time v0.0.0-20260929013028-aa3c57ae1371
14-
github.com/go-fft/fft v0.1.14
14+
github.com/go-fft/fft v0.23.0
1515
github.com/go-images/images v0.0.0-20260927173152-87444e36aac4
1616
github.com/go-kramdown/kramdown v0.2.0
1717
github.com/go-liquid/liquid v0.1.1
1818
github.com/go-mustache/mustache v0.1.0
19-
github.com/go-ndarray/ndarray v0.3.0
19+
github.com/go-ndarray/ndarray v0.11.0
2020
github.com/go-nokogiri/nokogiri v0.1.0
2121
github.com/go-rouge/rouge v0.3.0
2222
github.com/go-ruby-aasm/aasm v0.0.0-20260717061120-cec0976ec205

‎go.sum‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ github.com/go-encryptions/unixcrypt v0.1.0 h1:XWk4h9uCDd+T2hovOzgO0B2vOdJvAwi9nb
249249
github.com/go-encryptions/unixcrypt v0.1.0/go.mod h1:B1dJSjz3kdxvP7cpi5xSzyxezgBijmtNCUeCKqqDd3o=
250250
github.com/go-facter/facter v0.0.0-20260830120958-454b72e642ab h1:bXPxQDhADpiF/S/8jlRFhVisFPRaxPtPgjibLnOzvsE=
251251
github.com/go-facter/facter v0.0.0-20260830120958-454b72e642ab/go.mod h1:ghuoOHWXdq5dE3GLPDiz3Uy0ORrvK+1hB7QC997ez2U=
252-
github.com/go-fft/fft v0.1.14 h1:8RrUztXcaj/N6fXdnAsCkbY3ZqbW61O5S6aDQFH9j3Q=
253-
github.com/go-fft/fft v0.1.14/go.mod h1:KcAewzZmZpMxeylWZGp0/R9p03sInnPQW8B4ttL6WCc=
252+
github.com/go-fft/fft v0.23.0 h1:SBQmOliIthFyRz1MbnrT/HnJejmLJxoWdLXkXsGMCis=
253+
github.com/go-fft/fft v0.23.0/go.mod h1:GN5V4TWCjV05ECqH1ONfjFM6Z1m2shqCdssxKGqQ41s=
254254
github.com/go-gfx/gfx v0.34.0 h1:K46srusNnZls/xkmJRvxpBajlptgrXc6d3exa4ZxhoA=
255255
github.com/go-gfx/gfx v0.34.0/go.mod h1:TC2X+3GMXm8mYZPmp/v7bFrRa69p+k0xz0Nabbd9sII=
256256
github.com/go-hiera/hiera v0.0.0-20260830144306-f9304f6bec92 h1:eyAunbBKGExaT/qWTpapYsBw4lvXApxMixxGs+wek6k=
@@ -291,8 +291,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
291291
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
292292
github.com/go-mustache/mustache v0.1.0 h1:WvnpPlBn8/v1dwVzp7XvzF5dDC1agYMzPyCl1MdPVzY=
293293
github.com/go-mustache/mustache v0.1.0/go.mod h1:jXns/eIBKrnaFbE8BXX44VrTLp92732R0oXBiZC+v/Q=
294-
github.com/go-ndarray/ndarray v0.3.0 h1:t4Nka16rNOTikjnRGrcHR7sqJT3SY24BWdZrmRtYcro=
295-
github.com/go-ndarray/ndarray v0.3.0/go.mod h1:h1IU4Rmev1txi0B2PGMMPUXK1+VCJBKcHM1vBiW/saA=
294+
github.com/go-ndarray/ndarray v0.11.0 h1:HMW7rAI25wSNNgVxOFTYCz48vW/wIBRT8IN17DNZHQ8=
295+
github.com/go-ndarray/ndarray v0.11.0/go.mod h1:ZcgNCBVLXt1FxeZjQWI5Z+k0SSWAT0/wR8slfat/gRA=
296296
github.com/go-nokogiri/nokogiri v0.1.0 h1:3Q3BewGHK16Bt34ggJeyf9xeIMJyA3dmfbIxARPgR9U=
297297
github.com/go-nokogiri/nokogiri v0.1.0/go.mod h1:zjghooYIK8Xfg+fL4/OUhROslx51xGsM3JKtB2TI0tg=
298298
github.com/go-opentype/fonts v0.10.0 h1:9f7ik795SlBEJjz6iqBE/ZV2PaTauAj537MMWw9t4OQ=

‎internal/vm/host_test.go‎

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package vm_test
22

33
import (
44
"bytes"
5-
"strings"
65
"testing"
76

87
"github.com/go-embedded-ruby/ruby/internal/compiler"
@@ -47,24 +46,3 @@ func TestSetConst(t *testing.T) {
4746
t.Fatalf("got %q, want %q", out, "HI\n")
4847
}
4948
}
50-
51-
// TestNativeFaultBecomesRubyError covers the embedding safety net: a native
52-
// binding that indexes past a missing argument raises a Go runtime fault, which
53-
// callNative converts into a rescuable Ruby ArgumentError rather than crashing
54-
// the host. NDArray.arange needs (start, stop); one argument trips it.
55-
func TestNativeFaultBecomesRubyError(t *testing.T) {
56-
_, err := runProg(t, `NDArray.arange(6)`, nil)
57-
if err == nil {
58-
t.Fatal("expected an error, got none")
59-
}
60-
if re, ok := err.(vm.RubyError); !ok || re.Class != "ArgumentError" {
61-
t.Fatalf("got %#v, want an ArgumentError", err)
62-
}
63-
if !strings.Contains(err.Error(), "index out of range") {
64-
t.Fatalf("error %q should describe the underlying fault", err.Error())
65-
}
66-
// Rescuable from Ruby, and the VM keeps running afterwards.
67-
if got := eval(t, `p (begin; NDArray.arange(6); rescue ArgumentError; "caught"; end)`); got != "\"caught\"\n" {
68-
t.Fatalf("rescue got %q, want %q", got, "\"caught\"\n")
69-
}
70-
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) the go-embedded-ruby/ruby authors
2+
//
3+
// SPDX-License-Identifier: BSD-3-Clause
4+
5+
package vm
6+
7+
import (
8+
"bytes"
9+
"strings"
10+
"testing"
11+
12+
"github.com/go-embedded-ruby/ruby/internal/compiler"
13+
"github.com/go-embedded-ruby/ruby/internal/object"
14+
"github.com/go-ruby-parser/parser"
15+
)
16+
17+
// TestNativeFaultBecomesRubyError covers the embedding safety net: a native
18+
// binding that indexes past a missing argument raises a Go runtime fault, which
19+
// callNative converts into a rescuable Ruby ArgumentError rather than crashing
20+
// the host.
21+
//
22+
// This used to be witnessed by NDArray.arange(6), which indexed a second
23+
// argument it never checked for. The binding now checks every argument, so the
24+
// fault is planted here instead, on a native that exists only in this test:
25+
// the safety net must not depend on some binding still being broken.
26+
func TestNativeFaultBecomesRubyError(t *testing.T) {
27+
run := func(src string) (string, error) {
28+
t.Helper()
29+
prog, err := parser.Parse(src)
30+
if err != nil {
31+
t.Fatalf("parse: %v", err)
32+
}
33+
iseq, err := compiler.Compile(prog)
34+
if err != nil {
35+
t.Fatalf("compile: %v", err)
36+
}
37+
var buf bytes.Buffer
38+
machine := New(&buf)
39+
machine.cObject.define("fault_on_missing_arg", func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
40+
return args[1] // index out of range with fewer than two arguments
41+
})
42+
_, err = machine.Run(iseq)
43+
return buf.String(), err
44+
}
45+
46+
_, err := run(`fault_on_missing_arg(6)`)
47+
if err == nil {
48+
t.Fatal("expected an error, got none")
49+
}
50+
if re, ok := err.(RubyError); !ok || re.Class != "ArgumentError" {
51+
t.Fatalf("got %#v, want an ArgumentError", err)
52+
}
53+
if !strings.Contains(err.Error(), "index out of range") {
54+
t.Fatalf("error %q should describe the underlying fault", err.Error())
55+
}
56+
// Rescuable from Ruby, and the VM keeps running afterwards.
57+
out, err := run(`p (begin; fault_on_missing_arg(6); rescue ArgumentError; "caught"; end)`)
58+
if err != nil || out != "\"caught\"\n" {
59+
t.Fatalf("rescue got %q, %v; want %q", out, err, "\"caught\"\n")
60+
}
61+
}

0 commit comments

Comments
 (0)