[squeak-dev] FFI related questions

Igor Stasenko siguctua at gmail.com
Tue Jan 5 14:02:51 UTC 2010


2010/1/5 vagy <vagy at freemail.gr>:
> Hello and a happy new year!,
>
> first of i would like to express a big thumbs up for the tremendous effort
> you
> all have been doing the last months wrt the trunk image. Its really amazing
> :-)
>
> I have some FFI on Windows related questions.
> (fyi, i am using the trunk image on which i loaded the latest FFI code
> from SqueakSource, FFI-Kernel-ar.12.mcz and FFI-Pools-ar.1.mcz)
>
> Is it possible, in general, to pass down to C "basic" Smalltalk objects,
> like Integers,
> Floats etc. and have their value changed by the C routine? I am trying to do
> something
> like:
>
>  anInteger := 5.
>
> that i want to send to C and simply change it by, say :
>
>  apiChangeInteger: anInteger
>  <cdecl: void 'change_integer' (long*) module:'MyDLL'>
>
>  (which finally in C reads like:       void set_integer(int* anInteger) {
> *anInteger = *anInteger + 5; } )
>
> I have found the above scheme to not work as i would expect.
>
> Interestingly, if i push down to C an array of basic values like:
>
>  integers := {10. 20. 30} asIntegerArray
>
> the C routine can change the values. For completeness, passing down arrays
> of values
> works for FloatArrays like:
>
>  singlePrecisionFloats := {10.5. 20.5. 30.5} asFloatArray
>  (and then passed to a C routine having a float* argument)
>
> but not for :
>
>  doublePrecisionFloats := {10.5. 20.5. 30.5}
>  (with the C routine having a double* argument)
>
> Probably i am not using FFI as intended although i would expect to be able
> to do all this.
> Can someone (Andreas?) please shed some light to all this?
>
The difference between FloatArray and Array of floats (or array of
arbitrary objects)
is in storage format. While first storing a number of native
floating-point values, edigible for C,
while second stores references to other objects (oop-s).

>
> Another thing that isn't clear to me is if its possible to have FFI create
> Smalltalk objects
> in behalf of a requesting client. In Java's JNI for instance one can say:
>
>  jni->NewFloatArray(length of the array);
>

in that case, i think, you'd better create a VM plugin, which having
an access to something similar to 'jni'
object, which in squeak is called 'interpreterProxy'.

Thinking about it, i came to interesting idea, that really, we could
pass a pointer to interpreterProxy
variable to FFI function, so, potentially, it could make use of VM API
functions to create objects, or access existing ones.
I don't know if that will be ever used, since writing a plugin looks
much more appropriate in that case.

> If it can be done (i mean no VM restrictions) and you also think that it
> would be generally useful (i do),
> i am willing to implement such "helper" routines.
>
> Cheers
>  - Vagelis


-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list