energy/cef/cef-v8-value-bind-object.go

321 lines
10 KiB
Go
Raw Normal View History

2022-10-04 13:21:05 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
2022-10-04 13:21:05 +08:00
//
//----------------------------------------
2023-02-20 14:42:17 +08:00
// V8 JSValue 对象类型实现
2022-10-04 13:21:05 +08:00
package cef
import (
"bytes"
"fmt"
"github.com/energye/energy/common"
"github.com/energye/energy/common/imports"
2022-10-04 22:34:57 +08:00
. "github.com/energye/energy/consts"
"github.com/energye/energy/logger"
2022-10-04 13:21:05 +08:00
"github.com/energye/golcl/lcl/api"
"reflect"
"strconv"
"unsafe"
)
2023-02-22 12:31:35 +08:00
var objectTI = &objectTypeInfo{ObjectInfos: make(map[string]*objectInfo), FieldInfos: make(map[string]*fieldInfo), FunctionInfos: make(map[string]*functionInfo)}
2022-10-04 13:21:05 +08:00
// 函数详情 1.参数个数 2.每个参数类型 3.返回参数类型
2022-10-04 13:21:05 +08:00
type funcInfo struct {
2023-02-22 12:31:35 +08:00
InNum int32 `json:"inNum"` //入参个数
InParam []*VT `json:"inParam"` //入参类型
OutNum int32 `json:"outNum"` //出参个数
OutParam []*VT `json:"outParam"` //出参类型
OutParamErrIdx int32 `json:"outParamErrIdx"` //出参错误位置
OutParamIdx int32 `json:"outParamIdx"` //出参位置
FnType FN_TYPE `json:"fnType"` //函数类型, 直接定义函数或对象函数
2022-10-04 13:21:05 +08:00
}
2023-02-22 12:31:35 +08:00
// VT 值类型
type VT struct {
2022-10-04 13:21:05 +08:00
Jsv V8_JS_VALUE_TYPE `json:"jsv"`
Gov GO_VALUE_TYPE `json:"gov"`
}
2023-02-22 12:31:35 +08:00
// objectTypeInfo 结构类型描述,结构的字段描述和方法函数描述
type objectTypeInfo struct {
ObjectInfos map[string]*objectInfo //分析后有关系的结构描述
FieldInfos map[string]*fieldInfo `json:"fieldInfos"` //字段描述 key=字段名 value=字段描述
FunctionInfos map[string]*functionInfo `json:"functionInfos"` //函数描述 key=函数名 value=函数描述
2022-10-04 13:21:05 +08:00
}
2023-02-22 12:31:35 +08:00
// objectInfo 分析后的 结构描述
type objectInfo struct {
Instance uintptr `json:"instance"` //对象地址
ParentInstance uintptr `json:"parentInstance"` //父对象地址
Parent *objectInfo `json:"-"` //父对象结构描述
Ptr unsafe.Pointer `json:"-"` //对象指针
ObjName string `json:"objName"` //对象名称
FullObjName string `json:"fullObjName"` //对象全路径名称: structName.FieldName.XXX
FieldInfos map[string]*fieldInfo `json:"fieldInfos"` //字段描述 key=字段名 value=字段描述
FunctionInfos map[string]*functionInfo `json:"functionInfos"` //函数描述 key=函数名 value=函数描述
SubObjectInfo map[string]*objectInfo `json:"subObjectInfo"` //子对象描述
2022-10-04 13:21:05 +08:00
}
2023-02-22 12:31:35 +08:00
// fieldInfo 字段描述
type fieldInfo struct {
2022-10-04 13:21:05 +08:00
EventId uintptr `json:"event_id"`
2023-02-22 12:31:35 +08:00
ValueType *VT `json:"valueType"` //字段类型go 和 js
2022-10-04 13:21:05 +08:00
FieldValue *reflect.Value `json:"-"` //用于字段值修改和获取
}
2023-02-22 12:31:35 +08:00
// functionInfo 结构的函数描述
type functionInfo struct {
2022-10-04 13:21:05 +08:00
EventId uintptr `json:"event_id"`
*funcInfo
Method reflect.Value `json:"-"`
}
type cefObject struct {
Id uintptr
ParentId uintptr
Name uintptr //string
FullObjName uintptr //string
FieldLen uintptr
Fields uintptr //array
FuncLen uintptr
Funcs uintptr //array
}
2023-02-22 12:31:35 +08:00
func (m *VT) ToValueTypeString() string {
govs := common.FuncParamGoTypeStr(m.Gov)
jsvs := common.FuncParamJsTypeStr(m.Jsv)
2022-10-04 13:21:05 +08:00
return fmt.Sprintf("GO=%s,JS:=%s", govs, jsvs)
}
2023-02-20 14:42:17 +08:00
// IsGoIntAuto 判断Go 所有 int 类型
2023-02-22 12:31:35 +08:00
func (m *VT) IsGoIntAuto() bool {
2022-10-04 13:21:05 +08:00
switch m.Gov {
case GO_VALUE_INT, GO_VALUE_INT8, GO_VALUE_INT16, GO_VALUE_INT32, GO_VALUE_INT64, GO_VALUE_UINT, GO_VALUE_UINT8, GO_VALUE_UINT16, GO_VALUE_UINT32, GO_VALUE_UINT64:
return true
}
return false
}
2023-02-20 14:42:17 +08:00
// IsGoFloatAuto 判断Go 所有 float 类型
2023-02-22 12:31:35 +08:00
func (m *VT) IsGoFloatAuto() bool {
2022-10-04 13:21:05 +08:00
switch m.Gov {
case GO_VALUE_FLOAT32, GO_VALUE_FLOAT64:
return true
}
return false
}
2023-02-22 08:47:48 +08:00
// bindObject ICefV8Context
2023-02-20 14:42:17 +08:00
// 对应Go不支持字段的类型修改包括对象类型,不支持删除和增加字段变更,支持字段值修改。和获取。
2022-10-04 13:21:05 +08:00
func bindObject(objects ...interface{}) {
for i := 0; i < len(objects); i++ {
object := objects[i]
typ := reflect.TypeOf(object)
if typ.Kind() == reflect.Ptr {
var value = reflect.ValueOf(object)
var objTyp = reflect.ValueOf(object).Type().Elem()
2023-02-22 12:31:35 +08:00
objectTI.ObjectInfos[objTyp.Name()] = &objectInfo{FieldInfos: make(map[string]*fieldInfo), SubObjectInfo: make(map[string]*objectInfo)}
objectTI.ObjectInfos[objTyp.Name()].analysisObjectField(objTyp, typ, value)
2022-10-04 13:21:05 +08:00
} else {
2022-10-05 20:43:25 +08:00
logger.Error("结构对象非指针类型:", typ.Name())
2022-10-04 13:21:05 +08:00
}
}
2023-02-22 12:31:35 +08:00
objectTI._objectToCefObject()
2022-10-04 13:21:05 +08:00
}
2023-02-20 14:42:17 +08:00
// _objectToCefObject
// 对象字段和函数统计
// 对象转换 go to cef
2023-02-22 12:31:35 +08:00
func (m *objectTypeInfo) _objectToCefObject() {
for _, info := range m.ObjectInfos {
2022-10-04 13:21:05 +08:00
m._subInfoToCefObject(info)
}
}
2023-02-20 14:42:17 +08:00
2023-02-22 12:31:35 +08:00
func (m *objectTypeInfo) _subInfoToCefObject(info *objectInfo) {
2022-10-04 13:21:05 +08:00
m._infoTo(info)
2023-02-22 12:31:35 +08:00
if len(info.SubObjectInfo) > 0 {
for _, subInfo := range info.SubObjectInfo {
2022-10-04 13:21:05 +08:00
m._subInfoToCefObject(subInfo)
}
}
}
2023-02-22 12:31:35 +08:00
func (m *objectTypeInfo) _infoTo(info *objectInfo) {
2022-10-04 13:21:05 +08:00
var (
2023-02-22 12:31:35 +08:00
fieldLen = len(info.FieldInfos)
2022-10-04 13:21:05 +08:00
fieldPtr uintptr
2023-02-22 12:31:35 +08:00
funcLen = len(info.FunctionInfos)
2022-10-04 13:21:05 +08:00
funcPtr uintptr
cofs []*valueBindInfo
fns []*valueBindInfo
i = 0
)
if fieldLen > 0 {
//字段
cofs = make([]*valueBindInfo, fieldLen, fieldLen)
2023-02-22 12:31:35 +08:00
for fieldName, fi := range info.FieldInfos {
2022-10-04 13:21:05 +08:00
cofs[i] = &valueBindInfo{
2022-12-03 21:56:51 +08:00
Name: api.PascalStr(fieldName),
2022-10-04 13:21:05 +08:00
EventId: fi.EventId,
BindType: uintptr(fi.ValueType.Jsv),
}
i++
m.createObjectFieldVariable(info.FullObjName, fieldName, fi)
}
fieldPtr = uintptr(unsafe.Pointer(&cofs[0]))
i = 0
}
if funcLen > 0 {
fns = make([]*valueBindInfo, funcLen, funcLen)
2023-02-22 12:31:35 +08:00
for fnName, fn := range info.FunctionInfos {
2022-10-04 13:21:05 +08:00
var inParamBuf bytes.Buffer
var outParamBuf bytes.Buffer
for j, inParamType := range fn.InParam {
if j > 0 {
inParamBuf.WriteString(",")
}
inParamBuf.WriteString(strconv.Itoa(int(inParamType.Jsv)))
}
for j, outParanType := range fn.OutParam {
if j > 0 {
outParamBuf.WriteString(",")
}
outParamBuf.WriteString(strconv.Itoa(int(outParanType.Jsv)))
}
fns[i] = &valueBindInfo{
2022-12-03 21:56:51 +08:00
Name: api.PascalStr(fnName),
2022-10-04 13:21:05 +08:00
EventId: fn.EventId,
BindType: uintptr(V8_VALUE_FUNCTION),
FnInNum: uintptr(fn.InNum),
2022-12-03 21:56:51 +08:00
FnInParamType: api.PascalStr(inParamBuf.String()),
2022-10-04 13:21:05 +08:00
FnOutNum: uintptr(fn.OutNum),
2022-12-03 21:56:51 +08:00
FnOutParamType: api.PascalStr(outParamBuf.String()),
2022-10-04 13:21:05 +08:00
}
i++
m.createObjectFuncVariable(info.FullObjName, fnName, fn)
}
funcPtr = uintptr(unsafe.Pointer(&fns[0]))
}
co := &cefObject{
Id: info.Instance,
ParentId: info.ParentInstance,
2022-12-03 21:56:51 +08:00
Name: api.PascalStr(info.ObjName),
FullObjName: api.PascalStr(info.FullObjName),
2022-10-04 13:21:05 +08:00
FieldLen: uintptr(fieldLen),
Fields: fieldPtr,
FuncLen: uintptr(funcLen),
Funcs: funcPtr,
}
imports.Proc(internale_CEFV8ValueRef_ObjectValueBindInfo).Call(uintptr(unsafe.Pointer(co)))
2022-10-04 13:21:05 +08:00
}
// 创建 结构对象的字段变量
2023-02-22 12:31:35 +08:00
func (m *objectTypeInfo) createObjectFieldVariable(fullParentName, fieldName string, sfi *fieldInfo) {
2023-02-21 23:16:35 +08:00
newV8Value(sfi.EventId, fullParentName, fieldName, sfi.FieldValue, nil, sfi.ValueType.Jsv, IS_OBJECT)
2022-10-04 13:21:05 +08:00
}
// 创建 结构对象的函数变量
2023-02-22 12:31:35 +08:00
func (m *objectTypeInfo) createObjectFuncVariable(fullParentName, funcName string, sfi *functionInfo) {
2023-02-21 23:16:35 +08:00
newV8Value(sfi.EventId, fullParentName, funcName, nil, sfi, V8_VALUE_FUNCTION, IS_OBJECT)
2022-10-04 13:21:05 +08:00
}
// 分析对象的字段
2023-02-22 12:31:35 +08:00
func (m *objectInfo) analysisObjectField(typ reflect.Type, typPtr reflect.Type, value reflect.Value) {
2022-10-04 13:21:05 +08:00
if m.Parent == nil {
m.ObjName = typ.Name()
m.FullObjName = m.ObjName
}
m.Instance = value.Elem().UnsafeAddr()
m.Ptr = unsafe.Pointer(m.Instance)
2023-02-22 12:31:35 +08:00
//字段描述遍历
2022-10-04 13:21:05 +08:00
for i := 0; i < typ.NumField(); i++ {
field := value.Elem().Field(i)
fieldType := field.Type()
fieldName := typ.Field(i).Name
var b, isPrt = false, false
//取出类型,同时判断出是循环引用的类型
if fieldType.Kind() == reflect.Ptr {
fieldType = fieldType.Elem()
isPrt = true
}
//结构对象,循环引用的对象不被支持
if isPrt && fieldType.Kind() == reflect.Struct && !field.IsZero() {
2023-02-22 12:31:35 +08:00
subSoi := &objectInfo{FieldInfos: make(map[string]*fieldInfo), SubObjectInfo: make(map[string]*objectInfo)}
2022-10-04 13:21:05 +08:00
subSoi.Parent = m
subSoi.ParentInstance = m.Instance
subSoi.ObjName = fieldName
subSoi.FullObjName = fmt.Sprintf("%s.%s", subSoi.Parent.FullObjName, subSoi.ObjName)
2023-02-22 12:31:35 +08:00
m.SubObjectInfo[fieldName] = subSoi
2022-10-04 13:21:05 +08:00
//是结构对象分析
if isPrt {
subSoi.analysisObjectField(fieldType, field.Type(), field)
} else {
subSoi.analysisObjectField(fieldType, fieldType, field)
}
b = true
} else if fieldType.Kind() == reflect.Slice { //数组
} else if typ.Field(i).Type.Kind() != reflect.Ptr && fieldType.Kind() != reflect.Struct && typ.Field(i).IsExported() {
//过滤掉指针类型和非导出大写字段,和不是结构类型
b = true
} else if fieldType.Kind() == reflect.Struct && field.IsZero() {
2022-10-05 20:43:25 +08:00
logger.Debug("字段类型-对象,", fieldName, " 未初始化, 忽略JS绑定映射.")
2022-10-04 13:21:05 +08:00
}
if b { //b=true可以正常解析映射
filedValue := value.Elem().FieldByName(fieldName)
t := fieldType.Kind().String()
2023-02-22 12:31:35 +08:00
m.FieldInfos[fieldName] = &fieldInfo{
2022-10-04 13:21:05 +08:00
EventId: uintptr(__bind_id()),
2023-02-22 12:31:35 +08:00
ValueType: &VT{
Jsv: common.JSValueType(t),
Gov: common.GOValueType(t),
2022-10-04 13:21:05 +08:00
},
FieldValue: &filedValue,
}
}
}
2023-02-22 12:31:35 +08:00
m.analysisObjectFunction(typPtr, value)
2022-10-04 13:21:05 +08:00
}
2023-02-22 12:31:35 +08:00
// 分析对象的函数
// 不符合js类型的函数的参数不会被解析成js函数
2023-02-22 12:31:35 +08:00
func (m *objectInfo) analysisObjectFunction(typPtr reflect.Type, value reflect.Value) {
m.FunctionInfos = make(map[string]*functionInfo)
2022-10-04 13:21:05 +08:00
for idx := 0; idx < typPtr.NumMethod(); idx++ {
method := typPtr.Method(idx)
if method.IsExported() {
if fi, err := checkFunc(method.Type, FN_TYPE_OBJECT); err == nil {
methodValue := value.MethodByName(method.Name)
2023-02-22 12:31:35 +08:00
m.FunctionInfos[method.Name] = &functionInfo{
2022-10-04 13:21:05 +08:00
EventId: uintptr(__bind_id()),
funcInfo: fi,
Method: methodValue,
}
} else {
panic(err.Error())
}
}
}
}
2023-02-22 12:31:35 +08:00
func (m *objectTypeInfo) bind(value JSValue) {
if value.IsFunction() {
} else {
m.FieldInfos[value.Name()] = &fieldInfo{
EventId: uintptr(__bind_id()),
ValueType: value.ValueType(),
//FieldValue: &filedValue,
}
}
}