Skip to content

ramsdenct/go-duktape

 
 

Repository files navigation

Duktape bindings for Go(Golang) wercker status

Duktape is a thin, embeddable javascript engine. Most of the api is implemented. The exceptions are listed here.

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.v1 to install.

package main

import "fmt"
import "gopkg.in/olebedev/go-duktape.v1"

func main() {
  ctx := duktape.New()
  ctx.EvalString(`2 + 3`)
  result := ctx.GetNumber(-1)
  ctx.Pop()
  fmt.Println("result is:", result)
}

Go specific notes

Bindings between Go and Javascript contexts are not fully functional. However, binding a Go function to the Javascript context is available:

package main

import "fmt"
import "gopkg.in/olebedev/go-duktape.v1"

func main() {
  ctx := duktape.New()
  ctx.PushGlobalGoFunction("log", func(c *duktape.Context) int {
    fmt.Println(c.SafeToString(-1))
    return 0
  })
  ctx.EvalString(`log('Go lang Go!')`)
}

then run it.

$ go run *.go
Go lang Go!
$

Timers

There is a method to inject timers to the global scope:

package main

import "fmt"
import "gopkg.in/olebedev/go-duktape.v1"

func main() {
  ctx := duktape.New()

  // Let's inject `setTimeout`, `setInterval`, `clearTimeout`,
  // `clearInterval` into global scope.
  ctx.DefineTimers()

  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

$ go run *.go
first step
second step
$

Status

The package is not fully tested, so be careful.

Contribution

Pull requests are welcome!
Convention: fork the repository and make changes on your fork in a feature branch.

About

Duktape JavaScript engine bindings for Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 97.8%
  • Go 2.2%