Git Product home page Git Product logo

goja's Introduction

goja

ECMAScript 5.1(+) implementation in Go.

Go Reference

Goja is an implementation of ECMAScript 5.1 in pure Go with emphasis on standard compliance and performance.

This project was largely inspired by otto.

The minimum required Go version is 1.20.

Features

  • Full ECMAScript 5.1 support (including regex and strict mode).
  • Passes nearly all tc39 tests for the features implemented so far. The goal is to pass all of them. See .tc39_test262_checkout.sh for the latest working commit id.
  • Capable of running Babel, Typescript compiler and pretty much anything written in ES5.
  • Sourcemaps.
  • Most of ES6 functionality, still work in progress, see https://github.com/dop251/goja/milestone/1?closed=1

Known incompatibilities and caveats

WeakMap

WeakMap is implemented by embedding references to the values into the keys. This means that as long as the key is reachable all values associated with it in any weak maps also remain reachable and therefore cannot be garbage collected even if they are not otherwise referenced, even after the WeakMap is gone. The reference to the value is dropped either when the key is explicitly removed from the WeakMap or when the key becomes unreachable.

To illustrate this:

var m = new WeakMap();
var key = {};
var value = {/* a very large object */};
m.set(key, value);
value = undefined;
m = undefined; // The value does NOT become garbage-collectable at this point
key = undefined; // Now it does
// m.delete(key); // This would work too

The reason for it is the limitation of the Go runtime. At the time of writing (version 1.15) having a finalizer set on an object which is part of a reference cycle makes the whole cycle non-garbage-collectable. The solution above is the only reasonable way I can think of without involving finalizers. This is the third attempt (see #250 and #199 for more details).

Note, this does not have any effect on the application logic, but may cause a higher-than-expected memory usage.

WeakRef and FinalizationRegistry

For the reason mentioned above implementing WeakRef and FinalizationRegistry does not seem to be possible at this stage.

JSON

JSON.parse() uses the standard Go library which operates in UTF-8. Therefore, it cannot correctly parse broken UTF-16 surrogate pairs, for example:

JSON.parse(`"\\uD800"`).charCodeAt(0).toString(16) // returns "fffd" instead of "d800"

Date

Conversion from calendar date to epoch timestamp uses the standard Go library which uses int, rather than float as per ECMAScript specification. This means if you pass arguments that overflow int to the Date() constructor or if there is an integer overflow, the result will be incorrect, for example:

Date.UTC(1970, 0, 1, 80063993375, 29, 1, -288230376151711740) // returns 29256 instead of 29312

FAQ

How fast is it?

Although it's faster than many scripting language implementations in Go I have seen (for example it's 6-7 times faster than otto on average) it is not a replacement for V8 or SpiderMonkey or any other general-purpose JavaScript engine. You can find some benchmarks here.

Why would I want to use it over a V8 wrapper?

It greatly depends on your usage scenario. If most of the work is done in javascript (for example crypto or any other heavy calculations) you are definitely better off with V8.

If you need a scripting language that drives an engine written in Go so that you need to make frequent calls between Go and javascript passing complex data structures then the cgo overhead may outweigh the benefits of having a faster javascript engine.

Because it's written in pure Go there are no cgo dependencies, it's very easy to build and it should run on any platform supported by Go.

It gives you a much better control over execution environment so can be useful for research.

Is it goroutine-safe?

No. An instance of goja.Runtime can only be used by a single goroutine at a time. You can create as many instances of Runtime as you like but it's not possible to pass object values between runtimes.

Where is setTimeout()?

setTimeout() assumes concurrent execution of code which requires an execution environment, for example an event loop similar to nodejs or a browser. There is a separate project aimed at providing some NodeJS functionality, and it includes an event loop.

Can you implement (feature X from ES6 or higher)?

I will be adding features in their dependency order and as quickly as time permits. Please do not ask for ETAs. Features that are open in the milestone are either in progress or will be worked on next.

The ongoing work is done in separate feature branches which are merged into master when appropriate. Every commit in these branches represents a relatively stable state (i.e. it compiles and passes all enabled tc39 tests), however because the version of tc39 tests I use is quite old, it may be not as well tested as the ES5.1 functionality. Because there are (usually) no major breaking changes between ECMAScript revisions it should not break your existing code. You are encouraged to give it a try and report any bugs found. Please do not submit fixes though without discussing it first, as the code could be changed in the meantime.

How do I contribute?

Before submitting a pull request please make sure that:

  • You followed ECMA standard as close as possible. If adding a new feature make sure you've read the specification, do not just base it on a couple of examples that work fine.
  • Your change does not have a significant negative impact on performance (unless it's a bugfix and it's unavoidable)
  • It passes all relevant tc39 tests.

Current Status

  • There should be no breaking changes in the API, however it may be extended.
  • Some of the AnnexB functionality is missing.

Basic Example

Run JavaScript and get the result value.

vm := goja.New()
v, err := vm.RunString("2 + 2")
if err != nil {
    panic(err)
}
if num := v.Export().(int64); num != 4 {
    panic(num)
}

Passing Values to JS

Any Go value can be passed to JS using Runtime.ToValue() method. See the method's documentation for more details.

Exporting Values from JS

A JS value can be exported into its default Go representation using Value.Export() method.

Alternatively it can be exported into a specific Go variable using Runtime.ExportTo() method.

Within a single export operation the same Object will be represented by the same Go value (either the same map, slice or a pointer to the same struct). This includes circular objects and makes it possible to export them.

Calling JS functions from Go

There are 2 approaches:

const SCRIPT = `
function sum(a, b) {
    return +a + b;
}
`

vm := goja.New()
_, err := vm.RunString(SCRIPT)
if err != nil {
    panic(err)
}
sum, ok := goja.AssertFunction(vm.Get("sum"))
if !ok {
    panic("Not a function")
}

res, err := sum(goja.Undefined(), vm.ToValue(40), vm.ToValue(2))
if err != nil {
    panic(err)
}
fmt.Println(res)
// Output: 42
const SCRIPT = `
function sum(a, b) {
    return +a + b;
}
`

vm := goja.New()
_, err := vm.RunString(SCRIPT)
if err != nil {
    panic(err)
}

var sum func(int, int) int
err = vm.ExportTo(vm.Get("sum"), &sum)
if err != nil {
    panic(err)
}

fmt.Println(sum(40, 2)) // note, _this_ value in the function will be undefined.
// Output: 42

The first one is more low level and allows specifying this value, whereas the second one makes the function look like a normal Go function.

Mapping struct field and method names

By default, the names are passed through as is which means they are capitalised. This does not match the standard JavaScript naming convention, so if you need to make your JS code look more natural or if you are dealing with a 3rd party library, you can use a FieldNameMapper:

vm := goja.New()
vm.SetFieldNameMapper(TagFieldNameMapper("json", true))
type S struct {
    Field int `json:"field"`
}
vm.Set("s", S{Field: 42})
res, _ := vm.RunString(`s.field`) // without the mapper it would have been s.Field
fmt.Println(res.Export())
// Output: 42

There are two standard mappers: TagFieldNameMapper and UncapFieldNameMapper, or you can use your own implementation.

Native Constructors

In order to implement a constructor function in Go use func (goja.ConstructorCall) *goja.Object. See Runtime.ToValue() documentation for more details.

Regular Expressions

Goja uses the embedded Go regexp library where possible, otherwise it falls back to regexp2.

Exceptions

Any exception thrown in JavaScript is returned as an error of type *Exception. It is possible to extract the value thrown by using the Value() method:

vm := goja.New()
_, err := vm.RunString(`

throw("Test");

`)

if jserr, ok := err.(*Exception); ok {
    if jserr.Value().Export() != "Test" {
        panic("wrong value")
    }
} else {
    panic("wrong type")
}

If a native Go function panics with a Value, it is thrown as a Javascript exception (and therefore can be caught):

var vm *Runtime

func Test() {
    panic(vm.ToValue("Error"))
}

vm = goja.New()
vm.Set("Test", Test)
_, err := vm.RunString(`

try {
    Test();
} catch(e) {
    if (e !== "Error") {
        throw e;
    }
}

`)

if err != nil {
    panic(err)
}

Interrupting

func TestInterrupt(t *testing.T) {
    const SCRIPT = `
    var i = 0;
    for (;;) {
        i++;
    }
    `

    vm := goja.New()
    time.AfterFunc(200 * time.Millisecond, func() {
        vm.Interrupt("halt")
    })

    _, err := vm.RunString(SCRIPT)
    if err == nil {
        t.Fatal("Err is nil")
    }
    // err is of type *InterruptError and its Value() method returns whatever has been passed to vm.Interrupt()
}

NodeJS Compatibility

There is a separate project aimed at providing some of the NodeJS functionality.

goja's People

Contributors

adamkeys avatar danieldocu avatar dennwc avatar dependabot[bot] avatar dop251 avatar federicotdn avatar ganigeorgiev avatar gballet avatar hswanepoel-godaddy avatar kudla avatar levi-nz avatar mansoor-s avatar monkeywie avatar mstoykov avatar noctarius avatar notsatvrn avatar nwidger avatar olegator77 avatar olegbespalov avatar palumacil avatar profer avatar shiroyk avatar slavikm avatar steve-gray avatar tyamagu2 avatar vanackere avatar zupa-hu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

goja's Issues

Babel reorders exported variables

I'm not sure why this is happening, but if Babel is run through goja and it processes export statements, they will be reordered in a seemingly arbitrary (but deterministic) fashion in the generated code.

This is normally harmless, except when the exported members are classes, which need to be initialized after their superclasses.

This script reproduces the bug - the number of members are significant, as only certain positions are swapped with each other.

export const Var1 = 0;
export const Var2 = 1;
export const Var3 = 2;
export const Var4 = 3;

export class Superclass {}

export class ClassA extends Superclass {}
export class ClassB extends Superclass {}
export class ClassC extends Superclass {}
export class ClassD extends Superclass {}

Running babel-standalone through node.js correctly produces... (amended for brevity):

function _possibleConstructorReturn(self, call) { /* ... */ }
function _inherits(subClass, superClass) { /* ... */ }
function _classCallCheck(instance, Constructor) { /* ... */ }

var Var1 = exports.Var1 = 0;
var Var2 = exports.Var2 = 1;
var Var3 = exports.Var3 = 2;
var Var4 = exports.Var4 = 3;

var Superclass = exports.Superclass = function Superclass() { /* ... */ };
var ClassA = exports.ClassA = function (_Superclass) { /* ... */ }(Superclass);
var ClassB = exports.ClassB = function (_Superclass2) { /* ... */ }(Superclass);
var ClassC = exports.ClassC = function (_Superclass3) { /* ... */ }(Superclass);
var ClassD = exports.ClassD = function (_Superclass4) { /* ... */ }(Superclass);

However, running it through goja produces:

var Var2 = exports.Var2 = 1;
var Var3 = exports.Var3 = 2;

function _classCallCheck(instance, Constructor) { /* ... */ }

var Var1 = exports.Var1 = 0;

function _inherits(subClass, superClass) { /* ... */ }
function _possibleConstructorReturn(self, call) { /* ... */ }

var ClassC = exports.ClassC = function (_Superclass3) { /* ... */ }(Superclass);

var Superclass = exports.Superclass = function Superclass() { /* ... */ };

var ClassA = exports.ClassA = function (_Superclass) { /* ... */ }(Superclass);
var ClassB = exports.ClassB = function (_Superclass2) { /* ... */ }(Superclass);

var Var4 = exports.Var4 = 3;

