[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test[S8FFI] ObjC

SwikiCodeRobot @> #testS8FFIObjC

"Load Foundation framework"
| foundation |
self print: 'We have to load Foundation first, otherwise Objective-C exceptions will not work.'.
foundation := S8FFI current dynamicLibrary: '/System/Library/Frameworks/Foundation.framework/Foundation'.
foundation isNil ifTrue: [ self error: 'Foundation framework missing' ].
self print: 'Foundation framework loaded.'.

"Access symbol of Foundation framework"
| foundation key symbol |
key := #NSFullUserName.
foundation := S8FFI current dynamicLibrary: '/System/Library/Frameworks/Foundation.framework/Foundation'.
foundation isNil ifTrue: [ self error: 'Foundation framework missing' ].
symbol := foundation get: key.
symbol isNil ifTrue: [ self error: 'Symbol ' ,key asLiteral ,' not found' ].

Uploaded Image: alert.gifNote that symbol is a NodeBuffer pointing to NSFullUserName function

"Access ObjC library"
| objcLib id sel cls pool |
id := sel := cls := S8Ref voidPtr.
objcLib := S8FFI current library: #libobjc functions: (
	  #objc_msgSend -> (Array with: id with: (Array with: id with: sel))
	,(#objc_getClass -> (Array with: cls with: #(string)))
	,(#sel_registerName -> (Array with: sel with: #(string)))
	).
objcLib isNil ifTrue: [ self error: 'ObjC library missing' ].
cls  := objcLib @ #objc_getClass value: #NSAutoreleasePool.
pool := objcLib @ #objc_msgSend value: cls value: (objcLib @ #sel_registerName value: #new).
self print: '// pool created ' ,pool.
[	"force an ObjC exception..."
	self print: '// about to retain.'.

self error: 'CRASH - ToDo,Broken - exception is not captured'.

	objcLib @ #objc_msgSend value: pool value: (objcLib @ #sel_registerName value: #retain).
] on: Error do: [:ex| ^self print: 'ok - ' ,ex toString stream nextLine ].
self error: 'should throw a Buffer instance when an exception happens'


References