##- # Author: Brian Tiffin # Dedicated to the public domain # # Date: Decemeber 2016 # Modified: 2016-12-30/01:56-0500 ##+ # # testffi.icn, demonstrate an experimental C FFI # $include "natives.inc" procedure main() # will be RTLD_LAZY | RTLD_GLOBAL (so add to the search path) addLibrary := loadfunc("./uninative.so", "addLibrary") # allow arbitrary C functions, marshalled by a piece of assembler native := loadfunc("./uninative.so", "native") # add the testing functions to the dlsym search path, # the handle is somewhat irrelevant, but won't be soonish dlHandle := addLibrary("./libtestnative.so") write("Unicon dlHandle: ", dlHandle) write() # pass two integers, get the sum as int ans := native("testnative", TYPEINT, 40, 2) write("Unicon: called testnative and got ", ans) if ans ~= 42 then write("ERROR with testnative") write() # pass two reals, get the sum as real ans := native("testdouble", TYPEDOUBLE, 9.0, 8.0) write("Unicon: called testdouble and got ", ans) if ans ~= 17.0 then write("ERROR with testdouble") write() # third arg is an integer, returns real ans := native("testfive", TYPEDOUBLE, 1.0, 2.0, 3, 4.0, 5.0) write("Unicon: called testfive and got ", ans) if ans ~= 15.0 then write("ERROR with testfive") write() # get a pointer/handle ans := native("teststar", TYPESTAR) write("Unicon: called teststar and got ", ans) if ans = 0 then write("ERROR with teststar") write() # a string ans := native("teststring", TYPESTRING, "this is a string") write("Unicon: called teststring and got ", ans) if ans ~== "this is a string" then write("ERROR with teststring") write() # a string, and int and a real, returning string ans := native("testmulti", TYPESTRING, "this is a string for multi", 42, &pi) write("Unicon: called testmulti and got ", ans) if ans ~== "this is a string for multi" then write("ERROR with testmulti") write() # a string, and int and a real, returning real ans := native("testmultid", TYPEDOUBLE, "this is a string for multid", 42, &pi) write("Unicon: called testmultid and got ", ans) if ans ~= &pi then write("ERROR with testmultid") write() # # Variant for float versus double # # pass two reals, get the sum as real write("float variant") nativeFloat := loadfunc("./uninative.so", "nativeFloat") ans := nativeFloat("testfloat", TYPEFLOAT, 9.0, 8.0) write("Unicon: called testfloat and got ", ans) if ans ~= 17.0 then write("ERROR with testfloat") write() # a string, and int and a real, returning real ans := nativeFloat("testmultif", TYPEFLOAT, "this is a short string for multif", 21, &pi/2) write("Unicon: called testmultif and got ", ans) if ans - &pi/2 > 0.0000001 then write("ERROR with testmultif") write() end