quickjs

package module
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2022 License: MIT Imports: 11 Imported by: 0

README

go-quickjs, make quickjs be embedded easily

QuickJS is a small and embeddable Javascript engine written by Fabrice Bellard.

go-quickjs is a package wrapping quickjs and making it a pragmatic embeddable language. With some helper functions provided by go-quickjs, calling Golang functions from Javascript, or calling Javascript functions from Golang are both very simple. So, with the help of go-quickjs, quickjs can be embedded in Golang application easily.

Install

To install go-quickjs, following the steps (under Linux/macos):

  1. make sure gcc, make, wget are ready
  2. run git clone github.com/rosbit/go-quickjs to clone the source
  3. cd go-quickjs to change the directory
  4. run make to build the libquickjs.a
  5. optionally run go build.
Usage
1. Evaluates expressions
package main

import (
  "github.com/rosbit/go-quickjs"
  "fmt"
)

func main() {
  ctx, err := quickjs.NewContext()
  if err != nil {
    fmt.Printf("%v\n", err)
    return
  }
  defer ctx.Free() // not necessary, ctx will be released by GC.

  res, _ := ctx.Eval("a + b", map[string]interface{}{
     "a": 10,
     "b": 1,
  })
  fmt.Println("result is:", res)
}
2. Go calls Javascript function

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

function add(a, b) {
    return a+b
}

one can call the Javascript function add() in Go code like the following:

package main

import (
  "github.com/rosbit/go-quickjs"
  "fmt"
)

var add func(int, int)int

func main() {
  ctx, err := quickjs.NewContext()
  if err != nil {
     fmt.Printf("%v\n", err)
     return
  }
  defer ctx.Free()

  if _, err := ctx.EvalFile("a.js", nil); err != nil {
     fmt.Printf("%v\n", err)
     return
  }

  // 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)
}
3. Javascript calls Go function

Javascript calling Go function is also easy. In the Go code, make a Golang function as Javascript global function by calling EvalFile by registering. There's the example:

package main

import "github.com/rosbit/go-quickjs"

// function to be called by Javascript
func adder(a1 float64, a2 float64) float64 {
    return a1 + a2
}

func main() {
  ctx, err := quickjs.NewContext()
  if err != nil {
      fmt.Printf("%v\n", err)
      return
  }
  defer ctx.Free()

  if _, err := ctx.EvalFile("b.js", map[string]interface{}{
      "adder": adder,
  })  // b.js containing code calling "adder"
}

In Javascript code, one can call the registered function directly. There's the example b.js.

r = adder(1, 100)   // the function "adder" is implemented in Go
console.log(r)
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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitCache added in v0.1.3

func InitCache()

Types

type FnFreeKey added in v0.1.6

type FnFreeKey func(key, val interface{}) // func called when val is release

type JsContext

type JsContext struct {
	// contains filtered or unexported fields
}

func LoadFileFromCache added in v0.1.3

func LoadFileFromCache(path string, vars map[string]interface{}) (ctx *JsContext, err error)

func NewContext

func NewContext() (*JsContext, error)

func (*JsContext) BindFunc

func (ctx *JsContext) BindFunc(funcName string, funcVarPtr interface{}) (err error)

bind a var of golang func with a JS function name, so calling JS function is just calling the related golang func. @param funcVarPtr in format `var funcVar func(....) ...; funcVarPtr = &funcVar`

func (*JsContext) BindFuncs

func (ctx *JsContext) BindFuncs(funcName2FuncVarPtr map[string]interface{}) (err error)

func (*JsContext) CallFunc added in v0.1.4

func (ctx *JsContext) CallFunc(funcName string, args ...interface{}) (res interface{}, err error)

func (*JsContext) Eval

func (ctx *JsContext) Eval(script string, env map[string]interface{}) (res interface{}, err error)

func (*JsContext) EvalFile

func (ctx *JsContext) EvalFile(scriptFile string, env map[string]interface{}) (res interface{}, err error)

func (*JsContext) Free

func (ctx *JsContext) Free()

func (*JsContext) GetGlobal

func (ctx *JsContext) GetGlobal(name string) (res interface{}, err error)

type V2KPool added in v0.1.2

type V2KPool struct {
	// contains filtered or unexported fields
}

func NewV2KPool added in v0.1.2

func NewV2KPool() *V2KPool

func (*V2KPool) GetVal added in v0.1.2

func (p *V2KPool) GetVal(key interface{}) interface{}

func (*V2KPool) Quit added in v0.1.2

func (p *V2KPool) Quit()

func (*V2KPool) RemoveKey added in v0.1.2

func (p *V2KPool) RemoveKey(key interface{})

func (*V2KPool) SetKV added in v0.1.6

func (p *V2KPool) SetKV(key, val interface{}, freeKey FnFreeKey)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL