Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Fix js exports issues #1099

Merged
merged 4 commits into from
Aug 8, 2019
Merged
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
37 changes: 17 additions & 20 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import (
)

type programWithSource struct {
pgm *goja.Program
src string
exports goja.Value
pgm *goja.Program
src string
module *goja.Object
}

// InitContext provides APIs for use in the init context.
Expand Down Expand Up @@ -135,20 +135,13 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) {

// First, check if we have a cached program already.
pgm, ok := i.programs[fileURL.String()]
if !ok || pgm.exports == nil {
if !ok || pgm.module == nil {
i.pwd = loader.Dir(fileURL)
defer func() { i.pwd = pwd }()

// Swap the importing scope's exports out, then put it back again.
oldExports := i.runtime.Get("exports")
defer i.runtime.Set("exports", oldExports)
oldModule := i.runtime.Get("module")
defer i.runtime.Set("module", oldModule)
exports := i.runtime.NewObject()
i.runtime.Set("exports", exports)
module := i.runtime.NewObject()
_ = module.Set("exports", exports)
i.runtime.Set("module", module)
pgm.module = i.runtime.NewObject()
_ = pgm.module.Set("exports", exports)

if pgm.pgm == nil {
// Load the sources; the loader takes care of remote loading, etc.
data, err := loader.Load(i.filesystems, fileURL, name)
Expand All @@ -165,22 +158,26 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) {
}
}

pgm.exports = module.Get("exports")
i.programs[fileURL.String()] = pgm

// Run the program.
if _, err := i.runtime.RunProgram(pgm.pgm); err != nil {
f, err := i.runtime.RunProgram(pgm.pgm)
if err != nil {
delete(i.programs, fileURL.String())
return goja.Undefined(), err
}

pgm.exports = module.Get("exports")
if call, ok := goja.AssertFunction(f); ok {
if _, err = call(exports, pgm.module, exports); err != nil {
na-- marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
}
}

return pgm.exports, nil
return pgm.module.Get("exports"), nil
}

func (i *InitContext) compileImport(src, filename string) (*goja.Program, error) {
pgm, _, err := i.compiler.Compile(src, filename, "(function(){\n", "\n})()\n", true)
pgm, _, err := i.compiler.Compile(src, filename, "(function(module, exports){\n", "\n})\n", true)
return pgm, err
}

Expand Down
95 changes: 80 additions & 15 deletions js/module_loading_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,50 @@ func newDevNullSampleChannel() chan stats.SampleContainer {
}

func TestLoadOnceGlobalVars(t *testing.T) {
fs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fs, "/C.js", []byte(`
var globalVar;
if (!globalVar) {
globalVar = Math.random();
}
export function C() {
return globalVar;
}
`), os.ModePerm))
var testCases = map[string]string{
"module.exports": `
var globalVar;
if (!globalVar) {
globalVar = Math.random();
}
function C() {
return globalVar;
}
module.exports = {
C: C,
}
`,
"direct export": `

require.NoError(t, afero.WriteFile(fs, "/A.js", []byte(`
var globalVar;
if (!globalVar) {
globalVar = Math.random();
}
export function C() {
return globalVar;
}
`,
}
for name, data := range testCases {
cData := data
t.Run(name, func(t *testing.T) {

fs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fs, "/C.js", []byte(cData), os.ModePerm))

require.NoError(t, afero.WriteFile(fs, "/A.js", []byte(`
import { C } from "./C.js";
export function A() {
return C();
}
`), os.ModePerm))
require.NoError(t, afero.WriteFile(fs, "/B.js", []byte(`
import { C } from "./C.js";
require.NoError(t, afero.WriteFile(fs, "/B.js", []byte(`
var c = require("./C.js");
export function B() {
return C();
return c.C();
}
`), os.ModePerm))
r1, err := getSimpleRunnerWithFileFs("/script.js", `
r1, err := getSimpleRunnerWithFileFs("/script.js", `
import { A } from "./A.js";
import { B } from "./B.js";

Expand All @@ -79,6 +99,51 @@ func TestLoadOnceGlobalVars(t *testing.T) {
}
}
`, fs)
require.NoError(t, err)

arc := r1.MakeArchive()
r2, err := NewFromArchive(arc, lib.RuntimeOptions{})
require.NoError(t, err)

runners := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range runners {
r := r
t.Run(name, func(t *testing.T) {
ch := newDevNullSampleChannel()
defer close(ch)
vu, err := r.NewVU(ch)
require.NoError(t, err)
err = vu.RunOnce(context.Background())
require.NoError(t, err)
})
}
})
}
}

func TestLoadExportsIsUsableInModule(t *testing.T) {
fs := afero.NewMemMapFs()
require.NoError(t, afero.WriteFile(fs, "/A.js", []byte(`
export function A() {
return "A";
}
export function B() {
return exports.A() + "B";
}
`), os.ModePerm))
r1, err := getSimpleRunnerWithFileFs("/script.js", `
import { A, B } from "./A.js";

export default function(data) {
if (A() != "A") {
throw new Error("wrong value of A() " + A());
}

if (B() != "AB") {
throw new Error("wrong value of B() " + B());
}
}
`, fs)
require.NoError(t, err)

arc := r1.MakeArchive()
Expand Down