gojs

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2024 License: MIT Imports: 19 Imported by: 13

README

GoJS

The project is a low-code framework implemented in Go, with a low-code language written in JavaScript. The framework allows developers to create modules in Go that can be called from the JavaScript code. The entire project can be compiled into a standalone executable file, making it easy to deploy and use.

create a module

package apigo.cc/yourorg/modulename

import "apigo.cc/gojs"

func init() {
	defaultObject := Object{id: "o-00"}
	gojs.Register("apigo.cc/yourorg/modulename", gojs.Module{
		Object: map[string]any{
			"name":  "abc",
			"plus": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
				args := gojs.MakeArgs(&argsIn, vm).Check(2)
				return vm.ToValue(args.Int(0) + args.Int(1))
			},
			"getName": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
				args := gojs.MakeArgs(&argsIn, vm)
				this := args.ThisObj()
				return vm.ToValue(this.Str("name"))
			},
		},
	})
}

usage for inline js code

package main

import (
    "fmt"
	"apigo.cc/gojs"
    _ "apigo.cc/yourorg/modulename"
)

func main() {
	r, err := gojs.Run(`
import mod from 'apigo.cc/yourorg/modulename'

function main(args){
	return mod.getName()
}
	`, "test.js")

    fmt.Println(r, err, r == "abc")
}

usage for separate js file

main.go

package main

import (
    "fmt"
	"apigo.cc/gojs"
    _ "apigo.cc/yourorg/modulename"
)

func main() {
	r, err := gojs.RunFile("plus.js", 1, 2)
    fmt.Println(r, err, r == 3)
}

plus.js

import mod from 'apigo.cc/yourorg/modulename'

