1use serde::{Deserialize, Serialize};
4
5pub use shape_value::StringId;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum OpcodeCategory {
11 Stack,
12 Arithmetic,
13 Comparison,
14 Logical,
15 Control,
16 Variable,
17 Object,
18 Loop,
19 Builtin,
20 Exception,
21 DataFrame,
22 Async,
23 Trait,
24 Special,
25}
26
27macro_rules! define_opcodes {
37 ($($(#[doc = $doc:expr])* $name:ident = $byte:literal, $cat:ident, pops: $pops:expr, pushes: $pushes:expr);* $(;)?) => {
38 #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
39 #[repr(u8)]
40 pub enum OpCode {
41 $(
42 $(#[doc = $doc])*
43 $name = $byte,
44 )*
45 }
46
47 impl OpCode {
48 pub const fn category(self) -> OpcodeCategory {
50 match self {
51 $( OpCode::$name => OpcodeCategory::$cat, )*
52 }
53 }
54
55 pub const fn stack_pops(self) -> u8 {
58 match self {
59 $( OpCode::$name => $pops, )*
60 }
61 }
62
63 pub const fn stack_pushes(self) -> u8 {
66 match self {
67 $( OpCode::$name => $pushes, )*
68 }
69 }
70 }
71 };
72}
73
74define_opcodes! {
75 PushConst = 0x00, Stack, pops: 0, pushes: 1;
78 PushNull = 0x01, Stack, pops: 0, pushes: 1;
80 Pop = 0x02, Stack, pops: 1, pushes: 0;
82 Dup = 0x03, Stack, pops: 1, pushes: 2;
84 Swap = 0x04, Stack, pops: 2, pushes: 2;
86
87 Add = 0x10, Arithmetic, pops: 2, pushes: 1;
90 Sub = 0x11, Arithmetic, pops: 2, pushes: 1;
92 Mul = 0x12, Arithmetic, pops: 2, pushes: 1;
94 Div = 0x13, Arithmetic, pops: 2, pushes: 1;
96 Mod = 0x14, Arithmetic, pops: 2, pushes: 1;
98 Neg = 0x15, Arithmetic, pops: 1, pushes: 1;
100 Pow = 0x16, Arithmetic, pops: 2, pushes: 1;
102 BitAnd = 0x17, Arithmetic, pops: 2, pushes: 1;
104 BitOr = 0x18, Arithmetic, pops: 2, pushes: 1;
106 BitShl = 0x19, Arithmetic, pops: 2, pushes: 1;
108 BitShr = 0x1A, Arithmetic, pops: 2, pushes: 1;
110 BitNot = 0x1B, Arithmetic, pops: 1, pushes: 1;
112 BitXor = 0x1C, Arithmetic, pops: 2, pushes: 1;
114
115 Gt = 0x20, Comparison, pops: 2, pushes: 1;
118 Lt = 0x21, Comparison, pops: 2, pushes: 1;
120 Gte = 0x22, Comparison, pops: 2, pushes: 1;
122 Lte = 0x23, Comparison, pops: 2, pushes: 1;
124 Eq = 0x24, Comparison, pops: 2, pushes: 1;
126 Neq = 0x25, Comparison, pops: 2, pushes: 1;
128
129 GtInt = 0x26, Comparison, pops: 2, pushes: 1;
132 GtNumber = 0x27, Comparison, pops: 2, pushes: 1;
134 GtDecimal = 0x28, Comparison, pops: 2, pushes: 1;
136 LtInt = 0x29, Comparison, pops: 2, pushes: 1;
138 LtNumber = 0x2A, Comparison, pops: 2, pushes: 1;
140 LtDecimal = 0x2B, Comparison, pops: 2, pushes: 1;
142 GteInt = 0x2C, Comparison, pops: 2, pushes: 1;
144 GteNumber = 0x2D, Comparison, pops: 2, pushes: 1;
146 GteDecimal = 0x2E, Comparison, pops: 2, pushes: 1;
148 LteInt = 0x2F, Comparison, pops: 2, pushes: 1;
150
151 And = 0x30, Logical, pops: 2, pushes: 1;
154 Or = 0x31, Logical, pops: 2, pushes: 1;
156 Not = 0x32, Logical, pops: 1, pushes: 1;
158
159 AddInt = 0x33, Arithmetic, pops: 2, pushes: 1;
162 AddNumber = 0x34, Arithmetic, pops: 2, pushes: 1;
164 AddDecimal = 0x35, Arithmetic, pops: 2, pushes: 1;
166 SubInt = 0x36, Arithmetic, pops: 2, pushes: 1;
168 SubNumber = 0x37, Arithmetic, pops: 2, pushes: 1;
170 SubDecimal = 0x38, Arithmetic, pops: 2, pushes: 1;
172 MulInt = 0x39, Arithmetic, pops: 2, pushes: 1;
174 MulNumber = 0x3A, Arithmetic, pops: 2, pushes: 1;
176 MulDecimal = 0x3B, Arithmetic, pops: 2, pushes: 1;
178 DivInt = 0x3C, Arithmetic, pops: 2, pushes: 1;
180 DivNumber = 0x3D, Arithmetic, pops: 2, pushes: 1;
182 DivDecimal = 0x3E, Arithmetic, pops: 2, pushes: 1;
184 ModInt = 0x3F, Arithmetic, pops: 2, pushes: 1;
186
187 Jump = 0x40, Control, pops: 0, pushes: 0;
190 JumpIfFalse = 0x41, Control, pops: 1, pushes: 0;
192 JumpIfTrue = 0x42, Control, pops: 1, pushes: 0;
194 Call = 0x43, Control, pops: 0, pushes: 0;
196 Return = 0x44, Control, pops: 0, pushes: 0;
198 ReturnValue = 0x45, Control, pops: 1, pushes: 0;
200 CallValue = 0x46, Control, pops: 0, pushes: 0;
202
203 LoadLocal = 0x50, Variable, pops: 0, pushes: 1;
206 StoreLocal = 0x51, Variable, pops: 1, pushes: 0;
208 LoadModuleBinding = 0x52, Variable, pops: 0, pushes: 1;
210 StoreModuleBinding = 0x53, Variable, pops: 1, pushes: 0;
212 LoadClosure = 0x54, Variable, pops: 0, pushes: 1;
214 StoreClosure = 0x55, Variable, pops: 1, pushes: 0;
216 MakeClosure = 0x56, Variable, pops: 0, pushes: 1;
218 CloseUpvalue = 0x57, Variable, pops: 0, pushes: 0;
220 MakeRef = 0x58, Variable, pops: 0, pushes: 1;
222 DerefLoad = 0x59, Variable, pops: 0, pushes: 1;
224 DerefStore = 0x5A, Variable, pops: 1, pushes: 0;
226 SetIndexRef = 0x5B, Variable, pops: 2, pushes: 0;
228 BoxLocal = 0x5C, Variable, pops: 0, pushes: 1;
232 BoxModuleBinding = 0x5D, Variable, pops: 0, pushes: 1;
235
236 NewArray = 0x60, Object, pops: 0, pushes: 1;
239 NewObject = 0x61, Object, pops: 0, pushes: 1;
241 GetProp = 0x62, Object, pops: 2, pushes: 1;
243 SetProp = 0x63, Object, pops: 3, pushes: 0;
245 Length = 0x64, Object, pops: 1, pushes: 1;
247 ArrayPush = 0x65, Object, pops: 2, pushes: 0;
249 ArrayPop = 0x66, Object, pops: 1, pushes: 1;
251 MergeObject = 0x67, Object, pops: 2, pushes: 1;
253 SetLocalIndex = 0x68, Object, pops: 2, pushes: 0;
255 SetModuleBindingIndex = 0x69, Object, pops: 2, pushes: 0;
257 ArrayPushLocal = 0x6A, Object, pops: 1, pushes: 0;
259 NewMatrix = 0x6B, Object, pops: 0, pushes: 1;
261 NewTypedArray = 0x6C, Object, pops: 0, pushes: 1;
265
266 LoopStart = 0x70, Loop, pops: 0, pushes: 0;
269 LoopEnd = 0x71, Loop, pops: 0, pushes: 0;
271 Break = 0x72, Loop, pops: 0, pushes: 0;
273 Continue = 0x73, Loop, pops: 0, pushes: 0;
275 IterNext = 0x74, Loop, pops: 1, pushes: 1;
277 IterDone = 0x75, Loop, pops: 1, pushes: 1;
279
280 Pattern = 0x83, Builtin, pops: 0, pushes: 0;
283 CallMethod = 0x88, Builtin, pops: 0, pushes: 0;
285 PushTimeframe = 0x89, Builtin, pops: 1, pushes: 0;
287 PopTimeframe = 0x8A, Builtin, pops: 0, pushes: 0;
289 RunSimulation = 0x8B, Builtin, pops: 0, pushes: 0;
291
292 BuiltinCall = 0x90, Builtin, pops: 0, pushes: 0;
295 TypeCheck = 0x91, Builtin, pops: 1, pushes: 1;
297 Convert = 0x92, Builtin, pops: 1, pushes: 1;
299
300 ModNumber = 0x93, Arithmetic, pops: 2, pushes: 1;
303 ModDecimal = 0x94, Arithmetic, pops: 2, pushes: 1;
305 PowInt = 0x95, Arithmetic, pops: 2, pushes: 1;
307 PowNumber = 0x96, Arithmetic, pops: 2, pushes: 1;
309 PowDecimal = 0x97, Arithmetic, pops: 2, pushes: 1;
311
312 LteNumber = 0x98, Comparison, pops: 2, pushes: 1;
315 LteDecimal = 0x99, Comparison, pops: 2, pushes: 1;
317 EqInt = 0x9A, Comparison, pops: 2, pushes: 1;
319 EqNumber = 0x9B, Comparison, pops: 2, pushes: 1;
321 NeqInt = 0x9C, Comparison, pops: 2, pushes: 1;
323 NeqNumber = 0x9D, Comparison, pops: 2, pushes: 1;
325
326 SetupTry = 0xA0, Exception, pops: 0, pushes: 0;
329 PopHandler = 0xA1, Exception, pops: 0, pushes: 0;
331 Throw = 0xA2, Exception, pops: 1, pushes: 0;
333 TryUnwrap = 0xA3, Exception, pops: 1, pushes: 1;
335 UnwrapOption = 0xA4, Exception, pops: 1, pushes: 1;
337 ErrorContext = 0xA5, Exception, pops: 1, pushes: 1;
339 IsOk = 0xA6, Exception, pops: 1, pushes: 1;
341 IsErr = 0xA7, Exception, pops: 1, pushes: 1;
343 UnwrapOk = 0xA8, Exception, pops: 1, pushes: 1;
345 UnwrapErr = 0xA9, Exception, pops: 1, pushes: 1;
347
348 SliceAccess = 0xB0, Object, pops: 3, pushes: 1;
351 NullCoalesce = 0xB1, Logical, pops: 2, pushes: 1;
353 MakeRange = 0xB2, Object, pops: 2, pushes: 1;
355
356 AddTyped = 0xB3, Arithmetic, pops: 2, pushes: 1;
359 SubTyped = 0xB4, Arithmetic, pops: 2, pushes: 1;
361 MulTyped = 0xB5, Arithmetic, pops: 2, pushes: 1;
363 DivTyped = 0xB6, Arithmetic, pops: 2, pushes: 1;
365 ModTyped = 0xB7, Arithmetic, pops: 2, pushes: 1;
367 CmpTyped = 0xB8, Comparison, pops: 2, pushes: 1;
370
371 GetDataField = 0xC0, DataFrame, pops: 1, pushes: 1;
374 GetDataRow = 0xC1, DataFrame, pops: 1, pushes: 1;
376
377 GetFieldTyped = 0xD0, Object, pops: 1, pushes: 1;
380 SetFieldTyped = 0xD1, Object, pops: 2, pushes: 0;
382 NewTypedObject = 0xD2, Object, pops: 0, pushes: 1;
384 TypedMergeObject = 0xD3, Object, pops: 2, pushes: 1;
386 WrapTypeAnnotation = 0xD4, Object, pops: 1, pushes: 1;
388
389 Yield = 0xE0, Async, pops: 0, pushes: 0;
392 Suspend = 0xE1, Async, pops: 0, pushes: 0;
394 Resume = 0xE2, Async, pops: 1, pushes: 0;
396 Poll = 0xE3, Async, pops: 0, pushes: 1;
398 AwaitBar = 0xE4, Async, pops: 0, pushes: 1;
400 AwaitTick = 0xE5, Async, pops: 0, pushes: 0;
402 Await = 0xE6, Async, pops: 1, pushes: 1;
404 SpawnTask = 0xE7, Async, pops: 1, pushes: 1;
406
407 EmitAlert = 0xE8, Async, pops: 1, pushes: 0;
410 EmitEvent = 0xE9, Async, pops: 1, pushes: 0;
412 JoinInit = 0xEA, Async, pops: 0, pushes: 1;
414 JoinAwait = 0xEB, Async, pops: 1, pushes: 1;
416 CancelTask = 0xEC, Async, pops: 1, pushes: 0;
418 AsyncScopeEnter = 0xED, Async, pops: 0, pushes: 0;
420 AsyncScopeExit = 0xEE, Async, pops: 0, pushes: 0;
422
423 LoadColF64 = 0xC2, DataFrame, pops: 1, pushes: 1;
426 LoadColI64 = 0xC3, DataFrame, pops: 1, pushes: 1;
428 LoadColBool = 0xC4, DataFrame, pops: 1, pushes: 1;
430 LoadColStr = 0xC5, DataFrame, pops: 1, pushes: 1;
432 BindSchema = 0xC6, DataFrame, pops: 1, pushes: 1;
434
435 BoxTraitObject = 0xEF, Trait, pops: 1, pushes: 1;
438 DynMethodCall = 0xC7, Trait, pops: 0, pushes: 0;
440 DropCall = 0xC8, Trait, pops: 1, pushes: 0;
442 DropCallAsync = 0xC9, Trait, pops: 1, pushes: 0;
444
445 AddIntTrusted = 0xCA, Arithmetic, pops: 2, pushes: 1;
448 SubIntTrusted = 0xCB, Arithmetic, pops: 2, pushes: 1;
450 MulIntTrusted = 0xCC, Arithmetic, pops: 2, pushes: 1;
452 DivIntTrusted = 0xCD, Arithmetic, pops: 2, pushes: 1;
454 AddNumberTrusted = 0xCE, Arithmetic, pops: 2, pushes: 1;
456 SubNumberTrusted = 0xCF, Arithmetic, pops: 2, pushes: 1;
458 MulNumberTrusted = 0xD5, Arithmetic, pops: 2, pushes: 1;
460 DivNumberTrusted = 0xD6, Arithmetic, pops: 2, pushes: 1;
462
463 LoadLocalTrusted = 0xD7, Variable, pops: 0, pushes: 1;
466
467 JumpIfFalseTrusted = 0xD8, Control, pops: 1, pushes: 0;
470
471 GtIntTrusted = 0xD9, Comparison, pops: 2, pushes: 1;
474 LtIntTrusted = 0xDA, Comparison, pops: 2, pushes: 1;
476 GteIntTrusted = 0xDB, Comparison, pops: 2, pushes: 1;
478 LteIntTrusted = 0xDC, Comparison, pops: 2, pushes: 1;
480 GtNumberTrusted = 0xDD, Comparison, pops: 2, pushes: 1;
482 LtNumberTrusted = 0xDE, Comparison, pops: 2, pushes: 1;
484 GteNumberTrusted = 0xDF, Comparison, pops: 2, pushes: 1;
486 LteNumberTrusted = 0x9E, Comparison, pops: 2, pushes: 1;
488
489 Nop = 0xF0, Special, pops: 0, pushes: 0;
492 Halt = 0xF1, Special, pops: 0, pushes: 0;
494 Debug = 0xF2, Special, pops: 0, pushes: 0;
496
497 IntToNumber = 0xF3, Arithmetic, pops: 1, pushes: 1;
500 NumberToInt = 0xF4, Arithmetic, pops: 1, pushes: 1;
502
503 CallForeign = 0xF5, Control, pops: 0, pushes: 0;
509
510 StoreLocalTyped = 0xF6, Variable, pops: 1, pushes: 0;
514
515 CastWidth = 0xF7, Arithmetic, pops: 1, pushes: 1;
519}
520
521impl OpCode {
522 pub const fn is_trusted(self) -> bool {
524 matches!(
525 self,
526 OpCode::AddIntTrusted
527 | OpCode::SubIntTrusted
528 | OpCode::MulIntTrusted
529 | OpCode::DivIntTrusted
530 | OpCode::AddNumberTrusted
531 | OpCode::SubNumberTrusted
532 | OpCode::MulNumberTrusted
533 | OpCode::DivNumberTrusted
534 | OpCode::LoadLocalTrusted
535 | OpCode::JumpIfFalseTrusted
536 | OpCode::GtIntTrusted
537 | OpCode::LtIntTrusted
538 | OpCode::GteIntTrusted
539 | OpCode::LteIntTrusted
540 | OpCode::GtNumberTrusted
541 | OpCode::LtNumberTrusted
542 | OpCode::GteNumberTrusted
543 | OpCode::LteNumberTrusted
544 )
545 }
546
547 pub const fn guarded_variant(self) -> Option<OpCode> {
553 match self {
554 OpCode::AddIntTrusted => Some(OpCode::AddInt),
555 OpCode::SubIntTrusted => Some(OpCode::SubInt),
556 OpCode::MulIntTrusted => Some(OpCode::MulInt),
557 OpCode::DivIntTrusted => Some(OpCode::DivInt),
558 OpCode::AddNumberTrusted => Some(OpCode::AddNumber),
559 OpCode::SubNumberTrusted => Some(OpCode::SubNumber),
560 OpCode::MulNumberTrusted => Some(OpCode::MulNumber),
561 OpCode::DivNumberTrusted => Some(OpCode::DivNumber),
562 OpCode::GtIntTrusted => Some(OpCode::GtInt),
563 OpCode::LtIntTrusted => Some(OpCode::LtInt),
564 OpCode::GteIntTrusted => Some(OpCode::GteInt),
565 OpCode::LteIntTrusted => Some(OpCode::LteInt),
566 OpCode::GtNumberTrusted => Some(OpCode::GtNumber),
567 OpCode::LtNumberTrusted => Some(OpCode::LtNumber),
568 OpCode::GteNumberTrusted => Some(OpCode::GteNumber),
569 OpCode::LteNumberTrusted => Some(OpCode::LteNumber),
570 OpCode::LoadLocalTrusted => Some(OpCode::LoadLocal),
571 OpCode::JumpIfFalseTrusted => Some(OpCode::JumpIfFalse),
572 _ => None,
573 }
574 }
575
576 pub const fn trusted_variant(self) -> Option<OpCode> {
578 match self {
579 OpCode::AddInt => Some(OpCode::AddIntTrusted),
580 OpCode::SubInt => Some(OpCode::SubIntTrusted),
581 OpCode::MulInt => Some(OpCode::MulIntTrusted),
582 OpCode::DivInt => Some(OpCode::DivIntTrusted),
583 OpCode::AddNumber => Some(OpCode::AddNumberTrusted),
584 OpCode::SubNumber => Some(OpCode::SubNumberTrusted),
585 OpCode::MulNumber => Some(OpCode::MulNumberTrusted),
586 OpCode::DivNumber => Some(OpCode::DivNumberTrusted),
587 OpCode::GtInt => Some(OpCode::GtIntTrusted),
588 OpCode::LtInt => Some(OpCode::LtIntTrusted),
589 OpCode::GteInt => Some(OpCode::GteIntTrusted),
590 OpCode::LteInt => Some(OpCode::LteIntTrusted),
591 OpCode::GtNumber => Some(OpCode::GtNumberTrusted),
592 OpCode::LtNumber => Some(OpCode::LtNumberTrusted),
593 OpCode::GteNumber => Some(OpCode::GteNumberTrusted),
594 OpCode::LteNumber => Some(OpCode::LteNumberTrusted),
595 OpCode::LoadLocal => Some(OpCode::LoadLocalTrusted),
596 OpCode::JumpIfFalse => Some(OpCode::JumpIfFalseTrusted),
597 _ => None,
598 }
599 }
600}
601
602#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
608#[repr(u8)]
609pub enum NumericWidth {
610 I8 = 0,
611 I16 = 1,
612 I32 = 2,
613 I64 = 3,
614 U8 = 4,
615 U16 = 5,
616 U32 = 6,
617 U64 = 7,
618 F32 = 8,
619 F64 = 9,
620}
621
622impl NumericWidth {
623 pub const ALL: [Self; 10] = [
624 Self::I8,
625 Self::I16,
626 Self::I32,
627 Self::I64,
628 Self::U8,
629 Self::U16,
630 Self::U32,
631 Self::U64,
632 Self::F32,
633 Self::F64,
634 ];
635
636 #[inline(always)]
637 pub const fn is_integer(self) -> bool {
638 matches!(
639 self,
640 Self::I8
641 | Self::I16
642 | Self::I32
643 | Self::I64
644 | Self::U8
645 | Self::U16
646 | Self::U32
647 | Self::U64
648 )
649 }
650
651 #[inline(always)]
652 pub const fn is_float(self) -> bool {
653 matches!(self, Self::F32 | Self::F64)
654 }
655
656 #[inline(always)]
658 pub const fn is_signed(self) -> bool {
659 matches!(self, Self::I8 | Self::I16 | Self::I32 | Self::I64)
660 }
661
662 #[inline(always)]
664 pub const fn is_unsigned(self) -> bool {
665 matches!(self, Self::U8 | Self::U16 | Self::U32 | Self::U64)
666 }
667
668 #[inline(always)]
670 pub const fn bits(self) -> u32 {
671 match self {
672 Self::I8 | Self::U8 => 8,
673 Self::I16 | Self::U16 => 16,
674 Self::I32 | Self::U32 | Self::F32 => 32,
675 Self::I64 | Self::U64 | Self::F64 => 64,
676 }
677 }
678
679 #[inline(always)]
681 pub const fn mask(self) -> u64 {
682 match self {
683 Self::I8 | Self::U8 => 0xFF,
684 Self::I16 | Self::U16 => 0xFFFF,
685 Self::I32 | Self::U32 | Self::F32 => 0xFFFF_FFFF,
686 Self::I64 | Self::U64 | Self::F64 => u64::MAX,
687 }
688 }
689
690 #[inline]
692 pub fn from_int_width(w: shape_ast::IntWidth) -> Self {
693 match w {
694 shape_ast::IntWidth::I8 => Self::I8,
695 shape_ast::IntWidth::U8 => Self::U8,
696 shape_ast::IntWidth::I16 => Self::I16,
697 shape_ast::IntWidth::U16 => Self::U16,
698 shape_ast::IntWidth::I32 => Self::I32,
699 shape_ast::IntWidth::U32 => Self::U32,
700 shape_ast::IntWidth::U64 => Self::U64,
701 }
702 }
703
704 #[inline]
706 pub fn to_int_width(self) -> Option<shape_ast::IntWidth> {
707 match self {
708 Self::I8 => Some(shape_ast::IntWidth::I8),
709 Self::U8 => Some(shape_ast::IntWidth::U8),
710 Self::I16 => Some(shape_ast::IntWidth::I16),
711 Self::U16 => Some(shape_ast::IntWidth::U16),
712 Self::I32 => Some(shape_ast::IntWidth::I32),
713 Self::U32 => Some(shape_ast::IntWidth::U32),
714 Self::U64 => Some(shape_ast::IntWidth::U64),
715 Self::I64 | Self::F32 | Self::F64 => None,
716 }
717 }
718}
719
720#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
722pub struct Instruction {
723 pub opcode: OpCode,
724 pub operand: Option<Operand>,
725}
726
727#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
729pub enum Operand {
730 Const(u16),
732 Local(u16),
734 ModuleBinding(u16),
736 Offset(i32),
738 Function(shape_value::FunctionId),
740 Builtin(BuiltinFunction),
742 Count(u16),
744 Property(u16),
746 ColumnIndex(u32),
748 TypedField {
753 type_id: u16,
754 field_idx: u16,
755 field_type_tag: u16,
756 },
757 TypedObjectAlloc {
760 schema_id: u16,
762 field_count: u16,
764 },
765 TypedMerge {
768 target_schema_id: u16,
770 left_size: u16,
772 right_size: u16,
774 },
775 ColumnAccess {
778 col_id: u32,
780 },
781 Name(StringId),
783 MethodCall { name: StringId, arg_count: u16 },
785 TypedMethodCall {
790 method_id: u16,
792 arg_count: u16,
794 string_id: u16,
797 },
798 ForeignFunction(u16),
800 MatrixDims { rows: u16, cols: u16 },
802 Width(NumericWidth),
804 TypedLocal(u16, NumericWidth),
806}
807
808#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
810pub enum BuiltinFunction {
811 Abs,
813 Sqrt,
814 Ln,
815 Pow,
816 Exp,
817 Log,
818 Min,
819 Max,
820 Floor,
821 Ceil,
822 Round,
823 Sin,
824 Cos,
825 Tan,
826 Asin,
827 Acos,
828 Atan,
829
830 StdDev,
832
833 Range,
835 Slice,
836 Push,
837 Pop,
838 First,
839 Last,
840 Zip,
841 Filled,
842
843 Map,
845 Filter,
846 Reduce,
847 ForEach,
848 Find,
849 FindIndex,
850 Some,
851 Every,
852
853 Print,
855 Format,
856 Len,
857 Snapshot,
859 Exit,
860
861 ObjectRest,
863
864 ControlFold,
866
867 TypeOf,
869 IsNumber,
870 IsString,
871 IsBool,
872 IsArray,
873 IsObject,
874 IsDataRow,
875
876 ToString,
878 ToNumber,
879 ToBool,
880 IntoInt,
881 IntoNumber,
882 IntoDecimal,
883 IntoBool,
884 IntoString,
885 TryIntoInt,
886 TryIntoNumber,
887 TryIntoDecimal,
888 TryIntoBool,
889 TryIntoString,
890
891 NativePtrSize,
893 NativePtrNewCell,
894 NativePtrFreeCell,
895 NativePtrReadPtr,
896 NativePtrWritePtr,
897 NativeTableFromArrowC,
898 NativeTableFromArrowCTyped,
899 NativeTableBindType,
900 FormatValueWithMeta,
903 FormatValueWithSpec,
906
907 IntrinsicSum,
909 IntrinsicMean,
910 IntrinsicMin,
911 IntrinsicMax,
912 IntrinsicStd,
913 IntrinsicVariance,
914
915 IntrinsicRandom,
917 IntrinsicRandomInt,
918 IntrinsicRandomSeed,
919 IntrinsicRandomNormal,
920 IntrinsicRandomArray,
921
922 IntrinsicDistUniform,
924 IntrinsicDistLognormal,
925 IntrinsicDistExponential,
926 IntrinsicDistPoisson,
927 IntrinsicDistSampleN,
928
929 IntrinsicBrownianMotion,
931 IntrinsicGbm,
932 IntrinsicOuProcess,
933 IntrinsicRandomWalk,
934
935 IntrinsicRollingSum,
937 IntrinsicRollingMean,
938 IntrinsicRollingStd,
939 IntrinsicRollingMin,
940 IntrinsicRollingMax,
941 IntrinsicEma,
942 IntrinsicLinearRecurrence,
943
944 IntrinsicShift,
946 IntrinsicDiff,
947 IntrinsicPctChange,
948 IntrinsicFillna,
949 IntrinsicCumsum,
950 IntrinsicCumprod,
951 IntrinsicClip,
952
953 IntrinsicCorrelation,
955 IntrinsicCovariance,
956 IntrinsicPercentile,
957 IntrinsicMedian,
958
959 IntrinsicCharCode,
961 IntrinsicFromCharCode,
962
963 IntrinsicSeries,
965
966 IntrinsicVecAbs,
968 IntrinsicVecSqrt,
969 IntrinsicVecLn,
970 IntrinsicVecExp,
971 IntrinsicVecAdd,
972 IntrinsicVecSub,
973 IntrinsicVecMul,
974 IntrinsicVecDiv,
975 IntrinsicVecMax,
976 IntrinsicVecMin,
977 IntrinsicVecSelect,
978
979 IntrinsicMatMulVec,
981 IntrinsicMatMulMat,
982
983 EvalTimeRef,
985 EvalDateTimeExpr,
986 EvalDataDateTimeRef,
987 EvalDataSet,
988 EvalDataRelative,
989 EvalDataRelativeRange,
990
991 SomeCtor,
993 OkCtor,
994 ErrCtor,
995
996 HashMapCtor,
998 SetCtor,
999 DequeCtor,
1000 PriorityQueueCtor,
1001
1002 JsonObjectGet,
1004 JsonArrayAt,
1005 JsonObjectKeys,
1006 JsonArrayLen,
1007 JsonObjectLen,
1008
1009 WindowRowNumber,
1011 WindowRank,
1012 WindowDenseRank,
1013 WindowNtile,
1014 WindowLag,
1015 WindowLead,
1016 WindowFirstValue,
1017 WindowLastValue,
1018 WindowNthValue,
1019 WindowSum,
1020 WindowAvg,
1021 WindowMin,
1022 WindowMax,
1023 WindowCount,
1024
1025 JoinExecute,
1027
1028 Reflect,
1030
1031 MakeContentText,
1034 MakeContentFragment,
1036 ApplyContentStyle,
1038 MakeContentChartFromValue,
1040
1041 ContentChart,
1044 ContentTextCtor,
1046 ContentTableCtor,
1048 ContentCodeCtor,
1050 ContentKvCtor,
1052 ContentFragmentCtor,
1054
1055 DateTimeNow,
1058 DateTimeUtc,
1060 DateTimeParse,
1062 DateTimeFromEpoch,
1064
1065 MutexCtor,
1068 AtomicCtor,
1070 LazyCtor,
1072 ChannelCtor,
1074
1075 Sign,
1078 Gcd,
1080 Lcm,
1082 Hypot,
1084 Clamp,
1086 IsNaN,
1088 IsFinite,
1090
1091 MakeTableFromRows,
1094}
1095
1096impl BuiltinFunction {
1097 pub fn from_discriminant(id: u16) -> Option<Self> {
1103 const VARIANTS: &[BuiltinFunction] = &[
1105 BuiltinFunction::Abs,
1107 BuiltinFunction::Sqrt,
1108 BuiltinFunction::Ln,
1109 BuiltinFunction::Pow,
1110 BuiltinFunction::Exp,
1111 BuiltinFunction::Log,
1112 BuiltinFunction::Min,
1113 BuiltinFunction::Max,
1114 BuiltinFunction::Floor,
1115 BuiltinFunction::Ceil,
1116 BuiltinFunction::Round,
1117 BuiltinFunction::Sin,
1118 BuiltinFunction::Cos,
1119 BuiltinFunction::Tan,
1120 BuiltinFunction::Asin,
1121 BuiltinFunction::Acos,
1122 BuiltinFunction::Atan,
1123 BuiltinFunction::StdDev,
1125 BuiltinFunction::Range,
1127 BuiltinFunction::Slice,
1128 BuiltinFunction::Push,
1129 BuiltinFunction::Pop,
1130 BuiltinFunction::First,
1131 BuiltinFunction::Last,
1132 BuiltinFunction::Zip,
1133 BuiltinFunction::Filled,
1134 BuiltinFunction::Map,
1136 BuiltinFunction::Filter,
1137 BuiltinFunction::Reduce,
1138 BuiltinFunction::ForEach,
1139 BuiltinFunction::Find,
1140 BuiltinFunction::FindIndex,
1141 BuiltinFunction::Some,
1142 BuiltinFunction::Every,
1143 BuiltinFunction::Print,
1145 BuiltinFunction::Format,
1146 BuiltinFunction::Len,
1147 BuiltinFunction::Snapshot,
1148 BuiltinFunction::Exit,
1149 BuiltinFunction::ObjectRest,
1151 BuiltinFunction::ControlFold,
1153 BuiltinFunction::TypeOf,
1155 BuiltinFunction::IsNumber,
1156 BuiltinFunction::IsString,
1157 BuiltinFunction::IsBool,
1158 BuiltinFunction::IsArray,
1159 BuiltinFunction::IsObject,
1160 BuiltinFunction::IsDataRow,
1161 BuiltinFunction::ToString,
1163 BuiltinFunction::ToNumber,
1164 BuiltinFunction::ToBool,
1165 BuiltinFunction::IntoInt,
1166 BuiltinFunction::IntoNumber,
1167 BuiltinFunction::IntoDecimal,
1168 BuiltinFunction::IntoBool,
1169 BuiltinFunction::IntoString,
1170 BuiltinFunction::TryIntoInt,
1171 BuiltinFunction::TryIntoNumber,
1172 BuiltinFunction::TryIntoDecimal,
1173 BuiltinFunction::TryIntoBool,
1174 BuiltinFunction::TryIntoString,
1175 BuiltinFunction::NativePtrSize,
1177 BuiltinFunction::NativePtrNewCell,
1178 BuiltinFunction::NativePtrFreeCell,
1179 BuiltinFunction::NativePtrReadPtr,
1180 BuiltinFunction::NativePtrWritePtr,
1181 BuiltinFunction::NativeTableFromArrowC,
1182 BuiltinFunction::NativeTableFromArrowCTyped,
1183 BuiltinFunction::NativeTableBindType,
1184 BuiltinFunction::FormatValueWithMeta,
1186 BuiltinFunction::FormatValueWithSpec,
1187 BuiltinFunction::IntrinsicSum,
1189 BuiltinFunction::IntrinsicMean,
1190 BuiltinFunction::IntrinsicMin,
1191 BuiltinFunction::IntrinsicMax,
1192 BuiltinFunction::IntrinsicStd,
1193 BuiltinFunction::IntrinsicVariance,
1194 BuiltinFunction::IntrinsicRandom,
1196 BuiltinFunction::IntrinsicRandomInt,
1197 BuiltinFunction::IntrinsicRandomSeed,
1198 BuiltinFunction::IntrinsicRandomNormal,
1199 BuiltinFunction::IntrinsicRandomArray,
1200 BuiltinFunction::IntrinsicDistUniform,
1202 BuiltinFunction::IntrinsicDistLognormal,
1203 BuiltinFunction::IntrinsicDistExponential,
1204 BuiltinFunction::IntrinsicDistPoisson,
1205 BuiltinFunction::IntrinsicDistSampleN,
1206 BuiltinFunction::IntrinsicBrownianMotion,
1208 BuiltinFunction::IntrinsicGbm,
1209 BuiltinFunction::IntrinsicOuProcess,
1210 BuiltinFunction::IntrinsicRandomWalk,
1211 BuiltinFunction::IntrinsicRollingSum,
1213 BuiltinFunction::IntrinsicRollingMean,
1214 BuiltinFunction::IntrinsicRollingStd,
1215 BuiltinFunction::IntrinsicRollingMin,
1216 BuiltinFunction::IntrinsicRollingMax,
1217 BuiltinFunction::IntrinsicEma,
1218 BuiltinFunction::IntrinsicLinearRecurrence,
1219 BuiltinFunction::IntrinsicShift,
1221 BuiltinFunction::IntrinsicDiff,
1222 BuiltinFunction::IntrinsicPctChange,
1223 BuiltinFunction::IntrinsicFillna,
1224 BuiltinFunction::IntrinsicCumsum,
1225 BuiltinFunction::IntrinsicCumprod,
1226 BuiltinFunction::IntrinsicClip,
1227 BuiltinFunction::IntrinsicCorrelation,
1229 BuiltinFunction::IntrinsicCovariance,
1230 BuiltinFunction::IntrinsicPercentile,
1231 BuiltinFunction::IntrinsicMedian,
1232 BuiltinFunction::IntrinsicCharCode,
1234 BuiltinFunction::IntrinsicFromCharCode,
1235 BuiltinFunction::IntrinsicSeries,
1237 BuiltinFunction::IntrinsicVecAbs,
1239 BuiltinFunction::IntrinsicVecSqrt,
1240 BuiltinFunction::IntrinsicVecLn,
1241 BuiltinFunction::IntrinsicVecExp,
1242 BuiltinFunction::IntrinsicVecAdd,
1243 BuiltinFunction::IntrinsicVecSub,
1244 BuiltinFunction::IntrinsicVecMul,
1245 BuiltinFunction::IntrinsicVecDiv,
1246 BuiltinFunction::IntrinsicVecMax,
1247 BuiltinFunction::IntrinsicVecMin,
1248 BuiltinFunction::IntrinsicVecSelect,
1249 BuiltinFunction::IntrinsicMatMulVec,
1251 BuiltinFunction::IntrinsicMatMulMat,
1252 BuiltinFunction::EvalTimeRef,
1254 BuiltinFunction::EvalDateTimeExpr,
1255 BuiltinFunction::EvalDataDateTimeRef,
1256 BuiltinFunction::EvalDataSet,
1257 BuiltinFunction::EvalDataRelative,
1258 BuiltinFunction::EvalDataRelativeRange,
1259 BuiltinFunction::SomeCtor,
1261 BuiltinFunction::OkCtor,
1262 BuiltinFunction::ErrCtor,
1263 BuiltinFunction::HashMapCtor,
1264 BuiltinFunction::SetCtor,
1265 BuiltinFunction::DequeCtor,
1266 BuiltinFunction::PriorityQueueCtor,
1267 BuiltinFunction::JsonObjectGet,
1269 BuiltinFunction::JsonArrayAt,
1270 BuiltinFunction::JsonObjectKeys,
1271 BuiltinFunction::JsonArrayLen,
1272 BuiltinFunction::JsonObjectLen,
1273 BuiltinFunction::WindowRowNumber,
1275 BuiltinFunction::WindowRank,
1276 BuiltinFunction::WindowDenseRank,
1277 BuiltinFunction::WindowNtile,
1278 BuiltinFunction::WindowLag,
1279 BuiltinFunction::WindowLead,
1280 BuiltinFunction::WindowFirstValue,
1281 BuiltinFunction::WindowLastValue,
1282 BuiltinFunction::WindowNthValue,
1283 BuiltinFunction::WindowSum,
1284 BuiltinFunction::WindowAvg,
1285 BuiltinFunction::WindowMin,
1286 BuiltinFunction::WindowMax,
1287 BuiltinFunction::WindowCount,
1288 BuiltinFunction::JoinExecute,
1290 BuiltinFunction::Reflect,
1291 BuiltinFunction::MakeContentText,
1293 BuiltinFunction::MakeContentFragment,
1294 BuiltinFunction::ApplyContentStyle,
1295 BuiltinFunction::MakeContentChartFromValue,
1296 BuiltinFunction::ContentChart,
1297 BuiltinFunction::ContentTextCtor,
1298 BuiltinFunction::ContentTableCtor,
1299 BuiltinFunction::ContentCodeCtor,
1300 BuiltinFunction::ContentKvCtor,
1301 BuiltinFunction::ContentFragmentCtor,
1302 BuiltinFunction::DateTimeNow,
1304 BuiltinFunction::DateTimeUtc,
1305 BuiltinFunction::DateTimeParse,
1306 BuiltinFunction::DateTimeFromEpoch,
1307 BuiltinFunction::MutexCtor,
1309 BuiltinFunction::AtomicCtor,
1310 BuiltinFunction::LazyCtor,
1311 BuiltinFunction::ChannelCtor,
1312 BuiltinFunction::Sign,
1314 BuiltinFunction::Gcd,
1315 BuiltinFunction::Lcm,
1316 BuiltinFunction::Hypot,
1317 BuiltinFunction::Clamp,
1318 BuiltinFunction::IsNaN,
1319 BuiltinFunction::IsFinite,
1320 BuiltinFunction::MakeTableFromRows,
1322 ];
1323 VARIANTS.get(id as usize).copied()
1324 }
1325}