hebi 0.4.0

A dynamic scripting language
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#![allow(clippy::new_without_default)]

#[macro_use]
mod macros;

use std::cell::RefMut;
use std::fmt::{Debug, Display};
use std::future::Future;
use std::marker::PhantomData;
use std::ops::Deref;
use std::pin::Pin;

use futures_util::TryFutureExt;

use self::value::FromValuePack;
use crate::internal::error::{Error, Result};
use crate::internal::object::function::Disassembly;
use crate::internal::object::native::NativeClassInstance;
use crate::internal::object::{table, Ptr, Type};
use crate::internal::value::Value as OwnedValue;
use crate::internal::vm;
use crate::internal::vm::global::{Input, Output};
use crate::internal::vm::thread::{Args, Slot0, Thread};
use crate::internal::vm::{global, Config, Vm};
use crate::Cow;

// public API
pub mod module;
pub mod object;
pub mod value;

pub use crate::fail;
pub use crate::internal::object::module::ModuleLoader;
pub use crate::internal::object::native::LocalBoxFuture;
pub use crate::public::module::NativeModule;
pub use crate::public::object::list::List;
pub use crate::public::object::string::Str;
pub use crate::public::object::table::Table;
pub use crate::public::object::Any;
pub use crate::public::value::{FromValue, IntoValue, Value};

#[derive(Default)]
pub struct Hebi {
  vm: Vm,
}

// # Safety
// The VM uses reference counting similar to `Rc`, but without weak references.
// Reference counts are *not* atomic, which means that the VM is not thread
// safe. To make it safe to implement `Send`, we completely lock down the public
// API of the VM to ensure very limited access to values. Values are never given
// out as *owned*, they are always *borrowed*. This means that thread safety is
// ensured via the borrow checker as opposed to a `!Send` bound.
//
// In summary:
// - User cannot obtain owned `Rc<T>` from the VM
// - User cannot clone the VM and move it to another thread
//
// Thus it should be safe even if the reference counts are not atomic, as they
// will never be accessed from two or more threads at the same time.
unsafe impl Send for Hebi {}

struct ForceSendFuture<F: Future<Output = Result<OwnedValue>>> {
  fut: F,
}
impl<F: Future<Output = Result<OwnedValue>>> ForceSendFuture<F> {
  pub unsafe fn new(fut: F) -> Self {
    Self { fut }
  }
}
unsafe impl<F: Future<Output = Result<OwnedValue>>> Send for ForceSendFuture<F> {}
impl<F> Future for ForceSendFuture<F>
where
  F: Future<Output = Result<OwnedValue>>,
{
  type Output = F::Output;

  fn poll(
    self: std::pin::Pin<&mut Self>,
    cx: &mut std::task::Context<'_>,
  ) -> std::task::Poll<Self::Output> {
    let this = unsafe { self.get_unchecked_mut() };
    let fut = unsafe { Pin::new_unchecked(&mut this.fut) };
    fut.poll(cx)
  }
}

pub struct HebiBuilder<M, I, O> {
  module_loader: Option<Box<dyn crate::internal::object::module::ModuleLoader>>,
  input: Option<Box<dyn crate::internal::vm::global::Input>>,
  output: Option<Box<dyn crate::internal::vm::global::Output>>,
  __: PhantomData<(M, I, O)>,
}

pub struct HasModuleLoader {
  __: (),
}
impl<I, O> HebiBuilder<(), I, O> {
  pub fn module_loader(
    self,
    module_loader: impl ModuleLoader + 'static,
  ) -> HebiBuilder<HasModuleLoader, I, O> {
    HebiBuilder {
      module_loader: Some(Box::new(module_loader)),
      input: self.input,
      output: self.output,
      __: PhantomData,
    }
  }
}

pub struct HasInput {
  __: (),
}
impl<M, O> HebiBuilder<M, (), O> {
  pub fn input(self, input: impl Input + 'static) -> HebiBuilder<M, HasInput, O> {
    HebiBuilder {
      module_loader: self.module_loader,
      input: Some(Box::new(input)),
      output: self.output,
      __: PhantomData,
    }
  }
}

pub struct HasOutput {
  __: (),
}
impl<M, I> HebiBuilder<M, I, ()> {
  pub fn output(self, output: impl Output + 'static) -> HebiBuilder<M, I, HasOutput> {
    HebiBuilder {
      module_loader: self.module_loader,
      input: self.input,
      output: Some(Box::new(output)),
      __: PhantomData,
    }
  }
}

impl<M, I, O> HebiBuilder<M, I, O> {
  pub fn finish(self) -> Hebi {
    Hebi {
      vm: Vm::with_config(Config {
        module_loader: self.module_loader,
        input: self.input,
        output: self.output,
      }),
    }
  }
}

impl Hebi {
  pub fn new() -> Self {
    Self { vm: Vm::default() }
  }

  pub fn builder() -> HebiBuilder<(), (), ()> {
    HebiBuilder {
      module_loader: None,
      input: None,
      output: None,
      __: PhantomData,
    }
  }