var ClassD = exports.ClassD = function (_Superclass4) { /* ... */ }(Superclass);

In which the (individually correct) pieces are jumbled, in a way that puts ClassC above Superclass, thus making it inherit from undefined, which produces a runtime error.

Demo: https://github.com/uppfinnarn/goja-babel-bug

Adding native constructors

I've forked and started dabbling into allowing package users to create "native" constructors and instances.

I'm implementing part of the Web API (Response, Request, Headers, etc.) and needed some way to create more complex instances that kept a state outside of goja.

Here's my rough implementation so far:

// constructor.go

package goja

type NativeFunction func(FunctionCall) Value
type ConstructorFunction func(args []Value, inst *Object) *Object

type Method struct {
	call   NativeFunction
	length int
}

type Constructor struct {
	Name string
	r    *Runtime

	proto  *Object
	static *Object

	ctor   ConstructorFunction
	length int

	methods map[string]Method
}

func (r *Runtime) NewConstructor(name string, ctor ConstructorFunction, length int) *Constructor {
	return &Constructor{
		Name:    name,
		r:       r,
		ctor:    ctor,
		length:  length,
		methods: map[string]Method{},
	}
}

func (c *Constructor) createProto(val *Object) objectImpl {
	o := &baseObject{
		class:      c.Name,
		val:        val,
		extensible: true,
		prototype:  c.r.global.ObjectPrototype,
	}
	o.init()

	for name, method := range c.methods {
		o._putProp(name, c.r.newNativeFunc(method.call, nil, name, nil, method.length), true, false, true)
	}

	return o
}

func (c *Constructor) createStatic(val *Object) objectImpl {
	o := c.r.newNativeFuncConstructObj(val, c.ctor, c.Name, c.proto, c.length)

	// _putProp here...

	return o
}

func (c *Constructor) NewInstance(args []Value, proto *Object) *Object {
	bo := c.r.newBaseObject(proto, c.Name)
	val := c.ctor(args, bo.val)
	return val
}

func (c *Constructor) Init() {
	c.proto = c.r.newLazyObject(c.createProto)
	c.static = c.r.newLazyObject(c.createStatic)

	c.r.addToGlobal(c.Name, c.static)
}

func (c *Constructor) DefineFunction(name string, call NativeFunction, length int) {
	c.methods[name] = Method{call, length}
}

Surprisingly, this works ok. Here's an example of how I'm using it:

func constructResponse(vm *goja.Runtime) goja.ConstructorFunction {
	return func(args []goja.Value, res *goja.Object) *goja.Object {
		res.Set("body", nil)
		res.Set("bodyUsed", false)
		res.Set("ok", true)
		res.Set("redirected", false)
		res.Set("status", 200)
		res.Set("statusText", "OK")
		res.Set("type", "default")
		res.Set("url", "")

		res.Set("text", bodyText(vm, strings.NewReader("")))

		return res
	}
}

func initResponse(vm *goja.Runtime) {
	c := vm.NewConstructor("Response", constructResponse(vm), 2)
	c.Init()
}

This works. Except I'm left with the same issue trying to keep state around. I have to create a bunch of closures.

For instance, for the text() function: I'd like it to keep around a io.Reader and read from it from the text() function.

As far as I can tell, there's already a similar pattern in goja. Namely, the newDateObject function seems to create a special kind of object that embed baseObject and a few more fields. It makes sense, except that's all unexported and I'm not sure how I can make it generic enough.

I'm mostly looking for advice on how to proceed. Ideally, from a package user's perspective, the developer would only have to provide a constructor and a struct instance to make it work.

I think maybe abstracting and exporting a few more structures would help. I guess I'd mostly need to create a few structs embedding baseObject or implementing objectImpl.

Any thoughts?

(*vm).Interrupt causes a data race

Per pull request #14, (*vm).Interrupt causes a data race. This results in undefined behavior and means that interrupts are currently unsafe to use. It can be resolved in the short term by using atomics (as in #14) or other explicit synchronization to signal the interrupt from a separate goroutine, and in the long term may be fixed in another manner.

As discussed in #14, it is unacceptable for a data race to be included in a critical part of a VM, especially as part of an interrupt. According to @dop251, the data race and undefined behavior here is intentional in the pursuit of performance, and objects to the use of atomics to resolve this. The goal, then, is to find another method of causing an interrupt that both satisfies @dop251's performance requirements (currently unstated) and eliminates the data race, preferably without introducing unsafe code that cannot be checked by the race detector.

This issue is intended to discuss other approaches to removing the interrupt data race, knowing that atomics are off the table (or somehow too slow).

limit resource usage

I feel this might not be possible, but something missing in otto is being able to somehow limit how much memory a VM is allowed to use, or CPU (there's a time based interrupt thingy), and so on.

It would be pretty cool if this package allowed strict control over these.

Nice work, love to see more language interpreters!

TypeError: Object has no member 'error'

Hi, I realise this is a duplicate of #27 but it was closed so I thought I'd create a new one. The line that is causing this error for me was calling a react-dom/server render to string with a react-redux Provider wrapper around a RouterContext.

An easy way to see this happen is to clone latest of https://github.com/olebedev/go-starter-kit and run 'make install' and 'make serve' to run the app. With js disabled, no server side render occurs. You just have to change line 235 to assign an error var and check if it's not nil in server/react.go. I just panicked on it to produce output similar to:

main.(*JSVM).Handle.func1(0xc420180180)
        /Users/pieter/coding/go/src/teststarter/server/react.go:238 +0x232
teststarter/vendor/github.com/dop251/goja_nodejs/eventloop.(*EventLoop).RunOnLoop.func1()
        /Users/pieter/coding/go/src/teststarter/vendor/github.com/dop251/goja_nodejs/eventloop/eventloop.go:109 +0x31
teststarter/vendor/github.com/dop251/goja_nodejs/eventloop.(*EventLoop).run(0xc4201b0000, 0xc4201b0201)
        /Users/pieter/coding/go/src/teststarter/vendor/github.com/dop251/goja_nodejs/eventloop/eventloop.go:116 +0x77
created by teststarter/vendor/github.com/dop251/goja_nodejs/eventloop.(*EventLoop).Start
        /Users/pieter/coding/go/src/teststarter/vendor/github.com/dop251/goja_nodejs/eventloop/eventloop.go:87 +0x44

I had the exact same error with a smaller test that renders out a react app, and the same error occurred when react-redux's connect function was used. I don't really know where to start looking for a cause to this. If you've got an idea of where I can start looking, I might be able to help fix the issue. Thanks!

My go env is:

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/pieter/coding/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8.1/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8.1/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ss/7vgx61gj12373gbg_pbst6pw0000gn/T/go-build423786578=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

How do I get the javascript error trace?

Hi,

I am trying to debug this issue Object has no memeber 'assign'

How can I get the stacktrace when there is an error in the javascript?
I am using e.stack, but it's giving me an undefined value.

JS code:
result.error = "Render Error: " + e + "\n\t\tStackTrace: " + e.stack + "."

outcome:
Render Error: TypeError: Object has no member 'assign'
StackTrace: undefined.

Make object/var/function immutable/constant?

I'm wondering how I could make a value created using vm.Set() constant or immutible. I know that ES5 doesn't support const but what about Object.freeze or similar methods? I am trying to make sure the JS callable Go functions and mapped vars can't be accidentally overwritten to another value.

goja panics on TypeScript compiler

When I try to run typescriptServices.js downloaded from here through goja's vm.RunString method it panics.

Here is the small test program I used:

package main

import (
	"fmt"
	"io/ioutil"
	"log"

	"github.com/dop251/goja"
)

func main() {
	buf, err := ioutil.ReadFile("typescriptServices.js")
	if err != nil {
		log.Fatalln(err)
	}

	vm := goja.New()
	v, err := vm.RunString(string(buf))
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(v)

	str := `
interface Person {
    firstName: string;
    lastName: string;
}

function greeter(person: Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

var user = { firstName: "Jane", lastName: "User" };

document.body.innerHTML = greeter(user);
`

	v, err = vm.RunString(`ts.compile('` + str + `')`)
	if err != nil {
		log.Fatalln("compile:", err)
	}

	fmt.Println(v)
}

Running the test program produces this output:

$ go run main.go
panic: Unknown statement type: *ast.DebuggerStatement [recovered]
	panic: Unknown statement type: *ast.DebuggerStatement

goroutine 1 [running]:
panic(0x277880, 0xc42014b940)
	/usr/local/go/src/runtime/panic.go:500 +0x1a1
github.com/dop251/goja.compile.func1(0xc4219c3d40, 0xc4219c3d48)
	/Users/niels/go/src/github.com/dop251/goja/runtime.go:679 +0xc9
panic(0x277880, 0xc42014b940)
	/usr/local/go/src/runtime/panic.go:458 +0x243
github.com/dop251/goja.(*compiler).compileStatement(0xc421db6180, 0x4023a0, 0xc420f3f658, 0xc4219c2700)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:53 +0x186
github.com/dop251/goja.(*compiler).compileStatements(0xc421db6180, 0xc420f3c2c0, 0x4, 0x4, 0xc421f75400)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:562 +0x96
github.com/dop251/goja.(*compiler).compileBlockStatement(0xc421db6180, 0xc420f378f0, 0x27fa00)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:586 +0x54
github.com/dop251/goja.(*compiler).compileStatement(0xc421db6180, 0x402220, 0xc420f378f0, 0x0)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:16 +0x397
github.com/dop251/goja.(*compiler).compileIfStatement(0xc421db6180, 0xc420f3c200, 0x0)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:517 +0x543
github.com/dop251/goja.(*compiler).compileStatement(0xc421db6180, 0x4025e0, 0xc420f3c200, 0xc41ff03b00)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:24 +0x3cc
github.com/dop251/goja.(*compiler).compileStatements(0xc421db6180, 0xc420f3fe40, 0x1, 0x1, 0x5f1000)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:567 +0x10c
github.com/dop251/goja.(*compiler).compileBlockStatement(0xc421db6180, 0xc420f37890, 0xffffffffffffff00)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:586 +0x54
github.com/dop251/goja.(*compiler).compileStatement(0xc421db6180, 0x402220, 0xc420f37890, 0x0)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:16 +0x397
github.com/dop251/goja.(*compiledFunctionLiteral).emitGetter(0xc421f89cc0, 0xa7401)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:785 +0x7bc
github.com/dop251/goja.(*compiler).emitExpr(0xc421db6180, 0x403c60, 0xc421f89cc0, 0xc421f90b01)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:990 +0x8c
github.com/dop251/goja.(*compiler).emitVarSetter.func1(0xc421f90b00)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:352 +0x41
github.com/dop251/goja.(*compiler).emitVarSetter1(0xc421db6180, 0xc42097d91a, 0x6, 0x23911, 0xc4219c2ec0)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:323 +0x408
github.com/dop251/goja.(*compiler).emitVarSetter(0xc421db6180, 0xc42097d91a, 0x6, 0x23911, 0x403c60, 0xc421f89cc0)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:353 +0x7b
github.com/dop251/goja.(*compiledIdentifierExpr).emitSetter(0xc4219c2f70, 0x403c60, 0xc421f89cc0)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:361 +0x5d
github.com/dop251/goja.(*compiler).compileFunction(0xc421db6180, 0xc4200a93d0)
	/Users/niels/go/src/github.com/dop251/goja/compiler.go:416 +0xd5
github.com/dop251/goja.(*compiler).compileFunctions(0xc421db6180, 0xc420f3c340, 0x3, 0x4)
	/Users/niels/go/src/github.com/dop251/goja/compiler.go:312 +0x76
github.com/dop251/goja.(*compiledFunctionLiteral).emitGetter(0xc421f89be0, 0xc4219c3301)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:784 +0x78d
github.com/dop251/goja.(*compiledCallExpr).emitGetter(0xc421f8d580, 0x402300)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:1432 +0x90d
github.com/dop251/goja.(*compiler).compileExpressionStatement(0xc421db6180, 0xc420f40670, 0xc4219c3500)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:594 +0xbe
github.com/dop251/goja.(*compiler).compileStatement(0xc421db6180, 0x4024a0, 0xc420f40670, 0x27fa00)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:18 +0x63d
github.com/dop251/goja.(*compiler).compileStatements(0xc421db6180, 0xc420f3a000, 0x11d, 0x200, 0xc4219c3500)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:562 +0x96
github.com/dop251/goja.(*compiler).compileBlockStatement(0xc421db6180, 0xc420e82cf0, 0x400500)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:586 +0x54
github.com/dop251/goja.(*compiler).compileStatement(0xc421db6180, 0x402220, 0xc420e82cf0, 0x100)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:16 +0x397
github.com/dop251/goja.(*compiledFunctionLiteral).emitGetter(0xc421ece020, 0xc4219c3901)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:785 +0x7bc
github.com/dop251/goja.(*compiledCallExpr).emitGetter(0xc421eca540, 0x402300)
	/Users/niels/go/src/github.com/dop251/goja/compiler_expr.go:1432 +0x90d
github.com/dop251/goja.(*compiler).compileExpressionStatement(0xc421db6180, 0xc420f4a410, 0xc421db3600)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:594 +0xbe
github.com/dop251/goja.(*compiler).compileStatement(0xc421db6180, 0x4024a0, 0xc420f4a410, 0x0)
	/Users/niels/go/src/github.com/dop251/goja/compiler_stmt.go:18 +0x63d
github.com/dop251/goja.(*compiler).compile(0xc421db6180, 0xc421db6140)
	/Users/niels/go/src/github.com/dop251/goja/compiler.go:265 +0x219
github.com/dop251/goja.compile(0x0, 0x0, 0xc420546000, 0x4134b0, 0x0, 0x0, 0x0, 0x0)
	/Users/niels/go/src/github.com/dop251/goja/runtime.go:684 +0x3e2
github.com/dop251/goja.Compile(0x0, 0x0, 0xc420546000, 0x4134b0, 0x0, 0x0, 0x0, 0x0)
	/Users/niels/go/src/github.com/dop251/goja/runtime.go:642 +0x57
github.com/dop251/goja.(*Runtime).RunScript(0xc420096580, 0x0, 0x0, 0xc420546000, 0x4134b0, 0x0, 0x0, 0x0, 0x0)
	/Users/niels/go/src/github.com/dop251/goja/runtime.go:713 +0x52
github.com/dop251/goja.(*Runtime).RunString(0xc420096580, 0xc420546000, 0x4134b0, 0x4136b0, 0xc420546000, 0x4134b0, 0x0)
	/Users/niels/go/src/github.com/dop251/goja/runtime.go:708 +0x51
main.main()
	/Users/niels/go/src/github.com/nwidger/goja-test/main.go:18 +0xfd
exit status 2

Here is a copy of the typescriptServices.js file I used: typescriptServices.js.txt.

Ordered objects?

Is there any chance we could have objects preserve insertion order in goja? The ES spec no longer mentions if objects should be ordered or unordered, but Chrome, Firefox and Node all preserve insertion order.

Test script:

let obj = {"a": 1, "b": 2, "c": 3}
for (key in obj) { console.log(key) }

Does not support javascript built-in functions: escape, unescape...

import * as CryptoJS from 'crypto-js';

function encrypt(word: string, secureKey: string, initvector: string): string {
    let key = CryptoJS.enc.Utf8.parse(secureKey);
    let iv = CryptoJS.enc.Utf8.parse(initvector);
    let srcs = CryptoJS.enc.Utf8.parse(word);
    let encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC });
    return encrypted.toString();
}

