-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cjsmodule.go
73 lines (62 loc) · 1.72 KB
/
cjsmodule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package modules
import (
"fmt"
"net/url"
"github.com/dop251/goja"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/compiler"
)
// cjsModule represents a commonJS module
type cjsModule struct {
prg *goja.Program
url *url.URL
}
var _ module = &cjsModule{}
type cjsModuleInstance struct {
mod *cjsModule
moduleObj *goja.Object
vu VU
}
func (c *cjsModule) instantiate(vu VU) moduleInstance {
return &cjsModuleInstance{vu: vu, mod: c}
}
func (c *cjsModuleInstance) execute() error {
rt := c.vu.Runtime()
exports := rt.NewObject()
c.moduleObj = rt.NewObject()
err := c.moduleObj.Set("exports", exports)
if err != nil {
return fmt.Errorf("error while getting ready to import commonJS, couldn't set exports property of module: %w",
err)
}
// Run the program.
f, err := rt.RunProgram(c.mod.prg)
if err != nil {
return err
}
if call, ok := goja.AssertFunction(f); ok {
if _, err = call(exports, c.moduleObj, exports); err != nil {
return err
}
}
return nil
}
func (c *cjsModuleInstance) exports() *goja.Object {
exportsV := c.moduleObj.Get("exports")
if common.IsNullish(exportsV) {
return nil
}
return exportsV.ToObject(c.vu.Runtime())
}
// cjsModuleFromString is a helper function which returns CJSModule given the argument it has.
// It is mostly a wrapper around compiler.Compiler@Compile
//
// TODO: extract this to not make this package dependant on compilers.
// this is potentially a moot point after ESM when the compiler will likely get mostly dropped.
func cjsModuleFromString(fileURL *url.URL, data []byte, c *compiler.Compiler) (*cjsModule, error) {
pgm, _, err := c.Compile(string(data), fileURL.String(), false)
if err != nil {
return nil, err
}
return &cjsModule{prg: pgm, url: fileURL}, nil
}