Hi again, Does anyone know what is the state of return by reference in FFI? Example: glGetAttribute: attr into: value <primitive: #primitiveNativeCall module: #NativeBoostPlugin error: errorCode> ^ self nbCall: #( int SDL_GL_GetAttribute(SDL_GLattr attr, int *value) ) On my image (50666) it does nothing on value yet (if nil, remains nil, if SmallInteger 0, remains 0). (SDL2 glGetAttribute: 6 into: value) should return 16. I've seen NBOpenGL put "out" before value in method header, should it matter? (does not make it work though) Last but not least, should we attach '*' to the type or variable for FFI to understand it is a pointer? Cheers, Thibault Raffaillac
I think in that case you need to pass an instance of FFIExternalValueHolder. On 7 April 2016 at 17:42, Thibault Raffaillac <thibault.raffaillac@inria.fr> wrote:
Hi again,
Does anyone know what is the state of return by reference in FFI? Example: glGetAttribute: attr into: value <primitive: #primitiveNativeCall module: #NativeBoostPlugin error: errorCode> ^ self nbCall: #( int SDL_GL_GetAttribute(SDL_GLattr attr, int *value) )
On my image (50666) it does nothing on value yet (if nil, remains nil, if SmallInteger 0, remains 0). (SDL2 glGetAttribute: 6 into: value) should return 16. I've seen NBOpenGL put "out" before value in method header, should it matter? (does not make it work though) Last but not least, should we attach '*' to the type or variable for FFI to understand it is a pointer?
Cheers, Thibault Raffaillac
On 07 Apr 2016, at 14:15, Damien Pollet <damien.pollet+pharo@gmail.com> wrote:
I think in that case you need to pass an instance of FFIExternalValueHolder.
yes. you can take the test FFIExternalValueHolderTests>>testCall as an example. But since it uses anonymous classes it might require a better explanation, there is the class comment, but basically, you do this: 1) Declare a class variable MyClass class>>initialize INT_PTR := FFIExternalValueHolder ofType: âintâ. 2) Replace "int *â in your function calls with your new type: glGetAttribute: attr into: value ^ self ffiCall: #( int SDL_GL_GetAttribute(SDL_GLattr attr, INT_PTR value) ) 3) pass an instance of INT_PTR to get the value: | ptr value | ptr := INT_PTR new. self glGetAttribute: anAttributeIDontKnow into: ptr. value := ptr value. Esteban
On 7 April 2016 at 17:42, Thibault Raffaillac <thibault.raffaillac@inria.fr <mailto:thibault.raffaillac@inria.fr>> wrote: Hi again,
Does anyone know what is the state of return by reference in FFI? Example: glGetAttribute: attr into: value <primitive: #primitiveNativeCall module: #NativeBoostPlugin error: errorCode> ^ self nbCall: #( int SDL_GL_GetAttribute(SDL_GLattr attr, int *value) )
On my image (50666) it does nothing on value yet (if nil, remains nil, if SmallInteger 0, remains 0). (SDL2 glGetAttribute: 6 into: value) should return 16. I've seen NBOpenGL put "out" before value in method header, should it matter? (does not make it work though) Last but not least, should we attach '*' to the type or variable for FFI to understand it is a pointer?
Cheers, Thibault Raffaillac
On 08 Apr 2016, at 11:34, Esteban Lorenzano <estebanlm@gmail.com> wrote:
On 07 Apr 2016, at 14:15, Damien Pollet <damien.pollet+pharo@gmail.com <mailto:damien.pollet+pharo@gmail.com>> wrote:
I think in that case you need to pass an instance of FFIExternalValueHolder.
yes. you can take the test FFIExternalValueHolderTests>>testCall as an example.
But since it uses anonymous classes it might require a better explanation, there is the class comment, but basically, you do this:
1) Declare a class variable
MyClass class>>initialize INT_PTR := FFIExternalValueHolder ofType: âintâ.
as a larger explanation, this will create an anonymous subclass to keep âintâ values, than later you can use as a type in your function call. Some people would call this obscure, but is a good way of solving this problem (I adopted it from old NB). If you want to have a named class instead an anonymous class, you can just subclass FFIExternalValueHolder: FFIExternalValueHolder subclass: #MyIntPtr and then implement typeDecl class method MyIntPtr class>>typeDecl ^ âint' and this will work as the anonymous class
2) Replace "int *â in your function calls with your new type:
glGetAttribute: attr into: value ^ self ffiCall: #( int SDL_GL_GetAttribute(SDL_GLattr attr, INT_PTR value) )
3) pass an instance of INT_PTR to get the value:
| ptr value |
ptr := INT_PTR new. self glGetAttribute: anAttributeIDontKnow into: ptr.
value := ptr value.
Esteban
On 7 April 2016 at 17:42, Thibault Raffaillac <thibault.raffaillac@inria.fr <mailto:thibault.raffaillac@inria.fr>> wrote: Hi again,
Does anyone know what is the state of return by reference in FFI? Example: glGetAttribute: attr into: value <primitive: #primitiveNativeCall module: #NativeBoostPlugin error: errorCode> ^ self nbCall: #( int SDL_GL_GetAttribute(SDL_GLattr attr, int *value) )
On my image (50666) it does nothing on value yet (if nil, remains nil, if SmallInteger 0, remains 0). (SDL2 glGetAttribute: 6 into: value) should return 16. I've seen NBOpenGL put "out" before value in method header, should it matter? (does not make it work though) Last but not least, should we attach '*' to the type or variable for FFI to understand it is a pointer?
Cheers, Thibault Raffaillac
participants (3)
-
Damien Pollet -
Esteban Lorenzano -
Thibault Raffaillac