Skip to content
This repository has been archived by the owner on Jun 21, 2018. It is now read-only.

Latest commit

 

History

History
85 lines (63 loc) · 2.84 KB

README.md

File metadata and controls

85 lines (63 loc) · 2.84 KB

go-candyjs Build Status Coverage Status GoDoc GitHub release

CandyJS is an intent of create a fully transparent bridge between Go and the JavaScript engine duktape. Basicly is a syntax-sugar library built it on top of go-duktape using reflection techniques.

ok but what for ...

build extensible applications that allow to the user execute arbitrary code (let's say plugins) without the requirement of compile it.

Demo

asciicast

Features

  • Embeddable Ecmascript E5/E5.1 compliant engine (duktape).
  • Call Go function from JavaScript and vice versa.
  • Transparent interface between Go structs and JavaScript.
  • Import of Go packages into the JavaScript context.

Installation

The recommended way to install go-candyjs is:

go get -u github.com/mcuadros/go-candyjs/...

CandyJS includes a binary tool used by go generate, please be sure that $GOPATH/bin is on your $PATH

Examples

In this example a gin server is executed and a small JSON is server. In CandyJS you can import Go packages directly if they are defined previously on the Go code.

var time = CandyJS.require('time');
var gin = CandyJS.require('github.com/gin-gonic/gin');

var engine = gin.default();
engine.get("/back", CandyJS.proxy(function(ctx) {
  var future = time.date(2015, 10, 21, 4, 29 ,0, 0, time.UTC);
  var now = time.now();

  ctx.json(200, {
    future: future.string(),
    now: now.string(),
    nsecs: future.sub(now)
  });
}));

engine.run(':8080');

The previous JS can be executed using this small piece of code:

...
//go:generate candyjs import time
//go:generate candyjs import github.com/gin-gonic/gin
func main() {
	ctx := candyjs.NewContext()
	ctx.PevalFile("example.js")
}

Caveats

Due to an incompatibility with Duktape's error handling system and Go, you can't throw errors from Go. All errors generated from Go functions are generic ones error error (rc -100)

License

MIT, see LICENSE