cbqn 0.2.3

Embed BQN in Rust via CBQN FFI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#![allow(non_snake_case)]

use super::{
    bindings::{self, BQNV},
    Error, Result,
};
use crate::BQNValue;
use std::{cell::UnsafeCell, io::Read, mem, num::TryFromIntError, sync::LazyLock};
use wasmer::*;
use wasmer_wasix::{virtual_fs, Pipe, WasiEnv};

#[inline]
pub fn backend_eval(bqn: &str) -> Result<BQNValue> {
    Ok(BQNValue::new(bqn_eval(BQNValue::from(bqn).value)?))
}

macro_rules! impl_error(($err:ty) => {
    impl From<$err> for Error {
        fn from(_e: $err) -> Error {
            let stderr = {
                let _l = crate::LOCK.lock();
                BQNFFI.stderr_unsafe()
            };
            Error::CBQN(stderr)
        }
    }
});

impl_error!(MemoryAccessError);
impl_error!(RuntimeError);
impl_error!(TryFromIntError);

struct BqnFfi {
    free: TypedFunction<WasmPtr<u32>, ()>,
    malloc: TypedFunction<u32, WasmPtr<u32>>,

    bqn_bound: TypedFunction<BQNV, u32>,
    bqn_call1: TypedFunction<(BQNV, BQNV), BQNV>,
    bqn_call2: TypedFunction<(BQNV, BQNV, BQNV), BQNV>,
    bqn_copy: TypedFunction<BQNV, BQNV>,
    bqn_directArrType: TypedFunction<BQNV, u32>,
    bqn_free: TypedFunction<BQNV, ()>,
    bqn_getField: TypedFunction<(BQNV, BQNV), BQNV>,
    bqn_hasField: TypedFunction<(BQNV, BQNV), i32>,
    bqn_eval: TypedFunction<BQNV, BQNV>,
    bqn_init: TypedFunction<(), ()>,
    bqn_makeChar: TypedFunction<u32, BQNV>,
    bqn_makeF64: TypedFunction<f64, BQNV>,
    bqn_makeF64Vec: TypedFunction<(u32, WasmPtr<u32>), BQNV>,
    bqn_makeI32Vec: TypedFunction<(u32, WasmPtr<u32>), BQNV>,
    bqn_makeI16Vec: TypedFunction<(u32, WasmPtr<u32>), BQNV>,
    bqn_makeI8Vec: TypedFunction<(u32, WasmPtr<u32>), BQNV>,
    bqn_makeObjVec: TypedFunction<(u32, WasmPtr<u32>), BQNV>,
    bqn_makeUTF8Str: TypedFunction<(u32, WasmPtr<u32>), BQNV>,
    bqn_pick: TypedFunction<(BQNV, u32), BQNV>,
    bqn_readC32Arr: TypedFunction<(BQNV, WasmPtr<u32>), ()>,
    bqn_readChar: TypedFunction<BQNV, u32>,
    bqn_readF64: TypedFunction<BQNV, f64>,
    bqn_readF64Arr: TypedFunction<(BQNV, WasmPtr<u32>), ()>,
    bqn_readObjArr: TypedFunction<(BQNV, WasmPtr<u32>), ()>,
    bqn_type: TypedFunction<BQNV, i32>,
    bqn_rank: TypedFunction<BQNV, u32>,
    bqn_shape: TypedFunction<(BQNV, WasmPtr<u32>), ()>,

    store: UnsafeCell<Store>,
    stderr: UnsafeCell<Pipe>,
    memory: Memory,
}

impl BqnFfi {
    /// NOTE: This is highly unsafe. The caller *must* make sure there's only one &mut Store reference
    /// active at any point of execution, or otherwise Rust treats the code as undefined behavior.
    /// However, in practice, the cbqn-sys wrapper to the C shared object is also not thread-safe and
    /// the implementation in cbqn-rs crate does locking in a way that this code cannot be
    /// multi-threaded in practice.
    fn get_store_unsafe(&self) -> &mut Store {
        unsafe { self.store.get().as_mut().unwrap() }
    }

    fn stderr_unsafe(&self) -> String {
        let stderr = unsafe { self.stderr.get().as_mut().unwrap() };
        let mut ret = String::new();
        stderr.read_to_string(&mut ret).unwrap_or(0);
        ret
    }
}
/// NOTE: The type is not really Sync. The user of this library must make sure this code is not
/// going to be run in a multi-threaded context.
unsafe impl Sync for BqnFfi {}

macro_rules! wasmfn(($instance:ident, $store:ident, $name:expr) => {
    $instance.exports.get_typed_function(&$store, $name).expect($name)
});

