gdscript_vm.cpp 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492
  1. /*************************************************************************/
  2. /* gdscript_vm.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_function.h"
  31. #include "core/core_string_names.h"
  32. #include "core/os/os.h"
  33. #include "gdscript.h"
  34. #include "gdscript_lambda_callable.h"
  35. Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, Variant *p_stack, String &r_error) const {
  36. int address = p_address & ADDR_MASK;
  37. //sequential table (jump table generated by compiler)
  38. switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) {
  39. case ADDR_TYPE_STACK: {
  40. #ifdef DEBUG_ENABLED
  41. ERR_FAIL_INDEX_V(address, _stack_size, nullptr);
  42. #endif
  43. return &p_stack[address];
  44. } break;
  45. case ADDR_TYPE_CONSTANT: {
  46. #ifdef DEBUG_ENABLED
  47. ERR_FAIL_INDEX_V(address, _constant_count, nullptr);
  48. #endif
  49. return &_constants_ptr[address];
  50. } break;
  51. case ADDR_TYPE_MEMBER: {
  52. #ifdef DEBUG_ENABLED
  53. if (unlikely(!p_instance)) {
  54. r_error = "Cannot access member without instance.";
  55. return nullptr;
  56. }
  57. #endif
  58. //member indexing is O(1)
  59. return &p_instance->members.write[address];
  60. } break;
  61. }
  62. ERR_FAIL_V_MSG(nullptr, "Bad code! (unknown addressing mode).");
  63. return nullptr;
  64. }
  65. #ifdef DEBUG_ENABLED
  66. static String _get_script_name(const Ref<Script> p_script) {
  67. Ref<GDScript> gdscript = p_script;
  68. if (gdscript.is_valid()) {
  69. return gdscript->get_script_class_name();
  70. } else if (p_script->get_name().is_empty()) {
  71. return p_script->get_path().get_file();
  72. } else {
  73. return p_script->get_name();
  74. }
  75. }
  76. static String _get_var_type(const Variant *p_var) {
  77. String basestr;
  78. if (p_var->get_type() == Variant::OBJECT) {
  79. bool was_freed;
  80. Object *bobj = p_var->get_validated_object_with_check(was_freed);
  81. if (!bobj) {
  82. if (was_freed) {
  83. basestr = "previously freed";
  84. } else {
  85. basestr = "null instance";
  86. }
  87. } else {
  88. if (bobj->is_class_ptr(GDScriptNativeClass::get_class_ptr_static())) {
  89. basestr = Object::cast_to<GDScriptNativeClass>(bobj)->get_name();
  90. } else {
  91. basestr = bobj->get_class();
  92. if (bobj->get_script_instance()) {
  93. basestr += " (" + _get_script_name(bobj->get_script_instance()->get_script()) + ")";
  94. }
  95. }
  96. }
  97. } else {
  98. if (p_var->get_type() == Variant::ARRAY) {
  99. basestr = "Array";
  100. const Array *p_array = VariantInternal::get_array(p_var);
  101. Variant::Type builtin_type = (Variant::Type)p_array->get_typed_builtin();
  102. StringName native_type = p_array->get_typed_class_name();
  103. Ref<Script> script_type = p_array->get_typed_script();
  104. if (script_type.is_valid() && script_type->is_valid()) {
  105. basestr += "[" + _get_script_name(script_type) + "]";
  106. } else if (native_type != StringName()) {
  107. basestr += "[" + native_type.operator String() + "]";
  108. } else if (builtin_type != Variant::NIL) {
  109. basestr += "[" + Variant::get_type_name(builtin_type) + "]";
  110. }
  111. } else {
  112. basestr = Variant::get_type_name(p_var->get_type());
  113. }
  114. }
  115. return basestr;
  116. }
  117. #endif // DEBUG_ENABLED
  118. Variant GDScriptFunction::_get_default_variant_for_data_type(const GDScriptDataType &p_data_type) {
  119. if (p_data_type.kind == GDScriptDataType::BUILTIN) {
  120. if (p_data_type.builtin_type == Variant::ARRAY) {
  121. Array array;
  122. // Typed array.
  123. if (p_data_type.has_container_element_type()) {
  124. const GDScriptDataType &element_type = p_data_type.get_container_element_type();
  125. array.set_typed(
  126. element_type.kind == GDScriptDataType::BUILTIN ? element_type.builtin_type : Variant::OBJECT,
  127. element_type.native_type,
  128. element_type.script_type);
  129. }
  130. return array;
  131. } else {
  132. Callable::CallError ce;
  133. Variant variant;
  134. Variant::construct(p_data_type.builtin_type, variant, nullptr, 0, ce);
  135. ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, Variant());
  136. return variant;
  137. }
  138. }
  139. return Variant();
  140. }
  141. String GDScriptFunction::_get_call_error(const Callable::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  142. String err_text;
  143. if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  144. int errorarg = p_err.argument;
  145. // Handle the Object to Object case separately as we don't have further class details.
  146. #ifdef DEBUG_ENABLED
  147. if (p_err.expected == Variant::OBJECT && argptrs[errorarg]->get_type() == p_err.expected) {
  148. err_text = "Invalid type in " + p_where + ". The Object-derived class of argument " + itos(errorarg + 1) + " (" + _get_var_type(argptrs[errorarg]) + ") is not a subclass of the expected argument class.";
  149. } else
  150. #endif // DEBUG_ENABLED
  151. {
  152. err_text = "Invalid type in " + p_where + ". Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(p_err.expected)) + ".";
  153. }
  154. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
  155. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  156. } else if (p_err.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
  157. err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments.";
  158. } else if (p_err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  159. err_text = "Invalid call. Nonexistent " + p_where + ".";
  160. } else if (p_err.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
  161. err_text = "Attempt to call " + p_where + " on a null instance.";
  162. } else {
  163. err_text = "Bug, call error: #" + itos(p_err.error);
  164. }
  165. return err_text;
  166. }
  167. void (*type_init_function_table[])(Variant *) = {
  168. nullptr, // NIL (shouldn't be called).
  169. &VariantInitializer<bool>::init, // BOOL.
  170. &VariantInitializer<int64_t>::init, // INT.
  171. &VariantInitializer<double>::init, // FLOAT.
  172. &VariantInitializer<String>::init, // STRING.
  173. &VariantInitializer<Vector2>::init, // VECTOR2.
  174. &VariantInitializer<Vector2i>::init, // VECTOR2I.
  175. &VariantInitializer<Rect2>::init, // RECT2.
  176. &VariantInitializer<Rect2i>::init, // RECT2I.
  177. &VariantInitializer<Vector3>::init, // VECTOR3.
  178. &VariantInitializer<Vector3i>::init, // VECTOR3I.
  179. &VariantInitializer<Transform2D>::init, // TRANSFORM2D.
  180. &VariantInitializer<Plane>::init, // PLANE.
  181. &VariantInitializer<Quaternion>::init, // QUATERNION.
  182. &VariantInitializer<AABB>::init, // AABB.
  183. &VariantInitializer<Basis>::init, // BASIS.
  184. &VariantInitializer<Transform3D>::init, // TRANSFORM3D.
  185. &VariantInitializer<Color>::init, // COLOR.
  186. &VariantInitializer<StringName>::init, // STRING_NAME.
  187. &VariantInitializer<NodePath>::init, // NODE_PATH.
  188. &VariantInitializer<RID>::init, // RID.
  189. &VariantInitializer<Object *>::init, // OBJECT.
  190. &VariantInitializer<Callable>::init, // CALLABLE.
  191. &VariantInitializer<Signal>::init, // SIGNAL.
  192. &VariantInitializer<Dictionary>::init, // DICTIONARY.
  193. &VariantInitializer<Array>::init, // ARRAY.
  194. &VariantInitializer<PackedByteArray>::init, // PACKED_BYTE_ARRAY.
  195. &VariantInitializer<PackedInt32Array>::init, // PACKED_INT32_ARRAY.
  196. &VariantInitializer<PackedInt64Array>::init, // PACKED_INT64_ARRAY.
  197. &VariantInitializer<PackedFloat32Array>::init, // PACKED_FLOAT32_ARRAY.
  198. &VariantInitializer<PackedFloat64Array>::init, // PACKED_FLOAT64_ARRAY.
  199. &VariantInitializer<PackedStringArray>::init, // PACKED_STRING_ARRAY.
  200. &VariantInitializer<PackedVector2Array>::init, // PACKED_VECTOR2_ARRAY.
  201. &VariantInitializer<PackedVector3Array>::init, // PACKED_VECTOR3_ARRAY.
  202. &VariantInitializer<PackedColorArray>::init, // PACKED_COLOR_ARRAY.
  203. };
  204. #if defined(__GNUC__)
  205. #define OPCODES_TABLE \
  206. static const void *switch_table_ops[] = { \
  207. &&OPCODE_OPERATOR, \
  208. &&OPCODE_OPERATOR_VALIDATED, \
  209. &&OPCODE_EXTENDS_TEST, \
  210. &&OPCODE_IS_BUILTIN, \
  211. &&OPCODE_SET_KEYED, \
  212. &&OPCODE_SET_KEYED_VALIDATED, \
  213. &&OPCODE_SET_INDEXED_VALIDATED, \
  214. &&OPCODE_GET_KEYED, \
  215. &&OPCODE_GET_KEYED_VALIDATED, \
  216. &&OPCODE_GET_INDEXED_VALIDATED, \
  217. &&OPCODE_SET_NAMED, \
  218. &&OPCODE_SET_NAMED_VALIDATED, \
  219. &&OPCODE_GET_NAMED, \
  220. &&OPCODE_GET_NAMED_VALIDATED, \
  221. &&OPCODE_SET_MEMBER, \
  222. &&OPCODE_GET_MEMBER, \
  223. &&OPCODE_ASSIGN, \
  224. &&OPCODE_ASSIGN_TRUE, \
  225. &&OPCODE_ASSIGN_FALSE, \
  226. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  227. &&OPCODE_ASSIGN_TYPED_ARRAY, \
  228. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  229. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  230. &&OPCODE_CAST_TO_BUILTIN, \
  231. &&OPCODE_CAST_TO_NATIVE, \
  232. &&OPCODE_CAST_TO_SCRIPT, \
  233. &&OPCODE_CONSTRUCT, \
  234. &&OPCODE_CONSTRUCT_VALIDATED, \
  235. &&OPCODE_CONSTRUCT_ARRAY, \
  236. &&OPCODE_CONSTRUCT_TYPED_ARRAY, \
  237. &&OPCODE_CONSTRUCT_DICTIONARY, \
  238. &&OPCODE_CALL, \
  239. &&OPCODE_CALL_RETURN, \
  240. &&OPCODE_CALL_ASYNC, \
  241. &&OPCODE_CALL_UTILITY, \
  242. &&OPCODE_CALL_UTILITY_VALIDATED, \
  243. &&OPCODE_CALL_GDSCRIPT_UTILITY, \
  244. &&OPCODE_CALL_BUILTIN_TYPE_VALIDATED, \
  245. &&OPCODE_CALL_SELF_BASE, \
  246. &&OPCODE_CALL_METHOD_BIND, \
  247. &&OPCODE_CALL_METHOD_BIND_RET, \
  248. &&OPCODE_CALL_BUILTIN_STATIC, \
  249. &&OPCODE_CALL_NATIVE_STATIC, \
  250. &&OPCODE_CALL_PTRCALL_NO_RETURN, \
  251. &&OPCODE_CALL_PTRCALL_BOOL, \
  252. &&OPCODE_CALL_PTRCALL_INT, \
  253. &&OPCODE_CALL_PTRCALL_FLOAT, \
  254. &&OPCODE_CALL_PTRCALL_STRING, \
  255. &&OPCODE_CALL_PTRCALL_VECTOR2, \
  256. &&OPCODE_CALL_PTRCALL_VECTOR2I, \
  257. &&OPCODE_CALL_PTRCALL_RECT2, \
  258. &&OPCODE_CALL_PTRCALL_RECT2I, \
  259. &&OPCODE_CALL_PTRCALL_VECTOR3, \
  260. &&OPCODE_CALL_PTRCALL_VECTOR3I, \
  261. &&OPCODE_CALL_PTRCALL_TRANSFORM2D, \
  262. &&OPCODE_CALL_PTRCALL_PLANE, \
  263. &&OPCODE_CALL_PTRCALL_QUATERNION, \
  264. &&OPCODE_CALL_PTRCALL_AABB, \
  265. &&OPCODE_CALL_PTRCALL_BASIS, \
  266. &&OPCODE_CALL_PTRCALL_TRANSFORM3D, \
  267. &&OPCODE_CALL_PTRCALL_COLOR, \
  268. &&OPCODE_CALL_PTRCALL_STRING_NAME, \
  269. &&OPCODE_CALL_PTRCALL_NODE_PATH, \
  270. &&OPCODE_CALL_PTRCALL_RID, \
  271. &&OPCODE_CALL_PTRCALL_OBJECT, \
  272. &&OPCODE_CALL_PTRCALL_CALLABLE, \
  273. &&OPCODE_CALL_PTRCALL_SIGNAL, \
  274. &&OPCODE_CALL_PTRCALL_DICTIONARY, \
  275. &&OPCODE_CALL_PTRCALL_ARRAY, \
  276. &&OPCODE_CALL_PTRCALL_PACKED_BYTE_ARRAY, \
  277. &&OPCODE_CALL_PTRCALL_PACKED_INT32_ARRAY, \
  278. &&OPCODE_CALL_PTRCALL_PACKED_INT64_ARRAY, \
  279. &&OPCODE_CALL_PTRCALL_PACKED_FLOAT32_ARRAY, \
  280. &&OPCODE_CALL_PTRCALL_PACKED_FLOAT64_ARRAY, \
  281. &&OPCODE_CALL_PTRCALL_PACKED_STRING_ARRAY, \
  282. &&OPCODE_CALL_PTRCALL_PACKED_VECTOR2_ARRAY, \
  283. &&OPCODE_CALL_PTRCALL_PACKED_VECTOR3_ARRAY, \
  284. &&OPCODE_CALL_PTRCALL_PACKED_COLOR_ARRAY, \
  285. &&OPCODE_AWAIT, \
  286. &&OPCODE_AWAIT_RESUME, \
  287. &&OPCODE_CREATE_LAMBDA, \
  288. &&OPCODE_CREATE_SELF_LAMBDA, \
  289. &&OPCODE_JUMP, \
  290. &&OPCODE_JUMP_IF, \
  291. &&OPCODE_JUMP_IF_NOT, \
  292. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  293. &&OPCODE_JUMP_IF_SHARED, \
  294. &&OPCODE_RETURN, \
  295. &&OPCODE_RETURN_TYPED_BUILTIN, \
  296. &&OPCODE_RETURN_TYPED_ARRAY, \
  297. &&OPCODE_RETURN_TYPED_NATIVE, \
  298. &&OPCODE_RETURN_TYPED_SCRIPT, \
  299. &&OPCODE_ITERATE_BEGIN, \
  300. &&OPCODE_ITERATE_BEGIN_INT, \
  301. &&OPCODE_ITERATE_BEGIN_FLOAT, \
  302. &&OPCODE_ITERATE_BEGIN_VECTOR2, \
  303. &&OPCODE_ITERATE_BEGIN_VECTOR2I, \
  304. &&OPCODE_ITERATE_BEGIN_VECTOR3, \
  305. &&OPCODE_ITERATE_BEGIN_VECTOR3I, \
  306. &&OPCODE_ITERATE_BEGIN_STRING, \
  307. &&OPCODE_ITERATE_BEGIN_DICTIONARY, \
  308. &&OPCODE_ITERATE_BEGIN_ARRAY, \
  309. &&OPCODE_ITERATE_BEGIN_PACKED_BYTE_ARRAY, \
  310. &&OPCODE_ITERATE_BEGIN_PACKED_INT32_ARRAY, \
  311. &&OPCODE_ITERATE_BEGIN_PACKED_INT64_ARRAY, \
  312. &&OPCODE_ITERATE_BEGIN_PACKED_FLOAT32_ARRAY, \
  313. &&OPCODE_ITERATE_BEGIN_PACKED_FLOAT64_ARRAY, \
  314. &&OPCODE_ITERATE_BEGIN_PACKED_STRING_ARRAY, \
  315. &&OPCODE_ITERATE_BEGIN_PACKED_VECTOR2_ARRAY, \
  316. &&OPCODE_ITERATE_BEGIN_PACKED_VECTOR3_ARRAY, \
  317. &&OPCODE_ITERATE_BEGIN_PACKED_COLOR_ARRAY, \
  318. &&OPCODE_ITERATE_BEGIN_OBJECT, \
  319. &&OPCODE_ITERATE, \
  320. &&OPCODE_ITERATE_INT, \
  321. &&OPCODE_ITERATE_FLOAT, \
  322. &&OPCODE_ITERATE_VECTOR2, \
  323. &&OPCODE_ITERATE_VECTOR2I, \
  324. &&OPCODE_ITERATE_VECTOR3, \
  325. &&OPCODE_ITERATE_VECTOR3I, \
  326. &&OPCODE_ITERATE_STRING, \
  327. &&OPCODE_ITERATE_DICTIONARY, \
  328. &&OPCODE_ITERATE_ARRAY, \
  329. &&OPCODE_ITERATE_PACKED_BYTE_ARRAY, \
  330. &&OPCODE_ITERATE_PACKED_INT32_ARRAY, \
  331. &&OPCODE_ITERATE_PACKED_INT64_ARRAY, \
  332. &&OPCODE_ITERATE_PACKED_FLOAT32_ARRAY, \
  333. &&OPCODE_ITERATE_PACKED_FLOAT64_ARRAY, \
  334. &&OPCODE_ITERATE_PACKED_STRING_ARRAY, \
  335. &&OPCODE_ITERATE_PACKED_VECTOR2_ARRAY, \
  336. &&OPCODE_ITERATE_PACKED_VECTOR3_ARRAY, \
  337. &&OPCODE_ITERATE_PACKED_COLOR_ARRAY, \
  338. &&OPCODE_ITERATE_OBJECT, \
  339. &&OPCODE_STORE_GLOBAL, \
  340. &&OPCODE_STORE_NAMED_GLOBAL, \
  341. &&OPCODE_TYPE_ADJUST_BOOL, \
  342. &&OPCODE_TYPE_ADJUST_INT, \
  343. &&OPCODE_TYPE_ADJUST_FLOAT, \
  344. &&OPCODE_TYPE_ADJUST_STRING, \
  345. &&OPCODE_TYPE_ADJUST_VECTOR2, \
  346. &&OPCODE_TYPE_ADJUST_VECTOR2I, \
  347. &&OPCODE_TYPE_ADJUST_RECT2, \
  348. &&OPCODE_TYPE_ADJUST_RECT2I, \
  349. &&OPCODE_TYPE_ADJUST_VECTOR3, \
  350. &&OPCODE_TYPE_ADJUST_VECTOR3I, \
  351. &&OPCODE_TYPE_ADJUST_TRANSFORM2D, \
  352. &&OPCODE_TYPE_ADJUST_PLANE, \
  353. &&OPCODE_TYPE_ADJUST_QUATERNION, \
  354. &&OPCODE_TYPE_ADJUST_AABB, \
  355. &&OPCODE_TYPE_ADJUST_BASIS, \
  356. &&OPCODE_TYPE_ADJUST_TRANSFORM3D, \
  357. &&OPCODE_TYPE_ADJUST_COLOR, \
  358. &&OPCODE_TYPE_ADJUST_STRING_NAME, \
  359. &&OPCODE_TYPE_ADJUST_NODE_PATH, \
  360. &&OPCODE_TYPE_ADJUST_RID, \
  361. &&OPCODE_TYPE_ADJUST_OBJECT, \
  362. &&OPCODE_TYPE_ADJUST_CALLABLE, \
  363. &&OPCODE_TYPE_ADJUST_SIGNAL, \
  364. &&OPCODE_TYPE_ADJUST_DICTIONARY, \
  365. &&OPCODE_TYPE_ADJUST_ARRAY, \
  366. &&OPCODE_TYPE_ADJUST_PACKED_BYTE_ARRAY, \
  367. &&OPCODE_TYPE_ADJUST_PACKED_INT32_ARRAY, \
  368. &&OPCODE_TYPE_ADJUST_PACKED_INT64_ARRAY, \
  369. &&OPCODE_TYPE_ADJUST_PACKED_FLOAT32_ARRAY, \
  370. &&OPCODE_TYPE_ADJUST_PACKED_FLOAT64_ARRAY, \
  371. &&OPCODE_TYPE_ADJUST_PACKED_STRING_ARRAY, \
  372. &&OPCODE_TYPE_ADJUST_PACKED_VECTOR2_ARRAY, \
  373. &&OPCODE_TYPE_ADJUST_PACKED_VECTOR3_ARRAY, \
  374. &&OPCODE_TYPE_ADJUST_PACKED_COLOR_ARRAY, \
  375. &&OPCODE_ASSERT, \
  376. &&OPCODE_BREAKPOINT, \
  377. &&OPCODE_LINE, \
  378. &&OPCODE_END \
  379. }; \
  380. static_assert((sizeof(switch_table_ops) / sizeof(switch_table_ops[0]) == (OPCODE_END + 1)), "Opcodes in jump table aren't the same as opcodes in enum.");
  381. #define OPCODE(m_op) \
  382. m_op:
  383. #define OPCODE_WHILE(m_test) \
  384. OPSWHILE:
  385. #define OPCODES_END \
  386. OPSEXIT:
  387. #define OPCODES_OUT \
  388. OPSOUT:
  389. #define DISPATCH_OPCODE goto OPSWHILE
  390. #define OPCODE_SWITCH(m_test) goto *switch_table_ops[m_test];
  391. #define OPCODE_BREAK goto OPSEXIT
  392. #define OPCODE_OUT goto OPSOUT
  393. #else
  394. #define OPCODES_TABLE
  395. #define OPCODE(m_op) case m_op:
  396. #define OPCODE_WHILE(m_test) while (m_test)
  397. #define OPCODES_END
  398. #define OPCODES_OUT
  399. #define DISPATCH_OPCODE continue
  400. #define OPCODE_SWITCH(m_test) switch (m_test)
  401. #define OPCODE_BREAK break
  402. #define OPCODE_OUT break
  403. #endif
  404. // Helpers for VariantInternal methods in macros.
  405. #define OP_GET_BOOL get_bool
  406. #define OP_GET_INT get_int
  407. #define OP_GET_FLOAT get_float
  408. #define OP_GET_VECTOR2 get_vector2
  409. #define OP_GET_VECTOR2I get_vector2i
  410. #define OP_GET_VECTOR3 get_vector3
  411. #define OP_GET_VECTOR3I get_vector3i
  412. #define OP_GET_RECT2 get_rect2
  413. #define OP_GET_RECT2I get_rect2i
  414. #define OP_GET_QUATERNION get_quaternion
  415. #define OP_GET_COLOR get_color
  416. #define OP_GET_STRING get_string
  417. #define OP_GET_STRING_NAME get_string_name
  418. #define OP_GET_NODE_PATH get_node_path
  419. #define OP_GET_CALLABLE get_callable
  420. #define OP_GET_SIGNAL get_signal
  421. #define OP_GET_ARRAY get_array
  422. #define OP_GET_DICTIONARY get_dictionary
  423. #define OP_GET_PACKED_BYTE_ARRAY get_byte_array
  424. #define OP_GET_PACKED_INT32_ARRAY get_int32_array
  425. #define OP_GET_PACKED_INT64_ARRAY get_int64_array
  426. #define OP_GET_PACKED_FLOAT32_ARRAY get_float32_array
  427. #define OP_GET_PACKED_FLOAT64_ARRAY get_float64_array
  428. #define OP_GET_PACKED_STRING_ARRAY get_string_array
  429. #define OP_GET_PACKED_VECTOR2_ARRAY get_vector2_array
  430. #define OP_GET_PACKED_VECTOR3_ARRAY get_vector3_array
  431. #define OP_GET_PACKED_COLOR_ARRAY get_color_array
  432. #define OP_GET_TRANSFORM3D get_transform
  433. #define OP_GET_TRANSFORM2D get_transform2d
  434. #define OP_GET_PLANE get_plane
  435. #define OP_GET_AABB get_aabb
  436. #define OP_GET_BASIS get_basis
  437. #define OP_GET_RID get_rid
  438. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Callable::CallError &r_err, CallState *p_state) {
  439. OPCODES_TABLE;
  440. if (!_code_ptr) {
  441. return _get_default_variant_for_data_type(return_type);
  442. }
  443. r_err.error = Callable::CallError::CALL_OK;
  444. Variant retvalue;
  445. Variant *stack = nullptr;
  446. Variant **instruction_args = nullptr;
  447. const void **call_args_ptr = nullptr;
  448. int defarg = 0;
  449. #ifdef DEBUG_ENABLED
  450. //GDScriptLanguage::get_singleton()->calls++;
  451. #endif
  452. uint32_t alloca_size = 0;
  453. GDScript *script;
  454. int ip = 0;
  455. int line = _initial_line;
  456. if (p_state) {
  457. //use existing (supplied) state (awaited)
  458. stack = (Variant *)p_state->stack.ptr();
  459. instruction_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  460. line = p_state->line;
  461. ip = p_state->ip;
  462. alloca_size = p_state->stack.size();
  463. script = p_state->script;
  464. p_instance = p_state->instance;
  465. defarg = p_state->defarg;
  466. } else {
  467. if (p_argcount != _argument_count) {
  468. if (p_argcount > _argument_count) {
  469. r_err.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  470. r_err.argument = _argument_count;
  471. return _get_default_variant_for_data_type(return_type);
  472. } else if (p_argcount < _argument_count - _default_arg_count) {
  473. r_err.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  474. r_err.argument = _argument_count - _default_arg_count;
  475. return _get_default_variant_for_data_type(return_type);
  476. } else {
  477. defarg = _argument_count - p_argcount;
  478. }
  479. }
  480. // Add 3 here for self, class, and nil.
  481. alloca_size = sizeof(Variant *) * 3 + sizeof(Variant *) * _instruction_args_size + sizeof(Variant) * _stack_size;
  482. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  483. stack = (Variant *)aptr;
  484. for (int i = 0; i < p_argcount; i++) {
  485. if (!argument_types[i].has_type) {
  486. memnew_placement(&stack[i + 3], Variant(*p_args[i]));
  487. continue;
  488. }
  489. // If types already match, don't call Variant::construct(). Constructors of some types
  490. // (e.g. packed arrays) do copies, whereas they pass by reference when inside a Variant.
  491. if (argument_types[i].is_type(*p_args[i], false)) {
  492. memnew_placement(&stack[i + 3], Variant(*p_args[i]));
  493. continue;
  494. }
  495. if (!argument_types[i].is_type(*p_args[i], true)) {
  496. r_err.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  497. r_err.argument = i;
  498. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  499. return _get_default_variant_for_data_type(return_type);
  500. }
  501. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  502. Variant arg;
  503. Variant::construct(argument_types[i].builtin_type, arg, &p_args[i], 1, r_err);
  504. memnew_placement(&stack[i + 3], Variant(arg));
  505. } else {
  506. memnew_placement(&stack[i + 3], Variant(*p_args[i]));
  507. }
  508. }
  509. for (int i = p_argcount + 3; i < _stack_size; i++) {
  510. memnew_placement(&stack[i], Variant);
  511. }
  512. if (_instruction_args_size) {
  513. instruction_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  514. } else {
  515. instruction_args = nullptr;
  516. }
  517. for (const KeyValue<int, Variant::Type> &E : temporary_slots) {
  518. type_init_function_table[E.value](&stack[E.key]);
  519. }
  520. }
  521. if (_ptrcall_args_size) {
  522. call_args_ptr = (const void **)alloca(_ptrcall_args_size * sizeof(void *));
  523. } else {
  524. call_args_ptr = nullptr;
  525. }
  526. if (p_instance) {
  527. memnew_placement(&stack[ADDR_STACK_SELF], Variant(p_instance->owner));
  528. script = p_instance->script.ptr();
  529. } else {
  530. memnew_placement(&stack[ADDR_STACK_SELF], Variant);
  531. script = _script;
  532. }
  533. memnew_placement(&stack[ADDR_STACK_CLASS], Variant(script));
  534. memnew_placement(&stack[ADDR_STACK_NIL], Variant);
  535. String err_text;
  536. #ifdef DEBUG_ENABLED
  537. if (EngineDebugger::is_active()) {
  538. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  539. }
  540. #define GD_ERR_BREAK(m_cond) \
  541. { \
  542. if (unlikely(m_cond)) { \
  543. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  544. OPCODE_BREAK; \
  545. } \
  546. }
  547. #define CHECK_SPACE(m_space) \
  548. GD_ERR_BREAK((ip + m_space) > _code_size)
  549. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  550. Variant *m_v; \
  551. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, stack, err_text); \
  552. if (unlikely(!m_v)) \
  553. OPCODE_BREAK;
  554. #else
  555. #define GD_ERR_BREAK(m_cond)
  556. #define CHECK_SPACE(m_space)
  557. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  558. Variant *m_v; \
  559. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, stack, err_text);
  560. #endif
  561. #define GET_INSTRUCTION_ARG(m_v, m_idx) \
  562. Variant *m_v = instruction_args[m_idx]
  563. #ifdef DEBUG_ENABLED
  564. uint64_t function_start_time = 0;
  565. uint64_t function_call_time = 0;
  566. if (GDScriptLanguage::get_singleton()->profiling) {
  567. function_start_time = OS::get_singleton()->get_ticks_usec();
  568. function_call_time = 0;
  569. profile.call_count++;
  570. profile.frame_call_count++;
  571. }
  572. bool exit_ok = false;
  573. bool awaited = false;
  574. #endif
  575. #ifdef DEBUG_ENABLED
  576. OPCODE_WHILE(ip < _code_size) {
  577. int last_opcode = _code_ptr[ip] & INSTR_MASK;
  578. #else
  579. OPCODE_WHILE(true) {
  580. #endif
  581. // Load arguments for the instruction before each instruction.
  582. int instr_arg_count = ((_code_ptr[ip]) & INSTR_ARGS_MASK) >> INSTR_BITS;
  583. for (int i = 0; i < instr_arg_count; i++) {
  584. GET_VARIANT_PTR(v, i + 1);
  585. instruction_args[i] = v;
  586. }
  587. OPCODE_SWITCH(_code_ptr[ip] & INSTR_MASK) {
  588. OPCODE(OPCODE_OPERATOR) {
  589. CHECK_SPACE(5);
  590. bool valid;
  591. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 4];
  592. GD_ERR_BREAK(op >= Variant::OP_MAX);
  593. GET_INSTRUCTION_ARG(a, 0);
  594. GET_INSTRUCTION_ARG(b, 1);
  595. GET_INSTRUCTION_ARG(dst, 2);
  596. #ifdef DEBUG_ENABLED
  597. Variant ret;
  598. Variant::evaluate(op, *a, *b, ret, valid);
  599. #else
  600. Variant::evaluate(op, *a, *b, *dst, valid);
  601. #endif
  602. #ifdef DEBUG_ENABLED
  603. if (!valid) {
  604. if (ret.get_type() == Variant::STRING) {
  605. //return a string when invalid with the error
  606. err_text = ret;
  607. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  608. } else {
  609. err_text = "Invalid operands '" + Variant::get_type_name(a->get_type()) + "' and '" + Variant::get_type_name(b->get_type()) + "' in operator '" + Variant::get_operator_name(op) + "'.";
  610. }
  611. OPCODE_BREAK;
  612. }
  613. *dst = ret;
  614. #endif
  615. ip += 5;
  616. }
  617. DISPATCH_OPCODE;
  618. OPCODE(OPCODE_OPERATOR_VALIDATED) {
  619. CHECK_SPACE(5);
  620. int operator_idx = _code_ptr[ip + 4];
  621. GD_ERR_BREAK(operator_idx < 0 || operator_idx >= _operator_funcs_count);
  622. Variant::ValidatedOperatorEvaluator operator_func = _operator_funcs_ptr[operator_idx];
  623. GET_INSTRUCTION_ARG(a, 0);
  624. GET_INSTRUCTION_ARG(b, 1);
  625. GET_INSTRUCTION_ARG(dst, 2);
  626. operator_func(a, b, dst);
  627. ip += 5;
  628. }
  629. DISPATCH_OPCODE;
  630. OPCODE(OPCODE_EXTENDS_TEST) {
  631. CHECK_SPACE(4);
  632. GET_INSTRUCTION_ARG(a, 0);
  633. GET_INSTRUCTION_ARG(b, 1);
  634. GET_INSTRUCTION_ARG(dst, 2);
  635. #ifdef DEBUG_ENABLED
  636. if (b->get_type() != Variant::OBJECT || b->operator Object *() == nullptr) {
  637. err_text = "Right operand of 'is' is not a class.";
  638. OPCODE_BREAK;
  639. }
  640. #endif
  641. bool extends_ok = false;
  642. if (a->get_type() == Variant::OBJECT && a->operator Object *() != nullptr) {
  643. #ifdef DEBUG_ENABLED
  644. bool was_freed;
  645. Object *obj_A = a->get_validated_object_with_check(was_freed);
  646. if (was_freed) {
  647. err_text = "Left operand of 'is' is a previously freed instance.";
  648. OPCODE_BREAK;
  649. }
  650. Object *obj_B = b->get_validated_object_with_check(was_freed);
  651. if (was_freed) {
  652. err_text = "Right operand of 'is' is a previously freed instance.";
  653. OPCODE_BREAK;
  654. }
  655. #else
  656. Object *obj_A = *a;
  657. Object *obj_B = *b;
  658. #endif // DEBUG_ENABLED
  659. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  660. if (scr_B) {
  661. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  662. //in other situation, this should return false.
  663. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  664. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  665. //bool found=false;
  666. while (cmp) {
  667. if (cmp == scr_B) {
  668. //inherits from script, all ok
  669. extends_ok = true;
  670. break;
  671. }
  672. cmp = cmp->_base;
  673. }
  674. }
  675. } else {
  676. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  677. #ifdef DEBUG_ENABLED
  678. if (!nc) {
  679. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  680. OPCODE_BREAK;
  681. }
  682. #endif
  683. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  684. }
  685. }
  686. *dst = extends_ok;
  687. ip += 4;
  688. }
  689. DISPATCH_OPCODE;
  690. OPCODE(OPCODE_IS_BUILTIN) {
  691. CHECK_SPACE(4);
  692. GET_INSTRUCTION_ARG(value, 0);
  693. GET_INSTRUCTION_ARG(dst, 1);
  694. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  695. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  696. *dst = value->get_type() == var_type;
  697. ip += 4;
  698. }
  699. DISPATCH_OPCODE;
  700. OPCODE(OPCODE_SET_KEYED) {
  701. CHECK_SPACE(3);
  702. GET_INSTRUCTION_ARG(dst, 0);
  703. GET_INSTRUCTION_ARG(index, 1);
  704. GET_INSTRUCTION_ARG(value, 2);
  705. bool valid;
  706. dst->set(*index, *value, &valid);
  707. #ifdef DEBUG_ENABLED
  708. if (!valid) {
  709. String v = index->operator String();
  710. if (!v.is_empty()) {
  711. v = "'" + v + "'";
  712. } else {
  713. v = "of type '" + _get_var_type(index) + "'";
  714. }
  715. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  716. OPCODE_BREAK;
  717. }
  718. #endif
  719. ip += 4;
  720. }
  721. DISPATCH_OPCODE;
  722. OPCODE(OPCODE_SET_KEYED_VALIDATED) {
  723. CHECK_SPACE(4);
  724. GET_INSTRUCTION_ARG(dst, 0);
  725. GET_INSTRUCTION_ARG(index, 1);
  726. GET_INSTRUCTION_ARG(value, 2);
  727. int index_setter = _code_ptr[ip + 4];
  728. GD_ERR_BREAK(index_setter < 0 || index_setter >= _keyed_setters_count);
  729. const Variant::ValidatedKeyedSetter setter = _keyed_setters_ptr[index_setter];
  730. bool valid;
  731. setter(dst, index, value, &valid);
  732. #ifdef DEBUG_ENABLED
  733. if (!valid) {
  734. String v = index->operator String();
  735. if (!v.is_empty()) {
  736. v = "'" + v + "'";
  737. } else {
  738. v = "of type '" + _get_var_type(index) + "'";
  739. }
  740. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  741. OPCODE_BREAK;
  742. }
  743. #endif
  744. ip += 5;
  745. }
  746. DISPATCH_OPCODE;
  747. OPCODE(OPCODE_SET_INDEXED_VALIDATED) {
  748. CHECK_SPACE(4);
  749. GET_INSTRUCTION_ARG(dst, 0);
  750. GET_INSTRUCTION_ARG(index, 1);
  751. GET_INSTRUCTION_ARG(value, 2);
  752. int index_setter = _code_ptr[ip + 4];
  753. GD_ERR_BREAK(index_setter < 0 || index_setter >= _indexed_setters_count);
  754. const Variant::ValidatedIndexedSetter setter = _indexed_setters_ptr[index_setter];
  755. int64_t int_index = *VariantInternal::get_int(index);
  756. bool oob;
  757. setter(dst, int_index, value, &oob);
  758. #ifdef DEBUG_ENABLED
  759. if (oob) {
  760. String v = index->operator String();
  761. if (!v.is_empty()) {
  762. v = "'" + v + "'";
  763. } else {
  764. v = "of type '" + _get_var_type(index) + "'";
  765. }
  766. err_text = "Out of bounds set index " + v + " (on base: '" + _get_var_type(dst) + "')";
  767. OPCODE_BREAK;
  768. }
  769. #endif
  770. ip += 5;
  771. }
  772. DISPATCH_OPCODE;
  773. OPCODE(OPCODE_GET_KEYED) {
  774. CHECK_SPACE(3);
  775. GET_INSTRUCTION_ARG(src, 0);
  776. GET_INSTRUCTION_ARG(index, 1);
  777. GET_INSTRUCTION_ARG(dst, 2);
  778. bool valid;
  779. #ifdef DEBUG_ENABLED
  780. // Allow better error message in cases where src and dst are the same stack position.
  781. Variant ret = src->get(*index, &valid);
  782. #else
  783. *dst = src->get(*index, &valid);
  784. #endif
  785. #ifdef DEBUG_ENABLED
  786. if (!valid) {
  787. String v = index->operator String();
  788. if (!v.is_empty()) {
  789. v = "'" + v + "'";
  790. } else {
  791. v = "of type '" + _get_var_type(index) + "'";
  792. }
  793. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  794. OPCODE_BREAK;
  795. }
  796. *dst = ret;
  797. #endif
  798. ip += 4;
  799. }
  800. DISPATCH_OPCODE;
  801. OPCODE(OPCODE_GET_KEYED_VALIDATED) {
  802. CHECK_SPACE(4);
  803. GET_INSTRUCTION_ARG(src, 0);
  804. GET_INSTRUCTION_ARG(key, 1);
  805. GET_INSTRUCTION_ARG(dst, 2);
  806. int index_getter = _code_ptr[ip + 4];
  807. GD_ERR_BREAK(index_getter < 0 || index_getter >= _keyed_getters_count);
  808. const Variant::ValidatedKeyedGetter getter = _keyed_getters_ptr[index_getter];
  809. bool valid;
  810. #ifdef DEBUG_ENABLED
  811. // Allow better error message in cases where src and dst are the same stack position.
  812. Variant ret;
  813. getter(src, key, &ret, &valid);
  814. #else
  815. getter(src, key, dst, &valid);
  816. #endif
  817. #ifdef DEBUG_ENABLED
  818. if (!valid) {
  819. String v = key->operator String();
  820. if (!v.is_empty()) {
  821. v = "'" + v + "'";
  822. } else {
  823. v = "of type '" + _get_var_type(key) + "'";
  824. }
  825. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  826. OPCODE_BREAK;
  827. }
  828. *dst = ret;
  829. #endif
  830. ip += 5;
  831. }
  832. DISPATCH_OPCODE;
  833. OPCODE(OPCODE_GET_INDEXED_VALIDATED) {
  834. CHECK_SPACE(4);
  835. GET_INSTRUCTION_ARG(src, 0);
  836. GET_INSTRUCTION_ARG(index, 1);
  837. GET_INSTRUCTION_ARG(dst, 2);
  838. int index_getter = _code_ptr[ip + 4];
  839. GD_ERR_BREAK(index_getter < 0 || index_getter >= _indexed_getters_count);
  840. const Variant::ValidatedIndexedGetter getter = _indexed_getters_ptr[index_getter];
  841. int64_t int_index = *VariantInternal::get_int(index);
  842. bool oob;
  843. getter(src, int_index, dst, &oob);
  844. #ifdef DEBUG_ENABLED
  845. if (oob) {
  846. String v = index->operator String();
  847. if (!v.is_empty()) {
  848. v = "'" + v + "'";
  849. } else {
  850. v = "of type '" + _get_var_type(index) + "'";
  851. }
  852. err_text = "Out of bounds get index " + v + " (on base: '" + _get_var_type(src) + "')";
  853. OPCODE_BREAK;
  854. }
  855. #endif
  856. ip += 5;
  857. }
  858. DISPATCH_OPCODE;
  859. OPCODE(OPCODE_SET_NAMED) {
  860. CHECK_SPACE(3);
  861. GET_INSTRUCTION_ARG(dst, 0);
  862. GET_INSTRUCTION_ARG(value, 1);
  863. int indexname = _code_ptr[ip + 3];
  864. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  865. const StringName *index = &_global_names_ptr[indexname];
  866. bool valid;
  867. dst->set_named(*index, *value, valid);
  868. #ifdef DEBUG_ENABLED
  869. if (!valid) {
  870. String err_type;
  871. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  872. OPCODE_BREAK;
  873. }
  874. #endif
  875. ip += 4;
  876. }
  877. DISPATCH_OPCODE;
  878. OPCODE(OPCODE_SET_NAMED_VALIDATED) {
  879. CHECK_SPACE(3);
  880. GET_INSTRUCTION_ARG(dst, 0);
  881. GET_INSTRUCTION_ARG(value, 1);
  882. int index_setter = _code_ptr[ip + 3];
  883. GD_ERR_BREAK(index_setter < 0 || index_setter >= _setters_count);
  884. const Variant::ValidatedSetter setter = _setters_ptr[index_setter];
  885. setter(dst, value);
  886. ip += 4;
  887. }
  888. DISPATCH_OPCODE;
  889. OPCODE(OPCODE_GET_NAMED) {
  890. CHECK_SPACE(4);
  891. GET_INSTRUCTION_ARG(src, 0);
  892. GET_INSTRUCTION_ARG(dst, 1);
  893. int indexname = _code_ptr[ip + 3];
  894. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  895. const StringName *index = &_global_names_ptr[indexname];
  896. bool valid;
  897. #ifdef DEBUG_ENABLED
  898. //allow better error message in cases where src and dst are the same stack position
  899. Variant ret = src->get_named(*index, valid);
  900. #else
  901. *dst = src->get_named(*index, valid);
  902. #endif
  903. #ifdef DEBUG_ENABLED
  904. if (!valid) {
  905. if (src->has_method(*index)) {
  906. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "'). Did you mean '." + index->operator String() + "()' or funcref(obj, \"" + index->operator String() + "\") ?";
  907. } else {
  908. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  909. }
  910. OPCODE_BREAK;
  911. }
  912. *dst = ret;
  913. #endif
  914. ip += 4;
  915. }
  916. DISPATCH_OPCODE;
  917. OPCODE(OPCODE_GET_NAMED_VALIDATED) {
  918. CHECK_SPACE(3);
  919. GET_INSTRUCTION_ARG(src, 0);
  920. GET_INSTRUCTION_ARG(dst, 1);
  921. int index_getter = _code_ptr[ip + 3];
  922. GD_ERR_BREAK(index_getter < 0 || index_getter >= _getters_count);
  923. const Variant::ValidatedGetter getter = _getters_ptr[index_getter];
  924. getter(src, dst);
  925. ip += 4;
  926. }
  927. DISPATCH_OPCODE;
  928. OPCODE(OPCODE_SET_MEMBER) {
  929. CHECK_SPACE(3);
  930. GET_INSTRUCTION_ARG(src, 0);
  931. int indexname = _code_ptr[ip + 2];
  932. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  933. const StringName *index = &_global_names_ptr[indexname];
  934. bool valid;
  935. #ifndef DEBUG_ENABLED
  936. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  937. #else
  938. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  939. if (!ok) {
  940. err_text = "Internal error setting property: " + String(*index);
  941. OPCODE_BREAK;
  942. } else if (!valid) {
  943. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  944. OPCODE_BREAK;
  945. }
  946. #endif
  947. ip += 3;
  948. }
  949. DISPATCH_OPCODE;
  950. OPCODE(OPCODE_GET_MEMBER) {
  951. CHECK_SPACE(3);
  952. GET_INSTRUCTION_ARG(dst, 0);
  953. int indexname = _code_ptr[ip + 2];
  954. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  955. const StringName *index = &_global_names_ptr[indexname];
  956. #ifndef DEBUG_ENABLED
  957. ClassDB::get_property(p_instance->owner, *index, *dst);
  958. #else
  959. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  960. if (!ok) {
  961. err_text = "Internal error getting property: " + String(*index);
  962. OPCODE_BREAK;
  963. }
  964. #endif
  965. ip += 3;
  966. }
  967. DISPATCH_OPCODE;
  968. OPCODE(OPCODE_ASSIGN) {
  969. CHECK_SPACE(3);
  970. GET_INSTRUCTION_ARG(dst, 0);
  971. GET_INSTRUCTION_ARG(src, 1);
  972. *dst = *src;
  973. ip += 3;
  974. }
  975. DISPATCH_OPCODE;
  976. OPCODE(OPCODE_ASSIGN_TRUE) {
  977. CHECK_SPACE(2);
  978. GET_INSTRUCTION_ARG(dst, 0);
  979. *dst = true;
  980. ip += 2;
  981. }
  982. DISPATCH_OPCODE;
  983. OPCODE(OPCODE_ASSIGN_FALSE) {
  984. CHECK_SPACE(2);
  985. GET_INSTRUCTION_ARG(dst, 0);
  986. *dst = false;
  987. ip += 2;
  988. }
  989. DISPATCH_OPCODE;
  990. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  991. CHECK_SPACE(4);
  992. GET_INSTRUCTION_ARG(dst, 0);
  993. GET_INSTRUCTION_ARG(src, 1);
  994. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 3];
  995. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  996. if (src->get_type() != var_type) {
  997. #ifdef DEBUG_ENABLED
  998. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  999. #endif // DEBUG_ENABLED
  1000. Callable::CallError ce;
  1001. Variant::construct(var_type, *dst, const_cast<const Variant **>(&src), 1, ce);
  1002. } else {
  1003. #ifdef DEBUG_ENABLED
  1004. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  1005. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  1006. OPCODE_BREAK;
  1007. }
  1008. } else {
  1009. #endif // DEBUG_ENABLED
  1010. *dst = *src;
  1011. }
  1012. ip += 4;
  1013. }
  1014. DISPATCH_OPCODE;
  1015. OPCODE(OPCODE_ASSIGN_TYPED_ARRAY) {
  1016. CHECK_SPACE(3);
  1017. GET_INSTRUCTION_ARG(dst, 0);
  1018. GET_INSTRUCTION_ARG(src, 1);
  1019. Array *dst_arr = VariantInternal::get_array(dst);
  1020. if (src->get_type() != Variant::ARRAY) {
  1021. #ifdef DEBUG_ENABLED
  1022. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  1023. "' to a variable of type '" + +"'.";
  1024. #endif
  1025. OPCODE_BREAK;
  1026. }
  1027. if (!dst_arr->typed_assign(*src)) {
  1028. #ifdef DEBUG_ENABLED
  1029. err_text = "Trying to assign a typed array with an array of different type.'";
  1030. #endif
  1031. OPCODE_BREAK;
  1032. }
  1033. ip += 3;
  1034. }
  1035. DISPATCH_OPCODE;
  1036. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  1037. CHECK_SPACE(4);
  1038. GET_INSTRUCTION_ARG(dst, 0);
  1039. GET_INSTRUCTION_ARG(src, 1);
  1040. #ifdef DEBUG_ENABLED
  1041. GET_INSTRUCTION_ARG(type, 2);
  1042. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  1043. GD_ERR_BREAK(!nc);
  1044. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1045. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  1046. "' to a variable of type '" + nc->get_name() + "'.";
  1047. OPCODE_BREAK;
  1048. }
  1049. Object *src_obj = src->operator Object *();
  1050. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  1051. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  1052. "' to a variable of type '" + nc->get_name() + "'.";
  1053. OPCODE_BREAK;
  1054. }
  1055. #endif // DEBUG_ENABLED
  1056. *dst = *src;
  1057. ip += 4;
  1058. }
  1059. DISPATCH_OPCODE;
  1060. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  1061. CHECK_SPACE(4);
  1062. GET_INSTRUCTION_ARG(dst, 0);
  1063. GET_INSTRUCTION_ARG(src, 1);
  1064. #ifdef DEBUG_ENABLED
  1065. GET_INSTRUCTION_ARG(type, 2);
  1066. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  1067. GD_ERR_BREAK(!base_type);
  1068. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1069. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  1070. OPCODE_BREAK;
  1071. }
  1072. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  1073. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  1074. if (!scr_inst) {
  1075. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  1076. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  1077. OPCODE_BREAK;
  1078. }
  1079. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  1080. bool valid = false;
  1081. while (src_type) {
  1082. if (src_type == base_type) {
  1083. valid = true;
  1084. break;
  1085. }
  1086. src_type = src_type->get_base_script().ptr();
  1087. }
  1088. if (!valid) {
  1089. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  1090. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  1091. OPCODE_BREAK;
  1092. }
  1093. }
  1094. #endif // DEBUG_ENABLED
  1095. *dst = *src;
  1096. ip += 4;
  1097. }
  1098. DISPATCH_OPCODE;
  1099. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  1100. CHECK_SPACE(4);
  1101. GET_INSTRUCTION_ARG(src, 0);
  1102. GET_INSTRUCTION_ARG(dst, 1);
  1103. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 3];
  1104. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  1105. #ifdef DEBUG_ENABLED
  1106. if (src->operator Object *() && !src->get_validated_object()) {
  1107. err_text = "Trying to cast a freed object.";
  1108. OPCODE_BREAK;
  1109. }
  1110. #endif
  1111. Callable::CallError err;
  1112. Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);
  1113. #ifdef DEBUG_ENABLED
  1114. if (err.error != Callable::CallError::CALL_OK) {
  1115. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  1116. OPCODE_BREAK;
  1117. }
  1118. #endif
  1119. ip += 4;
  1120. }
  1121. DISPATCH_OPCODE;
  1122. OPCODE(OPCODE_CAST_TO_NATIVE) {
  1123. CHECK_SPACE(4);
  1124. GET_INSTRUCTION_ARG(src, 0);
  1125. GET_INSTRUCTION_ARG(dst, 1);
  1126. GET_INSTRUCTION_ARG(to_type, 2);
  1127. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  1128. GD_ERR_BREAK(!nc);
  1129. #ifdef DEBUG_ENABLED
  1130. if (src->operator Object *() && !src->get_validated_object()) {
  1131. err_text = "Trying to cast a freed object.";
  1132. OPCODE_BREAK;
  1133. }
  1134. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1135. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  1136. OPCODE_BREAK;
  1137. }
  1138. #endif
  1139. Object *src_obj = src->operator Object *();
  1140. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  1141. *dst = Variant(); // invalid cast, assign NULL
  1142. } else {
  1143. *dst = *src;
  1144. }
  1145. ip += 4;
  1146. }
  1147. DISPATCH_OPCODE;
  1148. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  1149. CHECK_SPACE(4);
  1150. GET_INSTRUCTION_ARG(src, 0);
  1151. GET_INSTRUCTION_ARG(dst, 1);
  1152. GET_INSTRUCTION_ARG(to_type, 2);
  1153. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  1154. GD_ERR_BREAK(!base_type);
  1155. #ifdef DEBUG_ENABLED
  1156. if (src->operator Object *() && !src->get_validated_object()) {
  1157. err_text = "Trying to cast a freed object.";
  1158. OPCODE_BREAK;
  1159. }
  1160. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1161. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  1162. OPCODE_BREAK;
  1163. }
  1164. #endif
  1165. bool valid = false;
  1166. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  1167. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  1168. if (scr_inst) {
  1169. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  1170. while (src_type) {
  1171. if (src_type == base_type) {
  1172. valid = true;
  1173. break;
  1174. }
  1175. src_type = src_type->get_base_script().ptr();
  1176. }
  1177. }
  1178. }
  1179. if (valid) {
  1180. *dst = *src; // Valid cast, copy the source object
  1181. } else {
  1182. *dst = Variant(); // invalid cast, assign NULL
  1183. }
  1184. ip += 4;
  1185. }
  1186. DISPATCH_OPCODE;
  1187. OPCODE(OPCODE_CONSTRUCT) {
  1188. CHECK_SPACE(2 + instr_arg_count);
  1189. ip += instr_arg_count;
  1190. int argc = _code_ptr[ip + 1];
  1191. Variant::Type t = Variant::Type(_code_ptr[ip + 2]);
  1192. Variant **argptrs = instruction_args;
  1193. GET_INSTRUCTION_ARG(dst, argc);
  1194. Callable::CallError err;
  1195. Variant::construct(t, *dst, (const Variant **)argptrs, argc, err);
  1196. #ifdef DEBUG_ENABLED
  1197. if (err.error != Callable::CallError::CALL_OK) {
  1198. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  1199. OPCODE_BREAK;
  1200. }
  1201. #endif
  1202. ip += 3;
  1203. }
  1204. DISPATCH_OPCODE;
  1205. OPCODE(OPCODE_CONSTRUCT_VALIDATED) {
  1206. CHECK_SPACE(2 + instr_arg_count);
  1207. ip += instr_arg_count;
  1208. int argc = _code_ptr[ip + 1];
  1209. int constructor_idx = _code_ptr[ip + 2];
  1210. GD_ERR_BREAK(constructor_idx < 0 || constructor_idx >= _constructors_count);
  1211. Variant::ValidatedConstructor constructor = _constructors_ptr[constructor_idx];
  1212. Variant **argptrs = instruction_args;
  1213. GET_INSTRUCTION_ARG(dst, argc);
  1214. constructor(dst, (const Variant **)argptrs);
  1215. ip += 3;
  1216. }
  1217. DISPATCH_OPCODE;
  1218. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  1219. CHECK_SPACE(1 + instr_arg_count);
  1220. ip += instr_arg_count;
  1221. int argc = _code_ptr[ip + 1];
  1222. Array array;
  1223. array.resize(argc);
  1224. for (int i = 0; i < argc; i++) {
  1225. array[i] = *(instruction_args[i]);
  1226. }
  1227. GET_INSTRUCTION_ARG(dst, argc);
  1228. *dst = Variant(); // Clear potential previous typed array.
  1229. *dst = array;
  1230. ip += 2;
  1231. }
  1232. DISPATCH_OPCODE;
  1233. OPCODE(OPCODE_CONSTRUCT_TYPED_ARRAY) {
  1234. CHECK_SPACE(3 + instr_arg_count);
  1235. ip += instr_arg_count;
  1236. int argc = _code_ptr[ip + 1];
  1237. GET_INSTRUCTION_ARG(script_type, argc + 1);
  1238. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 2];
  1239. int native_type_idx = _code_ptr[ip + 3];
  1240. GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
  1241. const StringName native_type = _global_names_ptr[native_type_idx];
  1242. Array array;
  1243. array.set_typed(builtin_type, native_type, *script_type);
  1244. array.resize(argc);
  1245. for (int i = 0; i < argc; i++) {
  1246. array[i] = *(instruction_args[i]);
  1247. }
  1248. GET_INSTRUCTION_ARG(dst, argc);
  1249. *dst = Variant(); // Clear potential previous typed array.
  1250. *dst = array;
  1251. ip += 4;
  1252. }
  1253. DISPATCH_OPCODE;
  1254. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  1255. CHECK_SPACE(2 + instr_arg_count);
  1256. ip += instr_arg_count;
  1257. int argc = _code_ptr[ip + 1];
  1258. Dictionary dict;
  1259. for (int i = 0; i < argc; i++) {
  1260. GET_INSTRUCTION_ARG(k, i * 2 + 0);
  1261. GET_INSTRUCTION_ARG(v, i * 2 + 1);
  1262. dict[*k] = *v;
  1263. }
  1264. GET_INSTRUCTION_ARG(dst, argc * 2);
  1265. *dst = dict;
  1266. ip += 2;
  1267. }
  1268. DISPATCH_OPCODE;
  1269. OPCODE(OPCODE_CALL_ASYNC)
  1270. OPCODE(OPCODE_CALL_RETURN)
  1271. OPCODE(OPCODE_CALL) {
  1272. CHECK_SPACE(3 + instr_arg_count);
  1273. bool call_ret = (_code_ptr[ip] & INSTR_MASK) != OPCODE_CALL;
  1274. #ifdef DEBUG_ENABLED
  1275. bool call_async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  1276. #endif
  1277. ip += instr_arg_count;
  1278. int argc = _code_ptr[ip + 1];
  1279. GD_ERR_BREAK(argc < 0);
  1280. int methodname_idx = _code_ptr[ip + 2];
  1281. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  1282. const StringName *methodname = &_global_names_ptr[methodname_idx];
  1283. GET_INSTRUCTION_ARG(base, argc);
  1284. Variant **argptrs = instruction_args;
  1285. #ifdef DEBUG_ENABLED
  1286. uint64_t call_time = 0;
  1287. if (GDScriptLanguage::get_singleton()->profiling) {
  1288. call_time = OS::get_singleton()->get_ticks_usec();
  1289. }
  1290. #endif
  1291. Callable::CallError err;
  1292. if (call_ret) {
  1293. GET_INSTRUCTION_ARG(ret, argc + 1);
  1294. base->callp(*methodname, (const Variant **)argptrs, argc, *ret, err);
  1295. #ifdef DEBUG_ENABLED
  1296. if (!call_async && ret->get_type() == Variant::OBJECT) {
  1297. // Check if getting a function state without await.
  1298. bool was_freed = false;
  1299. Object *obj = ret->get_validated_object_with_check(was_freed);
  1300. if (was_freed) {
  1301. err_text = "Got a freed object as a result of the call.";
  1302. OPCODE_BREAK;
  1303. }
  1304. if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1305. err_text = R"(Trying to call an async function without "await".)";
  1306. OPCODE_BREAK;
  1307. }
  1308. }
  1309. #endif
  1310. } else {
  1311. Variant ret;
  1312. base->callp(*methodname, (const Variant **)argptrs, argc, ret, err);
  1313. }
  1314. #ifdef DEBUG_ENABLED
  1315. if (GDScriptLanguage::get_singleton()->profiling) {
  1316. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1317. }
  1318. if (err.error != Callable::CallError::CALL_OK) {
  1319. String methodstr = *methodname;
  1320. String basestr = _get_var_type(base);
  1321. bool is_callable = false;
  1322. if (methodstr == "call") {
  1323. if (argc >= 1 && base->get_type() != Variant::CALLABLE) {
  1324. methodstr = String(*argptrs[0]) + " (via call)";
  1325. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1326. err.argument += 1;
  1327. }
  1328. } else {
  1329. methodstr = base->operator String() + " (Callable)";
  1330. is_callable = true;
  1331. }
  1332. } else if (methodstr == "free") {
  1333. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1334. if (base->is_ref_counted()) {
  1335. err_text = "Attempted to free a reference.";
  1336. OPCODE_BREAK;
  1337. } else if (base->get_type() == Variant::OBJECT) {
  1338. err_text = "Attempted to free a locked object (calling or emitting).";
  1339. OPCODE_BREAK;
  1340. }
  1341. }
  1342. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  1343. if (argc >= 1) {
  1344. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  1345. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1346. err.argument += 1;
  1347. }
  1348. }
  1349. }
  1350. err_text = _get_call_error(err, "function '" + methodstr + (is_callable ? "" : "' in base '" + basestr) + "'", (const Variant **)argptrs);
  1351. OPCODE_BREAK;
  1352. }
  1353. #endif
  1354. ip += 3;
  1355. }
  1356. DISPATCH_OPCODE;
  1357. OPCODE(OPCODE_CALL_METHOD_BIND)
  1358. OPCODE(OPCODE_CALL_METHOD_BIND_RET) {
  1359. CHECK_SPACE(3 + instr_arg_count);
  1360. bool call_ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET;
  1361. ip += instr_arg_count;
  1362. int argc = _code_ptr[ip + 1];
  1363. GD_ERR_BREAK(argc < 0);
  1364. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1365. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1366. GET_INSTRUCTION_ARG(base, argc);
  1367. #ifdef DEBUG_ENABLED
  1368. bool freed = false;
  1369. Object *base_obj = base->get_validated_object_with_check(freed);
  1370. if (freed) {
  1371. err_text = "Trying to call a function on a previously freed instance.";
  1372. OPCODE_BREAK;
  1373. } else if (!base_obj) {
  1374. err_text = "Trying to call a function on a null value.";
  1375. OPCODE_BREAK;
  1376. }
  1377. #else
  1378. Object *base_obj = base->operator Object *();
  1379. #endif
  1380. Variant **argptrs = instruction_args;
  1381. #ifdef DEBUG_ENABLED
  1382. uint64_t call_time = 0;
  1383. if (GDScriptLanguage::get_singleton()->profiling) {
  1384. call_time = OS::get_singleton()->get_ticks_usec();
  1385. }
  1386. #endif
  1387. Callable::CallError err;
  1388. if (call_ret) {
  1389. GET_INSTRUCTION_ARG(ret, argc + 1);
  1390. *ret = method->call(base_obj, (const Variant **)argptrs, argc, err);
  1391. } else {
  1392. method->call(base_obj, (const Variant **)argptrs, argc, err);
  1393. }
  1394. #ifdef DEBUG_ENABLED
  1395. if (GDScriptLanguage::get_singleton()->profiling) {
  1396. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1397. }
  1398. if (err.error != Callable::CallError::CALL_OK) {
  1399. String methodstr = method->get_name();
  1400. String basestr = _get_var_type(base);
  1401. if (methodstr == "call") {
  1402. if (argc >= 1) {
  1403. methodstr = String(*argptrs[0]) + " (via call)";
  1404. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1405. err.argument += 1;
  1406. }
  1407. }
  1408. } else if (methodstr == "free") {
  1409. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1410. if (base->is_ref_counted()) {
  1411. err_text = "Attempted to free a reference.";
  1412. OPCODE_BREAK;
  1413. } else if (base->get_type() == Variant::OBJECT) {
  1414. err_text = "Attempted to free a locked object (calling or emitting).";
  1415. OPCODE_BREAK;
  1416. }
  1417. }
  1418. }
  1419. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  1420. OPCODE_BREAK;
  1421. }
  1422. #endif
  1423. ip += 3;
  1424. }
  1425. DISPATCH_OPCODE;
  1426. OPCODE(OPCODE_CALL_BUILTIN_STATIC) {
  1427. CHECK_SPACE(4 + instr_arg_count);
  1428. ip += instr_arg_count;
  1429. GD_ERR_BREAK(_code_ptr[ip + 1] < 0 || _code_ptr[ip + 1] >= Variant::VARIANT_MAX);
  1430. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 1];
  1431. int methodname_idx = _code_ptr[ip + 2];
  1432. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  1433. const StringName *methodname = &_global_names_ptr[methodname_idx];
  1434. int argc = _code_ptr[ip + 3];
  1435. GD_ERR_BREAK(argc < 0);
  1436. GET_INSTRUCTION_ARG(ret, argc);
  1437. const Variant **argptrs = const_cast<const Variant **>(instruction_args);
  1438. #ifdef DEBUG_ENABLED
  1439. uint64_t call_time = 0;
  1440. if (GDScriptLanguage::get_singleton()->profiling) {
  1441. call_time = OS::get_singleton()->get_ticks_usec();
  1442. }
  1443. #endif
  1444. Callable::CallError err;
  1445. Variant::call_static(builtin_type, *methodname, argptrs, argc, *ret, err);
  1446. #ifdef DEBUG_ENABLED
  1447. if (GDScriptLanguage::get_singleton()->profiling) {
  1448. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1449. }
  1450. if (err.error != Callable::CallError::CALL_OK) {
  1451. err_text = _get_call_error(err, "static function '" + methodname->operator String() + "' in type '" + Variant::get_type_name(builtin_type) + "'", argptrs);
  1452. OPCODE_BREAK;
  1453. }
  1454. #endif
  1455. ip += 4;
  1456. }
  1457. DISPATCH_OPCODE;
  1458. OPCODE(OPCODE_CALL_NATIVE_STATIC) {
  1459. CHECK_SPACE(3 + instr_arg_count);
  1460. ip += instr_arg_count;
  1461. GD_ERR_BREAK(_code_ptr[ip + 1] < 0 || _code_ptr[ip + 1] >= _methods_count);
  1462. MethodBind *method = _methods_ptr[_code_ptr[ip + 1]];
  1463. int argc = _code_ptr[ip + 2];
  1464. GD_ERR_BREAK(argc < 0);
  1465. GET_INSTRUCTION_ARG(ret, argc);
  1466. const Variant **argptrs = const_cast<const Variant **>(instruction_args);
  1467. #ifdef DEBUG_ENABLED
  1468. uint64_t call_time = 0;
  1469. if (GDScriptLanguage::get_singleton()->profiling) {
  1470. call_time = OS::get_singleton()->get_ticks_usec();
  1471. }
  1472. #endif
  1473. Callable::CallError err;
  1474. *ret = method->call(nullptr, argptrs, argc, err);
  1475. #ifdef DEBUG_ENABLED
  1476. if (GDScriptLanguage::get_singleton()->profiling) {
  1477. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1478. }
  1479. if (err.error != Callable::CallError::CALL_OK) {
  1480. err_text = _get_call_error(err, "static function '" + method->get_name().operator String() + "' in type '" + method->get_instance_class().operator String() + "'", argptrs);
  1481. OPCODE_BREAK;
  1482. }
  1483. #endif
  1484. ip += 3;
  1485. }
  1486. DISPATCH_OPCODE;
  1487. #ifdef DEBUG_ENABLED
  1488. #define OPCODE_CALL_PTR(m_type) \
  1489. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1490. CHECK_SPACE(3 + instr_arg_count); \
  1491. ip += instr_arg_count; \
  1492. int argc = _code_ptr[ip + 1]; \
  1493. GD_ERR_BREAK(argc < 0); \
  1494. GET_INSTRUCTION_ARG(base, argc); \
  1495. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count); \
  1496. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1497. bool freed = false; \
  1498. Object *base_obj = base->get_validated_object_with_check(freed); \
  1499. if (freed) { \
  1500. err_text = "Trying to call a function on a previously freed instance."; \
  1501. OPCODE_BREAK; \
  1502. } else if (!base_obj) { \
  1503. err_text = "Trying to call a function on a null value."; \
  1504. OPCODE_BREAK; \
  1505. } \
  1506. const void **argptrs = call_args_ptr; \
  1507. for (int i = 0; i < argc; i++) { \
  1508. GET_INSTRUCTION_ARG(v, i); \
  1509. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1510. } \
  1511. uint64_t call_time = 0; \
  1512. if (GDScriptLanguage::get_singleton()->profiling) { \
  1513. call_time = OS::get_singleton()->get_ticks_usec(); \
  1514. } \
  1515. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1516. VariantInternal::initialize(ret, Variant::m_type); \
  1517. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1518. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1519. if (GDScriptLanguage::get_singleton()->profiling) { \
  1520. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time; \
  1521. } \
  1522. ip += 3; \
  1523. } \
  1524. DISPATCH_OPCODE
  1525. #else
  1526. #define OPCODE_CALL_PTR(m_type) \
  1527. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1528. CHECK_SPACE(3 + instr_arg_count); \
  1529. ip += instr_arg_count; \
  1530. int argc = _code_ptr[ip + 1]; \
  1531. GET_INSTRUCTION_ARG(base, argc); \
  1532. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1533. Object *base_obj = *VariantInternal::get_object(base); \
  1534. const void **argptrs = call_args_ptr; \
  1535. for (int i = 0; i < argc; i++) { \
  1536. GET_INSTRUCTION_ARG(v, i); \
  1537. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1538. } \
  1539. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1540. VariantInternal::initialize(ret, Variant::m_type); \
  1541. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1542. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1543. ip += 3; \
  1544. } \
  1545. DISPATCH_OPCODE
  1546. #endif
  1547. OPCODE_CALL_PTR(BOOL);
  1548. OPCODE_CALL_PTR(INT);
  1549. OPCODE_CALL_PTR(FLOAT);
  1550. OPCODE_CALL_PTR(STRING);
  1551. OPCODE_CALL_PTR(VECTOR2);
  1552. OPCODE_CALL_PTR(VECTOR2I);
  1553. OPCODE_CALL_PTR(RECT2);
  1554. OPCODE_CALL_PTR(RECT2I);
  1555. OPCODE_CALL_PTR(VECTOR3);
  1556. OPCODE_CALL_PTR(VECTOR3I);
  1557. OPCODE_CALL_PTR(TRANSFORM2D);
  1558. OPCODE_CALL_PTR(PLANE);
  1559. OPCODE_CALL_PTR(QUATERNION);
  1560. OPCODE_CALL_PTR(AABB);
  1561. OPCODE_CALL_PTR(BASIS);
  1562. OPCODE_CALL_PTR(TRANSFORM3D);
  1563. OPCODE_CALL_PTR(COLOR);
  1564. OPCODE_CALL_PTR(STRING_NAME);
  1565. OPCODE_CALL_PTR(NODE_PATH);
  1566. OPCODE_CALL_PTR(RID);
  1567. OPCODE_CALL_PTR(CALLABLE);
  1568. OPCODE_CALL_PTR(SIGNAL);
  1569. OPCODE_CALL_PTR(DICTIONARY);
  1570. OPCODE_CALL_PTR(ARRAY);
  1571. OPCODE_CALL_PTR(PACKED_BYTE_ARRAY);
  1572. OPCODE_CALL_PTR(PACKED_INT32_ARRAY);
  1573. OPCODE_CALL_PTR(PACKED_INT64_ARRAY);
  1574. OPCODE_CALL_PTR(PACKED_FLOAT32_ARRAY);
  1575. OPCODE_CALL_PTR(PACKED_FLOAT64_ARRAY);
  1576. OPCODE_CALL_PTR(PACKED_STRING_ARRAY);
  1577. OPCODE_CALL_PTR(PACKED_VECTOR2_ARRAY);
  1578. OPCODE_CALL_PTR(PACKED_VECTOR3_ARRAY);
  1579. OPCODE_CALL_PTR(PACKED_COLOR_ARRAY);
  1580. OPCODE(OPCODE_CALL_PTRCALL_OBJECT) {
  1581. CHECK_SPACE(3 + instr_arg_count);
  1582. ip += instr_arg_count;
  1583. int argc = _code_ptr[ip + 1];
  1584. GD_ERR_BREAK(argc < 0);
  1585. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1586. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1587. GET_INSTRUCTION_ARG(base, argc);
  1588. #ifdef DEBUG_ENABLED
  1589. bool freed = false;
  1590. Object *base_obj = base->get_validated_object_with_check(freed);
  1591. if (freed) {
  1592. err_text = "Trying to call a function on a previously freed instance.";
  1593. OPCODE_BREAK;
  1594. } else if (!base_obj) {
  1595. err_text = "Trying to call a function on a null value.";
  1596. OPCODE_BREAK;
  1597. }
  1598. #else
  1599. Object *base_obj = *VariantInternal::get_object(base);
  1600. #endif
  1601. const void **argptrs = call_args_ptr;
  1602. for (int i = 0; i < argc; i++) {
  1603. GET_INSTRUCTION_ARG(v, i);
  1604. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1605. }
  1606. #ifdef DEBUG_ENABLED
  1607. uint64_t call_time = 0;
  1608. if (GDScriptLanguage::get_singleton()->profiling) {
  1609. call_time = OS::get_singleton()->get_ticks_usec();
  1610. }
  1611. #endif
  1612. GET_INSTRUCTION_ARG(ret, argc + 1);
  1613. VariantInternal::initialize(ret, Variant::OBJECT);
  1614. Object **ret_opaque = VariantInternal::get_object(ret);
  1615. method->ptrcall(base_obj, argptrs, ret_opaque);
  1616. VariantInternal::object_assign(ret, *ret_opaque); // Set so ID is correct too.
  1617. #ifdef DEBUG_ENABLED
  1618. if (GDScriptLanguage::get_singleton()->profiling) {
  1619. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1620. }
  1621. #endif
  1622. ip += 3;
  1623. }
  1624. DISPATCH_OPCODE;
  1625. OPCODE(OPCODE_CALL_PTRCALL_NO_RETURN) {
  1626. CHECK_SPACE(3 + instr_arg_count);
  1627. ip += instr_arg_count;
  1628. int argc = _code_ptr[ip + 1];
  1629. GD_ERR_BREAK(argc < 0);
  1630. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1631. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1632. GET_INSTRUCTION_ARG(base, argc);
  1633. #ifdef DEBUG_ENABLED
  1634. bool freed = false;
  1635. Object *base_obj = base->get_validated_object_with_check(freed);
  1636. if (freed) {
  1637. err_text = "Trying to call a function on a previously freed instance.";
  1638. OPCODE_BREAK;
  1639. } else if (!base_obj) {
  1640. err_text = "Trying to call a function on a null value.";
  1641. OPCODE_BREAK;
  1642. }
  1643. #else
  1644. Object *base_obj = *VariantInternal::get_object(base);
  1645. #endif
  1646. const void **argptrs = call_args_ptr;
  1647. for (int i = 0; i < argc; i++) {
  1648. GET_INSTRUCTION_ARG(v, i);
  1649. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1650. }
  1651. #ifdef DEBUG_ENABLED
  1652. uint64_t call_time = 0;
  1653. if (GDScriptLanguage::get_singleton()->profiling) {
  1654. call_time = OS::get_singleton()->get_ticks_usec();
  1655. }
  1656. #endif
  1657. GET_INSTRUCTION_ARG(ret, argc + 1);
  1658. VariantInternal::initialize(ret, Variant::NIL);
  1659. method->ptrcall(base_obj, argptrs, nullptr);
  1660. #ifdef DEBUG_ENABLED
  1661. if (GDScriptLanguage::get_singleton()->profiling) {
  1662. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1663. }
  1664. #endif
  1665. ip += 3;
  1666. }
  1667. DISPATCH_OPCODE;
  1668. OPCODE(OPCODE_CALL_BUILTIN_TYPE_VALIDATED) {
  1669. CHECK_SPACE(3 + instr_arg_count);
  1670. ip += instr_arg_count;
  1671. int argc = _code_ptr[ip + 1];
  1672. GD_ERR_BREAK(argc < 0);
  1673. GET_INSTRUCTION_ARG(base, argc);
  1674. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _builtin_methods_count);
  1675. Variant::ValidatedBuiltInMethod method = _builtin_methods_ptr[_code_ptr[ip + 2]];
  1676. Variant **argptrs = instruction_args;
  1677. #ifdef DEBUG_ENABLED
  1678. uint64_t call_time = 0;
  1679. if (GDScriptLanguage::get_singleton()->profiling) {
  1680. call_time = OS::get_singleton()->get_ticks_usec();
  1681. }
  1682. #endif
  1683. GET_INSTRUCTION_ARG(ret, argc + 1);
  1684. method(base, (const Variant **)argptrs, argc, ret);
  1685. #ifdef DEBUG_ENABLED
  1686. if (GDScriptLanguage::get_singleton()->profiling) {
  1687. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1688. }
  1689. #endif
  1690. ip += 3;
  1691. }
  1692. DISPATCH_OPCODE;
  1693. OPCODE(OPCODE_CALL_UTILITY) {
  1694. CHECK_SPACE(3 + instr_arg_count);
  1695. ip += instr_arg_count;
  1696. int argc = _code_ptr[ip + 1];
  1697. GD_ERR_BREAK(argc < 0);
  1698. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _global_names_count);
  1699. StringName function = _global_names_ptr[_code_ptr[ip + 2]];
  1700. Variant **argptrs = instruction_args;
  1701. GET_INSTRUCTION_ARG(dst, argc);
  1702. Callable::CallError err;
  1703. Variant::call_utility_function(function, dst, (const Variant **)argptrs, argc, err);
  1704. #ifdef DEBUG_ENABLED
  1705. if (err.error != Callable::CallError::CALL_OK) {
  1706. String methodstr = function;
  1707. if (dst->get_type() == Variant::STRING) {
  1708. // Call provided error string.
  1709. err_text = "Error calling utility function '" + methodstr + "': " + String(*dst);
  1710. } else {
  1711. err_text = _get_call_error(err, "utility function '" + methodstr + "'", (const Variant **)argptrs);
  1712. }
  1713. OPCODE_BREAK;
  1714. }
  1715. #endif
  1716. ip += 3;
  1717. }
  1718. DISPATCH_OPCODE;
  1719. OPCODE(OPCODE_CALL_UTILITY_VALIDATED) {
  1720. CHECK_SPACE(3 + instr_arg_count);
  1721. ip += instr_arg_count;
  1722. int argc = _code_ptr[ip + 1];
  1723. GD_ERR_BREAK(argc < 0);
  1724. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _utilities_count);
  1725. Variant::ValidatedUtilityFunction function = _utilities_ptr[_code_ptr[ip + 2]];
  1726. Variant **argptrs = instruction_args;
  1727. GET_INSTRUCTION_ARG(dst, argc);
  1728. function(dst, (const Variant **)argptrs, argc);
  1729. ip += 3;
  1730. }
  1731. DISPATCH_OPCODE;
  1732. OPCODE(OPCODE_CALL_GDSCRIPT_UTILITY) {
  1733. CHECK_SPACE(3 + instr_arg_count);
  1734. ip += instr_arg_count;
  1735. int argc = _code_ptr[ip + 1];
  1736. GD_ERR_BREAK(argc < 0);
  1737. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _gds_utilities_count);
  1738. GDScriptUtilityFunctions::FunctionPtr function = _gds_utilities_ptr[_code_ptr[ip + 2]];
  1739. Variant **argptrs = instruction_args;
  1740. GET_INSTRUCTION_ARG(dst, argc);
  1741. Callable::CallError err;
  1742. function(dst, (const Variant **)argptrs, argc, err);
  1743. #ifdef DEBUG_ENABLED
  1744. if (err.error != Callable::CallError::CALL_OK) {
  1745. // TODO: Add this information in debug.
  1746. String methodstr = "<unknown function>";
  1747. if (dst->get_type() == Variant::STRING) {
  1748. // Call provided error string.
  1749. err_text = "Error calling GDScript utility function '" + methodstr + "': " + String(*dst);
  1750. } else {
  1751. err_text = _get_call_error(err, "GDScript utility function '" + methodstr + "'", (const Variant **)argptrs);
  1752. }
  1753. OPCODE_BREAK;
  1754. }
  1755. #endif
  1756. ip += 3;
  1757. }
  1758. DISPATCH_OPCODE;
  1759. OPCODE(OPCODE_CALL_SELF_BASE) {
  1760. CHECK_SPACE(3 + instr_arg_count);
  1761. ip += instr_arg_count;
  1762. int argc = _code_ptr[ip + 1];
  1763. GD_ERR_BREAK(argc < 0);
  1764. int self_fun = _code_ptr[ip + 2];
  1765. #ifdef DEBUG_ENABLED
  1766. if (self_fun < 0 || self_fun >= _global_names_count) {
  1767. err_text = "compiler bug, function name not found";
  1768. OPCODE_BREAK;
  1769. }
  1770. #endif
  1771. const StringName *methodname = &_global_names_ptr[self_fun];
  1772. Variant **argptrs = instruction_args;
  1773. GET_INSTRUCTION_ARG(dst, argc);
  1774. const GDScript *gds = _script;
  1775. HashMap<StringName, GDScriptFunction *>::ConstIterator E;
  1776. while (gds->base.ptr()) {
  1777. gds = gds->base.ptr();
  1778. E = gds->member_functions.find(*methodname);
  1779. if (E) {
  1780. break;
  1781. }
  1782. }
  1783. Callable::CallError err;
  1784. if (E) {
  1785. *dst = E->value->call(p_instance, (const Variant **)argptrs, argc, err);
  1786. } else if (gds->native.ptr()) {
  1787. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1788. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  1789. if (!mb) {
  1790. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1791. } else {
  1792. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  1793. }
  1794. } else {
  1795. err.error = Callable::CallError::CALL_OK;
  1796. }
  1797. } else {
  1798. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1799. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1800. } else {
  1801. err.error = Callable::CallError::CALL_OK;
  1802. }
  1803. }
  1804. if (err.error != Callable::CallError::CALL_OK) {
  1805. String methodstr = *methodname;
  1806. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  1807. OPCODE_BREAK;
  1808. }
  1809. ip += 3;
  1810. }
  1811. DISPATCH_OPCODE;
  1812. OPCODE(OPCODE_AWAIT) {
  1813. CHECK_SPACE(2);
  1814. // Do the oneshot connect.
  1815. GET_INSTRUCTION_ARG(argobj, 0);
  1816. Signal sig;
  1817. bool is_signal = true;
  1818. {
  1819. Variant result = *argobj;
  1820. if (argobj->get_type() == Variant::OBJECT) {
  1821. bool was_freed = false;
  1822. Object *obj = argobj->get_validated_object_with_check(was_freed);
  1823. if (was_freed) {
  1824. err_text = "Trying to await on a freed object.";
  1825. OPCODE_BREAK;
  1826. }
  1827. // Is this even possible to be null at this point?
  1828. if (obj) {
  1829. if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1830. result = Signal(obj, "completed");
  1831. }
  1832. }
  1833. }
  1834. if (result.get_type() != Variant::SIGNAL) {
  1835. // Not async, return immediately using the target from OPCODE_AWAIT_RESUME.
  1836. GET_VARIANT_PTR(target, 3);
  1837. *target = result;
  1838. ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
  1839. is_signal = false;
  1840. } else {
  1841. sig = result;
  1842. }
  1843. }
  1844. if (is_signal) {
  1845. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1846. gdfs->function = this;
  1847. gdfs->state.stack.resize(alloca_size);
  1848. // First 3 stack addresses are special, so we just skip them here.
  1849. for (int i = 3; i < _stack_size; i++) {
  1850. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1851. }
  1852. gdfs->state.stack_size = _stack_size;
  1853. gdfs->state.alloca_size = alloca_size;
  1854. gdfs->state.ip = ip + 2;
  1855. gdfs->state.line = line;
  1856. gdfs->state.script = _script;
  1857. {
  1858. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1859. _script->pending_func_states.add(&gdfs->scripts_list);
  1860. if (p_instance) {
  1861. gdfs->state.instance = p_instance;
  1862. p_instance->pending_func_states.add(&gdfs->instances_list);
  1863. } else {
  1864. gdfs->state.instance = nullptr;
  1865. }
  1866. }
  1867. #ifdef DEBUG_ENABLED
  1868. gdfs->state.function_name = name;
  1869. gdfs->state.script_path = _script->get_path();
  1870. #endif
  1871. gdfs->state.defarg = defarg;
  1872. gdfs->function = this;
  1873. retvalue = gdfs;
  1874. Error err = sig.connect(callable_bind(Callable(gdfs.ptr(), "_signal_callback"), retvalue), Object::CONNECT_ONESHOT);
  1875. if (err != OK) {
  1876. err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
  1877. OPCODE_BREAK;
  1878. }
  1879. #ifdef DEBUG_ENABLED
  1880. exit_ok = true;
  1881. awaited = true;
  1882. #endif
  1883. OPCODE_BREAK;
  1884. }
  1885. }
  1886. DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
  1887. OPCODE(OPCODE_AWAIT_RESUME) {
  1888. CHECK_SPACE(2);
  1889. #ifdef DEBUG_ENABLED
  1890. if (!p_state) {
  1891. err_text = ("Invalid Resume (bug?)");
  1892. OPCODE_BREAK;
  1893. }
  1894. #endif
  1895. GET_INSTRUCTION_ARG(result, 0);
  1896. *result = p_state->result;
  1897. ip += 2;
  1898. }
  1899. DISPATCH_OPCODE;
  1900. OPCODE(OPCODE_CREATE_LAMBDA) {
  1901. CHECK_SPACE(2 + instr_arg_count);
  1902. ip += instr_arg_count;
  1903. int captures_count = _code_ptr[ip + 1];
  1904. GD_ERR_BREAK(captures_count < 0);
  1905. int lambda_index = _code_ptr[ip + 2];
  1906. GD_ERR_BREAK(lambda_index < 0 || lambda_index >= _lambdas_count);
  1907. GDScriptFunction *lambda = _lambdas_ptr[lambda_index];
  1908. Vector<Variant> captures;
  1909. captures.resize(captures_count);
  1910. for (int i = 0; i < captures_count; i++) {
  1911. GET_INSTRUCTION_ARG(arg, i);
  1912. captures.write[i] = *arg;
  1913. }
  1914. GDScriptLambdaCallable *callable = memnew(GDScriptLambdaCallable(Ref<GDScript>(script), lambda, captures));
  1915. GET_INSTRUCTION_ARG(result, captures_count);
  1916. *result = Callable(callable);
  1917. ip += 3;
  1918. }
  1919. DISPATCH_OPCODE;
  1920. OPCODE(OPCODE_CREATE_SELF_LAMBDA) {
  1921. CHECK_SPACE(2 + instr_arg_count);
  1922. GD_ERR_BREAK(p_instance == nullptr);
  1923. ip += instr_arg_count;
  1924. int captures_count = _code_ptr[ip + 1];
  1925. GD_ERR_BREAK(captures_count < 0);
  1926. int lambda_index = _code_ptr[ip + 2];
  1927. GD_ERR_BREAK(lambda_index < 0 || lambda_index >= _lambdas_count);
  1928. GDScriptFunction *lambda = _lambdas_ptr[lambda_index];
  1929. Vector<Variant> captures;
  1930. captures.resize(captures_count);
  1931. for (int i = 0; i < captures_count; i++) {
  1932. GET_INSTRUCTION_ARG(arg, i);
  1933. captures.write[i] = *arg;
  1934. }
  1935. GDScriptLambdaSelfCallable *callable;
  1936. if (Object::cast_to<RefCounted>(p_instance->owner)) {
  1937. callable = memnew(GDScriptLambdaSelfCallable(Ref<RefCounted>(Object::cast_to<RefCounted>(p_instance->owner)), lambda, captures));
  1938. } else {
  1939. callable = memnew(GDScriptLambdaSelfCallable(p_instance->owner, lambda, captures));
  1940. }
  1941. GET_INSTRUCTION_ARG(result, captures_count);
  1942. *result = Callable(callable);
  1943. ip += 3;
  1944. }
  1945. DISPATCH_OPCODE;
  1946. OPCODE(OPCODE_JUMP) {
  1947. CHECK_SPACE(2);
  1948. int to = _code_ptr[ip + 1];
  1949. GD_ERR_BREAK(to < 0 || to > _code_size);
  1950. ip = to;
  1951. }
  1952. DISPATCH_OPCODE;
  1953. OPCODE(OPCODE_JUMP_IF) {
  1954. CHECK_SPACE(3);
  1955. GET_INSTRUCTION_ARG(test, 0);
  1956. bool result = test->booleanize();
  1957. if (result) {
  1958. int to = _code_ptr[ip + 2];
  1959. GD_ERR_BREAK(to < 0 || to > _code_size);
  1960. ip = to;
  1961. } else {
  1962. ip += 3;
  1963. }
  1964. }
  1965. DISPATCH_OPCODE;
  1966. OPCODE(OPCODE_JUMP_IF_NOT) {
  1967. CHECK_SPACE(3);
  1968. GET_INSTRUCTION_ARG(test, 0);
  1969. bool result = test->booleanize();
  1970. if (!result) {
  1971. int to = _code_ptr[ip + 2];
  1972. GD_ERR_BREAK(to < 0 || to > _code_size);
  1973. ip = to;
  1974. } else {
  1975. ip += 3;
  1976. }
  1977. }
  1978. DISPATCH_OPCODE;
  1979. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1980. CHECK_SPACE(2);
  1981. ip = _default_arg_ptr[defarg];
  1982. }
  1983. DISPATCH_OPCODE;
  1984. OPCODE(OPCODE_JUMP_IF_SHARED) {
  1985. CHECK_SPACE(3);
  1986. GET_INSTRUCTION_ARG(val, 0);
  1987. if (val->is_shared()) {
  1988. int to = _code_ptr[ip + 2];
  1989. GD_ERR_BREAK(to < 0 || to > _code_size);
  1990. ip = to;
  1991. } else {
  1992. ip += 3;
  1993. }
  1994. }
  1995. DISPATCH_OPCODE;
  1996. OPCODE(OPCODE_RETURN) {
  1997. CHECK_SPACE(2);
  1998. GET_INSTRUCTION_ARG(r, 0);
  1999. retvalue = *r;
  2000. #ifdef DEBUG_ENABLED
  2001. exit_ok = true;
  2002. #endif
  2003. OPCODE_BREAK;
  2004. }
  2005. OPCODE(OPCODE_RETURN_TYPED_BUILTIN) {
  2006. CHECK_SPACE(3);
  2007. GET_INSTRUCTION_ARG(r, 0);
  2008. Variant::Type ret_type = (Variant::Type)_code_ptr[ip + 2];
  2009. GD_ERR_BREAK(ret_type < 0 || ret_type >= Variant::VARIANT_MAX);
  2010. if (r->get_type() != ret_type) {
  2011. if (Variant::can_convert_strict(r->get_type(), ret_type)) {
  2012. Callable::CallError ce;
  2013. Variant::construct(ret_type, retvalue, const_cast<const Variant **>(&r), 1, ce);
  2014. } else {
  2015. #ifdef DEBUG_ENABLED
  2016. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  2017. Variant::get_type_name(r->get_type()), Variant::get_type_name(ret_type));
  2018. #endif // DEBUG_ENABLED
  2019. // Construct a base type anyway so type constraints are met.
  2020. Callable::CallError ce;
  2021. Variant::construct(ret_type, retvalue, nullptr, 0, ce);
  2022. OPCODE_BREAK;
  2023. }
  2024. } else {
  2025. retvalue = *r;
  2026. }
  2027. #ifdef DEBUG_ENABLED
  2028. exit_ok = true;
  2029. #endif // DEBUG_ENABLED
  2030. OPCODE_BREAK;
  2031. }
  2032. OPCODE(OPCODE_RETURN_TYPED_ARRAY) {
  2033. CHECK_SPACE(5);
  2034. GET_INSTRUCTION_ARG(r, 0);
  2035. GET_INSTRUCTION_ARG(script_type, 1);
  2036. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 3];
  2037. int native_type_idx = _code_ptr[ip + 4];
  2038. GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
  2039. const StringName native_type = _global_names_ptr[native_type_idx];
  2040. if (r->get_type() != Variant::ARRAY) {
  2041. #ifdef DEBUG_ENABLED
  2042. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "Array[%s]".)",
  2043. Variant::get_type_name(r->get_type()), Variant::get_type_name(builtin_type));
  2044. #endif
  2045. OPCODE_BREAK;
  2046. }
  2047. Array array;
  2048. array.set_typed(builtin_type, native_type, *script_type);
  2049. #ifdef DEBUG_ENABLED
  2050. bool valid = array.typed_assign(*VariantInternal::get_array(r));
  2051. #else
  2052. array.typed_assign(*VariantInternal::get_array(r));
  2053. #endif // DEBUG_ENABLED
  2054. // Assign the return value anyway since we want it to be the valid type.
  2055. retvalue = array;
  2056. #ifdef DEBUG_ENABLED
  2057. if (!valid) {
  2058. err_text = "Trying to return a typed array with an array of different type.'";
  2059. OPCODE_BREAK;
  2060. }
  2061. exit_ok = true;
  2062. #endif // DEBUG_ENABLED
  2063. OPCODE_BREAK;
  2064. }
  2065. OPCODE(OPCODE_RETURN_TYPED_NATIVE) {
  2066. CHECK_SPACE(3);
  2067. GET_INSTRUCTION_ARG(r, 0);
  2068. GET_INSTRUCTION_ARG(type, 1);
  2069. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  2070. GD_ERR_BREAK(!nc);
  2071. if (r->get_type() != Variant::OBJECT && r->get_type() != Variant::NIL) {
  2072. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  2073. Variant::get_type_name(r->get_type()), nc->get_name());
  2074. OPCODE_BREAK;
  2075. }
  2076. #ifdef DEBUG_ENABLED
  2077. bool freed = false;
  2078. Object *ret_obj = r->get_validated_object_with_check(freed);
  2079. if (freed) {
  2080. err_text = "Trying to return a previously freed instance.";
  2081. OPCODE_BREAK;
  2082. }
  2083. #else
  2084. Object *ret_obj = r->operator Object *();
  2085. #endif // DEBUG_ENABLED
  2086. if (ret_obj && !ClassDB::is_parent_class(ret_obj->get_class_name(), nc->get_name())) {
  2087. #ifdef DEBUG_ENABLED
  2088. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  2089. ret_obj->get_class_name(), nc->get_name());
  2090. #endif // DEBUG_ENABLED
  2091. OPCODE_BREAK;
  2092. }
  2093. retvalue = *r;
  2094. #ifdef DEBUG_ENABLED
  2095. exit_ok = true;
  2096. #endif // DEBUG_ENABLED
  2097. OPCODE_BREAK;
  2098. }
  2099. OPCODE(OPCODE_RETURN_TYPED_SCRIPT) {
  2100. CHECK_SPACE(3);
  2101. GET_INSTRUCTION_ARG(r, 0);
  2102. GET_INSTRUCTION_ARG(type, 1);
  2103. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  2104. GD_ERR_BREAK(!base_type);
  2105. if (r->get_type() != Variant::OBJECT && r->get_type() != Variant::NIL) {
  2106. #ifdef DEBUG_ENABLED
  2107. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  2108. Variant::get_type_name(r->get_type()), _get_script_name(Ref<Script>(base_type)));
  2109. #endif // DEBUG_ENABLED
  2110. OPCODE_BREAK;
  2111. }
  2112. #ifdef DEBUG_ENABLED
  2113. bool freed = false;
  2114. Object *ret_obj = r->get_validated_object_with_check(freed);
  2115. if (freed) {
  2116. err_text = "Trying to return a previously freed instance.";
  2117. OPCODE_BREAK;
  2118. }
  2119. #else
  2120. Object *ret_obj = r->operator Object *();
  2121. #endif // DEBUG_ENABLED
  2122. if (ret_obj) {
  2123. ScriptInstance *ret_inst = ret_obj->get_script_instance();
  2124. if (!ret_inst) {
  2125. #ifdef DEBUG_ENABLED
  2126. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  2127. ret_obj->get_class_name(), _get_script_name(Ref<GDScript>(base_type)));
  2128. #endif // DEBUG_ENABLED
  2129. OPCODE_BREAK;
  2130. }
  2131. Script *ret_type = ret_obj->get_script_instance()->get_script().ptr();
  2132. bool valid = false;
  2133. while (ret_type) {
  2134. if (ret_type == base_type) {
  2135. valid = true;
  2136. break;
  2137. }
  2138. ret_type = ret_type->get_base_script().ptr();
  2139. }
  2140. if (!valid) {
  2141. #ifdef DEBUG_ENABLED
  2142. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  2143. _get_script_name(ret_obj->get_script_instance()->get_script()), _get_script_name(Ref<GDScript>(base_type)));
  2144. #endif // DEBUG_ENABLED
  2145. OPCODE_BREAK;
  2146. }
  2147. }
  2148. retvalue = *r;
  2149. #ifdef DEBUG_ENABLED
  2150. exit_ok = true;
  2151. #endif // DEBUG_ENABLED
  2152. OPCODE_BREAK;
  2153. }
  2154. OPCODE(OPCODE_ITERATE_BEGIN) {
  2155. CHECK_SPACE(8); // Space for this and a regular iterate.
  2156. GET_INSTRUCTION_ARG(counter, 0);
  2157. GET_INSTRUCTION_ARG(container, 1);
  2158. bool valid;
  2159. if (!container->iter_init(*counter, valid)) {
  2160. #ifdef DEBUG_ENABLED
  2161. if (!valid) {
  2162. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  2163. OPCODE_BREAK;
  2164. }
  2165. #endif
  2166. int jumpto = _code_ptr[ip + 4];
  2167. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2168. ip = jumpto;
  2169. } else {
  2170. GET_INSTRUCTION_ARG(iterator, 2);
  2171. *iterator = container->iter_get(*counter, valid);
  2172. #ifdef DEBUG_ENABLED
  2173. if (!valid) {
  2174. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  2175. OPCODE_BREAK;
  2176. }
  2177. #endif
  2178. ip += 5; // Skip regular iterate which is always next.
  2179. }
  2180. }
  2181. DISPATCH_OPCODE;
  2182. OPCODE(OPCODE_ITERATE_BEGIN_INT) {
  2183. CHECK_SPACE(8); // Check space for iterate instruction too.
  2184. GET_INSTRUCTION_ARG(counter, 0);
  2185. GET_INSTRUCTION_ARG(container, 1);
  2186. int64_t size = *VariantInternal::get_int(container);
  2187. VariantInternal::initialize(counter, Variant::INT);
  2188. *VariantInternal::get_int(counter) = 0;
  2189. if (size > 0) {
  2190. GET_INSTRUCTION_ARG(iterator, 2);
  2191. VariantInternal::initialize(iterator, Variant::INT);
  2192. *VariantInternal::get_int(iterator) = 0;
  2193. // Skip regular iterate.
  2194. ip += 5;
  2195. } else {
  2196. // Jump to end of loop.
  2197. int jumpto = _code_ptr[ip + 4];
  2198. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2199. ip = jumpto;
  2200. }
  2201. }
  2202. DISPATCH_OPCODE;
  2203. OPCODE(OPCODE_ITERATE_BEGIN_FLOAT) {
  2204. CHECK_SPACE(8); // Check space for iterate instruction too.
  2205. GET_INSTRUCTION_ARG(counter, 0);
  2206. GET_INSTRUCTION_ARG(container, 1);
  2207. double size = *VariantInternal::get_float(container);
  2208. VariantInternal::initialize(counter, Variant::FLOAT);
  2209. *VariantInternal::get_float(counter) = 0.0;
  2210. if (size > 0) {
  2211. GET_INSTRUCTION_ARG(iterator, 2);
  2212. VariantInternal::initialize(iterator, Variant::FLOAT);
  2213. *VariantInternal::get_float(iterator) = 0;
  2214. // Skip regular iterate.
  2215. ip += 5;
  2216. } else {
  2217. // Jump to end of loop.
  2218. int jumpto = _code_ptr[ip + 4];
  2219. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2220. ip = jumpto;
  2221. }
  2222. }
  2223. DISPATCH_OPCODE;
  2224. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2) {
  2225. CHECK_SPACE(8); // Check space for iterate instruction too.
  2226. GET_INSTRUCTION_ARG(counter, 0);
  2227. GET_INSTRUCTION_ARG(container, 1);
  2228. Vector2 *bounds = VariantInternal::get_vector2(container);
  2229. VariantInternal::initialize(counter, Variant::FLOAT);
  2230. *VariantInternal::get_float(counter) = bounds->x;
  2231. if (bounds->x < bounds->y) {
  2232. GET_INSTRUCTION_ARG(iterator, 2);
  2233. VariantInternal::initialize(iterator, Variant::FLOAT);
  2234. *VariantInternal::get_float(iterator) = bounds->x;
  2235. // Skip regular iterate.
  2236. ip += 5;
  2237. } else {
  2238. // Jump to end of loop.
  2239. int jumpto = _code_ptr[ip + 4];
  2240. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2241. ip = jumpto;
  2242. }
  2243. }
  2244. DISPATCH_OPCODE;
  2245. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2I) {
  2246. CHECK_SPACE(8); // Check space for iterate instruction too.
  2247. GET_INSTRUCTION_ARG(counter, 0);
  2248. GET_INSTRUCTION_ARG(container, 1);
  2249. Vector2i *bounds = VariantInternal::get_vector2i(container);
  2250. VariantInternal::initialize(counter, Variant::FLOAT);
  2251. *VariantInternal::get_int(counter) = bounds->x;
  2252. if (bounds->x < bounds->y) {
  2253. GET_INSTRUCTION_ARG(iterator, 2);
  2254. VariantInternal::initialize(iterator, Variant::INT);
  2255. *VariantInternal::get_int(iterator) = bounds->x;
  2256. // Skip regular iterate.
  2257. ip += 5;
  2258. } else {
  2259. // Jump to end of loop.
  2260. int jumpto = _code_ptr[ip + 4];
  2261. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2262. ip = jumpto;
  2263. }
  2264. }
  2265. DISPATCH_OPCODE;
  2266. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3) {
  2267. CHECK_SPACE(8); // Check space for iterate instruction too.
  2268. GET_INSTRUCTION_ARG(counter, 0);
  2269. GET_INSTRUCTION_ARG(container, 1);
  2270. Vector3 *bounds = VariantInternal::get_vector3(container);
  2271. double from = bounds->x;
  2272. double to = bounds->y;
  2273. double step = bounds->z;
  2274. VariantInternal::initialize(counter, Variant::FLOAT);
  2275. *VariantInternal::get_float(counter) = from;
  2276. bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
  2277. if (do_continue) {
  2278. GET_INSTRUCTION_ARG(iterator, 2);
  2279. VariantInternal::initialize(iterator, Variant::FLOAT);
  2280. *VariantInternal::get_float(iterator) = from;
  2281. // Skip regular iterate.
  2282. ip += 5;
  2283. } else {
  2284. // Jump to end of loop.
  2285. int jumpto = _code_ptr[ip + 4];
  2286. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2287. ip = jumpto;
  2288. }
  2289. }
  2290. DISPATCH_OPCODE;
  2291. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3I) {
  2292. CHECK_SPACE(8); // Check space for iterate instruction too.
  2293. GET_INSTRUCTION_ARG(counter, 0);
  2294. GET_INSTRUCTION_ARG(container, 1);
  2295. Vector3i *bounds = VariantInternal::get_vector3i(container);
  2296. int64_t from = bounds->x;
  2297. int64_t to = bounds->y;
  2298. int64_t step = bounds->z;
  2299. VariantInternal::initialize(counter, Variant::INT);
  2300. *VariantInternal::get_int(counter) = from;
  2301. bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
  2302. if (do_continue) {
  2303. GET_INSTRUCTION_ARG(iterator, 2);
  2304. VariantInternal::initialize(iterator, Variant::INT);
  2305. *VariantInternal::get_int(iterator) = from;
  2306. // Skip regular iterate.
  2307. ip += 5;
  2308. } else {
  2309. // Jump to end of loop.
  2310. int jumpto = _code_ptr[ip + 4];
  2311. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2312. ip = jumpto;
  2313. }
  2314. }
  2315. DISPATCH_OPCODE;
  2316. OPCODE(OPCODE_ITERATE_BEGIN_STRING) {
  2317. CHECK_SPACE(8); // Check space for iterate instruction too.
  2318. GET_INSTRUCTION_ARG(counter, 0);
  2319. GET_INSTRUCTION_ARG(container, 1);
  2320. String *str = VariantInternal::get_string(container);
  2321. VariantInternal::initialize(counter, Variant::INT);
  2322. *VariantInternal::get_int(counter) = 0;
  2323. if (!str->is_empty()) {
  2324. GET_INSTRUCTION_ARG(iterator, 2);
  2325. VariantInternal::initialize(iterator, Variant::STRING);
  2326. *VariantInternal::get_string(iterator) = str->substr(0, 1);
  2327. // Skip regular iterate.
  2328. ip += 5;
  2329. } else {
  2330. // Jump to end of loop.
  2331. int jumpto = _code_ptr[ip + 4];
  2332. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2333. ip = jumpto;
  2334. }
  2335. }
  2336. DISPATCH_OPCODE;
  2337. OPCODE(OPCODE_ITERATE_BEGIN_DICTIONARY) {
  2338. CHECK_SPACE(8); // Check space for iterate instruction too.
  2339. GET_INSTRUCTION_ARG(counter, 0);
  2340. GET_INSTRUCTION_ARG(container, 1);
  2341. Dictionary *dict = VariantInternal::get_dictionary(container);
  2342. const Variant *next = dict->next(nullptr);
  2343. if (!dict->is_empty()) {
  2344. GET_INSTRUCTION_ARG(iterator, 2);
  2345. *counter = *next;
  2346. *iterator = *next;
  2347. // Skip regular iterate.
  2348. ip += 5;
  2349. } else {
  2350. // Jump to end of loop.
  2351. int jumpto = _code_ptr[ip + 4];
  2352. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2353. ip = jumpto;
  2354. }
  2355. }
  2356. DISPATCH_OPCODE;
  2357. OPCODE(OPCODE_ITERATE_BEGIN_ARRAY) {
  2358. CHECK_SPACE(8); // Check space for iterate instruction too.
  2359. GET_INSTRUCTION_ARG(counter, 0);
  2360. GET_INSTRUCTION_ARG(container, 1);
  2361. Array *array = VariantInternal::get_array(container);
  2362. VariantInternal::initialize(counter, Variant::INT);
  2363. *VariantInternal::get_int(counter) = 0;
  2364. if (!array->is_empty()) {
  2365. GET_INSTRUCTION_ARG(iterator, 2);
  2366. *iterator = array->get(0);
  2367. // Skip regular iterate.
  2368. ip += 5;
  2369. } else {
  2370. // Jump to end of loop.
  2371. int jumpto = _code_ptr[ip + 4];
  2372. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2373. ip = jumpto;
  2374. }
  2375. }
  2376. DISPATCH_OPCODE;
  2377. #define OPCODE_ITERATE_BEGIN_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_var_ret_type, m_ret_type, m_ret_get_func) \
  2378. OPCODE(OPCODE_ITERATE_BEGIN_PACKED_##m_var_type##_ARRAY) { \
  2379. CHECK_SPACE(8); \
  2380. GET_INSTRUCTION_ARG(counter, 0); \
  2381. GET_INSTRUCTION_ARG(container, 1); \
  2382. Vector<m_elem_type> *array = VariantInternal::m_get_func(container); \
  2383. VariantInternal::initialize(counter, Variant::INT); \
  2384. *VariantInternal::get_int(counter) = 0; \
  2385. if (!array->is_empty()) { \
  2386. GET_INSTRUCTION_ARG(iterator, 2); \
  2387. VariantInternal::initialize(iterator, Variant::m_var_ret_type); \
  2388. m_ret_type *it = VariantInternal::m_ret_get_func(iterator); \
  2389. *it = array->get(0); \
  2390. ip += 5; \
  2391. } else { \
  2392. int jumpto = _code_ptr[ip + 4]; \
  2393. GD_ERR_BREAK(jumpto<0 || jumpto> _code_size); \
  2394. ip = jumpto; \
  2395. } \
  2396. } \
  2397. DISPATCH_OPCODE
  2398. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, INT, int64_t, get_int);
  2399. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT32, int32_t, get_int32_array, INT, int64_t, get_int);
  2400. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT64, int64_t, get_int64_array, INT, int64_t, get_int);
  2401. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT32, float, get_float32_array, FLOAT, double, get_float);
  2402. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT64, double, get_float64_array, FLOAT, double, get_float);
  2403. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(STRING, String, get_string_array, STRING, String, get_string);
  2404. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, VECTOR2, Vector2, get_vector2);
  2405. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, VECTOR3, Vector3, get_vector3);
  2406. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(COLOR, Color, get_color_array, COLOR, Color, get_color);
  2407. OPCODE(OPCODE_ITERATE_BEGIN_OBJECT) {
  2408. CHECK_SPACE(4);
  2409. GET_INSTRUCTION_ARG(counter, 0);
  2410. GET_INSTRUCTION_ARG(container, 1);
  2411. #ifdef DEBUG_ENABLED
  2412. bool freed = false;
  2413. Object *obj = container->get_validated_object_with_check(freed);
  2414. if (freed) {
  2415. err_text = "Trying to iterate on a previously freed object.";
  2416. OPCODE_BREAK;
  2417. } else if (!obj) {
  2418. err_text = "Trying to iterate on a null value.";
  2419. OPCODE_BREAK;
  2420. }
  2421. #else
  2422. Object *obj = *VariantInternal::get_object(container);
  2423. #endif
  2424. Array ref;
  2425. ref.push_back(*counter);
  2426. Variant vref;
  2427. VariantInternal::initialize(&vref, Variant::ARRAY);
  2428. *VariantInternal::get_array(&vref) = ref;
  2429. Variant **args = instruction_args; // Overriding an instruction argument, but we don't need access to that anymore.
  2430. args[0] = &vref;
  2431. Callable::CallError ce;
  2432. Variant has_next = obj->callp(CoreStringNames::get_singleton()->_iter_init, (const Variant **)args, 1, ce);
  2433. #ifdef DEBUG_ENABLED
  2434. if (ce.error != Callable::CallError::CALL_OK) {
  2435. err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
  2436. OPCODE_BREAK;
  2437. }
  2438. #endif
  2439. if (!has_next.booleanize()) {
  2440. int jumpto = _code_ptr[ip + 4];
  2441. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2442. ip = jumpto;
  2443. } else {
  2444. GET_INSTRUCTION_ARG(iterator, 2);
  2445. *iterator = obj->callp(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce);
  2446. #ifdef DEBUG_ENABLED
  2447. if (ce.error != Callable::CallError::CALL_OK) {
  2448. err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
  2449. OPCODE_BREAK;
  2450. }
  2451. #endif
  2452. ip += 5; // Loop again.
  2453. }
  2454. }
  2455. DISPATCH_OPCODE;
  2456. OPCODE(OPCODE_ITERATE) {
  2457. CHECK_SPACE(4);
  2458. GET_INSTRUCTION_ARG(counter, 0);
  2459. GET_INSTRUCTION_ARG(container, 1);
  2460. bool valid;
  2461. if (!container->iter_next(*counter, valid)) {
  2462. #ifdef DEBUG_ENABLED
  2463. if (!valid) {
  2464. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  2465. OPCODE_BREAK;
  2466. }
  2467. #endif
  2468. int jumpto = _code_ptr[ip + 4];
  2469. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2470. ip = jumpto;
  2471. } else {
  2472. GET_INSTRUCTION_ARG(iterator, 2);
  2473. *iterator = container->iter_get(*counter, valid);
  2474. #ifdef DEBUG_ENABLED
  2475. if (!valid) {
  2476. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  2477. OPCODE_BREAK;
  2478. }
  2479. #endif
  2480. ip += 5; //loop again
  2481. }
  2482. }
  2483. DISPATCH_OPCODE;
  2484. OPCODE(OPCODE_ITERATE_INT) {
  2485. CHECK_SPACE(4);
  2486. GET_INSTRUCTION_ARG(counter, 0);
  2487. GET_INSTRUCTION_ARG(container, 1);
  2488. int64_t size = *VariantInternal::get_int(container);
  2489. int64_t *count = VariantInternal::get_int(counter);
  2490. (*count)++;
  2491. if (*count >= size) {
  2492. int jumpto = _code_ptr[ip + 4];
  2493. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2494. ip = jumpto;
  2495. } else {
  2496. GET_INSTRUCTION_ARG(iterator, 2);
  2497. *VariantInternal::get_int(iterator) = *count;
  2498. ip += 5; // Loop again.
  2499. }
  2500. }
  2501. DISPATCH_OPCODE;
  2502. OPCODE(OPCODE_ITERATE_FLOAT) {
  2503. CHECK_SPACE(4);
  2504. GET_INSTRUCTION_ARG(counter, 0);
  2505. GET_INSTRUCTION_ARG(container, 1);
  2506. double size = *VariantInternal::get_float(container);
  2507. double *count = VariantInternal::get_float(counter);
  2508. (*count)++;
  2509. if (*count >= size) {
  2510. int jumpto = _code_ptr[ip + 4];
  2511. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2512. ip = jumpto;
  2513. } else {
  2514. GET_INSTRUCTION_ARG(iterator, 2);
  2515. *VariantInternal::get_float(iterator) = *count;
  2516. ip += 5; // Loop again.
  2517. }
  2518. }
  2519. DISPATCH_OPCODE;
  2520. OPCODE(OPCODE_ITERATE_VECTOR2) {
  2521. CHECK_SPACE(4);
  2522. GET_INSTRUCTION_ARG(counter, 0);
  2523. GET_INSTRUCTION_ARG(container, 1);
  2524. const Vector2 *bounds = VariantInternal::get_vector2((const Variant *)container);
  2525. double *count = VariantInternal::get_float(counter);
  2526. (*count)++;
  2527. if (*count >= bounds->y) {
  2528. int jumpto = _code_ptr[ip + 4];
  2529. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2530. ip = jumpto;
  2531. } else {
  2532. GET_INSTRUCTION_ARG(iterator, 2);
  2533. *VariantInternal::get_float(iterator) = *count;
  2534. ip += 5; // Loop again.
  2535. }
  2536. }
  2537. DISPATCH_OPCODE;
  2538. OPCODE(OPCODE_ITERATE_VECTOR2I) {
  2539. CHECK_SPACE(4);
  2540. GET_INSTRUCTION_ARG(counter, 0);
  2541. GET_INSTRUCTION_ARG(container, 1);
  2542. const Vector2i *bounds = VariantInternal::get_vector2i((const Variant *)container);
  2543. int64_t *count = VariantInternal::get_int(counter);
  2544. (*count)++;
  2545. if (*count >= bounds->y) {
  2546. int jumpto = _code_ptr[ip + 4];
  2547. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2548. ip = jumpto;
  2549. } else {
  2550. GET_INSTRUCTION_ARG(iterator, 2);
  2551. *VariantInternal::get_int(iterator) = *count;
  2552. ip += 5; // Loop again.
  2553. }
  2554. }
  2555. DISPATCH_OPCODE;
  2556. OPCODE(OPCODE_ITERATE_VECTOR3) {
  2557. CHECK_SPACE(4);
  2558. GET_INSTRUCTION_ARG(counter, 0);
  2559. GET_INSTRUCTION_ARG(container, 1);
  2560. const Vector3 *bounds = VariantInternal::get_vector3((const Variant *)container);
  2561. double *count = VariantInternal::get_float(counter);
  2562. *count += bounds->z;
  2563. if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
  2564. int jumpto = _code_ptr[ip + 4];
  2565. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2566. ip = jumpto;
  2567. } else {
  2568. GET_INSTRUCTION_ARG(iterator, 2);
  2569. *VariantInternal::get_float(iterator) = *count;
  2570. ip += 5; // Loop again.
  2571. }
  2572. }
  2573. DISPATCH_OPCODE;
  2574. OPCODE(OPCODE_ITERATE_VECTOR3I) {
  2575. CHECK_SPACE(4);
  2576. GET_INSTRUCTION_ARG(counter, 0);
  2577. GET_INSTRUCTION_ARG(container, 1);
  2578. const Vector3i *bounds = VariantInternal::get_vector3i((const Variant *)container);
  2579. int64_t *count = VariantInternal::get_int(counter);
  2580. *count += bounds->z;
  2581. if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
  2582. int jumpto = _code_ptr[ip + 4];
  2583. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2584. ip = jumpto;
  2585. } else {
  2586. GET_INSTRUCTION_ARG(iterator, 2);
  2587. *VariantInternal::get_int(iterator) = *count;
  2588. ip += 5; // Loop again.
  2589. }
  2590. }
  2591. DISPATCH_OPCODE;
  2592. OPCODE(OPCODE_ITERATE_STRING) {
  2593. CHECK_SPACE(4);
  2594. GET_INSTRUCTION_ARG(counter, 0);
  2595. GET_INSTRUCTION_ARG(container, 1);
  2596. const String *str = VariantInternal::get_string((const Variant *)container);
  2597. int64_t *idx = VariantInternal::get_int(counter);
  2598. (*idx)++;
  2599. if (*idx >= str->length()) {
  2600. int jumpto = _code_ptr[ip + 4];
  2601. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2602. ip = jumpto;
  2603. } else {
  2604. GET_INSTRUCTION_ARG(iterator, 2);
  2605. *VariantInternal::get_string(iterator) = str->substr(*idx, 1);
  2606. ip += 5; // Loop again.
  2607. }
  2608. }
  2609. DISPATCH_OPCODE;
  2610. OPCODE(OPCODE_ITERATE_DICTIONARY) {
  2611. CHECK_SPACE(4);
  2612. GET_INSTRUCTION_ARG(counter, 0);
  2613. GET_INSTRUCTION_ARG(container, 1);
  2614. const Dictionary *dict = VariantInternal::get_dictionary((const Variant *)container);
  2615. const Variant *next = dict->next(counter);
  2616. if (!next) {
  2617. int jumpto = _code_ptr[ip + 4];
  2618. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2619. ip = jumpto;
  2620. } else {
  2621. GET_INSTRUCTION_ARG(iterator, 2);
  2622. *counter = *next;
  2623. *iterator = *next;
  2624. ip += 5; // Loop again.
  2625. }
  2626. }
  2627. DISPATCH_OPCODE;
  2628. OPCODE(OPCODE_ITERATE_ARRAY) {
  2629. CHECK_SPACE(4);
  2630. GET_INSTRUCTION_ARG(counter, 0);
  2631. GET_INSTRUCTION_ARG(container, 1);
  2632. const Array *array = VariantInternal::get_array((const Variant *)container);
  2633. int64_t *idx = VariantInternal::get_int(counter);
  2634. (*idx)++;
  2635. if (*idx >= array->size()) {
  2636. int jumpto = _code_ptr[ip + 4];
  2637. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2638. ip = jumpto;
  2639. } else {
  2640. GET_INSTRUCTION_ARG(iterator, 2);
  2641. *iterator = array->get(*idx);
  2642. ip += 5; // Loop again.
  2643. }
  2644. }
  2645. DISPATCH_OPCODE;
  2646. #define OPCODE_ITERATE_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_ret_get_func) \
  2647. OPCODE(OPCODE_ITERATE_PACKED_##m_var_type##_ARRAY) { \
  2648. CHECK_SPACE(4); \
  2649. GET_INSTRUCTION_ARG(counter, 0); \
  2650. GET_INSTRUCTION_ARG(container, 1); \
  2651. const Vector<m_elem_type> *array = VariantInternal::m_get_func((const Variant *)container); \
  2652. int64_t *idx = VariantInternal::get_int(counter); \
  2653. (*idx)++; \
  2654. if (*idx >= array->size()) { \
  2655. int jumpto = _code_ptr[ip + 4]; \
  2656. GD_ERR_BREAK(jumpto<0 || jumpto> _code_size); \
  2657. ip = jumpto; \
  2658. } else { \
  2659. GET_INSTRUCTION_ARG(iterator, 2); \
  2660. *VariantInternal::m_ret_get_func(iterator) = array->get(*idx); \
  2661. ip += 5; \
  2662. } \
  2663. } \
  2664. DISPATCH_OPCODE
  2665. OPCODE_ITERATE_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, get_int);
  2666. OPCODE_ITERATE_PACKED_ARRAY(INT32, int32_t, get_int32_array, get_int);
  2667. OPCODE_ITERATE_PACKED_ARRAY(INT64, int64_t, get_int64_array, get_int);
  2668. OPCODE_ITERATE_PACKED_ARRAY(FLOAT32, float, get_float32_array, get_float);
  2669. OPCODE_ITERATE_PACKED_ARRAY(FLOAT64, double, get_float64_array, get_float);
  2670. OPCODE_ITERATE_PACKED_ARRAY(STRING, String, get_string_array, get_string);
  2671. OPCODE_ITERATE_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, get_vector2);
  2672. OPCODE_ITERATE_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, get_vector3);
  2673. OPCODE_ITERATE_PACKED_ARRAY(COLOR, Color, get_color_array, get_color);
  2674. OPCODE(OPCODE_ITERATE_OBJECT) {
  2675. CHECK_SPACE(4);
  2676. GET_INSTRUCTION_ARG(counter, 0);
  2677. GET_INSTRUCTION_ARG(container, 1);
  2678. #ifdef DEBUG_ENABLED
  2679. bool freed = false;
  2680. Object *obj = container->get_validated_object_with_check(freed);
  2681. if (freed) {
  2682. err_text = "Trying to iterate on a previously freed object.";
  2683. OPCODE_BREAK;
  2684. } else if (!obj) {
  2685. err_text = "Trying to iterate on a null value.";
  2686. OPCODE_BREAK;
  2687. }
  2688. #else
  2689. Object *obj = *VariantInternal::get_object(container);
  2690. #endif
  2691. Array ref;
  2692. ref.push_back(*counter);
  2693. Variant vref;
  2694. VariantInternal::initialize(&vref, Variant::ARRAY);
  2695. *VariantInternal::get_array(&vref) = ref;
  2696. Variant **args = instruction_args; // Overriding an instruction argument, but we don't need access to that anymore.
  2697. args[0] = &vref;
  2698. Callable::CallError ce;
  2699. Variant has_next = obj->callp(CoreStringNames::get_singleton()->_iter_next, (const Variant **)args, 1, ce);
  2700. #ifdef DEBUG_ENABLED
  2701. if (ce.error != Callable::CallError::CALL_OK) {
  2702. err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
  2703. OPCODE_BREAK;
  2704. }
  2705. #endif
  2706. if (!has_next.booleanize()) {
  2707. int jumpto = _code_ptr[ip + 4];
  2708. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2709. ip = jumpto;
  2710. } else {
  2711. GET_INSTRUCTION_ARG(iterator, 2);
  2712. *iterator = obj->callp(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce);
  2713. #ifdef DEBUG_ENABLED
  2714. if (ce.error != Callable::CallError::CALL_OK) {
  2715. err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
  2716. OPCODE_BREAK;
  2717. }
  2718. #endif
  2719. ip += 5; // Loop again.
  2720. }
  2721. }
  2722. DISPATCH_OPCODE;
  2723. OPCODE(OPCODE_STORE_GLOBAL) {
  2724. CHECK_SPACE(3);
  2725. int global_idx = _code_ptr[ip + 2];
  2726. GD_ERR_BREAK(global_idx < 0 || global_idx >= GDScriptLanguage::get_singleton()->get_global_array_size());
  2727. GET_INSTRUCTION_ARG(dst, 0);
  2728. *dst = GDScriptLanguage::get_singleton()->get_global_array()[global_idx];
  2729. ip += 3;
  2730. }
  2731. DISPATCH_OPCODE;
  2732. OPCODE(OPCODE_STORE_NAMED_GLOBAL) {
  2733. CHECK_SPACE(3);
  2734. int globalname_idx = _code_ptr[ip + 2];
  2735. GD_ERR_BREAK(globalname_idx < 0 || globalname_idx >= _global_names_count);
  2736. const StringName *globalname = &_global_names_ptr[globalname_idx];
  2737. GET_INSTRUCTION_ARG(dst, 0);
  2738. *dst = GDScriptLanguage::get_singleton()->get_named_globals_map()[*globalname];
  2739. ip += 3;
  2740. }
  2741. DISPATCH_OPCODE;
  2742. #define OPCODE_TYPE_ADJUST(m_v_type, m_c_type) \
  2743. OPCODE(OPCODE_TYPE_ADJUST_##m_v_type) { \
  2744. CHECK_SPACE(2); \
  2745. GET_INSTRUCTION_ARG(arg, 0); \
  2746. VariantTypeAdjust<m_c_type>::adjust(arg); \
  2747. ip += 2; \
  2748. } \
  2749. DISPATCH_OPCODE
  2750. OPCODE_TYPE_ADJUST(BOOL, bool);
  2751. OPCODE_TYPE_ADJUST(INT, int64_t);
  2752. OPCODE_TYPE_ADJUST(FLOAT, double);
  2753. OPCODE_TYPE_ADJUST(STRING, String);
  2754. OPCODE_TYPE_ADJUST(VECTOR2, Vector2);
  2755. OPCODE_TYPE_ADJUST(VECTOR2I, Vector2i);
  2756. OPCODE_TYPE_ADJUST(RECT2, Rect2);
  2757. OPCODE_TYPE_ADJUST(RECT2I, Rect2i);
  2758. OPCODE_TYPE_ADJUST(VECTOR3, Vector3);
  2759. OPCODE_TYPE_ADJUST(VECTOR3I, Vector3i);
  2760. OPCODE_TYPE_ADJUST(TRANSFORM2D, Transform2D);
  2761. OPCODE_TYPE_ADJUST(PLANE, Plane);
  2762. OPCODE_TYPE_ADJUST(QUATERNION, Quaternion);
  2763. OPCODE_TYPE_ADJUST(AABB, AABB);
  2764. OPCODE_TYPE_ADJUST(BASIS, Basis);
  2765. OPCODE_TYPE_ADJUST(TRANSFORM3D, Transform3D);
  2766. OPCODE_TYPE_ADJUST(COLOR, Color);
  2767. OPCODE_TYPE_ADJUST(STRING_NAME, StringName);
  2768. OPCODE_TYPE_ADJUST(NODE_PATH, NodePath);
  2769. OPCODE_TYPE_ADJUST(RID, RID);
  2770. OPCODE_TYPE_ADJUST(OBJECT, Object *);
  2771. OPCODE_TYPE_ADJUST(CALLABLE, Callable);
  2772. OPCODE_TYPE_ADJUST(SIGNAL, Signal);
  2773. OPCODE_TYPE_ADJUST(DICTIONARY, Dictionary);
  2774. OPCODE_TYPE_ADJUST(ARRAY, Array);
  2775. OPCODE_TYPE_ADJUST(PACKED_BYTE_ARRAY, PackedByteArray);
  2776. OPCODE_TYPE_ADJUST(PACKED_INT32_ARRAY, PackedInt32Array);
  2777. OPCODE_TYPE_ADJUST(PACKED_INT64_ARRAY, PackedInt64Array);
  2778. OPCODE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY, PackedFloat32Array);
  2779. OPCODE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY, PackedFloat64Array);
  2780. OPCODE_TYPE_ADJUST(PACKED_STRING_ARRAY, PackedStringArray);
  2781. OPCODE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY, PackedVector2Array);
  2782. OPCODE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY, PackedVector3Array);
  2783. OPCODE_TYPE_ADJUST(PACKED_COLOR_ARRAY, PackedColorArray);
  2784. OPCODE(OPCODE_ASSERT) {
  2785. CHECK_SPACE(3);
  2786. #ifdef DEBUG_ENABLED
  2787. GET_INSTRUCTION_ARG(test, 0);
  2788. bool result = test->booleanize();
  2789. if (!result) {
  2790. String message_str;
  2791. if (_code_ptr[ip + 2] != 0) {
  2792. GET_INSTRUCTION_ARG(message, 1);
  2793. message_str = *message;
  2794. }
  2795. if (message_str.is_empty()) {
  2796. err_text = "Assertion failed.";
  2797. } else {
  2798. err_text = "Assertion failed: " + message_str;
  2799. }
  2800. OPCODE_BREAK;
  2801. }
  2802. #endif
  2803. ip += 3;
  2804. }
  2805. DISPATCH_OPCODE;
  2806. OPCODE(OPCODE_BREAKPOINT) {
  2807. #ifdef DEBUG_ENABLED
  2808. if (EngineDebugger::is_active()) {
  2809. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  2810. }
  2811. #endif
  2812. ip += 1;
  2813. }
  2814. DISPATCH_OPCODE;
  2815. OPCODE(OPCODE_LINE) {
  2816. CHECK_SPACE(2);
  2817. line = _code_ptr[ip + 1];
  2818. ip += 2;
  2819. if (EngineDebugger::is_active()) {
  2820. // line
  2821. bool do_break = false;
  2822. if (EngineDebugger::get_script_debugger()->get_lines_left() > 0) {
  2823. if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
  2824. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  2825. }
  2826. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
  2827. do_break = true;
  2828. }
  2829. }
  2830. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
  2831. do_break = true;
  2832. }
  2833. if (do_break) {
  2834. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  2835. }
  2836. EngineDebugger::get_singleton()->line_poll();
  2837. }
  2838. }
  2839. DISPATCH_OPCODE;
  2840. OPCODE(OPCODE_END) {
  2841. #ifdef DEBUG_ENABLED
  2842. exit_ok = true;
  2843. #endif
  2844. OPCODE_BREAK;
  2845. }
  2846. #if 0 // Enable for debugging.
  2847. default: {
  2848. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  2849. OPCODE_BREAK;
  2850. }
  2851. #endif
  2852. }
  2853. OPCODES_END
  2854. #ifdef DEBUG_ENABLED
  2855. if (exit_ok) {
  2856. OPCODE_OUT;
  2857. }
  2858. //error
  2859. // function, file, line, error, explanation
  2860. String err_file;
  2861. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && !p_instance->script->path.is_empty()) {
  2862. err_file = p_instance->script->path;
  2863. } else if (script) {
  2864. err_file = script->path;
  2865. }
  2866. if (err_file.is_empty()) {
  2867. err_file = "<built-in>";
  2868. }
  2869. String err_func = name;
  2870. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && !p_instance->script->name.is_empty()) {
  2871. err_func = p_instance->script->name + "." + err_func;
  2872. }
  2873. int err_line = line;
  2874. if (err_text.is_empty()) {
  2875. err_text = "Internal script error! Opcode: " + itos(last_opcode) + " (please report).";
  2876. }
  2877. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  2878. // debugger break did not happen
  2879. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), false, ERR_HANDLER_SCRIPT);
  2880. }
  2881. // Get a default return type in case of failure
  2882. retvalue = _get_default_variant_for_data_type(return_type);
  2883. #endif
  2884. OPCODE_OUT;
  2885. }
  2886. OPCODES_OUT
  2887. #ifdef DEBUG_ENABLED
  2888. if (GDScriptLanguage::get_singleton()->profiling) {
  2889. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  2890. profile.total_time += time_taken;
  2891. profile.self_time += time_taken - function_call_time;
  2892. profile.frame_total_time += time_taken;
  2893. profile.frame_self_time += time_taken - function_call_time;
  2894. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  2895. }
  2896. // Check if this is not the last time it was interrupted by `await` or if it's the first time executing.
  2897. // If that is the case then we exit the function as normal. Otherwise we postpone it until the last `await` is completed.
  2898. // This ensures the call stack can be properly shown when using `await`, showing what resumed the function.
  2899. if (!p_state || awaited) {
  2900. if (EngineDebugger::is_active()) {
  2901. GDScriptLanguage::get_singleton()->exit_function();
  2902. }
  2903. #endif
  2904. // Free stack, except reserved addresses.
  2905. for (int i = 3; i < _stack_size; i++) {
  2906. stack[i].~Variant();
  2907. }
  2908. #ifdef DEBUG_ENABLED
  2909. }
  2910. #endif
  2911. // Always free reserved addresses, since they are never copied.
  2912. for (int i = 0; i < 3; i++) {
  2913. stack[i].~Variant();
  2914. }
  2915. return retvalue;
  2916. }