gdscript_vm.cpp 109 KB

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