function main(args){
	return mod.plus(args[0], args[1])
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alias

func Alias(aliasName string, modName string)

func ExportForDev

func ExportForDev() string

func FindPath

func FindPath(vm *goja.Runtime, filename string) string

func GetFunc

func GetFunc(v goja.Value) goja.Callable

func GetLogger

func GetLogger(vm *goja.Runtime) *log.Logger

func Register

func Register(name string, mod Module)

func Run

func Run(code string, refFile string, args ...any) (any, error)

func RunFile

func RunFile(file string, args ...any) (any, error)

func RunProgram

func RunProgram(plg *Program, args ...any) (any, error)

Types

type Arr

type Arr struct {
	This      goja.Value
	Arguments []goja.Value

	Logger *log.Logger
	// contains filtered or unexported fields
}

func MakeArgs

func MakeArgs(args *goja.FunctionCall, vm *goja.Runtime) *Arr

func (*Arr) Any

func (args *Arr) Any(index int) any

func (*Arr) Arr

func (args *Arr) Arr(index int) *Arr

func (*Arr) Array

func (args *Arr) Array(startIndex int) []any

func (*Arr) Bool

func (args *Arr) Bool(index int) bool

func (*Arr) Bytes

func (args *Arr) Bytes(index int) []byte

func (*Arr) Check

func (args *Arr) Check(num int) *Arr

func (*Arr) Func

func (args *Arr) Func(index int) goja.Callable

func (*Arr) Int

func (args *Arr) Int(index int) int

func (*Arr) Int64

func (args *Arr) Int64(index int) int64

func (*Arr) Map

func (args *Arr) Map(index int) map[string]any

func (*Arr) Map2Array

func (args *Arr) Map2Array(index int) []any

func (*Arr) Map2StrArray

func (args *Arr) Map2StrArray(index int) []string

func (*Arr) Obj

func (args *Arr) Obj(index int) *Obj

func (*Arr) Object

func (args *Arr) Object(index int) *goja.Object

func (*Arr) Path

func (args *Arr) Path(index int) string

func (*Arr) Str

func (args *Arr) Str(index int) string

func (*Arr) StrArray

func (args *Arr) StrArray(startIndex int) []string

func (*Arr) ThisObj

func (args *Arr) ThisObj() *Obj

type LB

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

func NewLB

func NewLB(plg *Program, opt LBConfig, logger *log.Logger) *LB

func NewLBByCode

func NewLBByCode(code, refFile string, opt LBConfig, logger *log.Logger) *LB

func NewLBByFile

func NewLBByFile(file string, opt LBConfig, logger *log.Logger) *LB

func (*LB) Get

func (p *LB) Get() *Runtime

type LBConfig

type LBConfig struct {
	Num   uint
	Args  []any
	Debug bool
}

type Map

type Map = map[string]any

func MakeLogger

func MakeLogger(logger *log.Logger) Map

func MakeMap

func MakeMap(structObj any) Map

type Module

type Module struct {
	Object      Map
	ObjectMaker func(vm *goja.Runtime) Map
	TsCode      string
	Desc        string
	Example     string
}

type Obj

type Obj struct {
	This goja.Value

	Logger *log.Logger
	O      *goja.Object
	// contains filtered or unexported fields
}

func (*Obj) Any

func (obj *Obj) Any(name string) any

func (*Obj) Arr

func (obj *Obj) Arr(name string) *Arr

func (*Obj) Array

func (obj *Obj) Array(name string) []any

func (*Obj) Bool

func (obj *Obj) Bool(name string) bool

func (*Obj) Bytes

func (obj *Obj) Bytes(name string) []byte

func (*Obj) Func

func (obj *Obj) Func(name string) goja.Callable

func (*Obj) Get

func (obj *Obj) Get(name string) goja.Value

func (*Obj) Int

func (obj *Obj) Int(name string) int

func (*Obj) Int64

func (obj *Obj) Int64(name string) int64

func (*Obj) Map

func (obj *Obj) Map(name string) map[string]any

func (*Obj) Obj

func (obj *Obj) Obj(name string) *goja.Object

func (*Obj) Path

func (obj *Obj) Path(name string) string

func (*Obj) Str

func (obj *Obj) Str(name string) string

type Pool

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

func NewPool

func NewPool(plg *Program, opt PoolConfig, logger *log.Logger) *Pool

func NewPoolByCode

func NewPoolByCode(code, refFile string, opt PoolConfig, logger *log.Logger) *Pool

func NewPoolByFile

func NewPoolByFile(file string, opt PoolConfig, logger *log.Logger) *Pool

func (*Pool) Count

func (p *Pool) Count() (total, maxTotal, maxWaiting, createTimes uint)

func (*Pool) Get

func (p *Pool) Get() *Runtime

func (*Pool) Put

func (p *Pool) Put(rt *Runtime)

type PoolConfig

type PoolConfig struct {
	Min, Idle, Max uint
	Args           []any
	Debug          bool
}

type Program

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

func CompileCode

func CompileCode(code string, refFile string) (*Program, error)

func CompileFile

func CompileFile(file string) (*Program, error)

func CompileMain

func CompileMain(code string, refFile string) (*Program, error)

type Runtime

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

func New

func New() *Runtime

func (*Runtime) GetCallStack

func (rt *Runtime) GetCallStack() []string

func (*Runtime) GetGoData

func (rt *Runtime) GetGoData(name string) any

func (*Runtime) RunCode

func (rt *Runtime) RunCode(code string) (any, error)

func (*Runtime) RunFile

func (rt *Runtime) RunFile(file string) (any, error)

func (*Runtime) RunMain

func (rt *Runtime) RunMain(args ...any) (any, error)

func (*Runtime) RunProgram

func (rt *Runtime) RunProgram(prg *Program) (any, error)

func (*Runtime) RunVM

func (rt *Runtime) RunVM(callback func(vm *goja.Runtime) (any, error)) (any, error)

func (*Runtime) Set

func (rt *Runtime) Set(name string, value any) error

func (*Runtime) SetGlobal

func (rt *Runtime) SetGlobal(global Map) error

func (*Runtime) SetGoData

func (rt *Runtime) SetGoData(name string, value any)

func (*Runtime) SetModuleLoader

func (rt *Runtime) SetModuleLoader(fn func(filename string) string)

func (*Runtime) StartFromCode

func (rt *Runtime) StartFromCode(code, refFile string) error

func (*Runtime) StartFromFile

func (rt *Runtime) StartFromFile(file string) error

func (*Runtime) StartFromProgram

func (rt *Runtime) StartFromProgram(plg *Program) error

type WatchRunner

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

func WatchRun

func WatchRun(file string, extDirs, extTypes []string, args ...any) (*WatchRunner, error)

func (*WatchRunner) Stop

func (wr *WatchRunner) Stop()

func (*WatchRunner) WaitForKill

func (wr *WatchRunner) WaitForKill()

Directories

Path Synopsis
Package base64dec contains a universal base64 decoder that works on both the standard and url-safe variants, padded and raw.
Package base64dec contains a universal base64 decoder that works on both the standard and url-safe variants, padded and raw.
client module
console module
db module
file module
ast
Package ast declares types representing a JavaScript AST.
Package ast declares types representing a JavaScript AST.
file
Package file encapsulates the file abstractions used by the ast & parser.
Package file encapsulates the file abstractions used by the ast & parser.
ftoa
Package ftoa provides ECMAScript-compliant floating point number conversion to string.
Package ftoa provides ECMAScript-compliant floating point number conversion to string.
ftoa/internal/fast
Package fast contains code ported from V8 (https://github.com/v8/v8/blob/master/src/numbers/fast-dtoa.cc)
Package fast contains code ported from V8 (https://github.com/v8/v8/blob/master/src/numbers/fast-dtoa.cc)
parser
Package parser implements a parser for JavaScript.
Package parser implements a parser for JavaScript.
token
Package token defines constants representing the lexical tokens of JavaScript (ECMA5).
Package token defines constants representing the lexical tokens of JavaScript (ECMA5).
unistring
Package unistring contains an implementation of a hybrid ASCII/UTF-16 string.
Package unistring contains an implementation of a hybrid ASCII/UTF-16 string.
goja_nodejs
url
http module
log module
office module
redis module
service module
util module

Jump to

Keyboard shortcuts

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