1mod arithmetic;
22mod call;
23mod call_named;
24mod call_support;
25mod collections;
26mod comparison;
27mod control_flow;
28mod exception;
29mod imports;
30mod iter;
31mod logical;
32mod misc;
33mod parallel;
34mod stack;
35
36use std::future::Future;
37use std::pin::Pin;
38use std::task::{Context, Poll};
39
40use pin_project_lite::pin_project;
41
42use crate::value::{VmError, VmValue};
43
44pin_project! {
45 struct OpcodeDispatchFuture<F> {
48 #[pin]
49 inner: F,
50 }
51}
52
53impl<F> OpcodeDispatchFuture<F> {
54 fn new(inner: F) -> Self {
55 Self { inner }
56 }
57}
58
59impl<F: Future> Future for OpcodeDispatchFuture<F> {
60 type Output = F::Output;
61
62 #[inline(never)]
63 fn poll(self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
64 self.project().inner.poll(context)
65 }
66}
67
68harn_opcode_macros::define_opcodes! {
69 Constant { sync(self.execute_constant()), disasm: const_pool_u16("CONSTANT") };
71 Nil { sync_void(self.execute_nil()), disasm: bare("NIL") };
72 True { sync_void(self.execute_true()), disasm: bare("TRUE") };
73 False { sync_void(self.execute_false()), disasm: bare("FALSE") };
74
75 RootHarness { sync(self.execute_root_harness()), disasm: bare("ROOT_HARNESS") };
77 GetVar { sync(self.execute_get_var()), disasm: const_pool_u16("GET_VAR"), flags: [reads_outer_name] };
78 DefLet { sync(self.execute_def_let()), disasm: const_pool_u16("DEF_LET") };
79 DefVar { sync(self.execute_def_var()), disasm: const_pool_u16("DEF_VAR") };
80 DefCell { sync(self.execute_def_cell()), disasm: const_pool_u16("DEF_CELL") };
81 SetVar { sync(self.execute_set_var()), disasm: const_pool_u16("SET_VAR"), flags: [reads_outer_name] };
82 PushScope { sync_void(self.execute_push_scope()), disasm: bare("PUSH_SCOPE") };
83 PopScope { sync_void(self.execute_pop_scope()), disasm: bare("POP_SCOPE") };
84
85 Add { sync(self.execute_add()), disasm: bare("ADD"), flags: [adaptive_binary] };
87 Sub { sync(self.execute_sub()), disasm: bare("SUB"), flags: [adaptive_binary] };
88 Mul { sync(self.execute_mul()), disasm: bare("MUL"), flags: [adaptive_binary] };
89 Div { sync(self.execute_div()), disasm: bare("DIV"), flags: [adaptive_binary] };
90 Mod { sync(self.execute_mod()), disasm: bare("MOD"), flags: [adaptive_binary] };
91 Pow { sync(self.execute_pow()), disasm: bare("POW") };
92 Negate { sync(self.execute_negate()), disasm: bare("NEGATE") };
93
94 Equal { sync(self.execute_equal()), disasm: bare("EQUAL"), flags: [adaptive_binary] };
96 NotEqual { sync(self.execute_not_equal()), disasm: bare("NOT_EQUAL"), flags: [adaptive_binary] };
97 Less { sync(self.execute_less()), disasm: bare("LESS"), flags: [adaptive_binary] };
98 Greater { sync(self.execute_greater()), disasm: bare("GREATER"), flags: [adaptive_binary] };
99 LessEqual { sync(self.execute_less_equal()), disasm: bare("LESS_EQUAL"), flags: [adaptive_binary] };
100 GreaterEqual { sync(self.execute_greater_equal()), disasm: bare("GREATER_EQUAL"), flags: [adaptive_binary] };
101
102 Not { sync(self.execute_not()), disasm: bare("NOT") };
104
105 Jump { sync_void(self.execute_jump()), disasm: u16("JUMP") };
107 JumpIfFalse { sync(self.execute_jump_if_false()), disasm: u16("JUMP_IF_FALSE") };
108 JumpIfTrue { sync(self.execute_jump_if_true()), disasm: u16("JUMP_IF_TRUE") };
109 Pop { sync(self.execute_pop()), disasm: bare("POP") };
110
111 Call { split(self.execute_call_sync(), self.execute_call_async().await), disasm: u8("CALL"), flags: [reads_outer_name] };
113 TailCall { split(self.execute_tail_call_sync(), self.execute_tail_call_async().await), disasm: u8("TAIL_CALL"), flags: [reads_outer_name] };
114 Return { sync_return(self.execute_return()), disasm: bare("RETURN") };
115 Closure { sync_void(self.execute_closure()), disasm: u16("CLOSURE") };
116
117 BuildList { sync_void(self.execute_build_list()), disasm: u16("BUILD_LIST") };
119 BuildDict { sync_void(self.execute_build_dict()), disasm: u16("BUILD_DICT") };
120 Subscript { sync(self.execute_subscript(false)), disasm: bare("SUBSCRIPT") };
121 SubscriptOpt { sync(self.execute_subscript(true)), disasm: bare("SUBSCRIPT_OPT") };
122 Slice { sync(self.execute_slice()), disasm: bare("SLICE") };
123
124 GetProperty { sync(self.execute_get_property(false)), disasm: const_pool_u16("GET_PROPERTY") };
126 GetPropertyOpt { sync(self.execute_get_property(true)), disasm: const_pool_u16("GET_PROPERTY_OPT") };
127 SetProperty { sync(self.execute_set_property()), disasm: const_pool_u16("SET_PROPERTY") };
128 SetSubscript { sync(self.execute_set_subscript()), disasm: const_pool_u16("SET_SUBSCRIPT") };
129 SetLocalSlotProperty { sync(self.execute_set_local_slot_property()), disasm: const_pool_local_slot("SET_LOCAL_SLOT_PROPERTY") };
130 SetLocalSlotSubscript { sync(self.execute_set_local_slot_subscript()), disasm: local_slot_u16("SET_LOCAL_SLOT_SUBSCRIPT") };
131 MethodCall { split(self.execute_method_call_sync(false), self.execute_method_call(false).await), disasm: method_call("METHOD_CALL") };
132 MethodCallOpt { split(self.execute_method_call_sync(true), self.execute_method_call(true).await), disasm: method_call("METHOD_CALL_OPT") };
133
134 Concat { sync_void(self.execute_concat()), disasm: u16("CONCAT") };
136
137 IterInit { sync(self.execute_iter_init()), disasm: bare("ITER_INIT") };
139 IterNext { split(self.execute_iter_next_sync(), self.execute_iter_next_async().await), disasm: u16("ITER_NEXT") };
140
141 Pipe { async_op(self.execute_pipe().await), disasm: bare("PIPE"), flags: [reads_outer_name] };
143
144 Throw { sync(self.execute_throw()), disasm: bare("THROW") };
146 TryCatchSetup { sync_void(self.execute_try_catch_setup()), disasm: try_catch_setup("TRY_CATCH_SETUP") };
147 PopHandler { sync_void(self.execute_pop_handler()), disasm: bare("POP_HANDLER") };
148
149 Parallel { async_op(self.execute_parallel().await), disasm: bare("PARALLEL") };
151 ParallelMap { async_op(self.execute_parallel_map().await), disasm: bare("PARALLEL_MAP") };
152 ParallelMapStream { async_op(self.execute_parallel_map_stream().await), disasm: bare("PARALLEL_MAP_STREAM") };
153 ParallelSettle { async_op(self.execute_parallel_settle().await), disasm: bare("PARALLEL_SETTLE") };
154 Spawn { sync(self.execute_spawn()), disasm: bare("SPAWN") };
155 SyncMutexEnter { async_op(self.execute_sync_mutex_enter().await), disasm: bare("SYNC_MUTEX_ENTER") };
156 SyncMutexEnterKeyed { async_op(self.execute_sync_mutex_enter_keyed().await), disasm: bare("SYNC_MUTEX_ENTER_KEYED") };
157 TaskScopeEnter { sync_void(self.execute_task_scope_enter()), disasm: bare("TASK_SCOPE_ENTER") };
158 TaskScopeExit { async_op(self.execute_task_scope_exit().await), disasm: bare("TASK_SCOPE_EXIT") };
159
160 Import { async_op(self.execute_import_op().await), disasm: const_pool_u16("IMPORT") };
162 SelectiveImport { async_op(self.execute_selective_import().await), disasm: selective_import("SELECTIVE_IMPORT") };
163 NamespaceImport { async_op(self.execute_namespace_import().await), disasm: selective_import("NAMESPACE_IMPORT") };
164
165 DeadlineSetup { sync(self.execute_deadline_setup()), disasm: bare("DEADLINE_SETUP") };
167 DeadlineEnd { sync_void(self.execute_deadline_end()), disasm: bare("DEADLINE_END") };
168
169 BuildEnum { sync(self.execute_build_enum()), disasm: build_enum("BUILD_ENUM") };
171 MatchEnum { sync(self.execute_match_enum()), disasm: match_enum("MATCH_ENUM") };
172
173 PopIterator { sync_void(self.execute_pop_iterator()), disasm: bare("POP_ITERATOR") };
175
176 GetArgc { sync_void(self.execute_get_argc()), disasm: bare("GET_ARGC") };
178
179 CheckType { sync(self.execute_check_type()), disasm: check_type("CHECK_TYPE"), flags: [reads_outer_name] };
181
182 TryUnwrap { sync(self.execute_try_unwrap()), disasm: bare("TRY_UNWRAP") };
184 TryWrapOk { sync(self.execute_try_wrap_ok()), disasm: bare("TRY_WRAP_OK") };
185
186 CallSpread { async_op(self.execute_call_spread().await), disasm: bare("CALL_SPREAD"), flags: [reads_outer_name] };
188 CallBuiltin { split(self.execute_call_builtin_sync(), self.execute_call_builtin_async().await), disasm: call_builtin("CALL_BUILTIN"), flags: [reads_outer_name] };
189 CallBuiltinSpread { async_op(self.execute_call_builtin_spread().await), disasm: call_builtin_spread("CALL_BUILTIN_SPREAD"), flags: [reads_outer_name] };
190 MethodCallSpread { async_op(self.execute_method_call_spread().await), disasm: method_call_spread("METHOD_CALL_SPREAD") };
191
192 Dup { sync(self.execute_dup()), disasm: bare("DUP") };
194 Swap { sync_void(self.execute_swap()), disasm: bare("SWAP") };
195 Contains { sync(self.execute_contains()), disasm: bare("CONTAINS") };
196
197 AddInt { sync(self.execute_add_int()), disasm: bare("ADD_INT") };
199 SubInt { sync(self.execute_sub_int()), disasm: bare("SUB_INT") };
200 MulInt { sync(self.execute_mul_int()), disasm: bare("MUL_INT") };
201 DivInt { sync(self.execute_div_int()), disasm: bare("DIV_INT") };
202 ModInt { sync(self.execute_mod_int()), disasm: bare("MOD_INT") };
203 AddFloat { sync(self.execute_add_float()), disasm: bare("ADD_FLOAT") };
204 SubFloat { sync(self.execute_sub_float()), disasm: bare("SUB_FLOAT") };
205 MulFloat { sync(self.execute_mul_float()), disasm: bare("MUL_FLOAT") };
206 DivFloat { sync(self.execute_div_float()), disasm: bare("DIV_FLOAT") };
207 ModFloat { sync(self.execute_mod_float()), disasm: bare("MOD_FLOAT") };
208
209 EqualInt { sync(self.execute_equal_int()), disasm: bare("EQUAL_INT") };
211 NotEqualInt { sync(self.execute_not_equal_int()), disasm: bare("NOT_EQUAL_INT") };
212 LessInt { sync(self.execute_less_int()), disasm: bare("LESS_INT") };
213 GreaterInt { sync(self.execute_greater_int()), disasm: bare("GREATER_INT") };
214 LessEqualInt { sync(self.execute_less_equal_int()), disasm: bare("LESS_EQUAL_INT") };
215 GreaterEqualInt { sync(self.execute_greater_equal_int()), disasm: bare("GREATER_EQUAL_INT") };
216 EqualFloat { sync(self.execute_equal_float()), disasm: bare("EQUAL_FLOAT") };
217 NotEqualFloat { sync(self.execute_not_equal_float()), disasm: bare("NOT_EQUAL_FLOAT") };
218 LessFloat { sync(self.execute_less_float()), disasm: bare("LESS_FLOAT") };
219 GreaterFloat { sync(self.execute_greater_float()), disasm: bare("GREATER_FLOAT") };
220 LessEqualFloat { sync(self.execute_less_equal_float()), disasm: bare("LESS_EQUAL_FLOAT") };
221 GreaterEqualFloat { sync(self.execute_greater_equal_float()), disasm: bare("GREATER_EQUAL_FLOAT") };
222 EqualBool { sync(self.execute_equal_bool()), disasm: bare("EQUAL_BOOL") };
223 NotEqualBool { sync(self.execute_not_equal_bool()), disasm: bare("NOT_EQUAL_BOOL") };
224 EqualString { sync(self.execute_equal_string()), disasm: bare("EQUAL_STRING") };
225 NotEqualString { sync(self.execute_not_equal_string()), disasm: bare("NOT_EQUAL_STRING") };
226
227 Yield { async_op(self.execute_yield().await), disasm: bare("YIELD") };
229
230 GetLocalSlot { sync(self.execute_get_local_slot()), disasm: local_slot_u16("GET_LOCAL_SLOT") };
232 DefLocalSlot { sync(self.execute_def_local_slot()), disasm: local_slot_u16("DEF_LOCAL_SLOT") };
233 SetLocalSlot { sync(self.execute_set_local_slot()), disasm: local_slot_u16("SET_LOCAL_SLOT") };
234 ConcatAssignLocal { sync(self.execute_concat_assign_local()), disasm: local_slot_u16("CONCAT_ASSIGN_LOCAL") };
235}
236
237impl super::Vm {
238 pub(super) async fn execute_op(&mut self, op_byte: u8) -> Result<Option<VmValue>, VmError> {
243 let op = Op::from_byte(op_byte).ok_or(VmError::InvalidInstruction(op_byte))?;
244 if let Some(result) = self.execute_op_sync(op) {
245 result?;
246 return Ok(None);
247 }
248 self.execute_op_async(op).await?;
249 Ok(None)
250 }
251}