1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# Duktape bindings for Go(Golang)
[![wercker status](https://app.wercker.com/status/3a5bb2e639a4b4efaf4c8bf7cab7442d/s "wercker status")](https://app.wercker.com/project/bykey/3a5bb2e639a4b4efaf4c8bf7cab7442d)
[![Travis status](https://travis-ci.org/olebedev/go-duktape.svg?branch=v3)](https://travis-ci.org/olebedev/go-duktape)
[![Appveyor status](https://ci.appveyor.com/api/projects/status/github/olebedev/go-duktape?branch=v3&svg=true)](https://ci.appveyor.com/project/olebedev/go-duktape/branch/v3)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/olebedev/go-duktape?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[Duktape](http://duktape.org/index.html) is a thin, embeddable javascript engine.
Most of the [api](http://duktape.org/api.html) is implemented.
The exceptions are listed [here](https://github.com/olebedev/go-duktape/blob/master/api.go#L1566).
### Usage
The package is fully go-getable, no need to install any external C libraries.
So, just type `go get gopkg.in/olebedev/go-duktape.v3` to install.
```go
package main
import "fmt"
import "gopkg.in/olebedev/go-duktape.v3"
func main() {
ctx := duktape.New()
ctx.PevalString(`2 + 3`)
result := ctx.GetNumber(-1)
ctx.Pop()
fmt.Println("result is:", result)
// To prevent memory leaks, don't forget to clean up after
// yourself when you're done using a context.
ctx.DestroyHeap()
}
```
### Go specific notes
Bindings between Go and Javascript contexts are not fully functional.
However, binding a Go function to the Javascript context is available:
```go
package main
import "fmt"
import "gopkg.in/olebedev/go-duktape.v3"
func main() {
ctx := duktape.New()
ctx.PushGlobalGoFunction("log", func(c *duktape.Context) int {
fmt.Println(c.SafeToString(-1))
return 0
})
ctx.PevalString(`log('Go lang Go!')`)
}
```
then run it.
```bash
$ go run *.go
Go lang Go!
$
```
### Timers
There is a method to inject timers to the global scope:
```go
package main
import "fmt"
import "gopkg.in/olebedev/go-duktape.v3"
func main() {
ctx := duktape.New()
// Let's inject `setTimeout`, `setInterval`, `clearTimeout`,
// `clearInterval` into global scope.
ctx.PushTimers()
ch := make(chan string)
ctx.PushGlobalGoFunction("second", func(_ *Context) int {
ch <- "second step"
return 0
})
ctx.PevalString(`
setTimeout(second, 0);
print('first step');
`)
fmt.Println(<-ch)
}
```
then run it
```bash
$ go run *.go
first step
second step
$
```
Also you can `FlushTimers()`.
### Command line tool
Install `go get gopkg.in/olebedev/go-duktape.v3/...`.
Execute file.js: `$GOPATH/bin/go-duk file.js`.
### Benchmarks
| prog | time |
| ------------|-------|
|[otto](https://github.com/robertkrimen/otto)|200.13s|
|[anko](https://github.com/mattn/anko)|231.19s|
|[agora](https://github.com/PuerkitoBio/agora/)|149.33s|
|[GopherLua](https://github.com/yuin/gopher-lua/)|8.39s|
|**go-duktape**|**9.80s**|
More details are [here](https://github.com/olebedev/go-duktape/wiki/Benchmarks).
### Status
The package is not fully tested, so be careful.
### Contribution
Pull requests are welcome! Also, if you want to discuss something send a pull request with proposal and changes.
__Convention:__ fork the repository and make changes on your fork in a feature branch.
|