  pub fn eval<'cx, 'src>(&'cx mut self, code: &'src str) -> Result<Value<'cx>>
  where
    'src: 'cx,
  {
    pollster::block_on(self.eval_async(code))
  }

  pub fn eval_async<'cx, 'src>(
    &'cx mut self,
    code: &'src str,
  ) -> impl Future<Output = Result<Value<'cx>>> + Send + 'cx
  where
    'src: 'cx,
  {
    let fut = self.vm.eval(code);
    unsafe { ForceSendFuture::new(fut) }.map_ok(|value| unsafe { value.bind_raw::<'cx>() })
  }

  pub fn compile<'cx>(&self, code: &str) -> Result<Chunk<'cx>> {
    self.vm.compile(code).map(|chunk| Chunk {
      inner: chunk,
      lifetime: PhantomData,
    })
  }

  pub fn run<'cx>(&'cx mut self, chunk: Chunk<'cx>) -> Result<Value<'cx>> {
    pollster::block_on(self.run_async(chunk))
  }

  pub fn run_async<'cx>(
    &'cx mut self,
    chunk: Chunk<'cx>,
  ) -> impl Future<Output = Result<Value<'cx>>> + Send + 'cx {
    let fut = self.vm.entry(chunk.inner);
    unsafe { ForceSendFuture::new(fut) }.map_ok(|value| unsafe { value.bind_raw::<'cx>() })
  }

  pub fn global(&self) -> Global {
    Global {
      inner: self.vm.root.global.clone(),
      lifetime: PhantomData,
    }
  }

  pub fn register(&mut self, module: &NativeModule) {
    self.vm.register(module)
  }
}

impl Debug for Hebi {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_tuple("Hebi").field(&self.vm).finish()
  }
}

#[derive(Clone)]
pub struct Chunk<'cx> {
  pub(crate) inner: vm::Chunk,
  pub(crate) lifetime: PhantomData<&'cx ()>,
}

impl<'cx> Chunk<'cx> {
  pub fn disassemble(&self) -> Disassembly {
    self.inner.disassemble()
  }
}

#[derive(Clone)]
pub struct Global<'cx> {
  pub(crate) inner: global::Global,
  pub(crate) lifetime: PhantomData<&'cx ()>,
}

impl<'cx> Global<'cx> {
  pub fn get(&self, key: &str) -> Option<Value<'cx>> {
    self
      .inner
      .get(key)
      .map(|value| unsafe { value.bind_raw::<'cx>() })
  }

  pub fn set(&self, key: Str<'cx>, value: Value<'cx>) {
    self.inner.set(key.unbind(), value.unbind());
  }

  pub fn print(&self, f: impl Display) -> Result<()> {
    write!(&mut self.inner.io().output.borrow_mut(), "{f}").map_err(Error::user)
  }

  pub fn println(&self, f: impl Display) -> Result<()> {
    writeln!(&mut self.inner.io().output.borrow_mut(), "{f}").map_err(Error::user)
  }

  pub fn output(&mut self) -> RefMut<'_, dyn Output> {
    RefMut::map(self.inner.io().output.borrow_mut(), |output| {
      output.as_mut()
    })
  }

  pub fn input(&mut self) -> RefMut<'_, dyn Input> {
    RefMut::map(self.inner.io().input.borrow_mut(), |input| input.as_mut())
  }

  pub fn entries<'a>(&'a self) -> GlobalEntries<'a, 'cx> {
    GlobalEntries {
      entries: self.inner.entries(),
      lifetime: PhantomData,
    }
  }
}

pub struct GlobalEntries<'a, 'cx> {
  entries: table::Entries<'a>,
  lifetime: PhantomData<&'cx ()>,
}

impl<'a, 'cx> Iterator for GlobalEntries<'a, 'cx> {
  type Item = (Str<'cx>, Value<'cx>);

  fn next(&mut self) -> Option<Self::Item> {
    self
      .entries
      .next()
      .map(|(key, value)| unsafe { (key.bind_raw::<'cx>(), value.bind_raw::<'cx>()) })
  }
}

#[derive(Clone)]
pub struct Scope<'cx> {
  pub(crate) thread: Thread,
  pub(crate) stack_base: usize,
  pub(crate) args: Args,
  pub(crate) lifetime: PhantomData<&'cx ()>,
}

impl<'cx> Scope<'cx> {
  pub(crate) fn new(parent: &Thread, stack_base: usize, args: Args) -> Self {
    debug_assert!(unsafe { parent.stack.as_ref() }.regs.len() >= args.start + args.count);
    let thread = Thread::new(parent.global.clone(), parent.stack);
    Scope {
      thread,
      stack_base,
      args,
      lifetime: PhantomData,
    }
  }

  pub(crate) fn alloc<T: Type>(&self, v: T) -> Ptr<T> {
    self.thread.global.alloc(v)
  }

  pub(crate) fn intern(
    &self,
    s: impl Into<Cow<'static, str>>,
  ) -> Ptr<crate::internal::object::Str> {
    self.thread.global.intern(s)
  }

