Skip to content

Commit 92ef50a

Browse files
committed
[[ ForeignCallbacks ]] Minor cleanup.
1 parent 2682c6a commit 92ef50a

3 files changed

Lines changed: 30 additions & 29 deletions

File tree

libfoundation/src/foundation-handler.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,27 @@ static void __exec_closure(ffi_cif *cif, void *ret, void **args, void *user_data
106106
MCTypeInfoRef t_signature;
107107
t_signature = t_handler -> typeinfo;
108108

109+
// Check the arity of the handler - at the moment we can only handle 16
110+
// arguments.
109111
uindex_t t_arity;
110112
t_arity = MCHandlerTypeInfoGetParameterCount(t_signature);
113+
if (t_arity > 16)
114+
{
115+
MCErrorThrowUnimplemented(MCSTR("closures only supported for handlers with at most 16 parameters"));
116+
return;
117+
}
118+
119+
120+
// Check that we can resolve the return type.
121+
MCTypeInfoRef t_return_type;
122+
t_return_type = MCHandlerTypeInfoGetReturnType(t_signature);
123+
124+
MCResolvedTypeInfo t_resolved_return_type;
125+
if (!MCTypeInfoResolve(t_return_type, t_resolved_return_type))
126+
{
127+
MCErrorThrowUnboundType(t_return_type);
128+
return;
129+
}
111130

112131
MCValueRef t_value_result;
113132
MCValueRef t_value_args[16];
@@ -123,12 +142,15 @@ static void __exec_closure(ffi_cif *cif, void *ret, void **args, void *user_data
123142
t_type = MCHandlerTypeInfoGetParameterType(t_signature, t_arg_index);
124143

125144
if (t_mode != kMCHandlerTypeFieldModeIn)
126-
abort();
145+
{
146+
MCErrorThrowUnimplemented(MCSTR("closures only support in arguments"));
147+
goto error_exit;
148+
}
127149

128150
MCResolvedTypeInfo t_resolved_type;
129151
if (!MCTypeInfoResolve(t_type, t_resolved_type))
130152
{
131-
MCErrorThrowGeneric(nil);
153+
MCErrorThrowUnboundType(t_type);
132154
goto error_exit;
133155
}
134156

@@ -162,17 +184,6 @@ static void __exec_closure(ffi_cif *cif, void *ret, void **args, void *user_data
162184
if (!MCHandlerInvoke(t_handler, t_value_args, t_arity, t_value_result))
163185
goto error_exit;
164186

165-
MCTypeInfoRef t_return_type;
166-
t_return_type = MCHandlerTypeInfoGetReturnType(t_signature);
167-
168-
MCResolvedTypeInfo t_resolved_return_type;
169-
t_return_type = MCHandlerTypeInfoGetReturnType(t_signature);
170-
if (!MCTypeInfoResolve(t_return_type, t_resolved_return_type))
171-
{
172-
MCErrorThrowGeneric(nil);
173-
goto error_exit;
174-
}
175-
176187
if (t_resolved_return_type . named_type != kMCNullTypeInfo)
177188
{
178189
if (MCTypeInfoIsForeign(t_resolved_return_type . type))
@@ -189,8 +200,6 @@ static void __exec_closure(ffi_cif *cif, void *ret, void **args, void *user_data
189200
}
190201
}
191202

192-
return;
193-
194203
error_exit:
195204
if (t_value_result != nil)
196205
MCValueRelease(t_value_result);
@@ -218,7 +227,7 @@ bool MCHandlerGetFunctionPtr(MCHandlerRef self, void*& r_function_ptr)
218227
{
219228
ffi_closure_free(self -> closure);
220229
self -> closure = nil;
221-
return MCErrorThrowGeneric(nil);
230+
return MCErrorThrowGeneric(MCSTR("unexpected libffi failure"));
222231
}
223232

224233
r_function_ptr = self -> function_ptr;

libfoundation/src/foundation-typeinfo.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ bool MCHandlerTypeInfoGetLayoutType(MCTypeInfoRef unresolved_self, int p_abi, vo
838838

839839
MCResolvedTypeInfo t_resolved_return_type;
840840
if (!MCTypeInfoResolve(t_return_type, t_resolved_return_type))
841-
return MCErrorThrowGeneric(nil);
841+
return MCErrorThrowUnboundType(t_return_type);
842842

843843
ffi_type *t_ffi_return_type;
844844
if (t_return_type != kMCNullTypeInfo)
@@ -860,9 +860,7 @@ bool MCHandlerTypeInfoGetLayoutType(MCTypeInfoRef unresolved_self, int p_abi, vo
860860

861861
t_ffi_arg_types[0] = t_ffi_return_type;
862862

863-
bool t_success;
864-
t_success = true;
865-
for(uindex_t i = 0; t_success && i < t_arity; i++)
863+
for(uindex_t i = 0; i < t_arity; i++)
866864
{
867865
MCTypeInfoRef t_type;
868866
MCHandlerTypeFieldMode t_mode;
@@ -871,10 +869,7 @@ bool MCHandlerTypeInfoGetLayoutType(MCTypeInfoRef unresolved_self, int p_abi, vo
871869

872870
MCResolvedTypeInfo t_resolved_type;
873871
if (!MCTypeInfoResolve(t_type, t_resolved_type))
874-
{
875-
t_success = false;
876-
break;
877-
}
872+
return MCErrorThrowUnboundType(t_type);
878873

879874
if (t_mode == kMCHandlerTypeFieldModeIn)
880875
{
@@ -886,9 +881,6 @@ bool MCHandlerTypeInfoGetLayoutType(MCTypeInfoRef unresolved_self, int p_abi, vo
886881
else
887882
t_ffi_arg_types[i + 1] = &ffi_type_pointer;
888883
}
889-
890-
if (!t_success)
891-
return MCErrorThrowGeneric(nil);
892884

893885
self -> handler . layout_args = (void **)t_ffi_arg_types;
894886
}
@@ -901,7 +893,7 @@ bool MCHandlerTypeInfoGetLayoutType(MCTypeInfoRef unresolved_self, int p_abi, vo
901893
if (ffi_prep_cif((ffi_cif *)&t_layout -> cif, (ffi_abi)p_abi, self -> handler . field_count, (ffi_type *)self -> handler . layout_args[0], (ffi_type **)(self -> handler . layout_args + 1)) != FFI_OK)
902894
{
903895
MCMemoryDeallocate(t_layout);
904-
return MCErrorThrowGeneric(nil);
896+
return MCErrorThrowGeneric(MCSTR("unexpected libffi failure"));
905897
}
906898

907899
t_layout -> next = self -> handler . layouts;

libscript/src/script-instance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ bool MCScriptThrowUnableToResolveForeignHandlerError(MCScriptModuleRef p_module,
249249

250250
bool MCScriptThrowUnableToResolveTypeError(MCTypeInfoRef p_type)
251251
{
252-
return MCErrorThrowUnboundTypeError(p_type);
252+
return MCErrorThrowUnboundType(p_type);
253253
}
254254

255255
bool MCScriptThrowUnableToResolveMultiInvoke(MCScriptModuleRef p_module, MCScriptDefinition *p_definition, MCProperListRef p_arguments)

0 commit comments

Comments
 (0)