Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rosbit/duktape-bridge

A very easy-to-use wrapper of Duktape JavaScript engine, including wrappers for C, Go and Java. The bridge wrapper is also supporting module loader for loading modules written in JS, C and Go. This package is not a binding implementation, it is aimed to be used easily
https://github.com/rosbit/duktape-bridge

c duktape easy-to-use embedding golang java javascript

Last synced: about 1 month ago
JSON representation

A very easy-to-use wrapper of Duktape JavaScript engine, including wrappers for C, Go and Java. The bridge wrapper is also supporting module loader for loading modules written in JS, C and Go. This package is not a binding implementation, it is aimed to be used easily

Awesome Lists containing this project

README

        

# Duktape bridge for C, Go(Golang) and Java.

[Duktape](http://duktape.org/index.html) is a thin, embeddable javascript engine.
Its [api](http://duktape.org/api.html) is very well documented. For most of developers
who are not familiar with C language, it is very tedious to call duk\_push\_xxx() and
duk\_pop() to make use of the power of Duktape. Though there are some binding
implementations of Duktape for languages other than C, most of them inherit the
methods of using API of Duktape.

This package is intended to implement a wrapper called duk-bridge in C, and on top of
the C bridge, wrappers in Go and Java are implemented. All of the bridge wrappers
provide very common functions just like Eval(), CallFunc(), RegisterGoFunc().
So even without any Duktape knowledge, one can embed Duktape to an application.

What's more, Go functions registered to be called in JS are common Go functions.
Given a logic function written in Go, the Go bridge will convert arguments of
JavaScript code to those of Go function, call the Go function and convert the result
from Go function to JavaScript function.

The Go bridge can also make your Go package to a Duktape JavaScript module. So
what you can do is just writing `var mod = require('your_module')`, your Go package is
ready to be called by JS. See sample application [jsgo](https://github.com/rosbit/jsgo),
which is a **standalone** executable, but can be used as a **js interpreter** or even
a mini node.js container.

### Usage

The package is fully go-getable, no need to install any external C libraries.
So, just type

`go get github.com/rosbit/duktape-bridge/duk-bridge-go`

to install.

```go
package main

import "fmt"
import js "github.com/rosbit/duktape-bridge/duk-bridge-go"

func main() {
ctx := js.NewEnv(nil)
defer ctx.Destroy()

res, _ := ctx.Eval("2 + 3")
fmt.Println("result is:", res)

/*
// or run a js file
res, _ := ctx.EvalFile("filename.js")
fmt.Println("result is:", res)
*/
}
```

### Go calls JavaScript function

Suppose there's a js file named `a.js` like this:

```js
function test() {
var prefix = 'v'
var res = {}
for (var i=0; i.so
+-- .js
```

The Go plugin module implementation:

```go
package main

type Test struct {
Name string
Age int
Other map[string]interface{}
}

func NewGoModule() interface{} {
return &Test{"rosbit", 20}
}

func (t *Test) Adder(a1 float64, a2 float64) float64 {
return a1 + a2
}

func (t *Test) OtherFunctions() {
}
```

Save it as `test.go`, then build it to a Go plugin with the command:
`go build -buildmode=plugin test.go`, a file `test.so` is created.
copy it to the `modules` subdirectory. now run the main Go app.

#### Limitation of Go module

Not all Go packages can become js module. There are some limitations:

- function NewGoModule with prototype `func NewGoModule() interface{}` must be in the module,
and it returns a pointer to struct.
- Though as if function NewGoModule() will be called multi-times, in fact Duktape engine will
cache the required module and it is called **only once** even if you `require` it multi-times.
So don't declare module related variables in struct. Only read only constants are acceptable.
- function/field name with the first letter in capital will be exported as moudle method/attribute.
For example, `Adder` will be exported but `adder` will not. But to refer the `Adder` method,
please use `adder` as used in `c.js`

### Duktape bridge for C and Java

- Duktape bridge for C is under the main directory of the project, just run `make`,
a file `duk_bridge.so` will be created, with `duk_bridge.h` and `duk_bridge.so`,
one can embed Duktape in any C/C++ project.
- Duktape bridge for Java is under the subdiretory `duk-bridge-java`, run `make` to
create `dukbridge.jar` and `libdukjs.so`. Then one can run

`java -jar dukbridge.jar -Djava.library.path=. `

to hava a test.
- Of course, with duktape bridge for C, one can implement duktape bridge for
other language like Python.

### Lua vs. Dutakpe

- Duktape borrows a lot from Lua conceptually. The usage of Duktape API
is very similar to that of Lua.
- The difference of Lua and Duktape is the embedding language: Duketape engine supports
JavaScript, which is now very popular.

### Node.js vs. Duktape

- Node.js objects the web application develepment, in the area there exists PHP,
Java Servlet and Python WSGI.
- Duktape focuses the embedding area where one want to add dynamic modification without
rebuild the main application.

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