gdscript_vm.cpp 107 KB

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