gdscript_vm.cpp 106 KB

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