• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16import { getTab } from '../../common/tool';
17import { getReg, match } from '../../common/re';
18import { format } from 'util'
19import { FuncObj, ParamObj, StructObj, ClassObj } from '../datatype';
20import { transferMap } from '../../template/functypemap_template'
21import { classMethodDeclareTemplate, objectRet, promiseRet } from "../../template/func_template";
22import { replaceAll } from "../../common/tool";
23import { h2NapiInKey, h2NapiOutKey } from "../../template/dtscpp/dts2cpp_key";
24
25export function getFuncParamStr(params: ParamObj[]) {
26  let paramStr = '';
27  for (let i = 0; i < params.length; ++i) {
28    paramStr += (i === 0) ? '' : ', ';
29    paramStr += params[i].type + ' ' + params[i].name;
30  }
31  return paramStr;
32}
33
34// 生成头文件中的方法声明内容
35export function genDeclareContent(funcList: FuncObj[]) {
36  let funcTab = getTab(1);
37  let saFuncHContent = '';
38  for (let i = 0; i < funcList.length; ++i) {
39    let paramStr = getFuncParamStr(funcList[i].parameters);
40    // proxy.h中的方法定义
41    saFuncHContent += (i === 0) ? '' : '\n' + funcTab;
42    saFuncHContent += format('%s %s(%s) override;', funcList[i].returns, funcList[i].name, paramStr);
43  }
44  return saFuncHContent;
45}
46
47// 常用类型转换表, 将C语言常见类型(key)转换为remote data读写函数使用的类型(value)
48// 例如 ErrCode 类型在框架中的系统原型为int类型,这里映射成int32_t,
49// 因为int32_t类型在 DATA_W_MAP/DATA_R_MAP 表中有对应的读写数据方法(WriteInt32/ReadInt32)
50const TYPE_DEF_MAP = new Map(
51  [['ErrCode', 'int32_t'], ['char', 'int8_t'], ['short', 'int16_t'], ['int', 'int32_t'], ['long', 'int64_t'],
52  ['unsigned char', 'uint8_t'], ['unsigned short', 'uint16_t'], ['unsigned int', 'uint32_t'],
53  ['unsigned long', 'uint64_t'], ['double_t', 'double'], ['float_t', 'float'], ['size_t', 'double'],
54  ['long long', 'double'], ['long double', 'double'], ['std::string', 'string']
55  ]);
56
57export function getParcelType(srcType: string) {
58  let parcelType = TYPE_DEF_MAP.get(srcType);
59  return parcelType === undefined ? srcType : parcelType;
60}
61
62export function getTransferContent(parcelVecType: string, isWrite: number) {
63  let rwFunc = '';
64  for (let index = 0; index < transferMap.length; index++) {
65    if (parcelVecType === transferMap[index].fromType) {
66      rwFunc = transferMap[index].tranferContent[isWrite];
67    }
68  }
69  return rwFunc;
70}
71
72export function genWrite(srcName: string, parcelName: string, vType: string) {
73  let matchs = match('(std::)?vector<([\x21-\x7e]+)[ ]?>', vType);
74  if (matchs) {
75    // vector类型变量包装成parcel data
76    let rawType = getReg(vType, matchs.regs[2]);
77    let parcelVecType = 'vector<' + getParcelType(rawType) + '>';
78    let wVecFunc = getTransferContent(parcelVecType, 0);
79    if (wVecFunc === '') {
80      return '';
81    }
82    return format('%s.%s(%s);', parcelName, wVecFunc, srcName);
83  }
84
85  let parcelType = getParcelType(vType);
86  let wFunc = getTransferContent(parcelType, 0);
87
88  return format('%s.%s(%s);', parcelName, wFunc, srcName);
89}
90
91export function genRead(parcelName: string, destObj: ParamObj) {
92  let matchs = match('(std::)?vector<([\x21-\x7e]+)[ ]?>', destObj.type);
93  if (matchs) {
94    // 从parcel data中读取vector类型变量
95    let rawType = getReg(destObj.type, matchs.regs[2]);
96    let parcelVecType = getParcelType(rawType);
97    let rVecFunc = 'vector<' + getTransferContent(parcelVecType, 1) + '>';
98    if (rVecFunc === '') {
99      return '';
100    }
101    return format('%s.%s(&(%s));', parcelName, rVecFunc, parcelName);
102  }
103
104  let parcelType = getParcelType(destObj.type);
105  let rFunc = getTransferContent(parcelType, 1);
106  return format('%s = %s.%s();', destObj.name, parcelName, rFunc);
107}
108
109// ------------ gencpp common function --------------
110// 通过类型值映射模板,比如:uint32_t返回值 -> uint32tRet -> napi_create_uint32
111export function transCkey2NapiOutkey(key: string) {
112  // 如果是ts传递的Promise<>类型,并且transTs2C时未转换,那么就返回promiseRet
113  let tsPromiseReg = /Promise<([^>]+)>/g;
114  const tsPromiseMatch = tsPromiseReg.exec(key);
115  if (tsPromiseMatch) {
116    return promiseRet;
117  }
118
119 // 数组 map set iterator tuple pair 等都当作objectOut处理
120 for (const keyItem of h2NapiOutKey) {
121   for (const str of keyItem.keys) {
122     if (key.includes(str)) {
123       return keyItem.value;
124     }
125   }
126 }
127 let replaceKeyList = ['enum', 'struct', 'union'];
128 for (const rkey of replaceKeyList) {
129   key = key.replace(rkey, '').trim();
130 }
131 // 其他的全部当作object处理, 如typeDef/enum/struct/union/class等,当作objectOut处理,返回objectRet
132 return objectRet;
133}
134
135// 通过类型值映射模板,比如:uint32_t输入 -> uint32tIn -> napi_get_value_uint32
136export function transCkey2NapiInkey(key: string) {
137 for (const keyItem of h2NapiInKey) {
138   for (const str of keyItem.keys) {
139     if (key.includes(str)) {
140       return keyItem.value;
141     }
142   }
143 }
144 let replaceKeyList = ['enum', 'struct', 'union'];
145 for (const rkey of replaceKeyList) {
146   key = key.replace(rkey, '').trim();
147 }
148 // 其他的全部当作object处理, 如typeDef/enum/struct/union/class等, 此时不需要做任何处理,因此返回空
149 return '';
150}
151// class的成员变量声明,以及成员变量的Get/Set方法的声明
152export function genClsVariableDeclare(cls: ClassObj) {
153  let clsVariableDeclare = '';
154  let clsVariableGetSetDeclare = '';
155  for (let i = 0; i < cls.variableList.length; ++i) {
156    clsVariableDeclare += cls.variableList[i].type + ' ' + cls.variableList[i].name + ';\n    ';
157    let name = cls.variableList[i].name.toLocaleLowerCase();
158    name = name.substring(0, 1).toLocaleUpperCase() + name.substring(1);
159    // 属性Get函数声明
160    clsVariableGetSetDeclare += replaceAll(classMethodDeclareTemplate, '[class_method_name_replace]', 'Get' + name);
161    // 属性Set函数声明
162    clsVariableGetSetDeclare += replaceAll(classMethodDeclareTemplate, '[class_method_name_replace]', 'Set' + name);
163  }
164  return { clsVariableDeclare, clsVariableGetSetDeclare };
165}
166
167// struct的成员变量声明,以及成员变量的Get/Set方法的声明
168export function genStructVariableDeclare(struct: StructObj) {
169  let clsVariableDeclare = '';
170  let clsVariableGetSetDeclare = '';
171  for (let i = 0; i < struct.members.length; ++i) {
172    clsVariableDeclare += struct.members[i].type + ' ' + struct.members[i].name + ';\n    ';
173    let name = struct.members[i].name.toLocaleLowerCase();
174    name = name.substring(0, 1).toLocaleUpperCase() + name.substring(1);
175    // 属性Get函数声明
176    clsVariableGetSetDeclare += replaceAll(classMethodDeclareTemplate, '[class_method_name_replace]', 'Get' + name);
177    // 属性Set函数声明
178    clsVariableGetSetDeclare += replaceAll(classMethodDeclareTemplate, '[class_method_name_replace]', 'Set' + name);
179  }
180  return { clsVariableDeclare, clsVariableGetSetDeclare };
181}
182