Skip to content

Commit 75f085d

Browse files
fix: add the ability to provide a random source
1 parent de1e909 commit 75f085d

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

engine.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ func WithDebug() Opt {
113113
}
114114
}
115115

116+
// WithRandSource sets the random source to use in the engine.
117+
func WithRandSource(randSource func() float64) Opt {
118+
return func(e *Engine) {
119+
e.randSource = randSource
120+
}
121+
}
122+
116123
// Engine provides the templating engine.
117124
type Engine struct {
118125
searchLocations []string
@@ -125,6 +132,8 @@ type Engine struct {
125132

126133
tracer trace.Tracer
127134

135+
randSource vm.RandSource
136+
128137
vm *vm.VM
129138
}
130139

@@ -248,7 +257,7 @@ func (e *Engine) init(ctx context.Context, data any) (*vm.VM, error) {
248257
return nil, ErrAlreadyInitialized
249258
}
250259

251-
v, err := vm.New()
260+
v, err := vm.New(e.randSource)
252261
if err != nil {
253262
return nil, fmt.Errorf("failed to create vm: %w", err)
254263
}

internal/vm/vm.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ type Options struct {
5050
// Option represents an option for running a script.
5151
type Option func(*Options)
5252

53+
type RandSource func() float64
54+
5355
// WithStartingLineNumber sets the starting line number for the script.
5456
func WithStartingLineNumber(lineNumber int) Option {
5557
return func(o *Options) {
@@ -63,7 +65,7 @@ type program struct {
6365
}
6466

6567
// New creates a new VM.
66-
func New() (*VM, error) {
68+
func New(randSource RandSource) (*VM, error) {
6769
g := goja.New()
6870
_, err := g.RunString(underscore.JS)
6971
if err != nil {
@@ -73,6 +75,12 @@ func New() (*VM, error) {
7375
new(require.Registry).Enable(g)
7476
console.Enable(g)
7577

78+
if randSource != nil {
79+
g.SetRandSource(func() float64 {
80+
return randSource()
81+
})
82+
}
83+
7684
return &VM{Runtime: g, globalSourceMapCache: make(map[string]*sourcemap.Consumer)}, nil
7785
}
7886

internal/vm/vm_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,30 @@ import (
99
"github.com/stretchr/testify/require"
1010
)
1111

12+
func TestVM_Run_Runtime_Success(t *testing.T) {
13+
v, err := vm.New(nil)
14+
require.NoError(t, err)
15+
16+
typeScript := `console.log("hello world");`
17+
18+
_, err = v.Run(context.Background(), "test", typeScript)
19+
assert.NoError(t, err)
20+
}
21+
22+
func TestVM_Run_Runtime_WithRandSource_Success(t *testing.T) {
23+
v, err := vm.New(func() float64 {
24+
return 0
25+
})
26+
require.NoError(t, err)
27+
28+
typeScript := `console.log("hello world");`
29+
30+
_, err = v.Run(context.Background(), "test", typeScript)
31+
assert.NoError(t, err)
32+
}
33+
1234
func TestVM_Run_Runtime_Errors(t *testing.T) {
13-
v, err := vm.New()
35+
v, err := vm.New(nil)
1436
require.NoError(t, err)
1537

1638
typeScript := `type Test = {

0 commit comments

Comments
 (0)