From e6930a0128adc5609122454d2e1085456c5c9bca Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Wed, 19 Feb 2025 18:00:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?h2dts=E6=94=AF=E6=8C=81=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E6=8C=87=E9=92=88=E8=BD=AC=E6=8D=A2=EF=BC=8C=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?std::function=E8=BD=AC=E6=8D=A2bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gou-jingjing --- src/vscode_plugin/src/gen/gendts.ts | 48 +++++++++++++++++++---------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/vscode_plugin/src/gen/gendts.ts b/src/vscode_plugin/src/gen/gendts.ts index 86deb182..d542c0da 100644 --- a/src/vscode_plugin/src/gen/gendts.ts +++ b/src/vscode_plugin/src/gen/gendts.ts @@ -308,6 +308,35 @@ export function getInterfaceBody(testType: string, interfaceList: InterfaceList[ // h2dts export function transTskey2Ckey(key: string): string { + // 判断是否是std::function, 转换为箭头函数 如:std::function 转换为 (a: number, b: number)=>void + const regexFunction = /\b(std::)?function<([\w\s\:\*]+)\s?\(([\w\:\<\>\,\s*]*)\)>/; + const matchFunction = key.match(regexFunction); + if (matchFunction) { + const returnType = matchFunction[2].trim(); // 返回类型 + let paramstr = matchFunction[3] ? matchFunction[3].trim() : '' + let paramreg = /([\w\s\:\*]+<[^>]*>|[\*\w\s\:]+)/g; + let pmatch; + let paramList = []; + while ((pmatch = paramreg.exec(paramstr)) !== null) { + paramList.push(pmatch[0]); + } + let str = ''; + for (let i = 0; i < paramList.length; ++i) { + str += paramList[i].trim() === ''? '': `param${i}: ${transTskey2Ckey(paramList[i])}`; + if (i != paramList.length - 1) { + str += ', '; + } + } + return `(${str})=>${transTskey2Ckey(returnType)}`; + } + + // 智能指针,例如: std::unique_ptr -> number + const regexSmartPtr =/\b((std::)?(?:unique_ptr|shared_ptr|weak_ptr))\s*<([^>]*)>/; + const matchSmartPtr = key.match(regexSmartPtr); + if (matchSmartPtr) { + return transTskey2Ckey(matchSmartPtr[3].trim()); + } + // 判断迭代器: 如std::vector::iterator -> IterableIterator> const regexIterator = /(std::\w+<[^>]+>)::iterator/; const matchIterator = key.match(regexIterator); @@ -354,22 +383,6 @@ export function transTskey2Ckey(key: string): string { return `[${str}]`; } - // 判断是否是std::function, 转换为箭头函数 如:std::function 转换为 (a: number, b: number)=>void - const regexFunction = /\b(std::)?function<([^<>]+)\(([^)]+)\)>/; - const matchFunction = key.match(regexFunction); - if (matchFunction) { - const returnType = matchFunction[1].trim(); // 返回类型 - const paramList = (matchFunction[2].trim()).split(",").map(param => param.trim()); - let str = ''; - for (let i = 0; i < paramList.length; ++i) { - str += `param${i}: ${transTskey2Ckey(paramList[i])}`; - if (i != paramList.length - 1) { - str += ', '; - } - } - return `(${str})=>${transTskey2Ckey(returnType)}`; - } - // 判断是否是std::complex , 将复数类型转换为{real: number, imag: number}类型 const regexComplex = /\b((std::)?(?:complex))\s*<([^<>]*)>/; const matchComplex = key.match(regexComplex); @@ -395,7 +408,8 @@ export function transTskey2Ckey(key: string): string { for(const rkey of replaceKeyList) { key = key.replace(rkey, '').trim(); } - return key; + // 其他类型转换为 any 类型,如typeDef定义的类型 + return 'any' } export function getDtsEnum(rootInfo: GenInfo) { -- Gitee From 7ac94de6e816db90ef62bd2635644d80a39882e6 Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Thu, 20 Feb 2025 14:03:03 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug=E2=80=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gou-jingjing --- src/vscode_plugin/src/gen/gendts.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vscode_plugin/src/gen/gendts.ts b/src/vscode_plugin/src/gen/gendts.ts index d542c0da..82a9f6f0 100644 --- a/src/vscode_plugin/src/gen/gendts.ts +++ b/src/vscode_plugin/src/gen/gendts.ts @@ -309,7 +309,7 @@ export function getInterfaceBody(testType: string, interfaceList: InterfaceList[ // h2dts export function transTskey2Ckey(key: string): string { // 判断是否是std::function, 转换为箭头函数 如:std::function 转换为 (a: number, b: number)=>void - const regexFunction = /\b(std::)?function<([\w\s\:\*]+)\s?\(([\w\:\<\>\,\s*]*)\)>/; + const regexFunction = /\b(std::)?function<([\w\s\:\*]+)\s*\(([\w\:\<\>\,\s*]*)\)>/; const matchFunction = key.match(regexFunction); if (matchFunction) { const returnType = matchFunction[2].trim(); // 返回类型 @@ -331,7 +331,7 @@ export function transTskey2Ckey(key: string): string { } // 智能指针,例如: std::unique_ptr -> number - const regexSmartPtr =/\b((std::)?(?:unique_ptr|shared_ptr|weak_ptr))\s*<([^>]*)>/; + const regexSmartPtr = /\b((std::)?(?:unique_ptr|shared_ptr|weak_ptr))\s*<([\w\:\<\>\,\s*]+)>/; const matchSmartPtr = key.match(regexSmartPtr); if (matchSmartPtr) { return transTskey2Ckey(matchSmartPtr[3].trim()); -- Gitee