Skip to content

Commit

Permalink
Merge pull request #172 from trheyi/main
Browse files Browse the repository at this point in the history
[add] Refactor Runtime Standard Mode
  • Loading branch information
trheyi authored Dec 26, 2023
2 parents 288ca9a + e06630d commit a926b71
Show file tree
Hide file tree
Showing 31 changed files with 1,290 additions and 1,469 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ jobs:
- name: Checkout V8Go
uses: actions/checkout@v3
with:
repository: rogchap/v8go
# ref: aac4923ed74083e58ae7b42b1acb0fa0d11a26cb
repository: yaoapp/v8go
lfs: true
path: v8go

- name: Checkout Demo WMS
Expand Down
14 changes: 8 additions & 6 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ jobs:
path: gou-dev-app

- name: Checkout V8Go
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
repository: rogchap/v8go
# ref: aac4923ed74083e58ae7b42b1acb0fa0d11a26cb
repository: yaoapp/v8go
lfs: true
path: v8go

- name: Checkout Demo WMS
Expand Down Expand Up @@ -158,16 +158,18 @@ jobs:
chmod 600 $HOME/.yao/github_token
ls -l $HOME/.yao/github_token
- name: Run Benchmark
run: |
make bench
- name: Run Test
run: |
make vet
make fmt-check
make misspell-check
make test
- name: Run Benchmark
run: |
make bench

- name: Codecov Report
uses: codecov/codecov-action@v3
Expand Down
19 changes: 9 additions & 10 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,31 +217,27 @@ func (path Path) runStreamScript(ctx context.Context, c *gin.Context, getArgs fu
}
defer v8ctx.Close()

