gdscript_vm.cpp 106 KB

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