1 /** 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_PLUGINS_ETS_RUNTIME_ETS_LANGUAGE_CONTEXT_H_ 17 #define PANDA_PLUGINS_ETS_RUNTIME_ETS_LANGUAGE_CONTEXT_H_ 18 19 #include <libpandafile/include/source_lang_enum.h> 20 21 #include "libpandabase/macros.h" 22 #include "libpandabase/utils/utf.h" 23 #include "runtime/class_initializer.h" 24 #include "runtime/include/class_linker_extension.h" 25 #include "runtime/include/class_linker.h" 26 #include "runtime/include/class.h" 27 #include "runtime/include/coretypes/tagged_value.h" 28 #include "runtime/include/itable_builder.h" 29 #include "runtime/include/language_context.h" 30 #include "runtime/include/managed_thread.h" 31 #include "runtime/include/mem/panda_smart_pointers.h" 32 #include "runtime/include/method.h" 33 #include "runtime/include/vtable_builder_interface.h" 34 #include "runtime/interpreter/acc_vregister.h" 35 #include "runtime/include/runtime.h" 36 #include "runtime/include/runtime_options.h" 37 #include "runtime/mem/gc/gc.h" 38 #include "runtime/mem/gc/gc_types.h" 39 #include "runtime/mem/gc/gc_settings.h" 40 #include "runtime/include/mem/allocator.h" 41 #include "plugins/ets/runtime/ets_panda_file_items.h" 42 #include "plugins/ets/runtime/tooling/pt_ets_extension.h" 43 44 #include "ets_class_linker_extension.h" 45 #include "ets_vm.h" 46 47 namespace ark::ets { 48 49 class EtsLanguageContext : public LanguageContextBase { 50 public: 51 EtsLanguageContext() = default; 52 53 DEFAULT_COPY_SEMANTIC(EtsLanguageContext); 54 DEFAULT_MOVE_SEMANTIC(EtsLanguageContext); 55 56 ~EtsLanguageContext() override = default; 57 GetLanguage()58 panda_file::SourceLang GetLanguage() const override 59 { 60 return panda_file::SourceLang::ETS; 61 } 62 GetLanguageType()63 LangTypeT GetLanguageType() const override 64 { 65 return EtsLanguageConfig::LANG_TYPE; 66 } 67 GetObjectClassDescriptor()68 const uint8_t *GetObjectClassDescriptor() const override 69 { 70 return utf::CStringAsMutf8(panda_file_items::class_descriptors::OBJECT.data()); 71 } 72 GetClassClassDescriptor()73 const uint8_t *GetClassClassDescriptor() const override 74 { 75 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS.data()); 76 } 77 GetClassArrayClassDescriptor()78 const uint8_t *GetClassArrayClassDescriptor() const override 79 { 80 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS_ARRAY.data()); 81 } 82 GetStringArrayClassDescriptor()83 const uint8_t *GetStringArrayClassDescriptor() const override 84 { 85 return utf::CStringAsMutf8(panda_file_items::class_descriptors::STRING_ARRAY.data()); 86 } 87 GetCtorName()88 const uint8_t *GetCtorName() const override 89 { 90 return utf::CStringAsMutf8(panda_file_items::CTOR.data()); 91 } 92 GetCctorName()93 const uint8_t *GetCctorName() const override 94 { 95 return utf::CStringAsMutf8(panda_file_items::CCTOR.data()); 96 } 97 GetNullPointerExceptionClassDescriptor()98 const uint8_t *GetNullPointerExceptionClassDescriptor() const override 99 { 100 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NULL_POINTER_ERROR.data()); 101 } 102 GetStackOverflowErrorClassDescriptor()103 const uint8_t *GetStackOverflowErrorClassDescriptor() const override 104 { 105 return utf::CStringAsMutf8(panda_file_items::class_descriptors::STACK_OVERFLOW_ERROR.data()); 106 } 107 GetArrayIndexOutOfBoundsExceptionClassDescriptor()108 const uint8_t *GetArrayIndexOutOfBoundsExceptionClassDescriptor() const override 109 { 110 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ARRAY_INDEX_OUT_OF_BOUNDS_ERROR.data()); 111 } 112 GetIndexOutOfBoundsExceptionClassDescriptor()113 const uint8_t *GetIndexOutOfBoundsExceptionClassDescriptor() const override 114 { 115 return utf::CStringAsMutf8(panda_file_items::class_descriptors::INDEX_OUT_OF_BOUNDS_ERROR.data()); 116 } 117 GetIllegalStateExceptionClassDescriptor()118 const uint8_t *GetIllegalStateExceptionClassDescriptor() const override 119 { 120 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ILLEGAL_STATE_EXCEPTION.data()); 121 } GetNegativeArraySizeExceptionClassDescriptor()122 const uint8_t *GetNegativeArraySizeExceptionClassDescriptor() const override 123 { 124 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NEGATIVE_ARRAY_SIZE_ERROR.data()); 125 } 126 GetStringIndexOutOfBoundsExceptionClassDescriptor()127 const uint8_t *GetStringIndexOutOfBoundsExceptionClassDescriptor() const override 128 { 129 return utf::CStringAsMutf8(panda_file_items::class_descriptors::STRING_INDEX_OUT_OF_BOUNDS_ERROR.data()); 130 } 131 GetRangeErrorExceptionClassDescriptor()132 const uint8_t *GetRangeErrorExceptionClassDescriptor() const 133 { 134 return utf::CStringAsMutf8(panda_file_items::class_descriptors::RANGE_ERROR.data()); 135 } 136 GetArithmeticExceptionClassDescriptor()137 const uint8_t *GetArithmeticExceptionClassDescriptor() const override 138 { 139 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ARITHMETIC_ERROR.data()); 140 } 141 GetClassCastExceptionClassDescriptor()142 const uint8_t *GetClassCastExceptionClassDescriptor() const override 143 { 144 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS_CAST_ERROR.data()); 145 } 146 GetAbstractMethodErrorClassDescriptor()147 const uint8_t *GetAbstractMethodErrorClassDescriptor() const override 148 { 149 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ABSTRACT_METHOD_ERROR.data()); 150 } 151 GetArrayStoreExceptionClassDescriptor()152 const uint8_t *GetArrayStoreExceptionClassDescriptor() const override 153 { 154 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ARRAY_STORE_ERROR.data()); 155 } 156 GetRuntimeExceptionClassDescriptor()157 const uint8_t *GetRuntimeExceptionClassDescriptor() const override 158 { 159 return utf::CStringAsMutf8(panda_file_items::class_descriptors::RUNTIME_EXCEPTION.data()); 160 } 161 GetFileNotFoundExceptionClassDescriptor()162 const uint8_t *GetFileNotFoundExceptionClassDescriptor() const override 163 { 164 return utf::CStringAsMutf8(panda_file_items::class_descriptors::FILE_NOT_FOUND_EXCEPTION.data()); 165 } 166 GetIOExceptionClassDescriptor()167 const uint8_t *GetIOExceptionClassDescriptor() const override 168 { 169 return utf::CStringAsMutf8(panda_file_items::class_descriptors::IO_EXCEPTION.data()); 170 } 171 GetIllegalArgumentExceptionClassDescriptor()172 const uint8_t *GetIllegalArgumentExceptionClassDescriptor() const override 173 { 174 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ILLEGAL_ARGUMENT_EXCEPTION.data()); 175 } 176 GetIllegalAccessExceptionClassDescriptor()177 const uint8_t *GetIllegalAccessExceptionClassDescriptor() const override 178 { 179 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ILLEGAL_ACCESS_EXCEPTION.data()); 180 } 181 GetOutOfMemoryErrorClassDescriptor()182 const uint8_t *GetOutOfMemoryErrorClassDescriptor() const override 183 { 184 return utf::CStringAsMutf8(panda_file_items::class_descriptors::OUT_OF_MEMORY_ERROR.data()); 185 } 186 GetNoClassDefFoundErrorDescriptor()187 const uint8_t *GetNoClassDefFoundErrorDescriptor() const override 188 { 189 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NO_CLASS_DEF_FOUND_ERROR.data()); 190 } 191 GetClassCircularityErrorDescriptor()192 const uint8_t *GetClassCircularityErrorDescriptor() const override 193 { 194 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS_CIRCULARITY_ERROR.data()); 195 } 196 GetNoSuchFieldErrorDescriptor()197 const uint8_t *GetNoSuchFieldErrorDescriptor() const override 198 { 199 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NO_SUCH_FIELD_ERROR.data()); 200 } 201 GetNoSuchMethodErrorDescriptor()202 const uint8_t *GetNoSuchMethodErrorDescriptor() const override 203 { 204 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NO_SUCH_METHOD_ERROR.data()); 205 } 206 GetExceptionInInitializerErrorDescriptor()207 const uint8_t *GetExceptionInInitializerErrorDescriptor() const override 208 { 209 return utf::CStringAsMutf8(panda_file_items::class_descriptors::EXCEPTION_IN_INITIALIZER_ERROR.data()); 210 } 211 GetClassNotFoundExceptionDescriptor()212 const uint8_t *GetClassNotFoundExceptionDescriptor() const override 213 { 214 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS_NOT_FOUND_EXCEPTION.data()); 215 } 216 GetInstantiationErrorDescriptor()217 const uint8_t *GetInstantiationErrorDescriptor() const override 218 { 219 return utf::CStringAsMutf8(panda_file_items::class_descriptors::INSTANTIATION_ERROR.data()); 220 } 221 GetUnsupportedOperationExceptionClassDescriptor()222 const uint8_t *GetUnsupportedOperationExceptionClassDescriptor() const override 223 { 224 return utf::CStringAsMutf8(panda_file_items::class_descriptors::UNSUPPORTED_OPERATION_EXCEPTION.data()); 225 } 226 GetVerifyErrorClassDescriptor()227 const uint8_t *GetVerifyErrorClassDescriptor() const override 228 { 229 return utf::CStringAsMutf8(panda_file_items::class_descriptors::VERIFY_ERROR.data()); 230 } 231 GetErrorClassDescriptor()232 const uint8_t *GetErrorClassDescriptor() const override 233 { 234 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ERROR.data()); 235 } 236 GetIncompatibleClassChangeErrorDescriptor()237 const uint8_t *GetIncompatibleClassChangeErrorDescriptor() const override 238 { 239 return utf::CStringAsMutf8(panda_file_items::class_descriptors::INCOMPATIBLE_CLASS_CHANGE_ERROR.data()); 240 } 241 GetInitialTaggedValue()242 coretypes::TaggedValue GetInitialTaggedValue() const override 243 { 244 UNREACHABLE(); 245 return coretypes::TaggedValue(coretypes::TaggedValue::VALUE_UNDEFINED); 246 } 247 GetEncodedTaggedValue(int64_t value,int64_t tag)248 coretypes::TaggedValue GetEncodedTaggedValue([[maybe_unused]] int64_t value, 249 [[maybe_unused]] int64_t tag) const override 250 { 251 UNREACHABLE(); 252 return coretypes::TaggedValue(coretypes::TaggedValue::VALUE_UNDEFINED); 253 } 254 SetExceptionToVReg(interpreter::AccVRegister & vreg,ObjectHeader * obj)255 void SetExceptionToVReg(interpreter::AccVRegister &vreg, ObjectHeader *obj) const override 256 { 257 vreg.AsVRegRef().SetReference(obj); 258 } 259 IsCallableObject(ObjectHeader * obj)260 bool IsCallableObject([[maybe_unused]] ObjectHeader *obj) const override 261 { 262 UNREACHABLE(); 263 return false; 264 } 265 GetCallTarget(ObjectHeader * obj)266 Method *GetCallTarget([[maybe_unused]] ObjectHeader *obj) const override 267 { 268 UNREACHABLE(); 269 return nullptr; 270 } 271 GetReferenceErrorDescriptor()272 const uint8_t *GetReferenceErrorDescriptor() const override 273 { 274 UNREACHABLE(); 275 return nullptr; 276 } 277 GetTypedErrorDescriptor()278 const uint8_t *GetTypedErrorDescriptor() const override 279 { 280 UNREACHABLE(); 281 return nullptr; 282 } 283 GetIllegalMonitorStateExceptionDescriptor()284 const uint8_t *GetIllegalMonitorStateExceptionDescriptor() const override 285 { 286 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ILLEGAL_MONITOR_STATE_EXCEPTION.data()); 287 } 288 289 void ThrowException(ManagedThread *thread, const uint8_t *mutf8Name, const uint8_t *mutf8Msg) const override; 290 291 PandaUniquePtr<ITableBuilder> CreateITableBuilder(ClassLinkerErrorHandler *errHandler) const override; 292 293 PandaUniquePtr<VTableBuilder> CreateVTableBuilder(ClassLinkerErrorHandler *errHandler) const override; 294 InitializeClass(ClassLinker * classLinker,ManagedThread * thread,Class * klass)295 bool InitializeClass(ClassLinker *classLinker, ManagedThread *thread, Class *klass) const override 296 { 297 return ClassInitializer<MT_MODE_TASK>::Initialize(classLinker, thread, klass); 298 } 299 CreateClassLinkerExtension()300 std::unique_ptr<ClassLinkerExtension> CreateClassLinkerExtension() const override 301 { 302 return std::make_unique<EtsClassLinkerExtension>(); 303 } 304 305 ets::PandaEtsVM *CreateVM(Runtime *runtime, const RuntimeOptions &options) const override; 306 307 mem::GC *CreateGC(mem::GCType gcType, mem::ObjectAllocatorBase *objectAllocator, 308 const mem::GCSettings &settings) const override; 309 310 void ThrowStackOverflowException(ManagedThread *thread) const override; 311 312 VerificationInitAPI GetVerificationInitAPI() const override; 313 GetVerificationTypeClass()314 const char *GetVerificationTypeClass() const override 315 { 316 return "std.core.Class"; 317 } 318 GetVerificationTypeObject()319 const char *GetVerificationTypeObject() const override 320 { 321 return "std.core.Object"; 322 } 323 GetVerificationTypeThrowable()324 const char *GetVerificationTypeThrowable() const override 325 { 326 return "std.core.Object"; 327 } 328 WrapClassInitializerException(ClassLinker * classLinker,ManagedThread * thread)329 void WrapClassInitializerException([[maybe_unused]] ClassLinker *classLinker, 330 [[maybe_unused]] ManagedThread *thread) const override 331 { 332 } 333 CreatePtLangExt()334 std::unique_ptr<tooling::PtLangExt> CreatePtLangExt() const override 335 { 336 return std::make_unique<PtEtsExtension>(); 337 } 338 }; 339 340 } // namespace ark::ets 341 342 #endif // !PANDA_PLUGINS_ETS_RUNTIME_ETS_LANGUAGE_CONTEXT_H_ 343