@@ -13,11 +13,6 @@ import (
13
13
"github.com/expr-lang/expr/vm/runtime"
14
14
)
15
15
16
- var MemoryBudget uint = 1e6
17
- var errorType = reflect .TypeOf ((* error )(nil )).Elem ()
18
-
19
- type Function = func (params ... any ) (any , error )
20
-
21
16
func Run (program * Program , env any ) (any , error ) {
22
17
if program == nil {
23
18
return nil , fmt .Errorf ("program is nil" )
@@ -27,15 +22,24 @@ func Run(program *Program, env any) (any, error) {
27
22
return vm .Run (program , env )
28
23
}
29
24
25
+ func Debug () * VM {
26
+ vm := & VM {
27
+ debug : true ,
28
+ step : make (chan struct {}, 0 ),
29
+ curr : make (chan int , 0 ),
30
+ }
31
+ return vm
32
+ }
33
+
30
34
type VM struct {
31
- stack []any
35
+ Stack []any
36
+ Scopes []* Scope
32
37
ip int
33
- scopes []* Scope
38
+ memory uint
39
+ memoryBudget uint
34
40
debug bool
35
41
step chan struct {}
36
42
curr chan int
37
- memory uint
38
- memoryBudget uint
39
43
}
40
44
41
45
type Scope struct {
@@ -47,15 +51,6 @@ type Scope struct {
47
51
Acc any
48
52
}
49
53
50
- func Debug () * VM {
51
- vm := & VM {
52
- debug : true ,
53
- step : make (chan struct {}, 0 ),
54
- curr : make (chan int , 0 ),
55
- }
56
- return vm
57
- }
58
-
59
54
func (vm * VM ) Run (program * Program , env any ) (_ any , err error ) {
60
55
defer func () {
61
56
if r := recover (); r != nil {
@@ -74,22 +69,22 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
74
69
}
75
70
}()
76
71
77
- if vm .stack == nil {
78
- vm .stack = make ([]any , 0 , 2 )
72
+ if vm .Stack == nil {
73
+ vm .Stack = make ([]any , 0 , 2 )
79
74
} else {
80
- vm .stack = vm .stack [0 :0 ]
75
+ vm .Stack = vm .Stack [0 :0 ]
81
76
}
82
77
83
- if vm .scopes != nil {
84
- vm .scopes = vm .scopes [0 :0 ]
78
+ if vm .Scopes != nil {
79
+ vm .Scopes = vm .Scopes [0 :0 ]
85
80
}
86
81
87
82
vm .memoryBudget = MemoryBudget
88
83
vm .memory = 0
89
84
vm .ip = 0
90
85
91
86
for vm .ip < len (program .Bytecode ) {
92
- if vm .debug {
87
+ if debug && vm .debug {
93
88
<- vm .step
94
89
}
95
90
@@ -204,7 +199,7 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
204
199
}
205
200
206
201
case OpJumpIfEnd :
207
- scope := vm .Scope ()
202
+ scope := vm .scope ()
208
203
if scope .Index >= scope .Len {
209
204
vm .ip += arg
210
205
}
@@ -399,7 +394,7 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
399
394
400
395
case OpValidateArgs :
401
396
fn := vm .pop ().(Function )
402
- mem , err := fn (vm .stack [len (vm .stack )- arg :]... )
397
+ mem , err := fn (vm .Stack [len (vm .Stack )- arg :]... )
403
398
if err != nil {
404
399
panic (err )
405
400
}
@@ -443,49 +438,49 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
443
438
vm .push (runtime .Deref (a ))
444
439
445
440
case OpIncrementIndex :
446
- vm .Scope ().Index ++
441
+ vm .scope ().Index ++
447
442
448
443
case OpDecrementIndex :
449
- scope := vm .Scope ()
444
+ scope := vm .scope ()
450
445
scope .Index --
451
446
452
447
case OpIncrementCount :
453
- scope := vm .Scope ()
448
+ scope := vm .scope ()
454
449
scope .Count ++
455
450
456
451
case OpGetIndex :
457
- vm .push (vm .Scope ().Index )
452
+ vm .push (vm .scope ().Index )
458
453
459
454
case OpSetIndex :
460
- scope := vm .Scope ()
455
+ scope := vm .scope ()
461
456
scope .Index = vm .pop ().(int )
462
457
463
458
case OpGetCount :
464
- scope := vm .Scope ()
459
+ scope := vm .scope ()
465
460
vm .push (scope .Count )
466
461
467
462
case OpGetLen :
468
- scope := vm .Scope ()
463
+ scope := vm .scope ()
469
464
vm .push (scope .Len )
470
465
471
466
case OpGetGroupBy :
472
- vm .push (vm .Scope ().GroupBy )
467
+ vm .push (vm .scope ().GroupBy )
473
468
474
469
case OpGetAcc :
475
- vm .push (vm .Scope ().Acc )
470
+ vm .push (vm .scope ().Acc )
476
471
477
472
case OpSetAcc :
478
- vm .Scope ().Acc = vm .pop ()
473
+ vm .scope ().Acc = vm .pop ()
479
474
480
475
case OpPointer :
481
- scope := vm .Scope ()
476
+ scope := vm .scope ()
482
477
vm .push (scope .Array .Index (scope .Index ).Interface ())
483
478
484
479
case OpThrow :
485
480
panic (vm .pop ().(error ))
486
481
487
482
case OpGroupBy :
488
- scope := vm .Scope ()
483
+ scope := vm .scope ()
489
484
if scope .GroupBy == nil {
490
485
scope .GroupBy = make (map [any ][]any )
491
486
}
@@ -496,46 +491,46 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
496
491
case OpBegin :
497
492
a := vm .pop ()
498
493
array := reflect .ValueOf (a )
499
- vm .scopes = append (vm .scopes , & Scope {
494
+ vm .Scopes = append (vm .Scopes , & Scope {
500
495
Array : array ,
501
496
Len : array .Len (),
502
497
})
503
498
504
499
case OpEnd :
505
- vm .scopes = vm .scopes [:len (vm .scopes )- 1 ]
500
+ vm .Scopes = vm .Scopes [:len (vm .Scopes )- 1 ]
506
501
507
502
default :
508
503
panic (fmt .Sprintf ("unknown bytecode %#x" , op ))
509
504
}
510
505
511
- if vm .debug {
506
+ if debug && vm .debug {
512
507
vm .curr <- vm .ip
513
508
}
514
509
}
515
510
516
- if vm .debug {
511
+ if debug && vm .debug {
517
512
close (vm .curr )
518
513
close (vm .step )
519
514
}
520
515
521
- if len (vm .stack ) > 0 {
516
+ if len (vm .Stack ) > 0 {
522
517
return vm .pop (), nil
523
518
}
524
519
525
520
return nil , nil
526
521
}
527
522
528
523
func (vm * VM ) push (value any ) {
529
- vm .stack = append (vm .stack , value )
524
+ vm .Stack = append (vm .Stack , value )
530
525
}
531
526
532
527
func (vm * VM ) current () any {
533
- return vm .stack [len (vm .stack )- 1 ]
528
+ return vm .Stack [len (vm .Stack )- 1 ]
534
529
}
535
530
536
531
func (vm * VM ) pop () any {
537
- value := vm .stack [len (vm .stack )- 1 ]
538
- vm .stack = vm .stack [:len (vm .stack )- 1 ]
532
+ value := vm .Stack [len (vm .Stack )- 1 ]
533
+ vm .Stack = vm .Stack [:len (vm .Stack )- 1 ]
539
534
return value
540
535
}
541
536
@@ -546,15 +541,8 @@ func (vm *VM) memGrow(size uint) {
546
541
}
547
542
}
548
543
549
- func (vm * VM ) Stack () []any {
550
- return vm .stack
551
- }
552
-
553
- func (vm * VM ) Scope () * Scope {
554
- if len (vm .scopes ) > 0 {
555
- return vm .scopes [len (vm .scopes )- 1 ]
556
- }
557
- return nil
544
+ func (vm * VM ) scope () * Scope {
545
+ return vm .Scopes [len (vm .Scopes )- 1 ]
558
546
}
559
547
560
548
func (vm * VM ) Step () {
0 commit comments