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

Dmitry Panov 2bb4c724c0 Replaced map[Value]... with propNameSet because Value can be unhashable (if it's a unicodeString). Fixes #691. 1 week ago
.github 203961f822 Implemented logical assignment operators. Closes #647. 9 months ago
ast fc55792775 Fix indexes of nodes (#532) 2 years ago
file 311323fba4 Improved cross-OS compatibility for sourcemap URL resolution. 9 months ago
ftoa f32f727444 Fixed formatting for Go 1.19 3 years ago
goja 85e2e6106c Removed the usage of deprecated ioutil package. Bumped minimum Go version to 1.16. 3 years ago
parser 9b5d4e417b Fix https://github.com/dop251/goja/issues/688 3 weeks ago
testdata 25773609b6 Initial commit 9 years ago
token 203961f822 Implemented logical assignment operators. Closes #647. 9 months ago
unistring c5c52bd8d6 Optimized CPU and memory usage when importing strings and using JSON.{parse,stringify}(). 3 years ago
.gitignore 25773609b6 Initial commit 9 years ago
.tc39_test262_checkout.sh ac2ea62348 Updated tc39 tests. A number of fixes as a result: 1 year ago
LICENSE 25773609b6 Initial commit 9 years ago
README.md 46d383d606 Update README.md - setTimeout()/setInterval() (#643) 11 months ago
array.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
array_sparse.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
array_sparse_test.go f3aaa50fcb Improved export of Map, Set and iterable objects. Closes #368. 3 years ago
array_test.go ddea1f4469 Fixed misuse of reflect.SliceHeader. Added go vet. (#365) 3 years ago
builtin_array.go a52ceb6d86 Implemented {Array, TypedArray}.prototype.{with, toReversed, toSorted} 1 year ago
builtin_arrray_test.go a52ceb6d86 Implemented {Array, TypedArray}.prototype.{with, toReversed, toSorted} 1 year ago
builtin_bigint.go fa6d1ed5e4 Implemented BigInt (#597) 1 year ago
builtin_bigint_test.go fa6d1ed5e4 Implemented BigInt (#597) 1 year ago
builtin_boolean.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_date.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_error.go a38ab1794d Added Error cause 1 year ago
builtin_function.go 5ef83b82af Correctly handle null or undefined second argument to Function.prototype.apply(). Fixes #655 10 months ago
builtin_function_test.go 5ef83b82af Correctly handle null or undefined second argument to Function.prototype.apply(). Fixes #655 10 months ago
builtin_global.go fa6d1ed5e4 Implemented BigInt (#597) 1 year ago
builtin_global_test.go bf6af58bbc Refactored tests 4 years ago
builtin_json.go fa6d1ed5e4 Implemented BigInt (#597) 1 year ago
builtin_json_test.go 594410467b Correctly detect circular references in JSON.stringify for wrapped Go objects. Fixes #543. 2 years ago
builtin_map.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_map_test.go cb5011b539 Fixed the order in which the adder function and the iterator are obtained in {Weak}{Set,Map} constructors. 3 years ago
builtin_math.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_number.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_object.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_promise.go 79f3a7efcd Promise resolve and reject now return Interrupt errors (#624) 1 year ago
builtin_proxy.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_proxy_test.go 2bb4c724c0 Replaced map[Value]... with propNameSet because Value can be unhashable (if it's a unicodeString). Fixes #691. 1 week ago
builtin_reflect.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_regexp.go af2ceb9156 Fallback to regexp2 if repeat count is too large. Fixes #681 1 month ago
builtin_set.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_set_test.go cb5011b539 Fixed the order in which the adder function and the iterator are obtained in {Weak}{Set,Map} constructors. 3 years ago
builtin_string.go fa6d1ed5e4 Implemented BigInt (#597) 1 year ago
builtin_string_test.go b1681fb2a2 Fixed String.prototype.split() when string is empty and limit is > 0. Fixes #588. 1 year ago
builtin_symbol.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_typedarrays.go c665ba5341 Fix the subarray set method 3 weeks ago
builtin_typedarrays_test.go c665ba5341 Fix the subarray set method 3 weeks ago
builtin_weakmap.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_weakmap_test.go cb5011b539 Fixed the order in which the adder function and the iterator are obtained in {Weak}{Set,Map} constructors. 3 years ago
builtin_weakset.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
builtin_weakset_test.go cb5011b539 Fixed the order in which the adder function and the iterator are obtained in {Weak}{Set,Map} constructors. 3 years ago
compiler.go cb187b0869 Fixed extra value left on stack when 'this' escapes to stash. 6 months ago
compiler_expr.go 4d26883d18 Fixed incorrect 'this' value when arguments remain on stack. Closes #670 5 months ago
compiler_stmt.go 203961f822 Implemented logical assignment operators. Closes #647. 9 months ago
compiler_test.go 4d26883d18 Fixed incorrect 'this' value when arguments remain on stack. Closes #670 5 months ago
date.go 02193f39d0 Reimplement date parser (#611) 1 year ago
date_parser.go 02193f39d0 Reimplement date parser (#611) 1 year ago
date_test.go e37b27fbba Added V8 date parser test 1 year ago
destruct.go 2bb4c724c0 Replaced map[Value]... with propNameSet because Value can be unhashable (if it's a unicodeString). Fixes #691. 1 week ago
extract_failed_tests.sh eb99973cc1 Added toString tag to Reflect, allowed tests without id to run. Closes #416. 3 years ago
func.go c0d5092cad Make Export() for classes return func(ConstructorCall) *Object. Closes #671 5 months ago
func_test.go 58d95d85e9 Fixed test 5 months ago
go.mod 8130cadc57 Skip Unicode 14 and 15 tests for Go versions < 1.21 1 year ago
go.sum 8130cadc57 Skip Unicode 14 and 15 tests for Go versions < 1.21 1 year ago
ipow.go b196ae5149 Check for overflows in ipow() 2 years ago
map.go 5df89c3d31 Support for malformed UTF-16 strings and property keys. Missing String methods. (#146) 5 years ago
map_test.go fa6d1ed5e4 Implemented BigInt (#597) 1 year ago
object.go 2bb4c724c0 Replaced map[Value]... with propNameSet because Value can be unhashable (if it's a unicodeString). Fixes #691. 1 week ago
object_args.go f3aaa50fcb Improved export of Map, Set and iterable objects. Closes #368. 3 years ago
object_dynamic.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
object_dynamic_test.go fca9b95977 Added NewSharedDynamicObject() and NewSharedDynamicArray(). Closes #418. 3 years ago
object_goarray_reflect.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
object_goarray_reflect_test.go 1444e6b945 Refactored conversion to primitive. Always use `String()` method if defined. Closes #423. 3 years ago
object_gomap.go 28ee0ee714 Refactored conversion to primitive to better match modern ECMAScript standard. Wrapped Go values no longer have "toString" and "valueOf" methods defined on them. Closes #512. 2 years ago
object_gomap_reflect.go c665f0b58f Properly convert reflect map keys into strings. Fixes #590. 1 year ago
object_gomap_reflect_test.go c665f0b58f Properly convert reflect map keys into strings. Fixes #590. 1 year ago
object_gomap_test.go bf6af58bbc Refactored tests 4 years ago
object_goreflect.go 21e33551e7 Always use container's address, if we can. Fixes #620. 1 year ago
object_goreflect_test.go 21e33551e7 Always use container's address, if we can. Fixes #620. 1 year ago
object_goslice.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
object_goslice_reflect.go 7749907a8a Update the length property when it's accessed, rather than on grow/shrink. Maintain pointer type of a wrapped *[]any. Fixes #521. 2 years ago
object_goslice_reflect_test.go 7749907a8a Update the length property when it's accessed, rather than on grow/shrink. Maintain pointer type of a wrapped *[]any. Fixes #521. 2 years ago
object_goslice_test.go 7749907a8a Update the length property when it's accessed, rather than on grow/shrink. Maintain pointer type of a wrapped *[]any. Fixes #521. 2 years ago
object_template.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
object_test.go eb1f15ee67 Added Object.GetOwnPropertyNames(). Closes #584. 1 year ago
profiler.go 2b3c5c716f Collect profiler samples after stop 2 years ago
profiler_test.go 2b3c5c716f Collect profiler samples after stop 2 years ago
proxy.go 2bb4c724c0 Replaced map[Value]... with propNameSet because Value can be unhashable (if it's a unicodeString). Fixes #691. 1 week ago
regexp.go 7516b814d4 Fixed setting of lastIndex after String.replace() and String.match(). Closes #682 1 month ago
regexp_test.go af2ceb9156 Fallback to regexp2 if repeat count is too large. Fixes #681 1 month ago
runtime.go bcd7cc6bf6 Added IsNumber(), IsBigInt() and IsString() convenience methods. Treat nil *big.Int as 0n. 9 months ago
runtime_test.go c0d5092cad Make Export() for classes return func(ConstructorCall) *Object. Closes #671 5 months ago
staticcheck.conf a5e7acf16e Added staticcheck (#364) 3 years ago
string.go fa6d1ed5e4 Implemented BigInt (#597) 1 year ago
string_ascii.go 016eb72565 Numeric separator literal (#603) 1 year ago
string_imported.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
string_test.go c933cf95e1 Exposed String and StringBuilder. Closes #321. 2 years ago
string_unicode.go 9410bcaa81 Implemented template-backed objects and used them for most of the built-ins. Closes #524, closes #459. 2 years ago
tc39_norace_test.go a5e7acf16e Added staticcheck (#364) 3 years ago
tc39_race_test.go e67f3e7fa7 Fixed formatting for go 1.17 4 years ago
tc39_test.go 203961f822 Implemented logical assignment operators. Closes #647. 9 months ago
typedarrays.go cf18d89f3c Fixed exporting of typed arrays and DataView into []byte so that it respects the offset and length. 2 months ago
typedarrays_test.go fa6d1ed5e4 Implemented BigInt (#597) 1 year ago
value.go c0d5092cad Make Export() for classes return func(ConstructorCall) *Object. Closes #671 5 months ago
vm.go 203961f822 Implemented logical assignment operators. Closes #647. 9 months ago
vm_test.go cba40bd09c Optimised the handling of literal values during compilation. Bumped minimum required Go version to 1.20. Closes #566. 1 year ago

README.md

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 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()/setInterval()?

setTimeout() and setInterval() are common functions to provide concurrent execution in ECMAScript environments, but the two functions are not part of the ECMAScript standard. Browsers and NodeJS just happen to provide similar, but not identical, functions. The hosting application need to control the environment for concurrent execution, e.g. an event loop, and supply the functionality to script code.

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():

    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
    
  • Using Runtime.ExportTo():

    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.