1 /** 2 * Copyright (c) 2021-2022 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.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 43 #include "ets_class_linker_extension.h" 44 #include "ets_vm.h" 45 46 namespace panda::ets { 47 48 class EtsLanguageContext : public LanguageContextBase { 49 public: 50 EtsLanguageContext() = default; 51 52 DEFAULT_COPY_SEMANTIC(EtsLanguageContext); 53 DEFAULT_MOVE_SEMANTIC(EtsLanguageContext); 54 55 ~EtsLanguageContext() override = default; 56 GetLanguage()57 panda_file::SourceLang GetLanguage() const override 58 { 59 return panda_file::SourceLang::ETS; 60 } 61 GetLanguageType()62 LangTypeT GetLanguageType() const override 63 { 64 return EtsLanguageConfig::LANG_TYPE; 65 } 66 GetObjectClassDescriptor()67 const uint8_t *GetObjectClassDescriptor() const override 68 { 69 return utf::CStringAsMutf8(panda_file_items::class_descriptors::OBJECT.data()); 70 } 71 GetClassClassDescriptor()72 const uint8_t *GetClassClassDescriptor() const override 73 { 74 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS.data()); 75 } 76 GetClassArrayClassDescriptor()77 const uint8_t *GetClassArrayClassDescriptor() const override 78 { 79 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS_ARRAY.data()); 80 } 81 GetStringArrayClassDescriptor()82 const uint8_t *GetStringArrayClassDescriptor() const override 83 { 84 return utf::CStringAsMutf8(panda_file_items::class_descriptors::STRING_ARRAY.data()); 85 } 86 GetCtorName()87 const uint8_t *GetCtorName() const override 88 { 89 return utf::CStringAsMutf8(panda_file_items::CTOR.data()); 90 } 91 GetCctorName()92 const uint8_t *GetCctorName() const override 93 { 94 return utf::CStringAsMutf8(panda_file_items::CCTOR.data()); 95 } 96 GetNullPointerExceptionClassDescriptor()97 const uint8_t *GetNullPointerExceptionClassDescriptor() const override 98 { 99 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NULL_POINTER_EXCEPTION.data()); 100 } 101 GetStackOverflowErrorClassDescriptor()102 const uint8_t *GetStackOverflowErrorClassDescriptor() const override 103 { 104 return utf::CStringAsMutf8(panda_file_items::class_descriptors::STACK_OVERFLOW_ERROR.data()); 105 } 106 GetArrayIndexOutOfBoundsExceptionClassDescriptor()107 const uint8_t *GetArrayIndexOutOfBoundsExceptionClassDescriptor() const override 108 { 109 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION.data()); 110 } 111 GetIndexOutOfBoundsExceptionClassDescriptor()112 const uint8_t *GetIndexOutOfBoundsExceptionClassDescriptor() const override 113 { 114 return utf::CStringAsMutf8(panda_file_items::class_descriptors::INDEX_OUT_OF_BOUNDS_EXCEPTION.data()); 115 } 116 GetIllegalStateExceptionClassDescriptor()117 const uint8_t *GetIllegalStateExceptionClassDescriptor() const override 118 { 119 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ILLEGAL_STATE_EXCEPTION.data()); 120 } GetNegativeArraySizeExceptionClassDescriptor()121 const uint8_t *GetNegativeArraySizeExceptionClassDescriptor() const override 122 { 123 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NEGATIVE_ARRAY_SIZE_EXCEPTION.data()); 124 } 125 GetStringIndexOutOfBoundsExceptionClassDescriptor()126 const uint8_t *GetStringIndexOutOfBoundsExceptionClassDescriptor() const override 127 { 128 return utf::CStringAsMutf8(panda_file_items::class_descriptors::STRING_INDEX_OUT_OF_BOUNDS_EXCEPTION.data()); 129 } 130 GetRangeErrorExceptionClassDescriptor()131 const uint8_t *GetRangeErrorExceptionClassDescriptor() const 132 { 133 return utf::CStringAsMutf8(panda_file_items::class_descriptors::RANGE_ERROR.data()); 134 } 135 GetArithmeticExceptionClassDescriptor()136 const uint8_t *GetArithmeticExceptionClassDescriptor() const override 137 { 138 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ARITHMETIC_EXCEPTION.data()); 139 } 140 GetClassCastExceptionClassDescriptor()141 const uint8_t *GetClassCastExceptionClassDescriptor() const override 142 { 143 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS_CAST_EXCEPTION.data()); 144 } 145 GetAbstractMethodErrorClassDescriptor()146 const uint8_t *GetAbstractMethodErrorClassDescriptor() const override 147 { 148 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ABSTRACT_METHOD_ERROR.data()); 149 } 150 GetArrayStoreExceptionClassDescriptor()151 const uint8_t *GetArrayStoreExceptionClassDescriptor() const override 152 { 153 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ARRAY_STORE_EXCEPTION.data()); 154 } 155 GetRuntimeExceptionClassDescriptor()156 const uint8_t *GetRuntimeExceptionClassDescriptor() const override 157 { 158 return utf::CStringAsMutf8(panda_file_items::class_descriptors::RUNTIME_EXCEPTION.data()); 159 } 160 GetFileNotFoundExceptionClassDescriptor()161 const uint8_t *GetFileNotFoundExceptionClassDescriptor() const override 162 { 163 return utf::CStringAsMutf8(panda_file_items::class_descriptors::FILE_NOT_FOUND_EXCEPTION.data()); 164 } 165 GetIOExceptionClassDescriptor()166 const uint8_t *GetIOExceptionClassDescriptor() const override 167 { 168 return utf::CStringAsMutf8(panda_file_items::class_descriptors::IO_EXCEPTION.data()); 169 } 170 GetIllegalArgumentExceptionClassDescriptor()171 const uint8_t *GetIllegalArgumentExceptionClassDescriptor() const override 172 { 173 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ILLEGAL_ARGUMENT_EXCEPTION.data()); 174 } 175 GetIllegalAccessExceptionClassDescriptor()176 const uint8_t *GetIllegalAccessExceptionClassDescriptor() const override 177 { 178 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ILLEGAL_ACCESS_EXCEPTION.data()); 179 } 180 GetOutOfMemoryErrorClassDescriptor()181 const uint8_t *GetOutOfMemoryErrorClassDescriptor() const override 182 { 183 return utf::CStringAsMutf8(panda_file_items::class_descriptors::OUT_OF_MEMORY_ERROR.data()); 184 } 185 GetNoClassDefFoundErrorDescriptor()186 const uint8_t *GetNoClassDefFoundErrorDescriptor() const override 187 { 188 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NO_CLASS_DEF_FOUND_ERROR.data()); 189 } 190 GetClassCircularityErrorDescriptor()191 const uint8_t *GetClassCircularityErrorDescriptor() const override 192 { 193 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS_CIRCULARITY_ERROR.data()); 194 } 195 GetNoSuchFieldErrorDescriptor()196 const uint8_t *GetNoSuchFieldErrorDescriptor() const override 197 { 198 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NO_SUCH_FIELD_ERROR.data()); 199 } 200 GetNoSuchMethodErrorDescriptor()201 const uint8_t *GetNoSuchMethodErrorDescriptor() const override 202 { 203 return utf::CStringAsMutf8(panda_file_items::class_descriptors::NO_SUCH_METHOD_ERROR.data()); 204 } 205 GetExceptionInInitializerErrorDescriptor()206 const uint8_t *GetExceptionInInitializerErrorDescriptor() const override 207 { 208 return utf::CStringAsMutf8(panda_file_items::class_descriptors::EXCEPTION_IN_INITIALIZER_ERROR.data()); 209 } 210 GetClassNotFoundExceptionDescriptor()211 const uint8_t *GetClassNotFoundExceptionDescriptor() const override 212 { 213 return utf::CStringAsMutf8(panda_file_items::class_descriptors::CLASS_NOT_FOUND_EXCEPTION.data()); 214 } 215 GetInstantiationErrorDescriptor()216 const uint8_t *GetInstantiationErrorDescriptor() const override 217 { 218 return utf::CStringAsMutf8(panda_file_items::class_descriptors::INSTANTIATION_ERROR.data()); 219 } 220 GetUnsupportedOperationExceptionClassDescriptor()221 const uint8_t *GetUnsupportedOperationExceptionClassDescriptor() const override 222 { 223 return utf::CStringAsMutf8(panda_file_items::class_descriptors::UNSUPPORTED_OPERATION_EXCEPTION.data()); 224 } 225 GetVerifyErrorClassDescriptor()226 const uint8_t *GetVerifyErrorClassDescriptor() const override 227 { 228 return utf::CStringAsMutf8(panda_file_items::class_descriptors::VERIFY_ERROR.data()); 229 } 230 GetErrorClassDescriptor()231 const uint8_t *GetErrorClassDescriptor() const override 232 { 233 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ERROR.data()); 234 } 235 GetIncompatibleClassChangeErrorDescriptor()236 const uint8_t *GetIncompatibleClassChangeErrorDescriptor() const override 237 { 238 return utf::CStringAsMutf8(panda_file_items::class_descriptors::INCOMPATIBLE_CLASS_CHANGE_ERROR.data()); 239 } 240 GetInitialTaggedValue()241 coretypes::TaggedValue GetInitialTaggedValue() const override 242 { 243 UNREACHABLE(); 244 return coretypes::TaggedValue(coretypes::TaggedValue::VALUE_UNDEFINED); 245 } 246 GetEncodedTaggedValue(int64_t value,int64_t tag)247 coretypes::TaggedValue GetEncodedTaggedValue([[maybe_unused]] int64_t value, 248 [[maybe_unused]] int64_t tag) const override 249 { 250 UNREACHABLE(); 251 return coretypes::TaggedValue(coretypes::TaggedValue::VALUE_UNDEFINED); 252 } 253 SetExceptionToVReg(interpreter::AccVRegister & vreg,ObjectHeader * obj)254 void SetExceptionToVReg(interpreter::AccVRegister &vreg, ObjectHeader *obj) const override 255 { 256 vreg.AsVRegRef().SetReference(obj); 257 } 258 IsCallableObject(ObjectHeader * obj)259 bool IsCallableObject([[maybe_unused]] ObjectHeader *obj) const override 260 { 261 UNREACHABLE(); 262 return false; 263 } 264 GetCallTarget(ObjectHeader * obj)265 Method *GetCallTarget([[maybe_unused]] ObjectHeader *obj) const override 266 { 267 UNREACHABLE(); 268 return nullptr; 269 } 270 GetReferenceErrorDescriptor()271 const uint8_t *GetReferenceErrorDescriptor() const override 272 { 273 UNREACHABLE(); 274 return nullptr; 275 } 276 GetTypedErrorDescriptor()277 const uint8_t *GetTypedErrorDescriptor() const override 278 { 279 UNREACHABLE(); 280 return nullptr; 281 } 282 GetIllegalMonitorStateExceptionDescriptor()283 const uint8_t *GetIllegalMonitorStateExceptionDescriptor() const override 284 { 285 return utf::CStringAsMutf8(panda_file_items::class_descriptors::ILLEGAL_MONITOR_STATE_EXCEPTION.data()); 286 } 287 288 void ThrowException(ManagedThread *thread, const uint8_t *mutf8Name, const uint8_t *mutf8Msg) const override; 289 290 PandaUniquePtr<ITableBuilder> CreateITableBuilder() const override; 291 292 PandaUniquePtr<VTableBuilder> CreateVTableBuilder() const override; 293 InitializeClass(ClassLinker * classLinker,ManagedThread * thread,Class * klass)294 bool InitializeClass(ClassLinker *classLinker, ManagedThread *thread, Class *klass) const override 295 { 296 return ClassInitializer<MT_MODE_TASK>::Initialize(classLinker, thread, klass); 297 } 298 CreateClassLinkerExtension()299 std::unique_ptr<ClassLinkerExtension> CreateClassLinkerExtension() const override 300 { 301 return std::make_unique<EtsClassLinkerExtension>(); 302 } 303 304 ets::PandaEtsVM *CreateVM(Runtime *runtime, const RuntimeOptions &options) const override; 305 306 mem::GC *CreateGC(mem::GCType gcType, mem::ObjectAllocatorBase *objectAllocator, 307 const mem::GCSettings &settings) const override; 308 309 void ThrowStackOverflowException(ManagedThread *thread) const override; 310 311 VerificationInitAPI GetVerificationInitAPI() const override; 312 GetVerificationTypeClass()313 const char *GetVerificationTypeClass() const override 314 { 315 return "std.core.Class"; 316 } 317 GetVerificationTypeObject()318 const char *GetVerificationTypeObject() const override 319 { 320 return "std.core.Object"; 321 } 322 GetVerificationTypeThrowable()323 const char *GetVerificationTypeThrowable() const override 324 { 325 return "std.core.Object"; 326 } 327 WrapClassInitializerException(ClassLinker * classLinker,ManagedThread * thread)328 void WrapClassInitializerException([[maybe_unused]] ClassLinker *classLinker, 329 [[maybe_unused]] ManagedThread *thread) const override 330 { 331 } 332 }; 333 334 } // namespace panda::ets 335 336 #endif // !PANDA_PLUGINS_ETS_RUNTIME_ETS_LANGUAGE_CONTEXT_H 337