style: fix enum bug in class constructor

Signed-off-by: gou-jingjing <goujingjing@kaihong.com>
This commit is contained in:
gou-jingjing 2024-03-05 16:03:56 +08:00
parent a83e43cd97
commit f7023e6ba6
6 changed files with 110 additions and 21 deletions

View File

@ -86,7 +86,13 @@ function getHDefineOfVariable(name, type, variable, optional) {
variable.hDefine += "\n %s %s;".format(type, name)
}
} else if (EnumList.getValue(type)) {
// 如果是枚举string类型需要将其转换为std::string类型
let enumBasicType = EnumList.getValue(type)[0].type
if (enumBasicType === 'string') {
variable.hDefine += "\n %s %s;".format('std::string', name)
} else {
variable.hDefine += "\n %s %s;".format(type, name)
}
} else if (type.indexOf("Array<") == 0) {
typeArrFunctionOne(type, variable, name, optional);
} else if (type == "boolean") {
@ -343,6 +349,10 @@ function getConstructorFunc(data, param) {
getConParam += tmpBody[i] + ";\n";
}
}
let index = getConParam.lastIndexOf(';\n')
if (getConParam.substring(index-1, index) === ' ') {
getConParam = getConParam.substring(0, index -1)
}
return getConParam;
}

View File

@ -226,14 +226,13 @@ function unionTempleteFunc(dest, napiVn, type, optional) {
function jsToCEnum(type, dest, napiVn) {
let tt = ""
let ifl = EnumList.getValue(type)
for (let i in ifl) {
let type2 = ifl[i].type
if (dest.indexOf("p->") < 0) {
tt += jsToC("%s".format(dest), napiVn, type2, type)
} else {
tt += jsToC("%s".format(dest), getValueProperty(napiVn, dest), type2, type)
}
let type2 = ""
if (ifl && ifl.length > 0) {
type2 = ifl[0].type
} else {
return null;
}
tt += jsToC("%s".format(dest), napiVn, type2, type)
return tt
}

View File

@ -105,11 +105,33 @@ function c2JsForEnum(deep, type, value, dest, propertyName) {
let lt = deep
let result = ""
let ifl = EnumList.getValue(type)
let type2 = ifl[0].type
let enumCtoJsStr = cToJs("enumInt%d".format(lt), type2, "tnv%d".format(lt), deep + 1)
result += "{\nnapi_value tnv%d = nullptr;\n".format(lt) + "int enumInt%d = (int)(%s);\n".format(lt, value) +
enumCtoJsStr + `\npxt->SetValueProperty(%s, "%s", tnv%d);\n}\n`
.format(dest, propertyName, lt)
let type2 = ""
if (ifl && ifl.length > 0) {
type2 = ifl[0].type
}
let enumCtoJsStr = cToJs("enumC2Js%d".format(lt), type2, dest, deep)
if (type2 === 'string') {
// string型枚举的getvalue函数
result += "{\nstd::string enumC2Js%d = %s;\n".format(lt, value) + enumCtoJsStr + "}\n"
} else {
// number型枚举的getvalue函数
result += `
std::underlying_type<%s>::type enumType;
if (typeid(enumType) == typeid(uint32_t)) {
uint32_t enumC2Js%d = static_cast<uint32_t>(%s);
%s
} else if (typeid(enumType) == typeid(int32_t)) {
int32_t enumC2Js%d = static_cast<int32_t>(%s);
%s
} else if (typeid(enumType) == typeid(int64_t)) {
int64_t enumC2Js%d = static_cast<int64_t>(%s);
%s
} else if (typeid(enumType) == typeid(double_t)) {
double_t enumC2Js%d = static_cast<double_t>(%s);
%s
}`.format(type, lt, value, enumCtoJsStr, lt, value, enumCtoJsStr, lt, value, enumCtoJsStr,
lt, value, enumCtoJsStr,)
}
return result
}

View File

@ -33,12 +33,21 @@ declare namespace napitest {
age: number;
}
// 有参构造函数的转换
// 有参构造函数的转换,成员变量包含数值型枚举
export class Woman {
constructor(name_: string, age_: number, isMarried_: boolean);
constructor(name_: string, age_: number, isMarried_: boolean, status_: TestStatus);
w_name: string;
w_age: number;
w_isMarried: boolean;
w_status: TestStatus;
}
// 有参构造函数的转换,成员变量包含字符型枚举
export class Child {
constructor(name_: string, age_: number, status_: TestEnumString);
w_name: string;
w_age: number;
w_status: TestEnumString;
}
export enum LaunchReason {

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
const { TestClass1, TestClass2, TestClassUse, TestClassLater } = require("./out/build/Release/napitest")
const { Demo, Test, funcTest, funcTest2, Woman } = require("./out/build/Release/napitest")
const { Demo, Test, funcTest, funcTest2, Woman, Child } = require("./out/build/Release/napitest")
const test = require("./out/build/Release/napitest")
var assert = require("assert");
const { consumers } = require("stream");
@ -49,17 +49,66 @@ describe('Class', function () {
});
// export class Woman {
// constructor(name_: string, age_: number, isMarried_: boolean);
// constructor(name_: string, age_: number, isMarried_: boolean, status_: TestStatus;);
// w_name: string;
// w_age: number;
// w_isMarried: boolean;
// w_status: TestStatus;
// }
it('test Woman constructor', function () {
let tc = new Woman("haha", 22, true);
console.info("w_name is " + tc.w_name);
console.info("w_age is " + tc.w_age);
console.info("w_isMarried is " + tc.w_isMarried);
let tc = new Woman("haha", 22, true, test.TestStatus.START_ABILITY);
console.info("w_name is " + tc.w_name);
console.info("w_age is " + tc.w_age);
console.info("w_isMarried is " + tc.w_isMarried);
console.info("w_status is " + tc.w_status);
});
// export class Child {
// constructor(name_: string, age_: number, status_: TestEnumString);
// w_name: string;
// w_age: number;
// w_status: TestEnumString;
// }
it('test Child constructor', function () {
let tc = new Child("xixi", 10, test.TestEnumString.ACTION_SEARCH);
console.info("w_name is " + tc.w_name);
console.info("w_age is " + tc.w_age);
console.info("w_status is " + tc.w_status);
});
});
describe('Class Exception Test', function () {
// 异常测试class构造函数包含数值型枚举的异常测试
it('test Woman constructor exception', function () {
let ret = false;
try {
let tc = new Woman("hhh", 23, true, 5);
console.info("w_name is " + tc.w_name);
console.info("w_age is " + tc.w_age);
console.info("w_isMarried is " + tc.w_isMarried);
console.info("w_status is " + tc.w_status);
} catch(err) {
ret = true;
console.error("error: " + err);
}
assert.strictEqual(ret, true);
});
// 异常测试class构造函数包含字符型枚举的异常测试
it('test Child constructor exception', function () {
let ret = false;
try {
let tc = new Child("xixi", 10, "ggg");
console.info("w_name is " + tc.w_name);
console.info("w_age is " + tc.w_age);
console.info("w_status is " + tc.w_status);
} catch(err) {
ret = true;
console.error("error: " + err);
}
assert.strictEqual(ret, true);
});
});
describe('Class defined later', function () {

View File

@ -481,7 +481,7 @@ function partOfTest() {
it('test gen/generate/param_generate jsToCEnum', function () {
let ret = jsToCEnum('string', 'vio->in0', 'pxt->GetArgv(0)')
let retJson = JSON.stringify(ret)
assert.strictEqual(retJson, '""')
assert.strictEqual(retJson, 'null')
});
partOfTestTwo()