static WASM_BYTES: &'static [u8] = include_bytes!(env!("BQN_WASM"));

static BQNFFI: LazyLock<BqnFfi> = LazyLock::new(|| {
    let compiler_config = Cranelift::default();
    let mut store = Store::new(compiler_config);
    let module = Module::new(&store, WASM_BYTES).expect("Create module");

    let (tx, rx) = Pipe::channel();
    let mut wasi_env = WasiEnv::builder("cbqn")
        .fs(Box::new(virtual_fs::host_fs::FileSystem))
        .stderr(Box::new(tx))
        .preopen_dir("/")
        .unwrap()
        .finalize(&mut store)
        .expect("Create WasiEnv");

    let import_object = wasi_env
        .import_object(&mut store, &module)
        .expect("Get import object");

    let instance = Instance::new(&mut store, &module, &import_object).expect("Create instance");
    wasi_env
        .initialize(&mut store, instance.clone())
        .expect("Initialize wasi_env");

    instance
        .exports
        .get_function("_initialize")
        .expect("Get _initialize function")
        .call(&mut store, &[])
        .expect("Initialize wasm module");

    BqnFfi {
        free: wasmfn!(instance, store, "free"),
        malloc: wasmfn!(instance, store, "malloc"),

        bqn_bound: wasmfn!(instance, store, "bqn_bound"),
        bqn_call1: wasmfn!(instance, store, "bqn_call1"),
        bqn_call2: wasmfn!(instance, store, "bqn_call2"),
        bqn_copy: wasmfn!(instance, store, "bqn_copy"),
        bqn_directArrType: wasmfn!(instance, store, "bqn_directArrType"),
        bqn_free: wasmfn!(instance, store, "bqn_free"),
        bqn_getField: wasmfn!(instance, store, "bqn_getField"),
        bqn_hasField: wasmfn!(instance, store, "bqn_hasField"),
        bqn_eval: wasmfn!(instance, store, "bqn_eval"),
        bqn_init: wasmfn!(instance, store, "bqn_init"),
        bqn_makeChar: wasmfn!(instance, store, "bqn_makeChar"),
        bqn_makeF64: wasmfn!(instance, store, "bqn_makeF64"),
        bqn_makeF64Vec: wasmfn!(instance, store, "bqn_makeF64Vec"),
        bqn_makeI32Vec: wasmfn!(instance, store, "bqn_makeI32Vec"),
        bqn_makeI16Vec: wasmfn!(instance, store, "bqn_makeI16Vec"),
        bqn_makeI8Vec: wasmfn!(instance, store, "bqn_makeI8Vec"),
        bqn_makeObjVec: wasmfn!(instance, store, "bqn_makeObjVec"),
        bqn_makeUTF8Str: wasmfn!(instance, store, "bqn_makeUTF8Str"),
        bqn_pick: wasmfn!(instance, store, "bqn_pick"),
        bqn_readC32Arr: wasmfn!(instance, store, "bqn_readC32Arr"),
        bqn_readChar: wasmfn!(instance, store, "bqn_readChar"),
        bqn_readF64: wasmfn!(instance, store, "bqn_readF64"),
        bqn_readF64Arr: wasmfn!(instance, store, "bqn_readF64Arr"),
        bqn_readObjArr: wasmfn!(instance, store, "bqn_readObjArr"),
        bqn_type: wasmfn!(instance, store, "bqn_type"),
        bqn_rank: wasmfn!(instance, store, "bqn_rank"),
        bqn_shape: wasmfn!(instance, store, "bqn_shape"),

        store: UnsafeCell::new(store),
        stderr: UnsafeCell::new(rx.with_blocking(false)),
        memory: instance
            .exports
            .get_memory("memory")
            .expect("Get WASM memory")
            .clone(),
    }
});

fn with_buf<BT, T, F: FnMut(&[BT], &mut Store, WasmPtr<u32>) -> Result<T>>(
    buf: &[BT],
    mut f: F,
) -> Result<T> {
    let store = BQNFFI.get_store_unsafe();
    let ptr = BQNFFI
        .malloc
        .call(store, (buf.len() * mem::size_of::<BT>()).try_into()?)?;

    let ret = f(buf, store, ptr)?;

    BQNFFI.free.call(store, ptr)?;

    Ok(ret)
}

fn with_buf_mut<BT, T, F: FnMut(&mut [BT], &mut Store, WasmPtr<u32>) -> Result<T>>(
    buf: &mut [BT],
    mut f: F,
) -> Result<T> {
    let store = BQNFFI.get_store_unsafe();
    let ptr = BQNFFI
        .malloc
        .call(store, (buf.len() * mem::size_of::<BT>()).try_into()?)?;

    let ret = f(buf, store, ptr)?;

    BQNFFI.free.call(store, ptr)?;

    Ok(ret)
}

pub fn bqn_bound(v: BQNV) -> Result<u32> {
    Ok(BQNFFI.bqn_bound.call(BQNFFI.get_store_unsafe(), v)?)
}

pub fn bqn_call1(f: BQNV, x: BQNV) -> Result<BQNV> {
    Ok(BQNFFI.bqn_call1.call(BQNFFI.get_store_unsafe(), f, x)?)
}

pub fn bqn_call2(f: BQNV, w: BQNV, x: BQNV) -> Result<BQNV> {
    Ok(BQNFFI.bqn_call2.call(BQNFFI.get_store_unsafe(), f, w, x)?)
}

pub fn bqn_copy(v: BQNV) -> Result<BQNV> {
    Ok(BQNFFI.bqn_copy.call(BQNFFI.get_store_unsafe(), v)?)
}

pub fn bqn_directArrType(v: BQNV) -> Result<u32> {
    Ok(BQNFFI
        .bqn_directArrType
        .call(BQNFFI.get_store_unsafe(), v)?)
}

pub fn bqn_eval(v: BQNV) -> Result<BQNV> {
    Ok(BQNFFI.bqn_eval.call(BQNFFI.get_store_unsafe(), v)?)
}

pub fn bqn_free(v: BQNV) -> Result<()> {
    Ok(BQNFFI.bqn_free.call(BQNFFI.get_store_unsafe(), v)?)
}

pub fn bqn_getField(ns: BQNV, name: BQNV) -> Result<BQNV> {
    Ok(BQNFFI
        .bqn_getField
        .call(BQNFFI.get_store_unsafe(), ns, name)?)
}

pub fn bqn_hasField(ns: BQNV, name: BQNV) -> Result<bool> {
    Ok(BQNFFI
        .bqn_hasField
        .call(BQNFFI.get_store_unsafe(), ns, name)?
        != 0)
}

pub fn bqn_init() -> Result<()> {
    Ok(BQNFFI.bqn_init.call(BQNFFI.get_store_unsafe())?)
}

pub fn bqn_makeBoundFn1(_f: bindings::bqn_boundFn1, _obj: BQNV) -> Result<BQNV> {
    Err(Error::NotSupported(
        "BoundFns are not supported with WASI backend".into(),
    ))
}

pub fn bqn_makeBoundFn2(_f: bindings::bqn_boundFn2, _obj: BQNV) -> Result<BQNV> {
    Err(Error::NotSupported(
        "BoundFns are not supported with WASI backend".into(),
    ))
}

pub fn bqn_makeChar(c: u32) -> Result<BQNV> {
    Ok(BQNFFI.bqn_makeChar.call(BQNFFI.get_store_unsafe(), c)?)
}

pub fn bqn_makeF64(d: f64) -> Result<BQNV> {
    Ok(BQNFFI.bqn_makeF64.call(BQNFFI.get_store_unsafe(), d)?)
}

pub fn bqn_makeF64Vec(a: &[f64]) -> Result<BQNV> {
    with_buf(a, |buf, store, ptr| {
        let mem = BQNFFI.memory.view(store);
        let f64ptr: WasmPtr<f64> = ptr.cast();
        f64ptr.slice(&mem, buf.len().try_into()?)?.write_slice(a)?;

        Ok(BQNFFI
            .bqn_makeF64Vec
            .call(store, a.len().try_into().unwrap(), ptr)?)
    })
}

pub fn bqn_makeI32Vec(a: &[i32]) -> Result<BQNV> {
    with_buf(a, |buf, store, ptr| {
        let mem = BQNFFI.memory.view(store);
        let i32ptr: WasmPtr<i32> = ptr.cast();
        i32ptr.slice(&mem, buf.len().try_into()?)?.write_slice(a)?;

        Ok(BQNFFI
            .bqn_makeI32Vec
            .call(store, a.len().try_into().unwrap(), ptr)?)
    })
}

pub fn bqn_makeI16Vec(a: &[i16]) -> Result<BQNV> {
    with_buf(a, |buf, store, ptr| {
        let mem = BQNFFI.memory.view(store);
        let i16ptr: WasmPtr<i16> = ptr.cast();
        i16ptr.slice(&mem, buf.len().try_into()?)?.write_slice(a)?;

        Ok(BQNFFI
            .bqn_makeI16Vec
            .call(store, a.len().try_into().unwrap(), ptr)?)
    })
}

