Skip to content

Commit

Permalink
Merge pull request #236 from trheyi/main
Browse files Browse the repository at this point in the history
Enhance JsValue and GoValue functions to support io.Writer, io.Reader…
  • Loading branch information
trheyi authored Nov 7, 2024
2 parents c40a4cb + f810f2e commit 6396abb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 13 additions & 0 deletions runtime/v8/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package bridge

import (
"fmt"
"io"
"math/big"

"github.com/gin-gonic/gin"
jsoniter "github.com/json-iterator/go"
"github.com/yaoapp/kun/exception"
"rogchap.com/v8go"
Expand Down Expand Up @@ -155,6 +157,12 @@ func GoValues(jsValues []*v8go.Value, ctx *v8go.Context) ([]interface{}, error)
// * | ✅ | struct | object |
// * | ✅ | bridge.PromiseT | object(Promise) |
// * | ✅ | bridge.FunctionT | function |
// * | ✅ | io.Writer | object(external) |
// * | ✅ | io.Reader | object(external) |
// * | ✅ | io.ReadCloser | object(external) |
// * | ✅ | io.WriteCloser | object(external) |
// * | ✅ | *gin.Context | object(external) |
// * | ✅ | *gin.ResponseWriter | object(external) |
// * |-----------------------------------------------------------
func JsValue(ctx *v8go.Context, value interface{}) (*v8go.Value, error) {

Expand Down Expand Up @@ -219,6 +227,10 @@ func JsValue(ctx *v8go.Context, value interface{}) (*v8go.Value, error) {
case UndefinedT:
return v8go.Undefined(ctx.Isolate()), nil

// For io stream
case io.Writer, io.Reader, io.ReadCloser, io.WriteCloser, gin.ResponseWriter, *gin.Context:
return v8go.NewExternal(ctx.Isolate(), v)

default:
return jsValueParse(ctx, v)
}
Expand Down Expand Up @@ -258,6 +270,7 @@ func jsValueParse(ctx *v8go.Context, value interface{}) (*v8go.Value, error) {
// * | ✅ | array | []interface{} |
// * | ✅ | object(Promise) | bridge.PromiseT |
// * | ✅ | function | bridge.FunctionT |
// * | ✅ | object(external) | interface{} |
// * |-----------------------------------------------------------
func GoValue(value *v8go.Value, ctx *v8go.Context) (interface{}, error) {

Expand Down
9 changes: 6 additions & 3 deletions runtime/v8/functions/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ func exec(info *v8go.FunctionCallbackInfo) *v8go.Value {

goArgs := []interface{}{}
if len(jsArgs) > 1 {
goArgs, err = bridge.GoValues(jsArgs[1:], info.Context())
if err != nil {
return bridge.JsException(info.Context(), err)
for _, arg := range jsArgs[1:] {
v, err := bridge.GoValue(arg, info.Context())
if err != nil {
return bridge.JsException(info.Context(), err)
}
goArgs = append(goArgs, v)
}
}

Expand Down

0 comments on commit 6396abb

Please sign in to comment.