Skip to content

Commit

Permalink
CraterCrashGH-121 Dispatch only script user-defined functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Naros committed Mar 4, 2024
1 parent 19026d5 commit d771699
Showing 1 changed file with 40 additions and 50 deletions.
90 changes: 40 additions & 50 deletions src/script/instances/script_instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,63 +719,53 @@ void OScriptInstance::call(const StringName& p_method, const Variant* const* p_a

// We first check whether this function is defined as part of the Orchestration
// If it is, we invoke that variant of the method.
HashMap<StringName, Function>::Iterator F = _functions.find(p_method);
if (F)
const HashMap<StringName, Function>::Iterator F = _functions.find(p_method);
if (!F)
{
Function* f = &F->value;
if (!f->instance)
{
// Lookup node that offers the function call
HashMap<int, OScriptNodeInstance*>::Iterator E = _nodes.find(f->node);
if (!E)
{
r_err->error = GDEXTENSION_CALL_ERROR_INVALID_METHOD;
ERR_FAIL_MSG("Unable to locate node for method '" + p_method + "' with id " + itos(f->node));
}
f->instance = E->value;
}
// Method invalid
r_err->error = GDEXTENSION_CALL_ERROR_INVALID_METHOD;
*r_return = Variant();
return;
}

if (f->max_stack > _max_call_stack)
Function* f = &F->value;
if (!f->instance)
{
// Lookup node that offers the function call
HashMap<int, OScriptNodeInstance*>::Iterator E = _nodes.find(f->node);
if (!E)
{
ERR_FAIL_MSG("Unable to call function, call stack exceeds " + itos(_max_call_stack));
r_err->error = GDEXTENSION_CALL_ERROR_INVALID_METHOD;
ERR_FAIL_MSG("Unable to locate node for method '" + p_method + "' with id " + itos(f->node));
}

// Setup the OScriptExecutionStackInfo object
OScriptExecutionStackInfo si;
si.max_stack_size = f->max_stack; //! Max Call Stack
si.node_count = f->node_count; //! Number of nodes
si.max_inputs = _max_input_args; //! max input arguments
si.max_outputs = _max_output_args; //! max output arguments
si.flow_size = f->flow_stack_size; //! flow size
si.pass_size = f->pass_stack_size; //! pass stack size

const int stack_size = si.get_stack_size();
void* fstack = alloca(stack_size);
memset(fstack, 0, stack_size);

// Setup the stack
OScriptExecutionStack stack(si, fstack, true, false);
stack.push_node_onto_flow_stack(f->node);
stack.push_arguments(p_args, static_cast<int>(p_arg_count));

// Dispatch the call to the internal handler
_call_internal(p_method, &stack, 0, 0, false, f->instance, f, *r_return, *r_err);
f->instance = E->value;
}
else if (_script->has_method(p_method))

if (f->max_stack > _max_call_stack)
{
// Calling a native method
Array args;
for (int i = 0; i < p_arg_count; i++)
args.push_back(*p_args[i]);
r_err->error = GDEXTENSION_CALL_OK;
*r_return = _script->callv(p_method, args);
}
else
{
// Method invalid
r_err->error = GDEXTENSION_CALL_ERROR_INVALID_METHOD;
*r_return = Variant();
ERR_FAIL_MSG("Unable to call function, call stack exceeds " + itos(_max_call_stack));
}

// Setup the OScriptExecutionStackInfo object
OScriptExecutionStackInfo si;
si.max_stack_size = f->max_stack; //! Max Call Stack
si.node_count = f->node_count; //! Number of nodes
si.max_inputs = _max_input_args; //! max input arguments
si.max_outputs = _max_output_args; //! max output arguments
si.flow_size = f->flow_stack_size; //! flow size
si.pass_size = f->pass_stack_size; //! pass stack size

const int stack_size = si.get_stack_size();
void* fstack = alloca(stack_size);
memset(fstack, 0, stack_size);

// Setup the stack
OScriptExecutionStack stack(si, fstack, true, false);
stack.push_node_onto_flow_stack(f->node);
stack.push_arguments(p_args, static_cast<int>(p_arg_count));

// Dispatch the call to the internal handler
_call_internal(p_method, &stack, 0, 0, false, f->instance, f, *r_return, *r_err);
}

void OScriptInstance::_call_internal(const StringName& p_method, OScriptExecutionStack* p_stack, int p_flow_pos,
Expand Down

0 comments on commit d771699

Please sign in to comment.