2017-03-05 22:47:28 +08:00
/*************************************************************************/
2017-11-17 01:38:18 +08:00
/* gdscript_function.cpp */
2017-03-05 22:47:28 +08:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 20:16:55 +08:00
/* https://godotengine.org */
2017-03-05 22:47:28 +08:00
/*************************************************************************/
2022-01-04 04:27:34 +08:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2017-03-05 22:47:28 +08:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2018-01-05 07:50:27 +08:00
2017-11-17 01:38:18 +08:00
# include "gdscript_function.h"
2017-03-05 22:47:28 +08:00
2017-11-17 01:38:18 +08:00
# include "gdscript.h"
2016-06-01 09:28:27 +08:00
2017-11-17 01:38:18 +08:00
const int * GDScriptFunction : : get_code ( ) const {
2016-06-01 09:28:27 +08:00
return _code_ptr ;
}
2020-05-14 20:29:06 +08:00
2017-11-17 01:38:18 +08:00
int GDScriptFunction : : get_code_size ( ) const {
2016-06-01 09:28:27 +08:00
return _code_size ;
}
2017-11-17 01:38:18 +08:00
Variant GDScriptFunction : : get_constant ( int p_idx ) const {
2016-06-01 09:28:27 +08:00
ERR_FAIL_INDEX_V ( p_idx , constants . size ( ) , " <errconst> " ) ;
return constants [ p_idx ] ;
}
2017-11-17 01:38:18 +08:00
StringName GDScriptFunction : : get_global_name ( int p_idx ) const {
2016-06-01 09:28:27 +08:00
ERR_FAIL_INDEX_V ( p_idx , global_names . size ( ) , " <errgname> " ) ;
return global_names [ p_idx ] ;
}
2017-11-17 01:38:18 +08:00
int GDScriptFunction : : get_default_argument_count ( ) const {
2018-07-25 22:41:27 +08:00
return _default_arg_count ;
2016-06-01 09:28:27 +08:00
}
2020-05-14 20:29:06 +08:00
2017-11-17 01:38:18 +08:00
int GDScriptFunction : : get_default_argument_addr ( int p_idx ) const {
2017-08-13 00:52:50 +08:00
ERR_FAIL_INDEX_V ( p_idx , default_arguments . size ( ) , - 1 ) ;
return default_arguments [ p_idx ] ;
2016-06-01 09:28:27 +08:00
}
2018-05-30 10:16:54 +08:00
GDScriptDataType GDScriptFunction : : get_return_type ( ) const {
return return_type ;
}
GDScriptDataType GDScriptFunction : : get_argument_type ( int p_idx ) const {
ERR_FAIL_INDEX_V ( p_idx , argument_types . size ( ) , GDScriptDataType ( ) ) ;
return argument_types [ p_idx ] ;
}
2017-11-17 01:38:18 +08:00
StringName GDScriptFunction : : get_name ( ) const {
2016-06-01 09:28:27 +08:00
return name ;
}
2017-11-17 01:38:18 +08:00
int GDScriptFunction : : get_max_stack_size ( ) const {
2016-06-01 09:28:27 +08:00
return _stack_size ;
}
struct _GDFKC {
2021-02-08 17:57:18 +08:00
int order = 0 ;
2016-06-01 09:28:27 +08:00
List < int > pos ;
} ;
struct _GDFKCS {
2021-02-08 17:57:18 +08:00
int order = 0 ;
2016-06-01 09:28:27 +08:00
StringName id ;
2021-02-08 17:57:18 +08:00
int pos = 0 ;
2016-06-01 09:28:27 +08:00
bool operator < ( const _GDFKCS & p_r ) const {
return order < p_r . order ;
}
} ;
2020-03-17 14:33:00 +08:00
void GDScriptFunction : : debug_get_stack_member_state ( int p_line , List < Pair < StringName , int > > * r_stackvars ) const {
2017-03-05 23:44:50 +08:00
int oc = 0 ;
2022-05-13 21:04:37 +08:00
HashMap < StringName , _GDFKC > sdmap ;
2021-07-16 11:45:57 +08:00
for ( const StackDebug & sd : stack_debug ) {
2022-02-17 02:12:38 +08:00
if ( sd . line > = p_line ) {
2016-06-01 09:28:27 +08:00
break ;
2020-05-14 22:41:43 +08:00
}
2016-06-01 09:28:27 +08:00
if ( sd . added ) {
if ( ! sdmap . has ( sd . identifier ) ) {
_GDFKC d ;
d . order = oc + + ;
d . pos . push_back ( sd . pos ) ;
sdmap [ sd . identifier ] = d ;
} else {
sdmap [ sd . identifier ] . pos . push_back ( sd . pos ) ;
}
} else {
ERR_CONTINUE ( ! sdmap . has ( sd . identifier ) ) ;
sdmap [ sd . identifier ] . pos . pop_back ( ) ;
2020-12-15 20:04:21 +08:00
if ( sdmap [ sd . identifier ] . pos . is_empty ( ) ) {
2016-06-01 09:28:27 +08:00
sdmap . erase ( sd . identifier ) ;
2020-05-14 22:41:43 +08:00
}
2016-06-01 09:28:27 +08:00
}
}
List < _GDFKCS > stackpositions ;
2021-08-10 04:13:42 +08:00
for ( const KeyValue < StringName , _GDFKC > & E : sdmap ) {
2016-06-01 09:28:27 +08:00
_GDFKCS spp ;
2021-08-10 04:13:42 +08:00
spp . id = E . key ;
spp . order = E . value . order ;
spp . pos = E . value . pos . back ( ) - > get ( ) ;
2016-06-01 09:28:27 +08:00
stackpositions . push_back ( spp ) ;
}
stackpositions . sort ( ) ;
2021-07-16 11:45:57 +08:00
for ( _GDFKCS & E : stackpositions ) {
2016-06-01 09:28:27 +08:00
Pair < StringName , int > p ;
2021-07-16 11:45:57 +08:00
p . first = E . id ;
p . second = E . pos ;
2016-06-01 09:28:27 +08:00
r_stackvars - > push_back ( p ) ;
}
}
2020-11-13 21:31:14 +08:00
GDScriptFunction : : GDScriptFunction ( ) {
2016-06-01 09:28:27 +08:00
name = " <anonymous> " ;
# ifdef DEBUG_ENABLED
2020-02-26 18:28:13 +08:00
{
MutexLock lock ( GDScriptLanguage : : get_singleton ( ) - > lock ) ;
GDScriptLanguage : : get_singleton ( ) - > function_list . add ( & function_list ) ;
2016-06-01 09:28:27 +08:00
}
# endif
}
2017-11-17 01:38:18 +08:00
GDScriptFunction : : ~ GDScriptFunction ( ) {
2021-03-28 22:03:13 +08:00
for ( int i = 0 ; i < lambdas . size ( ) ; i + + ) {
memdelete ( lambdas [ i ] ) ;
}
2016-06-01 09:28:27 +08:00
# ifdef DEBUG_ENABLED
2020-02-26 18:28:13 +08:00
MutexLock lock ( GDScriptLanguage : : get_singleton ( ) - > lock ) ;
GDScriptLanguage : : get_singleton ( ) - > function_list . remove ( & function_list ) ;
2016-06-01 09:28:27 +08:00
# endif
}
/////////////////////
2020-02-20 03:27:19 +08:00
Variant GDScriptFunctionState : : _signal_callback ( const Variant * * p_args , int p_argcount , Callable : : CallError & r_error ) {
2016-06-01 09:28:27 +08:00
Variant arg ;
2020-02-20 03:27:19 +08:00
r_error . error = Callable : : CallError : : CALL_OK ;
2016-06-01 09:28:27 +08:00
if ( p_argcount = = 0 ) {
2020-02-20 03:27:19 +08:00
r_error . error = Callable : : CallError : : CALL_ERROR_TOO_FEW_ARGUMENTS ;
2016-06-01 09:28:27 +08:00
r_error . argument = 1 ;
return Variant ( ) ;
} else if ( p_argcount = = 1 ) {
//noooneee
} else if ( p_argcount = = 2 ) {
arg = * p_args [ 0 ] ;
} else {
Array extra_args ;
for ( int i = 0 ; i < p_argcount - 1 ; i + + ) {
extra_args . push_back ( * p_args [ i ] ) ;
}
arg = extra_args ;
}
2017-11-17 01:38:18 +08:00
Ref < GDScriptFunctionState > self = * p_args [ p_argcount - 1 ] ;
2016-06-01 09:28:27 +08:00
if ( self . is_null ( ) ) {
2020-02-20 03:27:19 +08:00
r_error . error = Callable : : CallError : : CALL_ERROR_INVALID_ARGUMENT ;
2016-06-01 09:28:27 +08:00
r_error . argument = p_argcount - 1 ;
r_error . expected = Variant : : OBJECT ;
return Variant ( ) ;
}
2019-07-18 04:47:00 +08:00
return resume ( arg ) ;
2016-06-01 09:28:27 +08:00
}
2017-11-17 01:38:18 +08:00
bool GDScriptFunctionState : : is_valid ( bool p_extended_check ) const {
2020-05-14 22:41:43 +08:00
if ( function = = nullptr ) {
2017-05-17 20:47:17 +08:00
return false ;
2020-05-14 22:41:43 +08:00
}
2017-05-17 20:47:17 +08:00
if ( p_extended_check ) {
2020-05-05 18:53:05 +08:00
MutexLock lock ( GDScriptLanguage : : get_singleton ( ) - > lock ) ;
// Script gone?
if ( ! scripts_list . in_list ( ) ) {
2017-05-17 20:47:17 +08:00
return false ;
2020-04-29 19:38:00 +08:00
}
2020-05-05 18:53:05 +08:00
// Class instance gone? (if not static function)
if ( state . instance & & ! instances_list . in_list ( ) ) {
2020-04-29 19:38:00 +08:00
return false ;
}
2017-05-17 20:47:17 +08:00
}
2016-06-01 09:28:27 +08:00
2017-05-17 20:47:17 +08:00
return true ;
2016-06-01 09:28:27 +08:00
}
2017-11-17 01:38:18 +08:00
Variant GDScriptFunctionState : : resume ( const Variant & p_arg ) {
2016-06-01 09:28:27 +08:00
ERR_FAIL_COND_V ( ! function , Variant ( ) ) ;
2020-05-05 18:53:05 +08:00
{
MutexLock lock ( GDScriptLanguage : : singleton - > lock ) ;
if ( ! scripts_list . in_list ( ) ) {
2018-11-25 19:38:12 +08:00
# ifdef DEBUG_ENABLED
2020-05-02 06:14:56 +08:00
ERR_FAIL_V_MSG ( Variant ( ) , " Resumed function ' " + state . function_name + " ()' after await, but script is gone. At script: " + state . script_path + " : " + itos ( state . line ) ) ;
2020-04-29 19:38:00 +08:00
# else
2020-05-05 18:53:05 +08:00
return Variant ( ) ;
2020-04-29 19:38:00 +08:00
# endif
2020-05-05 18:53:05 +08:00
}
if ( state . instance & & ! instances_list . in_list ( ) ) {
2020-04-29 19:38:00 +08:00
# ifdef DEBUG_ENABLED
2020-05-02 06:14:56 +08:00
ERR_FAIL_V_MSG ( Variant ( ) , " Resumed function ' " + state . function_name + " ()' after await, but class instance is gone. At script: " + state . script_path + " : " + itos ( state . line ) ) ;
2018-11-25 19:38:12 +08:00
# else
2020-05-05 18:53:05 +08:00
return Variant ( ) ;
2016-06-30 08:06:16 +08:00
# endif
2020-05-05 18:53:05 +08:00
}
// Do these now to avoid locking again after the call
scripts_list . remove_from_list ( ) ;
instances_list . remove_from_list ( ) ;
2018-11-25 19:38:12 +08:00
}
2016-06-01 09:28:27 +08:00
state . result = p_arg ;
2020-02-20 03:27:19 +08:00
Callable : : CallError err ;
2020-04-02 07:20:12 +08:00
Variant ret = function - > call ( nullptr , nullptr , 0 , err , & state ) ;
2017-06-23 08:29:23 +08:00
bool completed = true ;
2017-11-17 01:38:18 +08:00
// If the return value is a GDScriptFunctionState reference,
2021-03-12 21:35:16 +08:00
// then the function did await again after resuming.
2021-08-27 03:37:17 +08:00
if ( ret . is_ref_counted ( ) ) {
2017-11-17 01:38:18 +08:00
GDScriptFunctionState * gdfs = Object : : cast_to < GDScriptFunctionState > ( ret ) ;
2018-03-14 23:42:13 +08:00
if ( gdfs & & gdfs - > function = = function ) {
2017-06-23 08:29:23 +08:00
completed = false ;
2018-06-28 23:40:11 +08:00
gdfs - > first_state = first_state . is_valid ( ) ? first_state : Ref < GDScriptFunctionState > ( this ) ;
2018-03-14 23:42:13 +08:00
}
2017-06-23 08:29:23 +08:00
}
2020-04-02 07:20:12 +08:00
function = nullptr ; //cleaned up;
2016-06-01 09:28:27 +08:00
state . result = Variant ( ) ;
2017-06-23 08:29:23 +08:00
if ( completed ) {
2018-06-28 23:40:11 +08:00
if ( first_state . is_valid ( ) ) {
2021-07-18 05:22:52 +08:00
first_state - > emit_signal ( SNAME ( " completed " ) , ret ) ;
2018-06-28 23:40:11 +08:00
} else {
2021-07-18 05:22:52 +08:00
emit_signal ( SNAME ( " completed " ) , ret ) ;
2018-03-14 23:42:13 +08:00
}
2019-05-11 03:38:48 +08:00
# ifdef DEBUG_ENABLED
2020-05-14 22:41:43 +08:00
if ( EngineDebugger : : is_active ( ) ) {
2019-05-11 03:38:48 +08:00
GDScriptLanguage : : get_singleton ( ) - > exit_function ( ) ;
2020-05-14 22:41:43 +08:00
}
2022-05-27 20:13:25 +08:00
_clear_stack ( ) ;
2019-05-11 03:38:48 +08:00
# endif
2017-06-23 08:29:23 +08:00
}
2016-06-01 09:28:27 +08:00
return ret ;
}
2020-05-05 18:53:05 +08:00
void GDScriptFunctionState : : _clear_stack ( ) {
if ( state . stack_size ) {
Variant * stack = ( Variant * ) state . stack . ptr ( ) ;
2022-05-14 01:03:48 +08:00
// The first 3 are special addresses and not copied to the state, so we skip them here.
for ( int i = 3 ; i < state . stack_size ; i + + ) {
2020-05-05 18:53:05 +08:00
stack [ i ] . ~ Variant ( ) ;
2020-05-14 22:41:43 +08:00
}
2020-05-05 18:53:05 +08:00
state . stack_size = 0 ;
}
}
2017-11-17 01:38:18 +08:00
void GDScriptFunctionState : : _bind_methods ( ) {
ClassDB : : bind_method ( D_METHOD ( " resume " , " arg " ) , & GDScriptFunctionState : : resume , DEFVAL ( Variant ( ) ) ) ;
ClassDB : : bind_method ( D_METHOD ( " is_valid " , " extended_check " ) , & GDScriptFunctionState : : is_valid , DEFVAL ( false ) ) ;
ClassDB : : bind_vararg_method ( METHOD_FLAGS_DEFAULT , " _signal_callback " , & GDScriptFunctionState : : _signal_callback , MethodInfo ( " _signal_callback " ) ) ;
2017-06-23 08:29:23 +08:00
2017-10-05 19:44:00 +08:00
ADD_SIGNAL ( MethodInfo ( " completed " , PropertyInfo ( Variant : : NIL , " result " , PROPERTY_HINT_NONE , " " , PROPERTY_USAGE_NIL_IS_VARIANT ) ) ) ;
2016-06-01 09:28:27 +08:00
}
2020-05-05 18:53:05 +08:00
GDScriptFunctionState : : GDScriptFunctionState ( ) :
scripts_list ( this ) ,
instances_list ( this ) {
2016-06-01 09:28:27 +08:00
}
2017-11-17 01:38:18 +08:00
GDScriptFunctionState : : ~ GDScriptFunctionState ( ) {
2020-05-05 18:53:05 +08:00
{
MutexLock lock ( GDScriptLanguage : : singleton - > lock ) ;
scripts_list . remove_from_list ( ) ;
instances_list . remove_from_list ( ) ;
2016-06-01 09:28:27 +08:00
}
}