From ebc684c4a074f83fc0a8fab0cc199e05fd00d8e4 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Fri, 2 Aug 2019 11:31:31 +0300 Subject: [PATCH] fix: make require to work without setting global module, exports --- js/initcontext.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/js/initcontext.go b/js/initcontext.go index 074d46fb203..ecd3008a1f3 100644 --- a/js/initcontext.go +++ b/js/initcontext.go @@ -138,17 +138,10 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) { 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) pgm.module = i.runtime.NewObject() _ = pgm.module.Set("exports", exports) - i.runtime.Set("module", pgm.module) + if pgm.pgm == nil { // Load the sources; the loader takes care of remote loading, etc. data, err := loader.Load(i.filesystems, fileURL, name) @@ -168,17 +161,23 @@ func (i *InitContext) requireFile(name string) (goja.Value, error) { 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 } + if call, ok := goja.AssertFunction(f); ok { + if _, err = call(exports, pgm.module, exports); err != nil { + return nil, err + } + } } 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 }