diff --git a/plugins/ets/runtime/ets_vm.cpp b/plugins/ets/runtime/ets_vm.cpp index 09498dffa8ac20a63bb60b664f168c45a038aa4d..2f0e85c543546bf6704b563550d1d854a71205b9 100644 --- a/plugins/ets/runtime/ets_vm.cpp +++ b/plugins/ets/runtime/ets_vm.cpp @@ -400,7 +400,7 @@ Expected PandaEtsVM::InvokeEntrypointImpl(Method *entrypoin ObjectHeader *object_header = nullptr; if (entrypoint->GetNumArgs() == 1) { - object_header = reinterpret_cast(CreateArgumentsArray(args, coroutine->GetVM())); + object_header = CreateArgumentsArray(args, coroutine->GetVM()); } [[maybe_unused]] HandleScope scope(coroutine); diff --git a/plugins/ets/runtime/intrinsics/std_core_gc.cpp b/plugins/ets/runtime/intrinsics/std_core_gc.cpp index d0772db68edda3cfa9d8894524dec5a4711c015c..b97af7a51c732b25ed42eea8372c57e5b69584a9 100644 --- a/plugins/ets/runtime/intrinsics/std_core_gc.cpp +++ b/plugins/ets/runtime/intrinsics/std_core_gc.cpp @@ -244,10 +244,10 @@ template return nullptr; } auto *vm = thread->GetVM(); - if (vm->GetGC()->GetType() == mem::GCType::GEN_GC) { + if (!vm->GetGC()->IsPinningSupported()) { auto ctx = thread->GetLanguageContext(); ThrowException(ctx, thread, ctx.GetUnsupportedOperationExceptionClassDescriptor(), - utf::CStringAsMutf8("Object pinning does not support with Gen-GC")); + utf::CStringAsMutf8("Object pinning does not support with current GC")); return nullptr; } auto *array = ResArrayType::Create(length); @@ -334,21 +334,23 @@ extern "C" EtsInt StdGCGetObjectSpaceType(EtsObject *obj) extern "C" void StdGCPinObject(EtsObject *obj) { + auto *thread = ManagedThread::GetCurrent(); + ASSERT(thread != nullptr); if (obj == nullptr) { - auto *thread = ManagedThread::GetCurrent(); - ASSERT(thread != nullptr); auto ctx = thread->GetLanguageContext(); ThrowException(ctx, thread, ctx.GetNullPointerExceptionClassDescriptor(), utf::CStringAsMutf8("The value must be an object")); return; } - auto *vm = Thread::GetCurrent()->GetVM(); + auto *vm = thread->GetVM(); auto *obj_allocator = vm->GetHeapManager()->GetObjectAllocator().AsObjectAllocator(); auto *gc = vm->GetGC(); - if (gc->GetType() == mem::GCType::GEN_GC && obj_allocator->IsObjectInYoungSpace(obj->GetCoreType())) { - auto task = MakePandaUnique(GCTaskCause::YOUNG_GC_CAUSE); - gc->WaitForGCInManaged(*task); + if (!gc->IsPinningSupported()) { + auto ctx = thread->GetLanguageContext(); + ThrowException(ctx, thread, ctx.GetUnsupportedOperationExceptionClassDescriptor(), + utf::CStringAsMutf8("Object pinning does not support with current gc")); + return; } obj_allocator->PinObject(obj->GetCoreType()); } diff --git a/plugins/ets/runtime/mem/ets_reference.h b/plugins/ets/runtime/mem/ets_reference.h index 5e710d4f1832364e256853fe6809077c7958924d..d0f6542293e4e12e49837a3a1d6691b5a47a2138 100644 --- a/plugins/ets/runtime/mem/ets_reference.h +++ b/plugins/ets/runtime/mem/ets_reference.h @@ -113,7 +113,7 @@ public: EtsReference *NewEtsRef(EtsObject *obj, EtsReference::EtsObjectType obj_type) { - mem::Reference *ref = NewRef(EtsObject::ToCoreType(obj), obj_type); + mem::Reference *ref = NewRef(obj->GetCoreType(), obj_type); return EtsReference::CastFromReference(ref); } diff --git a/plugins/ets/runtime/types/ets_array.h b/plugins/ets/runtime/types/ets_array.h index 9bc6b493de2aa0bd21918b08039612a78b12128b..699b60f99c8ef98dfe2296cf11ee0cb1fd0944cb 100644 --- a/plugins/ets/runtime/types/ets_array.h +++ b/plugins/ets/runtime/types/ets_array.h @@ -25,7 +25,8 @@ namespace panda::ets { -class EtsArray { +// Private inheritance, because need to disallow implicit conversion to core type +class EtsArray : private coretypes::Array { public: EtsArray() = delete; ~EtsArray() = delete; @@ -87,6 +88,9 @@ public: NO_MOVE_SEMANTIC(EtsArray); protected: + // Use type alias to allow using into derived classes + using ObjectHeader = ::panda::ObjectHeader; + template static T *Create(EtsClass *array_class, uint32_t length, SpaceType space_type = SpaceType::SPACE_TYPE_OBJECT) { diff --git a/plugins/ets/runtime/types/ets_class.cpp b/plugins/ets/runtime/types/ets_class.cpp index 349ff6ed02f304a4a632a7e3cd6ef69c18ac22ab..02c534098df7b87366e4b2b8405c8f2c25fcfd63 100644 --- a/plugins/ets/runtime/types/ets_class.cpp +++ b/plugins/ets/runtime/types/ets_class.cpp @@ -134,7 +134,8 @@ EtsClass *EtsClass::GetPrimitiveClass(EtsString *name) break; } - if (primitive_name != nullptr && name->IsEqual(primitive_name)) { + // StringIndexOutOfBoundsException is not thrown by At method above, because index (0, 1) < length (>= 2) + if (primitive_name != nullptr && name->IsEqual(primitive_name)) { // SUPPRESS_CSA(alpha.core.WasteObjHeader) return EtsClass::FromRuntimeClass( Runtime::GetCurrent()->GetClassLinker()->GetExtension(SourceLanguage::ETS)->GetClassRoot(class_root)); } diff --git a/plugins/ets/runtime/types/ets_object.h b/plugins/ets/runtime/types/ets_object.h index aa715c129ebb3352af42e2207b69e21ca684b32b..8893cba7e55e5d4a8130e7a35904f87ecd5a6fcd 100644 --- a/plugins/ets/runtime/types/ets_object.h +++ b/plugins/ets/runtime/types/ets_object.h @@ -22,7 +22,8 @@ namespace panda::ets { -class EtsObject { +// Private inheritance, because need to disallow implicit conversion to core type +class EtsObject : private ObjectHeader { public: static EtsObject *Create(EtsClass *klass) { @@ -165,22 +166,12 @@ public: ObjectHeader *GetCoreType() const { - return reinterpret_cast(const_cast(&header_)); + return static_cast(const_cast(this)); } - static ObjectHeader *ToCoreType(EtsObject *obj) + static constexpr EtsObject *FromCoreType(ObjectHeader *object_header) { - return reinterpret_cast(obj); - } - - static EtsObject *FromCoreType(ObjectHeader *object_header) - { - return reinterpret_cast(object_header); - } - - static constexpr size_t GetHeaderOffset() - { - return MEMBER_OFFSET(EtsObject, header_); + return static_cast(object_header); } bool IsStringClass() @@ -196,15 +187,16 @@ public: EtsObject() = delete; ~EtsObject() = delete; -private: - ObjectHeader header_; +protected: + // Use type alias to allow using into derived classes + using ObjectHeader = ::panda::ObjectHeader; +private: NO_COPY_SEMANTIC(EtsObject); NO_MOVE_SEMANTIC(EtsObject); }; -// Object header field must be first and size of EtsObject must be equal size of ObjectHeader -static_assert(EtsObject::GetHeaderOffset() == 0); +// Size of EtsObject must be equal size of ObjectHeader static_assert(sizeof(EtsObject) == sizeof(ObjectHeader)); } // namespace panda::ets diff --git a/plugins/ets/runtime/types/ets_string.h b/plugins/ets/runtime/types/ets_string.h index 280f3a6bc6efc5c66ff97e9cb111a5e3d8e45d1f..94e83ee4c3ab606131817a8f9fc241eca18aab4b 100644 --- a/plugins/ets/runtime/types/ets_string.h +++ b/plugins/ets/runtime/types/ets_string.h @@ -24,7 +24,8 @@ namespace panda::ets { -class EtsString { +// Private inheritance, because need to disallow implicit conversion to core type +class EtsString : private coretypes::String { public: static EtsString *CreateFromMUtf8(const char *mutf8) { diff --git a/plugins/ets/stdlib/std/core/GC.ets b/plugins/ets/stdlib/std/core/GC.ets index 0b86949b2e063b9d34a220b6e0ca75d2c7186269..7da5b897ffba8fd9f89f3046e88453c858d5cab0 100644 --- a/plugins/ets/stdlib/std/core/GC.ets +++ b/plugins/ets/stdlib/std/core/GC.ets @@ -95,6 +95,7 @@ export class GC { * @see unpinObject * * @throws NullPointerException if object is null + * @throws UnsupportedOperationException if used GC does not support pinning */ public static native pinObject(obj: Object): void;