gopkg.in/olebedev/go-duktape.v1@v1.0.0-20151008052556-e2ae92f01e4a/README.md (about) 1 # Duktape bindings for Go(Golang) [![wercker status](https://app.wercker.com/status/1ce7671d7223880e967bf8a81b96341d/s/master "wercker status")](https://app.wercker.com/project/bykey/1ce7671d7223880e967bf8a81b96341d) 2 [Duktape](http://duktape.org/index.html) is a thin, embeddable javascript engine. 3 Most of the [api](http://duktape.org/api.html) is implemented. 4 The exceptions are listed [here](https://github.com/olebedev/go-duktape/blob/master/api.go#L1679). 5 6 ### Usage 7 8 The package is fully go-getable, no need to install any external C libraries. 9 So, just type `go get gopkg.in/olebedev/go-duktape.v1` to install. 10 11 12 ```go 13 package main 14 15 import "fmt" 16 import "gopkg.in/olebedev/go-duktape.v1" 17 18 func main() { 19 ctx := duktape.New() 20 ctx.EvalString(`2 + 3`) 21 result := ctx.GetNumber(-1) 22 ctx.Pop() 23 fmt.Println("result is:", result) 24 } 25 ``` 26 27 ### Go specific notes 28 29 Bindings between Go and Javascript contexts are not fully functional. 30 However, binding a Go function to the Javascript context is available: 31 ```go 32 package main 33 34 import "fmt" 35 import "gopkg.in/olebedev/go-duktape.v1" 36 37 func main() { 38 ctx := duktape.New() 39 ctx.PushGlobalGoFunction("log", func(c *duktape.Context) int { 40 fmt.Println(c.SafeToString(-1)) 41 return 0 42 }) 43 ctx.EvalString(`log('Go lang Go!')`) 44 } 45 ``` 46 then run it. 47 ```bash 48 $ go run *.go 49 Go lang Go! 50 $ 51 ``` 52 53 ### Timers 54 55 There is a method to inject timers to the global scope: 56 ```go 57 package main 58 59 import "fmt" 60 import "gopkg.in/olebedev/go-duktape.v1" 61 62 func main() { 63 ctx := duktape.New() 64 65 // Let's inject `setTimeout`, `setInterval`, `clearTimeout`, 66 // `clearInterval` into global scope. 67 ctx.DefineTimers() 68 69 ch := make(chan string) 70 ctx.PushGlobalGoFunction("second", func(_ *Context) int { 71 ch <- "second step" 72 return 0 73 }) 74 ctx.PevalString(` 75 setTimeout(second, 0); 76 print('first step'); 77 `) 78 fmt.Println(<-ch) 79 } 80 ``` 81 then run it 82 ```bash 83 $ go run *.go 84 first step 85 second step 86 $ 87 ``` 88 89 90 ### Status 91 92 The package is not fully tested, so be careful. 93 94 95 ### Contribution 96 97 Pull requests are welcome! 98 __Convention:__ fork the repository and make changes on your fork in a feature branch.