Skip to content

Commit d0175bf

Browse files
committed
[[ FFI ]] Tidy up libscript FFI support
This patch tidies up the current implementation of FFI in libscript. The type of foreign handler is now determined by the 'language' field. This is set after successful parsing of the binding string and can be one of 'C', 'BuiltinC' or 'Java'. Parsing of the binding string has been split up into per-language functions. The language prefix of the string is first parsed out, and that is used to determine which further function to call. The C++ and ObjC related errors and bindings have been removed as they are both to be implemented via C bindings and therefore the distinction is unnecessary.
1 parent c97e686 commit d0175bf

5 files changed

Lines changed: 315 additions & 270 deletions

File tree

libscript/src/script-error.cpp

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -316,29 +316,29 @@ MCScriptThrowUnableToResolveForeignHandlerError(MCScriptInstanceRef p_instance,
316316
}
317317

318318
bool
319-
MCScriptThrowUnknownForeignCallingConventionError(void)
319+
MCScriptThrowUnknownForeignLanguageError(void)
320320
{
321321
return MCErrorCreateAndThrow(kMCGenericErrorTypeInfo,
322322
"reason",
323-
MCSTR("unknown calling convention"),
323+
MCSTR("unknown language"),
324324
nil);
325325
}
326326

327327
bool
328-
MCScriptThrowMissingFunctionInForeignBindingError(void)
328+
MCScriptThrowUnknownForeignCallingConventionError(void)
329329
{
330330
return MCErrorCreateAndThrow(kMCGenericErrorTypeInfo,
331331
"reason",
332-
MCSTR("no function specified in binding string"),
332+
MCSTR("unknown calling convention"),
333333
nil);
334334
}
335335

336336
bool
337-
MCScriptThrowClassNotAllowedInCBindingError(void)
337+
MCScriptThrowMissingFunctionInForeignBindingError(void)
338338
{
339339
return MCErrorCreateAndThrow(kMCGenericErrorTypeInfo,
340340
"reason",
341-
MCSTR("class not allowed in c binding string"),
341+
MCSTR("no function specified in binding string"),
342342
nil);
343343
}
344344

@@ -350,24 +350,6 @@ MCScriptThrowUnableToLoadForiegnLibraryError(void)
350350
MCSTR("unable to load foreign library"),
351351
nil);
352352
}
353-
354-
bool
355-
MCScriptThrowCXXBindingNotImplemented(void)
356-
{
357-
return MCErrorCreateAndThrow(kMCGenericErrorTypeInfo,
358-
"reason",
359-
MCSTR("c++ binding not implemented yet"),
360-
nil);
361-
}
362-
363-
bool
364-
MCScriptThrowObjCBindingNotImplemented(void)
365-
{
366-
return MCErrorCreateAndThrow(kMCGenericErrorTypeInfo,
367-
"reason",
368-
MCSTR("objc binding not implemented yet"),
369-
nil);
370-
}
371353

372354
bool
373355
MCScriptThrowJavaBindingNotImplemented(void)
@@ -387,15 +369,6 @@ MCScriptThrowJavaBindingNotSupported(void)
387369
nil);
388370
}
389371

390-
bool
391-
MCScriptThrowObjCBindingNotSupported(void)
392-
{
393-
return MCErrorCreateAndThrow(kMCGenericErrorTypeInfo,
394-
"reason",
395-
MCSTR("objc binding not supported on this platform"),
396-
nil);
397-
}
398-
399372
bool
400373
MCScriptCreateErrorExpectedError(MCErrorRef& r_error)
401374
{

libscript/src/script-execute.cpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,17 @@ class MCScriptForeignInvocation
167167
MCTypeInfoRef p_handler_signature,
168168
void *p_result_slot_ptr)
169169
{
170-
if (p_handler->is_java)
171-
{
170+
switch(p_handler->language)
171+
{
172+
case kMCScriptForeignHandlerLanguageJava:
172173
#ifdef TARGET_SUBPLATFORM_ANDROID
173174
extern bool MCAndroidIsOnSystemThread();
174175
if (!p_handler->is_ui_bound || MCAndroidIsOnSystemThread())
175176
#endif
176177
{
177-
MCJavaCallJNIMethod(p_handler -> java . class_name,
178-
p_handler -> java . method_id,
179-
p_handler -> java . call_type,
178+
MCJavaCallJNIMethod(p_handler->java.class_name,
179+
p_handler->java.method_id,
180+
p_handler->java.call_type,
180181
p_handler_signature,
181182
p_result_slot_ptr,
182183
m_argument_values,
@@ -198,18 +199,23 @@ class MCScriptForeignInvocation
198199
co_yield_to_android_and_call(remote_call_func, &t_context);
199200
}
200201
#endif
201-
}
202-
else if (p_handler->is_builtin)
203-
{
204-
((void(*)(void*, void**))p_handler->native.function)(p_result_slot_ptr,
205-
m_argument_values);
206-
}
207-
else
208-
{
209-
ffi_call((ffi_cif *)p_handler -> native . function_cif,
210-
(void(*)())p_handler -> native . function,
202+
break;
203+
204+
case kMCScriptForeignHandlerLanguageC:
205+
ffi_call((ffi_cif *)p_handler ->c.function_cif,
206+
(void(*)())p_handler ->c.function,
211207
p_result_slot_ptr,
212208
m_argument_values);
209+
break;
210+
211+
case kMCScriptForeignHandlerLanguageBuiltinC:
212+
((void(*)(void*, void**))p_handler->builtin_c.function)(p_result_slot_ptr,
213+
m_argument_values);
214+
break;
215+
216+
case kMCScriptForeignHandlerLanguageUnknown:
217+
MCUnreachableReturn(false);
218+
break;
213219
}
214220

215221
return true;
@@ -294,7 +300,7 @@ MCScriptExecuteContext::InvokeForeign(MCScriptInstanceRef p_instance,
294300
return;
295301
}
296302

297-
if (!p_handler_def->is_bound)
303+
if (p_handler_def->language == kMCScriptForeignHandlerLanguageUnknown)
298304
{
299305
if (!MCScriptBindForeignHandlerInInstanceInternal(p_instance,
300306
p_handler_def))
@@ -304,6 +310,10 @@ MCScriptExecuteContext::InvokeForeign(MCScriptInstanceRef p_instance,
304310
}
305311
}
306312

313+
/* If we get here then then handler *must* have been bound successfully, and
314+
* so have a non 'unknown' language. */
315+
MCAssert(p_handler_def->language != kMCScriptForeignHandlerLanguageUnknown);
316+
307317
// Fetch the handler signature.
308318
MCTypeInfoRef t_signature =
309319
GetSignatureOfHandler(p_instance,

0 commit comments

Comments
 (0)