Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package qjs_test
import (
"path"
"testing"
"testing/fstest"

"github.com/fastschema/qjs"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -377,6 +378,25 @@ func TestModuleLoading(t *testing.T) {
}
runEvalTests(t, tests, "./testdata/00_loader")
})

t.Run("FS_Module_Imports", func(t *testing.T) {
rt := must(qjs.New(qjs.Option{
FS: fstest.MapFS{
"math-utils.js": &fstest.MapFile{
Data: []byte("export function add(a, b) { return a + b; }"),
},
"main.js": &fstest.MapFile{
Data: []byte("import { add } from 'math-utils.js'; export default String(add(5, 5));"),
},
},
}))
defer rt.Close()

val, err := rt.Eval("main.js", qjs.TypeModule())
defer val.Free()
assert.NoError(t, err)
assert.Equal(t, "10", val.String())
})
}

// Compilation Tests
Expand Down
2 changes: 2 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package qjs
import (
"context"
"fmt"
"io/fs"
"io"
"os"
)
Expand Down Expand Up @@ -32,6 +33,7 @@ const (

type Option struct {
CWD string
FS fs.FS
StartFunctionName string
Context context.Context
// Enabling this option significantly increases evaluation time
Expand Down
19 changes: 19 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"path/filepath"
"testing"
"testing/fstest"

"github.com/fastschema/qjs"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -66,6 +67,24 @@ func TestEvalOptions(t *testing.T) {
})
})

t.Run("FSOption", func(t *testing.T) {
runtime, err := qjs.New(qjs.Option{
FS: fstest.MapFS{
"main.js": &fstest.MapFile{
Data: []byte("export default 'from fs'"),
},
},
})
require.NoError(t, err)
defer runtime.Close()

val, err := runtime.Eval("main.js", qjs.TypeModule())
require.NoError(t, err)
defer val.Free()

assert.Equal(t, "from fs", val.String())
})

t.Run("CodeOption", func(t *testing.T) {
runtime := must(qjs.New())
defer runtime.Close()
Expand Down
9 changes: 6 additions & 3 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ func New(options ...Option) (runtime *Runtime, err error) {
return nil, fmt.Errorf("failed to setup host module: %w", err)
}

fsConfig := wazero.
NewFSConfig().
WithDirMount(runtime.option.CWD, "/")
fsConfig := wazero.NewFSConfig()
if runtime.option.FS != nil {
fsConfig = fsConfig.WithFSMount(runtime.option.FS, "/")
} else {
fsConfig = fsConfig.WithDirMount(runtime.option.CWD, "/")
}
if runtime.module, err = runtime.wrt.InstantiateModule(
option.Context,
compiledQJSModule,
Expand Down