Duktape JavaScript engine bindings for Go
Go
Switch branches/tags
Nothing to show
Clone or download
olebedev Merge pull request #57 from PombeirP/pr/fix-unused-value-note
Fix compiler warning with -Wno-unused-value
Latest commit abf0ba0 Mar 2, 2018
Permalink
Failed to load latest commit information.
cmd/go-duk Update documentation and cmd to use v3 instead of v2 Oct 29, 2017
.gitattributes Add .gitattributes for correct language detection Jan 11, 2017
.gitignore Fix tests Oct 30, 2017
.travis.yml Enable travis builds for 32/64 bit for linux/macos/windows Dec 20, 2017
Gopkg.lock Fix tests Oct 30, 2017
Gopkg.toml Fix tests Oct 30, 2017
LICENSE.md Create LICENSE.md Jan 8, 2015
README.md Add Travis and Appveyor badges to the readme. Dec 20, 2017
api.go Fix compiler warning with -Wno-unused-value Mar 2, 2018
api_test.go Fix fast32 int overflow issues Dec 20, 2017
appveyor.yml Enable travis builds for 32/64 bit for linux/macos/windows Dec 20, 2017
conts.go - remove const that doesn't exist in duktape anymore Nov 8, 2017
duk_alloc_pool.c - update duktape to 2.2 Nov 8, 2017
duk_alloc_pool.h - update duktape to 2.2 Nov 8, 2017
duk_config.h - update duktape to 2.2 Nov 8, 2017
duk_console.c - update duktape to 2.2 Nov 8, 2017
duk_console.h - update duktape to 2.2 Nov 8, 2017
duk_logging.c - update duktape to 2.2 Nov 8, 2017
duk_logging.h - update duktape to 2.2 Nov 8, 2017
duk_minimal_printf.c - update duktape to 2.2 Nov 8, 2017
duk_minimal_printf.h - update duktape to 2.2 Nov 8, 2017
duk_module_duktape.c - update duktape to 2.2 Nov 8, 2017
duk_module_duktape.h - update duktape to 2.2 Nov 8, 2017
duk_module_node.c - update duktape to 2.2 Nov 8, 2017
duk_module_node.h - update duktape to 2.2 Nov 8, 2017
duk_print_alert.c - update duktape to 2.2 Nov 8, 2017
duk_print_alert.h - update duktape to 2.2 Nov 8, 2017
duk_v1_compat.c - update duktape to 2.2 Nov 8, 2017
duk_v1_compat.h - update duktape to 2.2 Nov 8, 2017
duktape.c - update duktape to 2.2 Nov 8, 2017
duktape.go Add OpenBSD support Jan 9, 2018
duktape.h Fix fast32 int overflow issues Dec 20, 2017
duktape_test.go returning idx in PushGoFunction functions May 27, 2015
example_bytecode_test.go Change API to make possible serialize/deserialize duktape function (#47) Aug 7, 2017
timers.go Rename timers methods Nov 3, 2015
timers_test.go Rename timers methods Nov 3, 2015
utils.go Add mutex to the context Aug 6, 2015
utils_test.go Add mutex to the context Aug 6, 2015
wercker.yml Update CI declaration Feb 23, 2016

README.md

Duktape bindings for Go(Golang)

wercker status Travis status Appveyor status Gitter

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

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:

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.

$ 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.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

$ 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 200.13s
anko 231.19s
agora 149.33s
GopherLua 8.39s
go-duktape 9.80s

More details are here.

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.