Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[terra]upgrade terra to cxx-parser #1149

Merged
merged 9 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions scripts/terra/code_config.yaml

This file was deleted.

19 changes: 19 additions & 0 deletions scripts/terra/config/impl_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
parsers:
- name: RTCParser
package: '@agoraio-extensions/terra_shared_configs'
args:
sdkVersion: 4.3.0
definesMacros:
- __ELECTRON__
FixEnumConstantParser:
skipCalEnumValue: true

- name: UpdateSimpleTypeParser
package: '@agoraio-extensions/terra_shared_configs'
args:
parserDefaultValue: true
ignoreDefaultConfig: true
configFilePath: '@agoraio-extensions/terra_shared_configs:configs/rtc/ts/c_to_ts_config.ts'

renderers:
- path: ../impl.ts
19 changes: 19 additions & 0 deletions scripts/terra/config/types_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
parsers:
- name: RTCParser
package: '@agoraio-extensions/terra_shared_configs'
args:
sdkVersion: 4.3.0
definesMacros:
- __ELECTRON__
FixEnumConstantParser:
skipCalEnumValue: true

- name: UpdateSimpleTypeParser
package: '@agoraio-extensions/terra_shared_configs'
args:
parserDefaultValue: true
ignoreDefaultConfig: true
configFilePath: '@agoraio-extensions/terra_shared_configs:configs/rtc/ts/c_to_ts_config.ts'

renderers:
- path: ../index.ts
6 changes: 5 additions & 1 deletion scripts/terra/generate-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ MY_PATH=$(realpath $(dirname "$0"))
PROJECT_ROOT=$(realpath ${MY_PATH}/../..)

npm exec terra -- run \
--config ${PROJECT_ROOT}/scripts/terra/code_config.yaml \
--config ${PROJECT_ROOT}/scripts/terra/config/types_config.yaml \
--output-dir=${PROJECT_ROOT}/ts/Private

npm exec terra -- run \
--config ${PROJECT_ROOT}/scripts/terra/config/impl_config.yaml \
--output-dir=${PROJECT_ROOT}/ts/Private

cd ${PROJECT_ROOT}
Expand Down
233 changes: 233 additions & 0 deletions scripts/terra/impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
import * as path from 'path';

import {
CXXFile,
CXXTYPE,
CXXTerraNode,
Variable,
} from '@agoraio-extensions/cxx-parser';

import { ParseResult, TerraContext } from '@agoraio-extensions/terra-core';

import {
IrisApiIdParserUserData,
renderWithConfiguration,
} from '@agoraio-extensions/terra_shared_configs';

import {
convertToCamelCase,
deepClone,
findClazz,
findEnumz,
findStruct,
isMatch,
upperFirstWord,
} from './utils';

interface CXXFileUserData {
fileName: string;
renderFileEnd: boolean;
}

type TerraNodeUserData = IrisApiIdParserUserData & {
isStruct: boolean;
isEnumz: boolean;
isClazz: boolean;
isCallback: boolean;
hasBaseClazzs: boolean;
prefix_name: string;
};

type VariableUserData = IrisApiIdParserUserData & {
name: string;
};

type ClazzMethodUserData = IrisApiIdParserUserData & {
output: string;
input: string;
input_map: Variable[];
input_map_fixed: Variable[];
output_map: Variable[];
hasParameters: boolean;
bindingFunctionName: string;
returnParam: string;
};

