Refactor code to add context object and cookie function

This commit is contained in:
Max 2024-01-17 21:56:39 +08:00
parent bf31ad4bf1
commit 524a63d7af

View file

@ -164,6 +164,8 @@ func (r *Request) scriptGuardExec(c *gin.Context, name string, args []interface{
}
defer ctx.Close()
// Should be refector after the runtime refector
// Add the context object
ctx.WithFunction("SetSid", func(info *v8go.FunctionCallbackInfo) *v8go.Value {
if len(info.Args()) < 1 {
log.Error("SetSid no sid")
@ -242,6 +244,37 @@ func (r *Request) scriptGuardExec(c *gin.Context, name string, args []interface{
return nil
})
ctx.WithFunction("Cookie", func(info *v8go.FunctionCallbackInfo) *v8go.Value {
if len(info.Args()) < 1 {
log.Error("SetGlobal no global")
return v8go.Undefined(info.Context().Isolate())
}
name, err := bridge.GoValue(info.Args()[0], info.Context())
if err != nil {
log.Error("Cookie %s", err.Error())
return v8go.Undefined(info.Context().Isolate())
}
if name, ok := name.(string); ok {
value, err := c.Cookie(name)
if err != nil {
log.Error("Cookie %s", err.Error())
return v8go.Undefined(info.Context().Isolate())
}
jsValue, err := bridge.JsValue(info.Context(), value)
if err != nil {
log.Error("Cookie %s", err.Error())
return v8go.Undefined(info.Context().Isolate())
}
return jsValue
}
return v8go.Undefined(info.Context().Isolate())
})
// This function should be refector after the next version
ctx.WithFunction("SetCookie", func(info *v8go.FunctionCallbackInfo) *v8go.Value {