• This repository has been archived on 21/Jun/2018
  • Stars
    star
    455
  • Rank 96,175 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 7 years ago

Reviews

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

Repository Details

fully transparent bridge between Go and the JavaScript

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

ctx := candyjs.NewContext()
ctx.EvalString(`
  function factorial(n) {
    if (n === 0) return 1;    
    return n * factorial(n - 1);
  }

  print(factorial(10));
`)  //3628800

Call Go functions from JavaScript and vice versa.

ctx := candyjs.NewContext()
ctx.PushGlobalGoFunction("golangMultiply", func(a, b int) int {
    return a * b
})

ctx.EvalString(`print(golangMultiply(5, 10));`) //50

Transparent interface between Go structs and JavaScript.

type MyStruct struct {
    Number int
}

func (m *MyStruct) Multiply(x int) int {
    return m.Number * x
}
...
ctx := candyjs.NewContext()
ctx.PushGlobalStruct("golangStruct", &MyStruct{10})

ctx.EvalString(`print(golangStruct.number);`) //10
ctx.EvalString(`print(golangStruct.multiply(5));`) //50

Import of Go packages into the JavaScript context.

//go:generate candyjs import fmt
...
ctx := candyjs.NewContext()
ctx.EvalString(`
    var fmt = CandyJS.require('fmt');
    fmt.printf('candyjs is %s', 'awesome')
`) // 'candyjs is awesome'

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

JavaScript running a HTTP server

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.

Interpreter code (main.go)

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

Program code (example.js)

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');

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

More Repositories

1

ofelia

A docker job scheduler (aka. crontab for docker)
Go
3,002
star
2

go-syslog

Syslog server library for go.
Go
513
star
3

dockership

dead simple docker deploy tool
Go
302
star
4

go-defaults

Go structures with default values using tags
Go
274
star
5

OctoPrint-TFT

A OctoPrint touch interface for TFT touch modules based on GTK+3
Go
171
star
6

go-version

Version normalizer and comparison library for go
Go
136
star
7

go-jsonschema-generator

json-schemas generator based on Go types
Go
118
star
8

ascode

AsCode - Terraform Alternative Syntax
Go
111
star
9

homebrew-hhvm

HHVM repository for Homebrew
Ruby
103
star
10

go-rpi-rgb-led-matrix

Go binding for rpi-rgb-led-matrix an excellent C++ library to control RGB LED displays with Raspberry Pi GPIO.
Go
83
star
11

pynats

[Abandoned] A Python client for the NATS messaging system.
Python
75
star
12

gce-docker

Google Cloud Engine integration for Docker
Go
59
star
13

go-rpi-ws281x

Go bindings for Raspberry Pi PWM library for WS281X LEDs
Go
41
star
14

go-lookup

Small library on top of reflect for make lookups to any Structs or Maps
Go
36
star
15

go-monitor

a simple and extensible way to build monitorizable go process via HTTP.
Go
32
star
16

rocketizer

[Abandoned] Painless Dockerfile transformation to Rocket containers
Go
28
star
17

go-stable

Golang fixed versions tool for your private and public dependencies
Go
23
star
18

passage

SSH tunnels on steroids
Go
22
star
19

go-crxmake

CRX Package generator
Go
19
star
20

currency-detector

Class to detect the currency from any string
PHP
19
star
21

go-rat

tar extension for random access
Go
19
star
22

php-cayley

PHP wrapper of the Google's Cayley graph database REST interface
PHP
18
star
23

go-octoprint

Go library for accessing the OctoPrint's REST API
Go
17
star
24

go-composer

Basic replacement of Composer, the Dependency Manager for PHP
Go
16
star
25

harvester

Harvesterd is a low footprint collector and parser for events and logs
Go
12
star
26

fabric-composer

Deploy system for PHP composer based projects made with Fabric
Python
11
star
27

silex-hateoas

A RESTful example based on Hateoas and Silex
PHP
8
star
28

lemondb

Extensible MongoDB proxy
Go
8
star
29

go-git-aerospike

Example of go-git backed by a Aerospike
Go
7
star
30

go-raa

fast random access filesystem archive
Go
5
star
31

pimple-hack

A Pimple version in Hack/HHVM, just for fun!
PHP
4
star
32

python-solid-example

Simple example implementing SOLID principles with Python
Python
4
star
33

go-etcd-hydrator

Go
4
star
34

bson-hni

C++
3
star
35

pgwire

Go
3
star
36

ansi-slides

Slide with ANSI style!
PHP
3
star
37

go-time-aggregator

Go
3
star
38

bson-php-cpp

Libbson + PHP-CPP
C++
2
star
39

go-personal

A tiny library to score strings as fullnames or emails.
Go
2
star
40

go-mgo-cache

MongoDB interface for a Go cache
Go
2
star
41

dotfiles

Coming back to Linux! My new config files.
C
2
star
42

golang-arm

Docker Image packaging for golang for ARM platforms.
Shell
2
star
43

dockership-site

Dockership documentation
HTML
1
star
44

natstress

A stress tool for NATS Servers
Go
1
star
45

yCrawler

PHP
1
star
46

cli-array-editor

This class provides an easy way to edit arrays in CLI using vim or your preferred editor.
PHP
1
star
47

statsd-ducksboard-backend

A StatsD backend that sends metrics to Ducksboard
JavaScript
1
star
48

docker-arm

Dockerfiles for ARM
Shell
1
star
49

presentations

Repository of presentations
1
star
50

go-bson-schema

Go
1
star