ejs

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 6 Imported by: 0

README

Embeding JS for golang

go-ejs is a wrapper of goja. go-ejs is intended to embed js in golang applications easily.

Usage

The package is fully go-getable, So, just type

go get github.com/rosbit/go-ejs

to install.

package main

import "fmt"
import "github.com/rosbit/go-ejs"

func main() {
  ctx := ejs.NewContext()

  res, _ := ctx.Eval("1 + 2")
  fmt.Println("result is:", res)
}
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 "fmt"
import "github.com/rosbit/go-ejs"

var add func(int, int)int

func main() {
  ctx := ejs.NewContext()
  if err := ctx.LoadFile("a.js", nil); err != nil {
     fmt.Printf("%v\n", err)
     return
  }

  if err := ctx.BindFunc("add", &add); err != nil {
     fmt.Printf("%v\n", err)
     return
  }

  res := add(1, 2)
  fmt.Println("result is:", res)
}
JavaScript calls Go function

JavaScript calling Go function is also easy. Just bind a golang func with a var name with AddVar("funcname", function). There's the example:

package main

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

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

func main() {
  ctx := ejs.NewContext()

  ctx.AddVar("adder", adder)
  ctx.EvalFile("b.js", nil)  // b.js containing code calling "adder"
}

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

r = adder(1, 100)   // the function "adder" is implemented in Go
console.log(r)
add more than 1 variables and functions at one time
package main

import "github.com/rosbit/go-ejs"
import "fmt"

func adder(a1 float64, a2 float64) float64 {
    return a1 + a2
}

func main() {
  vars := map[string]interface{}{
     "adder": adder,    // to JavaScript built-in function
     "a": []int{1,2,3}, // to JavaScript array
  }

  ctx := js.NewContext()
  if err := ctx.LoadFile("file.js", vars); err != nil {
     fmt.Printf("%v\n", err)
     return
  }
  // or call ctx.AddVars(vars) to add variables.

  res, err := ctx.GetGlobals("global_var_name") // get the value of var global_var_name
  if err != nil {
     fmt.Printf("%v\n", err)
     return
  }
  fmt.Printf("res:", 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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitCache added in v0.0.7

func InitCache()

Types

type JsVm

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

func LoadFileFromCache added in v0.0.7

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

func LoadFileFromCacheWithEnvs added in v0.1.0

func LoadFileFromCacheWithEnvs(path string, envs, vars map[string]interface{}) (ctx *JsVm, existing bool, err error)

func NewContext added in v0.1.0

func NewContext() *JsVm

func NewVM

func NewVM(env map[string]interface{}) *JsVm

func (*JsVm) AddVar

func (js *JsVm) AddVar(name string, val interface{})

func (*JsVm) AddVars

func (js *JsVm) AddVars(vars map[string]interface{})

func (*JsVm) BeginSafeCall added in v0.2.0

func (js *JsVm) BeginSafeCall()

func (*JsVm) BindFunc

func (js *JsVm) BindFunc(funcName string, funcVarPtr interface{}) (err error)

@param funcVarPtr in format `var funcVar func(....) ...; funcVarPtr = &funcVar`

func (*JsVm) BindFuncs added in v0.0.8

func (js *JsVm) BindFuncs(funcName2FuncVarPtr map[string]interface{}) (err error)

func (*JsVm) CallFunc

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

func (*JsVm) EndSafeCall added in v0.2.0

func (js *JsVm) EndSafeCall()

func (*JsVm) Eval

func (js *JsVm) Eval(script string, vars ...map[string]interface{}) (res interface{}, err error)

func (*JsVm) EvalFile

func (js *JsVm) EvalFile(path string, vars ...map[string]interface{}) (res interface{}, err error)

func (*JsVm) GetGlobal

func (js *JsVm) GetGlobal(name string) (res interface{}, err error)

func (*JsVm) LoadFile

func (js *JsVm) LoadFile(path string, vars map[string]interface{}) (err error)

func (*JsVm) LoadScript

func (js *JsVm) LoadScript(script string, vars map[string]interface{}) (err error)

Jump to

Keyboard shortcuts

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