You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is your feature request related to a problem? Please describe.
Support for calling external functions from shared libraries is very valuable, as it allows for interoperability between the VM and arbitrary native code which is desirable not only for the sake of efficiency but also for functionality.
Describe the solution you'd like
Create a simple API exposing 'primitive' types like Int, Flt, Chr, along with an additional type NativeHandle (which is merely an opaque pointer managed by the native library) which can be accessed from a 'Context/Environment'-type structure by the underlying method, e.g.
/// An FFI function for sin(x)#[no_mangle]extern"C"fnsin(env:&mutCallContext){let x = fr_env_get_positional_flt(env,0);let r = f64::sin(x);fr_env_set_return_flt(env, r);}// Suppose this function has been compiled into libtrig
The existing ExternDecl apparatus can be hijacked to include an additional flag that indicates if an ExternDecl is a native function.
The corresponding assembly syntax could simply use .nf, as in
.cpool 1
f64 1.5707, $PI_2
.extern1# v------ This is the only change ..
.nf libtrig:sin, @libtrig_sin
# .ef libtrig:sin, @libtrig_sin# ...
.code
main:
ldx @libtrig_sin, %0
ldc $PI_2, %1
stdcall %2, %2, %1
print %1ret
The VM should make use of this type of function, by invoking it with a suitable CallContext/CallEnvironment populated with parameters passed from stdcall on an FRef.
Describe alternatives you've considered
The native methods could alternatively be registered through the use of a hook method, which also supplies meta-information about the functions (parameter types, arity, etc.) alongside their pointers. The FFI interface then converts VM types based on this information, and invokes the function. The good thing about this approach is that for libraries which supply more or less the same function types like libmath, a large number of native functions can be implemented in a 'non-intrusive' way.
Additional context
Errors occurring in native context need to be reported to the environment so that they can propagate up the stack.
The text was updated successfully, but these errors were encountered:
Is your feature request related to a problem? Please describe.
Support for calling external functions from shared libraries is very valuable, as it allows for interoperability between the VM and arbitrary native code which is desirable not only for the sake of efficiency but also for functionality.
Describe the solution you'd like
Create a simple API exposing 'primitive' types like
Int
,Flt
,Chr
, along with an additional typeNativeHandle
(which is merely an opaque pointer managed by the native library) which can be accessed from a 'Context/Environment'-type structure by the underlying method, e.g.The existing
ExternDecl
apparatus can be hijacked to include an additional flag that indicates if anExternDecl
is anative
function.The corresponding assembly syntax could simply use
.nf
, as inThe VM should make use of this type of function, by invoking it with a suitable
CallContext
/CallEnvironment
populated with parameters passed fromstdcall
on anFRef
.Describe alternatives you've considered
The native methods could alternatively be registered through the use of a
hook
method, which also supplies meta-information about the functions (parameter types, arity, etc.) alongside their pointers. The FFI interface then converts VM types based on this information, and invokes the function. The good thing about this approach is that for libraries which supply more or less the same function types likelibmath
, a large number of native functions can be implemented in a 'non-intrusive' way.Additional context
Errors occurring in native context need to be reported to the environment so that they can propagate up the stack.
The text was updated successfully, but these errors were encountered: