gdscript_function.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. /*************************************************************************/
  2. /* gdscript_function.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/os/os.h"
  32. #include "gdscript.h"
  33. #include "gdscript_functions.h"
  34. Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant *p_stack, String &r_error) const {
  35. int address = p_address & ADDR_MASK;
  36. //sequential table (jump table generated by compiler)
  37. switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) {
  38. case ADDR_TYPE_SELF: {
  39. #ifdef DEBUG_ENABLED
  40. if (unlikely(!p_instance)) {
  41. r_error = "Cannot access self without instance.";
  42. return NULL;
  43. }
  44. #endif
  45. return &self;
  46. } break;
  47. case ADDR_TYPE_CLASS: {
  48. return &p_script->_static_ref;
  49. } break;
  50. case ADDR_TYPE_MEMBER: {
  51. #ifdef DEBUG_ENABLED
  52. if (unlikely(!p_instance)) {
  53. r_error = "Cannot access member without instance.";
  54. return NULL;
  55. }
  56. #endif
  57. //member indexing is O(1)
  58. return &p_instance->members.write[address];
  59. } break;
  60. case ADDR_TYPE_CLASS_CONSTANT: {
  61. //todo change to index!
  62. GDScript *o = p_script;
  63. #ifdef DEBUG_ENABLED
  64. ERR_FAIL_INDEX_V(address, _global_names_count, NULL);
  65. #endif
  66. const StringName *sn = &_global_names_ptr[address];
  67. while (o) {
  68. GDScript *s = o;
  69. while (s) {
  70. Map<StringName, Variant>::Element *E = s->constants.find(*sn);
  71. if (E) {
  72. return &E->get();
  73. }
  74. s = s->_base;
  75. }
  76. o = o->_owner;
  77. }
  78. ERR_EXPLAIN("GDScriptCompiler bug...");
  79. ERR_FAIL_V(NULL);
  80. } break;
  81. case ADDR_TYPE_LOCAL_CONSTANT: {
  82. #ifdef DEBUG_ENABLED
  83. ERR_FAIL_INDEX_V(address, _constant_count, NULL);
  84. #endif
  85. return &_constants_ptr[address];
  86. } break;
  87. case ADDR_TYPE_STACK:
  88. case ADDR_TYPE_STACK_VARIABLE: {
  89. #ifdef DEBUG_ENABLED
  90. ERR_FAIL_INDEX_V(address, _stack_size, NULL);
  91. #endif
  92. return &p_stack[address];
  93. } break;
  94. case ADDR_TYPE_GLOBAL: {
  95. #ifdef DEBUG_ENABLED
  96. ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), NULL);
  97. #endif
  98. return &GDScriptLanguage::get_singleton()->get_global_array()[address];
  99. } break;
  100. #ifdef TOOLS_ENABLED
  101. case ADDR_TYPE_NAMED_GLOBAL: {
  102. #ifdef DEBUG_ENABLED
  103. ERR_FAIL_INDEX_V(address, _named_globals_count, NULL);
  104. #endif
  105. StringName id = _named_globals_ptr[address];
  106. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
  107. return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
  108. } else {
  109. r_error = "Autoload singleton '" + String(id) + "' has been removed.";
  110. return NULL;
  111. }
  112. } break;
  113. #endif
  114. case ADDR_TYPE_NIL: {
  115. return &nil;
  116. } break;
  117. }
  118. ERR_EXPLAIN("Bad Code! (Addressing Mode)");
  119. ERR_FAIL_V(NULL);
  120. return NULL;
  121. }
  122. String GDScriptFunction::_get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const {
  123. String err_text;
  124. if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  125. int errorarg = p_err.argument;
  126. 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(p_err.expected) + ".";
  127. } else if (p_err.error == Variant::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 == Variant::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 == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  132. err_text = "Invalid call. Nonexistent " + p_where + ".";
  133. } else if (p_err.error == Variant::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. #ifdef DEBUG_ENABLED
  141. static String _get_var_type(const Variant *p_type) {
  142. String basestr;
  143. if (p_type->get_type() == Variant::OBJECT) {
  144. Object *bobj = *p_type;
  145. if (!bobj) {
  146. basestr = "null instance";
  147. } else {
  148. if (ObjectDB::instance_validate(bobj)) {
  149. if (bobj->get_script_instance())
  150. basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")";
  151. else
  152. basestr = bobj->get_class();
  153. } else {
  154. basestr = "previously freed instance";
  155. }
  156. }
  157. } else {
  158. basestr = Variant::get_type_name(p_type->get_type());
  159. }
  160. return basestr;
  161. }
  162. #endif
  163. #if defined(__GNUC__)
  164. #define OPCODES_TABLE \
  165. static const void *switch_table_ops[] = { \
  166. &&OPCODE_OPERATOR, \
  167. &&OPCODE_EXTENDS_TEST, \
  168. &&OPCODE_IS_BUILTIN, \
  169. &&OPCODE_SET, \
  170. &&OPCODE_GET, \
  171. &&OPCODE_SET_NAMED, \
  172. &&OPCODE_GET_NAMED, \
  173. &&OPCODE_SET_MEMBER, \
  174. &&OPCODE_GET_MEMBER, \
  175. &&OPCODE_ASSIGN, \
  176. &&OPCODE_ASSIGN_TRUE, \
  177. &&OPCODE_ASSIGN_FALSE, \
  178. &&OPCODE_ASSIGN_TYPED_BUILTIN, \
  179. &&OPCODE_ASSIGN_TYPED_NATIVE, \
  180. &&OPCODE_ASSIGN_TYPED_SCRIPT, \
  181. &&OPCODE_CAST_TO_BUILTIN, \
  182. &&OPCODE_CAST_TO_NATIVE, \
  183. &&OPCODE_CAST_TO_SCRIPT, \
  184. &&OPCODE_CONSTRUCT, \
  185. &&OPCODE_CONSTRUCT_ARRAY, \
  186. &&OPCODE_CONSTRUCT_DICTIONARY, \
  187. &&OPCODE_CALL, \
  188. &&OPCODE_CALL_RETURN, \
  189. &&OPCODE_CALL_BUILT_IN, \
  190. &&OPCODE_CALL_SELF, \
  191. &&OPCODE_CALL_SELF_BASE, \
  192. &&OPCODE_YIELD, \
  193. &&OPCODE_YIELD_SIGNAL, \
  194. &&OPCODE_YIELD_RESUME, \
  195. &&OPCODE_JUMP, \
  196. &&OPCODE_JUMP_IF, \
  197. &&OPCODE_JUMP_IF_NOT, \
  198. &&OPCODE_JUMP_TO_DEF_ARGUMENT, \
  199. &&OPCODE_RETURN, \
  200. &&OPCODE_ITERATE_BEGIN, \
  201. &&OPCODE_ITERATE, \
  202. &&OPCODE_ASSERT, \
  203. &&OPCODE_BREAKPOINT, \
  204. &&OPCODE_LINE, \
  205. &&OPCODE_END \
  206. };
  207. #define OPCODE(m_op) \
  208. m_op:
  209. #define OPCODE_WHILE(m_test)
  210. #define OPCODES_END \
  211. OPSEXIT:
  212. #define OPCODES_OUT \
  213. OPSOUT:
  214. #define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]]
  215. #define OPCODE_SWITCH(m_test) DISPATCH_OPCODE;
  216. #define OPCODE_BREAK goto OPSEXIT
  217. #define OPCODE_OUT goto OPSOUT
  218. #else
  219. #define OPCODES_TABLE
  220. #define OPCODE(m_op) case m_op:
  221. #define OPCODE_WHILE(m_test) while (m_test)
  222. #define OPCODES_END
  223. #define OPCODES_OUT
  224. #define DISPATCH_OPCODE continue
  225. #define OPCODE_SWITCH(m_test) switch (m_test)
  226. #define OPCODE_BREAK break
  227. #define OPCODE_OUT break
  228. #endif
  229. Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state) {
  230. OPCODES_TABLE;
  231. if (!_code_ptr) {
  232. return Variant();
  233. }
  234. r_err.error = Variant::CallError::CALL_OK;
  235. Variant self;
  236. Variant retvalue;
  237. Variant *stack = NULL;
  238. Variant **call_args;
  239. int defarg = 0;
  240. #ifdef DEBUG_ENABLED
  241. //GDScriptLanguage::get_singleton()->calls++;
  242. #endif
  243. uint32_t alloca_size = 0;
  244. GDScript *script;
  245. int ip = 0;
  246. int line = _initial_line;
  247. if (p_state) {
  248. //use existing (supplied) state (yielded)
  249. stack = (Variant *)p_state->stack.ptr();
  250. call_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check
  251. line = p_state->line;
  252. ip = p_state->ip;
  253. alloca_size = p_state->stack.size();
  254. script = p_state->script.ptr();
  255. p_instance = p_state->instance;
  256. defarg = p_state->defarg;
  257. self = p_state->self;
  258. //stack[p_state->result_pos]=p_state->result; //assign stack with result
  259. } else {
  260. if (p_argcount != _argument_count) {
  261. if (p_argcount > _argument_count) {
  262. r_err.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  263. r_err.argument = _argument_count;
  264. return Variant();
  265. } else if (p_argcount < _argument_count - _default_arg_count) {
  266. r_err.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  267. r_err.argument = _argument_count - _default_arg_count;
  268. return Variant();
  269. } else {
  270. defarg = _argument_count - p_argcount;
  271. }
  272. }
  273. alloca_size = sizeof(Variant *) * _call_size + sizeof(Variant) * _stack_size;
  274. if (alloca_size) {
  275. uint8_t *aptr = (uint8_t *)alloca(alloca_size);
  276. if (_stack_size) {
  277. stack = (Variant *)aptr;
  278. for (int i = 0; i < p_argcount; i++) {
  279. if (!argument_types[i].has_type) {
  280. memnew_placement(&stack[i], Variant(*p_args[i]));
  281. continue;
  282. }
  283. if (!argument_types[i].is_type(*p_args[i], true)) {
  284. r_err.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  285. r_err.argument = i;
  286. r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT;
  287. return Variant();
  288. }
  289. if (argument_types[i].kind == GDScriptDataType::BUILTIN) {
  290. Variant arg = Variant::construct(argument_types[i].builtin_type, &p_args[i], 1, r_err);
  291. memnew_placement(&stack[i], Variant(arg));
  292. } else {
  293. memnew_placement(&stack[i], Variant(*p_args[i]));
  294. }
  295. }
  296. for (int i = p_argcount; i < _stack_size; i++) {
  297. memnew_placement(&stack[i], Variant);
  298. }
  299. } else {
  300. stack = NULL;
  301. }
  302. if (_call_size) {
  303. call_args = (Variant **)&aptr[sizeof(Variant) * _stack_size];
  304. } else {
  305. call_args = NULL;
  306. }
  307. } else {
  308. stack = NULL;
  309. call_args = NULL;
  310. }
  311. if (p_instance) {
  312. if (p_instance->base_ref && static_cast<Reference *>(p_instance->owner)->is_referenced()) {
  313. self = REF(static_cast<Reference *>(p_instance->owner));
  314. } else {
  315. self = p_instance->owner;
  316. }
  317. script = p_instance->script.ptr();
  318. } else {
  319. script = _script;
  320. }
  321. }
  322. String err_text;
  323. #ifdef DEBUG_ENABLED
  324. if (ScriptDebugger::get_singleton())
  325. GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line);
  326. #define GD_ERR_BREAK(m_cond) \
  327. { \
  328. if (unlikely(m_cond)) { \
  329. _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \
  330. OPCODE_BREAK; \
  331. } else \
  332. _err_error_exists = false; \
  333. }
  334. #define CHECK_SPACE(m_space) \
  335. GD_ERR_BREAK((ip + m_space) > _code_size)
  336. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  337. Variant *m_v; \
  338. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, stack, err_text); \
  339. if (unlikely(!m_v)) \
  340. OPCODE_BREAK;
  341. #else
  342. #define GD_ERR_BREAK(m_cond)
  343. #define CHECK_SPACE(m_space)
  344. #define GET_VARIANT_PTR(m_v, m_code_ofs) \
  345. Variant *m_v; \
  346. m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, script, self, stack, err_text);
  347. #endif
  348. #ifdef DEBUG_ENABLED
  349. uint64_t function_start_time = 0;
  350. uint64_t function_call_time = 0;
  351. if (GDScriptLanguage::get_singleton()->profiling) {
  352. function_start_time = OS::get_singleton()->get_ticks_usec();
  353. function_call_time = 0;
  354. profile.call_count++;
  355. profile.frame_call_count++;
  356. }
  357. bool exit_ok = false;
  358. #endif
  359. #ifdef DEBUG_ENABLED
  360. OPCODE_WHILE(ip < _code_size) {
  361. int last_opcode = _code_ptr[ip];
  362. #else
  363. OPCODE_WHILE(true) {
  364. #endif
  365. OPCODE_SWITCH(_code_ptr[ip]) {
  366. OPCODE(OPCODE_OPERATOR) {
  367. CHECK_SPACE(5);
  368. bool valid;
  369. Variant::Operator op = (Variant::Operator)_code_ptr[ip + 1];
  370. GD_ERR_BREAK(op >= Variant::OP_MAX);
  371. GET_VARIANT_PTR(a, 2);
  372. GET_VARIANT_PTR(b, 3);
  373. GET_VARIANT_PTR(dst, 4);
  374. #ifdef DEBUG_ENABLED
  375. Variant ret;
  376. Variant::evaluate(op, *a, *b, ret, valid);
  377. #else
  378. Variant::evaluate(op, *a, *b, *dst, valid);
  379. #endif
  380. #ifdef DEBUG_ENABLED
  381. if (!valid) {
  382. if (ret.get_type() == Variant::STRING) {
  383. //return a string when invalid with the error
  384. err_text = ret;
  385. err_text += " in operator '" + Variant::get_operator_name(op) + "'.";
  386. } else {
  387. 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) + "'.";
  388. }
  389. OPCODE_BREAK;
  390. }
  391. *dst = ret;
  392. #endif
  393. ip += 5;
  394. }
  395. DISPATCH_OPCODE;
  396. OPCODE(OPCODE_EXTENDS_TEST) {
  397. CHECK_SPACE(4);
  398. GET_VARIANT_PTR(a, 1);
  399. GET_VARIANT_PTR(b, 2);
  400. GET_VARIANT_PTR(dst, 3);
  401. #ifdef DEBUG_ENABLED
  402. if (b->get_type() != Variant::OBJECT || b->operator Object *() == NULL) {
  403. err_text = "Right operand of 'is' is not a class.";
  404. OPCODE_BREAK;
  405. }
  406. #endif
  407. bool extends_ok = false;
  408. if (a->get_type() == Variant::OBJECT && a->operator Object *() != NULL) {
  409. Object *obj_A = *a;
  410. Object *obj_B = *b;
  411. GDScript *scr_B = Object::cast_to<GDScript>(obj_B);
  412. if (scr_B) {
  413. //if B is a script, the only valid condition is that A has an instance which inherits from the script
  414. //in other situation, this shoul return false.
  415. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) {
  416. GDScript *cmp = static_cast<GDScript *>(obj_A->get_script_instance()->get_script().ptr());
  417. //bool found=false;
  418. while (cmp) {
  419. if (cmp == scr_B) {
  420. //inherits from script, all ok
  421. extends_ok = true;
  422. break;
  423. }
  424. cmp = cmp->_base;
  425. }
  426. }
  427. } else {
  428. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(obj_B);
  429. #ifdef DEBUG_ENABLED
  430. if (!nc) {
  431. err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "').";
  432. OPCODE_BREAK;
  433. }
  434. #endif
  435. extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name());
  436. }
  437. }
  438. *dst = extends_ok;
  439. ip += 4;
  440. }
  441. DISPATCH_OPCODE;
  442. OPCODE(OPCODE_IS_BUILTIN) {
  443. CHECK_SPACE(4);
  444. GET_VARIANT_PTR(value, 1);
  445. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 2];
  446. GET_VARIANT_PTR(dst, 3);
  447. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  448. *dst = value->get_type() == var_type;
  449. ip += 4;
  450. }
  451. DISPATCH_OPCODE;
  452. OPCODE(OPCODE_SET) {
  453. CHECK_SPACE(3);
  454. GET_VARIANT_PTR(dst, 1);
  455. GET_VARIANT_PTR(index, 2);
  456. GET_VARIANT_PTR(value, 3);
  457. bool valid;
  458. dst->set(*index, *value, &valid);
  459. #ifdef DEBUG_ENABLED
  460. if (!valid) {
  461. String v = index->operator String();
  462. if (v != "") {
  463. v = "'" + v + "'";
  464. } else {
  465. v = "of type '" + _get_var_type(index) + "'";
  466. }
  467. err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'";
  468. OPCODE_BREAK;
  469. }
  470. #endif
  471. ip += 4;
  472. }
  473. DISPATCH_OPCODE;
  474. OPCODE(OPCODE_GET) {
  475. CHECK_SPACE(3);
  476. GET_VARIANT_PTR(src, 1);
  477. GET_VARIANT_PTR(index, 2);
  478. GET_VARIANT_PTR(dst, 3);
  479. bool valid;
  480. #ifdef DEBUG_ENABLED
  481. //allow better error message in cases where src and dst are the same stack position
  482. Variant ret = src->get(*index, &valid);
  483. #else
  484. *dst = src->get(*index, &valid);
  485. #endif
  486. #ifdef DEBUG_ENABLED
  487. if (!valid) {
  488. String v = index->operator String();
  489. if (v != "") {
  490. v = "'" + v + "'";
  491. } else {
  492. v = "of type '" + _get_var_type(index) + "'";
  493. }
  494. err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "').";
  495. OPCODE_BREAK;
  496. }
  497. *dst = ret;
  498. #endif
  499. ip += 4;
  500. }
  501. DISPATCH_OPCODE;
  502. OPCODE(OPCODE_SET_NAMED) {
  503. CHECK_SPACE(3);
  504. GET_VARIANT_PTR(dst, 1);
  505. GET_VARIANT_PTR(value, 3);
  506. int indexname = _code_ptr[ip + 2];
  507. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  508. const StringName *index = &_global_names_ptr[indexname];
  509. bool valid;
  510. dst->set_named(*index, *value, &valid);
  511. #ifdef DEBUG_ENABLED
  512. if (!valid) {
  513. String err_type;
  514. err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'.";
  515. OPCODE_BREAK;
  516. }
  517. #endif
  518. ip += 4;
  519. }
  520. DISPATCH_OPCODE;
  521. OPCODE(OPCODE_GET_NAMED) {
  522. CHECK_SPACE(4);
  523. GET_VARIANT_PTR(src, 1);
  524. GET_VARIANT_PTR(dst, 3);
  525. int indexname = _code_ptr[ip + 2];
  526. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  527. const StringName *index = &_global_names_ptr[indexname];
  528. bool valid;
  529. #ifdef DEBUG_ENABLED
  530. //allow better error message in cases where src and dst are the same stack position
  531. Variant ret = src->get_named(*index, &valid);
  532. #else
  533. *dst = src->get_named(*index, &valid);
  534. #endif
  535. #ifdef DEBUG_ENABLED
  536. if (!valid) {
  537. if (src->has_method(*index)) {
  538. 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() + "\") ?";
  539. } else {
  540. err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "').";
  541. }
  542. OPCODE_BREAK;
  543. }
  544. *dst = ret;
  545. #endif
  546. ip += 4;
  547. }
  548. DISPATCH_OPCODE;
  549. OPCODE(OPCODE_SET_MEMBER) {
  550. CHECK_SPACE(3);
  551. int indexname = _code_ptr[ip + 1];
  552. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  553. const StringName *index = &_global_names_ptr[indexname];
  554. GET_VARIANT_PTR(src, 2);
  555. bool valid;
  556. #ifndef DEBUG_ENABLED
  557. ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  558. #else
  559. bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid);
  560. if (!ok) {
  561. err_text = "Internal error setting property: " + String(*index);
  562. OPCODE_BREAK;
  563. } else if (!valid) {
  564. err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + ".";
  565. OPCODE_BREAK;
  566. }
  567. #endif
  568. ip += 3;
  569. }
  570. DISPATCH_OPCODE;
  571. OPCODE(OPCODE_GET_MEMBER) {
  572. CHECK_SPACE(3);
  573. int indexname = _code_ptr[ip + 1];
  574. GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count);
  575. const StringName *index = &_global_names_ptr[indexname];
  576. GET_VARIANT_PTR(dst, 2);
  577. #ifndef DEBUG_ENABLED
  578. ClassDB::get_property(p_instance->owner, *index, *dst);
  579. #else
  580. bool ok = ClassDB::get_property(p_instance->owner, *index, *dst);
  581. if (!ok) {
  582. err_text = "Internal error getting property: " + String(*index);
  583. OPCODE_BREAK;
  584. }
  585. #endif
  586. ip += 3;
  587. }
  588. DISPATCH_OPCODE;
  589. OPCODE(OPCODE_ASSIGN) {
  590. CHECK_SPACE(3);
  591. GET_VARIANT_PTR(dst, 1);
  592. GET_VARIANT_PTR(src, 2);
  593. *dst = *src;
  594. ip += 3;
  595. }
  596. DISPATCH_OPCODE;
  597. OPCODE(OPCODE_ASSIGN_TRUE) {
  598. CHECK_SPACE(2);
  599. GET_VARIANT_PTR(dst, 1);
  600. *dst = true;
  601. ip += 2;
  602. }
  603. DISPATCH_OPCODE;
  604. OPCODE(OPCODE_ASSIGN_FALSE) {
  605. CHECK_SPACE(2);
  606. GET_VARIANT_PTR(dst, 1);
  607. *dst = false;
  608. ip += 2;
  609. }
  610. DISPATCH_OPCODE;
  611. OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) {
  612. CHECK_SPACE(4);
  613. GET_VARIANT_PTR(dst, 2);
  614. GET_VARIANT_PTR(src, 3);
  615. #ifdef DEBUG_ENABLED
  616. Variant::Type var_type = (Variant::Type)_code_ptr[ip + 1];
  617. GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX);
  618. if (src->get_type() != var_type) {
  619. if (Variant::can_convert_strict(src->get_type(), var_type)) {
  620. Variant::CallError ce;
  621. *dst = Variant::construct(var_type, const_cast<const Variant **>(&src), 1, ce);
  622. } else {
  623. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  624. "' to a variable of type '" + Variant::get_type_name(var_type) + "'.";
  625. OPCODE_BREAK;
  626. }
  627. } else {
  628. #endif // DEBUG_ENABLED
  629. *dst = *src;
  630. #ifdef DEBUG_ENABLED
  631. }
  632. #endif // DEBUG_ENABLED
  633. ip += 4;
  634. }
  635. DISPATCH_OPCODE;
  636. OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) {
  637. CHECK_SPACE(4);
  638. GET_VARIANT_PTR(dst, 2);
  639. GET_VARIANT_PTR(src, 3);
  640. #ifdef DEBUG_ENABLED
  641. GET_VARIANT_PTR(type, 1);
  642. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(type->operator Object *());
  643. GD_ERR_BREAK(!nc);
  644. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  645. err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) +
  646. "' to a variable of type '" + nc->get_name() + "'.";
  647. OPCODE_BREAK;
  648. }
  649. Object *src_obj = src->operator Object *();
  650. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  651. err_text = "Trying to assign value of type '" + src_obj->get_class_name() +
  652. "' to a variable of type '" + nc->get_name() + "'.";
  653. OPCODE_BREAK;
  654. }
  655. #endif // DEBUG_ENABLED
  656. *dst = *src;
  657. ip += 4;
  658. }
  659. DISPATCH_OPCODE;
  660. OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) {
  661. CHECK_SPACE(4);
  662. GET_VARIANT_PTR(dst, 2);
  663. GET_VARIANT_PTR(src, 3);
  664. #ifdef DEBUG_ENABLED
  665. GET_VARIANT_PTR(type, 1);
  666. Script *base_type = Object::cast_to<Script>(type->operator Object *());
  667. GD_ERR_BREAK(!base_type);
  668. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  669. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  670. OPCODE_BREAK;
  671. }
  672. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  673. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  674. if (!scr_inst) {
  675. err_text = "Trying to assign value of type '" + src->operator Object *()->get_class_name() +
  676. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  677. OPCODE_BREAK;
  678. }
  679. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  680. bool valid = false;
  681. while (src_type) {
  682. if (src_type == base_type) {
  683. valid = true;
  684. break;
  685. }
  686. src_type = src_type->get_base_script().ptr();
  687. }
  688. if (!valid) {
  689. err_text = "Trying to assign value of type '" + src->operator Object *()->get_script_instance()->get_script()->get_path().get_file() +
  690. "' to a variable of type '" + base_type->get_path().get_file() + "'.";
  691. OPCODE_BREAK;
  692. }
  693. }
  694. #endif // DEBUG_ENABLED
  695. *dst = *src;
  696. ip += 4;
  697. }
  698. DISPATCH_OPCODE;
  699. OPCODE(OPCODE_CAST_TO_BUILTIN) {
  700. CHECK_SPACE(4);
  701. Variant::Type to_type = (Variant::Type)_code_ptr[ip + 1];
  702. GET_VARIANT_PTR(src, 2);
  703. GET_VARIANT_PTR(dst, 3);
  704. GD_ERR_BREAK(to_type < 0 || to_type >= Variant::VARIANT_MAX);
  705. Variant::CallError err;
  706. *dst = Variant::construct(to_type, (const Variant **)&src, 1, err);
  707. #ifdef DEBUG_ENABLED
  708. if (err.error != Variant::CallError::CALL_OK) {
  709. err_text = "Invalid cast: could not convert value to '" + Variant::get_type_name(to_type) + "'.";
  710. OPCODE_BREAK;
  711. }
  712. #endif
  713. ip += 4;
  714. }
  715. DISPATCH_OPCODE;
  716. OPCODE(OPCODE_CAST_TO_NATIVE) {
  717. CHECK_SPACE(4);
  718. GET_VARIANT_PTR(to_type, 1);
  719. GET_VARIANT_PTR(src, 2);
  720. GET_VARIANT_PTR(dst, 3);
  721. GDScriptNativeClass *nc = Object::cast_to<GDScriptNativeClass>(to_type->operator Object *());
  722. GD_ERR_BREAK(!nc);
  723. #ifdef DEBUG_ENABLED
  724. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  725. err_text = "Invalid cast: can't convert a non-object value to an object type.";
  726. OPCODE_BREAK;
  727. }
  728. #endif
  729. Object *src_obj = src->operator Object *();
  730. if (src_obj && !ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) {
  731. *dst = Variant(); // invalid cast, assign NULL
  732. } else {
  733. *dst = *src;
  734. }
  735. ip += 4;
  736. }
  737. DISPATCH_OPCODE;
  738. OPCODE(OPCODE_CAST_TO_SCRIPT) {
  739. CHECK_SPACE(4);
  740. GET_VARIANT_PTR(to_type, 1);
  741. GET_VARIANT_PTR(src, 2);
  742. GET_VARIANT_PTR(dst, 3);
  743. Script *base_type = Object::cast_to<Script>(to_type->operator Object *());
  744. GD_ERR_BREAK(!base_type);
  745. #ifdef DEBUG_ENABLED
  746. if (src->get_type() != Variant::OBJECT && src->get_type() != Variant::NIL) {
  747. err_text = "Trying to assign a non-object value to a variable of type '" + base_type->get_path().get_file() + "'.";
  748. OPCODE_BREAK;
  749. }
  750. #endif
  751. bool valid = false;
  752. if (src->get_type() != Variant::NIL && src->operator Object *() != NULL) {
  753. ScriptInstance *scr_inst = src->operator Object *()->get_script_instance();
  754. if (scr_inst) {
  755. Script *src_type = src->operator Object *()->get_script_instance()->get_script().ptr();
  756. while (src_type) {
  757. if (src_type == base_type) {
  758. valid = true;
  759. break;
  760. }
  761. src_type = src_type->get_base_script().ptr();
  762. }
  763. }
  764. }
  765. if (valid) {
  766. *dst = *src; // Valid cast, copy the source object
  767. } else {
  768. *dst = Variant(); // invalid cast, assign NULL
  769. }
  770. ip += 4;
  771. }
  772. DISPATCH_OPCODE;
  773. OPCODE(OPCODE_CONSTRUCT) {
  774. CHECK_SPACE(2);
  775. Variant::Type t = Variant::Type(_code_ptr[ip + 1]);
  776. int argc = _code_ptr[ip + 2];
  777. CHECK_SPACE(argc + 2);
  778. Variant **argptrs = call_args;
  779. for (int i = 0; i < argc; i++) {
  780. GET_VARIANT_PTR(v, 3 + i);
  781. argptrs[i] = v;
  782. }
  783. GET_VARIANT_PTR(dst, 3 + argc);
  784. Variant::CallError err;
  785. *dst = Variant::construct(t, (const Variant **)argptrs, argc, err);
  786. #ifdef DEBUG_ENABLED
  787. if (err.error != Variant::CallError::CALL_OK) {
  788. err_text = _get_call_error(err, "'" + Variant::get_type_name(t) + "' constructor", (const Variant **)argptrs);
  789. OPCODE_BREAK;
  790. }
  791. #endif
  792. ip += 4 + argc;
  793. //construct a basic type
  794. }
  795. DISPATCH_OPCODE;
  796. OPCODE(OPCODE_CONSTRUCT_ARRAY) {
  797. CHECK_SPACE(1);
  798. int argc = _code_ptr[ip + 1];
  799. Array array; //arrays are always shared
  800. array.resize(argc);
  801. CHECK_SPACE(argc + 2);
  802. for (int i = 0; i < argc; i++) {
  803. GET_VARIANT_PTR(v, 2 + i);
  804. array[i] = *v;
  805. }
  806. GET_VARIANT_PTR(dst, 2 + argc);
  807. *dst = array;
  808. ip += 3 + argc;
  809. }
  810. DISPATCH_OPCODE;
  811. OPCODE(OPCODE_CONSTRUCT_DICTIONARY) {
  812. CHECK_SPACE(1);
  813. int argc = _code_ptr[ip + 1];
  814. Dictionary dict; //arrays are always shared
  815. CHECK_SPACE(argc * 2 + 2);
  816. for (int i = 0; i < argc; i++) {
  817. GET_VARIANT_PTR(k, 2 + i * 2 + 0);
  818. GET_VARIANT_PTR(v, 2 + i * 2 + 1);
  819. dict[*k] = *v;
  820. }
  821. GET_VARIANT_PTR(dst, 2 + argc * 2);
  822. *dst = dict;
  823. ip += 3 + argc * 2;
  824. }
  825. DISPATCH_OPCODE;
  826. OPCODE(OPCODE_CALL_RETURN)
  827. OPCODE(OPCODE_CALL) {
  828. CHECK_SPACE(4);
  829. bool call_ret = _code_ptr[ip] == OPCODE_CALL_RETURN;
  830. int argc = _code_ptr[ip + 1];
  831. GET_VARIANT_PTR(base, 2);
  832. int nameg = _code_ptr[ip + 3];
  833. GD_ERR_BREAK(nameg < 0 || nameg >= _global_names_count);
  834. const StringName *methodname = &_global_names_ptr[nameg];
  835. GD_ERR_BREAK(argc < 0);
  836. ip += 4;
  837. CHECK_SPACE(argc + 1);
  838. Variant **argptrs = call_args;
  839. for (int i = 0; i < argc; i++) {
  840. GET_VARIANT_PTR(v, i);
  841. argptrs[i] = v;
  842. }
  843. #ifdef DEBUG_ENABLED
  844. uint64_t call_time = 0;
  845. if (GDScriptLanguage::get_singleton()->profiling) {
  846. call_time = OS::get_singleton()->get_ticks_usec();
  847. }
  848. #endif
  849. Variant::CallError err;
  850. if (call_ret) {
  851. GET_VARIANT_PTR(ret, argc);
  852. base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err);
  853. } else {
  854. base->call_ptr(*methodname, (const Variant **)argptrs, argc, NULL, err);
  855. }
  856. #ifdef DEBUG_ENABLED
  857. if (GDScriptLanguage::get_singleton()->profiling) {
  858. function_call_time += OS::get_singleton()->get_ticks_usec() - call_time;
  859. }
  860. if (err.error != Variant::CallError::CALL_OK) {
  861. String methodstr = *methodname;
  862. String basestr = _get_var_type(base);
  863. if (methodstr == "call") {
  864. if (argc >= 1) {
  865. methodstr = String(*argptrs[0]) + " (via call)";
  866. if (err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) {
  867. err.argument -= 1;
  868. }
  869. }
  870. } else if (methodstr == "free") {
  871. if (err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) {
  872. if (base->is_ref()) {
  873. err_text = "Attempted to free a reference.";
  874. OPCODE_BREAK;
  875. } else if (base->get_type() == Variant::OBJECT) {
  876. err_text = "Attempted to free a locked object (calling or emitting).";
  877. OPCODE_BREAK;
  878. }
  879. }
  880. }
  881. err_text = _get_call_error(err, "function '" + methodstr + "' in base '" + basestr + "'", (const Variant **)argptrs);
  882. OPCODE_BREAK;
  883. }
  884. #endif
  885. //_call_func(NULL,base,*methodname,ip,argc,p_instance,stack);
  886. ip += argc + 1;
  887. }
  888. DISPATCH_OPCODE;
  889. OPCODE(OPCODE_CALL_BUILT_IN) {
  890. CHECK_SPACE(4);
  891. GDScriptFunctions::Function func = GDScriptFunctions::Function(_code_ptr[ip + 1]);
  892. int argc = _code_ptr[ip + 2];
  893. GD_ERR_BREAK(argc < 0);
  894. ip += 3;
  895. CHECK_SPACE(argc + 1);
  896. Variant **argptrs = call_args;
  897. for (int i = 0; i < argc; i++) {
  898. GET_VARIANT_PTR(v, i);
  899. argptrs[i] = v;
  900. }
  901. GET_VARIANT_PTR(dst, argc);
  902. Variant::CallError err;
  903. GDScriptFunctions::call(func, (const Variant **)argptrs, argc, *dst, err);
  904. #ifdef DEBUG_ENABLED
  905. if (err.error != Variant::CallError::CALL_OK) {
  906. String methodstr = GDScriptFunctions::get_func_name(func);
  907. if (dst->get_type() == Variant::STRING) {
  908. //call provided error string
  909. err_text = "Error calling built-in function '" + methodstr + "': " + String(*dst);
  910. } else {
  911. err_text = _get_call_error(err, "built-in function '" + methodstr + "'", (const Variant **)argptrs);
  912. }
  913. OPCODE_BREAK;
  914. }
  915. #endif
  916. ip += argc + 1;
  917. }
  918. DISPATCH_OPCODE;
  919. OPCODE(OPCODE_CALL_SELF) {
  920. OPCODE_BREAK;
  921. }
  922. OPCODE(OPCODE_CALL_SELF_BASE) {
  923. CHECK_SPACE(2);
  924. int self_fun = _code_ptr[ip + 1];
  925. #ifdef DEBUG_ENABLED
  926. if (self_fun < 0 || self_fun >= _global_names_count) {
  927. err_text = "compiler bug, function name not found";
  928. OPCODE_BREAK;
  929. }
  930. #endif
  931. const StringName *methodname = &_global_names_ptr[self_fun];
  932. int argc = _code_ptr[ip + 2];
  933. CHECK_SPACE(2 + argc + 1);
  934. Variant **argptrs = call_args;
  935. for (int i = 0; i < argc; i++) {
  936. GET_VARIANT_PTR(v, i + 3);
  937. argptrs[i] = v;
  938. }
  939. GET_VARIANT_PTR(dst, argc + 3);
  940. const GDScript *gds = _script;
  941. const Map<StringName, GDScriptFunction *>::Element *E = NULL;
  942. while (gds->base.ptr()) {
  943. gds = gds->base.ptr();
  944. E = gds->member_functions.find(*methodname);
  945. if (E)
  946. break;
  947. }
  948. Variant::CallError err;
  949. if (E) {
  950. *dst = E->get()->call(p_instance, (const Variant **)argptrs, argc, err);
  951. } else if (gds->native.ptr()) {
  952. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  953. MethodBind *mb = ClassDB::get_method(gds->native->get_name(), *methodname);
  954. if (!mb) {
  955. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  956. } else {
  957. *dst = mb->call(p_instance->owner, (const Variant **)argptrs, argc, err);
  958. }
  959. } else {
  960. err.error = Variant::CallError::CALL_OK;
  961. }
  962. } else {
  963. if (*methodname != GDScriptLanguage::get_singleton()->strings._init) {
  964. err.error = Variant::CallError::CALL_ERROR_INVALID_METHOD;
  965. } else {
  966. err.error = Variant::CallError::CALL_OK;
  967. }
  968. }
  969. if (err.error != Variant::CallError::CALL_OK) {
  970. String methodstr = *methodname;
  971. err_text = _get_call_error(err, "function '" + methodstr + "'", (const Variant **)argptrs);
  972. OPCODE_BREAK;
  973. }
  974. ip += 4 + argc;
  975. }
  976. DISPATCH_OPCODE;
  977. OPCODE(OPCODE_YIELD)
  978. OPCODE(OPCODE_YIELD_SIGNAL) {
  979. int ipofs = 1;
  980. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  981. CHECK_SPACE(4);
  982. ipofs += 2;
  983. } else {
  984. CHECK_SPACE(2);
  985. }
  986. Ref<GDScriptFunctionState> gdfs = memnew(GDScriptFunctionState);
  987. gdfs->function = this;
  988. gdfs->state.stack.resize(alloca_size);
  989. //copy variant stack
  990. for (int i = 0; i < _stack_size; i++) {
  991. memnew_placement(&gdfs->state.stack.write[sizeof(Variant) * i], Variant(stack[i]));
  992. }
  993. gdfs->state.stack_size = _stack_size;
  994. gdfs->state.self = self;
  995. gdfs->state.alloca_size = alloca_size;
  996. gdfs->state.script = Ref<GDScript>(_script);
  997. gdfs->state.ip = ip + ipofs;
  998. gdfs->state.line = line;
  999. gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : 0;
  1000. //gdfs->state.result_pos=ip+ipofs-1;
  1001. gdfs->state.defarg = defarg;
  1002. gdfs->state.instance = p_instance;
  1003. gdfs->function = this;
  1004. retvalue = gdfs;
  1005. if (_code_ptr[ip] == OPCODE_YIELD_SIGNAL) {
  1006. //do the oneshot connect
  1007. GET_VARIANT_PTR(argobj, 1);
  1008. GET_VARIANT_PTR(argname, 2);
  1009. #ifdef DEBUG_ENABLED
  1010. if (argobj->get_type() != Variant::OBJECT) {
  1011. err_text = "First argument of yield() not of type object.";
  1012. OPCODE_BREAK;
  1013. }
  1014. if (argname->get_type() != Variant::STRING) {
  1015. err_text = "Second argument of yield() not a string (for signal name).";
  1016. OPCODE_BREAK;
  1017. }
  1018. #endif
  1019. Object *obj = argobj->operator Object *();
  1020. String signal = argname->operator String();
  1021. #ifdef DEBUG_ENABLED
  1022. if (!obj) {
  1023. err_text = "First argument of yield() is null.";
  1024. OPCODE_BREAK;
  1025. }
  1026. if (ScriptDebugger::get_singleton()) {
  1027. if (!ObjectDB::instance_validate(obj)) {
  1028. err_text = "First argument of yield() is a previously freed instance.";
  1029. OPCODE_BREAK;
  1030. }
  1031. }
  1032. if (signal.length() == 0) {
  1033. err_text = "Second argument of yield() is an empty string (for signal name).";
  1034. OPCODE_BREAK;
  1035. }
  1036. Error err = obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1037. if (err != OK) {
  1038. err_text = "Error connecting to signal: " + signal + " during yield().";
  1039. OPCODE_BREAK;
  1040. }
  1041. #else
  1042. obj->connect(signal, gdfs.ptr(), "_signal_callback", varray(gdfs), Object::CONNECT_ONESHOT);
  1043. #endif
  1044. }
  1045. #ifdef DEBUG_ENABLED
  1046. exit_ok = true;
  1047. #endif
  1048. OPCODE_BREAK;
  1049. }
  1050. OPCODE(OPCODE_YIELD_RESUME) {
  1051. CHECK_SPACE(2);
  1052. #ifdef DEBUG_ENABLED
  1053. if (!p_state) {
  1054. err_text = ("Invalid Resume (bug?)");
  1055. OPCODE_BREAK;
  1056. }
  1057. #endif
  1058. GET_VARIANT_PTR(result, 1);
  1059. *result = p_state->result;
  1060. ip += 2;
  1061. }
  1062. DISPATCH_OPCODE;
  1063. OPCODE(OPCODE_JUMP) {
  1064. CHECK_SPACE(2);
  1065. int to = _code_ptr[ip + 1];
  1066. GD_ERR_BREAK(to < 0 || to > _code_size);
  1067. ip = to;
  1068. }
  1069. DISPATCH_OPCODE;
  1070. OPCODE(OPCODE_JUMP_IF) {
  1071. CHECK_SPACE(3);
  1072. GET_VARIANT_PTR(test, 1);
  1073. bool result = test->booleanize();
  1074. if (result) {
  1075. int to = _code_ptr[ip + 2];
  1076. GD_ERR_BREAK(to < 0 || to > _code_size);
  1077. ip = to;
  1078. } else {
  1079. ip += 3;
  1080. }
  1081. }
  1082. DISPATCH_OPCODE;
  1083. OPCODE(OPCODE_JUMP_IF_NOT) {
  1084. CHECK_SPACE(3);
  1085. GET_VARIANT_PTR(test, 1);
  1086. bool result = test->booleanize();
  1087. if (!result) {
  1088. int to = _code_ptr[ip + 2];
  1089. GD_ERR_BREAK(to < 0 || to > _code_size);
  1090. ip = to;
  1091. } else {
  1092. ip += 3;
  1093. }
  1094. }
  1095. DISPATCH_OPCODE;
  1096. OPCODE(OPCODE_JUMP_TO_DEF_ARGUMENT) {
  1097. CHECK_SPACE(2);
  1098. ip = _default_arg_ptr[defarg];
  1099. }
  1100. DISPATCH_OPCODE;
  1101. OPCODE(OPCODE_RETURN) {
  1102. CHECK_SPACE(2);
  1103. GET_VARIANT_PTR(r, 1);
  1104. retvalue = *r;
  1105. #ifdef DEBUG_ENABLED
  1106. exit_ok = true;
  1107. #endif
  1108. OPCODE_BREAK;
  1109. }
  1110. OPCODE(OPCODE_ITERATE_BEGIN) {
  1111. CHECK_SPACE(8); //space for this a regular iterate
  1112. GET_VARIANT_PTR(counter, 1);
  1113. GET_VARIANT_PTR(container, 2);
  1114. bool valid;
  1115. if (!container->iter_init(*counter, valid)) {
  1116. #ifdef DEBUG_ENABLED
  1117. if (!valid) {
  1118. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1119. OPCODE_BREAK;
  1120. }
  1121. #endif
  1122. int jumpto = _code_ptr[ip + 3];
  1123. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1124. ip = jumpto;
  1125. } else {
  1126. GET_VARIANT_PTR(iterator, 4);
  1127. *iterator = container->iter_get(*counter, valid);
  1128. #ifdef DEBUG_ENABLED
  1129. if (!valid) {
  1130. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "'.";
  1131. OPCODE_BREAK;
  1132. }
  1133. #endif
  1134. ip += 5; //skip regular iterate which is always next
  1135. }
  1136. }
  1137. DISPATCH_OPCODE;
  1138. OPCODE(OPCODE_ITERATE) {
  1139. CHECK_SPACE(4);
  1140. GET_VARIANT_PTR(counter, 1);
  1141. GET_VARIANT_PTR(container, 2);
  1142. bool valid;
  1143. if (!container->iter_next(*counter, valid)) {
  1144. #ifdef DEBUG_ENABLED
  1145. if (!valid) {
  1146. err_text = "Unable to iterate on object of type " + Variant::get_type_name(container->get_type()) + "' (type changed since first iteration?).";
  1147. OPCODE_BREAK;
  1148. }
  1149. #endif
  1150. int jumpto = _code_ptr[ip + 3];
  1151. GD_ERR_BREAK(jumpto < 0 || jumpto > _code_size);
  1152. ip = jumpto;
  1153. } else {
  1154. GET_VARIANT_PTR(iterator, 4);
  1155. *iterator = container->iter_get(*counter, valid);
  1156. #ifdef DEBUG_ENABLED
  1157. if (!valid) {
  1158. err_text = "Unable to obtain iterator object of type " + Variant::get_type_name(container->get_type()) + "' (but was obtained on first iteration?).";
  1159. OPCODE_BREAK;
  1160. }
  1161. #endif
  1162. ip += 5; //loop again
  1163. }
  1164. }
  1165. DISPATCH_OPCODE;
  1166. OPCODE(OPCODE_ASSERT) {
  1167. CHECK_SPACE(2);
  1168. #ifdef DEBUG_ENABLED
  1169. GET_VARIANT_PTR(test, 1);
  1170. bool result = test->booleanize();
  1171. if (!result) {
  1172. err_text = "Assertion failed.";
  1173. OPCODE_BREAK;
  1174. }
  1175. #endif
  1176. ip += 2;
  1177. }
  1178. DISPATCH_OPCODE;
  1179. OPCODE(OPCODE_BREAKPOINT) {
  1180. #ifdef DEBUG_ENABLED
  1181. if (ScriptDebugger::get_singleton()) {
  1182. GDScriptLanguage::get_singleton()->debug_break("Breakpoint Statement", true);
  1183. }
  1184. #endif
  1185. ip += 1;
  1186. }
  1187. DISPATCH_OPCODE;
  1188. OPCODE(OPCODE_LINE) {
  1189. CHECK_SPACE(2);
  1190. line = _code_ptr[ip + 1];
  1191. ip += 2;
  1192. if (ScriptDebugger::get_singleton()) {
  1193. // line
  1194. bool do_break = false;
  1195. if (ScriptDebugger::get_singleton()->get_lines_left() > 0) {
  1196. if (ScriptDebugger::get_singleton()->get_depth() <= 0)
  1197. ScriptDebugger::get_singleton()->set_lines_left(ScriptDebugger::get_singleton()->get_lines_left() - 1);
  1198. if (ScriptDebugger::get_singleton()->get_lines_left() <= 0)
  1199. do_break = true;
  1200. }
  1201. if (ScriptDebugger::get_singleton()->is_breakpoint(line, source))
  1202. do_break = true;
  1203. if (do_break) {
  1204. GDScriptLanguage::get_singleton()->debug_break("Breakpoint", true);
  1205. }
  1206. ScriptDebugger::get_singleton()->line_poll();
  1207. }
  1208. }
  1209. DISPATCH_OPCODE;
  1210. OPCODE(OPCODE_END) {
  1211. #ifdef DEBUG_ENABLED
  1212. exit_ok = true;
  1213. #endif
  1214. OPCODE_BREAK;
  1215. }
  1216. // Enable for debugging
  1217. #if 0
  1218. default: {
  1219. err_text = "Illegal opcode " + itos(_code_ptr[ip]) + " at address " + itos(ip);
  1220. OPCODE_BREAK;
  1221. }
  1222. #endif
  1223. }
  1224. OPCODES_END
  1225. #ifdef DEBUG_ENABLED
  1226. if (exit_ok)
  1227. OPCODE_OUT;
  1228. //error
  1229. // function, file, line, error, explanation
  1230. String err_file;
  1231. if (p_instance)
  1232. err_file = p_instance->script->path;
  1233. else if (script)
  1234. err_file = script->path;
  1235. if (err_file == "")
  1236. err_file = "<built-in>";
  1237. String err_func = name;
  1238. if (p_instance && p_instance->script->name != "")
  1239. err_func = p_instance->script->name + "." + err_func;
  1240. int err_line = line;
  1241. if (err_text == "") {
  1242. err_text = "Internal Script Error! - opcode #" + itos(last_opcode) + " (report please).";
  1243. }
  1244. if (!GDScriptLanguage::get_singleton()->debug_break(err_text, false)) {
  1245. // debugger break did not happen
  1246. _err_print_error(err_func.utf8().get_data(), err_file.utf8().get_data(), err_line, err_text.utf8().get_data(), ERR_HANDLER_SCRIPT);
  1247. }
  1248. #endif
  1249. OPCODE_OUT;
  1250. }
  1251. OPCODES_OUT
  1252. #ifdef DEBUG_ENABLED
  1253. if (GDScriptLanguage::get_singleton()->profiling) {
  1254. uint64_t time_taken = OS::get_singleton()->get_ticks_usec() - function_start_time;
  1255. profile.total_time += time_taken;
  1256. profile.self_time += time_taken - function_call_time;
  1257. profile.frame_total_time += time_taken;
  1258. profile.frame_self_time += time_taken - function_call_time;
  1259. GDScriptLanguage::get_singleton()->script_frame_time += time_taken - function_call_time;
  1260. }
  1261. if (ScriptDebugger::get_singleton())
  1262. GDScriptLanguage::get_singleton()->exit_function();
  1263. #endif
  1264. if (_stack_size) {
  1265. //free stack
  1266. for (int i = 0; i < _stack_size; i++)
  1267. stack[i].~Variant();
  1268. }
  1269. return retvalue;
  1270. }
  1271. const int *GDScriptFunction::get_code() const {
  1272. return _code_ptr;
  1273. }
  1274. int GDScriptFunction::get_code_size() const {
  1275. return _code_size;
  1276. }
  1277. Variant GDScriptFunction::get_constant(int p_idx) const {
  1278. ERR_FAIL_INDEX_V(p_idx, constants.size(), "<errconst>");
  1279. return constants[p_idx];
  1280. }
  1281. StringName GDScriptFunction::get_global_name(int p_idx) const {
  1282. ERR_FAIL_INDEX_V(p_idx, global_names.size(), "<errgname>");
  1283. return global_names[p_idx];
  1284. }
  1285. int GDScriptFunction::get_default_argument_count() const {
  1286. return _default_arg_count;
  1287. }
  1288. int GDScriptFunction::get_default_argument_addr(int p_idx) const {
  1289. ERR_FAIL_INDEX_V(p_idx, default_arguments.size(), -1);
  1290. return default_arguments[p_idx];
  1291. }
  1292. GDScriptDataType GDScriptFunction::get_return_type() const {
  1293. return return_type;
  1294. }
  1295. GDScriptDataType GDScriptFunction::get_argument_type(int p_idx) const {
  1296. ERR_FAIL_INDEX_V(p_idx, argument_types.size(), GDScriptDataType());
  1297. return argument_types[p_idx];
  1298. }
  1299. StringName GDScriptFunction::get_name() const {
  1300. return name;
  1301. }
  1302. int GDScriptFunction::get_max_stack_size() const {
  1303. return _stack_size;
  1304. }
  1305. struct _GDFKC {
  1306. int order;
  1307. List<int> pos;
  1308. };
  1309. struct _GDFKCS {
  1310. int order;
  1311. StringName id;
  1312. int pos;
  1313. bool operator<(const _GDFKCS &p_r) const {
  1314. return order < p_r.order;
  1315. }
  1316. };
  1317. void GDScriptFunction::debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const {
  1318. int oc = 0;
  1319. Map<StringName, _GDFKC> sdmap;
  1320. for (const List<StackDebug>::Element *E = stack_debug.front(); E; E = E->next()) {
  1321. const StackDebug &sd = E->get();
  1322. if (sd.line > p_line)
  1323. break;
  1324. if (sd.added) {
  1325. if (!sdmap.has(sd.identifier)) {
  1326. _GDFKC d;
  1327. d.order = oc++;
  1328. d.pos.push_back(sd.pos);
  1329. sdmap[sd.identifier] = d;
  1330. } else {
  1331. sdmap[sd.identifier].pos.push_back(sd.pos);
  1332. }
  1333. } else {
  1334. ERR_CONTINUE(!sdmap.has(sd.identifier));
  1335. sdmap[sd.identifier].pos.pop_back();
  1336. if (sdmap[sd.identifier].pos.empty())
  1337. sdmap.erase(sd.identifier);
  1338. }
  1339. }
  1340. List<_GDFKCS> stackpositions;
  1341. for (Map<StringName, _GDFKC>::Element *E = sdmap.front(); E; E = E->next()) {
  1342. _GDFKCS spp;
  1343. spp.id = E->key();
  1344. spp.order = E->get().order;
  1345. spp.pos = E->get().pos.back()->get();
  1346. stackpositions.push_back(spp);
  1347. }
  1348. stackpositions.sort();
  1349. for (List<_GDFKCS>::Element *E = stackpositions.front(); E; E = E->next()) {
  1350. Pair<StringName, int> p;
  1351. p.first = E->get().id;
  1352. p.second = E->get().pos;
  1353. r_stackvars->push_back(p);
  1354. }
  1355. }
  1356. GDScriptFunction::GDScriptFunction() :
  1357. function_list(this) {
  1358. _stack_size = 0;
  1359. _call_size = 0;
  1360. rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
  1361. name = "<anonymous>";
  1362. #ifdef DEBUG_ENABLED
  1363. _func_cname = NULL;
  1364. if (GDScriptLanguage::get_singleton()->lock) {
  1365. GDScriptLanguage::get_singleton()->lock->lock();
  1366. }
  1367. GDScriptLanguage::get_singleton()->function_list.add(&function_list);
  1368. if (GDScriptLanguage::get_singleton()->lock) {
  1369. GDScriptLanguage::get_singleton()->lock->unlock();
  1370. }
  1371. profile.call_count = 0;
  1372. profile.self_time = 0;
  1373. profile.total_time = 0;
  1374. profile.frame_call_count = 0;
  1375. profile.frame_self_time = 0;
  1376. profile.frame_total_time = 0;
  1377. profile.last_frame_call_count = 0;
  1378. profile.last_frame_self_time = 0;
  1379. profile.last_frame_total_time = 0;
  1380. #endif
  1381. }
  1382. GDScriptFunction::~GDScriptFunction() {
  1383. #ifdef DEBUG_ENABLED
  1384. if (GDScriptLanguage::get_singleton()->lock) {
  1385. GDScriptLanguage::get_singleton()->lock->lock();
  1386. }
  1387. GDScriptLanguage::get_singleton()->function_list.remove(&function_list);
  1388. if (GDScriptLanguage::get_singleton()->lock) {
  1389. GDScriptLanguage::get_singleton()->lock->unlock();
  1390. }
  1391. #endif
  1392. }
  1393. /////////////////////
  1394. Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  1395. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1396. #ifdef DEBUG_ENABLED
  1397. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1398. ERR_FAIL_V(Variant());
  1399. #else
  1400. return Variant();
  1401. #endif
  1402. }
  1403. Variant arg;
  1404. r_error.error = Variant::CallError::CALL_OK;
  1405. ERR_FAIL_COND_V(!function, Variant());
  1406. if (p_argcount == 0) {
  1407. r_error.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1408. r_error.argument = 1;
  1409. return Variant();
  1410. } else if (p_argcount == 1) {
  1411. //noooneee
  1412. } else if (p_argcount == 2) {
  1413. arg = *p_args[0];
  1414. } else {
  1415. Array extra_args;
  1416. for (int i = 0; i < p_argcount - 1; i++) {
  1417. extra_args.push_back(*p_args[i]);
  1418. }
  1419. arg = extra_args;
  1420. }
  1421. Ref<GDScriptFunctionState> self = *p_args[p_argcount - 1];
  1422. if (self.is_null()) {
  1423. r_error.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT;
  1424. r_error.argument = p_argcount - 1;
  1425. r_error.expected = Variant::OBJECT;
  1426. return Variant();
  1427. }
  1428. state.result = arg;
  1429. Variant ret = function->call(NULL, NULL, 0, r_error, &state);
  1430. bool completed = true;
  1431. // If the return value is a GDScriptFunctionState reference,
  1432. // then the function did yield again after resuming.
  1433. if (ret.is_ref()) {
  1434. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1435. if (gdfs && gdfs->function == function) {
  1436. completed = false;
  1437. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1438. }
  1439. }
  1440. function = NULL; //cleaned up;
  1441. state.result = Variant();
  1442. if (completed) {
  1443. if (first_state.is_valid()) {
  1444. first_state->emit_signal("completed", ret);
  1445. } else {
  1446. emit_signal("completed", ret);
  1447. }
  1448. }
  1449. return ret;
  1450. }
  1451. bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
  1452. if (function == NULL)
  1453. return false;
  1454. if (p_extended_check) {
  1455. //class instance gone?
  1456. if (state.instance_id && !ObjectDB::get_instance(state.instance_id))
  1457. return false;
  1458. }
  1459. return true;
  1460. }
  1461. Variant GDScriptFunctionState::resume(const Variant &p_arg) {
  1462. ERR_FAIL_COND_V(!function, Variant());
  1463. if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
  1464. #ifdef DEBUG_ENABLED
  1465. ERR_EXPLAIN("Resumed after yield, but class instance is gone");
  1466. ERR_FAIL_V(Variant());
  1467. #else
  1468. return Variant();
  1469. #endif
  1470. }
  1471. state.result = p_arg;
  1472. Variant::CallError err;
  1473. Variant ret = function->call(NULL, NULL, 0, err, &state);
  1474. bool completed = true;
  1475. // If the return value is a GDScriptFunctionState reference,
  1476. // then the function did yield again after resuming.
  1477. if (ret.is_ref()) {
  1478. GDScriptFunctionState *gdfs = Object::cast_to<GDScriptFunctionState>(ret);
  1479. if (gdfs && gdfs->function == function) {
  1480. completed = false;
  1481. gdfs->first_state = first_state.is_valid() ? first_state : Ref<GDScriptFunctionState>(this);
  1482. }
  1483. }
  1484. function = NULL; //cleaned up;
  1485. state.result = Variant();
  1486. if (completed) {
  1487. if (first_state.is_valid()) {
  1488. first_state->emit_signal("completed", ret);
  1489. } else {
  1490. emit_signal("completed", ret);
  1491. }
  1492. }
  1493. return ret;
  1494. }
  1495. void GDScriptFunctionState::_bind_methods() {
  1496. ClassDB::bind_method(D_METHOD("resume", "arg"), &GDScriptFunctionState::resume, DEFVAL(Variant()));
  1497. ClassDB::bind_method(D_METHOD("is_valid", "extended_check"), &GDScriptFunctionState::is_valid, DEFVAL(false));
  1498. ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_signal_callback", &GDScriptFunctionState::_signal_callback, MethodInfo("_signal_callback"));
  1499. ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::NIL, "result", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)));
  1500. }
  1501. GDScriptFunctionState::GDScriptFunctionState() {
  1502. function = NULL;
  1503. }
  1504. GDScriptFunctionState::~GDScriptFunctionState() {
  1505. if (function != NULL) {
  1506. //never called, deinitialize stack
  1507. for (int i = 0; i < state.stack_size; i++) {
  1508. Variant *v = (Variant *)&state.stack[sizeof(Variant) * i];
  1509. v->~Variant();
  1510. }
  1511. }
  1512. }