gdscript_vm.cpp 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327
  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<Quat>::init, // QUAT.
  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_QUAT, \
  236. &&OPCODE_CALL_PTRCALL_AABB, \
  237. &&OPCODE_CALL_PTRCALL_BASIS, \
  238. &&OPCODE_CALL_PTRCALL_TRANSFORM, \
  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_QUAT, \
  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_QUAT get_quat
  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_TRANSFORM 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. Callable::CallError err;
  1069. Variant::construct(to_type, *dst, (const Variant **)&src, 1, err);
  1070. #ifdef DEBUG_ENABLED
  1071. if (err.error != Callable::CallError::CALL_OK) {
  1072. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  1073. OPCODE_BREAK;
  1074. }
  1075. #endif
  1076. ip += 4;
  1077. }
  1078. DISPATCH_OPCODE;
  1079. OPCODE(OPCODE_CAST_TO_NATIVE) {
  1080. CHECK_SPACE(4);
  1081. GET_INSTRUCTION_ARG(src, 0);
  1082. GET_INSTRUCTION_ARG(dst, 1);
  1083. GET_INSTRUCTION_ARG(to_type, 2);
  1084. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  1085. GD_ERR_BREAK(!nc);
  1086. #ifdef DEBUG_ENABLED
  1087. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1088. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  1089. OPCODE_BREAK;
  1090. }
  1091. #endif
  1092. Object *src_obj = src->operator Object *();
  1093. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  1094. *dst = Variant(); // invalid cast, assign NULL
  1095. } else {
  1096. *dst = *src;
  1097. }
  1098. ip += 4;
  1099. }
  1100. DISPATCH_OPCODE;
  1101. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  1102. CHECK_SPACE(4);
  1103. GET_INSTRUCTION_ARG(src, 0);
  1104. GET_INSTRUCTION_ARG(dst, 1);
  1105. GET_INSTRUCTION_ARG(to_type, 2);
  1106. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  1107. GD_ERR_BREAK(!base_type);
  1108. #ifdef DEBUG_ENABLED
  1109. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  1110. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  1111. OPCODE_BREAK;
  1112. }
  1113. #endif
  1114. bool valid = false;
  1115. if (src->get_type() != Variant::NIL && src->operator Object *() != nullptr) {
  1116. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  1117. if (scr_inst) {
  1118. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  1119. while (src_type) {
  1120. if (src_type == base_type) {
  1121. valid = true;
  1122. break;
  1123. }
  1124. src_type = src_type->get_base_script().ptr();
  1125. }
  1126. }
  1127. }
  1128. if (valid) {
  1129. *dst = *src; // Valid cast, copy the source object
  1130. } else {
  1131. *dst = Variant(); // invalid cast, assign NULL
  1132. }
  1133. ip += 4;
  1134. }
  1135. DISPATCH_OPCODE;
  1136. OPCODE(OPCODE_CONSTRUCT) {
  1137. CHECK_SPACE(2 + instr_arg_count);
  1138. ip += instr_arg_count;
  1139. int argc = _code_ptr[ip + 1];
  1140. Variant::Type t = Variant::Type(_code_ptr[ip + 2]);
  1141. Variant **argptrs = instruction_args;
  1142. GET_INSTRUCTION_ARG(dst, argc);
  1143. Callable::CallError err;
  1144. Variant::construct(t, *dst, (const Variant **)argptrs, argc, err);
  1145. #ifdef DEBUG_ENABLED
  1146. if (err.error != Callable::CallError::CALL_OK) {
  1147. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  1148. OPCODE_BREAK;
  1149. }
  1150. #endif
  1151. ip += 3;
  1152. }
  1153. DISPATCH_OPCODE;
  1154. OPCODE(OPCODE_CONSTRUCT_VALIDATED) {
  1155. CHECK_SPACE(2 + instr_arg_count);
  1156. ip += instr_arg_count;
  1157. int argc = _code_ptr[ip + 1];
  1158. int constructor_idx = _code_ptr[ip + 2];
  1159. GD_ERR_BREAK(constructor_idx < 0 || constructor_idx >= _constructors_count);
  1160. Variant::ValidatedConstructor constructor = _constructors_ptr[constructor_idx];
  1161. Variant **argptrs = instruction_args;
  1162. GET_INSTRUCTION_ARG(dst, argc);
  1163. constructor(dst, (const Variant **)argptrs);
  1164. ip += 3;
  1165. }
  1166. DISPATCH_OPCODE;
  1167. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  1168. CHECK_SPACE(1 + instr_arg_count);
  1169. ip += instr_arg_count;
  1170. int argc = _code_ptr[ip + 1];
  1171. Array array;
  1172. array.resize(argc);
  1173. for (int i = 0; i < argc; i++) {
  1174. array[i] = *(instruction_args[i]);
  1175. }
  1176. GET_INSTRUCTION_ARG(dst, argc);
  1177. *dst = Variant(); // Clear potential previous typed array.
  1178. *dst = array;
  1179. ip += 2;
  1180. }
  1181. DISPATCH_OPCODE;
  1182. OPCODE(OPCODE_CONSTRUCT_TYPED_ARRAY) {
  1183. CHECK_SPACE(3 + instr_arg_count);
  1184. ip += instr_arg_count;
  1185. int argc = _code_ptr[ip + 1];
  1186. GET_INSTRUCTION_ARG(script_type, argc + 1);
  1187. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 2];
  1188. int native_type_idx = _code_ptr[ip + 3];
  1189. GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
  1190. const StringName native_type = _global_names_ptr[native_type_idx];
  1191. Array array;
  1192. array.set_typed(builtin_type, native_type, script_type);
  1193. array.resize(argc);
  1194. for (int i = 0; i < argc; i++) {
  1195. array[i] = *(instruction_args[i]);
  1196. }
  1197. GET_INSTRUCTION_ARG(dst, argc);
  1198. *dst = Variant(); // Clear potential previous typed array.
  1199. *dst = array;
  1200. ip += 4;
  1201. }
  1202. DISPATCH_OPCODE;
  1203. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  1204. CHECK_SPACE(2 + instr_arg_count);
  1205. ip += instr_arg_count;
  1206. int argc = _code_ptr[ip + 1];
  1207. Dictionary dict;
  1208. for (int i = 0; i < argc; i++) {
  1209. GET_INSTRUCTION_ARG(k, i * 2 + 0);
  1210. GET_INSTRUCTION_ARG(v, i * 2 + 1);
  1211. dict[*k] = *v;
  1212. }
  1213. GET_INSTRUCTION_ARG(dst, argc * 2);
  1214. *dst = dict;
  1215. ip += 2;
  1216. }
  1217. DISPATCH_OPCODE;
  1218. OPCODE(OPCODE_CALL_ASYNC)
  1219. OPCODE(OPCODE_CALL_RETURN)
  1220. OPCODE(OPCODE_CALL) {
  1221. CHECK_SPACE(3 + instr_arg_count);
  1222. bool call_ret = (_code_ptr[ip] & INSTR_MASK) != OPCODE_CALL;
  1223. #ifdef DEBUG_ENABLED
  1224. bool call_async = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_ASYNC;
  1225. #endif
  1226. ip += instr_arg_count;
  1227. int argc = _code_ptr[ip + 1];
  1228. GD_ERR_BREAK(argc < 0);
  1229. int methodname_idx = _code_ptr[ip + 2];
  1230. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  1231. const StringName *methodname = &_global_names_ptr[methodname_idx];
  1232. GET_INSTRUCTION_ARG(base, argc);
  1233. Variant **argptrs = instruction_args;
  1234. #ifdef DEBUG_ENABLED
  1235. uint64_t call_time = 0;
  1236. if (GDScriptLanguage::get_singleton()->profiling) {
  1237. call_time = OS::get_singleton()->get_ticks_usec();
  1238. }
  1239. #endif
  1240. Callable::CallError err;
  1241. if (call_ret) {
  1242. GET_INSTRUCTION_ARG(ret, argc + 1);
  1243. base->call(*methodname, (const Variant **)argptrs, argc, *ret, err);
  1244. #ifdef DEBUG_ENABLED
  1245. if (!call_async && ret->get_type() == Variant::OBJECT) {
  1246. // Check if getting a function state without await.
  1247. bool was_freed = false;
  1248. Object *obj = ret->get_validated_object_with_check(was_freed);
  1249. if (was_freed) {
  1250. err_text = "Got a freed object as a result of the call.";
  1251. OPCODE_BREAK;
  1252. }
  1253. if (obj && obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1254. err_text = R"(Trying to call an async function without "await".)";
  1255. OPCODE_BREAK;
  1256. }
  1257. }
  1258. #endif
  1259. } else {
  1260. Variant ret;
  1261. base->call(*methodname, (const Variant **)argptrs, argc, ret, err);
  1262. }
  1263. #ifdef DEBUG_ENABLED
  1264. if (GDScriptLanguage::get_singleton()->profiling) {
  1265. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1266. }
  1267. if (err.error != Callable::CallError::CALL_OK) {
  1268. String methodstr = *methodname;
  1269. String basestr = _get_var_type(base);
  1270. bool is_callable = false;
  1271. if (methodstr == "call") {
  1272. if (argc >= 1 && base->get_type() != Variant::CALLABLE) {
  1273. methodstr = String(*argptrs[0]) + " (via call)";
  1274. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1275. err.argument += 1;
  1276. }
  1277. } else {
  1278. methodstr = base->operator String() + " (Callable)";
  1279. is_callable = true;
  1280. }
  1281. } else if (methodstr == "free") {
  1282. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1283. if (base->is_ref()) {
  1284. err_text = "Attempted to free a reference.";
  1285. OPCODE_BREAK;
  1286. } else if (base->get_type() == Variant::OBJECT) {
  1287. err_text = "Attempted to free a locked object (calling or emitting).";
  1288. OPCODE_BREAK;
  1289. }
  1290. }
  1291. } else if (methodstr == "call_recursive" && basestr == "TreeItem") {
  1292. if (argc >= 1) {
  1293. methodstr = String(*argptrs[0]) + " (via TreeItem.call_recursive)";
  1294. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1295. err.argument += 1;
  1296. }
  1297. }
  1298. }
  1299. err_text = _get_call_error(err, "function '" + methodstr + (is_callable ? "" : "' in base '" + basestr) + "'", (const Variant **)argptrs);
  1300. OPCODE_BREAK;
  1301. }
  1302. #endif
  1303. ip += 3;
  1304. }
  1305. DISPATCH_OPCODE;
  1306. OPCODE(OPCODE_CALL_METHOD_BIND)
  1307. OPCODE(OPCODE_CALL_METHOD_BIND_RET) {
  1308. CHECK_SPACE(3 + instr_arg_count);
  1309. bool call_ret = (_code_ptr[ip] & INSTR_MASK) == OPCODE_CALL_METHOD_BIND_RET;
  1310. ip += instr_arg_count;
  1311. int argc = _code_ptr[ip + 1];
  1312. GD_ERR_BREAK(argc < 0);
  1313. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1314. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1315. GET_INSTRUCTION_ARG(base, argc);
  1316. #ifdef DEBUG_ENABLED
  1317. bool freed = false;
  1318. Object *base_obj = base->get_validated_object_with_check(freed);
  1319. if (freed) {
  1320. err_text = "Trying to call a function on a previously freed instance.";
  1321. OPCODE_BREAK;
  1322. } else if (!base_obj) {
  1323. err_text = "Trying to call a function on a null value.";
  1324. OPCODE_BREAK;
  1325. }
  1326. #else
  1327. Object *base_obj = base->operator Object *();
  1328. #endif
  1329. Variant **argptrs = instruction_args;
  1330. #ifdef DEBUG_ENABLED
  1331. uint64_t call_time = 0;
  1332. if (GDScriptLanguage::get_singleton()->profiling) {
  1333. call_time = OS::get_singleton()->get_ticks_usec();
  1334. }
  1335. #endif
  1336. Callable::CallError err;
  1337. if (call_ret) {
  1338. GET_INSTRUCTION_ARG(ret, argc + 1);
  1339. *ret = method->call(base_obj, (const Variant **)argptrs, argc, err);
  1340. } else {
  1341. method->call(base_obj, (const Variant **)argptrs, argc, err);
  1342. }
  1343. #ifdef DEBUG_ENABLED
  1344. if (GDScriptLanguage::get_singleton()->profiling) {
  1345. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1346. }
  1347. if (err.error != Callable::CallError::CALL_OK) {
  1348. String methodstr = method->get_name();
  1349. String basestr = _get_var_type(base);
  1350. if (methodstr == "call") {
  1351. if (argc >= 1) {
  1352. methodstr = String(*argptrs[0]) + " (via call)";
  1353. if (err.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  1354. err.argument += 1;
  1355. }
  1356. }
  1357. } else if (methodstr == "free") {
  1358. if (err.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
  1359. if (base->is_ref()) {
  1360. err_text = "Attempted to free a reference.";
  1361. OPCODE_BREAK;
  1362. } else if (base->get_type() == Variant::OBJECT) {
  1363. err_text = "Attempted to free a locked object (calling or emitting).";
  1364. OPCODE_BREAK;
  1365. }
  1366. }
  1367. }
  1368. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  1369. OPCODE_BREAK;
  1370. }
  1371. #endif
  1372. ip += 3;
  1373. }
  1374. DISPATCH_OPCODE;
  1375. OPCODE(OPCODE_CALL_BUILTIN_STATIC) {
  1376. CHECK_SPACE(4 + instr_arg_count);
  1377. ip += instr_arg_count;
  1378. GD_ERR_BREAK(_code_ptr[ip + 1] < 0 || _code_ptr[ip + 1] >= Variant::VARIANT_MAX);
  1379. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 1];
  1380. int methodname_idx = _code_ptr[ip + 2];
  1381. GD_ERR_BREAK(methodname_idx < 0 || methodname_idx >= _global_names_count);
  1382. const StringName *methodname = &_global_names_ptr[methodname_idx];
  1383. int argc = _code_ptr[ip + 3];
  1384. GD_ERR_BREAK(argc < 0);
  1385. GET_INSTRUCTION_ARG(ret, argc);
  1386. const Variant **argptrs = const_cast<const Variant **>(instruction_args);
  1387. #ifdef DEBUG_ENABLED
  1388. uint64_t call_time = 0;
  1389. if (GDScriptLanguage::get_singleton()->profiling) {
  1390. call_time = OS::get_singleton()->get_ticks_usec();
  1391. }
  1392. #endif
  1393. Callable::CallError err;
  1394. Variant::call_static(builtin_type, *methodname, argptrs, argc, *ret, err);
  1395. #ifdef DEBUG_ENABLED
  1396. if (GDScriptLanguage::get_singleton()->profiling) {
  1397. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1398. }
  1399. if (err.error != Callable::CallError::CALL_OK) {
  1400. err_text = _get_call_error(err, "static function '" + methodname->operator String() + "' in type '" + Variant::get_type_name(builtin_type) + "'", argptrs);
  1401. OPCODE_BREAK;
  1402. }
  1403. #endif
  1404. ip += 4;
  1405. }
  1406. DISPATCH_OPCODE;
  1407. #ifdef DEBUG_ENABLED
  1408. #define OPCODE_CALL_PTR(m_type) \
  1409. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1410. CHECK_SPACE(3 + instr_arg_count); \
  1411. ip += instr_arg_count; \
  1412. int argc = _code_ptr[ip + 1]; \
  1413. GD_ERR_BREAK(argc < 0); \
  1414. GET_INSTRUCTION_ARG(base, argc); \
  1415. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count); \
  1416. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1417. bool freed = false; \
  1418. Object *base_obj = base->get_validated_object_with_check(freed); \
  1419. if (freed) { \
  1420. err_text = "Trying to call a function on a previously freed instance."; \
  1421. OPCODE_BREAK; \
  1422. } else if (!base_obj) { \
  1423. err_text = "Trying to call a function on a null value."; \
  1424. OPCODE_BREAK; \
  1425. } \
  1426. const void **argptrs = call_args_ptr; \
  1427. for (int i = 0; i < argc; i++) { \
  1428. GET_INSTRUCTION_ARG(v, i); \
  1429. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1430. } \
  1431. uint64_t call_time = 0; \
  1432. if (GDScriptLanguage::get_singleton()->profiling) { \
  1433. call_time = OS::get_singleton()->get_ticks_usec(); \
  1434. } \
  1435. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1436. VariantInternal::initialize(ret, Variant::m_type); \
  1437. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1438. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1439. if (GDScriptLanguage::get_singleton()->profiling) { \
  1440. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time; \
  1441. } \
  1442. ip += 3; \
  1443. } \
  1444. DISPATCH_OPCODE
  1445. #else
  1446. #define OPCODE_CALL_PTR(m_type) \
  1447. OPCODE(OPCODE_CALL_PTRCALL_##m_type) { \
  1448. CHECK_SPACE(3 + instr_arg_count); \
  1449. int argc = _code_ptr[ip + 1]; \
  1450. GET_INSTRUCTION_ARG(base, argc); \
  1451. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]]; \
  1452. Object *base_obj = *VariantInternal::get_object(base); \
  1453. const void **argptrs = call_args_ptr; \
  1454. for (int i = 0; i < argc; i++) { \
  1455. GET_INSTRUCTION_ARG(v, i); \
  1456. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v); \
  1457. } \
  1458. GET_INSTRUCTION_ARG(ret, argc + 1); \
  1459. VariantInternal::initialize(ret, Variant::m_type); \
  1460. void *ret_opaque = VariantInternal::OP_GET_##m_type(ret); \
  1461. method->ptrcall(base_obj, argptrs, ret_opaque); \
  1462. ip += 3; \
  1463. } \
  1464. DISPATCH_OPCODE
  1465. #endif
  1466. OPCODE_CALL_PTR(BOOL);
  1467. OPCODE_CALL_PTR(INT);
  1468. OPCODE_CALL_PTR(FLOAT);
  1469. OPCODE_CALL_PTR(STRING);
  1470. OPCODE_CALL_PTR(VECTOR2);
  1471. OPCODE_CALL_PTR(VECTOR2I);
  1472. OPCODE_CALL_PTR(RECT2);
  1473. OPCODE_CALL_PTR(RECT2I);
  1474. OPCODE_CALL_PTR(VECTOR3);
  1475. OPCODE_CALL_PTR(VECTOR3I);
  1476. OPCODE_CALL_PTR(TRANSFORM2D);
  1477. OPCODE_CALL_PTR(PLANE);
  1478. OPCODE_CALL_PTR(QUAT);
  1479. OPCODE_CALL_PTR(AABB);
  1480. OPCODE_CALL_PTR(BASIS);
  1481. OPCODE_CALL_PTR(TRANSFORM);
  1482. OPCODE_CALL_PTR(COLOR);
  1483. OPCODE_CALL_PTR(STRING_NAME);
  1484. OPCODE_CALL_PTR(NODE_PATH);
  1485. OPCODE_CALL_PTR(RID);
  1486. OPCODE_CALL_PTR(CALLABLE);
  1487. OPCODE_CALL_PTR(SIGNAL);
  1488. OPCODE_CALL_PTR(DICTIONARY);
  1489. OPCODE_CALL_PTR(ARRAY);
  1490. OPCODE_CALL_PTR(PACKED_BYTE_ARRAY);
  1491. OPCODE_CALL_PTR(PACKED_INT32_ARRAY);
  1492. OPCODE_CALL_PTR(PACKED_INT64_ARRAY);
  1493. OPCODE_CALL_PTR(PACKED_FLOAT32_ARRAY);
  1494. OPCODE_CALL_PTR(PACKED_FLOAT64_ARRAY);
  1495. OPCODE_CALL_PTR(PACKED_STRING_ARRAY);
  1496. OPCODE_CALL_PTR(PACKED_VECTOR2_ARRAY);
  1497. OPCODE_CALL_PTR(PACKED_VECTOR3_ARRAY);
  1498. OPCODE_CALL_PTR(PACKED_COLOR_ARRAY);
  1499. OPCODE(OPCODE_CALL_PTRCALL_OBJECT) {
  1500. CHECK_SPACE(3 + instr_arg_count);
  1501. ip += instr_arg_count;
  1502. int argc = _code_ptr[ip + 1];
  1503. GD_ERR_BREAK(argc < 0);
  1504. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1505. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1506. GET_INSTRUCTION_ARG(base, argc);
  1507. #ifdef DEBUG_ENABLED
  1508. bool freed = false;
  1509. Object *base_obj = base->get_validated_object_with_check(freed);
  1510. if (freed) {
  1511. err_text = "Trying to call a function on a previously freed instance.";
  1512. OPCODE_BREAK;
  1513. } else if (!base_obj) {
  1514. err_text = "Trying to call a function on a null value.";
  1515. OPCODE_BREAK;
  1516. }
  1517. #else
  1518. Object *base_obj = *VariantInternal::get_object(base);
  1519. #endif
  1520. const void **argptrs = call_args_ptr;
  1521. for (int i = 0; i < argc; i++) {
  1522. GET_INSTRUCTION_ARG(v, i);
  1523. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1524. }
  1525. #ifdef DEBUG_ENABLED
  1526. uint64_t call_time = 0;
  1527. if (GDScriptLanguage::get_singleton()->profiling) {
  1528. call_time = OS::get_singleton()->get_ticks_usec();
  1529. }
  1530. #endif
  1531. GET_INSTRUCTION_ARG(ret, argc + 1);
  1532. VariantInternal::initialize(ret, Variant::OBJECT);
  1533. Object **ret_opaque = VariantInternal::get_object(ret);
  1534. method->ptrcall(base_obj, argptrs, ret_opaque);
  1535. VariantInternal::object_assign(ret, *ret_opaque); // Set so ID is correct too.
  1536. #ifdef DEBUG_ENABLED
  1537. if (GDScriptLanguage::get_singleton()->profiling) {
  1538. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1539. }
  1540. #endif
  1541. ip += 3;
  1542. }
  1543. DISPATCH_OPCODE;
  1544. OPCODE(OPCODE_CALL_PTRCALL_NO_RETURN) {
  1545. CHECK_SPACE(3 + instr_arg_count);
  1546. ip += instr_arg_count;
  1547. int argc = _code_ptr[ip + 1];
  1548. GD_ERR_BREAK(argc < 0);
  1549. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _methods_count);
  1550. MethodBind *method = _methods_ptr[_code_ptr[ip + 2]];
  1551. GET_INSTRUCTION_ARG(base, argc);
  1552. #ifdef DEBUG_ENABLED
  1553. bool freed = false;
  1554. Object *base_obj = base->get_validated_object_with_check(freed);
  1555. if (freed) {
  1556. err_text = "Trying to call a function on a previously freed instance.";
  1557. OPCODE_BREAK;
  1558. } else if (!base_obj) {
  1559. err_text = "Trying to call a function on a null value.";
  1560. OPCODE_BREAK;
  1561. }
  1562. #else
  1563. Object *base_obj = *VariantInternal::get_object(base);
  1564. #endif
  1565. const void **argptrs = call_args_ptr;
  1566. for (int i = 0; i < argc; i++) {
  1567. GET_INSTRUCTION_ARG(v, i);
  1568. argptrs[i] = VariantInternal::get_opaque_pointer((const Variant *)v);
  1569. }
  1570. #ifdef DEBUG_ENABLED
  1571. uint64_t call_time = 0;
  1572. if (GDScriptLanguage::get_singleton()->profiling) {
  1573. call_time = OS::get_singleton()->get_ticks_usec();
  1574. }
  1575. #endif
  1576. GET_INSTRUCTION_ARG(ret, argc + 1);
  1577. VariantInternal::initialize(ret, Variant::NIL);
  1578. method->ptrcall(base_obj, argptrs, nullptr);
  1579. #ifdef DEBUG_ENABLED
  1580. if (GDScriptLanguage::get_singleton()->profiling) {
  1581. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1582. }
  1583. #endif
  1584. ip += 3;
  1585. }
  1586. DISPATCH_OPCODE;
  1587. OPCODE(OPCODE_CALL_BUILTIN_TYPE_VALIDATED) {
  1588. CHECK_SPACE(3 + instr_arg_count);
  1589. ip += instr_arg_count;
  1590. int argc = _code_ptr[ip + 1];
  1591. GD_ERR_BREAK(argc < 0);
  1592. GET_INSTRUCTION_ARG(base, argc);
  1593. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _builtin_methods_count);
  1594. Variant::ValidatedBuiltInMethod method = _builtin_methods_ptr[_code_ptr[ip + 2]];
  1595. Variant **argptrs = instruction_args;
  1596. #ifdef DEBUG_ENABLED
  1597. uint64_t call_time = 0;
  1598. if (GDScriptLanguage::get_singleton()->profiling) {
  1599. call_time = OS::get_singleton()->get_ticks_usec();
  1600. }
  1601. #endif
  1602. GET_INSTRUCTION_ARG(ret, argc + 1);
  1603. method(base, (const Variant **)argptrs, argc, ret);
  1604. #ifdef DEBUG_ENABLED
  1605. if (GDScriptLanguage::get_singleton()->profiling) {
  1606. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  1607. }
  1608. #endif
  1609. ip += 3;
  1610. }
  1611. DISPATCH_OPCODE;
  1612. OPCODE(OPCODE_CALL_UTILITY) {
  1613. CHECK_SPACE(3 + instr_arg_count);
  1614. ip += instr_arg_count;
  1615. int argc = _code_ptr[ip + 1];
  1616. GD_ERR_BREAK(argc < 0);
  1617. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _global_names_count);
  1618. StringName function = _global_names_ptr[_code_ptr[ip + 2]];
  1619. Variant **argptrs = instruction_args;
  1620. GET_INSTRUCTION_ARG(dst, argc);
  1621. Callable::CallError err;
  1622. Variant::call_utility_function(function, dst, (const Variant **)argptrs, argc, err);
  1623. #ifdef DEBUG_ENABLED
  1624. if (err.error != Callable::CallError::CALL_OK) {
  1625. String methodstr = function;
  1626. if (dst->get_type() == Variant::STRING) {
  1627. // Call provided error string.
  1628. err_text = "Error calling utility function '" + methodstr + "': " + String(*dst);
  1629. } else {
  1630. err_text = _get_call_error(err, "utility function '" + methodstr + "'", (const Variant **)argptrs);
  1631. }
  1632. OPCODE_BREAK;
  1633. }
  1634. #endif
  1635. ip += 3;
  1636. }
  1637. DISPATCH_OPCODE;
  1638. OPCODE(OPCODE_CALL_UTILITY_VALIDATED) {
  1639. CHECK_SPACE(3 + instr_arg_count);
  1640. ip += instr_arg_count;
  1641. int argc = _code_ptr[ip + 1];
  1642. GD_ERR_BREAK(argc < 0);
  1643. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _utilities_count);
  1644. Variant::ValidatedUtilityFunction function = _utilities_ptr[_code_ptr[ip + 2]];
  1645. Variant **argptrs = instruction_args;
  1646. GET_INSTRUCTION_ARG(dst, argc);
  1647. function(dst, (const Variant **)argptrs, argc);
  1648. ip += 3;
  1649. }
  1650. DISPATCH_OPCODE;
  1651. OPCODE(OPCODE_CALL_GDSCRIPT_UTILITY) {
  1652. CHECK_SPACE(3 + instr_arg_count);
  1653. ip += instr_arg_count;
  1654. int argc = _code_ptr[ip + 1];
  1655. GD_ERR_BREAK(argc < 0);
  1656. GD_ERR_BREAK(_code_ptr[ip + 2] < 0 || _code_ptr[ip + 2] >= _gds_utilities_count);
  1657. GDScriptUtilityFunctions::FunctionPtr function = _gds_utilities_ptr[_code_ptr[ip + 2]];
  1658. Variant **argptrs = instruction_args;
  1659. GET_INSTRUCTION_ARG(dst, argc);
  1660. Callable::CallError err;
  1661. function(dst, (const Variant **)argptrs, argc, err);
  1662. #ifdef DEBUG_ENABLED
  1663. if (err.error != Callable::CallError::CALL_OK) {
  1664. // TODO: Add this information in debug.
  1665. String methodstr = "<unknown function>";
  1666. if (dst->get_type() == Variant::STRING) {
  1667. // Call provided error string.
  1668. err_text = "Error calling GDScript utility function '" + methodstr + "': " + String(*dst);
  1669. } else {
  1670. err_text = _get_call_error(err, "GDScript utility function '" + methodstr + "'", (const Variant **)argptrs);
  1671. }
  1672. OPCODE_BREAK;
  1673. }
  1674. #endif
  1675. ip += 3;
  1676. }
  1677. DISPATCH_OPCODE;
  1678. OPCODE(OPCODE_CALL_SELF_BASE) {
  1679. CHECK_SPACE(3 + instr_arg_count);
  1680. ip += instr_arg_count;
  1681. int self_fun = _code_ptr[ip + 1];
  1682. #ifdef DEBUG_ENABLED
  1683. if (self_fun < 0 || self_fun >= _global_names_count) {
  1684. err_text = "compiler bug, function name not found";
  1685. OPCODE_BREAK;
  1686. }
  1687. #endif
  1688. const StringName *methodname = &_global_names_ptr[self_fun];
  1689. int argc = _code_ptr[ip + 2];
  1690. GD_ERR_BREAK(argc < 0);
  1691. Variant **argptrs = instruction_args;
  1692. GET_INSTRUCTION_ARG(dst, argc);
  1693. const GDScript *gds = _script;
  1694. const Map<StringName, GDScriptFunction *>::Element *E = nullptr;
  1695. while (gds->base.ptr()) {
  1696. gds = gds->base.ptr();
  1697. E = gds->member_functions.find(*methodname);
  1698. if (E) {
  1699. break;
  1700. }
  1701. }
  1702. Callable::CallError err;
  1703. if (E) {
  1704. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  1705. } else if (gds->native.ptr()) {
  1706. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1707. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  1708. if (!mb) {
  1709. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1710. } else {
  1711. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  1712. }
  1713. } else {
  1714. err.error = Callable::CallError::CALL_OK;
  1715. }
  1716. } else {
  1717. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  1718. err.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1719. } else {
  1720. err.error = Callable::CallError::CALL_OK;
  1721. }
  1722. }
  1723. if (err.error != Callable::CallError::CALL_OK) {
  1724. String methodstr = *methodname;
  1725. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  1726. OPCODE_BREAK;
  1727. }
  1728. ip += 3;
  1729. }
  1730. DISPATCH_OPCODE;
  1731. OPCODE(OPCODE_AWAIT) {
  1732. CHECK_SPACE(2);
  1733. // Do the oneshot connect.
  1734. GET_INSTRUCTION_ARG(argobj, 0);
  1735. Signal sig;
  1736. bool is_signal = true;
  1737. {
  1738. Variant result = *argobj;
  1739. if (argobj->get_type() == Variant::OBJECT) {
  1740. bool was_freed = false;
  1741. Object *obj = argobj->get_validated_object_with_check(was_freed);
  1742. if (was_freed) {
  1743. err_text = "Trying to await on a freed object.";
  1744. OPCODE_BREAK;
  1745. }
  1746. // Is this even possible to be null at this point?
  1747. if (obj) {
  1748. if (obj->is_class_ptr(GDScriptFunctionState::get_class_ptr_static())) {
  1749. static StringName completed = _scs_create("completed");
  1750. result = Signal(obj, completed);
  1751. }
  1752. }
  1753. }
  1754. if (result.get_type() != Variant::SIGNAL) {
  1755. ip += 4; // Skip OPCODE_AWAIT_RESUME and its data.
  1756. // The stack pointer should be the same, so we don't need to set a return value.
  1757. is_signal = false;
  1758. } else {
  1759. sig = result;
  1760. }
  1761. }
  1762. if (is_signal) {
  1763. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  1764. gdfs->function = this;
  1765. gdfs->state.stack.resize(alloca_size);
  1766. //copy variant stack
  1767. for (int i = 0; i < _stack_size; i++) {
  1768. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  1769. }
  1770. gdfs->state.stack_size = _stack_size;
  1771. gdfs->state.alloca_size = alloca_size;
  1772. gdfs->state.ip = ip + 2;
  1773. gdfs->state.line = line;
  1774. gdfs->state.script = _script;
  1775. {
  1776. MutexLock lock(GDScriptLanguage::get_singleton()->lock);
  1777. _script->pending_func_states.add(&gdfs->scripts_list);
  1778. if (p_instance) {
  1779. gdfs->state.instance = p_instance;
  1780. p_instance->pending_func_states.add(&gdfs->instances_list);
  1781. } else {
  1782. gdfs->state.instance = nullptr;
  1783. }
  1784. }
  1785. #ifdef DEBUG_ENABLED
  1786. gdfs->state.function_name = name;
  1787. gdfs->state.script_path = _script->get_path();
  1788. #endif
  1789. gdfs->state.defarg = defarg;
  1790. gdfs->function = this;
  1791. retvalue = gdfs;
  1792. Error err = sig.connect(Callable(gdfs.ptr(), "_signal_callback"), varray(gdfs), Object::CONNECT_ONESHOT);
  1793. if (err != OK) {
  1794. err_text = "Error connecting to signal: " + sig.get_name() + " during await.";
  1795. OPCODE_BREAK;
  1796. }
  1797. #ifdef DEBUG_ENABLED
  1798. exit_ok = true;
  1799. awaited = true;
  1800. #endif
  1801. OPCODE_BREAK;
  1802. }
  1803. }
  1804. DISPATCH_OPCODE; // Needed for synchronous calls (when result is immediately available).
  1805. OPCODE(OPCODE_AWAIT_RESUME) {
  1806. CHECK_SPACE(2);
  1807. #ifdef DEBUG_ENABLED
  1808. if (!p_state) {
  1809. err_text = ("Invalid Resume (bug?)");
  1810. OPCODE_BREAK;
  1811. }
  1812. #endif
  1813. GET_INSTRUCTION_ARG(result, 0);
  1814. *result = p_state->result;
  1815. ip += 2;
  1816. }
  1817. DISPATCH_OPCODE;
  1818. OPCODE(OPCODE_CREATE_LAMBDA) {
  1819. CHECK_SPACE(2 + instr_arg_count);
  1820. ip += instr_arg_count;
  1821. int captures_count = _code_ptr[ip + 1];
  1822. GD_ERR_BREAK(captures_count < 0);
  1823. int lambda_index = _code_ptr[ip + 2];
  1824. GD_ERR_BREAK(lambda_index < 0 || lambda_index >= _lambdas_count);
  1825. GDScriptFunction *lambda = _lambdas_ptr[lambda_index];
  1826. Vector<Variant> captures;
  1827. captures.resize(captures_count);
  1828. for (int i = 0; i < captures_count; i++) {
  1829. GET_INSTRUCTION_ARG(arg, i);
  1830. captures.write[i] = *arg;
  1831. }
  1832. GDScriptLambdaCallable *callable = memnew(GDScriptLambdaCallable(Ref<GDScript>(script), lambda, captures));
  1833. GET_INSTRUCTION_ARG(result, captures_count);
  1834. *result = Callable(callable);
  1835. ip += 3;
  1836. }
  1837. DISPATCH_OPCODE;
  1838. OPCODE(OPCODE_JUMP) {
  1839. CHECK_SPACE(2);
  1840. int to = _code_ptr[ip + 1];
  1841. GD_ERR_BREAK(to < 0 || to > _code_size);
  1842. ip = to;
  1843. }
  1844. DISPATCH_OPCODE;
  1845. OPCODE(OPCODE_JUMP_IF) {
  1846. CHECK_SPACE(3);
  1847. GET_INSTRUCTION_ARG(test, 0);
  1848. bool result = test->booleanize();
  1849. if (result) {
  1850. int to = _code_ptr[ip + 2];
  1851. GD_ERR_BREAK(to < 0 || to > _code_size);
  1852. ip = to;
  1853. } else {
  1854. ip += 3;
  1855. }
  1856. }
  1857. DISPATCH_OPCODE;
  1858. OPCODE(OPCODE_JUMP_IF_NOT) {
  1859. CHECK_SPACE(3);
  1860. GET_INSTRUCTION_ARG(test, 0);
  1861. bool result = test->booleanize();
  1862. if (!result) {
  1863. int to = _code_ptr[ip + 2];
  1864. GD_ERR_BREAK(to < 0 || to > _code_size);
  1865. ip = to;
  1866. } else {
  1867. ip += 3;
  1868. }
  1869. }
  1870. DISPATCH_OPCODE;
  1871. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1872. CHECK_SPACE(2);
  1873. ip = _default_arg_ptr[defarg];
  1874. }
  1875. DISPATCH_OPCODE;
  1876. OPCODE(OPCODE_RETURN) {
  1877. CHECK_SPACE(2);
  1878. GET_INSTRUCTION_ARG(r, 0);
  1879. retvalue = *r;
  1880. #ifdef DEBUG_ENABLED
  1881. exit_ok = true;
  1882. #endif
  1883. OPCODE_BREAK;
  1884. }
  1885. OPCODE(OPCODE_RETURN_TYPED_BUILTIN) {
  1886. CHECK_SPACE(3);
  1887. GET_INSTRUCTION_ARG(r, 0);
  1888. Variant::Type ret_type = (Variant::Type)_code_ptr[ip + 2];
  1889. GD_ERR_BREAK(ret_type < 0 || ret_type >= Variant::VARIANT_MAX);
  1890. if (r->get_type() != ret_type) {
  1891. if (Variant::can_convert_strict(r->get_type(), ret_type)) {
  1892. Callable::CallError ce;
  1893. Variant::construct(ret_type, retvalue, const_cast<const Variant **>(&r), 1, ce);
  1894. } else {
  1895. #ifdef DEBUG_ENABLED
  1896. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  1897. Variant::get_type_name(r->get_type()), Variant::get_type_name(ret_type));
  1898. #endif // DEBUG_ENABLED
  1899. // Construct a base type anyway so type constraints are met.
  1900. Callable::CallError ce;
  1901. Variant::construct(ret_type, retvalue, nullptr, 0, ce);
  1902. OPCODE_BREAK;
  1903. }
  1904. } else {
  1905. retvalue = *r;
  1906. }
  1907. #ifdef DEBUG_ENABLED
  1908. exit_ok = true;
  1909. #endif // DEBUG_ENABLED
  1910. OPCODE_BREAK;
  1911. }
  1912. OPCODE(OPCODE_RETURN_TYPED_ARRAY) {
  1913. CHECK_SPACE(5);
  1914. GET_INSTRUCTION_ARG(r, 0);
  1915. GET_INSTRUCTION_ARG(script_type, 1);
  1916. Variant::Type builtin_type = (Variant::Type)_code_ptr[ip + 3];
  1917. int native_type_idx = _code_ptr[ip + 4];
  1918. GD_ERR_BREAK(native_type_idx < 0 || native_type_idx >= _global_names_count);
  1919. const StringName native_type = _global_names_ptr[native_type_idx];
  1920. if (r->get_type() != Variant::ARRAY) {
  1921. #ifdef DEBUG_ENABLED
  1922. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "Array[%s]".)",
  1923. Variant::get_type_name(r->get_type()), Variant::get_type_name(builtin_type));
  1924. #endif
  1925. OPCODE_BREAK;
  1926. }
  1927. Array array;
  1928. array.set_typed(builtin_type, native_type, script_type);
  1929. #ifdef DEBUG_ENABLED
  1930. bool valid = array.typed_assign(*VariantInternal::get_array(r));
  1931. #else
  1932. array.typed_assign(*VariantInternal::get_array(r));
  1933. #endif // DEBUG_ENABLED
  1934. // Assign the return value anyway since we want it to be the valid type.
  1935. retvalue = array;
  1936. #ifdef DEBUG_ENABLED
  1937. if (!valid) {
  1938. err_text = "Trying to return a typed array with an array of different type.'";
  1939. OPCODE_BREAK;
  1940. }
  1941. exit_ok = true;
  1942. #endif // DEBUG_ENABLED
  1943. OPCODE_BREAK;
  1944. }
  1945. OPCODE(OPCODE_RETURN_TYPED_NATIVE) {
  1946. CHECK_SPACE(3);
  1947. GET_INSTRUCTION_ARG(r, 0);
  1948. GET_INSTRUCTION_ARG(type, 1);
  1949. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  1950. GD_ERR_BREAK(!nc);
  1951. if (r->get_type() != Variant::OBJECT && r->get_type() != Variant::NIL) {
  1952. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  1953. Variant::get_type_name(r->get_type()), nc->get_name());
  1954. OPCODE_BREAK;
  1955. }
  1956. #ifdef DEBUG_ENABLED
  1957. bool freed = false;
  1958. Object *ret_obj = r->get_validated_object_with_check(freed);
  1959. if (freed) {
  1960. err_text = "Trying to return a previously freed instance.";
  1961. OPCODE_BREAK;
  1962. }
  1963. #else
  1964. Object *ret_obj = r->operator Object *();
  1965. #endif // DEBUG_ENABLED
  1966. if (ret_obj && !ClassDB::is_parent_class(ret_obj->get_class_name(), nc->get_name())) {
  1967. #ifdef DEBUG_ENABLED
  1968. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  1969. ret_obj->get_class_name(), nc->get_name());
  1970. #endif // DEBUG_ENABLED
  1971. OPCODE_BREAK;
  1972. }
  1973. retvalue = *r;
  1974. #ifdef DEBUG_ENABLED
  1975. exit_ok = true;
  1976. #endif // DEBUG_ENABLED
  1977. OPCODE_BREAK;
  1978. }
  1979. OPCODE(OPCODE_RETURN_TYPED_SCRIPT) {
  1980. CHECK_SPACE(3);
  1981. GET_INSTRUCTION_ARG(r, 0);
  1982. GET_INSTRUCTION_ARG(type, 1);
  1983. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  1984. GD_ERR_BREAK(!base_type);
  1985. if (r->get_type() != Variant::OBJECT && r->get_type() != Variant::NIL) {
  1986. #ifdef DEBUG_ENABLED
  1987. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  1988. Variant::get_type_name(r->get_type()), _get_script_name(Ref<Script>(base_type)));
  1989. #endif // DEBUG_ENABLED
  1990. OPCODE_BREAK;
  1991. }
  1992. #ifdef DEBUG_ENABLED
  1993. bool freed = false;
  1994. Object *ret_obj = r->get_validated_object_with_check(freed);
  1995. if (freed) {
  1996. err_text = "Trying to return a previously freed instance.";
  1997. OPCODE_BREAK;
  1998. }
  1999. #else
  2000. Object *ret_obj = r->operator Object *();
  2001. #endif // DEBUG_ENABLED
  2002. if (ret_obj) {
  2003. ScriptInstance *ret_inst = ret_obj->get_script_instance();
  2004. if (!ret_inst) {
  2005. #ifdef DEBUG_ENABLED
  2006. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  2007. ret_obj->get_class_name(), _get_script_name(Ref<GDScript>(base_type)));
  2008. #endif // DEBUG_ENABLED
  2009. OPCODE_BREAK;
  2010. }
  2011. Script *ret_type = ret_obj->get_script_instance()->get_script().ptr();
  2012. bool valid = false;
  2013. while (ret_type) {
  2014. if (ret_type == base_type) {
  2015. valid = true;
  2016. break;
  2017. }
  2018. ret_type = ret_type->get_base_script().ptr();
  2019. }
  2020. if (!valid) {
  2021. #ifdef DEBUG_ENABLED
  2022. err_text = vformat(R"(Trying to return value of type "%s" from a function which the return type is "%s".)",
  2023. _get_script_name(ret_obj->get_script_instance()->get_script()), _get_script_name(Ref<GDScript>(base_type)));
  2024. #endif // DEBUG_ENABLED
  2025. OPCODE_BREAK;
  2026. }
  2027. }
  2028. retvalue = *r;
  2029. #ifdef DEBUG_ENABLED
  2030. exit_ok = true;
  2031. #endif // DEBUG_ENABLED
  2032. OPCODE_BREAK;
  2033. }
  2034. OPCODE(OPCODE_ITERATE_BEGIN) {
  2035. CHECK_SPACE(8); // Space for this and a regular iterate.
  2036. GET_INSTRUCTION_ARG(counter, 0);
  2037. GET_INSTRUCTION_ARG(container, 1);
  2038. bool valid;
  2039. if (!container->iter_init(*counter, valid)) {
  2040. #ifdef DEBUG_ENABLED
  2041. if (!valid) {
  2042. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  2043. OPCODE_BREAK;
  2044. }
  2045. #endif
  2046. int jumpto = _code_ptr[ip + 4];
  2047. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2048. ip = jumpto;
  2049. } else {
  2050. GET_INSTRUCTION_ARG(iterator, 2);
  2051. *iterator = container->iter_get(*counter, valid);
  2052. #ifdef DEBUG_ENABLED
  2053. if (!valid) {
  2054. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "'.";
  2055. OPCODE_BREAK;
  2056. }
  2057. #endif
  2058. ip += 5; // Skip regular iterate which is always next.
  2059. }
  2060. }
  2061. DISPATCH_OPCODE;
  2062. OPCODE(OPCODE_ITERATE_BEGIN_INT) {
  2063. CHECK_SPACE(8); // Check space for iterate instruction too.
  2064. GET_INSTRUCTION_ARG(counter, 0);
  2065. GET_INSTRUCTION_ARG(container, 1);
  2066. int64_t size = *VariantInternal::get_int(container);
  2067. VariantInternal::initialize(counter, Variant::INT);
  2068. *VariantInternal::get_int(counter) = 0;
  2069. if (size > 0) {
  2070. GET_INSTRUCTION_ARG(iterator, 2);
  2071. VariantInternal::initialize(iterator, Variant::INT);
  2072. *VariantInternal::get_int(iterator) = 0;
  2073. // Skip regular iterate.
  2074. ip += 5;
  2075. } else {
  2076. // Jump to end of loop.
  2077. int jumpto = _code_ptr[ip + 4];
  2078. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2079. ip = jumpto;
  2080. }
  2081. }
  2082. DISPATCH_OPCODE;
  2083. OPCODE(OPCODE_ITERATE_BEGIN_FLOAT) {
  2084. CHECK_SPACE(8); // Check space for iterate instruction too.
  2085. GET_INSTRUCTION_ARG(counter, 0);
  2086. GET_INSTRUCTION_ARG(container, 1);
  2087. double size = *VariantInternal::get_float(container);
  2088. VariantInternal::initialize(counter, Variant::FLOAT);
  2089. *VariantInternal::get_float(counter) = 0.0;
  2090. if (size > 0) {
  2091. GET_INSTRUCTION_ARG(iterator, 2);
  2092. VariantInternal::initialize(iterator, Variant::FLOAT);
  2093. *VariantInternal::get_float(iterator) = 0;
  2094. // Skip regular iterate.
  2095. ip += 5;
  2096. } else {
  2097. // Jump to end of loop.
  2098. int jumpto = _code_ptr[ip + 4];
  2099. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2100. ip = jumpto;
  2101. }
  2102. }
  2103. DISPATCH_OPCODE;
  2104. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2) {
  2105. CHECK_SPACE(8); // Check space for iterate instruction too.
  2106. GET_INSTRUCTION_ARG(counter, 0);
  2107. GET_INSTRUCTION_ARG(container, 1);
  2108. Vector2 *bounds = VariantInternal::get_vector2(container);
  2109. VariantInternal::initialize(counter, Variant::FLOAT);
  2110. *VariantInternal::get_float(counter) = bounds->x;
  2111. if (bounds->x < bounds->y) {
  2112. GET_INSTRUCTION_ARG(iterator, 2);
  2113. VariantInternal::initialize(iterator, Variant::FLOAT);
  2114. *VariantInternal::get_float(iterator) = bounds->x;
  2115. // Skip regular iterate.
  2116. ip += 5;
  2117. } else {
  2118. // Jump to end of loop.
  2119. int jumpto = _code_ptr[ip + 4];
  2120. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2121. ip = jumpto;
  2122. }
  2123. }
  2124. DISPATCH_OPCODE;
  2125. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR2I) {
  2126. CHECK_SPACE(8); // Check space for iterate instruction too.
  2127. GET_INSTRUCTION_ARG(counter, 0);
  2128. GET_INSTRUCTION_ARG(container, 1);
  2129. Vector2i *bounds = VariantInternal::get_vector2i(container);
  2130. VariantInternal::initialize(counter, Variant::FLOAT);
  2131. *VariantInternal::get_int(counter) = bounds->x;
  2132. if (bounds->x < bounds->y) {
  2133. GET_INSTRUCTION_ARG(iterator, 2);
  2134. VariantInternal::initialize(iterator, Variant::INT);
  2135. *VariantInternal::get_int(iterator) = bounds->x;
  2136. // Skip regular iterate.
  2137. ip += 5;
  2138. } else {
  2139. // Jump to end of loop.
  2140. int jumpto = _code_ptr[ip + 4];
  2141. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2142. ip = jumpto;
  2143. }
  2144. }
  2145. DISPATCH_OPCODE;
  2146. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3) {
  2147. CHECK_SPACE(8); // Check space for iterate instruction too.
  2148. GET_INSTRUCTION_ARG(counter, 0);
  2149. GET_INSTRUCTION_ARG(container, 1);
  2150. Vector3 *bounds = VariantInternal::get_vector3(container);
  2151. double from = bounds->x;
  2152. double to = bounds->y;
  2153. double step = bounds->z;
  2154. VariantInternal::initialize(counter, Variant::FLOAT);
  2155. *VariantInternal::get_float(counter) = from;
  2156. bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
  2157. if (do_continue) {
  2158. GET_INSTRUCTION_ARG(iterator, 2);
  2159. VariantInternal::initialize(iterator, Variant::FLOAT);
  2160. *VariantInternal::get_float(iterator) = from;
  2161. // Skip regular iterate.
  2162. ip += 5;
  2163. } else {
  2164. // Jump to end of loop.
  2165. int jumpto = _code_ptr[ip + 4];
  2166. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2167. ip = jumpto;
  2168. }
  2169. }
  2170. DISPATCH_OPCODE;
  2171. OPCODE(OPCODE_ITERATE_BEGIN_VECTOR3I) {
  2172. CHECK_SPACE(8); // Check space for iterate instruction too.
  2173. GET_INSTRUCTION_ARG(counter, 0);
  2174. GET_INSTRUCTION_ARG(container, 1);
  2175. Vector3i *bounds = VariantInternal::get_vector3i(container);
  2176. int64_t from = bounds->x;
  2177. int64_t to = bounds->y;
  2178. int64_t step = bounds->z;
  2179. VariantInternal::initialize(counter, Variant::INT);
  2180. *VariantInternal::get_int(counter) = from;
  2181. bool do_continue = from == to ? false : (from < to ? step > 0 : step < 0);
  2182. if (do_continue) {
  2183. GET_INSTRUCTION_ARG(iterator, 2);
  2184. VariantInternal::initialize(iterator, Variant::INT);
  2185. *VariantInternal::get_int(iterator) = from;
  2186. // Skip regular iterate.
  2187. ip += 5;
  2188. } else {
  2189. // Jump to end of loop.
  2190. int jumpto = _code_ptr[ip + 4];
  2191. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2192. ip = jumpto;
  2193. }
  2194. }
  2195. DISPATCH_OPCODE;
  2196. OPCODE(OPCODE_ITERATE_BEGIN_STRING) {
  2197. CHECK_SPACE(8); // Check space for iterate instruction too.
  2198. GET_INSTRUCTION_ARG(counter, 0);
  2199. GET_INSTRUCTION_ARG(container, 1);
  2200. String *str = VariantInternal::get_string(container);
  2201. VariantInternal::initialize(counter, Variant::INT);
  2202. *VariantInternal::get_int(counter) = 0;
  2203. if (!str->is_empty()) {
  2204. GET_INSTRUCTION_ARG(iterator, 2);
  2205. VariantInternal::initialize(iterator, Variant::STRING);
  2206. *VariantInternal::get_string(iterator) = str->substr(0, 1);
  2207. // Skip regular iterate.
  2208. ip += 5;
  2209. } else {
  2210. // Jump to end of loop.
  2211. int jumpto = _code_ptr[ip + 4];
  2212. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2213. ip = jumpto;
  2214. }
  2215. }
  2216. DISPATCH_OPCODE;
  2217. OPCODE(OPCODE_ITERATE_BEGIN_DICTIONARY) {
  2218. CHECK_SPACE(8); // Check space for iterate instruction too.
  2219. GET_INSTRUCTION_ARG(counter, 0);
  2220. GET_INSTRUCTION_ARG(container, 1);
  2221. Dictionary *dict = VariantInternal::get_dictionary(container);
  2222. const Variant *next = dict->next(nullptr);
  2223. if (!dict->is_empty()) {
  2224. GET_INSTRUCTION_ARG(iterator, 2);
  2225. *counter = *next;
  2226. *iterator = *next;
  2227. // Skip regular iterate.
  2228. ip += 5;
  2229. } else {
  2230. // Jump to end of loop.
  2231. int jumpto = _code_ptr[ip + 4];
  2232. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2233. ip = jumpto;
  2234. }
  2235. }
  2236. DISPATCH_OPCODE;
  2237. OPCODE(OPCODE_ITERATE_BEGIN_ARRAY) {
  2238. CHECK_SPACE(8); // Check space for iterate instruction too.
  2239. GET_INSTRUCTION_ARG(counter, 0);
  2240. GET_INSTRUCTION_ARG(container, 1);
  2241. Array *array = VariantInternal::get_array(container);
  2242. VariantInternal::initialize(counter, Variant::INT);
  2243. *VariantInternal::get_int(counter) = 0;
  2244. if (!array->is_empty()) {
  2245. GET_INSTRUCTION_ARG(iterator, 2);
  2246. *iterator = array->get(0);
  2247. // Skip regular iterate.
  2248. ip += 5;
  2249. } else {
  2250. // Jump to end of loop.
  2251. int jumpto = _code_ptr[ip + 4];
  2252. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2253. ip = jumpto;
  2254. }
  2255. }
  2256. DISPATCH_OPCODE;
  2257. #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) \
  2258. OPCODE(OPCODE_ITERATE_BEGIN_PACKED_##m_var_type##_ARRAY) { \
  2259. CHECK_SPACE(8); \
  2260. GET_INSTRUCTION_ARG(counter, 0); \
  2261. GET_INSTRUCTION_ARG(container, 1); \
  2262. Vector<m_elem_type> *array = VariantInternal::m_get_func(container); \
  2263. VariantInternal::initialize(counter, Variant::INT); \
  2264. *VariantInternal::get_int(counter) = 0; \
  2265. if (!array->is_empty()) { \
  2266. GET_INSTRUCTION_ARG(iterator, 2); \
  2267. VariantInternal::initialize(iterator, Variant::m_var_ret_type); \
  2268. m_ret_type *it = VariantInternal::m_ret_get_func(iterator); \
  2269. *it = array->get(0); \
  2270. ip += 5; \
  2271. } else { \
  2272. int jumpto = _code_ptr[ip + 4]; \
  2273. GD_ERR_BREAK(jumpto<0 || jumpto> _code_size); \
  2274. ip = jumpto; \
  2275. } \
  2276. } \
  2277. DISPATCH_OPCODE
  2278. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, INT, int64_t, get_int);
  2279. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT32, int32_t, get_int32_array, INT, int64_t, get_int);
  2280. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(INT64, int64_t, get_int64_array, INT, int64_t, get_int);
  2281. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT32, float, get_float32_array, FLOAT, double, get_float);
  2282. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(FLOAT64, double, get_float64_array, FLOAT, double, get_float);
  2283. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(STRING, String, get_string_array, STRING, String, get_string);
  2284. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, VECTOR2, Vector2, get_vector2);
  2285. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, VECTOR3, Vector3, get_vector3);
  2286. OPCODE_ITERATE_BEGIN_PACKED_ARRAY(COLOR, Color, get_color_array, COLOR, Color, get_color);
  2287. OPCODE(OPCODE_ITERATE_BEGIN_OBJECT) {
  2288. CHECK_SPACE(4);
  2289. GET_INSTRUCTION_ARG(counter, 0);
  2290. GET_INSTRUCTION_ARG(container, 1);
  2291. #ifdef DEBUG_ENABLED
  2292. bool freed = false;
  2293. Object *obj = container->get_validated_object_with_check(freed);
  2294. if (freed) {
  2295. err_text = "Trying to iterate on a previously freed object.";
  2296. OPCODE_BREAK;
  2297. } else if (!obj) {
  2298. err_text = "Trying to iterate on a null value.";
  2299. OPCODE_BREAK;
  2300. }
  2301. #else
  2302. Object *obj = *VariantInternal::get_object(container);
  2303. #endif
  2304. Array ref;
  2305. ref.push_back(*counter);
  2306. Variant vref;
  2307. VariantInternal::initialize(&vref, Variant::ARRAY);
  2308. *VariantInternal::get_array(&vref) = ref;
  2309. Variant **args = instruction_args; // Overriding an instruction argument, but we don't need access to that anymore.
  2310. args[0] = &vref;
  2311. Callable::CallError ce;
  2312. Variant has_next = obj->call(CoreStringNames::get_singleton()->_iter_init, (const Variant **)args, 1, ce);
  2313. #ifdef DEBUG_ENABLED
  2314. if (ce.error != Callable::CallError::CALL_OK) {
  2315. err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
  2316. OPCODE_BREAK;
  2317. }
  2318. #endif
  2319. if (!has_next.booleanize()) {
  2320. int jumpto = _code_ptr[ip + 4];
  2321. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2322. ip = jumpto;
  2323. } else {
  2324. GET_INSTRUCTION_ARG(iterator, 2);
  2325. *iterator = obj->call(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce);
  2326. #ifdef DEBUG_ENABLED
  2327. if (ce.error != Callable::CallError::CALL_OK) {
  2328. err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
  2329. OPCODE_BREAK;
  2330. }
  2331. #endif
  2332. ip += 5; // Loop again.
  2333. }
  2334. }
  2335. DISPATCH_OPCODE;
  2336. OPCODE(OPCODE_ITERATE) {
  2337. CHECK_SPACE(4);
  2338. GET_INSTRUCTION_ARG(counter, 0);
  2339. GET_INSTRUCTION_ARG(container, 1);
  2340. bool valid;
  2341. if (!container->iter_next(*counter, valid)) {
  2342. #ifdef DEBUG_ENABLED
  2343. if (!valid) {
  2344. err_text = "Unable to iterate on object of type '" + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  2345. OPCODE_BREAK;
  2346. }
  2347. #endif
  2348. int jumpto = _code_ptr[ip + 4];
  2349. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2350. ip = jumpto;
  2351. } else {
  2352. GET_INSTRUCTION_ARG(iterator, 2);
  2353. *iterator = container->iter_get(*counter, valid);
  2354. #ifdef DEBUG_ENABLED
  2355. if (!valid) {
  2356. err_text = "Unable to obtain iterator object of type '" + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  2357. OPCODE_BREAK;
  2358. }
  2359. #endif
  2360. ip += 5; //loop again
  2361. }
  2362. }
  2363. DISPATCH_OPCODE;
  2364. OPCODE(OPCODE_ITERATE_INT) {
  2365. CHECK_SPACE(4);
  2366. GET_INSTRUCTION_ARG(counter, 0);
  2367. GET_INSTRUCTION_ARG(container, 1);
  2368. int64_t size = *VariantInternal::get_int(container);
  2369. int64_t *count = VariantInternal::get_int(counter);
  2370. (*count)++;
  2371. if (*count >= size) {
  2372. int jumpto = _code_ptr[ip + 4];
  2373. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2374. ip = jumpto;
  2375. } else {
  2376. GET_INSTRUCTION_ARG(iterator, 2);
  2377. *VariantInternal::get_int(iterator) = *count;
  2378. ip += 5; // Loop again.
  2379. }
  2380. }
  2381. DISPATCH_OPCODE;
  2382. OPCODE(OPCODE_ITERATE_FLOAT) {
  2383. CHECK_SPACE(4);
  2384. GET_INSTRUCTION_ARG(counter, 0);
  2385. GET_INSTRUCTION_ARG(container, 1);
  2386. double size = *VariantInternal::get_float(container);
  2387. double *count = VariantInternal::get_float(counter);
  2388. (*count)++;
  2389. if (*count >= size) {
  2390. int jumpto = _code_ptr[ip + 4];
  2391. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2392. ip = jumpto;
  2393. } else {
  2394. GET_INSTRUCTION_ARG(iterator, 2);
  2395. *VariantInternal::get_float(iterator) = *count;
  2396. ip += 5; // Loop again.
  2397. }
  2398. }
  2399. DISPATCH_OPCODE;
  2400. OPCODE(OPCODE_ITERATE_VECTOR2) {
  2401. CHECK_SPACE(4);
  2402. GET_INSTRUCTION_ARG(counter, 0);
  2403. GET_INSTRUCTION_ARG(container, 1);
  2404. const Vector2 *bounds = VariantInternal::get_vector2((const Variant *)container);
  2405. double *count = VariantInternal::get_float(counter);
  2406. (*count)++;
  2407. if (*count >= bounds->y) {
  2408. int jumpto = _code_ptr[ip + 4];
  2409. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2410. ip = jumpto;
  2411. } else {
  2412. GET_INSTRUCTION_ARG(iterator, 2);
  2413. *VariantInternal::get_float(iterator) = *count;
  2414. ip += 5; // Loop again.
  2415. }
  2416. }
  2417. DISPATCH_OPCODE;
  2418. OPCODE(OPCODE_ITERATE_VECTOR2I) {
  2419. CHECK_SPACE(4);
  2420. GET_INSTRUCTION_ARG(counter, 0);
  2421. GET_INSTRUCTION_ARG(container, 1);
  2422. const Vector2i *bounds = VariantInternal::get_vector2i((const Variant *)container);
  2423. int64_t *count = VariantInternal::get_int(counter);
  2424. (*count)++;
  2425. if (*count >= bounds->y) {
  2426. int jumpto = _code_ptr[ip + 4];
  2427. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2428. ip = jumpto;
  2429. } else {
  2430. GET_INSTRUCTION_ARG(iterator, 2);
  2431. *VariantInternal::get_int(iterator) = *count;
  2432. ip += 5; // Loop again.
  2433. }
  2434. }
  2435. DISPATCH_OPCODE;
  2436. OPCODE(OPCODE_ITERATE_VECTOR3) {
  2437. CHECK_SPACE(4);
  2438. GET_INSTRUCTION_ARG(counter, 0);
  2439. GET_INSTRUCTION_ARG(container, 1);
  2440. const Vector3 *bounds = VariantInternal::get_vector3((const Variant *)container);
  2441. double *count = VariantInternal::get_float(counter);
  2442. *count += bounds->z;
  2443. if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
  2444. int jumpto = _code_ptr[ip + 4];
  2445. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2446. ip = jumpto;
  2447. } else {
  2448. GET_INSTRUCTION_ARG(iterator, 2);
  2449. *VariantInternal::get_float(iterator) = *count;
  2450. ip += 5; // Loop again.
  2451. }
  2452. }
  2453. DISPATCH_OPCODE;
  2454. OPCODE(OPCODE_ITERATE_VECTOR3I) {
  2455. CHECK_SPACE(4);
  2456. GET_INSTRUCTION_ARG(counter, 0);
  2457. GET_INSTRUCTION_ARG(container, 1);
  2458. const Vector3i *bounds = VariantInternal::get_vector3i((const Variant *)container);
  2459. int64_t *count = VariantInternal::get_int(counter);
  2460. *count += bounds->z;
  2461. if ((bounds->z < 0 && *count <= bounds->y) || (bounds->z > 0 && *count >= bounds->y)) {
  2462. int jumpto = _code_ptr[ip + 4];
  2463. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2464. ip = jumpto;
  2465. } else {
  2466. GET_INSTRUCTION_ARG(iterator, 2);
  2467. *VariantInternal::get_int(iterator) = *count;
  2468. ip += 5; // Loop again.
  2469. }
  2470. }
  2471. DISPATCH_OPCODE;
  2472. OPCODE(OPCODE_ITERATE_STRING) {
  2473. CHECK_SPACE(4);
  2474. GET_INSTRUCTION_ARG(counter, 0);
  2475. GET_INSTRUCTION_ARG(container, 1);
  2476. const String *str = VariantInternal::get_string((const Variant *)container);
  2477. int64_t *idx = VariantInternal::get_int(counter);
  2478. (*idx)++;
  2479. if (*idx >= str->length()) {
  2480. int jumpto = _code_ptr[ip + 4];
  2481. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2482. ip = jumpto;
  2483. } else {
  2484. GET_INSTRUCTION_ARG(iterator, 2);
  2485. *VariantInternal::get_string(iterator) = str->substr(*idx, 1);
  2486. ip += 5; // Loop again.
  2487. }
  2488. }
  2489. DISPATCH_OPCODE;
  2490. OPCODE(OPCODE_ITERATE_DICTIONARY) {
  2491. CHECK_SPACE(4);
  2492. GET_INSTRUCTION_ARG(counter, 0);
  2493. GET_INSTRUCTION_ARG(container, 1);
  2494. const Dictionary *dict = VariantInternal::get_dictionary((const Variant *)container);
  2495. const Variant *next = dict->next(counter);
  2496. if (!next) {
  2497. int jumpto = _code_ptr[ip + 4];
  2498. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2499. ip = jumpto;
  2500. } else {
  2501. GET_INSTRUCTION_ARG(iterator, 2);
  2502. *counter = *next;
  2503. *iterator = *next;
  2504. ip += 5; // Loop again.
  2505. }
  2506. }
  2507. DISPATCH_OPCODE;
  2508. OPCODE(OPCODE_ITERATE_ARRAY) {
  2509. CHECK_SPACE(4);
  2510. GET_INSTRUCTION_ARG(counter, 0);
  2511. GET_INSTRUCTION_ARG(container, 1);
  2512. const Array *array = VariantInternal::get_array((const Variant *)container);
  2513. int64_t *idx = VariantInternal::get_int(counter);
  2514. (*idx)++;
  2515. if (*idx >= array->size()) {
  2516. int jumpto = _code_ptr[ip + 4];
  2517. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2518. ip = jumpto;
  2519. } else {
  2520. GET_INSTRUCTION_ARG(iterator, 2);
  2521. *iterator = array->get(*idx);
  2522. ip += 5; // Loop again.
  2523. }
  2524. }
  2525. DISPATCH_OPCODE;
  2526. #define OPCODE_ITERATE_PACKED_ARRAY(m_var_type, m_elem_type, m_get_func, m_ret_get_func) \
  2527. OPCODE(OPCODE_ITERATE_PACKED_##m_var_type##_ARRAY) { \
  2528. CHECK_SPACE(4); \
  2529. GET_INSTRUCTION_ARG(counter, 0); \
  2530. GET_INSTRUCTION_ARG(container, 1); \
  2531. const Vector<m_elem_type> *array = VariantInternal::m_get_func((const Variant *)container); \
  2532. int64_t *idx = VariantInternal::get_int(counter); \
  2533. (*idx)++; \
  2534. if (*idx >= array->size()) { \
  2535. int jumpto = _code_ptr[ip + 4]; \
  2536. GD_ERR_BREAK(jumpto<0 || jumpto> _code_size); \
  2537. ip = jumpto; \
  2538. } else { \
  2539. GET_INSTRUCTION_ARG(iterator, 2); \
  2540. *VariantInternal::m_ret_get_func(iterator) = array->get(*idx); \
  2541. ip += 5; \
  2542. } \
  2543. } \
  2544. DISPATCH_OPCODE
  2545. OPCODE_ITERATE_PACKED_ARRAY(BYTE, uint8_t, get_byte_array, get_int);
  2546. OPCODE_ITERATE_PACKED_ARRAY(INT32, int32_t, get_int32_array, get_int);
  2547. OPCODE_ITERATE_PACKED_ARRAY(INT64, int64_t, get_int64_array, get_int);
  2548. OPCODE_ITERATE_PACKED_ARRAY(FLOAT32, float, get_float32_array, get_float);
  2549. OPCODE_ITERATE_PACKED_ARRAY(FLOAT64, double, get_float64_array, get_float);
  2550. OPCODE_ITERATE_PACKED_ARRAY(STRING, String, get_string_array, get_string);
  2551. OPCODE_ITERATE_PACKED_ARRAY(VECTOR2, Vector2, get_vector2_array, get_vector2);
  2552. OPCODE_ITERATE_PACKED_ARRAY(VECTOR3, Vector3, get_vector3_array, get_vector3);
  2553. OPCODE_ITERATE_PACKED_ARRAY(COLOR, Color, get_color_array, get_color);
  2554. OPCODE(OPCODE_ITERATE_OBJECT) {
  2555. CHECK_SPACE(4);
  2556. GET_INSTRUCTION_ARG(counter, 0);
  2557. GET_INSTRUCTION_ARG(container, 1);
  2558. #ifdef DEBUG_ENABLED
  2559. bool freed = false;
  2560. Object *obj = container->get_validated_object_with_check(freed);
  2561. if (freed) {
  2562. err_text = "Trying to iterate on a previously freed object.";
  2563. OPCODE_BREAK;
  2564. } else if (!obj) {
  2565. err_text = "Trying to iterate on a null value.";
  2566. OPCODE_BREAK;
  2567. }
  2568. #else
  2569. Object *obj = *VariantInternal::get_object(container);
  2570. #endif
  2571. Array ref;
  2572. ref.push_back(*counter);
  2573. Variant vref;
  2574. VariantInternal::initialize(&vref, Variant::ARRAY);
  2575. *VariantInternal::get_array(&vref) = ref;
  2576. Variant **args = instruction_args; // Overriding an instruction argument, but we don't need access to that anymore.
  2577. args[0] = &vref;
  2578. Callable::CallError ce;
  2579. Variant has_next = obj->call(CoreStringNames::get_singleton()->_iter_next, (const Variant **)args, 1, ce);
  2580. #ifdef DEBUG_ENABLED
  2581. if (ce.error != Callable::CallError::CALL_OK) {
  2582. err_text = vformat(R"(There was an error calling "_iter_next" on iterator object of type %s.)", *container);
  2583. OPCODE_BREAK;
  2584. }
  2585. #endif
  2586. if (!has_next.booleanize()) {
  2587. int jumpto = _code_ptr[ip + 4];
  2588. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  2589. ip = jumpto;
  2590. } else {
  2591. GET_INSTRUCTION_ARG(iterator, 2);
  2592. *iterator = obj->call(CoreStringNames::get_singleton()->_iter_get, (const Variant **)args, 1, ce);
  2593. #ifdef DEBUG_ENABLED
  2594. if (ce.error != Callable::CallError::CALL_OK) {
  2595. err_text = vformat(R"(There was an error calling "_iter_get" on iterator object of type %s.)", *container);
  2596. OPCODE_BREAK;
  2597. }
  2598. #endif
  2599. ip += 5; // Loop again.
  2600. }
  2601. }
  2602. DISPATCH_OPCODE;
  2603. OPCODE(OPCODE_STORE_NAMED_GLOBAL) {
  2604. CHECK_SPACE(3);
  2605. int globalname_idx = _code_ptr[ip + 2];
  2606. GD_ERR_BREAK(globalname_idx < 0 || globalname_idx >= _global_names_count);
  2607. const StringName *globalname = &_global_names_ptr[globalname_idx];
  2608. GET_INSTRUCTION_ARG(dst, 0);
  2609. *dst = GDScriptLanguage::get_singleton()->get_named_globals_map()[*globalname];
  2610. ip += 3;
  2611. }
  2612. DISPATCH_OPCODE;
  2613. #define OPCODE_TYPE_ADJUST(m_v_type, m_c_type) \
  2614. OPCODE(OPCODE_TYPE_ADJUST_##m_v_type) { \
  2615. CHECK_SPACE(2); \
  2616. GET_INSTRUCTION_ARG(arg, 0); \
  2617. VariantTypeAdjust<m_c_type>::adjust(arg); \
  2618. ip += 2; \
  2619. } \
  2620. DISPATCH_OPCODE
  2621. OPCODE_TYPE_ADJUST(BOOL, bool);
  2622. OPCODE_TYPE_ADJUST(INT, int64_t);
  2623. OPCODE_TYPE_ADJUST(FLOAT, double);
  2624. OPCODE_TYPE_ADJUST(STRING, String);
  2625. OPCODE_TYPE_ADJUST(VECTOR2, Vector2);
  2626. OPCODE_TYPE_ADJUST(VECTOR2I, Vector2i);
  2627. OPCODE_TYPE_ADJUST(RECT2, Rect2);
  2628. OPCODE_TYPE_ADJUST(RECT2I, Rect2i);
  2629. OPCODE_TYPE_ADJUST(VECTOR3, Vector3);
  2630. OPCODE_TYPE_ADJUST(VECTOR3I, Vector3i);
  2631. OPCODE_TYPE_ADJUST(TRANSFORM2D, Transform2D);
  2632. OPCODE_TYPE_ADJUST(PLANE, Plane);
  2633. OPCODE_TYPE_ADJUST(QUAT, Quat);
  2634. OPCODE_TYPE_ADJUST(AABB, AABB);
  2635. OPCODE_TYPE_ADJUST(BASIS, Basis);
  2636. OPCODE_TYPE_ADJUST(TRANSFORM, Transform3D);
  2637. OPCODE_TYPE_ADJUST(COLOR, Color);
  2638. OPCODE_TYPE_ADJUST(STRING_NAME, StringName);
  2639. OPCODE_TYPE_ADJUST(NODE_PATH, NodePath);
  2640. OPCODE_TYPE_ADJUST(RID, RID);
  2641. OPCODE_TYPE_ADJUST(OBJECT, Object *);
  2642. OPCODE_TYPE_ADJUST(CALLABLE, Callable);
  2643. OPCODE_TYPE_ADJUST(SIGNAL, Signal);
  2644. OPCODE_TYPE_ADJUST(DICTIONARY, Dictionary);
  2645. OPCODE_TYPE_ADJUST(ARRAY, Array);
  2646. OPCODE_TYPE_ADJUST(PACKED_BYTE_ARRAY, PackedByteArray);
  2647. OPCODE_TYPE_ADJUST(PACKED_INT32_ARRAY, PackedInt32Array);
  2648. OPCODE_TYPE_ADJUST(PACKED_INT64_ARRAY, PackedInt64Array);
  2649. OPCODE_TYPE_ADJUST(PACKED_FLOAT32_ARRAY, PackedFloat32Array);
  2650. OPCODE_TYPE_ADJUST(PACKED_FLOAT64_ARRAY, PackedFloat64Array);
  2651. OPCODE_TYPE_ADJUST(PACKED_STRING_ARRAY, PackedStringArray);
  2652. OPCODE_TYPE_ADJUST(PACKED_VECTOR2_ARRAY, PackedVector2Array);
  2653. OPCODE_TYPE_ADJUST(PACKED_VECTOR3_ARRAY, PackedVector3Array);
  2654. OPCODE_TYPE_ADJUST(PACKED_COLOR_ARRAY, PackedColorArray);
  2655. OPCODE(OPCODE_ASSERT) {
  2656. CHECK_SPACE(3);
  2657. #ifdef DEBUG_ENABLED
  2658. GET_INSTRUCTION_ARG(test, 0);
  2659. bool result = test->booleanize();
  2660. if (!result) {
  2661. String message_str;
  2662. if (_code_ptr[ip + 2] != 0) {
  2663. GET_INSTRUCTION_ARG(message, 1);
  2664. message_str = *message;
  2665. }
  2666. if (message_str.is_empty()) {
  2667. err_text = "Assertion failed.";
  2668. } else {
  2669. err_text = "Assertion failed: " + message_str;
  2670. }
  2671. OPCODE_BREAK;
  2672. }
  2673. #endif
  2674. ip += 3;
  2675. }
  2676. DISPATCH_OPCODE;
  2677. OPCODE(OPCODE_BREAKPOINT) {
  2678. #ifdef DEBUG_ENABLED
  2679. if (EngineDebugger::is_active()) {
  2680. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  2681. }
  2682. #endif
  2683. ip += 1;
  2684. }
  2685. DISPATCH_OPCODE;
  2686. OPCODE(OPCODE_LINE) {
  2687. CHECK_SPACE(2);
  2688. line = _code_ptr[ip + 1];
  2689. ip += 2;
  2690. if (EngineDebugger::is_active()) {
  2691. // line
  2692. bool do_break = false;
  2693. if (EngineDebugger::get_script_debugger()->get_lines_left() > 0) {
  2694. if (EngineDebugger::get_script_debugger()->get_depth() <= 0) {
  2695. EngineDebugger::get_script_debugger()->set_lines_left(EngineDebugger::get_script_debugger()->get_lines_left() - 1);
  2696. }
  2697. if (EngineDebugger::get_script_debugger()->get_lines_left() <= 0) {
  2698. do_break = true;
  2699. }
  2700. }
  2701. if (EngineDebugger::get_script_debugger()->is_breakpoint(line, source)) {
  2702. do_break = true;
  2703. }
  2704. if (do_break) {
  2705. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  2706. }
  2707. EngineDebugger::get_singleton()->line_poll();
  2708. }
  2709. }
  2710. DISPATCH_OPCODE;
  2711. OPCODE(OPCODE_END) {
  2712. #ifdef DEBUG_ENABLED
  2713. exit_ok = true;
  2714. #endif
  2715. OPCODE_BREAK;
  2716. }
  2717. #if 0 // Enable for debugging.
  2718. default: {
  2719. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  2720. OPCODE_BREAK;
  2721. }
  2722. #endif
  2723. }
  2724. OPCODES_END
  2725. #ifdef DEBUG_ENABLED
  2726. if (exit_ok) {
  2727. OPCODE_OUT;
  2728. }
  2729. //error
  2730. // function, file, line, error, explanation
  2731. String err_file;
  2732. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->path != "") {
  2733. err_file = p_instance->script->path;
  2734. } else if (script) {
  2735. err_file = script->path;
  2736. }
  2737. if (err_file == "") {
  2738. err_file = "<built-in>";
  2739. }
  2740. String err_func = name;
  2741. if (p_instance && ObjectDB::get_instance(p_instance->owner_id) != nullptr && p_instance->script->is_valid() && p_instance->script->name != "") {
  2742. err_func = p_instance->script->name + "." + err_func;
  2743. }
  2744. int err_line = line;
  2745. if (err_text == "") {
  2746. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  2747. }
  2748. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  2749. // debugger break did not happen
  2750. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  2751. }
  2752. #endif
  2753. OPCODE_OUT;
  2754. }
  2755. OPCODES_OUT
  2756. #ifdef DEBUG_ENABLED
  2757. if (GDScriptLanguage::get_singleton()->profiling) {
  2758. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  2759. profile.total_time += time_taken;
  2760. profile.self_time += time_taken - function_call_time;
  2761. profile.frame_total_time += time_taken;
  2762. profile.frame_self_time += time_taken - function_call_time;
  2763. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  2764. }
  2765. // Check if this is the last time the function is resuming from await
  2766. // Will be true if never awaited as well
  2767. // When it's the last resume it will postpone the exit from stack,
  2768. // so the debugger knows which function triggered the resume of the next function (if any)
  2769. if (!p_state || awaited) {
  2770. if (EngineDebugger::is_active()) {
  2771. GDScriptLanguage::get_singleton()->exit_function();
  2772. }
  2773. #endif
  2774. if (_stack_size) {
  2775. //free stack
  2776. for (int i = 0; i < _stack_size; i++) {
  2777. stack[i].~Variant();
  2778. }
  2779. }
  2780. #ifdef DEBUG_ENABLED
  2781. }
  2782. #endif
  2783. return retvalue;
  2784. }