Files
waf-platform/EdgeNode/internal/js/template_object.go
2026-02-04 20:27:13 +08:00

52 lines
1.5 KiB
Go

// 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))
}