v8go

Execute JavaScript from Go

Github星跟踪图

Execute JavaScript from Go

Go Report Card
GoDoc
Build Status

Usage

import "rogchap.com/v8go"

Running a script

ctx, _ := v8go.NewContext(nil) // creates a new V8 context with a new Isolate aka VM
ctx.RunScript("const add = (a, b) => a + b", "math.js") // executes a script on the global context
ctx.RunScript("const result = add(3, 4)", "main.js") // any functions previously added to the context can be called
val, _ := ctx.RunScript("result", "value.js") // return a value in JavaScript back to Go
fmt.Printf("addition result: %s", val)

One VM, many contexts

vm, _ := v8go.NewIsolate() // creates a new JavaScript VM
ctx1, _ := v8go.NewContext(vm) // new context within the VM
ctx1.RunScript("const multiply = (a, b) => a * b", "math.js")

ctx2, _ := v8go.NewContext(vm) // another context on the same VM
if _, err := ctx2.RunScript("multiply(3, 4)", "main.js"); err != nil {
  // this will error as multiply is not defined in this context
}

Javascript errors

val, err := ctx.RunScript(src, filename)
if err != nil {
  err = err.(v8go.JSError) // JavaScript errors will be returned as the JSError struct
  fmt.Println(err.Message) // the message of the exception thrown
  fmt.Println(err.Location) // the filename, line number and the column where the error occured
  fmt.Println(err.StackTrace) // the full stack trace of the error, if available

  fmt.Printf("javascript error: %v", err) // will format the standard error message
  fmt.Printf("javascript stack trace: %+v", err) // will format the full error stack trace
}

Terminate long running scripts


vals := make(chan *v8go.Value, 1)
errs := make(chan error, 1)

go func() {
    val, err := ctx.RunScript(script, "forever.js") // exec a long running script
    if err != nil {
        errs <- err
        return
    }
    vals <- val
}()

select {
case val := <- vals:
    // sucess
case err := <- errs:
    // javascript error
case <- time.After(200 * time.Milliseconds):
    vm, _ := ctx.Isolate() // get the Isolate from the context
    vm.TerminateExecution() // terminate the execution 
    err := <- errs // will get a termination error back from the running script
}

Documentation

GoDoc: https://godoc.org/rogchap.com/v8go

V8 dependency

V8 version: 8.0.426.15

In order to make v8go usable as a standard Go package, prebuilt static libraries of V8
are included for Linux and OSX ie. you should not require to build V8 yourself.

V8 requires 64-bit, therefore will not work on 32-bit systems.


V8 Gopher image based on original artwork from the amazing Renee French.

主要指标

概览
名称与所有者rogchap/v8go
主编程语言Go
编程语言Go (语言数: 3)
平台
许可证BSD 3-Clause "New" or "Revised" License
所有者活动
创建于2019-08-28 07:15:05
推送于2024-08-02 10:56:01
最后一次提交2023-04-10 10:09:13
发布数11
最新版本名称v0.9.0 (发布于 )
第一版名称v0.0.1 (发布于 )
用户参与
星数3.3k
关注者数34
派生数228
提交数162
已启用问题?
问题数145
打开的问题数66
拉请求数153
打开的拉请求数33
关闭的拉请求数85
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?