pub fn bqn_makeI8Vec(a: &[i8]) -> Result<BQNV> {
    with_buf(a, |buf, store, ptr| {
        let mem = BQNFFI.memory.view(store);
        let i8ptr: WasmPtr<i8> = ptr.cast();
        i8ptr.slice(&mem, buf.len().try_into()?)?.write_slice(a)?;

        Ok(BQNFFI
            .bqn_makeI8Vec
            .call(store, a.len().try_into().unwrap(), ptr)?)
    })
}

pub fn bqn_makeObjVec(a: &[BQNV]) -> Result<BQNV> {
    with_buf(a, |buf, store, ptr| {
        let mem = BQNFFI.memory.view(store);
        let objptr: WasmPtr<BQNV> = ptr.cast();
        objptr.slice(&mem, buf.len().try_into()?)?.write_slice(a)?;

        Ok(BQNFFI
            .bqn_makeObjVec
            .call(store, a.len().try_into().unwrap(), ptr)?)
    })
}

pub fn bqn_makeUTF8Str(s: &str) -> Result<BQNV> {
    with_buf(s.as_bytes(), |buf, store, ptr| {
        let mem = BQNFFI.memory.view(store);
        mem.write(ptr.offset().into(), buf)?;

        Ok(BQNFFI.bqn_makeUTF8Str.call(store, buf.len() as u32, ptr)?)
    })
}

pub fn bqn_pick(v: BQNV, pos: usize) -> Result<BQNV> {
    Ok(BQNFFI
        .bqn_pick
        .call(BQNFFI.get_store_unsafe(), v, pos as u32)?)
}

pub fn bqn_readC32Arr(v: BQNV, buf: &mut [u32]) -> Result<()> {
    with_buf_mut(buf, |buf, store, ptr| {
        BQNFFI.bqn_readC32Arr.call(store, v, ptr)?;
        let mem = BQNFFI.memory.view(store);
        Ok(ptr.slice(&mem, buf.len().try_into()?)?.read_slice(buf)?)
    })?;
    Ok(())
}

pub fn bqn_readChar(v: BQNV) -> Result<u32> {
    Ok(BQNFFI.bqn_readChar.call(BQNFFI.get_store_unsafe(), v)?)
}

pub fn bqn_readF64(v: BQNV) -> Result<f64> {
    Ok(BQNFFI.bqn_readF64.call(BQNFFI.get_store_unsafe(), v)?)
}

pub fn bqn_readF64Arr(v: BQNV, buf: &mut [f64]) -> Result<()> {
    with_buf_mut(buf, |buf, store, ptr| {
        BQNFFI.bqn_readF64Arr.call(store, v, ptr)?;
        let ptr: WasmPtr<f64> = ptr.cast();
        let mem = BQNFFI.memory.view(store);
        ptr.slice(&mem, buf.len().try_into()?)?.read_slice(buf)?;
        Ok(())
    })?;
    Ok(())
}

pub fn bqn_readObjArr(v: BQNV, buf: &mut [BQNV]) -> Result<()> {
    with_buf_mut(buf, |buf, store, ptr| {
        BQNFFI.bqn_readObjArr.call(store, v, ptr)?;
        let ptr: WasmPtr<BQNV> = ptr.cast();
        let mem = BQNFFI.memory.view(store);
        Ok(ptr.slice(&mem, buf.len().try_into()?)?.read_slice(buf)?)
    })?;
    Ok(())
}

pub fn bqn_type(v: BQNV) -> Result<i32> {
    Ok(BQNFFI.bqn_type.call(BQNFFI.get_store_unsafe(), v)?)
}

pub fn bqn_rank(v: BQNV) -> Result<usize> {
    Ok(BQNFFI.bqn_rank.call(BQNFFI.get_store_unsafe(), v)? as usize)
}

pub fn bqn_shape(v: BQNV, buf: &mut [usize]) -> Result<()> {
    // In 32-bit WASI, usize is u32 so we need convert the values back and forth
    let mut shape: Vec<u32> = Vec::with_capacity(buf.len());
    unsafe {
        shape.set_len(buf.len());
    }

    with_buf_mut(&mut shape, |buf, store, ptr| {
        BQNFFI.bqn_shape.call(store, v, ptr)?;
        let ptr: WasmPtr<u32> = ptr.cast();
        let mem = BQNFFI.memory.view(store);
        Ok(ptr.slice(&mem, buf.len().try_into()?)?.read_slice(buf)?)
    })?;

    for i in 0..buf.len() {
        buf[i] = shape[i] as usize;
    }

    Ok(())
}