Skip to content

Commit ae41cf2

Browse files
Merge pull request #21 from speakeasy-api/provide-rand-source
2 parents de1e909 + cc2f8d9 commit ae41cf2

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ type Options struct {
5050
// Option represents an option for running a script.
5151
type Option func(*Options)
5252

53+
// RandSource is a function that returns a seeded float64 value.
54+
type RandSource func() float64
55+
5356
// WithStartingLineNumber sets the starting line number for the script.
5457
func WithStartingLineNumber(lineNumber int) Option {
5558
return func(o *Options) {
@@ -63,7 +66,7 @@ type program struct {
6366
}
6467

6568
// New creates a new VM.
66-
func New() (*VM, error) {
69+
func New(randSource RandSource) (*VM, error) {
6770
g := goja.New()
6871
_, err := g.RunString(underscore.JS)
6972
if err != nil {
@@ -73,6 +76,12 @@ func New() (*VM, error) {
7376
new(require.Registry).Enable(g)
7477
console.Enable(g)
7578

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

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)