• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2022 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// remote消息中变量类型名(key)与对应的写parcel方法名(value)的映射(参考parcel.h)
16const DATA_W_MAP = new Map(
17    [['bool', 'WriteBoolUnaligned'], ['int8_t', 'WriteInt8'], ['uint8_t', 'ReadUint8'],
18     ['int16_t', 'WriteInt16'], ['uint16_t', 'WriteUint16'],
19     ['int32_t', 'WriteInt32'], ['uint32_t', 'WriteUint32'], ['int64_t', 'WriteInt64'], ['uint64_t', 'WriteUint64'],
20     ['float', 'WriteFloat'], ['double', 'WriteDouble'], ['char *', 'WriteCString'], ['std::string', 'WriteString'],
21     ['string', 'WriteString']
22]);
23
24// remote消息中变量类型名(key)与对应的读parcel方法名(value)的映射(参考parcel.h)
25const DATA_R_MAP = new Map(
26    [['bool', 'ReadBoolUnaligned'], ['int8_t', 'ReadInt8'], ['uint8_t', 'ReadUint8'],
27     ['int16_t', 'ReadInt16'], ['uint16_t', 'ReadUint16'],
28     ['int32_t', 'ReadInt32'], ['uint32_t', 'ReadUint32'], ['int64_t', 'ReadInt64'], ['uint64_t', 'ReadUint64'],
29     ['float', 'ReadFloat'], ['double', 'ReadDouble'], ['char *', 'ReadCString'], ['std::string', 'ReadString'],
30     ['string', 'ReadString']
31]);
32
33// 常用类型转换表, 将C语言常见类型(key)转换为remote data读写函数使用的类型(value)
34// 例如 ErrCode 类型在框架中的系统原型为int类型,这里映射成int32_t,
35// 因为int32_t类型在 DATA_W_MAP/DATA_R_MAP 表中有对应的读写数据方法(WriteInt32/ReadInt32)
36const TYPE_DEF_MAP = new Map(
37    [['ErrCode', 'int32_t'], ['char', 'int8_t'], ['short', 'int16_t'], ['int', 'int32_t'], ['long', 'int64_t'],
38    ['unsigned char', 'uint8_t'], ['unsigned short', 'uint16_t'], ['unsigned int', 'uint32_t'],
39    ['unsigned long', 'uint64_t'], ['double_t', 'double'], ['float_t', 'float'], ['size_t', 'double'],
40    ['long long', 'double'], ['long double', 'double']
41]);
42
43// remote消息中vector变量类型名(key)与对应的写parcel方法名(value)的映射(参考parcel.h)
44const VECTOR_W_MAP = new Map(
45    [['bool', 'WriteBoolVector'], ['int8_t', 'WriteInt8Vector'], ['uint8_t', 'WriteUInt8Vector'],
46     ['int16_t', 'WriteInt16Vector'], ['uint16_t', 'WriteUInt16Vector'], ['int32_t', 'WriteInt32Vector'],
47     ['uint32_t', 'WriteUInt32Vector'], ['int64_t', 'WriteInt64Vector'], ['uint64_t', 'WriteUInt64Vector'],
48     ['float', 'WriteFloatVector'], ['double', 'WriteDoubleVector'], ['u16string', 'WriteString16Vector'],
49     ['std::string', 'WriteStringVector'], ['string', 'WriteStringVector']
50]);
51
52// remote消息中vector变量类型名(key)与对应的读parcel方法名(value)的映射(参考parcel.h)
53const VECTOR_R_MAP = new Map(
54    [['bool', 'ReadBoolVector'], ['int8_t', 'ReadInt8Vector'], ['uint8_t', 'ReadUInt8Vector'],
55     ['int16_t', 'ReadInt16Vector'], ['uint16_t', 'ReadUInt16Vector'], ['int32_t', 'ReadInt32Vector'],
56     ['uint32_t', 'ReadUInt32Vector'], ['int64_t', 'ReadInt64Vector'], ['uint64_t', 'ReadUInt64Vector'],
57     ['float', 'ReadFloatVector'], ['double', 'ReadDoubleVector'], ['u16string', 'ReadString16Vector'],
58     ['std::string', 'ReadStringVector'], ['string', 'ReadStringVector']
59]);
60
61function getParcelType(srcType) {
62    let parcelType = TYPE_DEF_MAP.get(srcType);
63    return parcelType === undefined ? srcType : parcelType;
64}
65
66class MarshallInfo {
67    constructor(className) {
68        this.className = className;
69        this.marshallFuncName = '';
70        this.marshallFuncStr = '';
71        this.unmarshallFuncName = '';
72        this.unmarshallFuncStr = '';
73    }
74}
75
76class AllParseFileList { }
77AllParseFileList.parseFile_ = [];
78AllParseFileList.push = function (ifs) {
79    AllParseFileList.parseFile_.push(ifs);
80};
81AllParseFileList.pop = function () {
82    AllParseFileList.parseFile_.pop();
83};
84AllParseFileList.clearAll = function () {
85    AllParseFileList.parseFile_.splice(0, AllParseFileList.parseFile_.length);
86};
87AllParseFileList.findClassByName = function (destClassName) {
88    for (let i = 0; i < AllParseFileList.parseFile_.length; ++i) {
89        let classes = AllParseFileList.parseFile_[i].classes;
90        for (let className in classes) {
91            if (className === destClassName) {
92                classes[className].isInclude = AllParseFileList.parseFile_[i].isInclude;
93                return classes[className];
94            }
95        }
96    }
97    return null;
98};
99
100
101/**
102 * 记录正在生成序列化代码的类名,防止嵌套循环
103 */
104class ProcessingClassList { }
105ProcessingClassList.classes_ = [];
106ProcessingClassList.push = function (classObj) {
107    if (this.findByName(classObj.className) !== null) {
108        // 已存在的class不重复添加
109        return;
110    }
111    ProcessingClassList.classes_.push(classObj);
112};
113ProcessingClassList.clearAll = function () {
114    ProcessingClassList.classes_.splice(0, ProcessingClassList.classes_.length);
115};
116ProcessingClassList.findByName = function (className) {
117    for (let i = 0; i < ProcessingClassList.classes_.length; ++i) {
118        if (ProcessingClassList.classes_[i].className === className) {
119            return ProcessingClassList.classes_[i];
120        }
121    }
122    return null;
123};
124
125module.exports = {
126    DATA_W_MAP, DATA_R_MAP, VECTOR_W_MAP, VECTOR_R_MAP, getParcelType, AllParseFileList, MarshallInfo,
127    ProcessingClassList
128};
129
130