Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance JsValue and GoValue functions to support io.Writer, io.Reader… #236

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading