|
@@ -21,7 +21,7 @@ Features
|
|
* Capable of running Babel, Typescript compiler and pretty much anything written in ES5.
|
|
* Capable of running Babel, Typescript compiler and pretty much anything written in ES5.
|
|
* Sourcemaps.
|
|
* 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/dop251/goja/milestone/1?closed=1
|
|
-
|
|
|
|
|
|
+
|
|
Known incompatibilities and caveats
|
|
Known incompatibilities and caveats
|
|
-----------------------------------
|
|
-----------------------------------
|
|
|
|
|
|
@@ -77,7 +77,7 @@ FAQ
|
|
|
|
|
|
### How fast is it?
|
|
### How fast is it?
|
|
|
|
|
|
-Although it's faster than many scripting language implementations in Go I have seen
|
|
|
|
|
|
+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
|
|
(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.
|
|
replacement for V8 or SpiderMonkey or any other general-purpose JavaScript engine.
|
|
You can find some benchmarks [here](https://github.com/dop251/goja/issues/2).
|
|
You can find some benchmarks [here](https://github.com/dop251/goja/issues/2).
|
|
@@ -99,7 +99,7 @@ It gives you a much better control over execution environment so can be useful f
|
|
### Is it goroutine-safe?
|
|
### Is it goroutine-safe?
|
|
|
|
|
|
No. An instance of goja.Runtime can only be used by a single goroutine
|
|
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
|
|
|
|
|
|
+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.
|
|
it's not possible to pass object values between runtimes.
|
|
|
|
|
|
### Where is setTimeout()?
|
|
### Where is setTimeout()?
|
|
@@ -171,8 +171,8 @@ There are 2 approaches:
|
|
- Using [AssertFunction()](https://pkg.go.dev/github.com/dop251/goja#AssertFunction):
|
|
- Using [AssertFunction()](https://pkg.go.dev/github.com/dop251/goja#AssertFunction):
|
|
```go
|
|
```go
|
|
const SCRIPT = `
|
|
const SCRIPT = `
|
|
-function f(param) {
|
|
|
|
- return +param + 2;
|
|
|
|
|
|
+function sum(a, b) {
|
|
|
|
+ return +a + b;
|
|
}
|
|
}
|
|
`
|
|
`
|
|
|
|
|
|
@@ -181,12 +181,12 @@ _, err := vm.RunString(SCRIPT)
|
|
if err != nil {
|
|
if err != nil {
|
|
panic(err)
|
|
panic(err)
|
|
}
|
|
}
|
|
-sum, ok := AssertFunction(vm.Get("sum"))
|
|
|
|
|
|
+sum, ok := goja.AssertFunction(vm.Get("sum"))
|
|
if !ok {
|
|
if !ok {
|
|
panic("Not a function")
|
|
panic("Not a function")
|
|
}
|
|
}
|
|
|
|
|
|
-res, err := sum(Undefined(), vm.ToValue(40), vm.ToValue(2))
|
|
|
|
|
|
+res, err := sum(goja.Undefined(), vm.ToValue(40), vm.ToValue(2))
|
|
if err != nil {
|
|
if err != nil {
|
|
panic(err)
|
|
panic(err)
|
|
}
|
|
}
|
|
@@ -196,8 +196,8 @@ fmt.Println(res)
|
|
- Using [Runtime.ExportTo()](https://pkg.go.dev/github.com/dop251/goja#Runtime.ExportTo):
|
|
- Using [Runtime.ExportTo()](https://pkg.go.dev/github.com/dop251/goja#Runtime.ExportTo):
|
|
```go
|
|
```go
|
|
const SCRIPT = `
|
|
const SCRIPT = `
|
|
-function f(param) {
|
|
|
|
- return +param + 2;
|
|
|
|
|
|
+function sum(a, b) {
|
|
|
|
+ return +a + b;
|
|
}
|
|
}
|
|
`
|
|
`
|
|
|
|
|
|
@@ -207,13 +207,13 @@ if err != nil {
|
|
panic(err)
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
|
|
-var fn func(string) string
|
|
|
|
-err = vm.ExportTo(vm.Get("f"), &fn)
|
|
|
|
|
|
+var sum func(int, int) int
|
|
|
|
+err = vm.ExportTo(vm.Get("sum"), &sum)
|
|
if err != nil {
|
|
if err != nil {
|
|
panic(err)
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
|
|
-fmt.Println(fn("40")) // note, _this_ value in the function will be undefined.
|
|
|
|
|
|
+fmt.Println(sum(40, 2)) // note, _this_ value in the function will be undefined.
|
|
// Output: 42
|
|
// Output: 42
|
|
```
|
|
```
|
|
|
|
|