Skip to content

Commit

Permalink
Rename to sobek
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jun 10, 2024
1 parent ccbae20 commit 2877de0
Show file tree
Hide file tree
Showing 107 changed files with 224 additions and 382 deletions.
50 changes: 25 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
goja
sobek
====

ECMAScript 5.1(+) implementation in Go.

[![Go Reference](https://pkg.go.dev/badge/github.com/dop251/goja.svg)](https://pkg.go.dev/github.com/dop251/goja)
[![Go Reference](https://pkg.go.dev/badge/github.com/grafana/sobek.svg)](https://pkg.go.dev/github.com/grafana/sobek)

Goja is an implementation of ECMAScript 5.1 in pure Go with emphasis on standard compliance and
performance.
Expand All @@ -20,7 +20,7 @@ Features
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
* Most of ES6 functionality, still work in progress, see https://github.com/grafana/sobek/milestone/1?closed=1

Known incompatibilities and caveats
-----------------------------------
Expand Down Expand Up @@ -98,21 +98,21 @@ It gives you a much better control over execution environment so can be useful f

### Is it goroutine-safe?

No. An instance of goja.Runtime can only be used by a single goroutine
No. An instance of sobek.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](https://github.com/dop251/goja_nodejs) aimed at providing some NodeJS functionality,
There is a [separate project](https://github.com/grafana/sobek_nodejs) 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](https://github.com/dop251/goja/milestone/1) are either in progress
for ETAs. Features that are open in the [milestone](https://github.com/grafana/sobek/milestone/1) 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.
Expand Down Expand Up @@ -141,7 +141,7 @@ Basic Example
Run JavaScript and get the result value.

```go
vm := goja.New()
vm := sobek.New()
v, err := vm.RunString("2 + 2")
if err != nil {
panic(err)
Expand All @@ -153,13 +153,13 @@ if num := v.Export().(int64); num != 4 {

Passing Values to JS
--------------------
Any Go value can be passed to JS using Runtime.ToValue() method. See the method's [documentation](https://pkg.go.dev/github.com/dop251/goja#Runtime.ToValue) for more details.
Any Go value can be passed to JS using Runtime.ToValue() method. See the method's [documentation](https://pkg.go.dev/github.com/grafana/sobek#Runtime.ToValue) 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()](https://pkg.go.dev/github.com/dop251/goja#Runtime.ExportTo) method.
Alternatively it can be exported into a specific Go variable using [Runtime.ExportTo()](https://pkg.go.dev/github.com/grafana/sobek#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.
Expand All @@ -168,40 +168,40 @@ Calling JS functions from Go
----------------------------
There are 2 approaches:

- Using [AssertFunction()](https://pkg.go.dev/github.com/dop251/goja#AssertFunction):
- Using [AssertFunction()](https://pkg.go.dev/github.com/grafana/sobek#AssertFunction):
```go
const SCRIPT = `
function sum(a, b) {
return +a + b;
}
`

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

res, err := sum(goja.Undefined(), vm.ToValue(40), vm.ToValue(2))
res, err := sum(sobek.Undefined(), vm.ToValue(40), vm.ToValue(2))
if err != nil {
panic(err)
}
fmt.Println(res)
// Output: 42
```
- Using [Runtime.ExportTo()](https://pkg.go.dev/github.com/dop251/goja#Runtime.ExportTo):
- Using [Runtime.ExportTo()](https://pkg.go.dev/github.com/grafana/sobek#Runtime.ExportTo):
```go
const SCRIPT = `
function sum(a, b) {
return +a + b;
}
`

vm := goja.New()
vm := sobek.New()
_, err := vm.RunString(SCRIPT)
if err != nil {
panic(err)
Expand All @@ -224,10 +224,10 @@ 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](https://pkg.go.dev/github.com/dop251/goja#FieldNameMapper):
dealing with a 3rd party library, you can use a [FieldNameMapper](https://pkg.go.dev/github.com/grafana/sobek#FieldNameMapper):

```go
vm := goja.New()
vm := sobek.New()
vm.SetFieldNameMapper(TagFieldNameMapper("json", true))
type S struct {
Field int `json:"field"`
Expand All @@ -238,14 +238,14 @@ fmt.Println(res.Export())
// Output: 42
```

There are two standard mappers: [TagFieldNameMapper](https://pkg.go.dev/github.com/dop251/goja#TagFieldNameMapper) and
[UncapFieldNameMapper](https://pkg.go.dev/github.com/dop251/goja#UncapFieldNameMapper), or you can use your own implementation.
There are two standard mappers: [TagFieldNameMapper](https://pkg.go.dev/github.com/grafana/sobek#TagFieldNameMapper) and
[UncapFieldNameMapper](https://pkg.go.dev/github.com/grafana/sobek#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()](https://pkg.go.dev/github.com/dop251/goja#Runtime.ToValue) documentation for more details.
In order to implement a constructor function in Go use `func (sobek.ConstructorCall) *sobek.Object`.
See [Runtime.ToValue()](https://pkg.go.dev/github.com/grafana/sobek#Runtime.ToValue) documentation for more details.

Regular Expressions
-------------------
Expand All @@ -259,7 +259,7 @@ Any exception thrown in JavaScript is returned as an error of type *Exception. I
by using the Value() method:

```go
vm := goja.New()
vm := sobek.New()
_, err := vm.RunString(`
throw("Test");
Expand All @@ -284,7 +284,7 @@ func Test() {
panic(vm.ToValue("Error"))
}

vm = goja.New()
vm = sobek.New()
vm.Set("Test", Test)
_, err := vm.RunString(`
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestInterrupt(t *testing.T) {
}
`

vm := goja.New()
vm := sobek.New()
time.AfterFunc(200 * time.Millisecond, func() {
vm.Interrupt("halt")
})
Expand All @@ -331,4 +331,4 @@ func TestInterrupt(t *testing.T) {
NodeJS Compatibility
--------------------

There is a [separate project](https://github.com/dop251/goja_nodejs) aimed at providing some of the NodeJS functionality.
There is a [separate project](https://github.com/grafana/sobek_nodejs) aimed at providing some of the NodeJS functionality.
4 changes: 2 additions & 2 deletions array.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"fmt"
Expand All @@ -7,7 +7,7 @@ import (
"reflect"
"strconv"

"github.com/dop251/goja/unistring"
"github.com/grafana/sobek/unistring"
)

type arrayIterObject struct {
Expand Down
4 changes: 2 additions & 2 deletions array_sparse.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"fmt"
Expand All @@ -8,7 +8,7 @@ import (
"sort"
"strconv"

"github.com/dop251/goja/unistring"
"github.com/grafana/sobek/unistring"
)

type sparseArrayItem struct {
Expand Down
2 changes: 1 addition & 1 deletion array_sparse_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion array_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"reflect"
Expand Down
2 changes: 1 addition & 1 deletion ast/README.markdown
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ast
--
import "github.com/dop251/goja/ast"
import "github.com/grafana/sobek/ast"

Package ast declares types representing a JavaScript AST.

Expand Down
6 changes: 3 additions & 3 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ node types are concerned) and may change in the future.
package ast

import (
"github.com/dop251/goja/file"
"github.com/dop251/goja/token"
"github.com/dop251/goja/unistring"
"github.com/grafana/sobek/file"
"github.com/grafana/sobek/token"
"github.com/grafana/sobek/unistring"
)

type PropertyKind string
Expand Down
2 changes: 1 addition & 1 deletion builtin_array.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"math"
Expand Down
2 changes: 1 addition & 1 deletion builtin_arrray_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import "testing"

Expand Down
2 changes: 1 addition & 1 deletion builtin_boolean.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

func (r *Runtime) booleanproto_toString(call FunctionCall) Value {
var b bool
Expand Down
2 changes: 1 addition & 1 deletion builtin_date.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions builtin_error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package goja
package sobek

import "github.com/dop251/goja/unistring"
import "github.com/grafana/sobek/unistring"

const propNameStack = "stack"

Expand Down
2 changes: 1 addition & 1 deletion builtin_function.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"math"
Expand Down
2 changes: 1 addition & 1 deletion builtin_function_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"testing"
Expand Down
4 changes: 2 additions & 2 deletions builtin_global.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package goja
package sobek

import (
"errors"
"github.com/dop251/goja/unistring"
"github.com/grafana/sobek/unistring"
"io"
"math"
"regexp"
Expand Down
2 changes: 1 addition & 1 deletion builtin_global_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"testing"
Expand Down
4 changes: 2 additions & 2 deletions builtin_json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"bytes"
Expand All @@ -12,7 +12,7 @@ import (
"unicode/utf16"
"unicode/utf8"

"github.com/dop251/goja/unistring"
"github.com/grafana/sobek/unistring"
)

const hex = "0123456789abcdef"
Expand Down
2 changes: 1 addition & 1 deletion builtin_json_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion builtin_map.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"reflect"
Expand Down
2 changes: 1 addition & 1 deletion builtin_map_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion builtin_math.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"math"
Expand Down
4 changes: 2 additions & 2 deletions builtin_number.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package goja
package sobek

import (
"math"
"sync"

"github.com/dop251/goja/ftoa"
"github.com/grafana/sobek/ftoa"
)

func (r *Runtime) toNumber(v Value) Value {
Expand Down
2 changes: 1 addition & 1 deletion builtin_object.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goja
package sobek

import (
"fmt"
Expand Down
10 changes: 5 additions & 5 deletions builtin_promise.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package goja
package sobek

import (
"github.com/dop251/goja/unistring"
"github.com/grafana/sobek/unistring"
"reflect"
)

Expand Down Expand Up @@ -605,18 +605,18 @@ func (r *Runtime) wrapPromiseReaction(fObj *Object) func(interface{}) {
// NewPromise creates and returns a Promise and resolving functions for it.
//
// WARNING: The returned values are not goroutine-safe and must not be called in parallel with VM running.
// In order to make use of this method you need an event loop such as the one in goja_nodejs (https://github.com/dop251/goja_nodejs)
// In order to make use of this method you need an event loop such as the one in sobek_nodejs (https://github.com/grafana/sobek_nodejs)
// where it can be used like this:
//
// loop := NewEventLoop()
// loop.Start()
// defer loop.Stop()
// loop.RunOnLoop(func(vm *goja.Runtime) {
// loop.RunOnLoop(func(vm *sobek.Runtime) {
// p, resolve, _ := vm.NewPromise()
// vm.Set("p", p)
// go func() {
// time.Sleep(500 * time.Millisecond) // or perform any other blocking operation
// loop.RunOnLoop(func(*goja.Runtime) { // resolve() must be called on the loop, cannot call it here
// loop.RunOnLoop(func(*sobek.Runtime) { // resolve() must be called on the loop, cannot call it here
// resolve(result)
// })
// }()
Expand Down
Loading

0 comments on commit 2877de0

Please sign in to comment.