Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 4acb51f

Browse files
committed
[[ Bug 19772 ]] Foreign handlers get repeatedly bound
This patch ensures that the 'bound' boolean instance var of a foreign handler definition is correctly updated after being successfully bound. Previously, this was not set correctly meaning that every call to a foreign handler would cause it to be rebound.
1 parent 331237d commit 4acb51f

File tree

1 file changed

+28
-49
lines changed

1 file changed

+28
-49
lines changed

libscript/src/script-instance.cpp

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -876,56 +876,38 @@ __MCScriptPrepareForeignFunction(MCScriptInstanceRef p_instance,
876876
MCScriptForeignHandlerDefinition *p_handler,
877877
bool *r_bound)
878878
{
879-
bool t_bound = p_handler->is_bound;
880-
881-
ffi_abi t_abi;
882-
if (!t_bound)
883-
{
884-
bool *t_bound_ptr = nullptr;
879+
// If the handler is already bound, then do nothing.
880+
if (p_handler->is_bound)
881+
{
885882
if (r_bound != nullptr)
886-
t_bound_ptr = &t_bound;
883+
{
884+
*r_bound = true;
885+
}
887886

888-
if (!__MCScriptResolveForeignFunctionBinding(p_instance,
889-
p_handler,
890-
t_abi,
891-
t_bound_ptr))
892-
{
893-
return false;
894-
}
895-
896-
p_handler->is_bound = t_bound;
897-
}
898-
899-
// If we are 'try to bind' and the foreign binding failed, then
887+
return true;
888+
}
889+
890+
// Attempt to resolve the foreign function binding. If r_bound == nullptr,
891+
// then this will fail if binding fails. If r_bound != nullptr, then *r_bound
892+
// will indicate whether the function was bound or not.
893+
ffi_abi t_abi;
894+
if (!__MCScriptResolveForeignFunctionBinding(p_instance,
895+
p_handler,
896+
t_abi,
897+
r_bound))
898+
{
899+
return false;
900+
}
901+
902+
// If we are 'try to bind' and the foreign binding failed, then
900903
// return the bound status to the caller and return.
901904
if (r_bound != nullptr &&
902-
!t_bound)
905+
!*r_bound)
903906
{
904-
*r_bound = false;
905-
return true;
906-
}
907-
908-
// If the function didn't produce a valid pointer either throw, or return
909-
// unbound depending on the r_bound out ptr.
910-
bool t_resolved;
911-
if (p_handler -> is_java)
912-
t_resolved = p_handler -> java . method_id != nullptr;
913-
else
914-
t_resolved = p_handler -> native . function != nullptr;
915-
916-
if (!t_resolved)
917-
{
918-
if (r_bound == nullptr)
919-
{
920-
return MCScriptThrowUnableToResolveForeignHandlerError(p_instance,
921-
p_handler);
922-
}
923-
924-
*r_bound = false;
925-
926907
return true;
927908
}
928-
909+
910+
// If we get here then it means that the binding succeeded.
929911
MCTypeInfoRef t_signature;
930912
t_signature = p_instance->module->types[p_handler->type]->typeinfo;
931913

@@ -940,12 +922,9 @@ __MCScriptPrepareForeignFunction(MCScriptInstanceRef p_instance,
940922
return false;
941923
}
942924

943-
// If we are a non-trapping bind, then indicate that we bound successfully.
944-
if (r_bound != nullptr)
945-
{
946-
*r_bound = true;
947-
}
948-
925+
// The function can only be considered bound, if it gets to this point.
926+
p_handler->is_bound = true;
927+
949928
return true;
950929
}
951930

0 commit comments

Comments
 (0)