function decrypt(cipherText: string, secureKey: string, initvector: string): string {
    let key = CryptoJS.enc.Utf8.parse(secureKey);
    let iv = CryptoJS.enc.Utf8.parse(initvector);
    let decrypted = CryptoJS.AES.decrypt(cipherText, key, { iv: iv, mode: CryptoJS.mode.CBC });

    return decrypted.toString(CryptoJS.enc.Utf8);
}


let key = '1234567887654321';
let iv = '1234567887654321';


let value1 = encrypt('bingo', key, iv);
console.log('aes encrypt:', value1);

let value2 = decrypt(value1, key, iv);
console.log('aes decrypt:', value2);


let value3 = CryptoJS.MD5("Hello, bingo");
console.log('md5:', value3.toString());

Run above code on goja, will report follow errors:

ReferenceError: unescape is not defined at crypto-js:500:42(10)

Benchmarks and performance data?

On average 6-7 times faster than otto. Also uses considerably less memory.

What benchmark code did you use for this, and what were the exact results?

Nice work so far :)

nil pointer dereference in VM execution

Reproduction js:

(function(){
	console.log("test");
	function x(a, b) {
		if (a || b) return 1;
		return 0;
	}

	function y(b) {
		return [].concat(Array.prototype.slice.call(arguments))
	}

	x.apply(this, y(void 0, 1));
})();

FunctionCall.Arguments that goes to x ends up having nil instead of undefined.

Are import statements supported?

I have an index.js with

import utils from 'util'

and a blank util.js

results in

panic: SyntaxError: index.js: Line 7:1 Unexpected reserved word [recovered]
        panic: Panic at 3: SyntaxError: index.js: Line 7:1 Unexpected reserved word [recovered]
        panic: Panic at 3: SyntaxError: index.js: Line 7:1 Unexpected reserved word

I'm playing with trying to get hubot script running from a Go application

I've included the load() function from goja/main.go, which seems to work with a simple load('util.js')

is on the top level not permited return statement?

Hi, i just run a script file ,don't care the result , but got a error:
panic: SyntaxError: test.js: Line 4:4 Illegal return statement (and 2 more errors)

package main

import (
	"fmt"

	"github.com/dop251/goja"
)

func main() {
	filename := "test.js"
	src := `
		var a=true;
		if(a){
			return 'err';
		}
		return 'ok';
	`

	runtime := goja.New()
	_, err := runtime.RunScript(filename, src)
	if err != nil {
		panic(err)
	}
	fmt.Println("done")
}

Interrupt based on work done/concept of "ticks"

I have a rather strange use-case. I run a few goja instances in parallel, along with some other goroutines.

This can cause CPU contention. For me, the timeouts are to ensure injected code isn't running an infinite-loop, rather than having a fixed wall-clock bound.

Under high load though, a goja VM might not get around to doing much, thus running out the Wall Clock allotted time.

I'm happy to do the work, but I wanted to field the idea of "ticks". Each time Goja does some work, a tick is updated. The tick doesn't have to be precise, or meaningful, so long as it is monotonic and increments itself periodically as things get executed.

It would do two things:

  1. I would be able to measure the inner-JS for ticks and compare two implementations which can report into performance metrics. This would help identify which scripts can be improved and when they are improved, that they really did improve.

  2. I would be able to set an interrupt after "ticks", so under contention or maybe because the code is blocked on some input, it wouldn't expire due to the wall clock. Yet, I can guard against infinite loops.

array_sparse very slowly.

import * as flow from 'flow';                                                       
                                                                                    
function executeFlow() {                                                            
                                                                                    
    try {                                                                           
        let result = flow.invoke("download", 0, { "url": "http://www.huawei.com" });
        console.log(result);                                                        
    } catch (error) {                                                               
        console.error(error.name, error.message);                                   
    }                                                                               
}                                                                                   
                                                                                    
executeFlow();  

flow.invoke is an go function. the code execute very slowly on follow stack:

