Giter VIP home page Giter VIP logo

go-duktape's Introduction

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.

go-duktape's People

Contributors

olebedev avatar pytlesk4 avatar nilium avatar mcuadros avatar karalabe avatar ashukur avatar agazso avatar dhiltonp avatar yazgazan avatar hampusohlsson avatar cryptix avatar bluele avatar buger avatar miku avatar

Watchers

twkun avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.