Rearrange the Order of Class Method Signature During AOT Generating Class Prototype HClass

1. Rearrange the order of class method signature During AOT generating class prototype HClass
2. Add test case

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I6ST6G

Signed-off-by: dingding <dingding5@huawei.com>
Change-Id: Icd58d6c3a10dcca97e565224a437651acd983c34
This commit is contained in:
dingding 2023-04-04 11:22:13 +08:00
parent f211ffafd6
commit 656043e526
8 changed files with 153 additions and 16 deletions

View File

@ -461,6 +461,17 @@ bool TSManager::IsAbstractMethod(GlobalTSTypeRef gt) const
return functionType->GetIsAbstract();
}
bool TSManager::IsMethodSignature(GlobalTSTypeRef gt) const
{
if (!IsFunctionTypeKind(gt)) {
return false;
}
JSHandle<JSTaggedValue> tsType = GetTSType(gt);
ASSERT(tsType->IsTSFunctionType());
JSHandle<TSFunctionType> functionType(tsType);
return functionType->GetIsSignature();
}
GlobalTSTypeRef TSManager::GetFuncReturnValueTypeGT(GlobalTSTypeRef gt) const
{
ASSERT(IsFunctionTypeKind(gt));

View File

@ -301,6 +301,8 @@ public:
bool IsAbstractMethod(GlobalTSTypeRef gt) const;
bool IsMethodSignature(GlobalTSTypeRef gt) const;
inline GlobalTSTypeRef PUBLIC_API GetFuncReturnValueTypeGT(kungfu::GateType gateType) const
{
GlobalTSTypeRef gt = gateType.GetGTRef();

View File

@ -69,7 +69,7 @@ JSHClass *TSObjectType::CreateHClassByProps(JSThread *thread, JSHandle<TSObjLayo
hclass->SetNumberOfProps(numOfProps);
} else {
// dictionary mode
hclass = factory->NewEcmaHClass(JSFunction::SIZE, JSType::JS_FUNCTION, 0); // without in-obj
hclass = factory->NewEcmaHClass(JSFunction::SIZE, JSType::JS_OBJECT, 0); // without in-obj
hclass->SetIsDictionaryMode(true);
hclass->SetNumberOfProps(0);
}
@ -92,14 +92,29 @@ JSHClass *TSObjectType::CreatePrototypeHClassByProps(JSThread *thread, JSHandle<
TSManager *tsManager = thread->GetEcmaVM()->GetTSManager();
JSHandle<JSTaggedValue> ctor = globalConst->GetHandledConstructorString();
CVector<std::pair<JSHandle<JSTaggedValue>, GlobalTSTypeRef>> sortedPrototype {{ctor, GlobalTSTypeRef()}};
CVector<std::pair<JSHandle<JSTaggedValue>, GlobalTSTypeRef>> signatureVec {};
for (uint32_t index = 0; index < numOfProps; ++index) {
auto key = propType->GetKey(index);
JSHandle<JSTaggedValue> key(thread, propType->GetKey(index));
auto value = GlobalTSTypeRef(propType->GetTypeId(index).GetInt());
if (!JSTaggedValue::SameValue(key, ctor.GetTaggedValue()) && !tsManager->IsAbstractMethod(value)) {
sortedPrototype.emplace_back(std::make_pair(JSHandle<JSTaggedValue>(thread, key), value));
// Usually, abstract methods in abstract class have no specific implementation,
// and method signatures will be added after class scope.
// Strategy: ignore abstract method, and rearrange the order of method signature to be at the end.
bool isSame = JSTaggedValue::SameValue(key, ctor);
bool isAbs = tsManager->IsAbstractMethod(value);
if (!isSame && !isAbs) {
bool isSign = tsManager->IsMethodSignature(value);
if (LIKELY(!isSign)) {
sortedPrototype.emplace_back(std::make_pair(key, value));
} else {
signatureVec.emplace_back(std::make_pair(key, value));
}
}
}
if (!signatureVec.empty()) {
sortedPrototype.insert(sortedPrototype.end(), signatureVec.begin(), signatureVec.end());
}
uint32_t keysLen = sortedPrototype.size();
JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
JSHandle<LayoutInfo> layout = factory->CreateLayoutInfo(keysLen);

View File

@ -187,18 +187,15 @@ public:
enum class Visibility : uint8_t { PUBLIC = 0, PRIVATE, PROTECTED };
// define BitField
static constexpr size_t VISIBILITY_BITS = 2;
static constexpr size_t STATIC_BITS = 1;
static constexpr size_t ASYNC_BITS = 1;
static constexpr size_t GENERATOR_BITS = 1;
static constexpr size_t GETTERSETTER_BITS = 1;
static constexpr size_t ABSTRACT_BITS = 1;
FIRST_BIT_FIELD(BitField, Visibility, Visibility, VISIBILITY_BITS);
NEXT_BIT_FIELD(BitField, Static, bool, STATIC_BITS, Visibility);
NEXT_BIT_FIELD(BitField, Async, bool, ASYNC_BITS, Static);
NEXT_BIT_FIELD(BitField, Generator, bool, GENERATOR_BITS, Async);
NEXT_BIT_FIELD(BitField, IsGetterSetter, bool, GETTERSETTER_BITS, Generator);
NEXT_BIT_FIELD(BitField, IsAbstract, bool, ABSTRACT_BITS, IsGetterSetter);
static constexpr size_t ONE_BIT = 1;
static constexpr size_t TWO_BITS = 2;
FIRST_BIT_FIELD(BitField, Visibility, Visibility, TWO_BITS);
NEXT_BIT_FIELD(BitField, Static, bool, ONE_BIT, Visibility);
NEXT_BIT_FIELD(BitField, Async, bool, ONE_BIT, Static);
NEXT_BIT_FIELD(BitField, Generator, bool, ONE_BIT, Async);
NEXT_BIT_FIELD(BitField, IsGetterSetter, bool, ONE_BIT, Generator);
NEXT_BIT_FIELD(BitField, IsAbstract, bool, ONE_BIT, IsGetterSetter);
NEXT_BIT_FIELD(BitField, IsSignature, bool, ONE_BIT, IsAbstract);
DECL_VISIT_OBJECT(NAME_OFFSET, RETURN_GT_OFFSET)
DECL_DUMP()

View File

@ -47,6 +47,7 @@ group("ark_aot_ts_test") {
"call_same_bytecode_func",
"callithisrange",
"calls",
"class_method_signature",
"closeiterator",
# "continue_from_finally",

View File

@ -0,0 +1,18 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//arkcompiler/ets_runtime/test/test_helper.gni")
host_aot_test_action("class_method_signature") {
deps = []
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare function print(str:any):string;
// one signature but no body
class C {
constructor() {}
foo?(): string;
bar(): string {
return "test one signature but no body";
}
}
let c = new C();
print(c.bar());
// multi-signatures but one body
class D {
constructor() {}
foo?(a: string): string;
foo?(a: string): string {
return a;
}
foo?(a: string, b?: string): string;
bar(): string {
return "test multi-signatures but one body";
}
}
let d = new D();
print(d.foo!("D"));
print(d.bar());
// multi-signature but no body.
class E {
constructor() {}
foo?(): string;
foo?(a: string): string;
foo?(a: string, b: string): string;
bar(): string {
return "test multi-signatures but no body";
}
}
E.prototype.foo = function(): string {
return "E";
}
let e = new E();
print(e.foo!());
print(e.bar());

View File

@ -0,0 +1,18 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
test one signature but no body
D
test multi-signatures but one body
E
test multi-signatures but no body