goroutine 1695 [runnable]:
vendor/github.com/dop251/goja.(*baseObject).init(...)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/object.go:98
vendor/github.com/dop251/goja.(*arrayObject).init(...)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:19
vendor/github.com/dop251/goja.(*sparseArrayObject).expand(0xc42f7cc3c0, 0x1d295c0)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:331 +0x14e
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f7cc3c0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:200 +0x269
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f7cc280, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc4303185a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc430318500, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f7cc140, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f7cc000, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc4303183c0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc430318280, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc430318140, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc430318000, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42c1786e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42c1785a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42c178460, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42c178320, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42c1781e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42c1780a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42d17a820, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42d17a6e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42d17a5a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42d17a460, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42d17a320, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42d17a1e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42d17a0a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f473f40, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f473e00, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f473cc0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f473b80, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f473ae0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f4739a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f473900, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f4737c0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f473680, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f473540, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f473400, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f4732c0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f473180, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f473040, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f472f00, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f472dc0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f472c80, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f472b40, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f472a00, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f4728c0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f472640, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f472500, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f4723c0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f472280, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f472140, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f472000, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed85f40, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed85e00, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed85cc0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed85b80, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed85a40, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed859a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed85860, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed85720, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed855e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed854a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed85360, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed85220, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed850e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed84fa0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed84e60, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed84d20, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed84be0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed84aa0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed84960, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed84820, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed846e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed845a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed84460, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed84320, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42ed841e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42ed840a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f785ea0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f785d60, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f785c20, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f785ae0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f7859a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f785860, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f785720, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f7855e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f7854a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f785360, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f785220, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f7850e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f784fa0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f784e60, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f784d20, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f784be0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f784aa0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f784960, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f784820, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f7846e0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f7845a0, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
vendor/github.com/dop251/goja.(*sparseArrayObject).putIdx(0xc42f784460, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array_sparse.go:208 +0x514
vendor/github.com/dop251/goja.(*arrayObject).putIdx(0xc42f784320, 0x162d, 0x1d28f80, 0xc424cae440, 0x0, 0x0, 0x0, 0x1d295c0, 0xc4222b7790)
	/data1/baas/bingo/src/vendor/github.com/dop251/goja/array.go:233 +0x2dd
created by net/http.(*Server).Serve
	/data1/baas/my/go/src/net/http/server.go:2720 +0x288

Interrupt not returning an error

func TestInterrupt(t *testing.T) {
	rt := goja.New()
	go func() {
		<-time.After(1 * time.Second)
		rt.Interrupt(errors.New("hi"))
	}()
	v, err := rt.RunString(`while (true) {}`)
	assert.Nil(t, v)
	assert.EqualError(t, err, "hi")
}

A nil error is returned.

Panics on `const` keyword

Goja panics with the following error message:

SyntaxError: (anonymous): Line 45:1 Unexpected reserved word

If it's passed a valid JS code with constant, like:

const foo = require("foo")

If you change const to var, it works.

Missing Object.assign member

I have a piece of JS code that does the following:

    var a = Object.assign({}, state, {
      isAuthenticating: false,
    });

where state is an empty object, and even though it runs fine in the browser when bundled, it fails running that same bundle in goji with the following error:

TypeError: Object has no member 'assign'

Bridged functions taking goja.Value args misbehave around null values

A function that takes a goja.Value argument will, when bridged to JS and invoked with null for that argument, receive a nil goja.Value, which is not recognised as a proper null by goja.IsNull().

Test case:

func TestNullArguments(t *testing.T) {
	rt := goja.New()
	rt.Set("fn", func(v goja.Value) {
		if v == nil {
			t.Error("null becomes nil")
		}
		if !goja.IsNull(v) {
			t.Error("null is not null")
		}
	})
	rt.RunString(`fn(null);`)
}

Creating prototype object (with constructor)

Currently it is not possible (or I am unaware how it works) to register a prototype object within the VM. This hidden from the public API and is used in the builtin_ files.

It would be nice to also add a test/example for this.

[]byte/string parameter conversion oddity

func TestByteToString(t *testing.T) {
	rt := goja.New()
	rt.Set("v", []byte("hi"))
	rt.Set("fn", func(v string) {
		assert.Equal(t, "hi", v)
	})
	_, err := rt.RunString(`fn(v)`)
	assert.NoError(t, err)
}

Expected "hi", got "104,105". Looks like mangling of arrays that could use a special case for []byte? The opposite direction appears to work just fine.

Passing/registering function from javascript to go

I digged through the docs, tests and implementation but could not figure out how to "register" a function from javascript to go. This to create a callback-style application.

Something like this:

function myfunc() {
  mygofunc();
}

// Register function with id 1234 for go
registerfunc(1234, myfunc);

I am aware of the Callable type but I have no idea how this should be used.

Crash with Get and Interrupt

func TestInterrupt(t *testing.T) {
	rt := goja.New()
	_, err := rt.RunString(`
		var fn = function() {
			while (true) {}
		};
	`)
	if !assert.NoError(t, err) {
		return
	}
	fn, ok := goja.AssertFunction(rt.Get("fn"))
	if !assert.True(t, ok) {
		return
	}
	go func() {
		<-time.After(1 * time.Second)
		rt.Interrupt(errors.New("hi"))
	}()
	v, err := fn(goja.Undefined())
	assert.Nil(t, v)
	assert.EqualError(t, err, "hi")
}

Causes a panic:

panic: <nil> [recovered]
	panic: <nil> [recovered]
	panic: <nil>

goroutine 4 [running]:
testing.tRunner.func1(0xc4210af0e0)
	/usr/lib/go/src/testing/testing.go:711 +0x2d2
panic(0xaa5820, 0xc4212d5740)
	/usr/lib/go/src/runtime/panic.go:491 +0x283
github.com/loadimpact/k6/vendor/github.com/dop251/goja.(*vm).try.func1(0xc4204ceea0, 0x0, 0xc422b27e60, 0x0, 0x0, 0x0, 0xc422b27eb8)
	/home/emily/go/src/github.com/loadimpact/k6/vendor/github.com/dop251/goja/vm.go:356 +0x4a4
panic(0xaa5820, 0xc4212d5740)
	/usr/lib/go/src/runtime/panic.go:491 +0x283
github.com/loadimpact/k6/vendor/github.com/dop251/goja.(*vm).run(0xc4204ceea0)
	/home/emily/go/src/github.com/loadimpact/k6/vendor/github.com/dop251/goja/vm.go:299 +0x183
github.com/loadimpact/k6/vendor/github.com/dop251/goja.(*funcObject).Call(0xc420387c80, 0xfd2b40, 0x117fd20, 0x0, 0x0, 0x0, 0x0, 0xc420024000)
	/home/emily/go/src/github.com/loadimpact/k6/vendor/github.com/dop251/goja/func.go:130 +0x3c8
github.com/loadimpact/k6/vendor/github.com/dop251/goja.(*funcObject).Call-fm(0xfd2b40, 0x117fd20, 0x0, 0x0, 0x0, 0x42bd99, 0xc420feca40)
	/home/emily/go/src/github.com/loadimpact/k6/vendor/github.com/dop251/goja/builtin_function.go:120 +0x51
github.com/loadimpact/k6/vendor/github.com/dop251/goja.AssertFunction.func1.1()
	/home/emily/go/src/github.com/loadimpact/k6/vendor/github.com/dop251/goja/runtime.go:1374 +0xb6
github.com/loadimpact/k6/vendor/github.com/dop251/goja.(*vm).try(0xc4204ceea0, 0xc422b27ed8, 0x0)
	/home/emily/go/src/github.com/loadimpact/k6/vendor/github.com/dop251/goja/vm.go:370 +0x119
github.com/loadimpact/k6/vendor/github.com/dop251/goja.AssertFunction.func1(0xfd2b40, 0x117fd20, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19829e1200000001, 0x5a222c1d)
	/home/emily/go/src/github.com/loadimpact/k6/vendor/github.com/dop251/goja/runtime.go:1373 +0xdd
github.com/loadimpact/k6/js.TestInterrupt(0xc4210af0e0)
	/home/emily/go/src/github.com/loadimpact/k6/js/runner_test.go:259 +0x152
testing.tRunner(0xc4210af0e0, 0xb8eb10)
	/usr/lib/go/src/testing/testing.go:746 +0xd0
created by testing.(*T).Run
	/usr/lib/go/src/testing/testing.go:789 +0x2de

It doesn't happen if you use a value returned from Run*, only if you get it with Get.

Memory leaks?

Hi

I'm building a universal web app, where goja handles JS rendering on the server.

Now I'm in the phase of doing performance tests, and for some reason goja seems to be leaking memory.

I'm using artillery.io to perform load tests, and after the load test it seems vm.run is holding memory and not releasing it. I'm not too sure if that's goja fault (probably not), but maybe you know anything off the top of your head how to prevent memory leaks?

Here's the heap pprof BEFORE the load test:
http://jarosinski.uk/files/heap_base.svg

And here's the one after the load test:
http://jarosinski.uk/files/heap_current.svg

And this is the code that handles VM creation:
https://gist.github.com/iKonrad/a046de73c8e478b3cac07fa9e419af58

Any help or suggestions will be appreciated.

How could I new a go struct or type in natvie module

I am trying to register my native module, but I could not find a way to create a struct instance in js vm

type AB struct {
}

func (a *AB) A() {
	fmt.Println("A")
}
o.Set("AB", reflect.TypeOf((*AB)(nil)).Elem())

.....

console.log(new(MyNativePackage.AB))
TypeError: Not a constructor at <eval>:3:13(5)

Debugging goja stacktraces

For example:

2017/11/24 16:18:52 values: [normal throw]
2017/11/24 16:18:52  0: goja.enterFuncStashless({1 3})
2017/11/24 16:18:52  1: goja.try({15 0 false})
2017/11/24 16:18:52  2: goja._newObject({})
2017/11/24 16:18:52  3: goja.loadVal(0)
2017/11/24 16:18:52  4: goja.setProp1(type)
2017/11/24 16:18:52  5: goja.loadStack(-1)
2017/11/24 16:18:52  6: goja._dup({})
2017/11/24 16:18:52  7: goja.getPropCallee(call)
2017/11/24 16:18:52 run buildnext.then called
2017/11/24 16:18:52  8: goja.loadStack(-2)
2017/11/24 16:18:52  9: goja.loadStack(-3)
2017/11/24 16:18:52  10: goja.call(2)
2017/11/24 16:18:52  11: goja.setProp1(arg)
2017/11/24 16:18:52  12: goja._halt({})
2017/11/24 16:18:52  13: goja._retStashless({})
2017/11/24 16:18:52  14: goja._halt({})
2017/11/24 16:18:52  15: goja.jump(11)
2017/11/24 16:18:52  16: goja.storeStack(1)
2017/11/24 16:18:52  17: goja._pop({})
2017/11/24 16:18:52  18: goja._newObject({})
2017/11/24 16:18:52  19: goja.loadVal(1)
2017/11/24 16:18:52  20: goja.setProp1(type)
2017/11/24 16:18:52  21: goja.loadStack(1)
2017/11/24 16:18:52  22: goja.setProp1(arg)
2017/11/24 16:18:52  23: goja._halt({})
2017/11/24 16:18:52  24: goja._retStashless({})
2017/11/24 16:18:52  25: goja._halt({})
2017/11/24 16:18:52  26: goja._loadUndef({})
2017/11/24 16:18:52  27: goja._retStashless({})
2017/11/24 16:18:52 values: [true Promise can't be resolved itself 1 false]
2017/11/24 16:18:52  0: goja.enterFunc(1)
2017/11/24 16:18:52  1: goja.bindName(value)
2017/11/24 16:18:52  2: goja.bindName(promise)
2017/11/24 16:18:52  3: goja.bindName(then)
2017/11/24 16:18:52  4: goja.bindName( __tmp0)
2017/11/24 16:18:52  5: goja.loadStack(0)
2017/11/24 16:18:52  6: goja.setLocal(1)
2017/11/24 16:18:52  7: goja._pop({})
2017/11/24 16:18:52  8: goja.getLocal(1)
2017/11/24 16:18:52  9: goja.getProp(_d)
2017/11/24 16:18:52  10: goja.jne(3)
2017/11/24 16:18:52  11: goja._loadUndef({})
2017/11/24 16:18:52  12: goja._ret({})
2017/11/24 16:18:52  13: goja.getLocal(1)
2017/11/24 16:18:52  14: goja.loadVal(0)
2017/11/24 16:18:52  15: goja.setPropStrict(_d)
2017/11/24 16:18:52  16: goja._pop({})
2017/11/24 16:18:52  17: goja.getLocal(1)
2017/11/24 16:18:52  18: goja.getProp(_w)
2017/11/24 16:18:52  19: goja.jeq1(3)
2017/11/24 16:18:52  20: goja._pop({})
2017/11/24 16:18:52  21: goja.getLocal(1)
2017/11/24 16:18:52  22: goja.setLocal(1)
2017/11/24 16:18:52  23: goja._pop({})
2017/11/24 16:18:52  24: goja.try({38 0 false})
2017/11/24 16:18:52  25: goja.getLocal(1)
2017/11/24 16:18:52  26: goja.getLocal(0)
2017/11/24 16:18:52  27: goja._op_strict_eq({})
2017/11/24 16:18:52  28: goja.jne(6)
2017/11/24 16:18:52  29: goja._loadUndef({})
2017/11/24 16:18:52  30: goja.getLocal(16777235)
2017/11/24 16:18:52  31: goja.loadVal(1)
2017/11/24 16:18:52  32: goja.call(1)
2017/11/24 16:18:52  33: goja._throw({})
2017/11/24 16:18:52  34: goja._loadUndef({})
2017/11/24 16:18:52  35: goja.getLocal(16777247)
2017/11/24 16:18:52  36: goja.getLocal(0)
2017/11/24 16:18:52  37: goja.call(1)
2017/11/24 16:18:52  38: goja.setLocal(2)
2017/11/24 16:18:52  39: goja.jne(7)
2017/11/24 16:18:52  40: goja._loadUndef({})
2017/11/24 16:18:52  41: goja.getLocal(16777230)
2017/11/24 16:18:52  42: *goja.newFunc(&{0xc422276de0  0 true 144833 145075})
2017/11/24 16:18:52 values: [false 1]
2017/11/24 16:18:52 > 0: goja.enterFuncStashless({2 0})
2017/11/24 16:18:52 > 1: goja._newObject({})
2017/11/24 16:18:52 > 2: goja.getLocal(1)
2017/11/24 16:18:52 > 3: goja.setProp1(_w)
2017/11/24 16:18:52 > 4: goja.loadVal(0)
2017/11/24 16:18:52 > 5: goja.setProp1(_d)
2017/11/24 16:18:52 > 6: goja.storeStack(1)
2017/11/24 16:18:52 > 7: goja._pop({})
2017/11/24 16:18:52 > 8: goja.try({21 0 false})
2017/11/24 16:18:52 > 9: goja.getLocal(2)
2017/11/24 16:18:52 > 10: goja._dup({})
2017/11/24 16:18:52 > 11: goja.getPropCallee(call)
2017/11/24 16:18:52 > 12: goja.getLocal(0)
2017/11/24 16:18:52 > 13: goja._loadUndef({})
2017/11/24 16:18:52 > 14: goja.getLocal(16777221)
2017/11/24 16:18:52 > 15: goja.getLocal(16777253)
2017/11/24 16:18:52 > 16: goja.loadStack(1)
2017/11/24 16:18:52 > 17: goja.loadVal(1)
2017/11/24 16:18:52 > 18: goja.call(3)
2017/11/24 16:18:52 > 19: goja._loadUndef({})
2017/11/24 16:18:52 > 20: goja.getLocal(16777221)
2017/11/24 16:18:52 > 21: goja.getLocal(16777252)
2017/11/24 16:18:52 > 22: goja.loadStack(1)
2017/11/24 16:18:52 > 23: goja.loadVal(1)
2017/11/24 16:18:52 > 24: goja.call(3)
2017/11/24 16:18:52 > 25: goja.call(3)
2017/11/24 16:18:52 > 26: goja._pop({})
2017/11/24 16:18:52 > 27: goja._halt({})
2017/11/24 16:18:52 > 28: goja.jump(11)
2017/11/24 16:18:52 > 29: goja.storeStack(2)
2017/11/24 16:18:52 > 30: goja._pop({})
2017/11/24 16:18:52 > 31: goja.getLocal(16777252)
2017/11/24 16:18:52 > 32: goja._dup({})
2017/11/24 16:18:52 > 33: goja.getPropCallee(call)
2017/11/24 16:18:52 > 34: goja.loadStack(1)
2017/11/24 16:18:52 > 35: goja.loadStack(2)
2017/11/24 16:18:52 > 36: goja.call(2)
2017/11/24 16:18:52 > 37: goja._pop({})
2017/11/24 16:18:52 > 38: goja._halt({})
2017/11/24 16:18:52 > 39: goja._loadUndef({})
2017/11/24 16:18:52 > 40: goja._retStashless({})
2017/11/24 16:18:52  43: goja.call(1)
2017/11/24 16:18:52  44: goja._pop({})
2017/11/24 16:18:52  45: goja.jump(15)
2017/11/24 16:18:52  46: goja.getLocal(1)
2017/11/24 16:18:52  47: goja.getLocal(0)
2017/11/24 16:18:52  48: goja.setPropStrict(_v)
2017/11/24 16:18:52  49: goja._pop({})
2017/11/24 16:18:52  50: goja.getLocal(1)
2017/11/24 16:18:52  51: goja.loadVal(2)
2017/11/24 16:18:52  52: goja.setPropStrict(_s)
2017/11/24 16:18:52  53: goja._pop({})
2017/11/24 16:18:52  54: goja._loadUndef({})
2017/11/24 16:18:52  55: goja.getLocal(16777248)
2017/11/24 16:18:52  56: goja.getLocal(1)
2017/11/24 16:18:52  57: goja.loadVal(3)
2017/11/24 16:18:52  58: goja.call(2)
2017/11/24 16:18:52  59: goja._pop({})
2017/11/24 16:18:52  60: goja._halt({})
2017/11/24 16:18:52  61: goja.jump(15)
2017/11/24 16:18:52  62: goja.setLocal(3)
2017/11/24 16:18:52  63: goja._pop({})
2017/11/24 16:18:52  64: goja.getLocal(16777252)
2017/11/24 16:18:52  65: goja._dup({})
2017/11/24 16:18:52  66: goja.getPropCallee(call)
2017/11/24 16:18:52  67: goja._newObject({})
2017/11/24 16:18:52  68: goja.getLocal(1)
2017/11/24 16:18:52  69: goja.setProp1(_w)
2017/11/24 16:18:52  70: goja.loadVal(3)
2017/11/24 16:18:52  71: goja.setProp1(_d)
2017/11/24 16:18:52  72: goja.getLocal(3)
2017/11/24 16:18:52  73: goja.call(2)
2017/11/24 16:18:52  74: goja._pop({})
2017/11/24 16:18:52  75: goja._halt({})
2017/11/24 16:18:52  76: goja._loadUndef({})
2017/11/24 16:18:52  77: goja._ret({})
2017/11/24 16:18:52 values: [2 1 true Promise-chain cycle]
2017/11/24 16:18:52  0: goja.enterFuncStashless({7 1})
2017/11/24 16:18:52  1: goja.getLocal(1)
2017/11/24 16:18:52  2: goja.jne(4)
2017/11/24 16:18:52  3: goja.loadStack(-1)
2017/11/24 16:18:52  4: goja.getProp(ok)
2017/11/24 16:18:52  5: goja.jump(3)
2017/11/24 16:18:52  6: goja.loadStack(-1)
2017/11/24 16:18:52  7: goja.getProp(fail)
2017/11/24 16:18:52  8: goja.storeStack(1)
2017/11/24 16:18:52  9: goja._pop({})
2017/11/24 16:18:52  10: goja.loadStack(-1)
2017/11/24 16:18:52  11: goja.getProp(resolve)
2017/11/24 16:18:52  12: goja.storeStack(2)
2017/11/24 16:18:52  13: goja._pop({})
2017/11/24 16:18:52  14: goja.loadStack(-1)
2017/11/24 16:18:52  15: goja.getProp(reject)
2017/11/24 16:18:52  16: goja.storeStack(3)
2017/11/24 16:18:52  17: goja._pop({})
2017/11/24 16:18:52  18: goja.loadStack(-1)
2017/11/24 16:18:52  19: goja.getProp(domain)
2017/11/24 16:18:52  20: goja.storeStack(4)
2017/11/24 16:18:52  21: goja._pop({})
2017/11/24 16:18:52  22: goja.try({90 0 false})
2017/11/24 16:18:52  23: goja.loadStack(1)
2017/11/24 16:18:52  24: goja.jne(81)
2017/11/24 16:18:52  25: goja.getLocal(1)
2017/11/24 16:18:52  26: goja._not({})
2017/11/24 16:18:52  27: goja.jne(15)
2017/11/24 16:18:52  28: goja.getLocal(16777216)
2017/11/24 16:18:52  29: goja.getProp(_h)
2017/11/24 16:18:52  30: goja.loadVal(0)
2017/11/24 16:18:52  31: goja._op_eq({})
2017/11/24 16:18:52  32: goja.jne(6)
2017/11/24 16:18:52  33: goja._loadUndef({})
2017/11/24 16:18:52  34: goja.getLocal(33554467)
2017/11/24 16:18:52  35: goja.getLocal(16777216)
2017/11/24 16:18:52  36: goja.call(1)
2017/11/24 16:18:52  37: goja._pop({})
2017/11/24 16:18:52  38: goja.getLocal(16777216)
2017/11/24 16:18:52  39: goja.loadVal(1)
2017/11/24 16:18:52  40: goja.setPropStrict(_h)
2017/11/24 16:18:52  41: goja._pop({})
2017/11/24 16:18:52  42: goja.loadStack(1)
2017/11/24 16:18:52  43: goja.loadVal(2)
2017/11/24 16:18:52  44: goja._op_strict_eq({})
2017/11/24 16:18:52  45: goja.jne(5)
2017/11/24 16:18:52  46: goja.getLocal(0)
2017/11/24 16:18:52  47: goja.storeStack(5)
2017/11/24 16:18:52  48: goja._pop({})
2017/11/24 16:18:52  49: goja.jump(21)
2017/11/24 16:18:52  50: goja.loadStack(4)
2017/11/24 16:18:52  51: goja.jne(6)
2017/11/24 16:18:52  52: goja.loadStack(4)
2017/11/24 16:18:52  53: goja._dup({})
2017/11/24 16:18:52  54: goja.getPropCallee(enter)
2017/11/24 16:18:52  55: goja.call(0)
2017/11/24 16:18:52  56: goja._pop({})
2017/11/24 16:18:52  57: goja._loadUndef({})
2017/11/24 16:18:52  58: goja.loadStack(1)
2017/11/24 16:18:52  59: goja.getLocal(0)
2017/11/24 16:18:52  60: goja.call(1)
2017/11/24 16:18:52  61: goja.storeStack(5)
2017/11/24 16:18:52  62: goja._pop({})
2017/11/24 16:18:52  63: goja.loadStack(4)
2017/11/24 16:18:52  64: goja.jne(6)
2017/11/24 16:18:52  65: goja.loadStack(4)
2017/11/24 16:18:52  66: goja._dup({})
2017/11/24 16:18:52  67: goja.getPropCallee(exit)
2017/11/24 16:18:52  68: goja.call(0)
2017/11/24 16:18:52  69: goja._pop({})
2017/11/24 16:18:52  70: goja.loadStack(5)
2017/11/24 16:18:52  71: goja.loadStack(-1)
2017/11/24 16:18:52  72: goja.getProp(promise)
2017/11/24 16:18:52  73: goja._op_strict_eq({})
2017/11/24 16:18:52  74: goja.jne(10)
2017/11/24 16:18:52  75: goja._loadUndef({})
2017/11/24 16:18:52  76: goja.loadStack(3)
2017/11/24 16:18:52  77: goja._loadUndef({})
2017/11/24 16:18:52  78: goja.getLocal(33554451)
2017/11/24 16:18:52  79: goja.loadVal(3)
2017/11/24 16:18:52  80: goja.call(1)
2017/11/24 16:18:52  81: goja.call(1)
2017/11/24 16:18:52  82: goja._pop({})
2017/11/24 16:18:52  83: goja.jump(21)
2017/11/24 16:18:52  84: goja._loadUndef({})
2017/11/24 16:18:52  85: goja.getLocal(33554463)
2017/11/24 16:18:52  86: goja.loadStack(5)
2017/11/24 16:18:52  87: goja.call(1)
2017/11/24 16:18:52  88: goja.storeStack(6)
2017/11/24 16:18:52  89: goja.jne(10)
2017/11/24 16:18:52  90: goja.loadStack(6)
2017/11/24 16:18:52  91: goja._dup({})
2017/11/24 16:18:52  92: goja.getPropCallee(call)
2017/11/24 16:18:52  93: goja.loadStack(5)
2017/11/24 16:18:52  94: goja.loadStack(2)
2017/11/24 16:18:52  95: goja.loadStack(3)
2017/11/24 16:18:52  96: goja.call(3)
2017/11/24 16:18:52  97: goja._pop({})
2017/11/24 16:18:52  98: goja.jump(6)
2017/11/24 16:18:52  99: goja._loadUndef({})
2017/11/24 16:18:52  100: goja.loadStack(2)
2017/11/24 16:18:52  101: goja.loadStack(5)
2017/11/24 16:18:52  102: goja.call(1)
2017/11/24 16:18:52  103: goja._pop({})
2017/11/24 16:18:52  104: goja.jump(6)
2017/11/24 16:18:52  105: goja._loadUndef({})
2017/11/24 16:18:52  106: goja.loadStack(3)
2017/11/24 16:18:52  107: goja.getLocal(0)
2017/11/24 16:18:52  108: goja.call(1)
2017/11/24 16:18:52  109: goja._pop({})
2017/11/24 16:18:52  110: goja._halt({})
2017/11/24 16:18:52  111: goja.jump(9)
2017/11/24 16:18:52  112: goja.storeStack(7)
2017/11/24 16:18:52  113: goja._pop({})
2017/11/24 16:18:52  114: goja._loadUndef({})
2017/11/24 16:18:52  115: goja.loadStack(3)
2017/11/24 16:18:52  116: goja.loadStack(7)
2017/11/24 16:18:52  117: goja.call(1)
2017/11/24 16:18:52  118: goja._pop({})
2017/11/24 16:18:52  119: goja._halt({})
2017/11/24 16:18:52  120: goja._loadUndef({})
2017/11/24 16:18:52  121: goja._retStashless({})
2017/11/24 16:18:52 values: []
2017/11/24 16:18:52  0: goja.enterFuncStashless({3 0})
2017/11/24 16:18:52  1: goja.getLocal(16777224)
2017/11/24 16:18:52  2: goja.jneq1(5)
2017/11/24 16:18:52  3: goja._pop({})
2017/11/24 16:18:52  4: goja.getLocal(16777222)
2017/11/24 16:18:52  5: goja.getProp(domain)
2017/11/24 16:18:52  6: goja.storeStack(1)
2017/11/24 16:18:52  7: goja.jne(6)
2017/11/24 16:18:52  8: goja.loadStack(1)
2017/11/24 16:18:52  9: goja._dup({})
2017/11/24 16:18:52  10: goja.getPropCallee(exit)
2017/11/24 16:18:52  11: goja.call(0)
2017/11/24 16:18:52  12: goja._pop({})
2017/11/24 16:18:52  13: goja.getLocal(0)
2017/11/24 16:18:52  14: goja.jne(32)
2017/11/24 16:18:52  15: goja.getLocal(0)
2017/11/24 16:18:52  16: goja.getProp(fn)
2017/11/24 16:18:52  17: goja.storeStack(2)
2017/11/24 16:18:52  18: goja._pop({})
2017/11/24 16:18:52  19: goja.getLocal(0)
2017/11/24 16:18:52  20: goja.getProp(next)
2017/11/24 16:18:52  21: goja.setLocal(0)
2017/11/24 16:18:52  22: goja._pop({})
2017/11/24 16:18:52  23: goja.try({7 0 false})
2017/11/24 16:18:52  24: goja._loadUndef({})
2017/11/24 16:18:52  25: goja.loadStack(2)
2017/11/24 16:18:52  26: goja.call(0)
2017/11/24 16:18:52  27: goja._pop({})
2017/11/24 16:18:52  28: goja._halt({})
2017/11/24 16:18:52  29: goja.jump(16)
2017/11/24 16:18:52  30: goja.storeStack(3)
2017/11/24 16:18:52  31: goja._pop({})
2017/11/24 16:18:52  32: goja.getLocal(0)
2017/11/24 16:18:52  33: goja.jne(6)
2017/11/24 16:18:52  34: goja._loadUndef({})
2017/11/24 16:18:52  35: goja.getLocal(2)
2017/11/24 16:18:52  36: goja.call(0)
2017/11/24 16:18:52  37: goja._pop({})
2017/11/24 16:18:52  38: goja.jump(4)
2017/11/24 16:18:52  39: goja.getVar1(undefined)
2017/11/24 16:18:52  40: goja.setLocal(1)
2017/11/24 16:18:52  41: goja._pop({})
2017/11/24 16:18:52  42: goja.loadStack(3)
2017/11/24 16:18:52  43: goja._throw({})
2017/11/24 16:18:52  44: goja._halt({})
2017/11/24 16:18:52  45: goja.jump(-32)
2017/11/24 16:18:52  46: goja.getVar1(undefined)
2017/11/24 16:18:52  47: goja.setLocal(1)
2017/11/24 16:18:52  48: goja._pop({})
2017/11/24 16:18:52  49: goja.loadStack(1)
2017/11/24 16:18:52  50: goja.jne(6)
2017/11/24 16:18:52  51: goja.loadStack(1)
2017/11/24 16:18:52  52: goja._dup({})
2017/11/24 16:18:52  53: goja.getPropCallee(enter)
2017/11/24 16:18:52  54: goja.call(0)
2017/11/24 16:18:52  55: goja._pop({})
2017/11/24 16:18:52  56: goja._loadUndef({})
2017/11/24 16:18:52  57: goja._retStashless({})
panic: runtime error: index out of range [recovered]
	panic: Panic at 10: runtime error: index out of range [recovered]
	panic: Panic at 25: Panic at 10: runtime error: index out of range [recovered]
	panic: Panic at 23: Panic at 25: Panic at 10: runtime error: index out of range [recovered]
	panic: Panic at 24: Panic at 23: Panic at 25: Panic at 10: runtime error: index out of range

goroutine 46 [running]:
github.com/dop251/goja.(*vm).try.func1(0xc420263860, 0x0, 0xc426dc7e08, 0x0, 0x0, 0x0, 0xc426dc7e60)
	github.com/dop251/goja/vm.go:364 +0x4b4
panic(0x17cff00, 0xc422820380)
	/usr/local/opt/go/libexec/src/runtime/panic.go:491 +0x283
github.com/dop251/goja.(*vm).try.func1(0xc420263860, 0x6, 0xc426dc76c0, 0x19, 0x0, 0x0, 0xc426dc7718)
	github.com/dop251/goja/vm.go:364 +0x4b4
panic(0x17cff00, 0xc422820360)
	/usr/local/opt/go/libexec/src/runtime/panic.go:491 +0x283
github.com/dop251/goja.(*vm).try.func1(0xc420263860, 0x8, 0xc426dc74c0, 0x25, 0x0, 0x0, 0xc426dc7518)
	github.com/dop251/goja/vm.go:364 +0x4b4
panic(0x17cff00, 0xc422751fe0)
	/usr/local/opt/go/libexec/src/runtime/panic.go:491 +0x283
github.com/dop251/goja.(*vm).try.func1(0xc420263860, 0xb, 0xc426dc6ee8, 0x2e, 0x0, 0x0, 0xc426dc6f40)
	github.com/dop251/goja/vm.go:364 +0x4b4
panic(0x17e8720, 0x1f3db30)
	/usr/local/opt/go/libexec/src/runtime/panic.go:491 +0x283
github.com/dop251/goja.(*vm).popCtx(...)
	github.com/dop251/goja/vm.go:427
github.com/dop251/goja.(*vm)._nativeCall(0xc420263860, 0xc420c05b80, 0x2)
	github.com/dop251/goja/vm.go:1834 +0x858
github.com/dop251/goja.call.exec(0x2, 0xc420263860)
	github.com/dop251/goja/vm.go:1810 +0x6b3
github.com/dop251/goja.(*call).exec(0xc42048b02c, 0xc420263860)
	<autogenerated>:1 +0x44
github.com/dop251/goja.(*vm).run(0xc420263860)
	github.com/dop251/goja/vm.go:288 +0x51
github.com/dop251/goja.(*funcObject).Call(0xc420b64600, 0x1d880e0, 0xc42175fb40, 0xc420f75690, 0x1, 0xe5, 0x20, 0x179c440)
	github.com/dop251/goja/func.go:130 +0x3c8
github.com/dop251/goja.(*funcObject).Call-fm(0x1d880e0, 0xc42175fb40, 0xc420f75690, 0x1, 0xe5, 0x1012b70, 0x1f52360)
	github.com/dop251/goja/builtin_function.go:120 +0x51
github.com/dop251/goja.(*Runtime).functionproto_call(0xc425d06f00, 0x1d880e0, 0xc4245a4120, 0xc420f75680, 0x2, 0xe6, 0xc420a11c68, 0x4)
	github.com/dop251/goja/builtin_function.go:71 +0x155
github.com/dop251/goja.(*Runtime).(github.com/dop251/goja.functionproto_call)-fm(0x1d880e0, 0xc4245a4120, 0xc420f75680, 0x2, 0xe6, 0x13d3f43, 0x17e1340)
	github.com/dop251/goja/builtin_function.go:160 +0x51
github.com/dop251/goja.(*vm)._nativeCall(0xc420263860, 0xc422d2aa50, 0x2)
	github.com/dop251/goja/vm.go:1826 +0x2ec
github.com/dop251/goja.call.exec(0x2, 0xc420263860)
	github.com/dop251/goja/vm.go:1810 +0x6b3
github.com/dop251/goja.(*call).exec(0xc4209e56fc, 0xc420263860)
	<autogenerated>:1 +0x44
github.com/dop251/goja.(*vm).run(0xc420263860)
	github.com/dop251/goja/vm.go:288 +0x51
github.com/dop251/goja.(*vm).(github.com/dop251/goja.run)-fm()
	github.com/dop251/goja/vm.go:375 +0x2a
github.com/dop251/goja.(*vm).try(0xc420263860, 0xc427c92f48, 0x0)
	github.com/dop251/goja/vm.go:370 +0x119
github.com/dop251/goja.(*vm).runTry(0xc420263860, 0xc424f19230)
	github.com/dop251/goja/vm.go:375 +0x46
github.com/dop251/goja.try.exec(0x26, 0x0, 0xc420263860)
	github.com/dop251/goja/vm.go:2245 +0x51
github.com/dop251/goja.(*try).exec(0xc42121f070, 0xc420263860)
	<autogenerated>:1 +0x53
github.com/dop251/goja.(*vm).run(0xc420263860)
	github.com/dop251/goja/vm.go:288 +0x51
github.com/dop251/goja.(*funcObject).Call(0xc420b65a40, 0x1d880e0, 0xc4235d1060, 0xc420f755b0, 0x1, 0xf3, 0x1d880e0, 0xc4235d1160)
	github.com/dop251/goja/func.go:130 +0x3c8
github.com/dop251/goja.(*funcObject).Call-fm(0x1d880e0, 0xc4235d1060, 0xc420f755b0, 0x1, 0xf3, 0x1d880e0, 0xc4235d1160)
	github.com/dop251/goja/builtin_function.go:120 +0x51
github.com/dop251/goja.(*Runtime).functionproto_call(0xc425d06f00, 0x1d880e0, 0xc4245a4ba0, 0xc420f755a0, 0x2, 0xf4, 0xc420a0b896, 0x4)
	github.com/dop251/goja/builtin_function.go:71 +0x155
github.com/dop251/goja.(*Runtime).(github.com/dop251/goja.functionproto_call)-fm(0x1d880e0, 0xc4245a4ba0, 0xc420f755a0, 0x2, 0xf4, 0x13d3f43, 0x17e1340)
	github.com/dop251/goja/builtin_function.go:160 +0x51
github.com/dop251/goja.(*vm)._nativeCall(0xc420263860, 0xc422d2aa50, 0x2)
	github.com/dop251/goja/vm.go:1826 +0x2ec
github.com/dop251/goja.call.exec(0xffffffff00000002, 0xc420263860)
	github.com/dop251/goja/vm.go:1810 +0x6b3
github.com/dop251/goja.(*call).exec(0xc4212380dc, 0xc420263860)
	<autogenerated>:1 +0x44
github.com/dop251/goja.(*vm).run(0xc420263860)
	github.com/dop251/goja/vm.go:288 +0x51
github.com/dop251/goja.(*vm).(github.com/dop251/goja.run)-fm()
	github.com/dop251/goja/vm.go:375 +0x2a
github.com/dop251/goja.(*vm).try(0xc420263860, 0xc427c93520, 0x0)
	github.com/dop251/goja/vm.go:370 +0x119
github.com/dop251/goja.(*vm).runTry(0xc420263860, 0xc424f19380)
	github.com/dop251/goja/vm.go:375 +0x46
github.com/dop251/goja.try.exec(0x5a, 0x0, 0xc420263860)
	github.com/dop251/goja/vm.go:2245 +0x51
github.com/dop251/goja.(*try).exec(0xc42121e380, 0xc420263860)
	<autogenerated>:1 +0x53
github.com/dop251/goja.(*vm).run(0xc420263860)
	github.com/dop251/goja/vm.go:288 +0x51
github.com/dop251/goja.(*vm).(github.com/dop251/goja.run)-fm()
	github.com/dop251/goja/vm.go:375 +0x2a
github.com/dop251/goja.(*vm).try(0xc420263860, 0xc427c93720, 0x0)
	github.com/dop251/goja/vm.go:370 +0x119
github.com/dop251/goja.(*vm).runTry(0xc420263860, 0xc422ab1e00)
	github.com/dop251/goja/vm.go:375 +0x46
github.com/dop251/goja.try.exec(0x7, 0x0, 0xc420263860)
	github.com/dop251/goja/vm.go:2245 +0x51
github.com/dop251/goja.(*try).exec(0xc4209e53e0, 0xc420263860)
	<autogenerated>:1 +0x53
github.com/dop251/goja.(*vm).run(0xc420263860)
	github.com/dop251/goja/vm.go:288 +0x51
github.com/dop251/goja.(*funcObject).Call(0xc420b64480, 0x1d885e0, 0xc420018d90, 0xc420f75360, 0x1, 0x118, 0x1d88720, 0x1f71e60)
	github.com/dop251/goja/func.go:130 +0x3c8
github.com/dop251/goja.(*funcObject).Call-fm(0x1d885e0, 0xc420018d90, 0xc420f75360, 0x1, 0x118, 0x1d88720, 0x1f71e60)
	github.com/dop251/goja/builtin_function.go:120 +0x51
github.com/dop251/goja.(*Runtime).functionproto_call(0xc425d06f00, 0x1d880e0, 0xc4245a40a0, 0xc420f75350, 0x2, 0x119, 0xc420a0b896, 0x4)
	github.com/dop251/goja/builtin_function.go:71 +0x155
github.com/dop251/goja.(*Runtime).(github.com/dop251/goja.functionproto_call)-fm(0x1d880e0, 0xc4245a40a0, 0xc420f75350, 0x2, 0x119, 0x13d3f43, 0x17e1340)
	github.com/dop251/goja/builtin_function.go:160 +0x51
github.com/dop251/goja.(*vm)._nativeCall(0xc420263860, 0xc422d2aa50, 0x2)
	github.com/dop251/goja/vm.go:1826 +0x2ec
github.com/dop251/goja.call.exec(0xffffffff00000002, 0xc420263860)
	github.com/dop251/goja/vm.go:1810 +0x6b3
github.com/dop251/goja.(*call).exec(0xc4212380dc, 0xc420263860)
	<autogenerated>:1 +0x44
github.com/dop251/goja.(*vm).run(0xc420263860)
	github.com/dop251/goja/vm.go:288 +0x51
github.com/dop251/goja.(*funcObject).Call(0xc425be83c0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc427c93d90, 0xc42002b900)
	github.com/dop251/goja/func.go:130 +0x3c8
github.com/dop251/goja.(*funcObject).Call-fm(0x0, 0x0, 0x0, 0x0, 0x0, 0x102ad99, 0xc4201e3fb0)
	github.com/dop251/goja/builtin_function.go:120 +0x51
github.com/dop251/goja.AssertFunction.func1.1()
	github.com/dop251/goja/runtime.go:1374 +0xb6
github.com/dop251/goja.(*vm).try(0xc420263860, 0xc427c93e80, 0x0)
	github.com/dop251/goja/vm.go:370 +0x119
github.com/dop251/goja.AssertFunction.func1(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc427c93f30, 0x0)
	github.com/dop251/goja/runtime.go:1373 +0xdd
github.com/dop251/goja_nodejs/eventloop.(*EventLoop).doTimeout(0xc42087e720, 0xc42448e030)
	github.com/dop251/goja_nodejs/eventloop/eventloop.go:178 +0x6d
github.com/dop251/goja_nodejs/eventloop.(*EventLoop).addTimeout.func1.1()
	github.com/dop251/goja_nodejs/eventloop/eventloop.go:156 +0xad
github.com/dop251/goja_nodejs/eventloop.(*EventLoop).run(0xc42087e720, 0x1)
	github.com/dop251/goja_nodejs/eventloop/eventloop.go:137 +0x72
created by github.com/dop251/goja_nodejs/eventloop.(*EventLoop).Start
	github.com/dop251/goja_nodejs/eventloop/eventloop.go:94 +0x44

Note: Line numbers may not fit the eventloop's code available on github, I've added a lot of fmt.Println to try and debug this, but I haven't changed actual code.

I think I've been running all code that interacts with a *goja.Runtime inside RunOnLoop. My code is transpiled (babel) and then the runtime is polyfilled heavily, but as far as I know, it should work fine. It's been working fine for the most part.

Here's an example of my xhr polyfill: https://gist.github.com/jeromegn/cb76ba14437ab688074c6b2d607d358d (just in case you see anything that would break here)

My question is: Is there any way to make sense of this stacktrace? As far as I understand, the Panic at <n> references the stack level in the previous trace. Except I have no idea how to correlate this with actual JS code.

Doesn't goja keep track of the line and column of the source when making its AST?

Cannot identify error

Getting this error at runtime:

panic: TypeError: Object has no member 'error'

Is it possible to get more clues on which line this error occurred?

Difference from Otto

The README states

This project was largely inspired by otto.

Could you clarify a little how it differs from Otto?

panic on goja.New()

The following code produces a panic:

package main

import (
	"github.com/dop251/goja"
)

func main() {
	goja.New()
}

Output:

⇒  go run main.go 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x11659fb]

goroutine 1 [running]:
github.com/deis/dhow/vendor/golang.org/x/text/collate.newCollator(0x0, 0x0, 0x0)
	/Users/technosophos/Code/Go/src/github.com/deis/dhow/vendor/golang.org/x/text/collate/option.go:30 +0x6b
github.com/deis/dhow/vendor/golang.org/x/text/collate.New(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc42003ff78)
	/Users/technosophos/Code/Go/src/github.com/deis/dhow/vendor/golang.org/x/text/collate/collate.go:60 +0xbc
github.com/deis/dhow/vendor/github.com/dop251/goja.init()
	/Users/technosophos/Code/Go/src/github.com/deis/dhow/vendor/github.com/dop251/goja/builtin_string.go:15 +0x133
main.init()
	/Users/technosophos/Code/Go/src/github.com/deis/dhow/cmd/main.go:10 +0x44
exit status 2

Go version:

⇒  go version
go version go1.8.3 darwin/amd64

Complete dependencies:

⇒  cat glide.lock
hash: f21c0c8c2f06ddf1910034af4875b3baa5e8a8338b33eb8da97e8e5779af078a
updated: 2017-07-01T15:23:06.031128545-06:00
imports:
- name: github.com/dlclark/regexp2
  version: 902a5ce7a7812e2ba9f73b9d96c09d5136df39cd
  subpackages:
  - syntax
- name: github.com/dop251/goja
  version: 8f3380f012a7a699e478fcff1d0e46a6f2e1abc5
  subpackages:
  - ast
  - file
  - parser
  - token
- name: golang.org/x/text
  version: 2910a502d2bf9e43193af9d68ca516529614eed3
  subpackages:
  - cases
  - collate
  - collate/colltab
  - internal/colltab
  - internal/tag
  - language
  - transform
  - unicode/norm
testImports: []

Memory leaks, is my usage wrong?

Hi, this is my code

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"sync"
	"time"

	"github.com/dop251/goja"
	"github.com/dop251/goja_nodejs/console"
	"github.com/dop251/goja_nodejs/require"
)

