diff --git a/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml b/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml index 20640a94bee3518b9f4ed57bc5e7cb834b22a550..b20515d2ae72b419f45b94697fcfc573731f02f0 100644 --- a/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml +++ b/static_core/plugins/ets/runtime/ets_libbase_runtime.yaml @@ -6809,6 +6809,16 @@ intrinsics: args: [ ] impl: ark::ets::intrinsics::StdCoreClassIsNamespace + - name: StdCoreClassGetFixedArrayComponentType + space: ets + class_name: std.core.Class + method_name: getFixedArrayComponentType + static: false + signature: + ret: std.core.Class + args: [ ] + impl: ark::ets::intrinsics::StdCoreClassGetFixedArrayComponentType + - name: StdCoreClassGetLinker space: ets class_name: std.core.Class diff --git a/static_core/plugins/ets/runtime/intrinsics/std_core_Class.cpp b/static_core/plugins/ets/runtime/intrinsics/std_core_Class.cpp index a3fee7e4c7f46d089b22ade70af88f9352874e9a..c705720735018dfd5005227c3c3eb018c2f0a5f3 100644 --- a/static_core/plugins/ets/runtime/intrinsics/std_core_Class.cpp +++ b/static_core/plugins/ets/runtime/intrinsics/std_core_Class.cpp @@ -194,4 +194,45 @@ EtsBoolean StdCoreClassIsFixedArray(EtsClass *cls) return cls->IsArrayClass(); } +namespace { +EtsClass *ConvertPandaTypeIdToEtsClass(panda_file::Type::TypeId typeId) +{ + switch (typeId) { + case ark::panda_file::Type::TypeId::I32: + return PlatformTypes()->coreInt; + case ark::panda_file::Type::TypeId::I64: + return PlatformTypes()->coreLong; + case ark::panda_file::Type::TypeId::F64: + return PlatformTypes()->coreDouble; + case ark::panda_file::Type::TypeId::F32: + return PlatformTypes()->coreFloat; + case ark::panda_file::Type::TypeId::I8: + return PlatformTypes()->coreByte; + case ark::panda_file::Type::TypeId::I16: + return PlatformTypes()->coreShort; + case ark::panda_file::Type::TypeId::U16: + return PlatformTypes()->coreChar; + case ark::panda_file::Type::TypeId::U1: + return PlatformTypes()->coreBoolean; + default: + UNREACHABLE(); + } +} +} // namespace + +EtsClass *StdCoreClassGetFixedArrayComponentType(EtsClass *cls) +{ + if (!cls->IsArrayClass()) { + return nullptr; + } + + EtsClass *compCls = cls->GetComponentType(); + if (compCls->IsPrimitive()) { + auto *rtCls = compCls->GetRuntimeClass(); + return ConvertPandaTypeIdToEtsClass(rtCls->GetType().GetId()); + } + + return compCls; +} + } // namespace ark::ets::intrinsics diff --git a/static_core/plugins/ets/stdlib/std/core/Class.ets b/static_core/plugins/ets/stdlib/std/core/Class.ets index 05cda4e3630072ebbd510e31041826f5f2aa41b0..b6981bea0f49a94015b3d1dfc15f42eb2c45074a 100644 --- a/static_core/plugins/ets/stdlib/std/core/Class.ets +++ b/static_core/plugins/ets/stdlib/std/core/Class.ets @@ -37,12 +37,15 @@ export final class Class { } public native getLinker(): RuntimeLinker + public native isNamespace(): boolean; public getNamespace(): Class | undefined { throw new Error("not implemented") } + public native getFixedArrayComponentType(): Class | undefined + public native getInterfaces(): FixedArray /** diff --git a/static_core/plugins/ets/tests/ets-common-tests/class/class_test.ets b/static_core/plugins/ets/tests/ets-common-tests/class/class_test.ets index 4160dd23161a558c2a55db08b3dfb26d6703a182..9f4ba0e09a4d0ef27ab11fbd6c974bec4c791fa0 100644 --- a/static_core/plugins/ets/tests/ets-common-tests/class/class_test.ets +++ b/static_core/plugins/ets/tests/ets-common-tests/class/class_test.ets @@ -30,7 +30,7 @@ namespace Outer { } class Test { - public static testMethodsFromClass(): void { + public testMethodsFromClass(): void { const currentClass = Class.current() arktest.assertNE(currentClass, undefined) arktest.assertEQ(currentClass.getName(), "class_test.Test") @@ -40,7 +40,7 @@ class Test { arktest.assertEQ(callerClass!.getName(), globalClassName) } - public static testClassIsNamespace(): void { + public testClassIsNamespace(): void { const innerNs = Outer.Inner.getNamespace() arktest.assertNE(innerNs, undefined) arktest.assertTrue(innerNs!.isNamespace()) @@ -51,6 +51,74 @@ class Test { arktest.assertTrue(outerNs!.isNamespace()) arktest.assertEQ(outerNs!.getName(), "class_test.Outer") } + + private intArrayComponentType(): void { + const ints: FixedArray = [1, 2] + const compCls = Class.of(ints).getFixedArrayComponentType() + arktest.assertEQ(compCls, Class.of(new Int(1))) + } + + private longArrayComponentType(): void { + const longs: FixedArray = [1, 2] + const compCls = Class.of(longs).getFixedArrayComponentType() + arktest.assertEQ(compCls, Class.of(new Long(1))) + } + + private doubleArrayComponentType(): void { + const doubles: FixedArray = [1.0, 2.0] + const compCls = Class.of(doubles).getFixedArrayComponentType() + arktest.assertEQ(compCls, Class.of(new Double(1.0))) + } + + private floatArrayComponentType(): void { + const floats: FixedArray = [1.0, 2.0] + const compCls = Class.of(floats).getFixedArrayComponentType() + arktest.assertEQ(compCls, Class.of(new Float(1.0))) + } + + private byteArrayComponentType(): void { + const bytes: FixedArray = [1, 2] + const compCls = Class.of(bytes).getFixedArrayComponentType() + arktest.assertEQ(compCls, Class.of(new Byte(1))) + } + + private shortArrayComponentType(): void { + const shorts: FixedArray = [1, 2] + const compCls = Class.of(shorts).getFixedArrayComponentType() + arktest.assertEQ(compCls, Class.of(new Short(1))) + } + + private charArrayComponentType(): void { + const chars: FixedArray = [c'A', c'B'] + const compCls = Class.of(chars).getFixedArrayComponentType() + arktest.assertEQ(compCls, Class.of(new Char(c'A'))) + } + + private booleanArrayComponentType(): void { + const bools: FixedArray = [true, false] + const compCls = Class.of(bools).getFixedArrayComponentType() + arktest.assertEQ(compCls, Class.of(new Boolean(true))) + } + + public testClassGetFixedArrayComponentType(): void { + const obj = new Object() + const objCls = Class.of(obj) + arktest.assertEQ(objCls.getFixedArrayComponentType(), undefined) + + this.intArrayComponentType() + this.longArrayComponentType() + this.doubleArrayComponentType() + this.floatArrayComponentType() + this.byteArrayComponentType() + this.shortArrayComponentType() + this.charArrayComponentType() + + const arrays: FixedArray> = [[1], [2]] + const arraysCompCls = Class.of(arrays).getFixedArrayComponentType() + arktest.assertNE(arraysCompCls, undefined) + arktest.assertNE(arraysCompCls!.getFixedArrayComponentType(), undefined) + arktest.assertEQ(arraysCompCls!.getFixedArrayComponentType(), Class.of(new Int(1))) + } } function main(): void { @@ -63,6 +131,8 @@ function main(): void { const callerClass = Class.ofCaller() arktest.assertEQ(callerClass, undefined) - Test.testMethodsFromClass() - Test.testClassIsNamespace() + const test = new Test() + test.testMethodsFromClass() + test.testClassIsNamespace() + test.testClassGetFixedArrayComponentType() }