Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/rosbit/duktape-bridge
- Owner: rosbit
- License: mit
- Created: 2018-11-12T08:52:46.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-12-20T10:07:26.000Z (almost 3 years ago)
- Last Synced: 2024-09-27T06:04:38.567Z (about 2 months ago)
- Topics: c, duktape, easy-to-use, embedding, golang, java, javascript
- Language: Go
- Homepage:
- Size: 940 KB
- Stars: 13
- Watchers: 0
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 mainimport "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 maintype 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.