func handErr(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	if len(os.Args) == 2 {
		filename := os.Args[1]
		src, err := ioutil.ReadFile(filename)
		handErr(err)

		n := 10000
		var w sync.WaitGroup
		w.Add(n)
		for i := 0; i < n; i++ {
			go func() {
				r, err := runFile(filename, string(src))
				handErr(err)
				fmt.Println(*r)
				w.Done()
			}()
		}
		w.Wait()
	}
	fmt.Println("done.")
	<-time.After(10 * time.Minute)
}

func runFile(filename, src string) (*goja.Value, error) {
	runtime := goja.New()
	registry := new(require.Registry)
	registry.Enable(runtime)
	console.Enable(runtime)

	prg, err := goja.Compile(filename, src, false)
	if err != nil {
		return nil, err
	}

	result, err := runtime.RunProgram(prg)
	if err != nil {
		return nil, err
	}
	return &result, nil
}

And the a.js

console.log("a.js init")
exports.foo  = function (){
	console.log("a.js foo()")
}

b.js

var a = require('./test/a.js')
a.foo()
console.log('b.js done.')

Command

go build .\test.go
.\test.exe .\test\b.js

finally , the test.exe used more than 600Mb memory!

ToValue should support map types

Right now, ToValue treats objects of type t map[string]interface{} as objectGoReflect but should probably treat as standard objectGoMapSimple.

