@@ -51,7 +51,7 @@ class FFILib : LuaTable() {
5151 private val gcEntries = ConcurrentHashMap <Long , Pair <Pointer , LuaValue >>()
5252 private val metatypeRegistry = ConcurrentHashMap <String , LuaTable >()
5353 private val typeOfRegistry = ConcurrentHashMap <String , CType >()
54- private var debugMode = AtomicBoolean (false )
54+ private var debugMode = AtomicBoolean (true )
5555
5656 init {
5757 registerBasicTypes()
@@ -88,6 +88,7 @@ class FFILib : LuaTable() {
8888
8989 private fun parseCDef (cdefString : String ): CType ? {
9090 val trimmed = cdefString.trim()
91+ log(" parseCDef input: $trimmed " )
9192
9293 val enumRegex = Regex (""" enum\s+(\w+)\s*\{([^}]+)\}""" )
9394 val enumMatch = enumRegex.find(trimmed)
@@ -103,19 +104,22 @@ class FFILib : LuaTable() {
103104 values[name] = currentValue++
104105 }
105106 registerEnum(enumName, values, 4 )
107+ log(" Parsed enum: $enumName " )
106108 return typeRegistry[enumName]
107109 }
108110
109- val structRegex = Regex (""" struct\s+(\w+)\s*\{([\s\S]*?)\}(?:\s*__attribute__\(\(packed\)\))?""" )
111+ val structRegex = Regex (""" struct\s+(\w+)\s*\{([\s\S]*?)\s*\ }(?:\s*__attribute__\(\(packed\)\))?""" )
110112 val structMatch = structRegex.find(trimmed)
113+ log(" Struct regex match: ${structMatch?.groupValues} " )
111114 if (structMatch != null ) {
112115 val structName = structMatch.groupValues[1 ]
113116 val body = structMatch.groupValues[2 ]
114117 val isPacked = trimmed.contains(" __attribute__((packed))" )
118+ log(" Parsing struct: $structName , body: $body " )
115119 return parseStructBody(structName, body, isPacked)
116120 }
117121
118- val unionRegex = Regex (""" union\s+(\w+)\s*\{([\s\S]*?)\} """ )
122+ val unionRegex = Regex (""" union\s+(\w+)\s*\{([\s\S]*?)\s*\}(?:\s*__attribute__\(\(packed\)\))? """ )
119123 val unionMatch = unionRegex.find(trimmed)
120124 if (unionMatch != null ) {
121125 val unionName = unionMatch.groupValues[1 ]
@@ -133,6 +137,7 @@ class FFILib : LuaTable() {
133137 return typeRegistry[newName]
134138 }
135139
140+ log(" parseCDef: no match found" )
136141 return null
137142 }
138143
@@ -547,17 +552,16 @@ class FFILib : LuaTable() {
547552 }
548553 is LuaNumber -> if (v.isint()) v.toint() else v.todouble()
549554 is LuaString -> v.tojstring()
550- else -> null
555+ else -> return error( " FFI: unsupported argument type ${v.typename()} for parameter $i " )
551556 }
552557 }
553558
554559 val res = try {
555560 jnaFunc.invoke(Pointer ::class .java, jnaArgs)
561+ } catch (e: UnsatisfiedLinkError ) {
562+ return error(" FFI: Unable to call ${jnaFunc.name} : ${e.message} " )
556563 } catch (e: Exception ) {
557- if (ffi.debugMode.get()) {
558- log(" FFI Error: ${e.message} " )
559- }
560- null
564+ return error(" FFI: Error calling ${jnaFunc.name} : ${e.message} " )
561565 }
562566
563567 val voidType = typeRegistry[" void" ]!!
0 commit comments