export function impl(parseResult: ParseResult) {
let preParseResult = deepClone(parseResult, ['parent']);
let cxxfiles = parseResult.nodes as CXXFile[];
//only render file which has clazz
let view = cxxfiles
.filter((cxxfile) => {
return (
cxxfile.nodes.filter((node) => {
return node.__TYPE === CXXTYPE.Clazz;
}).length > 0
);
})
.map((cxxfile: CXXFile) => {
const cxxUserData: CXXFileUserData = {
renderFileEnd: false,
fileName: path.basename(
cxxfile.file_path,
path.extname(cxxfile.file_path)
),
};

let nodes = cxxfile.nodes.filter((node: CXXTerraNode) => {
return node.__TYPE === CXXTYPE.Clazz;
});

cxxfile.nodes = nodes.map((node: CXXTerraNode) => {
node.asClazz().methods.map((method) => {
let output_params: string[] = [];
let input_params: string[] = [];
const clazzMethodUserData: ClazzMethodUserData = {
output: '',
input: '',
input_map: [],
input_map_fixed: [],
output_map: [],
hasParameters: true,
bindingFunctionName: `getApiTypeFrom${upperFirstWord(method.name)}`,
returnParam: '',
...method.user_data,
};
method.return_type.name = convertToCamelCase(method.return_type.name);
method.asMemberFunction().parameters.map((param) => {
let variableUserData: VariableUserData = {
name: convertToCamelCase(param.name, false),
...param.user_data,
};
let typeName = convertToCamelCase(param.type.name);
let default_value = convertToCamelCase(param.default_value);
param.user_data = variableUserData;
if (param.is_output) {
output_params.push(`${variableUserData.name}: ${typeName}`);
clazzMethodUserData.output_map.push(param);
} else {
let member = `${variableUserData.name}: ${typeName}`;
if (param.default_value) {
if (
param.default_value == '__null' ||
param.default_value == 'nullptr'
) {
member = `${variableUserData.name}?: ${typeName}`;
} else if (param.type.is_builtin_type) {
member += ` = ${default_value}`;
} else {
let flag = false;
if (findStruct(param.type.name, preParseResult).length > 0) {
flag = true;
member += ` = new ${default_value}`;
}
if (findEnumz(param.type.name, preParseResult).length > 0) {
flag = true;
member += ` =${typeName}.${default_value}`;
}
if (!flag) {
member += ` = ${default_value}`;
}
}
}
input_params.push(member);
clazzMethodUserData.input_map.push(param);
if (
typeName !== 'Uint8Array' &&
findClazz(param.type.name, preParseResult).length <= 0
) {
clazzMethodUserData.input_map_fixed.push(param);
}
}
});
if (output_params.length > 0) {
if (
method.asMemberFunction().return_type.name !== 'void' &&
method.asMemberFunction().return_type.name !== 'number'
) {
output_params.push(`result: ${method.return_type.name},`);
}
}
clazzMethodUserData.input = input_params.join(',');
if (output_params.length > 1) {
clazzMethodUserData.output = `{${output_params.join(',')}}`;
} else if (output_params.length == 1) {
clazzMethodUserData.output = output_params[0]?.split(': ')[1]!;
} else {
clazzMethodUserData.output = `${method.return_type.name}`;
}
clazzMethodUserData.hasParameters =
clazzMethodUserData.input_map.length > 0;
if (
clazzMethodUserData.output_map.length > 0 ||
method.return_type.name !== 'void'
) {
clazzMethodUserData.returnParam = `const jsonResults = `;
}
clazzMethodUserData.returnParam +=
'callIrisApi.call(this, apiType, jsonParams);\n';
clazzMethodUserData.output_map.map((output) => {
clazzMethodUserData.returnParam += ` const ${output.name} = jsonResults.${output.name};\n`;
});
if (clazzMethodUserData.output_map.length === 0) {
if (method.return_type.name !== 'void') {
clazzMethodUserData.returnParam += ` return jsonResults.result;\n`;
}
} else {
if (clazzMethodUserData.output_map.length === 1) {
clazzMethodUserData.returnParam += ` return ${clazzMethodUserData.output_map[0]?.name}\n`;
} else {
clazzMethodUserData.returnParam += ` return {\n`;
clazzMethodUserData.output_map.map((output) => {
clazzMethodUserData.returnParam += `${output.name},\n`;
});
clazzMethodUserData.returnParam += `}\n`;
}
}
method.user_data = clazzMethodUserData;
});

const terraNodeUserData: TerraNodeUserData = {
isStruct: node.__TYPE === CXXTYPE.Struct,
isEnumz: node.__TYPE === CXXTYPE.Enumz,
isClazz: node.__TYPE === CXXTYPE.Clazz,
prefix_name: node.name.replace(new RegExp('^I(.*)'), '$1'),
hasBaseClazzs: node.asClazz().base_clazzs.length > 0,
isCallback: isMatch(node.name, 'isCallback'),
...node.user_data,
};
node.user_data = terraNodeUserData;
if (!cxxUserData.renderFileEnd && !terraNodeUserData.isCallback) {
cxxUserData.renderFileEnd = true;
}
return node;
});

cxxfile.user_data = cxxUserData;
return cxxfile;
});
return view;
}

export default function (
terraContext: TerraContext,
args: any,
parseResult: ParseResult
) {
let view = impl(parseResult);
return renderWithConfiguration({
fileNameTemplatePath: path.join(
__dirname,
'templates',
'impl',
'file_name.mustache'
),
fileContentTemplatePath: path.join(
__dirname,
'templates',
'impl',
'file_content.mustache'
),
view,
});
}
Loading