1.4.5.2
This commit is contained in:
51
EdgeNode/internal/js/template_object.go
Normal file
51
EdgeNode/internal/js/template_object.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
//go:build script
|
||||
// +build script
|
||||
|
||||
package js
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"rogchap.com/v8go"
|
||||
)
|
||||
|
||||
type ObjectTemplate struct {
|
||||
ctx *Context
|
||||
rawTemplate *v8go.ObjectTemplate
|
||||
}
|
||||
|
||||
func NewObjectTemplate(ctx *Context) *ObjectTemplate {
|
||||
return &ObjectTemplate{
|
||||
ctx: ctx,
|
||||
rawTemplate: v8go.NewObjectTemplate(ctx.Isolate().RawIsolate()),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *ObjectTemplate) NewInstance(ctx *Context) (*v8go.Object, error) {
|
||||
return this.rawTemplate.NewInstance(ctx.RawContext())
|
||||
}
|
||||
|
||||
func (this *ObjectTemplate) Set(name string, val any, attributes ...v8go.PropertyAttribute) error {
|
||||
return this.rawTemplate.Set(name, val, attributes...)
|
||||
}
|
||||
|
||||
func (this *ObjectTemplate) SetFunction(name string, callback FunctionCallback) error {
|
||||
return this.rawTemplate.Set(name, v8go.NewFunctionTemplate(this.ctx.Isolate().RawIsolate(), func(info *v8go.FunctionCallbackInfo) *v8go.Value {
|
||||
v, err := callback(NewFunctionArguments(this.ctx, info))
|
||||
if err != nil {
|
||||
remotelogs.Error("JS_FUNCTION_TEMPLATE", "call callback failed: "+err.Error())
|
||||
return nil
|
||||
}
|
||||
vv, err := this.ctx.NewValue(v)
|
||||
if err != nil {
|
||||
remotelogs.Error("JS_FUNCTION_TEMPLATE", "create new value failed: "+err.Error()+", value: "+fmt.Sprintf("%#v", v))
|
||||
return nil
|
||||
}
|
||||
return vv
|
||||
}))
|
||||
}
|
||||
|
||||
func (this *ObjectTemplate) SetInternalFieldCount(count int) {
|
||||
this.rawTemplate.SetInternalFieldCount(uint32(count))
|
||||
}
|
||||
Reference in New Issue
Block a user