https://github.com/rosbit/go-qjs
An embeddable JavaScript engine by interacting with QuickJS. 通过与QuickJS交互实现的嵌入式JavaScript
https://github.com/rosbit/go-qjs
embeddable expect golang javascript js quickjs
Last synced: 4 months ago
JSON representation
An embeddable JavaScript engine by interacting with QuickJS. 通过与QuickJS交互实现的嵌入式JavaScript
- Host: GitHub
- URL: https://github.com/rosbit/go-qjs
- Owner: rosbit
- License: mit
- Created: 2022-08-10T08:54:08.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-08-13T01:33:45.000Z (over 3 years ago)
- Last Synced: 2025-01-16T00:24:46.280Z (about 1 year ago)
- Topics: embeddable, expect, golang, javascript, js, quickjs
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-qjs, makes QuickJS be embedded easily
[QuickJS](https://bellard.org/quickjs/) is a small and embeddable Javascript engine written by [Fabrice Bellard](https://bellard.org).
This package is intended to provide a wrapper to interact `QuickJS` with application written in golang.
With some helper functions, `go-qjs` makes it simple to calle QuickJS from Golang, and `go-qjs` can be
treated as an embeddable JavaScript.
### Install
The package is fully go-getable, So, just type
`go get github.com/rosbit/go-qjs`
to install.
### Usage
Suppose there's a Javascript file named `a.js` like this:
```javascript
function add(a, b) {
return a+b
}
```
one can call the Javascript function `add()` in Go code like the following:
```go
package main
import (
"github.com/rosbit/go-qjs"
"fmt"
)
var add func(int, int)int
func main() {
ctx, err := qjs.NewQuickJS("/path/to/quickjs-exe/qjs", "a.js")
if err != nil {
fmt.Printf("%v\n", err)
return
}
defer ctx.Quit()
// method 1: bind JS function with a golang var
if err := ctx.BindFunc("add", &add); err != nil {
fmt.Printf("%v\n", err)
return
}
res := add(1, 2)
// method 2: call JS function using Call
res, err := ctx.CallFunc("add", 1, 2)
if err != nil {
fmt.Printf("%v\n", err)
return
}
fmt.Println("result is:", res)
}
```
### 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.