SIGSEGV when importing goja

I get SIGSEGV when importing goja, e.g. main.go

package main

import (
    _ "github.com/dop251/goja"
)

func main() {
}
$ go get
$ go run main.go
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x48 pc=0x56c37b]

goroutine 1 [running]:
golang.org/x/text/collate.newCollator(0x0, 0x0, 0x0)
        /home/user/go/src/golang.org/x/text/collate/option.go:30 +0x6b
golang.org/x/text/collate.New(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/user/go/src/golang.org/x/text/collate/collate.go:60 +0xbc
github.com/dop251/goja.init()
        /home/user/go/src/github.com/dop251/goja/builtin_string.go:15 +0x12d
main.init()
        <autogenerated>:1 +0x44
exit status 2

I use ArchLinux with go package version 2:1.9-1, go version go1.9 linux/amd64, goja revision 0aeff75.

Close/Destroy a vm

Hi there,
would be nice to have the possibility to destory the created vm if it makes the go-gb worker more efficent.

I would propose a Close() error method.

Cheers,
Artan

ReferenceError: self is not defined

I have what-wg-fetch loaded by one of the react modules, it seems to be expecting self to be defined.

ReferenceError: self is not defined
at bundle.js:56189:19(7)

the traced code at line 56189:

 56182 /***/ function(module, exports, __webpack_require__) {
 56183
 56184         // the whatwg-fetch polyfill installs the fetch() function
 56185         // on the global object (window or self)
 56186         //
 56187         // Return that as the export for use in Webpack, Browserify etc.
 56188         __webpack_require__(395);
 56189         module.exports = self.fetch.bind(self);
 56190
 56191

I tried setting 'self' as an empty object before running bundle.js, but then I get the error "Host Object is not extensible".

What can I do about this and any other future modules that expects to run on browsers? Was setting self/window an empty object a good approach?

Function to turn a *ast.Program into a *Program

I'm having some fun involving AST trees of JS snippets, and it feels a little silly that if I want to run one of them, I can't just chuck the already parsed tree at a runtime, but have to do the parsing twice.

Please consider sating my gluttony for microoptimizations.

Fetch polyfill Implementation

Hello @dop251,
first of all, need to say thank you for the project.

Just for your information.

The benchmarks were good enough to try to switch to goja from go-duktape. One critical thing I need to have is a fetch polyfill. So, now I have one written for goja, have a look. It's a window.fetch JavaScript polyfill. Would be good if you leave some feedback here.

Thanks

panic when executing Exception.String() or Exception.Error()

following code panics:

package main

import (
	"fmt"
	"time"

	"github.com/dop251/goja"
)

func main() {
	vm := goja.New()
	time.AfterFunc(200*time.Millisecond, func() {
		vm.Interrupt("halt")
	})
	if v, err := vm.RunString(`i`); err == nil {
		fmt.Println(v.Export())
	} else {
		if _, ok := err.(*goja.InterruptedError); ok {
			fmt.Println("script took too long to execute")
		} else {
			fmt.Println(err.Error())
		}
	}
	fmt.Println("it's all over")
}

here is the stack trace:

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/dop251/goja.(*Program).sourceOffset(0x12765d10, 0x0, 0x1)
	path/src/github.com/dop251/goja/compiler.go:170 +0x4e
github.com/dop251/goja.(*stackFrame).position(0x127be260, 0x65623a, 0x0)
	path/src/github.com/dop251/goja/runtime.go:93 +0x2a
github.com/dop251/goja.(*stackFrame).write(0x127be260, 0x127bc9c0)
	path/src/github.com/dop251/goja/runtime.go:108 +0x78
github.com/dop251/goja.(*Exception).Error(0x127c0360, 0x6557f1, 0x1)
	path/src/github.com/dop251/goja/runtime.go:166 +0xc9
main.main()
	path/src/testme/main.go:21 +0x196

lastIndexOf for unicode string doesn't work (panics)

Running this code will cause a panic in string_unicode.go (https://github.com/dop251/goja/blob/master/string_unicode.go#L270)
"abꞐcde".lastIndexOf("cd");

The indexing of the string is starting at the length of the string rather than the appropriate index. By fixing the start value before that line the function works correctly. My current version of lastIndex looks like this:

func (s unicodeString) lastIndex(substr valueString, start int64) int64 {
	var ss []uint16
	switch substr := substr.(type) {
	case unicodeString:
		ss = substr
	case asciiString:
		ss = make([]uint16, len(substr))
		for i := 0; i < len(substr); i++ {
			ss[i] = uint16(substr[i])
		}
	default:
		panic(fmt.Errorf("Unknown string type: %T", substr))
	}

	if start > int64(len(s)-len(ss)) {
		start = int64(len(s) - len(ss))
	}

	// TODO: optimise
	for start >= 0 {
		for i := int64(0); i < int64(len(ss)); i++ {
			if s[start+i] != ss[i] {
				goto nomatch
			}
		}

		return start
	nomatch:
		start--
	}
	return -1
}

repl?

Hi @dop251 ! This is such a cool project.

I was just curious if there is already a REPL (interpretive prompt), or an easy way to make one?

Thank you.

Sharing global scope between runtimes

One interesting feature Otto has is the ability to clone a VM, creating multiple, derivative VMs with their own state, but the same initial global scope. It's really handy for running scripts in parallel.

I can think of two ways this could be implemented:

  • Add a Clone() function, similar to Otto's.
  • Let me access the global scope, so I could Export/ToValue stuff myself.

Panic when throwing "pkg/errors" errors w/ a FieldNameMapper installed

Reproducible test case:

import (
	"reflect"
	"testing"

	"github.com/dop251/goja"
	"github.com/pkg/errors"
)

type FieldNameMapper struct{}

func (FieldNameMapper) FieldName(t reflect.Type, f reflect.StructField) string {
	return f.Name
}

func (FieldNameMapper) MethodName(t reflect.Type, m reflect.Method) string {
	return m.Name
}

func TestThrowPkgError(t *testing.T) {
	rt := goja.New()
	rt.SetFieldNameMapper(FieldNameMapper{})
	rt.Set("fn", func() { panic(rt.NewGoError(errors.New("Error"))) })
	rt.RunString("fn()")
}

Crash log:

	/usr/local/Cellar/go/1.8/libexec/src/testing/testing.go:622 +0x29d
panic(0x1600520, 0xc420364000)
	/usr/local/Cellar/go/1.8/libexec/src/runtime/panic.go:489 +0x2cf
github.com/dop251/goja.(*Runtime).RunProgram.func1(0xc420c6fec8)
	$GOPATH/src/github.com/dop251/goja/runtime.go:756 +0x98
panic(0x1600520, 0xc420364000)
	/usr/local/Cellar/go/1.8/libexec/src/runtime/panic.go:489 +0x2cf
github.com/dop251/goja.(*vm).try.func1(0xc42006ec30, 0x0, 0xc420c6fda0, 0x0, 0x0, 0x0, 0xc420c6fdf8)
	$GOPATH/src/github.com/dop251/goja/vm.go:364 +0x32e
panic(0x15df480, 0xc420385ff0)
	/usr/local/Cellar/go/1.8/libexec/src/runtime/panic.go:489 +0x2cf
github.com/dop251/goja.(*Object).Set.func1(0xc420c6f518)
	$GOPATH/src/github.com/dop251/goja/value.go:725 +0x98
panic(0x15df480, 0xc420385ff0)
	/usr/local/Cellar/go/1.8/libexec/src/runtime/panic.go:489 +0x2cf
reflect.(*rtype).NumField(0x16130a0, 0x3)
	/usr/local/Cellar/go/1.8/libexec/src/reflect/type.go:1015 +0x89
github.com/dop251/goja.(*Runtime).buildFieldInfo(0xc420b34600, 0x1a02e60, 0x16130a0, 0xc420385fe0, 0x1, 0x1, 0xc420221800)
	$GOPATH/src/github.com/dop251/goja/object_goreflect.go:415 +0x4f
github.com/dop251/goja.(*Runtime).buildFieldInfo(0xc420b34600, 0x1a02e60, 0x16453c0, 0x0, 0x0, 0x0, 0xc420221800)
	$GOPATH/src/github.com/dop251/goja/object_goreflect.go:448 +0x373
github.com/dop251/goja.(*Runtime).buildTypeInfo(0xc420b34600, 0x1a02e60, 0x16453c0, 0x1b94b20)
	$GOPATH/src/github.com/dop251/goja/object_goreflect.go:459 +0x5df
github.com/dop251/goja.(*Runtime).typeInfo(0xc420b34600, 0x1a02e60, 0x16453c0, 0x1a02e60)
	$GOPATH/src/github.com/dop251/goja/object_goreflect.go:492 +0xa2
github.com/dop251/goja.(*objectGoReflect).init(0xc4200a82d0)
	$GOPATH/src/github.com/dop251/goja/object_goreflect.go:69 +0x2b8
github.com/dop251/goja.(*Runtime).ToValue(0xc420b34600, 0x1620f00, 0xc420cd1fe0, 0xc420385f30, 0xc420385f20)
	$GOPATH/src/github.com/dop251/goja/runtime.go:956 +0x2f8
github.com/dop251/goja.(*Object).Set(0xc420cca000, 0x16b550b, 0x5, 0x1620f00, 0xc420cd1fe0, 0x0, 0x0)
	$GOPATH/src/github.com/dop251/goja/value.go:730 +0x87
github.com/dop251/goja.(*Runtime).NewGoError(0xc420b34600, 0x19f25c0, 0xc420cd1fe0, 0xc420cd1fe0)
	$GOPATH/src/github.com/dop251/goja/runtime.go:295 +0xdd
[...].TestThrowPkgError.func1()
	$GOPATH/src/.../crash_test.go:24 +0x61
reflect.Value.call(0x15d8e60, 0xc420385db0, 0x13, 0x16b4c19, 0x4, 0x1b93c18, 0x0, 0x0, 0x1041fdc, 0xc420385f00, ...)
	/usr/local/Cellar/go/1.8/libexec/src/reflect/value.go:434 +0x91f
reflect.Value.Call(0x15d8e60, 0xc420385db0, 0x13, 0x1b93c18, 0x0, 0x0, 0x1, 0x1, 0x3)
	/usr/local/Cellar/go/1.8/libexec/src/reflect/value.go:302 +0xa4
github.com/dop251/goja.(*Runtime).wrapReflectFunc.func1(0x1a00a60, 0x1b938c0, 0xc420cd1f40, 0x0, 0x0, 0xc4202217c0, 0x0)
	$GOPATH/src/github.com/dop251/goja/runtime.go:1014 +0x812
github.com/dop251/goja.(*vm)._nativeCall(0xc42006ec30, 0xc4207f48f0, 0x0)
	$GOPATH/src/github.com/dop251/goja/vm.go:1827 +0x2e9
github.com/dop251/goja.call.exec(0xc400000000, 0xc42006ec30)
	$GOPATH/src/github.com/dop251/goja/vm.go:1808 +0x6ff
github.com/dop251/goja.(*call).exec(0xc420385dfc, 0xc42006ec30)
	<autogenerated>:817 +0x55
github.com/dop251/goja.(*vm).run(0xc42006ec30)
	$GOPATH/src/github.com/dop251/goja/vm.go:288 +0x7f
github.com/dop251/goja.(*vm).(github.com/dop251/goja.run)-fm()
	$GOPATH/src/github.com/dop251/goja/vm.go:375 +0x2a
github.com/dop251/goja.(*vm).try(0xc42006ec30, 0xc420c6fe00, 0x0)
	$GOPATH/src/github.com/dop251/goja/vm.go:370 +0x119
github.com/dop251/goja.(*vm).runTry(0xc42006ec30, 0x16d3860)
	$GOPATH/src/github.com/dop251/goja/vm.go:375 +0x4f
github.com/dop251/goja.(*Runtime).RunProgram(0xc420b34600, 0xc420858900, 0x0, 0x0, 0x0, 0x0)
	$GOPATH/src/github.com/dop251/goja/runtime.go:767 +0x210
github.com/dop251/goja.(*Runtime).RunScript(0xc420b34600, 0x0, 0x0, 0x16b4e8d, 0x4, 0x2, 0x1a00420, 0xc420cd1ea0, 0xc420385d00)
	$GOPATH/src/github.com/dop251/goja/runtime.go:746 +0xa8
github.com/dop251/goja.(*Runtime).RunString(0xc420b34600, 0x16b4e8d, 0x4, 0x15d8e60, 0xc420385db0, 0x105ae25, 0x10759d2)
	$GOPATH/src/github.com/dop251/goja/runtime.go:735 +0x51
[...].TestThrowPkgError(0xc42006e9c0)
	$GOPATH/src/.../crash_test.go:25 +0x12a
testing.tRunner(0xc42006e9c0, 0x16d3a30)
	/usr/local/Cellar/go/1.8/libexec/src/testing/testing.go:657 +0x96
created by testing.(*T).Run
	/usr/local/Cellar/go/1.8/libexec/src/testing/testing.go:697 +0x2ca

Can you provider a runtime parameter in native function call ?

Most of the time,i just need use runtime.ToValue() convert GO value , such as

type test struct {
	runtime *goja.Runtime
}

func (t *test) myFunc(call goja.FunctionCall) goja.Value {
	return t.runtime.ToValue(1234)
}

but, must define a struct, if provider a runtime parameter, Things could be better.
thank you.

Null() returns _undefined instead of _null

The Null() function actually returns _undefined instead of the canonical null value.

goja/runtime.go

Lines 1445 to 1447 in 034fb74

func Null() Value {
return _undefined
}

A quick git blame shows that this bug has been present since the initial commit, but it has probably gone unnoticed since nothing appears to call/test Null().

For example:

fmt.Println("IsNull(Null()):          ", goja.IsNull(goja.Null()))
fmt.Println("IsUndefined(Null()):     ", goja.IsUndefined(goja.Null()))
fmt.Println("IsNull(Undefined()):     ", goja.IsNull(goja.Undefined()))
fmt.Println("IsUndefined(Undefined()):", goja.IsUndefined(goja.Undefined()))

Outputs:

IsNull(Null()):           false
IsUndefined(Null()):      true
IsNull(Undefined()):      false
IsUndefined(Undefined()): true

IsNull(Null()) and IsUndefined(Undefined()) being true should probably have a tiny test each to prevent goofs like this. 😄

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.