• This repository has been archived on 29/Oct/2021
  • Stars
    star
    777
  • Rank 56,193 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[abandoned] Duktape JavaScript engine bindings for Go

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.

More Repositories

1

go-starter-kit

[abandoned] Golang isomorphic react/hot reloadable/redux/css-modules/SSR starter kit
Go
2,826
star
2

when

A natural language date/time parser with pluggable rules
Go
1,314
star
3

emitter

Emits events in Go way, with wildcard, predicates, cancellation possibilities and many other good wins
Go
483
star
4

config

JSON or YAML configuration wrapper with convenient access methods.
Go
266
star
5

cdn

[abandoned] Content Delivery Network on the top of MongoDb GridFs with on-the-fly image crop/resize
Go
132
star
6

go-tgbot

Golang telegram bot API wrapper, session-based router and middleware
Go
117
star
7

srlt

Simple tool for save and restore states of all existing repositories on the given path
Go
53
star
8

staticbin

Gin middleware/handler for serving static files from binary data
Go
43
star
9

hook-to-trello

Github & Bitbucket web hooks handler for Trello
LiveScript
28
star
10

gojax

A set of extensions for goja javascript engine
Go
27
star
11

swarm

A CRDT-backed reactive real-time data with no merge conflicts, with offline mode. For business-critical data-driven apps on intermittently connected devices.
JavaScript
25
star
12

go-gamp

Google Analytics Measurement Protocol in Golang
Go
16
star
13

gin-cache

Tiny and simple cache middleware for gin framework
Go
15
star
14

rest

REST interface over MongoDB, as middlware for martini framework.
Go
14
star
15

go-duktape-fetch

Server side fetch polyfill for go-duktape
Go
12
star
16

on

CLI for fsnotify
Go
8
star
17

chat

SwarmDB Example Chat Application
JavaScript
4
star
18

node-mystem

Node.js wrapper for `MyStem` morphology text analyzer by Yandex.ru
LiveScript
4
star
19

todo

SwarmDB Example TodoMVC Application. Demo πŸ‘‰
JavaScript
3
star
20

pinentry-mac-keychain

A pinentry program for macOs that stores entered PINs in the macOS KeyChain. Convenient when use with smart cards, like Yubikey
Go
2
star
21

var

tool to fill in json/yaml stdin stream from the environment variables
Go
2
star
22

t2s

Text-To-Speech tool for Russian language, written in Golang
Go
1
star
23

pickle.js

JavaScript implementation of the Python pickle format. Fork.
JavaScript
1
star
24

dotfiles

$HOME sweet home
Vim Script
1
star
25

mice

SwarmDB Example Mice Application
JavaScript
1
star
26

go-googl

A Golang library for https://goo.gl URL shortener API
Go
1
star