// make a new bridge function
ssEventT := v8go.NewFunctionTemplate(v8ctx.Isolate(), func(info *v8go.FunctionCallbackInfo) *v8go.Value {
v8ctx.WithFunction("ssEvent", func(info *v8go.FunctionCallbackInfo) *v8go.Value {
args := info.Args()
if len(args) != 2 {
return v8go.Null(v8ctx.Isolate())
return v8go.Null(info.Context().Isolate())
}

name := args[0].String()
message, err := bridge.GoValue(args[1], info.Context())
if err != nil {
return v8go.Null(v8ctx.Isolate())
return v8go.Null(info.Context().Isolate())
}

onEvent(name, message)
return v8go.Null(v8ctx.Isolate())
return v8go.Null(info.Context().Isolate())
})

cancelT := v8go.NewFunctionTemplate(v8ctx.Isolate(), func(info *v8go.FunctionCallbackInfo) *v8go.Value {
v8ctx.WithFunction("cancel", func(info *v8go.FunctionCallbackInfo) *v8go.Value {
onCancel()
return v8go.Null(v8ctx.Isolate())
return v8go.Null(info.Context().Isolate())
})

v8ctx.Global().Set("ssEvent", ssEventT.GetFunction(v8ctx.Context))
v8ctx.Global().Set("cancel", cancelT.GetFunction(v8ctx.Context))

args := getArgs(c)
_, err = v8ctx.CallWith(ctx, method, args...)
if err != nil {
Expand All @@ -251,12 +247,14 @@ func (path Path) runStreamScript(ctx context.Context, c *gin.Context, getArgs fu
}

func (path Path) execProcess(ctx context.Context, chRes chan<- interface{}, c *gin.Context, getArgs func(c *gin.Context) []interface{}) {

var args []interface{} = getArgs(c)
var process, err = process.Of(path.Process, args...)
if err != nil {
log.Error("[Path] %s %s", path.Path, err.Error())
chRes <- err
}
defer process.Dispose()

if sid, has := c.Get("__sid"); has { // 设定会话ID
if sid, ok := sid.(string); ok {
Expand Down Expand Up @@ -284,6 +282,7 @@ func (path Path) execProcess(ctx context.Context, chRes chan<- interface{}, c *g
func (path Path) runProcess(ctx context.Context, c *gin.Context, getArgs func(c *gin.Context) []interface{}) interface{} {
var args []interface{} = getArgs(c)
var process = process.New(path.Process, args...)
defer process.Dispose()

if sid, has := c.Get("__sid"); has { // 设定会话ID
if sid, ok := sid.(string); ok {
Expand Down
276 changes: 19 additions & 257 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ func (process *Process) WithContext(ctx context.Context) *Process {
return process
}

// WithRuntime set the runtime interface
func (process *Process) WithRuntime(runtime Runtime) *Process {
process.Runtime = runtime
return process
}

// Dispose the process after run success
func (process *Process) Dispose() {
if process.Runtime != nil {
process.Runtime.Dispose()
}

process.Args = nil
process.Global = nil
process.Context = nil
process.Runtime = nil
process = nil
}

// handler get the process handler
func (process *Process) handler() (Handler, error) {
if hander, has := Handlers[process.Handler]; has && hander != nil {
Expand Down Expand Up @@ -165,260 +184,3 @@ func (process *Process) make() error {

return nil
}

// extraProcess 解析执行方法 name = "models.user.Find", name = "plugins.user.Login"
// return type=models, name=login, class=user
// @下一版优化这个函数
// func (process *Process) make() error {
// namer := strings.Split(process.Name, ".")
// last := len(namer) - 1

// if _, has := whitelist[namer[0]]; last < 2 && !has {
// exception.New("Process:%s format error", 400, process.Name).Throw()
// }

// process.Type = strings.ToLower(namer[0])
// if last > 1 {
// process.Class = strings.ToLower(strings.Join(namer[1:last], "."))
// process.Method = strings.ToLower(namer[last])
// } else {
// process.Class = strings.ToLower(namer[1])
// process.Method = ""
// }

// // Handler groups
// if handlers, has := HandlerGroups[process.Type]; has {
// method := process.Method
// if method == "" {
// method = process.Class
// }

// process.Name = strings.ToLower(process.Name)
// handler, has := handlers[method]
// if !has {
// exception.New("%s: %s %s does not exist", 404, process.Type, process.Name, process.Method).Throw()
// }
// process.Handler = handler
// return
// }

// switch process.Type {

// case "plugins":
// process.Name = strings.ToLower(process.Name)
// process.Handler = processPlugin
// return

// case "flows":
// process.Name = strings.ToLower(process.Name)
// process.Handler = processFlow
// return

// case "scripts":
// process.Class = strings.ToLower(strings.Join(namer[1:last], "."))
// process.Method = namer[last]
// process.Handler = processScript
// return

// case "session":
// process.Method = strings.ToLower(namer[last])
// process.Handler = processSession
// return

// case "stores":
// process.Name = strings.ToLower(process.Name)
// handler, has := StoreHandlers[process.Method]
// if !has {
// exception.New("Store: %s %s does not exist", 404, process.Name, process.Method).Throw()
// }
// process.Handler = handler
// return

// case "widgets":

// if widgetHanlders, has := WidgetCustomHandlers[strings.ToLower(process.Class)]; has {
// if handler, has := widgetHanlders[strings.ToLower(process.Method)]; has {
// process.Name = strings.ToLower(process.Name)
// process.Handler = handler
// return
// }
// }
// process.Name = strings.ToLower(process.Name)
// handler, has := WidgetHandlers[strings.ToLower(process.Method)]
// if !has {
// exception.New("Widget: %s %s does not exist", 404, process.Name, process.Method).Throw()
// }
// process.Handler = handler
// return

// case "schemas":
// process.Name = strings.ToLower(process.Name)
// handler, has := SchemaHandlers[process.Method]
// if !has {
// exception.New("Schema: %s %s does not exist", 404, process.Name, process.Method).Throw()
// }
// process.Handler = handler
// return

// case "tasks":
// process.Name = strings.ToLower(process.Name)
// handler, has := TaskHandlers[process.Method]
// if !has {
// exception.New("Task: %s %s does not exist", 404, process.Name, process.Method).Throw()
// }
// process.Handler = handler
// return

// case "schedules":
// process.Name = strings.ToLower(process.Name)
// handler, has := ScheduleHandlers[process.Method]
// if !has {
// exception.New("Schedule: %s %s does not exist", 404, process.Name, process.Method).Throw()
// }
// process.Handler = handler
// return

// case "models":
// process.Name = strings.ToLower(process.Name)
// handler, has := ModelHandlers[process.Method]
// if !has {
// exception.New("Model: %s %s does not exist", 404, process.Name, process.Method).Throw()
// }
// process.Handler = handler
// return

// default:
// if handler, has := Handlers[strings.ToLower(process.Name)]; has {
// process.Name = strings.ToLower(process.Name)
// process.Handler = handler
// return
// } else if handler, has := Handlers[process.Type]; has {
// process.Name = strings.ToLower(process.Name)
// process.Handler = handler
// return
// }
// }

// exception.New("%s does not found", 404, process.Name).Throw()
// }

// processPlugin 运行插件中的方法
// func processPlugin(process *Process) interface{} {
// plugin := SelectPluginModel(process.Class)
// res, err := plugin.Exec(process.Method, process.Args...)
// if err != nil {
// exception.Err(err, 500).Throw()
// }
// return res.MustValue()
// }

// // processFlow 运行工作流
// func processFlow(process *Process) interface{} {
// name := strings.TrimPrefix(process.Name, "flows.")
// flow := SelectFlow(name).WithGlobal(process.Global).WithSID(process.Sid)
// return flow.Exec(process.Args...)
// }

// // processScript 运行脚本中定义的处理器
// func processScript(process *Process) interface{} {
// res, err := Yao.New(process.Class, process.Method).
// WithGlobal(process.Global).
// WithSid(process.Sid).
// Call(process.Args...)

// if err != nil {
// message := err.Error()

// // JS Exception
// if strings.HasPrefix(message, "Exception|") {
// message = strings.Replace(message, "Exception|", "", -1)
// values := strings.Split(message, ":")
// if len(values) == 2 {
// code := 500
// if v, err := strconv.Atoi(values[0]); err == nil {
// code = v
// }
// message = strings.TrimSpace(values[1])
// exception.New(message, code).Throw()
// }
// }

// // Other
// code := 500
// values := strings.Split(message, "|")
// if len(values) == 2 {
// if v, err := strconv.Atoi(values[0]); err == nil {
// code = v
// }
// message = values[0]
// }

// exception.New(message, code).Throw()
// }
// return res
// }

// processSession
// **WARN** refactor in the next version
// func processSession(process *Process) interface{} {

// if process.Method == "start" {
// process.Sid = session.ID()
// return process.Sid
// }

// ss := session.Global()

// if process.Sid != "" {
// ss = ss.ID(process.Sid)
// }

// switch process.Method {

// case "id":
// return process.Sid

// case "get":
// process.ValidateArgNums(1)
// if process.NumOfArgs() == 2 {
// ss = session.Global().ID(process.ArgsString(1))
// return ss.MustGet(process.ArgsString(0))
// }
// return ss.MustGet(process.ArgsString(0))

// case "set":
// process.ValidateArgNums(2)
// if process.NumOfArgs() == 3 {
// ss.MustSetWithEx(process.ArgsString(0), process.Args[1], time.Duration(process.ArgsInt(2))*time.Second)
// return nil

// } else if process.NumOfArgs() == 4 {
// ss = session.Global().ID(process.ArgsString(3))
// ss.MustSetWithEx(process.ArgsString(0), process.Args[1], time.Duration(process.ArgsInt(2))*time.Second)
// }

// ss.MustSet(process.ArgsString(0), process.Args[1])
// return nil

// case "setmany":
// process.ValidateArgNums(1)
// if process.NumOfArgs() == 2 {
// ss.MustSetManyWithEx(process.ArgsMap(0), time.Duration(process.ArgsInt(1))*time.Second)
// return nil

// } else if process.NumOfArgs() == 3 {
// ss = session.Global().ID(process.ArgsString(2))
// ss.MustSetManyWithEx(process.ArgsMap(0), time.Duration(process.ArgsInt(1))*time.Second)
// return nil
// }
// ss.MustSetMany(process.ArgsMap(0))
// return nil
// case "dump":
// if process.NumOfArgs() == 1 {
// ss = session.Global().ID(process.ArgsString(0))
// return ss.MustDump()
// }
// return ss.MustDump()
// }
// return nil
// }
Loading

0 comments on commit a926b71

Please sign in to comment.