diff --git a/cxx-parser/__tests__/unit_test/cxx_terra_node_ext.test.ts b/cxx-parser/__tests__/unit_test/cxx_terra_node_ext.test.ts new file mode 100644 index 0000000..3beae67 --- /dev/null +++ b/cxx-parser/__tests__/unit_test/cxx_terra_node_ext.test.ts @@ -0,0 +1,17 @@ +import fs from 'fs'; +import os from 'os'; +import path from 'path'; + +import '../../src/cxx_terra_node_ext'; +import { SimpleType, SimpleTypeKind } from '../../src/cxx_terra_node'; + +describe('cxx_terra_node_ext', () => { + it('lenOfArrayType can get len of array type', () => { + let simpleType = new SimpleType(); + simpleType.kind = SimpleTypeKind.array_t; + simpleType.source = 'int[10]'; + + let len = simpleType.lenOfArrayType(); + expect(len).toBe('10'); + }); +}); diff --git a/cxx-parser/src/cxx_terra_node_ext.ts b/cxx-parser/src/cxx_terra_node_ext.ts index f11c927..98161c3 100644 --- a/cxx-parser/src/cxx_terra_node_ext.ts +++ b/cxx-parser/src/cxx_terra_node_ext.ts @@ -1,3 +1,7 @@ +import { strict as assert } from 'assert'; + +import { SimpleType, SimpleTypeKind } from './cxx_terra_node'; + export {}; declare global { @@ -17,6 +21,15 @@ declare global { } } +declare module '@agoraio-extensions/cxx-parser' { + export interface SimpleType { + /** + * The length of the array type. For example, "int[10]" returns "10". + */ + lenOfArrayType(): string; + } +} + /** * This function removes the namespace from a fully qualified name. * For example, "foo::bar" becomes "bar". @@ -51,3 +64,19 @@ String.prototype.getNamespace = function (): string { const splitted = this.split('::'); return Array.from(splitted.slice(0, splitted.length - 1)).join('::'); }; + +/** + * The length of the array type. For example, "int[10]" returns "10". + */ +SimpleType.prototype.lenOfArrayType = function (): string { + assert(this.kind === SimpleTypeKind.array_t); + assert(this.source); + + let regex = new RegExp(/\d+/); + let len = this.source.match(regex); + if (len) { + return len[0]; + } + + assert(false); // Should not reach here +}; diff --git a/cxx-parser/src/index.ts b/cxx-parser/src/index.ts index 49d81e5..e8822ff 100644 --- a/cxx-parser/src/index.ts +++ b/cxx-parser/src/index.ts @@ -1,5 +1,7 @@ -export * from './cxx_terra_node'; +// This should be above the `cxx_terra_node`, or it will throw TypeError: Cannot read properties of undefined (reading 'xxx') export * from './cxx_terra_node_ext'; +export * from './cxx_terra_node'; export * from './cxx_parser_configs'; -export * from './cxx_parser'; +// This should be above the `cxx_parser`, or it will throw TypeError: Cannot read properties of undefined (reading 'xxx') export * from './cxx_parser_ext'; +export * from './cxx_parser';