quickjs-go icon indicating copy to clipboard operation
quickjs-go copied to clipboard

使用Invoke调用函数,时成功时失败.

Open Mr-TianLang opened this issue 6 months ago • 5 comments

我使用Eval声明了一个方法,然后通过ctx.Invoke 调用它,时而成功,时而失败. reqRT, err := DefaultContext.Eval((request) => { + l.config.ReqJsScript + return request;}) defer reqRT.Free() if err != nil || DefaultContext.Exception() != nil { log.Error(err) log.Error(DefaultContext.Exception()) } reqVal := DefaultContext.Object() defer reqVal.Free() headersVal := DefaultContext.Map() defer headersVal.Free() for key, _ := range ctx.Request().Header().Headers() { headersVal.Put(DefaultContext.String(key), DefaultContext.String(ctx.Request().Header().GetHeader(key))) } reqVal.Set("headers", headersVal.ToValue()) RawBody, _ := ctx.Request().Body().RawBody() reqVal.Set("body", DefaultContext.String(string(RawBody))) reqVal.Set("uri", DefaultContext.String(ctx.Request().URI().Path()))

request := DefaultContext.Invoke(reqRT, DefaultContext.Null(), reqVal) defer request.Free() log.Info(request.PropertyNames()) log.Info(request.IsObject())

request.Get("headers").ToMap().ForEach(func(key, value quickjs.Value) { ctx.Proxy().Header().SetHeader(key.String(), value.String()) }) contentType, _, _ := mime.ParseMediaType(ctx.Proxy().Body().ContentType()) ctx.Proxy().Body().SetRaw(contentType, []byte(request.Get("body").String())) ctx.Proxy().URI().SetRawQuery(request.Get("uri").String()) 失败的时候会在request.Get 时,报空指针异常. 这是什么原因

其中DefaultContext 是全局变量

Mr-TianLang avatar Aug 06 '24 09:08 Mr-TianLang