hara-native 0.1.21

HAL-free native host runtime and package launcher for Hara
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! Single-instruction VM dispatch.
//!
//! The outer `Machine::run` loop remains in `machine.rs`; this child only
//! decodes and executes one validated instruction and returns the next
//! control action.

use super::*;

/// Result of executing one instruction. Call actions only carry their
/// collected operands: the nested machine runs from the thin `run` loop
/// after the fat `dispatch` frame has exited, keeping the native stack
/// cost per guest call level small (issue #223).
pub(super) enum Dispatch {
    Next(usize),
    Unwound(usize),
    Call {
        callee: VmSlot,
        args: Vec<VmSlot>,
    },
    CallStatic {
        prototype: u16,
        args: Vec<VmSlot>,
        captures: Vec<VmSlot>,
    },
    CallStaticDirect {
        prototype: u16,
        argc: u8,
    },
    Returned(VmSlot),
    Failed(VmError),
    Suspended(Promise),
    Yielded(Value),
}

impl Machine {
    /// Executes one instruction, returning where the `run` loop
    /// continues. Call instructions only collect their operands into a
    /// [`Dispatch`] action: the actual call happens in `run` after this
    /// frame has exited. The hot dispatch is inlined into the run loop now
    /// that guest calls no longer recurse through the native stack.
    #[inline(always)]
    pub(super) fn dispatch(
        &mut self,
        program: &Rc<Program>,
        function: &FunctionPrototype,
        instruction: &Instruction,
    ) -> Dispatch {
        macro_rules! guarded {
            ($expr:expr) => {
                match $expr {
                    Ok(()) => {}
                    Err(message) => match self.raise(function, message) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    },
                }
            };
        }
        let mut next_ip = self.ip + 1;
        match instruction {
            Instruction::Constant(index) => {
                let Some(value) = program.constants.get(*index as usize) else {
                    match self.raise(function, format!("constant index {index} out of range")) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                };
                self.stack.push(value.clone().into());
            }
            Instruction::Nil => self.stack.push(VmSlot::Nil),
            Instruction::True => self.stack.push(VmSlot::Bool(true)),
            Instruction::False => self.stack.push(VmSlot::Bool(false)),
            Instruction::LoadLocal(slot) => {
                let Some(value) = self.frame.local(*slot) else {
                    match self.raise(function, format!("local slot {slot} out of range")) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                };
                self.stack.push(value.clone());
            }
            Instruction::StoreLocal(slot) => {
                let Some(value) = self.stack.pop() else {
                    match self.raise(function, "stack underflow") {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                };
                if !self.frame.store(*slot, value) {
                    match self.raise(function, format!("local slot {slot} out of range")) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                }
            }
            Instruction::Pop => {
                if self.stack.pop().is_none() {
                    match self.raise(function, "stack underflow") {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                }
            }
            Instruction::Dup => {
                let Some(value) = self.stack.last().cloned() else {
                    return Dispatch::Failed(self.error(function, "stack underflow"));
                };
                self.stack.push(value);
            }
            Instruction::IntrinsicCall { target, argc }
            | Instruction::ProtocolCall { target, argc } => {
                let Some(name) = constant_string(program, *target) else {
                    return Dispatch::Failed(self.error(
                        function,
                        format!("intrinsic target constant {target} is invalid"),
                    ));
                };
                let argc = usize::from(*argc);
                if self.stack.len() < argc {
                    match self.raise(function, "stack underflow") {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                }
                #[cfg(all(feature = "direct-native", not(target_arch = "wasm32")))]
                if matches!(instruction, Instruction::ProtocolCall { .. })
                    || crate::core::IntrinsicOp::from_symbol(name).is_some()
                {
                    crate::direct_native::record_native_target(name);
                }
                if matches!(instruction, Instruction::ProtocolCall { .. })
                    && name == "std.protocol.ideref.IDeref/deref"
                    && argc == 1
                {
                    if let Some(Value::Promise(promise)) =
                        self.stack.last().and_then(VmSlot::runtime_value)
                    {
                        if matches!(promise.state(), PromiseState::Pending) {
                            return Dispatch::Suspended(promise);
                        }
                    }
                }
                let result = if name == "=" && argc == 2 {
                    let right = self.stack.pop().expect("intrinsic arity checked above");
                    let left = self.stack.pop().expect("intrinsic arity checked above");
                    match (left, right) {
                        (VmSlot::Closure(left), VmSlot::Closure(right)) => {
                            Ok(VmSlot::Bool(Rc::ptr_eq(&left, &right)))
                        }
                        (
                            VmSlot::InlineClosure { identity: left, .. },
                            VmSlot::InlineClosure {
                                identity: right, ..
                            },
                        ) => Ok(VmSlot::Bool(left == right)),
                        (VmSlot::MultiArity(left), VmSlot::MultiArity(right)) => {
                            Ok(VmSlot::Bool(Rc::ptr_eq(&left, &right)))
                        }
                        (
                            VmSlot::Closure(_)
                            | VmSlot::InlineClosure { .. }
                            | VmSlot::MultiArity(_),
                            _,
                        )
                        | (
                            _,
                            VmSlot::Closure(_)
                            | VmSlot::InlineClosure { .. }
                            | VmSlot::MultiArity(_),
                        ) => Ok(VmSlot::Bool(false)),
                        (left, right) => {
                            match (left.into_runtime_value(), right.into_runtime_value()) {
                                (Some(left), Some(right)) => {
                                    crate::core::apply_intrinsic_name(name, &[left, right])
                                        .map(VmSlot::from)
                                }
                                _ => Err("= expects values".into()),
                            }
                        }
                    }
                } else {
                    self.scratch.clear();
                    let argument_base = self.stack.len() - argc;
                    for value in self.stack.drain(argument_base..) {
                        self.scratch
                            .push(Machine::into_value(self.program.clone(), value));
                    }
                    if matches!(instruction, Instruction::ProtocolCall { .. }) {
                        crate::core::protocol_intrinsic_call(name, &self.scratch).map(VmSlot::from)
                    } else {
                        crate::core::apply_intrinsic_name(name, &self.scratch).map(VmSlot::from)
                    }
                };
                match result {
                    Ok(value) => self.stack.push(value),
                    Err(message) => match self.raise(function, message) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    },
                }
            }
            Instruction::Jump(target) => next_ip = *target as usize,
            Instruction::JumpIfFalse(target) => {
                let Some(condition) = self.stack.pop() else {
                    match self.raise(function, "stack underflow") {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                };
                if !condition.truthy() {
                    next_ip = *target as usize;
                }
            }
            Instruction::IntrinsicValue(target) => {
                let Some(name) = constant_string(program, *target) else {
                    return Dispatch::Failed(self.error(
                        function,
                        format!("intrinsic target constant {target} is invalid"),
                    ));
                };
                let Some(value) = crate::core::direct_function_value(name) else {
                    return Dispatch::Failed(self.error(
                        function,
                        format!("missing direct intrinsic callable: {name}"),
                    ));
                };
                self.stack.push(value.into());
            }
            Instruction::BuiltinValue(index) => {
                let Some(Value::String(name)) = program.constants.get(*index as usize) else {
                    return Dispatch::Failed(self.error(
                        function,
                        format!("builtin name constant {index} is invalid"),
                    ));
                };
                match crate::core::bytecode_callable_value(name) {
                    Ok(value) => self.stack.push(value.into()),
                    Err(message) => {
                        return Dispatch::Failed(self.error(function, message));
                    }
                }
            }
            Instruction::NamespaceValue(index) => {
                let Some(name) = constant_string(program, *index) else {
                    return Dispatch::Failed(self.error(
                        function,
                        format!("namespace name constant {index} is invalid"),
                    ));
                };
                match crate::core::vm_resolve_namespace_value(name) {
                    Ok(value) => self.stack.push(value.into()),
                    Err(message) => return Dispatch::Failed(self.error(function, message)),
                }
            }
            Instruction::NamespaceOperation(index) => {
                let Some(value) = program.constants.get(*index as usize) else {
                    return Dispatch::Failed(
                        self.error(function, format!("constant index {index} out of range")),
                    );
                };
                match crate::core::eval_bytecode_management(value) {
                    Ok(value) => self.stack.push(value.into()),
                    Err(message) => match self.raise(function, message) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    },
                }
            }
            Instruction::DynamicBind(index) => {
                let Some(Value::String(name)) = program.constants.get(*index as usize) else {
                    return Dispatch::Failed(self.error(
                        function,
                        format!("binding name constant {index} is invalid"),
                    ));
                };
                let Some(value) = self.stack.pop() else {
                    return Dispatch::Failed(self.error(function, "stack underflow"));
                };
                match crate::core::bytecode_dynamic_bind(
                    name,
                    Self::into_value(program.clone(), value),
                ) {
                    Ok(()) => self.stack.push(VmSlot::Nil),
                    Err(message) => match self.raise(function, message) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    },
                }
            }
            Instruction::DynamicUnbind(index) => {
                let Some(Value::String(name)) = program.constants.get(*index as usize) else {
                    return Dispatch::Failed(self.error(
                        function,
                        format!("binding name constant {index} is invalid"),
                    ));
                };
                match crate::core::bytecode_dynamic_unbind(name) {
                    Ok(()) => self.stack.push(VmSlot::Nil),
                    Err(message) => match self.raise(function, message) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    },
                }
            }
            Instruction::Closure {
                prototype,
                captures,
            } => {
                guarded!(self.exec_closure(program, *prototype, *captures));
            }
            Instruction::Call { argc } => match self.collect_call(*argc) {
                Ok((callee, args)) => return Dispatch::Call { callee, args },
                Err(message) => match self.raise(function, message) {
                    Ok(target) => return Dispatch::Unwound(target),
                    Err(error) => return Dispatch::Failed(error),
                },
            },
            Instruction::CallStatic { prototype, argc } => {
                let direct = program
                    .functions
                    .get(usize::from(*prototype))
                    .is_some_and(|proto| {
                        proto.capture_count == 0 && !proto.async_function && !proto.variadic
                    });
                if direct {
                    if self.stack.len() < usize::from(*argc) {
                        match self.raise(function, "stack underflow") {
                            Ok(target) => return Dispatch::Unwound(target),
                            Err(error) => return Dispatch::Failed(error),
                        }
                    }
                    return Dispatch::CallStaticDirect {
                        prototype: *prototype,
                        argc: *argc,
                    };
                }
                match self.collect_call_static(program, function, *prototype, *argc) {
                    Ok((prototype, args, captures)) => {
                        return Dispatch::CallStatic {
                            prototype,
                            args,
                            captures,
                        };
                    }
                    Err(message) => match self.raise(function, message) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    },
                }
            }
            Instruction::Throw => {
                let Some(value) = self.stack.pop() else {
                    match self.raise(function, "stack underflow") {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                };
                let value = Self::into_value(program.clone(), value);
                let position = function.source_map.position(self.ip);
                let site = position.map(|position| crate::core::ExceptionSite {
                    namespace: program.namespace.clone(),
                    resource: None,
                    line: position.line,
                    column: position.column,
                });
                if !matches!(value, Value::ExceptionInfo(_)) {
                    return Dispatch::Failed(
                        self.error(function, "throw expects an Exception value created by ex"),
                    );
                }
                let message = crate::core::thrown_error_at(value, site);
                match self.raise(function, message) {
                    Ok(target) => return Dispatch::Unwound(target),
                    Err(error) => return Dispatch::Failed(error),
                }
            }
            Instruction::Rethrow => {
                let Some(value) = self.stack.pop() else {
                    match self.raise(function, "stack underflow") {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                };
                let message = match value.into_runtime_value() {
                    Some(value @ Value::ExceptionInfo(_)) => {
                        let position = function.source_map.position(self.ip);
                        crate::core::thrown_error_at(
                            value,
                            position.map(|position| crate::core::ExceptionSite {
                                namespace: self.program.namespace.clone(),
                                resource: None,
                                line: position.line,
                                column: position.column,
                            }),
                        )
                    }
                    Some(Value::String(message)) => message,
                    Some(_) => "rethrow expects an Exception or pending error value".to_string(),
                    None => {
                        // Defensive: the compiler only emits Rethrow behind a
                        // pending-error flag it set to a message string.
                        "rethrow expects a string message".to_string()
                    }
                };
                match self.raise(function, message) {
                    Ok(target) => return Dispatch::Unwound(target),
                    Err(error) => return Dispatch::Failed(error),
                }
            }
            Instruction::GetGlobal(index) => {
                guarded!(self.exec_get_global(program, *index));
            }
            Instruction::DefGlobal { name, metadata } => {
                guarded!(self.exec_def_global(program, *name, *metadata));
            }
            Instruction::SetGlobal(index) => {
                guarded!(self.exec_set_global(program, *index));
            }
            Instruction::VarGlobal(index) => {
                guarded!(self.exec_var_global(program, *index));
            }
            Instruction::DeclareGlobal(index) => {
                guarded!(self.exec_declare_global(program, *index));
            }
            Instruction::MutableFieldGet(index) => {
                guarded!(self.exec_mutable_field_get(program, *index));
            }
            Instruction::MutableFieldSet(index) => {
                guarded!(self.exec_mutable_field_set(program, *index));
            }
            Instruction::InstanceOf => {
                guarded!(self.exec_instance_of());
            }
            Instruction::MakeMultiArity { name, count } => {
                guarded!(self.exec_make_multi_arity(program, *name, *count));
            }
            Instruction::BuildVector(count) => {
                guarded!(self.exec_build_collection(program, *count, false, false));
            }
            Instruction::BuildMap(pairs) => {
                guarded!(self.exec_build_collection(program, pairs.saturating_mul(2), true, false));
            }
            Instruction::BuildSet(count) => {
                guarded!(self.exec_build_collection(program, *count, false, true));
            }
            Instruction::BuildList(count) => {
                guarded!(self.exec_build_list(*count, false));
            }
            Instruction::ConcatList(count) => {
                guarded!(self.exec_build_list(*count, true));
            }
            Instruction::ToVector => {
                guarded!(self.exec_to_vector());
            }
            Instruction::DefMacro { name, metadata } => {
                guarded!(self.exec_def_macro(program, *name, *metadata));
            }
            Instruction::Await => {
                let Some(value) = self.stack.last() else {
                    return Dispatch::Failed(self.error(function, "stack underflow"));
                };
                let Some(Value::Promise(promise)) = value.runtime_value() else {
                    match self.raise(function, "await expects a promise") {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    }
                };
                match promise.state() {
                    PromiseState::Pending => return Dispatch::Suspended(promise),
                    PromiseState::Fulfilled(value) => {
                        self.stack.pop();
                        self.stack.push(value.into());
                    }
                    PromiseState::Rejected(error) => {
                        self.stack.pop();
                        match self.raise(function, error.message()) {
                            Ok(target) => return Dispatch::Unwound(target),
                            Err(error) => return Dispatch::Failed(error),
                        }
                    }
                }
            }
            Instruction::Yield => {
                let Some(value) = self.stack.pop() else {
                    return Dispatch::Failed(self.error(function, "stack underflow"));
                };
                return Dispatch::Yielded(Self::into_value(program.clone(), value));
            }
            Instruction::HostCall => {
                if self.stack.len() < 3 {
                    return Dispatch::Failed(self.error(function, "stack underflow"));
                }
                let values = self.stack.split_off(self.stack.len() - 3);
                let mut values = values
                    .into_iter()
                    .map(|value| value.into_runtime_value().ok_or("Host/call expects values"));
                let service = match values.next().expect("three host arguments") {
                    Ok(value) => value,
                    Err(message) => return Dispatch::Failed(self.error(function, message)),
                };
                let target = match values.next().expect("three host arguments") {
                    Ok(value) => value,
                    Err(message) => return Dispatch::Failed(self.error(function, message)),
                };
                let arguments = match values.next().expect("three host arguments") {
                    Ok(value) => value,
                    Err(message) => return Dispatch::Failed(self.error(function, message)),
                };
                match crate::core::call_host_value(service, target, arguments) {
                    Ok(value) => self.stack.push(value.into()),
                    Err(message) => match self.raise(function, message) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    },
                }
            }
            Instruction::DotCall { method, argc } => {
                let count = usize::from(*argc) + 1;
                if self.stack.len() < count {
                    return Dispatch::Failed(self.error(function, "stack underflow"));
                }
                let Some(method) = constant_string(program, *method) else {
                    return Dispatch::Failed(
                        self.error(function, "dot method constant is invalid"),
                    );
                };
                let values = self.stack.split_off(self.stack.len() - count);
                let mut values = values
                    .into_iter()
                    .map(|value| Machine::into_value(self.program.clone(), value));
                let receiver = values.next().expect("dot receiver was counted");
                match crate::core::dot_call_values(receiver, method, values.collect()) {
                    Ok(value) => self.stack.push(value.into()),
                    Err(message) => match self.raise(function, message) {
                        Ok(target) => return Dispatch::Unwound(target),
                        Err(error) => return Dispatch::Failed(error),
                    },
                }
            }
            Instruction::Return => {
                return match self.stack.pop() {
                    Some(value) => Dispatch::Returned(value),
                    None => Dispatch::Failed(self.error(function, "stack underflow")),
                };
            }
        }
        Dispatch::Next(next_ip)
    }
}