interscript 3.8.4 → 3.8.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +24 -0
- data/docs/wasm-engine.md +149 -0
- data/interscript.gemspec +6 -0
- data/lib/interscript/command.rb +4 -2
- data/lib/interscript/interpreter.rb +3 -0
- data/lib/interscript/isc/parser.rb +4 -1
- data/lib/interscript/isc.rb +1 -0
- data/lib/interscript/ml/imf.rb +1 -1
- data/lib/interscript/version.rb +1 -1
- data/lib/interscript/wasm/PROVENANCE.md +79 -0
- data/lib/interscript/wasm/engine_capi.wasm +0 -0
- data/lib/interscript/wasm/loader.rb +57 -0
- data/lib/interscript/wasm/runtime.rb +336 -0
- data/lib/interscript/wasm.rb +29 -0
- data/lib/interscript/wasm_engine.rb +134 -0
- data/lib/interscript.rb +7 -2
- metadata +23 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e3684072e50f85b814fd32e58fe0ba2de3fbdd15551f0929f253e252dc57eb9e
|
|
4
|
+
data.tar.gz: 0ddbfeffb8589fcdadb6e0575ccfffe16a8e2aa5427396327614ad2f0ea52f2d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 99463ab5044de3ce375e2491cbde4484d34b5bacc1a79e41ed3a13a86192a4a1c7cfdcd87b2c293a02d57a6a634c2e76a00f913f9b2cd574928243a9dbe19c9c
|
|
7
|
+
data.tar.gz: d69ce551bad2e1366be34cd9592a0dcf43b267c3556299b5cd998199d0a71a45c1112faf76be9c4fd5dea2ff564dde4037db1e7a51d13015aca9c5f083b82267
|
data/README.md
CHANGED
|
@@ -18,6 +18,13 @@ vectors.
|
|
|
18
18
|
gem install interscript
|
|
19
19
|
----
|
|
20
20
|
|
|
21
|
+
The wasm engine is opt-in and needs the `wasmtime` gem, which the gemspec
|
|
22
|
+
does not declare: add `gem "wasmtime", "~> 50.0"` to your Gemfile. It is
|
|
23
|
+
precompiled for the common platforms (x86_64-linux, x86_64-linux-musl,
|
|
24
|
+
aarch64-linux, aarch64-linux-musl, arm64-darwin, x64-mingw-ucrt,
|
|
25
|
+
aarch64-mingw-ucrt; Ruby >= 3.3); elsewhere it builds from source and needs
|
|
26
|
+
`cargo`. Details: link:docs/wasm-engine.md[docs/wasm-engine.md].
|
|
27
|
+
|
|
21
28
|
== Transliterate
|
|
22
29
|
|
|
23
30
|
[source,sh]
|
|
@@ -33,6 +40,23 @@ Interscript.transliterate("odni-rus-Cyrl-Latn-2015", "привет")
|
|
|
33
40
|
# => "privet"
|
|
34
41
|
----
|
|
35
42
|
|
|
43
|
+
== Wasm engine (opt-in)
|
|
44
|
+
|
|
45
|
+
The https://github.com/interscript/interscript-rs[interscript-rs] map
|
|
46
|
+
engine, vendored as WebAssembly and embedded through wasmtime-rb — the
|
|
47
|
+
same engine behind `iscrs` and the npm package, with no Ruby
|
|
48
|
+
transliteration logic of its own. It is opt-in until the full golden
|
|
49
|
+
parity gate is byte-exact (interscript-ruby#886):
|
|
50
|
+
|
|
51
|
+
[source,ruby]
|
|
52
|
+
----
|
|
53
|
+
Interscript.transliterate("iso-rus-Cyrl-Latn-9-2026", "Кириллица",
|
|
54
|
+
compiler: Interscript::WasmEngine)
|
|
55
|
+
----
|
|
56
|
+
|
|
57
|
+
Packaging notes, the loader design, memory, and the parity suite:
|
|
58
|
+
link:docs/wasm-engine.md[docs/wasm-engine.md].
|
|
59
|
+
|
|
36
60
|
== Workflow extension (ISC-WF-1)
|
|
37
61
|
|
|
38
62
|
Maps can reuse rule blocks and condition rows on a language. A
|
data/docs/wasm-engine.md
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# The wasm engine (`Interscript::WasmEngine`)
|
|
2
|
+
|
|
3
|
+
`Interscript::WasmEngine` runs the [interscript-rs](https://github.com/interscript/interscript-rs)
|
|
4
|
+
map engine in-process: the crate's C ABI (`src/capi.rs`) compiled to
|
|
5
|
+
WebAssembly (`engine_capi.wasm`, vendored in this gem), embedded through
|
|
6
|
+
[wasmtime-rb](https://github.com/bytecodealliance/wasmtime-rb). It is the
|
|
7
|
+
same engine the `iscrs` CLI and the npm `@interscript/engine-wasm` package
|
|
8
|
+
run — there is no Ruby transliteration logic behind it.
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
require "interscript"
|
|
12
|
+
|
|
13
|
+
Interscript.transliterate("iso-rus-Cyrl-Latn-9-2026", "Кириллица",
|
|
14
|
+
compiler: Interscript::WasmEngine) # => "Kirillica"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
On the CLI: `interscript <file> -s <map> -c Interscript::WasmEngine`.
|
|
18
|
+
|
|
19
|
+
## Status
|
|
20
|
+
|
|
21
|
+
Opt-in. The default engine is still the compiled-Regexp executor
|
|
22
|
+
(`Interscript::Interpreter`); the wasm engine becomes the default only after
|
|
23
|
+
the full parity gate below is byte-exact (interscript-ruby#886).
|
|
24
|
+
|
|
25
|
+
## What it covers
|
|
26
|
+
|
|
27
|
+
| | |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| `transliterate(map, text)` | `iscrs_transliterate` (stage `main`) |
|
|
30
|
+
| `language:` (ISC-WF-1 W3) | `iscrs_transliterate_lang` / `iscrs_transliterate_stage` |
|
|
31
|
+
| a named `stage` | `iscrs_transliterate_stage` |
|
|
32
|
+
| `"a\|b"` composition | a's main stage, then b's — as the DSL's virtual map does |
|
|
33
|
+
| dependency closures | `Interscript::Wasm::Loader` — see below |
|
|
34
|
+
| `map_info`, `stage_names`, `map_dependencies`, `run_map_tests` | the engine's `MapInfo` / `TestReport` JSON |
|
|
35
|
+
| `Interscript::WasmEngine.version` | the crate version that built the module |
|
|
36
|
+
|
|
37
|
+
Not covered (the Regexp executor keeps them): `transliterate_each` /
|
|
38
|
+
`each: true` (enumerating alternatives), and the synthesis of a *reverse*
|
|
39
|
+
map from its forward (`un-Latn-Grek-…` for `un-ell-Grek-Latn-…`): a name
|
|
40
|
+
the load path does not carry is `Interscript::MapNotFoundError`, on purpose.
|
|
41
|
+
|
|
42
|
+
## How maps reach the engine
|
|
43
|
+
|
|
44
|
+
Maps are ISC **source text**, read from the map resolution the gem already
|
|
45
|
+
has — `Interscript.locate` over the `interscript-maps` gem's load path
|
|
46
|
+
(`Interscript.map_locations`; `INTERSCRIPT_STAGING` adds the staging maps).
|
|
47
|
+
No map data ships in this gem; the data channel is the `interscript-maps`
|
|
48
|
+
gem, which is built from the maps repository's GitHub Release
|
|
49
|
+
(`interscript-maps-isc-<version>.tar.gz` + sha256 sidecar).
|
|
50
|
+
|
|
51
|
+
The closure of a map — every map its `dependency "x"` directives reach,
|
|
52
|
+
transitively — is walked **by the engine's own answers**: the loader calls
|
|
53
|
+
`iscrs_load_map`, and when a dependency is not loaded the engine names
|
|
54
|
+
every one that is missing (`Engine::load_map`, interscript-rs); the loader
|
|
55
|
+
resolves each name to a source, loads it the same way, and calls again
|
|
56
|
+
until the map loads. Nothing in Ruby parses a map on this path — the
|
|
57
|
+
engine parses each source exactly once. A dependency the load path cannot
|
|
58
|
+
resolve is `Interscript::MapNotFoundError` naming it; a chain that closes
|
|
59
|
+
on itself is `Interscript::MapLogicError` with the chain.
|
|
60
|
+
|
|
61
|
+
Maps accumulate in one process-wide runtime (`Interscript::WasmEngine.runtime`);
|
|
62
|
+
loading an existing name replaces it, so the second call on a map costs no
|
|
63
|
+
parse. `Interscript::WasmEngine.reset_runtime!` drops the runtime and every
|
|
64
|
+
loaded map with it (after the load path changes at runtime, or in tests).
|
|
65
|
+
|
|
66
|
+
Errors are the engine's, raised as the Ruby class the Regexp executor uses
|
|
67
|
+
for the same condition: `Interscript::MapLogicError` (a missing stage, a
|
|
68
|
+
cycle), `Interscript::SystemConversionError` (`unsupported:` constructs),
|
|
69
|
+
`Interscript::MapNotFoundError`, `Interscript::Isc::ParseError`.
|
|
70
|
+
|
|
71
|
+
## The vendored module
|
|
72
|
+
|
|
73
|
+
`lib/interscript/wasm/engine_capi.wasm` (2 535 491 bytes) is built from
|
|
74
|
+
interscript-rs at a pinned commit; `lib/interscript/wasm/PROVENANCE.md`
|
|
75
|
+
records the commit, the toolchain, the sha256 and the six-import object the
|
|
76
|
+
host must satisfy. `spec/interscript/wasm/provenance_spec.rb` pins the bytes
|
|
77
|
+
to that digest; rebuild only from the pinned commit, and treat a bump as an
|
|
78
|
+
engine change that needs the full parity suite re-run.
|
|
79
|
+
|
|
80
|
+
`Interscript::Wasm::Runtime` is the C-ABI host: wasmtime-rb over the module,
|
|
81
|
+
the import object (every import the module declares, matched by the stable
|
|
82
|
+
part of its name — only `__wbg_getRandomValues` is ever reached, once per
|
|
83
|
+
runtime, to seed parsanol's hashers; the host writes a fixed pattern), the
|
|
84
|
+
memory contract (`iscrs_alloc` inputs, the 8-byte out cell, `iscrs_free`
|
|
85
|
+
once, memory base never cached across calls) and the status codes.
|
|
86
|
+
|
|
87
|
+
The module compiles with cranelift at `:speed` (the reference host's
|
|
88
|
+
setting): a one-time cost of a few seconds per process on a laptop, bought
|
|
89
|
+
back on any multi-row run. `Interscript::Wasm::Runtime.new(cranelift_opt_level: :none)`
|
|
90
|
+
trades it the other way.
|
|
91
|
+
|
|
92
|
+
## Packaging: wasmtime-rb
|
|
93
|
+
|
|
94
|
+
The engine is opt-in, so `wasmtime ~> 50.0` is a **soft dependency**: the
|
|
95
|
+
gemspec does not declare it, `gem install interscript` does not bring it
|
|
96
|
+
in, and the first use of `Interscript::WasmEngine` names what to add. Add it
|
|
97
|
+
to the application's Gemfile:
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
gem "wasmtime", "~> 50.0"
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
(The gemspec declares it only as a development dependency, for the specs.)
|
|
104
|
+
|
|
105
|
+
wasmtime-rb ships **precompiled** gems (the C extension is already built)
|
|
106
|
+
for: `x86_64-linux`, `x86_64-linux-musl`, `aarch64-linux`,
|
|
107
|
+
`aarch64-linux-musl`, `arm64-darwin`, `x64-mingw-ucrt`, `aarch64-mingw-ucrt`
|
|
108
|
+
(Ruby >= 3.3, < 4.1) — every platform this gem's CI matrix runs on. Bundler
|
|
109
|
+
and `gem install` pick the one for the host automatically. Any other
|
|
110
|
+
platform (an uncommon Ruby build, a CPU outside that list) falls back to
|
|
111
|
+
the `ruby` platform gem, which **builds from source**: it needs the Rust
|
|
112
|
+
toolchain (`cargo` on `PATH`) and several minutes. That fallback is the
|
|
113
|
+
documented build-from-source path; there is no third option.
|
|
114
|
+
|
|
115
|
+
A lockfile that must cover several platforms lists them explicitly
|
|
116
|
+
(`bundle lock --add-platform x86_64-linux`, …) — see the wasmtime-rb README.
|
|
117
|
+
The version is pinned to the 50.x line because wasmtime-rb bumps its major
|
|
118
|
+
with every wasmtime release; moving to another line is an owner decision.
|
|
119
|
+
|
|
120
|
+
## Memory
|
|
121
|
+
|
|
122
|
+
wasm linear memory never shrinks, and the parse arena scales with the
|
|
123
|
+
largest map loaded in the process: roughly 150–250 bytes of linear memory
|
|
124
|
+
per byte of the largest map source, plus ~2 MiB (interscript-rs
|
|
125
|
+
`tools/wasm/README.md`, "Size budget"). Typical maps (sources under ~40 KiB)
|
|
126
|
+
land at 10–55 MiB; the largest corpus map (`acadsin-zho-Hani-Latn-2002`,
|
|
127
|
+
839 KiB) needs ~123 MiB on its own. Load the maps a process actually serves.
|
|
128
|
+
|
|
129
|
+
## Parity
|
|
130
|
+
|
|
131
|
+
The cross-runtime contract is the interscript-rs golden capture
|
|
132
|
+
(`tests/golden/isc-wf0.jsonl`, 16 322 rows / 453 maps, `iscrs capture`).
|
|
133
|
+
`spec/wasm_engine_parity_spec.rb` replays it through BOTH engines this gem
|
|
134
|
+
carries over the same map sources and requires, per row: wasm === golden,
|
|
135
|
+
Regexp === golden, wasm === Regexp.
|
|
136
|
+
|
|
137
|
+
* `bundle exec rspec` — the recorded subset when the golden is present
|
|
138
|
+
(first 20 rows of each of the 10 parity maps), skipped otherwise;
|
|
139
|
+
* the `wasm-parity` job (every PR) — the subset against the golden;
|
|
140
|
+
* the `wasm-parity-full` workflow (nightly, `gh workflow run wasm-parity-full.yml`)
|
|
141
|
+
— `PARITY_FULL=1`: every row, every map.
|
|
142
|
+
|
|
143
|
+
The golden is located as the TypeScript adapter locates it (a sibling
|
|
144
|
+
`../interscript-rs` checkout) or through `INTERSCRIPT_GOLDEN`; the maps come
|
|
145
|
+
from the `interscript-maps` gem, with `INTERSCRIPT_MAPS_PATH` (path-separated
|
|
146
|
+
directories) prepending others.
|
|
147
|
+
|
|
148
|
+
A delta is reported verbatim, map by map, and fails the build; the gate is
|
|
149
|
+
never relaxed to let a deprecation through.
|
data/interscript.gemspec
CHANGED
|
@@ -36,4 +36,10 @@ Gem::Specification.new do |spec|
|
|
|
36
36
|
spec.add_dependency "interscript-maps", "~> 3.0"
|
|
37
37
|
spec.add_dependency "text"
|
|
38
38
|
spec.add_dependency "parsanol", ">= 1.5.1", "< 2"
|
|
39
|
+
# The wasm engine's runtime (Interscript::WasmEngine) is opt-in, so its
|
|
40
|
+
# wasmtime-rb runtime is a SOFT dependency: the gem installs without it and
|
|
41
|
+
# the first use of WasmEngine says what to add (docs/wasm-engine.md).
|
|
42
|
+
# The specs run against the 50.x line — wasmtime-rb bumps its major with
|
|
43
|
+
# every wasmtime release and the vendored module's host surface follows it.
|
|
44
|
+
spec.add_development_dependency "wasmtime", "~> 50.0"
|
|
39
45
|
end
|
data/lib/interscript/command.rb
CHANGED
|
@@ -14,8 +14,10 @@ module Interscript
|
|
|
14
14
|
|
|
15
15
|
def translit(input)
|
|
16
16
|
compiler = if options[:compiler]
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
name = options[:compiler].split("::").last
|
|
18
|
+
# An engine autoloaded by Interscript (Interpreter, WasmEngine) needs
|
|
19
|
+
# no require; a compiler variant lives under interscript/compiler/.
|
|
20
|
+
require "interscript/compiler/#{name.downcase}" unless Interscript.const_defined?(name.to_sym, false)
|
|
19
21
|
Object.const_get(options[:compiler])
|
|
20
22
|
else
|
|
21
23
|
Interscript::Interpreter
|
|
@@ -71,7 +71,10 @@ module Interscript
|
|
|
71
71
|
# alternatives — rs/ts/py give compact line:col pointers.
|
|
72
72
|
MAX_EXPECTED = 6
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
# `filename` and `source` are optional: an engine that reports a
|
|
75
|
+
# parse failure of a source it parsed itself (the wasm engine) has
|
|
76
|
+
# the message and the name, not the Ruby-side parse context.
|
|
77
|
+
def initialize(message, filename: nil, source: nil, cause: nil)
|
|
75
78
|
@filename = filename
|
|
76
79
|
@source = source
|
|
77
80
|
@cause = cause
|
data/lib/interscript/isc.rb
CHANGED
data/lib/interscript/ml/imf.rb
CHANGED
|
@@ -14,7 +14,7 @@ module Interscript::ML
|
|
|
14
14
|
EOS_ID = 1
|
|
15
15
|
UNK_ID = 2
|
|
16
16
|
|
|
17
|
-
DEFAULT_INDEX_URL = "https://github.com/interscript/interscript-models/releases/download/index-
|
|
17
|
+
DEFAULT_INDEX_URL = "https://github.com/interscript/interscript-models/releases/download/index-v20/models-index.yaml"
|
|
18
18
|
|
|
19
19
|
class FormatError < StandardError; end
|
|
20
20
|
class RegistryError < StandardError; end
|
data/lib/interscript/version.rb
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# PROVENANCE — `engine_capi.wasm`
|
|
2
|
+
|
|
3
|
+
The Interscript map engine ([interscript-rs](https://github.com/interscript/interscript-rs))
|
|
4
|
+
compiled to WebAssembly behind its C ABI (`src/capi.rs`, feature `capi`), vendored
|
|
5
|
+
into this gem as the payload `Interscript::WasmEngine` runs under wasmtime-rb.
|
|
6
|
+
It is the SAME engine the `iscrs` CLI and the npm `@interscript/engine-wasm`
|
|
7
|
+
package run — there is no second engine and no Ruby transliteration logic
|
|
8
|
+
behind it.
|
|
9
|
+
|
|
10
|
+
| | |
|
|
11
|
+
| --- | --- |
|
|
12
|
+
| source repository | `https://github.com/interscript/interscript-rs` |
|
|
13
|
+
| source commit | `b6811e6042196aa26106f30eb59377952b8dda27` (main, interscript-rs#64 merged) |
|
|
14
|
+
| crate version (`iscrs_version`) | `0.4.2` |
|
|
15
|
+
| build script | `tools/wasm/build.sh` (step 3: `cargo build --release --target wasm32-unknown-unknown --lib --no-default-features --features capi`, then `wasm-opt -Oz`) |
|
|
16
|
+
| rustc | `1.98.0 (88d9e12ae 2026-08-18)` (rustup toolchain `1.98.0-aarch64-apple-darwin`) |
|
|
17
|
+
| wasm-opt | binaryen `version 117` (`-Oz`; the copy wasm-pack 0.13.1 downloads) |
|
|
18
|
+
| built | 2026-12-03 |
|
|
19
|
+
| file | `lib/interscript/wasm/engine_capi.wasm` |
|
|
20
|
+
| size | 2 535 491 bytes |
|
|
21
|
+
| sha256 | `067dfb7db5ec31473ed1300cfe94481976966fa6425924f2b91a9748c13a2667` |
|
|
22
|
+
|
|
23
|
+
The sha256 is the one the interscript-rs C-ABI PR reported for the module built
|
|
24
|
+
from that commit (its `tools/wasm/README.md`, "Size budget"), reproduced
|
|
25
|
+
byte-for-byte by a clean build on 2026-12-03. `spec/interscript/wasm/provenance_spec.rb`
|
|
26
|
+
pins the vendored bytes to this digest, so a rebuilt or edited module cannot
|
|
27
|
+
ship silently: a new module means a new row here.
|
|
28
|
+
|
|
29
|
+
## Rebuilding
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
git clone https://github.com/interscript/interscript-rs && cd interscript-rs
|
|
33
|
+
git checkout b6811e6042196aa26106f30eb59377952b8dda27
|
|
34
|
+
rustup toolchain install 1.98.0 && rustup target add wasm32-unknown-unknown --toolchain 1.98.0
|
|
35
|
+
RUSTUP_TOOLCHAIN=1.98.0 tools/wasm/build.sh out # builds both modules
|
|
36
|
+
shasum -a 256 out/capi/engine_capi.wasm # must equal the sha256 above
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Rebuild ONLY from the pinned commit. Bumping the commit is an owner decision:
|
|
40
|
+
it changes the engine every `Interscript::WasmEngine` call runs, so it needs the
|
|
41
|
+
full parity suite (the nightly `wasm-parity-full` workflow) re-recorded against
|
|
42
|
+
the new bytes before it lands.
|
|
43
|
+
|
|
44
|
+
## The import object
|
|
45
|
+
|
|
46
|
+
The module is not import-free: parsanol 1.5.6 enables `getrandom/wasm_js` on
|
|
47
|
+
wasm32, so the wasm-bindgen runtime comes along. It declares six imports;
|
|
48
|
+
`Interscript::Wasm::Runtime` defines every import the module declares, by the
|
|
49
|
+
STABLE part of its name (the `_<hash>` suffix is wasm-bindgen's and moves with
|
|
50
|
+
its version), and counts which are reached:
|
|
51
|
+
|
|
52
|
+
| import | signature | reached from the `iscrs_*` exports |
|
|
53
|
+
| --- | --- | --- |
|
|
54
|
+
| `__wbindgen_placeholder__.__wbg_getRandomValues_<hash>` | `(i32, i32)` | yes — once per bundle load; the host writes `len` bytes at `ptr` (outputs do not depend on them) |
|
|
55
|
+
| `__wbindgen_placeholder__.__wbindgen_describe` | `(i32)` | no (no-op) |
|
|
56
|
+
| `__wbindgen_placeholder__.__wbindgen_object_drop_ref` | `(i32)` | no (no-op) |
|
|
57
|
+
| `__wbindgen_externref_xform__.__wbindgen_externref_table_grow` | `(i32) -> i32` | no (returns `-1`) |
|
|
58
|
+
| `__wbindgen_externref_xform__.__wbindgen_externref_table_set_null` | `(i32)` | no (no-op) |
|
|
59
|
+
| `__wbindgen_externref_xform__.__wbindgen_externref_statics_end` | `() -> i32` | no (returns `0`) |
|
|
60
|
+
|
|
61
|
+
Making the module import-free is a dependency decision in interscript-rs
|
|
62
|
+
(parsanol's `wasm_js` feature), not something this gem works around.
|
|
63
|
+
|
|
64
|
+
## Memory contract (what the host implements)
|
|
65
|
+
|
|
66
|
+
From the `src/capi.rs` module documentation and `tests/wasm_capi.rs`, the
|
|
67
|
+
reference host:
|
|
68
|
+
|
|
69
|
+
* 32-bit pointers/lengths, lengths in bytes. Inputs: `iscrs_alloc(len)`, write
|
|
70
|
+
the bytes, call, `iscrs_free(ptr, len)`.
|
|
71
|
+
* Every string export writes `(ptr, len)` through its last two parameters — an
|
|
72
|
+
8-byte `iscrs_alloc` cell serves both (`p` and `p + 4`); copy the bytes out,
|
|
73
|
+
then `iscrs_free(ptr, len)` exactly once. Empty output = null pointer, 0.
|
|
74
|
+
* Allocations grow linear memory: never cache the memory base across calls.
|
|
75
|
+
* Status: `0` ok, `1` partial bundle load, `-1` invalid argument, `-2` engine
|
|
76
|
+
error (`iscrs_last_error`), `-3` caught panic (on wasm32 a panic is a trap).
|
|
77
|
+
* UTF-8 in, UTF-8 out; invalid input bytes become U+FFFD.
|
|
78
|
+
|
|
79
|
+
License: BSD-3-Clause (the interscript-rs crate's).
|
|
Binary file
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Map resolution for the wasm engine: names to ISC SOURCE texts over the
|
|
4
|
+
# map resolution the gem already has — `Interscript.locate` over the
|
|
5
|
+
# interscript-maps gem's load path (`Interscript.map_locations`). The
|
|
6
|
+
# engine parses the sources; this class only finds them. No wasm, and no
|
|
7
|
+
# Ruby parsing: the engine is asked what a map needs, and names every
|
|
8
|
+
# dependency it is missing (the `iscrs_load_map` contract), so the host
|
|
9
|
+
# never re-implements the grammar to walk a closure.
|
|
10
|
+
module Interscript::Wasm::Loader
|
|
11
|
+
# Load `map_name` and its transitive dependencies into `runtime`,
|
|
12
|
+
# dependencies first. Names already loaded are left as they are.
|
|
13
|
+
# Returns the names in load order (dependencies first, `map_name` last).
|
|
14
|
+
#
|
|
15
|
+
# runtime.load_map("bgnpcgn-ukr-Cyrl-Latn-2019", source)
|
|
16
|
+
# # => the engine: needs "un-ukr-Cyrl-Latn-2012" first
|
|
17
|
+
# runtime.load_map("un-ukr-Cyrl-Latn-2012", source)
|
|
18
|
+
# # => the engine: needs "ua-ukr-Cyrl-Latn-2010" first ...
|
|
19
|
+
#
|
|
20
|
+
# A dependency the load path cannot resolve is `Interscript::MapNotFoundError`
|
|
21
|
+
# naming it; a chain that closes on itself is a `MapLogicError` with the
|
|
22
|
+
# chain.
|
|
23
|
+
def self.load_closure(runtime, map_name, chain = [])
|
|
24
|
+
return [] if runtime.has_map?(map_name)
|
|
25
|
+
|
|
26
|
+
if chain.include?(map_name)
|
|
27
|
+
raise Interscript::MapLogicError, "dependency cycle: #{(chain + [map_name]).join(" -> ")}"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
loaded = []
|
|
31
|
+
loop do
|
|
32
|
+
missing = runtime.load_map(map_name, source(map_name))
|
|
33
|
+
break if missing.nil?
|
|
34
|
+
|
|
35
|
+
missing.each do |dependency|
|
|
36
|
+
loaded.concat(load_closure(runtime, dependency, chain + [map_name]))
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
loaded << map_name
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# The ISC source of a map on the load path.
|
|
43
|
+
def self.source(map_name)
|
|
44
|
+
File.read(locate(map_name), encoding: "UTF-8")
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# `Interscript.locate` (load path + map aliases), minus what the engine
|
|
48
|
+
# cannot run: the legacy Ruby-DSL `.imp` maps.
|
|
49
|
+
def self.locate(map_name)
|
|
50
|
+
path = Interscript.locate(map_name)
|
|
51
|
+
if path.end_with?(".imp")
|
|
52
|
+
raise Interscript::SystemConversionError,
|
|
53
|
+
"#{map_name} is a legacy .imp map (Ruby DSL); the wasm engine runs .isc and .iml only"
|
|
54
|
+
end
|
|
55
|
+
path
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
begin
|
|
4
|
+
require "wasmtime"
|
|
5
|
+
rescue LoadError => e
|
|
6
|
+
message = "Interscript::WasmEngine needs the wasmtime gem (an opt-in engine, so it is " \
|
|
7
|
+
"not a dependency of interscript). Add `gem \"wasmtime\", \"~> 50.0\"` to your " \
|
|
8
|
+
"Gemfile (or `gem install wasmtime`) — see docs/wasm-engine.md."
|
|
9
|
+
raise LoadError, message, cause: e
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# The C-ABI host: wasmtime-rb over the vendored `engine_capi.wasm`
|
|
13
|
+
# (PROVENANCE.md next to it records the build). One instance is one wasmtime
|
|
14
|
+
# store holding one engine handle from `iscrs_engine_new`; every public
|
|
15
|
+
# method is one `iscrs_*` export call under the memory contract. There is
|
|
16
|
+
# no Interscript logic here — the engine is the module's.
|
|
17
|
+
module Interscript::Wasm
|
|
18
|
+
class Runtime
|
|
19
|
+
# The status codes of interscript-rs src/capi.rs.
|
|
20
|
+
OK = 0
|
|
21
|
+
PARTIAL = 1
|
|
22
|
+
INVALID_ARGUMENT = -1
|
|
23
|
+
ENGINE_ERROR = -2
|
|
24
|
+
PANIC = -3
|
|
25
|
+
|
|
26
|
+
# The seventeen exports of the C ABI.
|
|
27
|
+
EXPORTS = %w[
|
|
28
|
+
iscrs_version
|
|
29
|
+
iscrs_engine_new iscrs_engine_free
|
|
30
|
+
iscrs_alloc iscrs_free
|
|
31
|
+
iscrs_load_bundle iscrs_load_map iscrs_last_load_report
|
|
32
|
+
iscrs_transliterate iscrs_transliterate_lang iscrs_transliterate_stage
|
|
33
|
+
iscrs_map_count iscrs_has_map iscrs_map_names iscrs_map_info iscrs_run_map_tests
|
|
34
|
+
iscrs_last_error
|
|
35
|
+
].freeze
|
|
36
|
+
|
|
37
|
+
# How an engine error surfaces in Ruby. interscript-rs formats every
|
|
38
|
+
# error with a kind prefix (src/error.rs, Display); the host reads the
|
|
39
|
+
# text back through `iscrs_last_error` and raises the class the Regexp
|
|
40
|
+
# executor raises for the same condition, with the prefix stripped so
|
|
41
|
+
# the messages compare.
|
|
42
|
+
ERROR_CLASSES = {
|
|
43
|
+
"map logic error: " => "Interscript::MapLogicError",
|
|
44
|
+
"unsupported: " => "Interscript::SystemConversionError",
|
|
45
|
+
"couldn't locate map " => "Interscript::MapNotFoundError",
|
|
46
|
+
"parse failed in " => "Interscript::Isc::ParseError"
|
|
47
|
+
}.freeze
|
|
48
|
+
|
|
49
|
+
# `getRandomValues` seeds parsanol's hashers; the engine's outputs do
|
|
50
|
+
# not depend on the bytes (every parity leg in interscript-rs holds
|
|
51
|
+
# that line), so the host writes a fixed pattern instead of reaching
|
|
52
|
+
# for a random source.
|
|
53
|
+
SEED = "\x9E7y\xB9\x7FJ|\x15".b.freeze
|
|
54
|
+
|
|
55
|
+
# The imports the module declared — `[module, name]` pairs — every
|
|
56
|
+
# one of which the host defines.
|
|
57
|
+
attr_reader :imports
|
|
58
|
+
# Which imported functions the module reached, and how often. The
|
|
59
|
+
# reference host (tests/wasm_capi.rs) records the same: only
|
|
60
|
+
# `getRandomValues` is ever called from the `iscrs_*` exports.
|
|
61
|
+
attr_reader :import_calls
|
|
62
|
+
|
|
63
|
+
# `cranelift_opt_level` defaults to `:speed`, the reference host's
|
|
64
|
+
# configuration: a few seconds of compile, bought back on any
|
|
65
|
+
# multi-row run. `:none` compiles faster and runs slower.
|
|
66
|
+
def initialize(module_path = Interscript::Wasm::MODULE_PATH, cranelift_opt_level: :speed)
|
|
67
|
+
@wasmtime = Wasmtime::Engine.new(cranelift_opt_level: cranelift_opt_level)
|
|
68
|
+
@module = Wasmtime::Module.from_file(@wasmtime, module_path)
|
|
69
|
+
@import_calls = Hash.new(0)
|
|
70
|
+
@imports = @module.imports.map { |imp| [imp["module"], imp["name"]] }.freeze
|
|
71
|
+
@linker = define_imports
|
|
72
|
+
@store = Wasmtime::Store.new(@wasmtime)
|
|
73
|
+
@instance = @linker.instantiate(@store, @module)
|
|
74
|
+
@memory = @instance.export("memory").to_memory
|
|
75
|
+
@funcs = EXPORTS.to_h { |name| [name.to_sym, @instance.export(name).to_func] }
|
|
76
|
+
# The out-parameter cell every string export writes (ptr, len) into:
|
|
77
|
+
# 8 host-owned bytes, reused for the runtime's lifetime.
|
|
78
|
+
@cell = call(:iscrs_alloc, 8)
|
|
79
|
+
# One engine per thread, no re-entrancy (the contract). The GVL
|
|
80
|
+
# serializes MRI; the mutex covers the rest.
|
|
81
|
+
@mutex = Mutex.new
|
|
82
|
+
@handle = call(:iscrs_engine_new)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The crate version that built the module (`iscrs_version`).
|
|
86
|
+
def version
|
|
87
|
+
string_export { call(:iscrs_version, @cell, @cell + 4) }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# The one recoverable outcome of `iscrs_load_map`, in the engine's
|
|
91
|
+
# own words (interscript-rs src/engine.rs, Engine::load_map): the map
|
|
92
|
+
# declares dependencies that are not loaded, every one of them named.
|
|
93
|
+
# Pinned by spec/interscript/wasm/runtime_spec.rb; the module is
|
|
94
|
+
# vendored at a pinned commit (PROVENANCE.md), so the text moves only
|
|
95
|
+
# with a module bump — which re-runs the parity suite.
|
|
96
|
+
MISSING_DEPENDENCIES = /needs dependencies that are not loaded: (.*?)(?: — |\z)/m
|
|
97
|
+
|
|
98
|
+
# Load one ISC source under `name` (`iscrs_load_map`). Returns nil when
|
|
99
|
+
# the map loaded; returns the names of its not-yet-loaded dependencies
|
|
100
|
+
# when that is the failure — the Loader loads them and calls again.
|
|
101
|
+
# Any other failure raises.
|
|
102
|
+
def load_map(name, source)
|
|
103
|
+
synchronize do
|
|
104
|
+
rc = with_input(name) do |np, nl|
|
|
105
|
+
with_input(source) { |sp, sl| call(:iscrs_load_map, @handle, np, nl, sp, sl) }
|
|
106
|
+
end
|
|
107
|
+
return nil if rc == OK
|
|
108
|
+
|
|
109
|
+
message = raw_last_error
|
|
110
|
+
missing = MISSING_DEPENDENCIES.match(message)
|
|
111
|
+
return missing[1].split(", ") if missing
|
|
112
|
+
|
|
113
|
+
raise_engine_error(message)
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Load a bundle document (`iscrs_load_bundle`): `1` (PARTIAL) when the
|
|
118
|
+
# document was valid but some maps did not build — `last_load_report`
|
|
119
|
+
# says which.
|
|
120
|
+
def load_bundle(json)
|
|
121
|
+
synchronize { check! with_input(json) { |ptr, len| call(:iscrs_load_bundle, @handle, ptr, len) } }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The JSON `LoadReport` of the last bundle load; "" when there was none.
|
|
125
|
+
def last_load_report
|
|
126
|
+
string_export { call(:iscrs_last_load_report, @handle, @cell, @cell + 4) }
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# `Interscript.transliterate(map, text)`: stage `main`, no language.
|
|
130
|
+
def transliterate(name, text)
|
|
131
|
+
string_export do
|
|
132
|
+
with_input(name) do |np, nl|
|
|
133
|
+
with_input(text) { |tp, tl| call(:iscrs_transliterate, @handle, np, nl, tp, tl, @cell, @cell + 4) }
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# ISC-WF-1 W3 language selection (`iscrs_transliterate_lang`): a nil
|
|
139
|
+
# language is the null selection — every `when` guard inert.
|
|
140
|
+
def transliterate_lang(name, text, language)
|
|
141
|
+
string_export do
|
|
142
|
+
with_input(name) do |np, nl|
|
|
143
|
+
with_input(text) do |tp, tl|
|
|
144
|
+
with_optional_input(language) do |lp, ll|
|
|
145
|
+
call(:iscrs_transliterate_lang, @handle, np, nl, tp, tl, lp, ll, @cell, @cell + 4)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# One named stage (`iscrs_transliterate_stage`), optional language.
|
|
153
|
+
def transliterate_stage(name, stage, text, language: nil)
|
|
154
|
+
string_export do
|
|
155
|
+
with_input(name) do |np, nl|
|
|
156
|
+
with_input(stage.to_s) do |sp, sl|
|
|
157
|
+
with_input(text) do |tp, tl|
|
|
158
|
+
with_optional_input(language) do |lp, ll|
|
|
159
|
+
call(:iscrs_transliterate_stage, @handle, np, nl, sp, sl, tp, tl, lp, ll, @cell, @cell + 4)
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# How many maps are loaded (dependencies included).
|
|
168
|
+
def map_count
|
|
169
|
+
synchronize { call(:iscrs_map_count, @handle) }
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
def has_map?(name)
|
|
173
|
+
synchronize do
|
|
174
|
+
rc = with_input(name) { |np, nl| call(:iscrs_has_map, @handle, np, nl) }
|
|
175
|
+
raise_engine_error if rc.negative?
|
|
176
|
+
rc == 1
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# JSON: the names of every loaded map.
|
|
181
|
+
def map_names
|
|
182
|
+
string_export { call(:iscrs_map_names, @handle, @cell, @cell + 4) }
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# JSON: the `MapInfo` of a loaded map (the engine's error when not loaded).
|
|
186
|
+
def map_info(name)
|
|
187
|
+
string_export { with_input(name) { |np, nl| call(:iscrs_map_info, @handle, np, nl, @cell, @cell + 4) } }
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# JSON: the map's own embedded tests as a `TestReport`.
|
|
191
|
+
def run_map_tests(name)
|
|
192
|
+
string_export { with_input(name) { |np, nl| call(:iscrs_run_map_tests, @handle, np, nl, @cell, @cell + 4) } }
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# The message of the last failed call on the engine.
|
|
196
|
+
def last_error
|
|
197
|
+
string_export { call(:iscrs_last_error, @handle, @cell, @cell + 4) }
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
# Release the engine handle (`iscrs_engine_free`); the runtime is
|
|
201
|
+
# closed afterwards.
|
|
202
|
+
def close
|
|
203
|
+
synchronize do
|
|
204
|
+
call(:iscrs_engine_free, @handle)
|
|
205
|
+
@handle = nil
|
|
206
|
+
nil
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def closed?
|
|
211
|
+
@handle.nil?
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
private
|
|
215
|
+
|
|
216
|
+
def synchronize(&)
|
|
217
|
+
raise Interscript::Wasm::ClosedError, "the wasm runtime is closed" if @handle.nil?
|
|
218
|
+
@mutex.synchronize(&)
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def call(export, *args)
|
|
222
|
+
@funcs[export].call(*args)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# A string-returning export under the mutex: the block makes the call
|
|
226
|
+
# (writing `(ptr, len)` into the cell); a failure code raises the
|
|
227
|
+
# engine's message, otherwise the bytes are copied out and the module's
|
|
228
|
+
# buffer released.
|
|
229
|
+
def string_export
|
|
230
|
+
synchronize do
|
|
231
|
+
check! yield
|
|
232
|
+
take_string
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
# The input protocol: `iscrs_alloc(len)`, write the UTF-8 bytes, yield
|
|
237
|
+
# `(ptr, len)`, `iscrs_free(ptr, len)` on the way out. A nil/empty
|
|
238
|
+
# input is the null pointer with length zero.
|
|
239
|
+
def with_input(text)
|
|
240
|
+
bytes = text.to_s.b
|
|
241
|
+
return yield(0, 0) if bytes.empty?
|
|
242
|
+
ptr = call(:iscrs_alloc, bytes.bytesize)
|
|
243
|
+
@memory.write(ptr, bytes)
|
|
244
|
+
begin
|
|
245
|
+
yield ptr, bytes.bytesize
|
|
246
|
+
ensure
|
|
247
|
+
call(:iscrs_free, ptr, bytes.bytesize)
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# The optional-language parameter: nil is the null selection.
|
|
252
|
+
def with_optional_input(text)
|
|
253
|
+
text.nil? ? yield(0, 0) : with_input(text.to_s) { |ptr, len| yield ptr, len }
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# The output protocol: read the `(ptr, len)` the last call wrote into
|
|
257
|
+
# `cell` (little-endian u32 pair), copy the bytes out, release the
|
|
258
|
+
# module's buffer once. Memory is re-read after every call — never cached.
|
|
259
|
+
def take_string(cell = @cell)
|
|
260
|
+
ptr, len = @memory.read(cell, 8).unpack("V2")
|
|
261
|
+
return "" if ptr.zero?
|
|
262
|
+
@memory.read(ptr, len).force_encoding(Encoding::UTF_8)
|
|
263
|
+
ensure
|
|
264
|
+
call(:iscrs_free, ptr, len) if ptr&.nonzero?
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# Raise on a failure code; PARTIAL (a tolerated bundle outcome) passes.
|
|
268
|
+
def check!(rc)
|
|
269
|
+
raise_engine_error if rc.negative? || rc == PANIC
|
|
270
|
+
rc
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def raise_engine_error(message = raw_last_error)
|
|
274
|
+
ERROR_CLASSES.each do |prefix, klass|
|
|
275
|
+
next unless message.start_with?(prefix)
|
|
276
|
+
raise Object.const_get(klass), message.delete_prefix(prefix)
|
|
277
|
+
end
|
|
278
|
+
raise Interscript::MapLogicError, message
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# The engine's message for its last failed call — its Display text
|
|
282
|
+
# with the kind prefix of interscript-rs src/error.rs — read without
|
|
283
|
+
# `check!`, so a failure to read it does not recurse.
|
|
284
|
+
def raw_last_error
|
|
285
|
+
rc = call(:iscrs_last_error, @handle, @cell, @cell + 4)
|
|
286
|
+
rc.negative? ? "engine error (code #{rc})" : take_string
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# The import object, exactly (tools/wasm/README.md, "The import object,
|
|
290
|
+
# exactly"): every import the module declares, defined with its
|
|
291
|
+
# declared type, matched by the STABLE part of its name — the
|
|
292
|
+
# `_<hash>` suffix is wasm-bindgen's and moves with its version.
|
|
293
|
+
def define_imports
|
|
294
|
+
linker = Wasmtime::Linker.new(@wasmtime)
|
|
295
|
+
@module.imports.each do |imp|
|
|
296
|
+
type = imp["type"].to_func_type
|
|
297
|
+
name = imp["name"]
|
|
298
|
+
linker.func_new(imp["module"], name, type.params, type.results) do |caller, *args|
|
|
299
|
+
@import_calls[name] += 1
|
|
300
|
+
stub(role(name), caller, args, type.results.length)
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
linker
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def role(name)
|
|
307
|
+
if name.include?("getRandomValues") then :random
|
|
308
|
+
elsif name.include?("__wbindgen_throw") then :throw
|
|
309
|
+
elsif name.include?("externref_table_grow") then :table_grow
|
|
310
|
+
else :noop
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# One stub per role. Everything the engine path needs is `random`
|
|
315
|
+
# (parsanol's hasher seeding); the rest are inert for a host that never
|
|
316
|
+
# hands the module a JS value.
|
|
317
|
+
def stub(role, caller, args, result_count)
|
|
318
|
+
case role
|
|
319
|
+
when :random
|
|
320
|
+
# `crypto.getRandomValues(view)`: the last two arguments are
|
|
321
|
+
# (ptr, len) in every wasm-bindgen shape — write `len` bytes at `ptr`.
|
|
322
|
+
ptr, len = args.last(2)
|
|
323
|
+
caller.export("memory").to_memory.write(ptr, (SEED * ((len + SEED.bytesize - 1) / SEED.bytesize)).byteslice(0, len)) if len.positive?
|
|
324
|
+
result_count.zero? ? nil : 0
|
|
325
|
+
when :throw
|
|
326
|
+
ptr, len = args
|
|
327
|
+
message = caller.export("memory").to_memory.read(ptr, len)
|
|
328
|
+
raise Interscript::Wasm::HostError, "wasm-bindgen throw: #{message.force_encoding(Encoding::UTF_8)}"
|
|
329
|
+
when :table_grow
|
|
330
|
+
-1
|
|
331
|
+
else
|
|
332
|
+
result_count.zero? ? nil : 0
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
module Interscript
|
|
4
|
+
# The wasmtime embed of the interscript-rs engine (the C ABI of
|
|
5
|
+
# `src/capi.rs`, vendored as `wasm/engine_capi.wasm` — see
|
|
6
|
+
# `wasm/PROVENANCE.md` for the build it came from). Two parts, one
|
|
7
|
+
# responsibility each:
|
|
8
|
+
#
|
|
9
|
+
# * {Runtime} — the C-ABI host: the import object, the memory contract,
|
|
10
|
+
# one method per `iscrs_*` export. No Interscript logic.
|
|
11
|
+
# * {Loader} — map resolution over `Interscript.locate`: the dependency
|
|
12
|
+
# closure of a map as ISC sources, dependencies first. No wasm.
|
|
13
|
+
#
|
|
14
|
+
# {Interscript::WasmEngine} composes them into the engine interface
|
|
15
|
+
# (`compiler: Interscript::WasmEngine`).
|
|
16
|
+
module Wasm
|
|
17
|
+
# The vendored module. Shipped in the gem (see PROVENANCE.md next to it).
|
|
18
|
+
MODULE_PATH = File.expand_path("wasm/engine_capi.wasm", __dir__)
|
|
19
|
+
|
|
20
|
+
# A call on a runtime whose engine handle was released (#close).
|
|
21
|
+
class ClosedError < StandardError; end
|
|
22
|
+
# The module reached a wasm-bindgen JS import a host cannot satisfy
|
|
23
|
+
# (`__wbindgen_throw`); the reference host surfaces it the same way.
|
|
24
|
+
class HostError < StandardError; end
|
|
25
|
+
|
|
26
|
+
autoload :Runtime, "interscript/wasm/runtime"
|
|
27
|
+
autoload :Loader, "interscript/wasm/loader"
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
# The wasm engine behind the compiler interface. `compiler:
|
|
6
|
+
# Interscript::WasmEngine` on `Interscript.transliterate` / `.load` /
|
|
7
|
+
# `.transliterate_file` (or `-c Interscript::WasmEngine` on the CLI) runs
|
|
8
|
+
# the interscript-rs engine in-process: the vendored `engine_capi.wasm`
|
|
9
|
+
# under wasmtime-rb (`Interscript::Wasm`), fed ISC sources by
|
|
10
|
+
# `Interscript::Wasm::Loader`. There is no transliteration logic in this
|
|
11
|
+
# class — a call is one engine call.
|
|
12
|
+
#
|
|
13
|
+
# Interscript.transliterate("iso-rus-Cyrl-Latn-9-2026", "Кириллица",
|
|
14
|
+
# compiler: Interscript::WasmEngine)
|
|
15
|
+
#
|
|
16
|
+
# Maps accumulate in one process-wide runtime (loading a name replaces it),
|
|
17
|
+
# so the second call on a map costs no parse. Introspection the engine
|
|
18
|
+
# exposes rides along: {#map_info}, {#stage_names}, {#run_map_tests}.
|
|
19
|
+
class Interscript::WasmEngine < Interscript::Compiler
|
|
20
|
+
include Interscript::LanguageAware
|
|
21
|
+
|
|
22
|
+
# The process-wide runtime: one wasmtime store, one engine handle.
|
|
23
|
+
def self.runtime
|
|
24
|
+
@runtime ||= Interscript::Wasm::Runtime.new
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Drop the process-wide runtime (and every loaded map with it). For
|
|
28
|
+
# tests, and after the load path or the map set changes at runtime.
|
|
29
|
+
def self.reset_runtime!
|
|
30
|
+
@runtime&.close
|
|
31
|
+
@runtime = nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Compiler interface: a map NAME (or a "a|b" composition) compiles to an
|
|
35
|
+
# engine over the map's dependency closure. A Ruby document node cannot
|
|
36
|
+
# be loaded — the engine takes ISC source text.
|
|
37
|
+
def self.call(map_name)
|
|
38
|
+
unless String === map_name
|
|
39
|
+
raise Interscript::SystemConversionError,
|
|
40
|
+
"the wasm engine loads ISC sources by map name, not #{map_name.class} nodes"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
new.compile(map_name)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Load every map of the closure that is not loaded yet, dependencies
|
|
47
|
+
# first (the Loader asks the engine what each map needs). Names already
|
|
48
|
+
# in the runtime are left as they are.
|
|
49
|
+
def compile(map_name)
|
|
50
|
+
@map_names = map_name.split("|").map(&:strip)
|
|
51
|
+
@runtime = self.class.runtime
|
|
52
|
+
@map_names.each { |name| Interscript::Wasm::Loader.load_closure(@runtime, name) }
|
|
53
|
+
self
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Transliterate `str` through the map: stage `main` by default, no
|
|
57
|
+
# language selection unless `language:` names one (ISC-WF-1 W3).
|
|
58
|
+
#
|
|
59
|
+
# `each: true` (the `transliterate_each` enumeration of alternatives) is
|
|
60
|
+
# not an engine surface: it stays on the Regexp executor.
|
|
61
|
+
def call(str, stage = :main, each: false, language: nil)
|
|
62
|
+
if each
|
|
63
|
+
raise Interscript::SystemConversionError,
|
|
64
|
+
"the wasm engine does not enumerate alternatives (each:); use Interscript::Interpreter"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
text = utf8(str)
|
|
68
|
+
if @map_names.length == 1
|
|
69
|
+
transliterate(@map_names.first, text, stage, language)
|
|
70
|
+
else
|
|
71
|
+
compose(text, stage)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# The engine's `MapInfo` of the map (name, systemCode, stages, rulesets,
|
|
76
|
+
# dependencies, languages, tests) as a Hash.
|
|
77
|
+
def map_info
|
|
78
|
+
JSON.parse(@runtime.map_info(@map_names.last))
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# The stages `transliterate_stage` accepts.
|
|
82
|
+
def stage_names
|
|
83
|
+
map_info["stages"]
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# The dependency documents the engine resolved the map against (the
|
|
87
|
+
# names `iscrs_load_map` required before the map itself).
|
|
88
|
+
def map_dependencies
|
|
89
|
+
map_info["dependencies"]
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The map's own embedded tests as the engine's `TestReport`.
|
|
93
|
+
def run_map_tests
|
|
94
|
+
JSON.parse(@runtime.run_map_tests(@map_names.first))
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# The crate version that built the embedded module.
|
|
98
|
+
def self.version
|
|
99
|
+
runtime.version
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
private
|
|
103
|
+
|
|
104
|
+
# One map: `iscrs_transliterate` for the `main` stage without a
|
|
105
|
+
# language, `iscrs_transliterate_lang` with one, `iscrs_transliterate_stage`
|
|
106
|
+
# for a named stage.
|
|
107
|
+
def transliterate(name, text, stage, language)
|
|
108
|
+
return @runtime.transliterate(name, text) if stage.to_s == "main" && language.nil?
|
|
109
|
+
return @runtime.transliterate_lang(name, text, language) if stage.to_s == "main"
|
|
110
|
+
|
|
111
|
+
@runtime.transliterate_stage(name, stage, text, language: language)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# "a|b" composes as the DSL does: a virtual map running a's main stage,
|
|
115
|
+
# then b's. The virtual map has no other stage and no `languages`
|
|
116
|
+
# declaration, so a selection is inert on it.
|
|
117
|
+
def compose(text, stage)
|
|
118
|
+
unless stage.to_s == "main"
|
|
119
|
+
raise Interscript::MapLogicError, %(stage "#{stage}" not found in #{@map_names.join("|")})
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
@map_names.reduce(text) { |str, name| @runtime.transliterate(name, str) }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Strings cross the boundary as UTF-8. A binary string is passed as its
|
|
126
|
+
# bytes (the engine replaces invalid sequences with U+FFFD, as the JS
|
|
127
|
+
# facade's TextEncoder does); any other encoding is transcoded.
|
|
128
|
+
def utf8(str)
|
|
129
|
+
encoding = str.encoding
|
|
130
|
+
return str if encoding == Encoding::UTF_8 || encoding == Encoding::ASCII_8BIT
|
|
131
|
+
|
|
132
|
+
str.encode(Encoding::UTF_8)
|
|
133
|
+
end
|
|
134
|
+
end
|
data/lib/interscript.rb
CHANGED
|
@@ -13,6 +13,8 @@ module Interscript
|
|
|
13
13
|
autoload :Detector, "interscript/detector"
|
|
14
14
|
autoload :Isc, "interscript/isc"
|
|
15
15
|
autoload :ML, "interscript/ml"
|
|
16
|
+
autoload :Wasm, "interscript/wasm"
|
|
17
|
+
autoload :WasmEngine, "interscript/wasm_engine"
|
|
16
18
|
|
|
17
19
|
# An error caused by a lack of some map
|
|
18
20
|
class MapNotFoundError < StandardError; end
|
|
@@ -22,6 +24,9 @@ module Interscript
|
|
|
22
24
|
class SystemConversionError < StandardError; end
|
|
23
25
|
# An error caused by an incorrect map implementation
|
|
24
26
|
class MapLogicError < StandardError; end
|
|
27
|
+
# Marker for engines that honour the ISC-WF-1 W3 `language:` selection
|
|
28
|
+
# of Interscript.transliterate (the Interpreter and the WasmEngine).
|
|
29
|
+
module LanguageAware; end
|
|
25
30
|
|
|
26
31
|
class << self
|
|
27
32
|
def load_path
|
|
@@ -60,11 +65,11 @@ module Interscript
|
|
|
60
65
|
compiled = load(system_code, maps, compiler: compiler)
|
|
61
66
|
if language.nil?
|
|
62
67
|
compiled.call(string)
|
|
63
|
-
elsif
|
|
68
|
+
elsif compiled.is_a?(Interscript::LanguageAware)
|
|
64
69
|
compiled.call(string, language: language)
|
|
65
70
|
else
|
|
66
71
|
raise ArgumentError,
|
|
67
|
-
"language selection is only supported by Interscript::Interpreter, not #{compiler}"
|
|
72
|
+
"language selection is only supported by Interscript::Interpreter and Interscript::WasmEngine, not #{compiler}"
|
|
68
73
|
end
|
|
69
74
|
end
|
|
70
75
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: interscript
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.8.
|
|
4
|
+
version: 3.8.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-12-
|
|
11
|
+
date: 2026-12-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: thor
|
|
@@ -72,6 +72,20 @@ dependencies:
|
|
|
72
72
|
- - "<"
|
|
73
73
|
- !ruby/object:Gem::Version
|
|
74
74
|
version: '2'
|
|
75
|
+
- !ruby/object:Gem::Dependency
|
|
76
|
+
name: wasmtime
|
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - "~>"
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '50.0'
|
|
82
|
+
type: :development
|
|
83
|
+
prerelease: false
|
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - "~>"
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '50.0'
|
|
75
89
|
description: Interoperable script conversion systems.
|
|
76
90
|
email:
|
|
77
91
|
- open.source@ribose.com
|
|
@@ -100,6 +114,7 @@ files:
|
|
|
100
114
|
- docs/samples/rus-Cyrl.txt
|
|
101
115
|
- docs/samples/thai.txt
|
|
102
116
|
- docs/utils/regexp_examples.txt
|
|
117
|
+
- docs/wasm-engine.md
|
|
103
118
|
- exe/codemod-imp-to-isc
|
|
104
119
|
- exe/diagnose_parse_failures
|
|
105
120
|
- exe/interscript
|
|
@@ -205,6 +220,12 @@ files:
|
|
|
205
220
|
- lib/interscript/visualize/json.rb
|
|
206
221
|
- lib/interscript/visualize/map.html.erb
|
|
207
222
|
- lib/interscript/visualize/nodes.rb
|
|
223
|
+
- lib/interscript/wasm.rb
|
|
224
|
+
- lib/interscript/wasm/PROVENANCE.md
|
|
225
|
+
- lib/interscript/wasm/engine_capi.wasm
|
|
226
|
+
- lib/interscript/wasm/loader.rb
|
|
227
|
+
- lib/interscript/wasm/runtime.rb
|
|
228
|
+
- lib/interscript/wasm_engine.rb
|
|
208
229
|
homepage: https://www.interscript.com
|
|
209
230
|
licenses:
|
|
210
231
|
- BSD-2-Clause
|