Goja - ECMAScript/JavaScript engine in pure Go
#scripting #script-engine #ecma #js #javascript

Dmitry Panov a34e79884e Fixed merge conflicts 3 年之前
.github ef3349e3c1 GitHub actions and go.mod (#269) 4 年之前
ast f82ffd2b2c Merge remote-tracking branch 'psilva261/bigints' into upgrade-tests-bigint 3 年之前
file 625017e51d Try to fix sourcemaps with urls (#325) 4 年之前
ftoa 351feaff53 Ported V8's fast-dtoa. Most conversions should now run at comparable speed to strconv.FormatFloat(). 5 年之前
goja 77b8a67e92 Added readFile() 8 年之前
parser f82ffd2b2c Merge remote-tracking branch 'psilva261/bigints' into upgrade-tests-bigint 3 年之前
testdata 25773609b6 Initial commit 9 年之前
token ce3fee827a Implemented template literals and \u{xxxx}. Closes #260 4 年之前
unistring 5df89c3d31 Support for malformed UTF-16 strings and property keys. Missing String methods. (#146) 5 年之前
.gitignore 25773609b6 Initial commit 9 年之前
.tc39_test262_checkout.sh f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
LICENSE 25773609b6 Initial commit 9 年之前
README.md f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
array.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
array_sparse.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
array_sparse_test.go 6fc852574a Fixed Export() of arrays with property values. Fixes #270. 4 年之前
array_test.go 946559a566 Fixed typed arrays' defineProperty and indexing. Fixes #308. 4 年之前
builtin_array.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_arrray_test.go ad1be0d693 Fixed possible panic when sorting non-standard arrays. 4 年之前
builtin_bigint.go 307513316c Basic implementation of BigInt, BigInt64Array and BigUint64Array 3 年之前
builtin_boolean.go 25773609b6 Initial commit 9 年之前
builtin_date.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_error.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_function.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_global.go 2440f896e9 Add globalThis 4 年之前
builtin_global_test.go 25773609b6 Initial commit 9 年之前
builtin_json.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_json_test.go b206dd5e2c Added missing Regexp functionality, enhanced unicode and UTF-16 support (#171) 5 年之前
builtin_map.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_map_test.go 1236576b0d Typedarrays (#137) 5 年之前
builtin_math.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_number.go 19cfe0fd8a Fixed some edge cases 4 年之前
builtin_object.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_promise.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_proxy.go 946559a566 Fixed typed arrays' defineProperty and indexing. Fixes #308. 4 年之前
builtin_proxy_test.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_reflect.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_regexp.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_set.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_set_test.go d63d7d53d2 Fixed panics when iterator returns values with missing properties. 5 年之前
builtin_string.go 307513316c Basic implementation of BigInt, BigInt64Array and BigUint64Array 3 年之前
builtin_string_test.go 462d53687b Fixed writing ASCII string into a valueStringBuilder that contains unicode. Fixes #283. 4 年之前
builtin_symbol.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_typedarrays.go a34e79884e Fixed merge conflicts 3 年之前
builtin_typedarrays_test.go a55e4cfac4 More typed arrays fixes 4 年之前
builtin_weakmap.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_weakmap_test.go eb3de9ec1a Changed WeakMap implementation to avoid memory leaks in some common usage scenarios. Fixes #250. 4 年之前
builtin_weakset.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
builtin_weakset_test.go eb3de9ec1a Changed WeakMap implementation to avoid memory leaks in some common usage scenarios. Fixes #250. 4 年之前
compiler.go 32956a348b Arrow function (#319) 4 年之前
compiler_expr.go a34e79884e Fixed merge conflicts 3 年之前
compiler_stmt.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
compiler_test.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
date.go 00bb30d125 Fixed ExportType() for Date objects 4 年之前
date_parser.go 6338b32468 Treat date-only formats as UTC and date-time as local timezone. Added support for additional datetime formats. Fixes #281, fixes #292. 4 年之前
date_parser_test.go 5e65f9206b Date.parse() now returns a number. Switched to own date parser for better compatibility (extended years, etc.). Fixes #79 6 年之前
date_test.go 00bb30d125 Fixed ExportType() for Date objects 4 年之前
destruct.go f82ffd2b2c Merge remote-tracking branch 'psilva261/bigints' into upgrade-tests-bigint 3 年之前
func.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
func_test.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
go.mod 96fa0aa6d9 Removed erroneous stacktrace frame when exception is thrown from a catch clause. 4 年之前
go.sum a7a3a1366b Destructuring assignments, rest and spread properties, default function parameters (#303) 4 年之前
ipow.go 25773609b6 Initial commit 9 年之前
map.go 5df89c3d31 Support for malformed UTF-16 strings and property keys. Missing String methods. (#146) 5 年之前
map_test.go 6060b0671c Exposed Symbol 4 年之前
object.go f82ffd2b2c Merge remote-tracking branch 'psilva261/bigints' into upgrade-tests-bigint 3 年之前
object_args.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
object_dynamic.go f82ffd2b2c Merge remote-tracking branch 'psilva261/bigints' into upgrade-tests-bigint 3 年之前
object_dynamic_test.go 084ecb42b0 Implemented DynamicObject and DynamicArray as a simplified Proxy alternative 4 年之前
object_gomap.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
object_gomap_reflect.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
object_gomap_reflect_test.go 5df89c3d31 Support for malformed UTF-16 strings and property keys. Missing String methods. (#146) 5 年之前
object_gomap_test.go 5df89c3d31 Support for malformed UTF-16 strings and property keys. Missing String methods. (#146) 5 年之前
object_goreflect.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
object_goreflect_test.go 6e5fa0950d Prefer the exported value when doing type conversions to preserve wrapped values. Only use reflect.Convert() if the types' kinds match. Closes #329. 4 年之前
object_goslice.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
object_goslice_reflect.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
object_goslice_reflect_test.go ac5354e9a8 Report 'length' as own property for Go slices. Fixes #328. 4 年之前
object_goslice_test.go ac5354e9a8 Report 'length' as own property for Go slices. Fixes #328. 4 年之前
object_lazy.go f82ffd2b2c Merge remote-tracking branch 'psilva261/bigints' into upgrade-tests-bigint 3 年之前
object_test.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
proxy.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
regexp.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
regexp_test.go f5884268f0 String.prototype.matchAll implementation (#248) 4 年之前
runtime.go a34e79884e Fixed merge conflicts 3 年之前
runtime_test.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
string.go f1567f3952 Upgraded tests, lots of fixes as a result 3 年之前
string_ascii.go 307513316c Basic implementation of BigInt, BigInt64Array and BigUint64Array 3 年之前
string_test.go 1c1127b852 Fixed getter for out-of-bounds integer properties of String objects 4 年之前
string_unicode.go 307513316c Basic implementation of BigInt, BigInt64Array and BigUint64Array 3 年之前
tc39_norace_test.go e67f3e7fa7 Fixed formatting for go 1.17 4 年之前
tc39_race_test.go e67f3e7fa7 Fixed formatting for go 1.17 4 年之前
tc39_test.go a34e79884e Fixed merge conflicts 3 年之前
typedarrays.go f82ffd2b2c Merge remote-tracking branch 'psilva261/bigints' into upgrade-tests-bigint 3 年之前
typedarrays_test.go a55e4cfac4 More typed arrays fixes 4 年之前
value.go f82ffd2b2c Merge remote-tracking branch 'psilva261/bigints' into upgrade-tests-bigint 3 年之前
vm.go f82ffd2b2c Merge remote-tracking branch 'psilva261/bigints' into upgrade-tests-bigint 3 年之前
vm_test.go f3cfc97811 Block-scoped declarations (#264) 4 年之前

README.md

goja

ECMAScript 5.1(+) implementation in Go.

GoDoc

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.

Minimum required Go version is 1.14.

Features

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 https://github.com/dop251/goja/issues/250 and https://github.com/dop251/goja/issues/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:

  • Using AssertFunction():

    vm := New()
    _, err := vm.RunString(`
    function sum(a, b) {
    return a+b;
    }
    `)
    if err != nil {
    panic(err)
    }
    sum, ok := AssertFunction(vm.Get("sum"))
    if !ok {
    panic("Not a function")
    }
    
    res, err := sum(Undefined(), vm.ToValue(40), vm.ToValue(2))
    if err != nil {
    panic(err)
    }
    fmt.Println(res)
    // Output: 42
    
  • Using Runtime.ExportTo():

    const SCRIPT = `
    function f(param) {
    return +param + 2;
    }
    `
    
    vm := New()
    _, err := vm.RunString(SCRIPT)
    if err != nil {
    panic(err)
    }
    
    var fn func(string) string
    err = vm.ExportTo(vm.Get("f"), &fn)
    if err != nil {
    panic(err)
    }
    
    fmt.Println(fn("40")) // 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 := 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 := 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 = 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 := 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.