  pub fn global(&self) -> Global<'cx> {
    Global {
      inner: self.thread.global.clone(),
      lifetime: PhantomData,
    }
  }

  pub fn num_args(&self) -> usize {
    self.args.count
  }

  pub fn params<T: FromValuePack<'cx>>(&self) -> Result<T::Output> {
    let stack = unsafe { self.thread.stack.as_ref() };
    let range = self.args.start..self.args.start + self.args.count;
    let Some(args) = stack.regs.get(range) else {
      fail!("expected {} args, got {}", T::len(), self.args.count);
    };
    T::from_value_pack(args, self.global())
  }

  pub fn param<T: FromValue<'cx>>(&self, n: usize) -> Result<T> {
    let stack = unsafe { self.thread.stack.as_ref() };
    let index = self.args.start + n;
    let Some(value) = stack.regs.get(index).cloned() else {
      fail!("missing argument {n}");
    };
    let value = unsafe { value.bind_raw::<'cx>() };
    T::from_value(value, self.global())
  }

  // TODO: does this also need to be force-Send?
  pub async fn call<'a>(
    &'a mut self,
    value: Any<'cx>,
    args: &'a [Value<'cx>],
  ) -> Result<Value<'cx>> {
    self
      .thread
      .call(value.unbind(), <_>::unbind_slice(args))
      .await
      .map(|value| unsafe { value.bind_raw::<'cx>() })
  }

  pub(crate) fn consume_args(&mut self, n: usize) {
    self.args.start += n;
    self.args.count -= n;
  }

  pub(crate) fn enter_nested(
    &mut self,
    slot0: Slot0,
    args: Args,
    frame_size: Option<usize>,
  ) -> Scope<'cx> {
    self
      .thread
      .enter_nested_scope(self.stack_base, slot0, args, frame_size)
  }

  pub(crate) fn leave(mut self) {
    self.thread.truncate_stack(self.stack_base);
  }
}

impl<'cx> Global<'cx> {
  pub fn new_instance<T: Send + 'static>(&self, value: T) -> Result<Value<'cx>> {
    let instance = match self.inner.get_type::<T>() {
      Some(ty) => NativeClassInstance {
        instance: Box::new(value),
        class: ty,
      },
      None => fail!("`{}` is not a registered type", std::any::type_name::<T>()),
    };
    let instance = OwnedValue::object(self.inner.alloc(instance));
    Ok(unsafe { instance.bind_raw::<'cx>() })
  }
}

impl<'cx> Scope<'cx> {
  pub fn new_instance<T: Send + 'static>(&self, value: T) -> Result<Value<'cx>> {
    self.global().new_instance(value)
  }
}

impl Hebi {
  pub fn new_instance<T: Send + 'static>(&self, value: T) -> Result<Value> {
    self.global().new_instance(value)
  }
}

pub struct This<'cx, T: Send> {
  pub(crate) inner: Ptr<NativeClassInstance>,
  lifetime: PhantomData<&'cx T>,
}

impl<'cx, T: Send + 'static> This<'cx, T> {
  pub fn new(inner: Ptr<NativeClassInstance>) -> Option<Self> {
    if !inner.instance.is::<T>() {
      return None;
    }
    Some(This {
      inner,
      lifetime: PhantomData,
    })
  }
}

impl<'cx, T: Send + 'static> Deref for This<'cx, T> {
  type Target = T;

  fn deref(&self) -> &Self::Target {
    debug_assert!(self.inner.instance.is::<T>());
    unsafe { self.inner.instance.downcast_ref().unwrap_unchecked() }
  }
}

/// # Safety
/// - `T` must be `#[repr(C)]`
/// - `T` must have only one non-ZST field (`<T as Unbind>::Owned`)
pub(crate) unsafe trait IsSimpleRef: Sized {}

pub(crate) trait Bind: Sized {
  type Ref<'cx>: IsSimpleRef;

  unsafe fn bind_raw<'cx>(self) -> Self::Ref<'cx>;

  fn bind<'cx>(self, global: Global<'cx>) -> Self::Ref<'cx> {
    let _ = global;
    unsafe { Self::bind_raw::<'cx>(self) }
  }

  fn bind_raw_slice<'a, 'cx>(slice: &'a [Self]) -> &'a [Self::Ref<'cx>] {
    unsafe { std::mem::transmute::<&[Self], &[Self::Ref<'cx>]>(slice) }
  }

  fn bind_slice<'a, 'cx>(slice: &'a [Self], global: Global<'cx>) -> &'a [Self::Ref<'cx>] {
    let _ = global;
    Self::bind_raw_slice(slice)
  }
}

pub(crate) trait Unbind: Sized + IsSimpleRef {
  type Owned;

  fn unbind(self) -> Self::Owned;
  fn unbind_slice(slice: &[Self]) -> &[Self::Owned] {
    // Safe due to `IsSimpleRef`
    unsafe { std::mem::transmute::<&[Self], &[Self::Owned]>(slice) }
  }
}