1 /* 2 * Copyright (c) 2021 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 ECMASCRIPT_INTERPRETER_SLOW_RUNTIME_STUB_H 17 #define ECMASCRIPT_INTERPRETER_SLOW_RUNTIME_STUB_H 18 19 #include "ecmascript/jspandafile/program_object.h" 20 #include "ecmascript/js_tagged_value.h" 21 #include "ecmascript/js_thread.h" 22 23 namespace panda::ecmascript { 24 class GlobalEnv; 25 class JSArray; 26 27 class SlowRuntimeStub { 28 public: 29 static JSTaggedValue CallSpread(JSThread *thread, JSTaggedValue func, JSTaggedValue obj, JSTaggedValue array); 30 static JSTaggedValue Neg(JSThread *thread, JSTaggedValue value); 31 static JSTaggedValue AsyncFunctionEnter(JSThread *thread); 32 static JSTaggedValue ToNumber(JSThread *thread, JSTaggedValue value); 33 static JSTaggedValue ToNumeric(JSThread *thread, JSTaggedValue value); 34 static JSTaggedValue Not(JSThread *thread, JSTaggedValue value); 35 static JSTaggedValue Inc(JSThread *thread, JSTaggedValue value); 36 static JSTaggedValue Dec(JSThread *thread, JSTaggedValue value); 37 static void Throw(JSThread *thread, JSTaggedValue value); 38 static JSTaggedValue GetPropIterator(JSThread *thread, JSTaggedValue value); 39 static void ThrowConstAssignment(JSThread *thread, JSTaggedValue value); 40 static JSTaggedValue Add2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 41 static JSTaggedValue Sub2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 42 static JSTaggedValue Mul2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 43 static JSTaggedValue Div2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 44 static JSTaggedValue Mod2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 45 static JSTaggedValue Eq(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 46 static JSTaggedValue NotEq(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 47 static JSTaggedValue Less(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 48 static JSTaggedValue LessEq(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 49 static JSTaggedValue Greater(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 50 static JSTaggedValue GreaterEq(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 51 static JSTaggedValue Shl2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 52 static JSTaggedValue Shr2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 53 static JSTaggedValue Ashr2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 54 static JSTaggedValue And2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 55 static JSTaggedValue Or2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 56 static JSTaggedValue Xor2(JSThread *thread, JSTaggedValue left, JSTaggedValue right); 57 58 static JSTaggedValue ToJSTaggedValueWithInt32(JSThread *thread, JSTaggedValue value); 59 static JSTaggedValue ToJSTaggedValueWithUint32(JSThread *thread, JSTaggedValue value); 60 61 static JSTaggedValue DelObjProp(JSThread *thread, JSTaggedValue obj, JSTaggedValue prop); 62 static JSTaggedValue NewObjRange(JSThread *thread, JSTaggedValue func, JSTaggedValue newTarget, 63 uint16_t firstArgIdx, uint16_t length); 64 static JSTaggedValue CreateObjectWithExcludedKeys(JSThread *thread, uint16_t numKeys, JSTaggedValue objVal, 65 uint16_t firstArgRegIdx); 66 static JSTaggedValue Exp(JSThread *thread, JSTaggedValue base, JSTaggedValue exponent); 67 static JSTaggedValue IsIn(JSThread *thread, JSTaggedValue prop, JSTaggedValue obj); 68 static JSTaggedValue Instanceof(JSThread *thread, JSTaggedValue obj, JSTaggedValue target); 69 static JSTaggedValue InstanceofByHandler(JSThread *thread, JSTaggedValue target, JSTaggedValue object, 70 JSTaggedValue instOfHandler); 71 72 static JSTaggedValue NewLexicalEnv(JSThread *thread, uint16_t numVars); 73 static JSTaggedValue NewSendableEnv(JSThread *thread, uint16_t numVars); 74 static JSTaggedValue NewLexicalEnvWithName(JSThread *thread, uint16_t numVars, uint16_t scopeId); 75 static JSTaggedValue CreateIterResultObj(JSThread *thread, JSTaggedValue value, JSTaggedValue flag); 76 77 static JSTaggedValue CreateGeneratorObj(JSThread *thread, JSTaggedValue genFunc); 78 static JSTaggedValue SuspendGenerator(JSThread *thread, JSTaggedValue genObj, JSTaggedValue value); 79 static void SetGeneratorState(JSThread *thread, JSTaggedValue genObj, int32_t index); 80 static JSTaggedValue AsyncFunctionAwaitUncaught(JSThread *thread, JSTaggedValue asyncFuncObj, JSTaggedValue value); 81 static JSTaggedValue AsyncFunctionResolveOrReject(JSThread *thread, JSTaggedValue asyncFuncObj, JSTaggedValue value, 82 bool is_resolve); 83 84 static JSTaggedValue NewObjApply(JSThread *thread, JSTaggedValue func, JSTaggedValue array); 85 static void ThrowUndefinedIfHole(JSThread *thread, JSTaggedValue obj); 86 static void ThrowIfNotObject(JSThread *thread); 87 static void ThrowThrowNotExists(JSThread *thread); 88 static void ThrowPatternNonCoercible(JSThread *thread); 89 static JSTaggedValue ThrowIfSuperNotCorrectCall(JSThread *thread, uint16_t index, JSTaggedValue thisValue); 90 static void ThrowDeleteSuperProperty(JSThread *thread); 91 92 static JSTaggedValue StOwnByName(JSThread *thread, JSTaggedValue obj, JSTaggedValue prop, JSTaggedValue value); 93 static JSTaggedValue StOwnByNameWithNameSet(JSThread *thread, JSTaggedValue obj, JSTaggedValue prop, 94 JSTaggedValue value); 95 static JSTaggedValue StOwnByIndex(JSThread *thread, JSTaggedValue obj, uint32_t idx, JSTaggedValue value); 96 static JSTaggedValue StOwnByValue(JSThread *thread, JSTaggedValue obj, JSTaggedValue key, JSTaggedValue value); 97 static JSTaggedValue StOwnByValueWithNameSet(JSThread *thread, JSTaggedValue obj, JSTaggedValue key, 98 JSTaggedValue value); 99 static JSTaggedValue CreateEmptyArray(JSThread *thread, ObjectFactory *factory, JSHandle<GlobalEnv> globalEnv); 100 static JSTaggedValue CreateEmptyObject(JSThread *thread, ObjectFactory *factory, JSHandle<GlobalEnv> globalEnv); 101 static JSTaggedValue CreateObjectWithBuffer(JSThread *thread, ObjectFactory *factory, JSObject *literal); 102 static JSTaggedValue CreateObjectHavingMethod(JSThread *thread, ObjectFactory *factory, JSObject *literal, 103 JSTaggedValue env); 104 static JSTaggedValue SetObjectWithProto(JSThread *thread, JSTaggedValue proto, JSTaggedValue obj); 105 static JSTaggedValue CreateArrayWithBuffer(JSThread *thread, ObjectFactory *factory, JSArray *literal); 106 107 static JSTaggedValue GetTemplateObject(JSThread *thread, JSTaggedValue literal); 108 static JSTaggedValue GetNextPropName(JSThread *thread, JSTaggedValue iter); 109 static JSTaggedValue CopyDataProperties(JSThread *thread, JSTaggedValue dst, JSTaggedValue src); 110 111 static JSTaggedValue GetUnmapedArgs(JSThread *thread, JSTaggedType *sp, uint32_t actualNumArgs, uint32_t startIdx); 112 static JSTaggedValue CopyRestArgs(JSThread *thread, JSTaggedType *sp, uint32_t restNumArgs, uint32_t startIdx); 113 static JSTaggedValue GetIterator(JSThread *thread, JSTaggedValue obj); 114 static JSTaggedValue GetAsyncIterator(JSThread *thread, JSTaggedValue obj); 115 static JSTaggedValue IterNext(JSThread *thread, JSTaggedValue iter); 116 static JSTaggedValue CloseIterator(JSThread *thread, JSTaggedValue iter); 117 static void StModuleVar(JSThread *thread, JSTaggedValue key, JSTaggedValue value); 118 static JSTaggedValue LdModuleVar(JSThread *thread, JSTaggedValue key, bool inner); 119 static void StModuleVar(JSThread *thread, int32_t index, JSTaggedValue value); 120 static JSTaggedValue LdLocalModuleVar(JSThread *thread, int32_t index); 121 static JSTaggedValue LdExternalModuleVar(JSThread *thread, int32_t index); 122 static JSTaggedValue LdSendableExternalModuleVar(JSThread *thread, int32_t index, JSTaggedValue thisFunc); 123 static JSTaggedValue LdLazyExternalModuleVar(JSThread *thread, int32_t index, JSTaggedValue thisFunc); 124 static JSTaggedValue LdLazySendableExternalModuleVar(JSThread *thread, int32_t index, JSTaggedValue thisFunc); 125 static JSTaggedValue CreateRegExpWithLiteral(JSThread *thread, JSTaggedValue pattern, uint8_t flags); 126 static JSTaggedValue GetIteratorNext(JSThread *thread, JSTaggedValue obj, JSTaggedValue method); 127 128 static JSTaggedValue DefineGetterSetterByValue(JSThread *thread, JSTaggedValue obj, JSTaggedValue prop, 129 JSTaggedValue getter, JSTaggedValue setter, bool flag); 130 131 static JSTaggedValue LdObjByIndex(JSThread *thread, JSTaggedValue obj, uint32_t idx, bool callGetter, 132 JSTaggedValue receiver); 133 static JSTaggedValue StObjByIndex(JSThread *thread, JSTaggedValue obj, uint32_t idx, JSTaggedValue value); 134 static JSTaggedValue LdObjByName(JSThread *thread, JSTaggedValue obj, JSTaggedValue prop, bool callGetter, 135 JSTaggedValue receiver); 136 static JSTaggedValue StObjByName(JSThread *thread, JSTaggedValue obj, JSTaggedValue prop, JSTaggedValue value); 137 static JSTaggedValue LdObjByValue(JSThread *thread, JSTaggedValue obj, JSTaggedValue prop, bool callGetter, 138 JSTaggedValue receiver); 139 static JSTaggedValue StObjByValue(JSThread *thread, JSTaggedValue obj, JSTaggedValue prop, JSTaggedValue value); 140 static JSTaggedValue TryLdGlobalByNameFromGlobalProto(JSThread *thread, JSTaggedValue global, JSTaggedValue prop); 141 static JSTaggedValue TryStGlobalByName(JSThread *thread, JSTaggedValue prop); 142 static JSTaggedValue LdGlobalVarFromGlobalProto(JSThread *thread, JSTaggedValue global, JSTaggedValue prop); 143 static JSTaggedValue StGlobalVar(JSThread *thread, JSTaggedValue prop, JSTaggedValue value); 144 static JSTaggedValue StGlobalRecord(JSThread *thread, JSTaggedValue prop, JSTaggedValue value, bool isConst); 145 static JSTaggedValue LdGlobalRecord(JSThread *thread, JSTaggedValue key); 146 static JSTaggedValue TryUpdateGlobalRecord(JSThread *thread, JSTaggedValue prop, JSTaggedValue value); 147 static JSTaggedValue StArraySpread(JSThread *thread, JSTaggedValue dst, JSTaggedValue index, JSTaggedValue src); 148 149 static JSTaggedValue DefineFunc(JSThread *thread, JSTaggedValue constPool, uint16_t methodId, 150 JSTaggedValue module, uint16_t length, JSTaggedValue envHandle, 151 JSTaggedValue homeObject); 152 static JSTaggedValue GetSuperConstructor(JSThread *thread, JSTaggedValue ctor); 153 static JSTaggedValue SuperCall(JSThread *thread, JSTaggedValue func, JSTaggedValue newTarget, uint16_t firstVRegIdx, 154 uint16_t length); 155 static JSTaggedValue SuperCallSpread(JSThread *thread, JSTaggedValue func, JSTaggedValue newTarget, 156 JSTaggedValue array); 157 static JSTaggedValue SuperCallForwardAllArgs(JSThread *thread, JSTaggedType *sp, JSTaggedValue func, 158 JSTaggedValue newTarget, uint32_t restNumArgs, uint32_t startIdx); 159 static JSTaggedValue DynamicImport(JSThread *thread, JSTaggedValue specifier, JSTaggedValue func); 160 static JSTaggedValue DefineMethod(JSThread *thread, Method *method, JSTaggedValue homeObject, 161 uint16_t length, JSTaggedValue env, JSTaggedValue module); 162 static JSTaggedValue LdSendableClass(JSThread *thread, JSTaggedValue env, uint16_t level); 163 static JSTaggedValue LdSuperByValue(JSThread *thread, JSTaggedValue obj, JSTaggedValue key, JSTaggedValue thisFunc); 164 static JSTaggedValue StSuperByValue(JSThread *thread, JSTaggedValue obj, JSTaggedValue key, JSTaggedValue value, 165 JSTaggedValue thisFunc); 166 static JSTaggedValue NotifyInlineCache(JSThread *thread, JSFunction *function); 167 static JSTaggedValue ThrowReferenceError(JSThread *thread, JSTaggedValue prop, const char *desc); 168 169 static JSTaggedValue ResolveClass(JSThread *thread, JSTaggedValue ctor, TaggedArray *literal, JSTaggedValue base, 170 JSTaggedValue lexenv); 171 static JSTaggedValue CloneClassFromTemplate(JSThread *thread, JSTaggedValue ctor, JSTaggedValue base, 172 JSTaggedValue lexenv); 173 static JSTaggedValue CreateClassWithBuffer(JSThread *thread, JSTaggedValue base, 174 JSTaggedValue lexenv, JSTaggedValue constpool, 175 uint16_t methodId, uint16_t literalId, JSTaggedValue module, 176 JSTaggedValue length); 177 static JSTaggedValue CreateSharedClass(JSThread *thread, JSTaggedValue base, 178 JSTaggedValue constpool, uint16_t methodId, uint16_t literalId, 179 uint16_t length, JSTaggedValue module); 180 static JSTaggedValue SetClassConstructorLength(JSThread *thread, JSTaggedValue ctor, JSTaggedValue length); 181 static JSTaggedValue GetModuleNamespace(JSThread *thread, JSTaggedValue localName); 182 static JSTaggedValue GetModuleNamespace(JSThread *thread, int32_t index); 183 static JSTaggedValue LdBigInt(JSThread *thread, JSTaggedValue numberBigInt); 184 static JSTaggedValue ThrowTypeError(JSThread *thread, const char *message); 185 static JSTaggedValue SetClassInheritanceRelationship(JSThread *thread, JSTaggedValue ctor, JSTaggedValue base); 186 187 static JSTaggedValue AsyncGeneratorResolve(JSThread *thread, JSTaggedValue asyncFuncObj, 188 const JSTaggedValue value, JSTaggedValue flag); 189 static JSTaggedValue AsyncGeneratorReject(JSThread *thread, JSTaggedValue asyncFuncObj, 190 const JSTaggedValue value); 191 static JSTaggedValue CreateAsyncGeneratorObj(JSThread *thread, JSTaggedValue genFunc); 192 193 static JSTaggedValue LdPatchVar(JSThread *thread, uint32_t index); 194 static JSTaggedValue StPatchVar(JSThread *thread, uint32_t index, JSTaggedValue value); 195 196 static JSTaggedValue NotifyConcurrentResult(JSThread *thread, JSTaggedValue result, JSTaggedValue hint); 197 static JSTaggedValue DefineField(JSThread *thread, JSTaggedValue obj, JSTaggedValue propKey, JSTaggedValue value); 198 static JSTaggedValue CreatePrivateProperty(JSThread *thread, JSTaggedValue lexicalEnv, 199 uint32_t count, JSTaggedValue constpool, uint32_t literalId, JSTaggedValue module); 200 static JSTaggedValue DefinePrivateProperty(JSThread *thread, JSTaggedValue lexicalEnv, 201 uint32_t levelIndex, uint32_t slotIndex, JSTaggedValue obj, JSTaggedValue value); 202 static JSTaggedValue LdPrivateProperty(JSThread *thread, JSTaggedValue lexicalEnv, 203 uint32_t levelIndex, uint32_t slotIndex, JSTaggedValue obj); 204 static JSTaggedValue StPrivateProperty(JSThread *thread, JSTaggedValue lexicalEnv, 205 uint32_t levelIndex, uint32_t slotIndex, JSTaggedValue obj, JSTaggedValue value); 206 static JSTaggedValue TestIn(JSThread *thread, JSTaggedValue lexicalEnv, 207 uint32_t levelIndex, uint32_t slotIndex, JSTaggedValue obj); 208 static JSTaggedValue UpdateAOTHClass(JSThread *thread, JSTaggedValue jshclass, 209 JSTaggedValue newjshclass, JSTaggedValue key); 210 211 private: 212 static JSTaggedValue ThrowSyntaxError(JSThread *thread, const char *message); 213 static JSTaggedValue GetCallSpreadArgs(JSThread *thread, JSTaggedValue array); 214 }; 215 } // namespace panda::ecmascript 216 #endif // ECMASCRIPT_INTERPRETER_SLOW_RUNTIME_STUB_H 217