<div id="__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: #000000">
                                        
                                        
                                            
                                        
                                        
                                        Btw: Type aliases to pointer types can already be handled through their referent sub-instances of ExternalTypeAlias. I did already put #doesNotUnderstand: there to forward the calls to #value.<div><br></div><div>INT_P class >> #originalTypeName</div><div>   ^ 'int*'</div><div><br></div><div>...</div><div>data := ExternalData</div><div>   fromHandle: #[ 12 13 14 15 ]</div><div>   type: INT_P externalType.</div><div>...</div><div>myIntP := data asExternalStructure. <span style="font-size: 10pt">"myIntP class == INT_P."</span></div><div>anInt := myIntP at: 1. "anInt isKindOf: Integer"</div><div><br></div><div>Maybe also take a look at ExternalStructure >> #asExternalData.</div><div><br></div><div>You would only lose track of the type INT_P if you do this:</div><div><br></div><div>myIntP value asExternalData.</div><div><br></div><div>After that you have int* and cannot go back to that ExternalTypeAlias:</div><div><br></div><div><span style="font-size: 13.3333px">myIntP value "OK" asExternalData "OK" asExternalStructure "ERROR"</span><br></div><div><span style="font-size: 13.3333px"><br></span></div><div><span style="font-size: 13.3333px">For aliases to struct pointers you will also lose track of the alias</span></div><div><span style="font-size: 13.3333px"><br></span></div><div><span style="font-size: 13.3333px">myStructP value "OK" asExternalData "OK" asExternalStructure "OK but different class"</span></div><div><br></div><div>Best,</div><div>Marcel</div><div class="mb_sig"></div>
                                        
                                        <blockquote class="history_container" type="cite" style="border-left-style: solid;border-width: 1px;margin-top: 20px;margin-left: 0px;padding-left: 10px;min-width: 500px">
                        <p style="color: #AAAAAA; margin-top: 10px;">Am 19.06.2020 19:01:50 schrieb Marcel Taeumel <marcel.taeumel@hpi.de>:</p><div style="font-family:Arial,Helvetica,sans-serif"><div id="__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: #000000">
                                        > <span style="font-family: Arial, Helvetica, sans-serif;font-size: 13px">I'd like to have a 3rd possibility: pointing to a sub-region of a ByteArray</span><br style="font-family: Arial, Helvetica, sans-serif;font-size: 13px"><span style="font-family: Arial, Helvetica, sans-serif;font-size: 13px">> (ByteArray + offset)</span><div><span style="font-family: Arial, Helvetica, sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial, Helvetica, sans-serif"><span style="font-size: 13px">For this, you could add "offset" to ExternalData (in addition to "type" and "size"). This would also postpone the expensive pointer arithmetic implemented in ExternalAddress >> #+ to the last possible moment. :-) </span></span></div><div><span style="font-family: Arial, Helvetica, sans-serif"><span style="font-size: 13px"><br></span></span></div><div><span style="font-family: Arial, Helvetica, sans-serif"><span style="font-size: 13px">Best,</span></span></div><div><span style="font-family: Arial, Helvetica, sans-serif"><span style="font-size: 13px">Marcel</span></span></div><div class="mb_sig"></div><blockquote class="history_container" type="cite" style="border-left-style: solid;border-width: 1px;margin-top: 20px;margin-left: 0px;padding-left: 10px;min-width: 500px">
                        <p style="color: #AAAAAA; margin-top: 10px;">Am 19.06.2020 18:52:02 schrieb Nicolas Cellier <nicolas.cellier.aka.nice@gmail.com>:</p><div style="font-family:Arial,Helvetica,sans-serif"> Well, my mental model is this one:
<br>
<br>ExternalData needs to refer to an area of memory, so as to be able to act
<br>as a pointer (single element or array does not really matter).
<br>If this memory area lies in external heap, then we need an ExternalAddress
<br>as handle.
<br>If this memory area lies in object memory, it must be a ByteArray.
<br>We cannot handle pointers to object memory, because object memory is
<br>relocatable...
<br>Well, unless we pin the object - that why we pass the ByteArray itself, not
<br>its address..
<br>
<br>This is independent of how we interpret the contents.
<br>IMO, it's more useful to avoid the pointer indirection in type, because we
<br>cannot handle double pointer currently...
<br>Of course, the only pointers that the memory area could contain are
<br>pointers to external heap (or eventually pinned objects).
<br>
<br>I'd like to have a 3rd possibility: pointing to a sub-region of a ByteArray
<br>(ByteArray + offset).
<br>That may be very useful for emulating.
<br>An example in Smallapack: pass a pointer to the imaginary part of a complex
<br>array.
<br>Since real and imaginary parts are interleaved, just offsetting the pointer
<br>and using a stride of 2 elements does the trick when one wants to access
<br>real or imaginary part
<br>(most lapack/blas routines are equipped to handle simple strides).
<br>This is currently possible with ExternalAddress, but NOT when matrix data
<br>lies in ObjectMemory.
<br>Another example would be if you wanted to implement an interpreter for a
<br>low level language... (emulate a processor, whatever).
<br>
<br>
<br>
<br>Le ven. 19 juin 2020 à 18:04, Marcel Taeumel <marcel.taeumel@hpi.de> a
<br>écrit :
<br>
<br>>
<br>> So, what about this:
<br>>
<br>> ExternalData<@00f3a3ee, int="">
<br>>
<br>> I think it could mean that you want to interpret the very pointer address
<br>> itself as an integer. That is, you do not want to follow to where it points
<br>> to.
<br>>
<br>> Then what about this:
<br>>
<br>> ExternalData<@00f3a3ee, double="">
<br>>
<br>> Ha! An error, I suppose. Unless ... given 8-byte pointers, you can try to
<br>> read a double out of it. :-D Just kidding. It should be an error.
<br>>
<br>> .
<br>> .
<br>> .
<br>>
<br>> > > (On a side note: I think that ExternalData should always have a
<br>> pointer type in its "type" field unless "handle" is a ByteArray instead of
<br>> ExternalAddress)
<br>> > Let me disagree here...
<br>>
<br>> The examples I just wrote down in the previous messages let me realize why
<br>> you did disagree. :-D
<br>>
<br>> I hope this helps you with improving argument coercing and return value
<br>> packaging in the FFI plugin. ^__^
<br>>
<br>> Best,
<br>> Marcel
<br>>
<br>> Am 19.06.2020 17:53:21 schrieb Marcel Taeumel <marcel.taeumel@hpi.de>:
<br>> > then you would use int* and not int**
<br>>
<br>> My bad: "  then you would use int** and not int*  " Sorry for the noise
<br>> -.-"
<br>>
<br>> Am 19.06.2020 17:52:21 schrieb Marcel Taeumel <marcel.taeumel@hpi.de>:
<br>> > If you want to make a pointer array out of it, change the type to int**
<br>>
<br>> Correction: If you happen to now, that this external address points to an
<br>> array of pointers to int, then you would use int* and not int**. You cannot
<br>> just "decide" what the data behind that external address should be. .. you
<br>> know what I mean. ;-)
<br>>
<br>> best,
<br>> Marcel
<br>>
<br>> Am 19.06.2020 17:47:36 schrieb Marcel Taeumel <marcel.taeumel@hpi.de>:
<br>> Hi Nicolas.
<br>>
<br>> Maybe one more thought on
<br>>
<br>> ExternalData<handle, type="">
<br>>
<br>> There is no need to have
<br>>
<br>> ExternalData<1312301580, int="">
<br>>
<br>> because it can just be 1312301580.
<br>>
<br>> On the other hand:
<br>>
<br>> ExternalData<#[ 12="" 34="" 56="" 78="" ],="" int="">
<br>>
<br>> Tells you that (1) this data wants to be interpreted as signed int and (2)
<br>> this data is already in Squeak's object memory.
<br>>
<br>> Finally:
<br>>
<br>> ExternalData<@00f3a3ee, int*="">
<br>>
<br>> Tells you that (1) this data wants to be interpreted as signed int and (2)
<br>> this data is in external memory.
<br>>
<br>> If you want to make an array out of it, make use of the "size" field in
<br>> ExternalData.
<br>>
<br>> If you want to make a pointer array out of it, change the type to int**
<br>>
<br>> ExternalData<@00f3a3ee, int**="">
<br>>
<br>> So, what does this mean then:
<br>>
<br>> ExternalData<#[ 12="" 34="" 56="" 78="" ],="" int*="">
<br>>
<br>> Well, it tells you that there is a pointer to an int stored in a byte
<br>> array. This comes very handy if you think about type aliases to pointer
<br>> types such as "typedef int* INT_P". Because then it would be
<br>>
<br>> ExternalData<#[ 12="" 34="" 56="" 78="" ],="" int_p="">
<br>>
<br>> To where this pointer points to? Probably garbage. I just made it up for
<br>> this example. :-D
<br>>
<br>> Best,
<br>> Marcel
<br>>
<br>> Am 19.06.2020 17:15:23 schrieb Marcel Taeumel <marcel.taeumel@hpi.de>:
<br>> > The type should better describe the contents of the handle (be it an
<br>> > ExternalAddress or a zone of object memory (ByteArray)).
<br>> > Typically, if you have an external (global) variable of type foo (extern
<br>> > foo my_var;) then the natural type is foo.
<br>> > Having foo*, would mean that we handle an array of foo*, or a pointer to
<br>> > foo* (foo**).
<br>> > This way, we make no difference between type surrogates
<br>> > (ExternalStruct/ExternalTypeAlias) and other atomic cases.
<br>>
<br>> We have the "size" field in ExternalData encode whether "int*" points to a
<br>> single or multiple things. No need to treat a global variable sitting in
<br>> external memory any different here.  ... IMO :-)
<br>>
<br>> I know, that this is my personal mental model for Squeak FFI to treat
<br>> pointer types as things in external memory and non-pointer types as things
<br>> in Squeak's object memory. The latter includes ByteArray and Integer and
<br>> Float and also IntegerArray etc. Just not ExternalAddress.
<br>>
<br>> This basically comes from the fact that, until now, the FFI plugin gave me
<br>> a ByteArray in return when I told it to be "Foo", not "Foo*". So,
<br>> non-pointer type means ByteArray. Pointer-type means ExternalAddress.
<br>>
<br>> Type aliases are not really different here. Each alias as a corresponding
<br>> struct type, which has again a non-pointer variant and a pointer variant.
<br>>
<br>> Type aliases to pointer types are just special because they store the
<br>> pointer address in a byte array. The corresponding non-pointer type will do
<br>> fine for such aliases. Their pointer type is not relevant in general, I
<br>> suppose.
<br>>
<br>> > Having foo*, would mean that we handle an array of foo*, or a pointer to foo*
<br>> (foo**).
<br>>
<br>> Well, I have a simple idea on how to represent foo** (and other
<br>> dimensions) without the FFI plugin needing to know. ... unless you are
<br>> concerned about type safety for that, too. Then we would really need to
<br>> encode that in the compiledSpec.
<br>>
<br>> If not, ExternalData with foo** type will just answer ExternalData with
<br>> foo* when asked via "at: 1" for example. :-)
<br>>
<br>> > or an Alien as we may want to support that too.
<br>>
<br>> I don't think that's necessary. Alien has its own call-out mechanism
<br>> through the IA32ABI plugin and also a different layout/usage of ByteArray.
<br>>
<br>> Best,
<br>> Marcel
<br>>
<br>> Am 19.06.2020 16:40:50 schrieb Nicolas Cellier <><br>> nicolas.cellier.aka.nice@gmail.com>:
<br>> Le ven. 19 juin 2020 à 16:16, Marcel Taeumel a
<br>> écrit :
<br>>
<br>> >
<br>> > > Err, I move this thread to Opensmalltalk VM dev, because it is not
<br>> > Squeak specific!
<br>> >
<br>> > Absolutely. :-)
<br>> >
<br>> > (On a side note: I think that ExternalData should always have a pointer
<br>> > type in its "type" field unless "handle" is a ByteArray instead of
<br>> > ExternalAddress)
<br>> >
<br>> > Let me disagree here...
<br>> The type should better describe the contents of the handle (be it an
<br>> ExternalAddress or a zone of object memory (ByteArray)).
<br>> Typically, if you have an external (global) variable of type foo (extern
<br>> foo my_var;) then the natural type is foo.
<br>> Having foo*, would mean that we handle an array of foo*, or a pointer to
<br>> foo* (foo**).
<br>> This way, we make no difference between type surrogates
<br>> (ExternalStruct/ExternalTypeAlias) and other atomic cases.
<br>>
<br>> (On another side note: I also think that "handle" in ExternalData should
<br>> > never ever be an atomic Smalltalk object (Integer or Float) but always
<br>> > either ByteArray or ExternalAddress. ... which makes it different from
<br>> what
<br>> > "handle" can be in ExternalStructs that represent type aliases. :-)
<br>> >
<br>> > Yes!
<br>> It could be an ExternalAddress, or a zone of object memory (ByteArray) or
<br>> an Alien as we may want to support that too.
<br>> I'd be inclined to support doubleByte, word, and doubleWord arrays if the
<br>> size of atomic type matches
<br>> (Typically passing a DoubleByteArray, WordArray, a Float32Array, a
<br>> Float64Array to a short */int */float*/double*).
<br>>
<br>> Best,
<br>> > Marcel
<br>> >
<br>> > Am 19.06.2020 16:03:10 schrieb Nicolas Cellier <>
<br>> > nicolas.cellier.aka.nice@gmail.com>:
<br>> > Err, I move this thread to Opensmalltalk VM dev, because it is not
<br>> Squeak
<br>> > specific!
<br>> >
<br>> >
<br>> >
<br>> > Le ven. 19 juin 2020 à 15:10, Nicolas Cellier <>
<br>> > nicolas.cellier.aka.nice@gmail.com> a écrit :
<br>> >
<br>> > > Hi Marcel,
<br>> > > thanks for your vote. Anyone else?
<br>> > >
<br>> > > Le ven. 19 juin 2020 à 09:30, Marcel Taeumel a
<br>> > > écrit :
<br>> > >
<br>> > >> Hi Nicolas, hi all! :-)
<br>> > >>
<br>> > >> > Type alias are subclasses of ExternalStructure.
<br>> > >>
<br>> > >> And with it, they get their own instance of ExternalType, which is
<br>> > >> managed in the classVar StructTypes. Let's call them struct type.
<br>> > >>
<br>> > >> > Now we have another mean by adding a class side method in
<br>> > ExternalType.
<br>> > >>
<br>> > >> That kind of aliasing is for compile-time type referencing only --
<br>> such
<br>> > >> as in FFI calls (i.e. pragmas) or struct-field definitions
<br>> > >> (i.e. #fields). (And soon if I have the first version of
<br>> > >> FFI-Callback to show you :)
<br>> > >>
<br>> > >> Because those aliases become fully transparent after compiling the
<br>> > method
<br>> > >> for the FFI call into an instance of ExternalLibraryFunction and
<br>> after
<br>> > >> compiling the field specs of external structures into compiledSpec
<br>> for
<br>> > >> struct types.
<br>> > >>
<br>> > >> For example, on my machine in 32-bit Squeak, a 'size_t' becomes a
<br>> > 'long',
<br>> > >> which is Squeak FFI term for 'int'. :-) See #ffiLongVsInt. And
<br>> > 'int32_t'
<br>> > >> also becomes a 'long'. So, it just translates C vocabulary into
<br>> Squeak
<br>> > FFI
<br>> > >> vocabulary. Nothing more. Nothing less.
<br>> > >>
<br>> > >> Having that, please do not use class-side methods in ExternalType to
<br>> > >> alias a (complex) struct type. Use them only for renaming atomic
<br>> types.
<br>> > >> Please. We may want to add checks to enforce that.
<br>> > >>
<br>> > >> I suppose that this discussion focuses on type aliases that are
<br>> > >> represented as subclasses of ExternalStructure (or ExternalTypeAlias)
<br>> > and
<br>> > >> thus get their own struct type. So, let's ignore this other mechanism
<br>> > for
<br>> > >> now.
<br>> > >>
<br>> > >> > This has one advantage: type safety. You cannot pass a Foo to a
<br>> > >> function expecting a Bar, even if they both alias int...
<br>> > >>
<br>> > >> Yes! So, we are now talking about type aliases to atomic types. The
<br>> > >> struct type you get when aliasing a 'char' as Foo, for example, looks
<br>> > like
<br>> > >> this:
<br>> > >>
<br>> > >> (...forgive my "creative" representation of WordArray here...)
<br>> > >>
<br>> > >> compiledSpec: 0A 04 00 01
<br>> > >> referentClass: Foo
<br>> > >>
<br>> > >> How does the atomic type for 'char' look like?
<br>> > >>
<br>> > >> compiledSpec: 0A 04 00 01
<br>> > >> referentClass: nil
<br>> > >>
<br>> > >> Consequently, argument coercing will fail if you pass a 'char' when a
<br>> > >> 'Foo' is expected. Because the referentClass does not match --- even
<br>> if
<br>> > the
<br>> > >> compiledSpec does match.
<br>> > >>
<br>> > >> > But this has one major drawback: with current FFI, you cannot pass
<br>> a
<br>> > >> Smalltalk Integer, to a method expecting a Bar, even if Bar is an
<br>> alias
<br>> > for
<br>> > >> an integer type...
<br>> > >>
<br>> > >> That's correct. If the origin of such an argument is from within the
<br>> > >> image, you have to wrap it. But when returned from another call ...
<br>> > well
<br>> > >> ... that wrapping should have happened in the plugin ... But read on!
<br>> > :-)
<br>> > >>
<br>> > >> > FFI plugin is clever enough to recognize an atomic type alias and
<br>> > >> simply return a Smalltalk Integer...
<br>> > >> > So it's not Bar all the way down, I have to manually wrap Bar...
<br>> > >> > This is not consistent.
<br>> > >>
<br>> > >> Uhh! That's a bug. The FFI Plugin must take a look at referentClass
<br>> > >> because it does so for argument coercing.
<br>> > >>
<br>> > >> Now I understand why the code generation of struct-field accessors
<br>> was
<br>> > so
<br>> > >> apparently broken for me. I changed that so that having an alias type
<br>> > as
<br>> > >> part of a larger struct will automatically wrap that for you into the
<br>> > alias
<br>> > >> structure. :-) Even if you are just aliasing a plain 'int' ... or
<br>> > 'long'
<br>> > >> ... Ha ha. ;-) #ffiLongVsInt.
<br>> > >>
<br>> > >> It should be "Bar all the way down". Definitely.
<br>> > >>
<br>> > >> > If we return an int, we must accept an int, otherwise, we cannot
<br>> > chain
<br>> > >> FFI calls...
<br>> > >>
<br>> > >> Well, not for type aliases. I would like keep the path of type safety
<br>> > >> here, you mentioned above.
<br>> > >>
<br>> > >> Just fix the FFI plugin to respect referentClass when packaging the
<br>> > >> return value.
<br>> > >>
<br>> > >> > I could use the lightweight alias instead, [...]
<br>> > >>
<br>> > >> Please don't. See above. Those "lightweight alias" are for renaming
<br>> > >> atomic types only. Let's keep it simple and try to address you
<br>> concern
<br>> > here
<br>> > >> with struct types and ExternalTypeAlias. :-)
<br>> > >>
<br>> > >> > there was another usage where it was convenient to use
<br>> > >> ExternalTypeAlias: enum.
<br>> > >> > Indeed, I simply defined all enum symbols as class side method.
<br>> > >>
<br>> > >> I like that! It reads like a next step for ExternalPool. Once you
<br>> have
<br>> > >> extracted the constant values from header files, and once you have
<br>> > those
<br>> > >> values for your platform in classVars in your external pool, use that
<br>> > pool
<br>> > >> to define enums through ExternalStructure.
<br>> > >>
<br>> > >> Like this:
<br>> > >>
<br>> > >> ExternalTypeAlias subclass: #MyEnumBar
<br>> > >> instanceVariableNames: ''
<br>> > >> classVariableNames: ''
<br>> > >> poolDictionaries: 'HDF5Pool'
<br>> > >> category: 'HDF5'
<br>> > >>
<br>> > >> MyEnumBar class >> #poolFields
<br>> > >> "
<br>> > >> self defineFields.
<br>> > >> "
<br>> > >> ^ #(
<br>> > >> (beg HDF5_BEG)
<br>> > >> (mid HDF5_MID)
<br>> > >> (end HDF5_END)
<br>> > >> )
<br>> > >>
<br>> > >> Note that HDF5_BEG etc. are from the HDF5Pool, which is managed via
<br>> > >> external-pool definitions. See class comment in ExternalPool to get
<br>> > started.
<br>> > >>
<br>> > >> Note that #poolFields would translate to class-side methods as you
<br>> > >> suggested.
<br>> > >>
<br>> > >> > If I use a simpler alias, where are those ids going to?
<br>> > >>
<br>> > >> Please don't. See above. :-)
<br>> > >>
<br>> > >> > IMO we cannot keep status quo in the plugin, we gotta to make the
<br>> > >> inputs/outputs behave consistently.
<br>> > >>
<br>> > >> Absolutely!
<br>> > >>
<br>> > >> > - should a function expecting an ExternalTypeAlias of atomic type
<br>> > >> accept an immediate value of compatible type?
<br>> > >>
<br>> > >> Considering type safety, I think not.
<br>> > >>
<br>> > >> Well ... since this is a convenience issue and thus not about saving
<br>> > >> run-time cost ... What about adding a callback into the image to
<br>> react
<br>> > on
<br>> > >> FFIErrorCoercionFailed? Maybe the selector for such a callback could
<br>> be
<br>> > >> stored in ExternalLibraryFunction since this is accessible to the
<br>> > plugin
<br>> > >> anyway?
<br>> > >>
<br>> > >>
<br>> > >>
<br>> > >> ... would it be possible? Like that #doesNotUnderstand: callback?
<br>> > >>
<br>> > >> I would love to do ad-hoc packaging of arguments. (Also thinking
<br>> about
<br>> > >> FFI-Callback. But ignore me here :)
<br>> > >>
<br>> > >> > - should a function returning an ExternalTypeAlias of atomic type
<br>> > >> instantiate the Alias?
<br>> > >>
<br>> > >> Yes, please! See above.
<br>> > >>
<br>> > >> If you want to trade type safety in for a performance gain, just use
<br>> a
<br>> > >> "lightweight alias" as explained above. And package that return value
<br>> > >> manually.
<br>> > >>
<br>> > >> Best,
<br>> > >> Marcel
<br>> > >>
<br>> > >> Am 18.06.2020 21:03:53 schrieb Nicolas Cellier <>
<br>> > >> nicolas.cellier.aka.nice@gmail.com>:
<br>> > >> Hi all,
<br>> > >> following the question of Marcel about usage of ExternalTypeAlias:
<br>> > >>
<br>> > >> Type alias are subclasses of ExternalStructure.
<br>> > >> I used them in HDF5 because there was no other means to define an
<br>> alias
<br>> > >> at the time.
<br>> > >> Now we have another mean by adding a class side method in
<br>> ExternalType
<br>> > (I
<br>> > >> would have rather imagined the usage of some annotation to get a
<br>> > >> declarative style)
<br>> > >>
<br>> > >> This has one advantage: type safety. You cannot pass a Foo to a
<br>> > function
<br>> > >> expecting a Bar, even if they both alias int...
<br>> > >>
<br>> > >> But this has one major drawback: with current FFI, you cannot pass a
<br>> > >> Smalltalk Integer, to a method expecting a Bar, even if Bar is an
<br>> alias
<br>> > for
<br>> > >> an integer type...
<br>> > >>
<br>> > >> Thus, you have to wrap every Bar argument into a Bar instance...
<br>> > >> Gasp, this is going too far...
<br>> > >>
<br>> > >> What about functions returning such alias?
<br>> > >> Function returning struct by value allocate a struct, but it's not
<br>> the
<br>> > >> case here, FFI plugin is clever enough to recognize an atomic type
<br>> > alias
<br>> > >> and simply return a Smalltalk Integer...
<br>> > >> So it's not Bar all the way down, I have to manually wrap Bar...
<br>> > >> This is not consistent.
<br>> > >> If we return an int, we must accept an int, otherwise, we cannot
<br>> chain
<br>> > >> FFI calls...
<br>> > >>
<br>> > >> I could use the lightweight alias instead, but there was another
<br>> usage
<br>> > >> where it was convenient to use ExternalTypeAlias: enum.
<br>> > >> Indeed, I simply defined all enum symbols as class side method. For
<br>> > >> example for enum bar { beg=0, mid=1, end=2 }; I simply create a type
<br>> > Bar
<br>> > >> aliasing int and having class side methods beg ^0, mid ^1, end ^2.
<br>> > >> This way, I normally can pass a Bar mid to an external function.
<br>> > >> (currently, we must defne mid ^Bar new value: 1; yourself)
<br>> > >>
<br>> > >> If I use a simpler alias, where are those ids going to?
<br>> > >>
<br>> > >> IMO we cannot keep statu quo in the plugin, we gotta to make the
<br>> > >> inputs/outputs behave consistently.
<br>> > >> The choice is this one:
<br>> > >> - should a function expecting an ExternalTypeAlias of atomic type
<br>> > accept
<br>> > >> an immediate value of compatible type? (the permissive
<br>> implementation)
<br>> > >> - should a function returning an ExternalTypeAlias of atomic type
<br>> > >> instantiate the Alias? (the typesafe implementation, with higher
<br>> > runtime
<br>> > >> cost due to pressure on GC)
<br>> > >>
<br>> > >>
<br>> > >>
<br>> > >>
<br>> > Err, I move this thread to Opensmalltalk VM dev, because it is not
<br>> Squeak
<br>> > specific!
<br>> >
<br>> >
<br>> >
<br>> > Le ven. 19 juin 2020 à 15:10, Nicolas Cellier <>
<br>> > nicolas.cellier.aka.nice@gmail.com> a écrit :
<br>> >
<br>> >> Hi Marcel,
<br>> >> thanks for your vote. Anyone else?
<br>> >>
<br>> >> Le ven. 19 juin 2020 à 09:30, Marcel Taeumel a
<br>> >> écrit :
<br>> >>
<br>> >>>
<br>> >>> Hi Nicolas, hi all! :-)
<br>> >>>
<br>> >>> > Type alias are subclasses of ExternalStructure.
<br>> >>>
<br>> >>> And with it, they get their own instance of ExternalType, which is
<br>> >>> managed in the classVar StructTypes. Let's call them struct type.
<br>> >>>
<br>> >>> > Now we have another mean by adding a class side method in
<br>> ExternalType.
<br>> >>>
<br>> >>> That kind of aliasing is for compile-time type referencing only --
<br>> such
<br>> >>> as in FFI calls (i.e. pragmas) or struct-field definitions
<br>> >>> (i.e. #fields). (And soon if I have the first version of
<br>> >>> FFI-Callback to show you :)
<br>> >>>
<br>> >>> Because those aliases become fully transparent after compiling the
<br>> >>> method for the FFI call into an instance of ExternalLibraryFunction
<br>> and
<br>> >>> after compiling the field specs of external structures into
<br>> compiledSpec
<br>> >>> for struct types.
<br>> >>>
<br>> >>> For example, on my machine in 32-bit Squeak, a 'size_t' becomes a
<br>> >>> 'long', which is Squeak FFI term for 'int'. :-) See #ffiLongVsInt. And
<br>> >>> 'int32_t' also becomes a 'long'. So, it just translates C vocabulary
<br>> into
<br>> >>> Squeak FFI vocabulary. Nothing more. Nothing less.
<br>> >>>
<br>> >>> Having that, please do not use class-side methods in ExternalType to
<br>> >>> alias a (complex) struct type. Use them only for renaming atomic
<br>> types.
<br>> >>> Please. We may want to add checks to enforce that.
<br>> >>>
<br>> >>> I suppose that this discussion focuses on type aliases that are
<br>> >>> represented as subclasses of ExternalStructure (or ExternalTypeAlias)
<br>> and
<br>> >>> thus get their own struct type. So, let's ignore this other mechanism
<br>> for
<br>> >>> now.
<br>> >>>
<br>> >>> > This has one advantage: type safety. You cannot pass a Foo to a
<br>> >>> function expecting a Bar, even if they both alias int...
<br>> >>>
<br>> >>> Yes! So, we are now talking about type aliases to atomic types. The
<br>> >>> struct type you get when aliasing a 'char' as Foo, for example, looks
<br>> like
<br>> >>> this:
<br>> >>>
<br>> >>> (...forgive my "creative" representation of WordArray here...)
<br>> >>>
<br>> >>> compiledSpec: 0A 04 00 01
<br>> >>> referentClass: Foo
<br>> >>>
<br>> >>> How does the atomic type for 'char' look like?
<br>> >>>
<br>> >>> compiledSpec: 0A 04 00 01
<br>> >>> referentClass: nil
<br>> >>>
<br>> >>> Consequently, argument coercing will fail if you pass a 'char' when a
<br>> >>> 'Foo' is expected. Because the referentClass does not match --- even
<br>> if the
<br>> >>> compiledSpec does match.
<br>> >>>
<br>> >>> > But this has one major drawback: with current FFI, you cannot pass a
<br>> >>> Smalltalk Integer, to a method expecting a Bar, even if Bar is an
<br>> alias for
<br>> >>> an integer type...
<br>> >>>
<br>> >>> That's correct. If the origin of such an argument is from within the
<br>> >>> image, you have to wrap it. But when returned from another call ...
<br>> well
<br>> >>> ... that wrapping should have happened in the plugin ... But read on!
<br>> :-)
<br>> >>>
<br>> >>> > FFI plugin is clever enough to recognize an atomic type alias and
<br>> >>> simply return a Smalltalk Integer...
<br>> >>> > So it's not Bar all the way down, I have to manually wrap Bar...
<br>> >>> > This is not consistent.
<br>> >>>
<br>> >>> Uhh! That's a bug. The FFI Plugin must take a look at referentClass
<br>> >>> because it does so for argument coercing.
<br>> >>>
<br>> >>> Now I understand why the code generation of struct-field accessors was
<br>> >>> so apparently broken for me. I changed that so that having an alias
<br>> type as
<br>> >>> part of a larger struct will automatically wrap that for you into the
<br>> alias
<br>> >>> structure. :-) Even if you are just aliasing a plain 'int' ... or
<br>> 'long'
<br>> >>> ... Ha ha. ;-) #ffiLongVsInt.
<br>> >>>
<br>> >>> It should be "Bar all the way down". Definitely.
<br>> >>>
<br>> >>> > If we return an int, we must accept an int, otherwise, we cannot
<br>> chain
<br>> >>> FFI calls...
<br>> >>>
<br>> >>> Well, not for type aliases. I would like keep the path of type safety
<br>> >>> here, you mentioned above.
<br>> >>>
<br>> >>> Just fix the FFI plugin to respect referentClass when packaging the
<br>> >>> return value.
<br>> >>>
<br>> >>> > I could use the lightweight alias instead, [...]
<br>> >>>
<br>> >>> Please don't. See above. Those "lightweight alias" are for renaming
<br>> >>> atomic types only. Let's keep it simple and try to address you concern
<br>> here
<br>> >>> with struct types and ExternalTypeAlias. :-)
<br>> >>>
<br>> >>> > there was another usage where it was convenient to use
<br>> >>> ExternalTypeAlias: enum.
<br>> >>> > Indeed, I simply defined all enum symbols as class side method.
<br>> >>>
<br>> >>> I like that! It reads like a next step for ExternalPool. Once you have
<br>> >>> extracted the constant values from header files, and once you have
<br>> those
<br>> >>> values for your platform in classVars in your external pool, use that
<br>> pool
<br>> >>> to define enums through ExternalStructure.
<br>> >>>
<br>> >>> Like this:
<br>> >>>
<br>> >>> ExternalTypeAlias subclass: #MyEnumBar
<br>> >>> instanceVariableNames: ''
<br>> >>> classVariableNames: ''
<br>> >>> poolDictionaries: 'HDF5Pool'
<br>> >>> category: 'HDF5'
<br>> >>>
<br>> >>> MyEnumBar class >> #poolFields
<br>> >>> "
<br>> >>> self defineFields.
<br>> >>> "
<br>> >>> ^ #(
<br>> >>> (beg HDF5_BEG)
<br>> >>> (mid HDF5_MID)
<br>> >>> (end HDF5_END)
<br>> >>> )
<br>> >>>
<br>> >>> Note that HDF5_BEG etc. are from the HDF5Pool, which is managed via
<br>> >>> external-pool definitions. See class comment in ExternalPool to get
<br>> started.
<br>> >>>
<br>> >>> Note that #poolFields would translate to class-side methods as you
<br>> >>> suggested.
<br>> >>>
<br>> >>> > If I use a simpler alias, where are those ids going to?
<br>> >>>
<br>> >>> Please don't. See above. :-)
<br>> >>>
<br>> >>> > IMO we cannot keep status quo in the plugin, we gotta to make the
<br>> >>> inputs/outputs behave consistently.
<br>> >>>
<br>> >>> Absolutely!
<br>> >>>
<br>> >>> > - should a function expecting an ExternalTypeAlias of atomic type
<br>> >>> accept an immediate value of compatible type?
<br>> >>>
<br>> >>> Considering type safety, I think not.
<br>> >>>
<br>> >>> Well ... since this is a convenience issue and thus not about saving
<br>> >>> run-time cost ... What about adding a callback into the image to react
<br>> on
<br>> >>> FFIErrorCoercionFailed? Maybe the selector for such a callback could
<br>> be
<br>> >>> stored in ExternalLibraryFunction since this is accessible to the
<br>> plugin
<br>> >>> anyway?
<br>> >>>
<br>> >>>
<br>> >>>
<br>> >>> ... would it be possible? Like that #doesNotUnderstand: callback?
<br>> >>>
<br>> >>> I would love to do ad-hoc packaging of arguments. (Also thinking about
<br>> >>> FFI-Callback. But ignore me here :)
<br>> >>>
<br>> >>> > - should a function returning an ExternalTypeAlias of atomic type
<br>> >>> instantiate the Alias?
<br>> >>>
<br>> >>> Yes, please! See above.
<br>> >>>
<br>> >>> If you want to trade type safety in for a performance gain, just use a
<br>> >>> "lightweight alias" as explained above. And package that return value
<br>> >>> manually.
<br>> >>>
<br>> >>> Best,
<br>> >>> Marcel
<br>> >>>
<br>> >>>
<br>> >>> Am 18.06.2020 21:03:53 schrieb Nicolas Cellier <>
<br>> >>> nicolas.cellier.aka.nice@gmail.com>:
<br>> >>>
<br>> >>> Hi all,
<br>> >>> following the question of Marcel about usage of ExternalTypeAlias:
<br>> >>>
<br>> >>> Type alias are subclasses of ExternalStructure.
<br>> >>> I used them in HDF5 because there was no other means to define an
<br>> alias
<br>> >>> at the time.
<br>> >>> Now we have another mean by adding a class side method in ExternalType
<br>> >>> (I would have rather imagined the usage of some annotation to get a
<br>> >>> declarative style)
<br>> >>>
<br>> >>> This has one advantage: type safety. You cannot pass a Foo to a
<br>> function
<br>> >>> expecting a Bar, even if they both alias int...
<br>> >>>
<br>> >>> But this has one major drawback: with current FFI, you cannot pass a
<br>> >>> Smalltalk Integer, to a method expecting a Bar, even if Bar is an
<br>> alias for
<br>> >>> an integer type...
<br>> >>>
<br>> >>> Thus, you have to wrap every Bar argument into a Bar instance...
<br>> >>> Gasp, this is going too far...
<br>> >>>
<br>> >>> What about functions returning such alias?
<br>> >>> Function returning struct by value allocate a struct, but it's not the
<br>> >>> case here, FFI plugin is clever enough to recognize an atomic type
<br>> alias
<br>> >>> and simply return a Smalltalk Integer...
<br>> >>> So it's not Bar all the way down, I have to manually wrap Bar...
<br>> >>> This is not consistent.
<br>> >>> If we return an int, we must accept an int, otherwise, we cannot chain
<br>> >>> FFI calls...
<br>> >>>
<br>> >>> I could use the lightweight alias instead, but there was another usage
<br>> >>> where it was convenient to use ExternalTypeAlias: enum.
<br>> >>> Indeed, I simply defined all enum symbols as class side method. For
<br>> >>> example for enum bar { beg=0, mid=1, end=2 }; I simply create a type
<br>> Bar
<br>> >>> aliasing int and having class side methods beg ^0, mid ^1, end ^2.
<br>> >>> This way, I normally can pass a Bar mid to an external function.
<br>> >>> (currently, we must defne mid ^Bar new value: 1; yourself)
<br>> >>>
<br>> >>> If I use a simpler alias, where are those ids going to?
<br>> >>>
<br>> >>> IMO we cannot keep statu quo in the plugin, we gotta to make the
<br>> >>> inputs/outputs behave consistently.
<br>> >>> The choice is this one:
<br>> >>> - should a function expecting an ExternalTypeAlias of atomic type
<br>> accept
<br>> >>> an immediate value of compatible type? (the permissive implementation)
<br>> >>> - should a function returning an ExternalTypeAlias of atomic type
<br>> >>> instantiate the Alias? (the typesafe implementation, with higher
<br>> runtime
<br>> >>> cost due to pressure on GC)
<br>> >>>
<br>> >>>
<br>> >>>
<br>> >>>
<br>> >>>
<br>> >>>
<br>> >>>
<br>> >>
<br>> >
<br>>
<br>>
<br>> Le ven. 19 juin 2020 à 16:16, Marcel Taeumel <marcel.taeumel@hpi.de> a
<br>> écrit :
<br>>
<br>>>
<br>>>
<br>>>
<br>>>
<br>>>
<br>>>
<br>>>
<br>>> > Err, I move this thread to Opensmalltalk VM dev, because it is not
<br>>> Squeak specific!
<br>>>
<br>>> Absolutely. :-)
<br>>>
<br>>> (On a side note: I think that ExternalData should always have a pointer
<br>>> type in its "type" field unless "handle" is a ByteArray instead of
<br>>> ExternalAddress)
<br>>>
<br>>> Let me disagree here...
<br>> The type should better describe the contents of the handle (be it an
<br>> ExternalAddress or a zone of object memory (ByteArray)).
<br>> Typically, if you have an external (global) variable of type foo (extern
<br>> foo my_var;) then the natural type is foo.
<br>> Having foo*, would mean that we handle an array of foo*, or a pointer to
<br>> foo* (foo**).
<br>> This way, we make no difference between type surrogates
<br>> (ExternalStruct/ExternalTypeAlias) and other atomic cases.
<br>>
<br>> (On another side note: I also think that "handle" in ExternalData should
<br>>> never ever be an atomic Smalltalk object (Integer or Float) but always
<br>>> either ByteArray or ExternalAddress. ... which makes it different from what
<br>>> "handle" can be in ExternalStructs that represent type aliases. :-)
<br>>>
<br>>> Yes!
<br>> It could be an ExternalAddress, or a zone of object memory (ByteArray) or
<br>> an Alien as we may want to support that too.
<br>> I'd be inclined to support doubleByte, word, and doubleWord arrays if the
<br>> size of atomic type matches
<br>> (Typically passing a DoubleByteArray, WordArray, a Float32Array, a
<br>> Float64Array to a short */int */float*/double*).
<br>>
<br>> Best,
<br>>> Marcel
<br>>>
<br>>>
<br>>>
<br>>> Am 19.06.2020 16:03:10 schrieb Nicolas Cellier <><br>>> nicolas.cellier.aka.nice@gmail.com>:
<br>>> Err, I move this thread to Opensmalltalk VM dev, because it is not Squeak
<br>>>
<br>>> specific!
<br>>>
<br>>>
<br>>>
<br>>>
<br>>>
<br>>>
<br>>>
<br>>> Le ven. 19 juin 2020 à 15:10, Nicolas Cellier <>
<br>>> nicolas.cellier.aka.nice@gmail.com> a écrit :
<br>>>
<br>>>
<br>>>
<br>>> > Hi Marcel,
<br>>>
<br>>> > thanks for your vote. Anyone else?
<br>>>
<br>>> >
<br>>>
<br>>> > Le ven. 19 juin 2020 à 09:30, Marcel Taeumel a
<br>>>
<br>>> > écrit :
<br>>>
<br>>> >
<br>>>
<br>>> >> Hi Nicolas, hi all! :-)
<br>>>
<br>>> >>
<br>>>
<br>>> >> > Type alias are subclasses of ExternalStructure.
<br>>>
<br>>> >>
<br>>>
<br>>> >> And with it, they get their own instance of ExternalType, which is
<br>>>
<br>>> >> managed in the classVar StructTypes. Let's call them struct type.
<br>>>
<br>>> >>
<br>>>
<br>>> >> > Now we have another mean by adding a class side method in
<br>>> ExternalType.
<br>>>
<br>>> >>
<br>>>
<br>>> >> That kind of aliasing is for compile-time type referencing only --
<br>>> such
<br>>>
<br>>> >> as in FFI calls (i.e. pragmas) or struct-field definitions
<br>>>
<br>>> >> (i.e. #fields). (And soon if I have the first version of
<br>>>
<br>>> >> FFI-Callback to show you :)
<br>>>
<br>>> >>
<br>>>
<br>>> >> Because those aliases become fully transparent after compiling the
<br>>> method
<br>>>
<br>>> >> for the FFI call into an instance of ExternalLibraryFunction and after
<br>>>
<br>>> >> compiling the field specs of external structures into compiledSpec for
<br>>>
<br>>> >> struct types.
<br>>>
<br>>> >>
<br>>>
<br>>> >> For example, on my machine in 32-bit Squeak, a 'size_t' becomes a
<br>>> 'long',
<br>>>
<br>>> >> which is Squeak FFI term for 'int'. :-) See #ffiLongVsInt. And
<br>>> 'int32_t'
<br>>>
<br>>> >> also becomes a 'long'. So, it just translates C vocabulary into Squeak
<br>>> FFI
<br>>>
<br>>> >> vocabulary. Nothing more. Nothing less.
<br>>>
<br>>> >>
<br>>>
<br>>> >> Having that, please do not use class-side methods in ExternalType to
<br>>>
<br>>> >> alias a (complex) struct type. Use them only for renaming atomic
<br>>> types.
<br>>>
<br>>> >> Please. We may want to add checks to enforce that.
<br>>>
<br>>> >>
<br>>>
<br>>> >> I suppose that this discussion focuses on type aliases that are
<br>>>
<br>>> >> represented as subclasses of ExternalStructure (or ExternalTypeAlias)
<br>>> and
<br>>>
<br>>> >> thus get their own struct type. So, let's ignore this other mechanism
<br>>> for
<br>>>
<br>>> >> now.
<br>>>
<br>>> >>
<br>>>
<br>>> >> > This has one advantage: type safety. You cannot pass a Foo to a
<br>>>
<br>>> >> function expecting a Bar, even if they both alias int...
<br>>>
<br>>> >>
<br>>>
<br>>> >> Yes! So, we are now talking about type aliases to atomic types. The
<br>>>
<br>>> >> struct type you get when aliasing a 'char' as Foo, for example, looks
<br>>> like
<br>>>
<br>>> >> this:
<br>>>
<br>>> >>
<br>>>
<br>>> >> (...forgive my "creative" representation of WordArray here...)
<br>>>
<br>>> >>
<br>>>
<br>>> >> compiledSpec: 0A 04 00 01
<br>>>
<br>>> >> referentClass: Foo
<br>>>
<br>>> >>
<br>>>
<br>>> >> How does the atomic type for 'char' look like?
<br>>>
<br>>> >>
<br>>>
<br>>> >> compiledSpec: 0A 04 00 01
<br>>>
<br>>> >> referentClass: nil
<br>>>
<br>>> >>
<br>>>
<br>>> >> Consequently, argument coercing will fail if you pass a 'char' when a
<br>>>
<br>>> >> 'Foo' is expected. Because the referentClass does not match --- even
<br>>> if the
<br>>>
<br>>> >> compiledSpec does match.
<br>>>
<br>>> >>
<br>>>
<br>>> >> > But this has one major drawback: with current FFI, you cannot pass a
<br>>>
<br>>> >> Smalltalk Integer, to a method expecting a Bar, even if Bar is an
<br>>> alias for
<br>>>
<br>>> >> an integer type...
<br>>>
<br>>> >>
<br>>>
<br>>> >> That's correct. If the origin of such an argument is from within the
<br>>>
<br>>> >> image, you have to wrap it. But when returned from another call ...
<br>>> well
<br>>>
<br>>> >> ... that wrapping should have happened in the plugin ... But read on!
<br>>> :-)
<br>>>
<br>>> >>
<br>>>
<br>>> >> > FFI plugin is clever enough to recognize an atomic type alias and
<br>>>
<br>>> >> simply return a Smalltalk Integer...
<br>>>
<br>>> >> > So it's not Bar all the way down, I have to manually wrap Bar...
<br>>>
<br>>> >> > This is not consistent.
<br>>>
<br>>> >>
<br>>>
<br>>> >> Uhh! That's a bug. The FFI Plugin must take a look at referentClass
<br>>>
<br>>> >> because it does so for argument coercing.
<br>>>
<br>>> >>
<br>>>
<br>>> >> Now I understand why the code generation of struct-field accessors was
<br>>> so
<br>>>
<br>>> >> apparently broken for me. I changed that so that having an alias type
<br>>> as
<br>>>
<br>>> >> part of a larger struct will automatically wrap that for you into the
<br>>> alias
<br>>>
<br>>> >> structure. :-) Even if you are just aliasing a plain 'int' ... or
<br>>> 'long'
<br>>>
<br>>> >> ... Ha ha. ;-) #ffiLongVsInt.
<br>>>
<br>>> >>
<br>>>
<br>>> >> It should be "Bar all the way down". Definitely.
<br>>>
<br>>> >>
<br>>>
<br>>> >> > If we return an int, we must accept an int, otherwise, we cannot
<br>>> chain
<br>>>
<br>>> >> FFI calls...
<br>>>
<br>>> >>
<br>>>
<br>>> >> Well, not for type aliases. I would like keep the path of type safety
<br>>>
<br>>> >> here, you mentioned above.
<br>>>
<br>>> >>
<br>>>
<br>>> >> Just fix the FFI plugin to respect referentClass when packaging the
<br>>>
<br>>> >> return value.
<br>>>
<br>>> >>
<br>>>
<br>>> >> > I could use the lightweight alias instead, [...]
<br>>>
<br>>> >>
<br>>>
<br>>> >> Please don't. See above. Those "lightweight alias" are for renaming
<br>>>
<br>>> >> atomic types only. Let's keep it simple and try to address you concern
<br>>> here
<br>>>
<br>>> >> with struct types and ExternalTypeAlias. :-)
<br>>>
<br>>> >>
<br>>>
<br>>> >> > there was another usage where it was convenient to use
<br>>>
<br>>> >> ExternalTypeAlias: enum.
<br>>>
<br>>> >> > Indeed, I simply defined all enum symbols as class side method.
<br>>>
<br>>> >>
<br>>>
<br>>> >> I like that! It reads like a next step for ExternalPool. Once you have
<br>>>
<br>>> >> extracted the constant values from header files, and once you have
<br>>> those
<br>>>
<br>>> >> values for your platform in classVars in your external pool, use that
<br>>> pool
<br>>>
<br>>> >> to define enums through ExternalStructure.
<br>>>
<br>>> >>
<br>>>
<br>>> >> Like this:
<br>>>
<br>>> >>
<br>>>
<br>>> >> ExternalTypeAlias subclass: #MyEnumBar
<br>>>
<br>>> >> instanceVariableNames: ''
<br>>>
<br>>> >> classVariableNames: ''
<br>>>
<br>>> >> poolDictionaries: 'HDF5Pool'
<br>>>
<br>>> >> category: 'HDF5'
<br>>>
<br>>> >>
<br>>>
<br>>> >> MyEnumBar class >> #poolFields
<br>>>
<br>>> >> "
<br>>>
<br>>> >> self defineFields.
<br>>>
<br>>> >> "
<br>>>
<br>>> >> ^ #(
<br>>>
<br>>> >> (beg HDF5_BEG)
<br>>>
<br>>> >> (mid HDF5_MID)
<br>>>
<br>>> >> (end HDF5_END)
<br>>>
<br>>> >> )
<br>>>
<br>>> >>
<br>>>
<br>>> >> Note that HDF5_BEG etc. are from the HDF5Pool, which is managed via
<br>>>
<br>>> >> external-pool definitions. See class comment in ExternalPool to get
<br>>> started.
<br>>>
<br>>> >>
<br>>>
<br>>> >> Note that #poolFields would translate to class-side methods as you
<br>>>
<br>>> >> suggested.
<br>>>
<br>>> >>
<br>>>
<br>>> >> > If I use a simpler alias, where are those ids going to?
<br>>>
<br>>> >>
<br>>>
<br>>> >> Please don't. See above. :-)
<br>>>
<br>>> >>
<br>>>
<br>>> >> > IMO we cannot keep status quo in the plugin, we gotta to make the
<br>>>
<br>>> >> inputs/outputs behave consistently.
<br>>>
<br>>> >>
<br>>>
<br>>> >> Absolutely!
<br>>>
<br>>> >>
<br>>>
<br>>> >> > - should a function expecting an ExternalTypeAlias of atomic type
<br>>>
<br>>> >> accept an immediate value of compatible type?
<br>>>
<br>>> >>
<br>>>
<br>>> >> Considering type safety, I think not.
<br>>>
<br>>> >>
<br>>>
<br>>> >> Well ... since this is a convenience issue and thus not about saving
<br>>>
<br>>> >> run-time cost ... What about adding a callback into the image to react
<br>>> on
<br>>>
<br>>> >> FFIErrorCoercionFailed? Maybe the selector for such a callback could
<br>>> be
<br>>>
<br>>> >> stored in ExternalLibraryFunction since this is accessible to the
<br>>> plugin
<br>>>
<br>>> >> anyway?
<br>>>
<br>>> >>
<br>>>
<br>>> >>
<br>>>
<br>>> >>
<br>>>
<br>>> >> ... would it be possible? Like that #doesNotUnderstand: callback?
<br>>>
<br>>> >>
<br>>>
<br>>> >> I would love to do ad-hoc packaging of arguments. (Also thinking about
<br>>>
<br>>> >> FFI-Callback. But ignore me here :)
<br>>>
<br>>> >>
<br>>>
<br>>> >> > - should a function returning an ExternalTypeAlias of atomic type
<br>>>
<br>>> >> instantiate the Alias?
<br>>>
<br>>> >>
<br>>>
<br>>> >> Yes, please! See above.
<br>>>
<br>>> >>
<br>>>
<br>>> >> If you want to trade type safety in for a performance gain, just use a
<br>>>
<br>>> >> "lightweight alias" as explained above. And package that return value
<br>>>
<br>>> >> manually.
<br>>>
<br>>> >>
<br>>>
<br>>> >> Best,
<br>>>
<br>>> >> Marcel
<br>>>
<br>>> >>
<br>>>
<br>>> >> Am 18.06.2020 21:03:53 schrieb Nicolas Cellier <>
<br>>> >> nicolas.cellier.aka.nice@gmail.com>:
<br>>>
<br>>> >> Hi all,
<br>>>
<br>>> >> following the question of Marcel about usage of ExternalTypeAlias:
<br>>>
<br>>> >>
<br>>>
<br>>> >> Type alias are subclasses of ExternalStructure.
<br>>>
<br>>> >> I used them in HDF5 because there was no other means to define an
<br>>> alias
<br>>>
<br>>> >> at the time.
<br>>>
<br>>> >> Now we have another mean by adding a class side method in ExternalType
<br>>> (I
<br>>>
<br>>> >> would have rather imagined the usage of some annotation to get a
<br>>>
<br>>> >> declarative style)
<br>>>
<br>>> >>
<br>>>
<br>>> >> This has one advantage: type safety. You cannot pass a Foo to a
<br>>> function
<br>>>
<br>>> >> expecting a Bar, even if they both alias int...
<br>>>
<br>>> >>
<br>>>
<br>>> >> But this has one major drawback: with current FFI, you cannot pass a
<br>>>
<br>>> >> Smalltalk Integer, to a method expecting a Bar, even if Bar is an
<br>>> alias for
<br>>>
<br>>> >> an integer type...
<br>>>
<br>>> >>
<br>>>
<br>>> >> Thus, you have to wrap every Bar argument into a Bar instance...
<br>>>
<br>>> >> Gasp, this is going too far...
<br>>>
<br>>> >>
<br>>>
<br>>> >> What about functions returning such alias?
<br>>>
<br>>> >> Function returning struct by value allocate a struct, but it's not the
<br>>>
<br>>> >> case here, FFI plugin is clever enough to recognize an atomic type
<br>>> alias
<br>>>
<br>>> >> and simply return a Smalltalk Integer...
<br>>>
<br>>> >> So it's not Bar all the way down, I have to manually wrap Bar...
<br>>>
<br>>> >> This is not consistent.
<br>>>
<br>>> >> If we return an int, we must accept an int, otherwise, we cannot chain
<br>>>
<br>>> >> FFI calls...
<br>>>
<br>>> >>
<br>>>
<br>>> >> I could use the lightweight alias instead, but there was another usage
<br>>>
<br>>> >> where it was convenient to use ExternalTypeAlias: enum.
<br>>>
<br>>> >> Indeed, I simply defined all enum symbols as class side method. For
<br>>>
<br>>> >> example for enum bar { beg=0, mid=1, end=2 }; I simply create a type
<br>>> Bar
<br>>>
<br>>> >> aliasing int and having class side methods beg ^0, mid ^1, end ^2.
<br>>>
<br>>> >> This way, I normally can pass a Bar mid to an external function.
<br>>>
<br>>> >> (currently, we must defne mid ^Bar new value: 1; yourself)
<br>>>
<br>>> >>
<br>>>
<br>>> >> If I use a simpler alias, where are those ids going to?
<br>>>
<br>>> >>
<br>>>
<br>>> >> IMO we cannot keep statu quo in the plugin, we gotta to make the
<br>>>
<br>>> >> inputs/outputs behave consistently.
<br>>>
<br>>> >> The choice is this one:
<br>>>
<br>>> >> - should a function expecting an ExternalTypeAlias of atomic type
<br>>> accept
<br>>>
<br>>> >> an immediate value of compatible type? (the permissive implementation)
<br>>>
<br>>> >> - should a function returning an ExternalTypeAlias of atomic type
<br>>>
<br>>> >> instantiate the Alias? (the typesafe implementation, with higher
<br>>> runtime
<br>>>
<br>>> >> cost due to pressure on GC)
<br>>>
<br>>> >>
<br>>>
<br>>> >>
<br>>>
<br>>> >>
<br>>>
<br>>> >>
<br>>>
<br>>> Err, I move this thread to Opensmalltalk VM dev, because it is not Squeak
<br>>> specific!
<br>>>
<br>>>
<br>>>
<br>>> Le ven. 19 juin 2020 à 15:10, Nicolas Cellier <><br>>> nicolas.cellier.aka.nice@gmail.com> a écrit :
<br>>>
<br>>>> Hi Marcel,
<br>>>> thanks for your vote. Anyone else?
<br>>>>
<br>>>> Le ven. 19 juin 2020 à 09:30, Marcel Taeumel <marcel.taeumel@hpi.de> a
<br>>>> écrit :
<br>>>>
<br>>>>>
<br>>>>>
<br>>>>> Hi Nicolas, hi all! :-)
<br>>>>>
<br>>>>> > Type alias are subclasses of ExternalStructure.
<br>>>>>
<br>>>>> And with it, they get their own instance of ExternalType, which is
<br>>>>> managed in the classVar StructTypes. Let's call them struct type.
<br>>>>>
<br>>>>> > Now we have another mean by adding a class side method in
<br>>>>> ExternalType.
<br>>>>>
<br>>>>> That kind of aliasing is for compile-time type referencing only -- such
<br>>>>> as in FFI calls (i.e. <apicall:...> pragmas) or struct-field definitions
<br>>>>> (i.e. #fields). (And soon <callback: ...=""> if I have the first version of
<br>>>>> FFI-Callback to show you :)
<br>>>>>
<br>>>>> Because those aliases become fully transparent after compiling the
<br>>>>> method for the FFI call into an instance of ExternalLibraryFunction and
<br>>>>> after compiling the field specs of external structures into compiledSpec
<br>>>>> for struct types.
<br>>>>>
<br>>>>> For example, on my machine in 32-bit Squeak, a 'size_t' becomes a
<br>>>>> 'long', which is Squeak FFI term for 'int'. :-) See #ffiLongVsInt. And
<br>>>>> 'int32_t' also becomes a 'long'. So, it just translates C vocabulary into
<br>>>>> Squeak FFI vocabulary. Nothing more. Nothing less.
<br>>>>>
<br>>>>> Having that, please do not use class-side methods in ExternalType to
<br>>>>> alias a (complex) struct type. Use them only for renaming atomic types.
<br>>>>> Please. We may want to add checks to enforce that.
<br>>>>>
<br>>>>> I suppose that this discussion focuses on type aliases that are
<br>>>>> represented as subclasses of ExternalStructure (or ExternalTypeAlias) and
<br>>>>> thus get their own struct type. So, let's ignore this other mechanism for
<br>>>>> now.
<br>>>>>
<br>>>>> > This has one advantage: type safety. You cannot pass a Foo to a
<br>>>>> function expecting a Bar, even if they both alias int...
<br>>>>>
<br>>>>> Yes! So, we are now talking about type aliases to atomic types. The
<br>>>>> struct type you get when aliasing a 'char' as Foo, for example, looks like
<br>>>>> this:
<br>>>>>
<br>>>>> (...forgive my "creative" representation of WordArray here...)
<br>>>>>
<br>>>>> compiledSpec: 0A 04 00 01
<br>>>>> referentClass: Foo
<br>>>>>
<br>>>>> How does the atomic type for 'char' look like?
<br>>>>>
<br>>>>> compiledSpec: 0A 04 00 01
<br>>>>> referentClass: nil
<br>>>>>
<br>>>>> Consequently, argument coercing will fail if you pass a 'char' when a
<br>>>>> 'Foo' is expected. Because the referentClass does not match --- even if the
<br>>>>> compiledSpec does match.
<br>>>>>
<br>>>>> > But this has one major drawback: with current FFI, you cannot pass a
<br>>>>> Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias for
<br>>>>> an integer type...
<br>>>>>
<br>>>>> That's correct. If the origin of such an argument is from within the
<br>>>>> image, you have to wrap it. But when returned from another call ... well
<br>>>>> ... that wrapping should have happened in the plugin ... But read on! :-)
<br>>>>>
<br>>>>> > FFI plugin is clever enough to recognize an atomic type alias and
<br>>>>> simply return a Smalltalk Integer...
<br>>>>> > So it's not Bar all the way down, I have to manually wrap Bar...
<br>>>>> > This is not consistent.
<br>>>>>
<br>>>>> Uhh! That's a bug. The FFI Plugin must take a look at referentClass
<br>>>>> because it does so for argument coercing.
<br>>>>>
<br>>>>> Now I understand why the code generation of struct-field accessors was
<br>>>>> so apparently broken for me. I changed that so that having an alias type as
<br>>>>> part of a larger struct will automatically wrap that for you into the alias
<br>>>>> structure. :-) Even if you are just aliasing a plain 'int' ... or 'long'
<br>>>>> ... Ha ha. ;-) #ffiLongVsInt.
<br>>>>>
<br>>>>> It should be "Bar all the way down". Definitely.
<br>>>>>
<br>>>>> > If we return an int, we must accept an int, otherwise, we cannot
<br>>>>> chain FFI calls...
<br>>>>>
<br>>>>> Well, not for type aliases. I would like keep the path of type safety
<br>>>>> here, you mentioned above.
<br>>>>>
<br>>>>> Just fix the FFI plugin to respect referentClass when packaging the
<br>>>>> return value.
<br>>>>>
<br>>>>> > I could use the lightweight alias instead, [...]
<br>>>>>
<br>>>>> Please don't. See above. Those "lightweight alias" are for renaming
<br>>>>> atomic types only. Let's keep it simple and try to address you concern here
<br>>>>> with struct types and ExternalTypeAlias. :-)
<br>>>>>
<br>>>>> > there was another usage where it was convenient to use
<br>>>>> ExternalTypeAlias: enum.
<br>>>>> > Indeed, I simply defined all enum symbols as class side method.
<br>>>>>
<br>>>>> I like that! It reads like a next step for ExternalPool. Once you have
<br>>>>> extracted the constant values from header files, and once you have those
<br>>>>> values for your platform in classVars in your external pool, use that pool
<br>>>>> to define enums through ExternalStructure.
<br>>>>>
<br>>>>> Like this:
<br>>>>>
<br>>>>> ExternalTypeAlias subclass: #MyEnumBar
<br>>>>> instanceVariableNames: ''
<br>>>>> classVariableNames: ''
<br>>>>> poolDictionaries: 'HDF5Pool'
<br>>>>> category: 'HDF5'
<br>>>>>
<br>>>>> MyEnumBar class >> #poolFields
<br>>>>>    "
<br>>>>>    self defineFields.
<br>>>>>    "
<br>>>>>    ^ #(
<br>>>>>       (beg HDF5_BEG)
<br>>>>>       (mid HDF5_MID)
<br>>>>>       (end HDF5_END)
<br>>>>>    )
<br>>>>>
<br>>>>> Note that HDF5_BEG etc. are from the HDF5Pool, which is managed via
<br>>>>> external-pool definitions. See class comment in ExternalPool to get started.
<br>>>>>
<br>>>>> Note that #poolFields would translate to class-side methods as you
<br>>>>> suggested.
<br>>>>>
<br>>>>> > If I use a simpler alias, where are those ids going to?
<br>>>>>
<br>>>>> Please don't. See above. :-)
<br>>>>>
<br>>>>> > IMO we cannot keep status quo in the plugin, we gotta to make the
<br>>>>> inputs/outputs behave consistently.
<br>>>>>
<br>>>>> Absolutely!
<br>>>>>
<br>>>>> > - should a function expecting an ExternalTypeAlias of atomic type
<br>>>>> accept an immediate value of compatible type?
<br>>>>>
<br>>>>> Considering type safety, I think not.
<br>>>>>
<br>>>>> Well ... since this is a convenience issue and thus not about saving
<br>>>>> run-time cost ... What about adding a callback into the image to react on
<br>>>>> FFIErrorCoercionFailed? Maybe the selector for such a callback could be
<br>>>>> stored in ExternalLibraryFunction since this is accessible to the plugin
<br>>>>> anyway?
<br>>>>>
<br>>>>> <apicall: void="" 'foo'="" (myint="" yourint="" in)="" iffailcoercevia:="" #wrapint:="">
<br>>>>>
<br>>>>> ... would it be possible? Like that #doesNotUnderstand: callback?
<br>>>>>
<br>>>>> I would love to do ad-hoc packaging of arguments. (Also thinking about
<br>>>>> FFI-Callback. But ignore me here :)
<br>>>>>
<br>>>>> > - should a function returning an ExternalTypeAlias of atomic type
<br>>>>> instantiate the Alias?
<br>>>>>
<br>>>>> Yes, please! See above.
<br>>>>>
<br>>>>> If you want to trade type safety in for a performance gain, just use a
<br>>>>> "lightweight alias" as explained above. And package that return value
<br>>>>> manually.
<br>>>>>
<br>>>>> Best,
<br>>>>> Marcel
<br>>>>>
<br>>>>>
<br>>>>>
<br>>>>> Am 18.06.2020 21:03:53 schrieb Nicolas Cellier <><br>>>>> nicolas.cellier.aka.nice@gmail.com>:
<br>>>>>
<br>>>>>
<br>>>>> Hi all,
<br>>>>> following the question of Marcel about usage of ExternalTypeAlias:
<br>>>>>
<br>>>>> Type alias are subclasses of ExternalStructure.
<br>>>>> I used them in HDF5 because there was no other means to define an alias
<br>>>>> at the time.
<br>>>>> Now we have another mean by adding a class side method in ExternalType
<br>>>>> (I would have rather imagined the usage of some annotation to get a
<br>>>>> declarative style)
<br>>>>>
<br>>>>> This has one advantage: type safety. You cannot pass a Foo to a
<br>>>>> function expecting a Bar, even if they both alias int...
<br>>>>>
<br>>>>> But this has one major drawback: with current FFI, you cannot pass a
<br>>>>> Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias for
<br>>>>> an integer type...
<br>>>>>
<br>>>>> Thus, you have to wrap every Bar argument into a Bar instance...
<br>>>>> Gasp, this is going too far...
<br>>>>>
<br>>>>> What about functions returning such alias?
<br>>>>> Function returning struct by value allocate a struct, but it's not the
<br>>>>> case here, FFI plugin is clever enough to recognize an atomic type alias
<br>>>>> and simply return a Smalltalk Integer...
<br>>>>> So it's not Bar all the way down, I have to manually wrap Bar...
<br>>>>> This is not consistent.
<br>>>>> If we return an int, we must accept an int, otherwise, we cannot chain
<br>>>>> FFI calls...
<br>>>>>
<br>>>>> I could use the lightweight alias instead, but there was another usage
<br>>>>> where it was convenient to use ExternalTypeAlias: enum.
<br>>>>> Indeed, I simply defined all enum symbols as class side method. For
<br>>>>> example for enum bar { beg=0, mid=1, end=2 }; I simply create a type Bar
<br>>>>> aliasing int and having class side methods beg ^0, mid ^1, end ^2.
<br>>>>> This way, I normally can pass a Bar mid to an external function.
<br>>>>> (currently, we must defne mid ^Bar new value: 1; yourself)
<br>>>>>
<br>>>>> If I use a simpler alias, where are those ids going to?
<br>>>>>
<br>>>>> IMO we cannot keep statu quo in the plugin, we gotta to make the
<br>>>>> inputs/outputs behave consistently.
<br>>>>>  The choice is this one:
<br>>>>> - should a function expecting an ExternalTypeAlias of atomic type
<br>>>>> accept an immediate value of compatible type? (the permissive
<br>>>>> implementation)
<br>>>>> - should a function returning an ExternalTypeAlias of atomic type
<br>>>>> instantiate the Alias? (the typesafe implementation, with higher runtime
<br>>>>> cost due to pressure on GC)
<br>>>>>
<br>>>>>
<br>>>>>
<br>>>>>
<br>>>>>
<br>>>>>
<br>>>>>
<br>>>>>
<br>>>>>
<br>>>>>
<br>>>>
<br>>>>
<br>>>
<br>>>
<br>>
<br><div dir="ltr"><div>Well, my mental model is this one:</div><div><br></div><div>ExternalData needs to refer to an area of memory, so as to be able to act as a pointer (single element or array does not really matter).</div><div>If this memory area lies in external heap, then we need an ExternalAddress as handle.</div><div>If this memory area lies in object memory, it must be a ByteArray.</div><div>We cannot handle pointers to object memory, because object memory is relocatable...</div><div>Well, unless we pin the object - that why we pass the ByteArray itself, not its address..<br></div><div><br></div><div>This is independent of how we interpret the contents.</div><div>IMO, it's more useful to avoid the pointer indirection in type, because we cannot handle double pointer currently...</div><div>Of course, the only pointers that the memory area could contain are pointers to external heap (or eventually pinned objects).</div><div><br></div><div>I'd like to have a 3rd possibility: pointing to a sub-region of a ByteArray (ByteArray + offset).</div><div>That may be very useful for emulating.</div><div>An example in Smallapack: pass a pointer to the imaginary part of a complex array.<br></div><div>Since real and imaginary parts are interleaved, just offsetting the pointer and using a stride of 2 elements does the trick when one wants to access real or imaginary part</div><div>(most lapack/blas routines are equipped to handle simple strides).<br></div><div>This is currently possible with ExternalAddress, but NOT when matrix data lies in ObjectMemory.</div><div>Another example would be if you wanted to implement an interpreter for a low level language... (emulate a processor, whatever).<br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le ven. 19 juin 2020 à 18:04, Marcel Taeumel <<a href="mailto:marcel.taeumel@hpi.de">marcel.taeumel@hpi.de</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left: 1px solid rgb(204,204,204);padding-left: 1ex;min-width: 500px"> <div><div id="gmail-m_-455482905057329652__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: rgb(0,0,0)">
<br>                                        <div>So, what about this:</div><div><br></div><div>ExternalData<@00F3A3EE, int></div><div><br></div><div>I think it could mean that you want to interpret the very pointer address itself as an integer. That is, you do not want to follow to where it points to.</div><div><br></div><div>Then what about this:</div><div><br></div><div>ExternalData<@00F3A3EE, double></div><div><br></div><div>Ha! An error, I suppose. Unless ... given 8-byte pointers, you can try to read a double out of it. :-D Just kidding. It should be an error.</div><div><br></div><div>.</div><div>.</div><div>.</div><div><br></div><div>> > (On a side note: I think that ExternalData should always have a pointer type in its "type" field unless "handle" is a ByteArray instead of ExternalAddress)</div><div>> Let me disagree here...</div><div><br></div><div>The examples I just wrote down in the previous messages let me realize why you did disagree. :-D</div><div><br></div><div>I hope this helps you with improving argument coercing and return value packaging in the FFI plugin. ^__^</div><div><br></div><div>Best,</div><div>Marcel</div><div></div><blockquote type="cite" style="border-left-style: solid;border-width: 1px;margin-top: 20px;margin-left: 0px;padding-left: 10px;min-width: 500px">
<br>                        <p style="color:rgb(170,170,170);margin-top:10px">Am 19.06.2020 17:53:21 schrieb Marcel Taeumel <<a href="mailto:marcel.taeumel@hpi.de" target="_blank">marcel.taeumel@hpi.de</a>>:</p><div style="font-family:Arial,Helvetica,sans-serif"><div id="gmail-m_-455482905057329652__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: rgb(0,0,0)">
<br>                                        > <span style="font-size: 13.3333px">then you would use int* and not int**</span><div><span style="font-size: 13.3333px"><br></span></div><div><span style="font-size: 13.3333px">My bad: "  then you would use int** and not int*  " Sorry for the noise -.-"</span></div><div></div><blockquote type="cite" style="border-left-style: solid;border-width: 1px;margin-top: 20px;margin-left: 0px;padding-left: 10px;min-width: 500px">
<br>                        <p style="color:rgb(170,170,170);margin-top:10px">Am 19.06.2020 17:52:21 schrieb Marcel Taeumel <<a href="mailto:marcel.taeumel@hpi.de" target="_blank">marcel.taeumel@hpi.de</a>>:</p><div style="font-family:Arial,Helvetica,sans-serif"><div id="gmail-m_-455482905057329652__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: rgb(0,0,0)">
<br>                                        > <span style="font-size: 13.3333px">If you want to make a pointer array out of it, change the type to int**</span><div><span style="font-size: 13.3333px"><br></span></div><div><span style="font-size: 13.3333px">Correction: If you happen to now, that this external address points to an array of pointers to int, then you would use int* and not int**. You cannot just "decide" what the data behind that external address should be. .. you know what I mean. ;-)</span></div><div><span style="font-size: 13.3333px"><br></span></div><div><span style="font-size: 13.3333px">best,</span></div><div><span style="font-size: 13.3333px">Marcel</span></div><div></div><blockquote type="cite" style="border-left-style: solid;border-width: 1px;margin-top: 20px;margin-left: 0px;padding-left: 10px;min-width: 500px">
<br>                        <p style="color:rgb(170,170,170);margin-top:10px">Am 19.06.2020 17:47:36 schrieb Marcel Taeumel <<a href="mailto:marcel.taeumel@hpi.de" target="_blank">marcel.taeumel@hpi.de</a>>:</p><div style="font-family:Arial,Helvetica,sans-serif"><div id="gmail-m_-455482905057329652__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: rgb(0,0,0)">
<br>                                        <div>Hi Nicolas.</div><div><br></div><div>Maybe one more thought on</div><div><br></div><div>ExternalData<handle, type></div><div><br></div><div>There is no need to have</div><div><br></div><div>ExternalData<1312301580, int></div><div><br></div><div>because it can just be 1312301580.</div><div><br></div><div>On the other hand:</div><div><br></div><div>ExternalData<#[ 12 34 56 78 ], int></div><div><br></div><div>Tells you that (1) this data wants to be interpreted as signed int and (2) this data is already in Squeak's object memory.</div><div><br></div><div>Finally:</div><div><br></div><div>ExternalData<@00F3A3EE, int*></div><div><br></div><div>Tells you that (1) this data wants to be interpreted as signed int and (2) this data is in external memory.</div><div><br></div><div>If you want to make an array out of it, make use of the "size" field in ExternalData.</div><div><br></div><div>If you want to make a pointer array out of it, change the type to int**</div><div><br></div><div>ExternalData<@00F3A3EE, int**></div><div><br></div><div>So, what does this mean then:</div><div><br></div><div>ExternalData<#[ 12 34 56 78 ], int*></div><div><br></div><div>Well, it tells you that there is a pointer to an int stored in a byte array. This comes very handy if you think about type aliases to pointer types such as "typedef int* INT_P". Because then it would be</div><div><br></div><div>ExternalData<#[ 12 34 56 78 ], INT_P></div><div><br></div><div>To where this pointer points to? Probably garbage. I just made it up for this example. :-D</div><div><br></div><div>Best,</div><div>Marcel</div><div></div><blockquote type="cite" style="border-left-style: solid;border-width: 1px;margin-top: 20px;margin-left: 0px;padding-left: 10px;min-width: 500px">
<br>                        <p style="color:rgb(170,170,170);margin-top:10px">Am 19.06.2020 17:15:23 schrieb Marcel Taeumel <<a href="mailto:marcel.taeumel@hpi.de" target="_blank">marcel.taeumel@hpi.de</a>>:</p><div style="font-family:Arial,Helvetica,sans-serif"><div id="gmail-m_-455482905057329652__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: rgb(0,0,0)">
<br>                                        
<br>                                        
<br>                                            
<br>                                        
<br>                                        
<br>                                        <span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> The type should better describe the contents of the handle (be it an</span><br style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> ExternalAddress or a zone of object memory (ByteArray)).</span><br style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> Typically, if you have an external (global) variable of type foo (extern</span><br style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> foo my_var;) then the natural type is foo.</span><br style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> Having foo*, would mean that we handle an array of foo*, or a pointer to</span><br style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> foo* (foo**).</span><br style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> This way, we make no difference between type surrogates</span><br style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> (ExternalStruct/ExternalTypeAlias) and other atomic cases.</span><br><div></div>
<br>                                        
<br>                                        <div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">We have the "size" field in ExternalData encode whether "int*" points to a single or multiple things. No need to treat a global variable sitting in external memory any different here.  ... IMO :-)</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">I know, that this is my personal mental model for Squeak FFI to treat pointer types as things in external memory and non-pointer types as things in Squeak's object memory. The latter includes ByteArray and Integer and Float and also IntegerArray etc. Just not ExternalAddress.</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">This basically comes from the fact that, until now, the FFI plugin gave me a ByteArray in return when I told it to be "Foo", not "Foo*". So, non-pointer type means ByteArray. Pointer-type means ExternalAddress.</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">Type aliases are not really different here. Each alias as a corresponding struct type, which has again a non-pointer variant and a pointer variant.</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">Type aliases to pointer types are just special because they store the pointer address in a byte array. The corresponding non-pointer type will do fine for such aliases. Their pointer type is not relevant in general, I suppose.</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> Having foo*, would mean that we handle an array of foo*, or a pointer to </span><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">foo* (foo**).</span><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">Well, I have a simple idea on how to represent foo** (and other dimensions) without the FFI plugin needing to know. ... unless you are concerned about type safety for that, too. Then we would really need to encode that in the compiledSpec.</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">If not, ExternalData with foo** type will just answer ExternalData with foo* when asked via "at: 1" for example. :-)</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">> </span><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">or </span><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">an Alien as we may want to support that too.</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">I don't think that's necessary. Alien has its own call-out mechanism through the IA32ABI plugin and also a different layout/usage of ByteArray.</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">Best,</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">Marcel</span></div><blockquote type="cite" style="border-left-style: solid;border-width: 1px;margin-top: 20px;margin-left: 0px;padding-left: 10px;min-width: 500px">
<br>                        <p style="color:rgb(170,170,170);margin-top:10px">Am 19.06.2020 16:40:50 schrieb Nicolas Cellier <<a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>>:</p><div style="font-family:Arial,Helvetica,sans-serif"> Le ven. 19 juin 2020 à 16:16, Marcel Taeumel <u></u> a
<br><br>écrit :
<br><br>
<br><br>>
<br><br>> > Err, I move this thread to Opensmalltalk VM dev, because it is not
<br><br>> Squeak specific!
<br><br>>
<br><br>> Absolutely. :-)
<br><br>>
<br><br>> (On a side note: I think that ExternalData should always have a pointer
<br><br>> type in its "type" field unless "handle" is a ByteArray instead of
<br><br>> ExternalAddress)
<br><br>>
<br><br>> Let me disagree here...
<br><br>The type should better describe the contents of the handle (be it an
<br><br>ExternalAddress or a zone of object memory (ByteArray)).
<br><br>Typically, if you have an external (global) variable of type foo (extern
<br><br>foo my_var;) then the natural type is foo.
<br><br>Having foo*, would mean that we handle an array of foo*, or a pointer to
<br><br>foo* (foo**).
<br><br>This way, we make no difference between type surrogates
<br><br>(ExternalStruct/ExternalTypeAlias) and other atomic cases.
<br><br>
<br><br>(On another side note: I also think that "handle" in ExternalData should
<br><br>> never ever be an atomic Smalltalk object (Integer or Float) but always
<br><br>> either ByteArray or ExternalAddress. ... which makes it different from what
<br><br>> "handle" can be in ExternalStructs that represent type aliases. :-)
<br><br>>
<br><br>> Yes!
<br><br>It could be an ExternalAddress, or a zone of object memory (ByteArray) or
<br><br>an Alien as we may want to support that too.
<br><br>I'd be inclined to support doubleByte, word, and doubleWord arrays if the
<br><br>size of atomic type matches
<br><br>(Typically passing a DoubleByteArray, WordArray, a Float32Array, a
<br><br>Float64Array to a short */int */float*/double*).
<br><br>
<br><br>Best,
<br><br>> Marcel
<br><br>>
<br><br>> Am 19.06.2020 16:03:10 schrieb Nicolas Cellier <><br>> <a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>>:
<br><br>> Err, I move this thread to Opensmalltalk VM dev, because it is not Squeak
<br><br>> specific!
<br><br>>
<br><br>>
<br><br>>
<br><br>> Le ven. 19 juin 2020 à 15:10, Nicolas Cellier <>
<br><br>> <a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>> a écrit :
<br><br>>
<br><br>> > Hi Marcel,
<br><br>> > thanks for your vote. Anyone else?
<br><br>> >
<br><br>> > Le ven. 19 juin 2020 à 09:30, Marcel Taeumel a
<br><br>> > écrit :
<br><br>> >
<br><br>> >> Hi Nicolas, hi all! :-)
<br><br>> >>
<br><br>> >> > Type alias are subclasses of ExternalStructure.
<br><br>> >>
<br><br>> >> And with it, they get their own instance of ExternalType, which is
<br><br>> >> managed in the classVar StructTypes. Let's call them struct type.
<br><br>> >>
<br><br>> >> > Now we have another mean by adding a class side method in
<br><br>> ExternalType.
<br><br>> >>
<br><br>> >> That kind of aliasing is for compile-time type referencing only -- such
<br><br>> >> as in FFI calls (i.e. pragmas) or struct-field definitions
<br><br>> >> (i.e. #fields). (And soon if I have the first version of
<br><br>> >> FFI-Callback to show you :)
<br><br>> >>
<br><br>> >> Because those aliases become fully transparent after compiling the
<br><br>> method
<br><br>> >> for the FFI call into an instance of ExternalLibraryFunction and after
<br><br>> >> compiling the field specs of external structures into compiledSpec for
<br><br>> >> struct types.
<br><br>> >>
<br><br>> >> For example, on my machine in 32-bit Squeak, a 'size_t' becomes a
<br><br>> 'long',
<br><br>> >> which is Squeak FFI term for 'int'. :-) See #ffiLongVsInt. And
<br><br>> 'int32_t'
<br><br>> >> also becomes a 'long'. So, it just translates C vocabulary into Squeak
<br><br>> FFI
<br><br>> >> vocabulary. Nothing more. Nothing less.
<br><br>> >>
<br><br>> >> Having that, please do not use class-side methods in ExternalType to
<br><br>> >> alias a (complex) struct type. Use them only for renaming atomic types.
<br><br>> >> Please. We may want to add checks to enforce that.
<br><br>> >>
<br><br>> >> I suppose that this discussion focuses on type aliases that are
<br><br>> >> represented as subclasses of ExternalStructure (or ExternalTypeAlias)
<br><br>> and
<br><br>> >> thus get their own struct type. So, let's ignore this other mechanism
<br><br>> for
<br><br>> >> now.
<br><br>> >>
<br><br>> >> > This has one advantage: type safety. You cannot pass a Foo to a
<br><br>> >> function expecting a Bar, even if they both alias int...
<br><br>> >>
<br><br>> >> Yes! So, we are now talking about type aliases to atomic types. The
<br><br>> >> struct type you get when aliasing a 'char' as Foo, for example, looks
<br><br>> like
<br><br>> >> this:
<br><br>> >>
<br><br>> >> (...forgive my "creative" representation of WordArray here...)
<br><br>> >>
<br><br>> >> compiledSpec: 0A 04 00 01
<br><br>> >> referentClass: Foo
<br><br>> >>
<br><br>> >> How does the atomic type for 'char' look like?
<br><br>> >>
<br><br>> >> compiledSpec: 0A 04 00 01
<br><br>> >> referentClass: nil
<br><br>> >>
<br><br>> >> Consequently, argument coercing will fail if you pass a 'char' when a
<br><br>> >> 'Foo' is expected. Because the referentClass does not match --- even if
<br><br>> the
<br><br>> >> compiledSpec does match.
<br><br>> >>
<br><br>> >> > But this has one major drawback: with current FFI, you cannot pass a
<br><br>> >> Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias
<br><br>> for
<br><br>> >> an integer type...
<br><br>> >>
<br><br>> >> That's correct. If the origin of such an argument is from within the
<br><br>> >> image, you have to wrap it. But when returned from another call ...
<br><br>> well
<br><br>> >> ... that wrapping should have happened in the plugin ... But read on!
<br><br>> :-)
<br><br>> >>
<br><br>> >> > FFI plugin is clever enough to recognize an atomic type alias and
<br><br>> >> simply return a Smalltalk Integer...
<br><br>> >> > So it's not Bar all the way down, I have to manually wrap Bar...
<br><br>> >> > This is not consistent.
<br><br>> >>
<br><br>> >> Uhh! That's a bug. The FFI Plugin must take a look at referentClass
<br><br>> >> because it does so for argument coercing.
<br><br>> >>
<br><br>> >> Now I understand why the code generation of struct-field accessors was
<br><br>> so
<br><br>> >> apparently broken for me. I changed that so that having an alias type
<br><br>> as
<br><br>> >> part of a larger struct will automatically wrap that for you into the
<br><br>> alias
<br><br>> >> structure. :-) Even if you are just aliasing a plain 'int' ... or
<br><br>> 'long'
<br><br>> >> ... Ha ha. ;-) #ffiLongVsInt.
<br><br>> >>
<br><br>> >> It should be "Bar all the way down". Definitely.
<br><br>> >>
<br><br>> >> > If we return an int, we must accept an int, otherwise, we cannot
<br><br>> chain
<br><br>> >> FFI calls...
<br><br>> >>
<br><br>> >> Well, not for type aliases. I would like keep the path of type safety
<br><br>> >> here, you mentioned above.
<br><br>> >>
<br><br>> >> Just fix the FFI plugin to respect referentClass when packaging the
<br><br>> >> return value.
<br><br>> >>
<br><br>> >> > I could use the lightweight alias instead, [...]
<br><br>> >>
<br><br>> >> Please don't. See above. Those "lightweight alias" are for renaming
<br><br>> >> atomic types only. Let's keep it simple and try to address you concern
<br><br>> here
<br><br>> >> with struct types and ExternalTypeAlias. :-)
<br><br>> >>
<br><br>> >> > there was another usage where it was convenient to use
<br><br>> >> ExternalTypeAlias: enum.
<br><br>> >> > Indeed, I simply defined all enum symbols as class side method.
<br><br>> >>
<br><br>> >> I like that! It reads like a next step for ExternalPool. Once you have
<br><br>> >> extracted the constant values from header files, and once you have
<br><br>> those
<br><br>> >> values for your platform in classVars in your external pool, use that
<br><br>> pool
<br><br>> >> to define enums through ExternalStructure.
<br><br>> >>
<br><br>> >> Like this:
<br><br>> >>
<br><br>> >> ExternalTypeAlias subclass: #MyEnumBar
<br><br>> >> instanceVariableNames: ''
<br><br>> >> classVariableNames: ''
<br><br>> >> poolDictionaries: 'HDF5Pool'
<br><br>> >> category: 'HDF5'
<br><br>> >>
<br><br>> >> MyEnumBar class >> #poolFields
<br><br>> >> "
<br><br>> >> self defineFields.
<br><br>> >> "
<br><br>> >> ^ #(
<br><br>> >> (beg HDF5_BEG)
<br><br>> >> (mid HDF5_MID)
<br><br>> >> (end HDF5_END)
<br><br>> >> )
<br><br>> >>
<br><br>> >> Note that HDF5_BEG etc. are from the HDF5Pool, which is managed via
<br><br>> >> external-pool definitions. See class comment in ExternalPool to get
<br><br>> started.
<br><br>> >>
<br><br>> >> Note that #poolFields would translate to class-side methods as you
<br><br>> >> suggested.
<br><br>> >>
<br><br>> >> > If I use a simpler alias, where are those ids going to?
<br><br>> >>
<br><br>> >> Please don't. See above. :-)
<br><br>> >>
<br><br>> >> > IMO we cannot keep status quo in the plugin, we gotta to make the
<br><br>> >> inputs/outputs behave consistently.
<br><br>> >>
<br><br>> >> Absolutely!
<br><br>> >>
<br><br>> >> > - should a function expecting an ExternalTypeAlias of atomic type
<br><br>> >> accept an immediate value of compatible type?
<br><br>> >>
<br><br>> >> Considering type safety, I think not.
<br><br>> >>
<br><br>> >> Well ... since this is a convenience issue and thus not about saving
<br><br>> >> run-time cost ... What about adding a callback into the image to react
<br><br>> on
<br><br>> >> FFIErrorCoercionFailed? Maybe the selector for such a callback could be
<br><br>> >> stored in ExternalLibraryFunction since this is accessible to the
<br><br>> plugin
<br><br>> >> anyway?
<br><br>> >>
<br><br>> >>
<br><br>> >>
<br><br>> >> ... would it be possible? Like that #doesNotUnderstand: callback?
<br><br>> >>
<br><br>> >> I would love to do ad-hoc packaging of arguments. (Also thinking about
<br><br>> >> FFI-Callback. But ignore me here :)
<br><br>> >>
<br><br>> >> > - should a function returning an ExternalTypeAlias of atomic type
<br><br>> >> instantiate the Alias?
<br><br>> >>
<br><br>> >> Yes, please! See above.
<br><br>> >>
<br><br>> >> If you want to trade type safety in for a performance gain, just use a
<br><br>> >> "lightweight alias" as explained above. And package that return value
<br><br>> >> manually.
<br><br>> >>
<br><br>> >> Best,
<br><br>> >> Marcel
<br><br>> >>
<br><br>> >> Am 18.06.2020 21:03:53 schrieb Nicolas Cellier <>
<br><br>> >> <a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>>:
<br><br>> >> Hi all,
<br><br>> >> following the question of Marcel about usage of ExternalTypeAlias:
<br><br>> >>
<br><br>> >> Type alias are subclasses of ExternalStructure.
<br><br>> >> I used them in HDF5 because there was no other means to define an alias
<br><br>> >> at the time.
<br><br>> >> Now we have another mean by adding a class side method in ExternalType
<br><br>> (I
<br><br>> >> would have rather imagined the usage of some annotation to get a
<br><br>> >> declarative style)
<br><br>> >>
<br><br>> >> This has one advantage: type safety. You cannot pass a Foo to a
<br><br>> function
<br><br>> >> expecting a Bar, even if they both alias int...
<br><br>> >>
<br><br>> >> But this has one major drawback: with current FFI, you cannot pass a
<br><br>> >> Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias
<br><br>> for
<br><br>> >> an integer type...
<br><br>> >>
<br><br>> >> Thus, you have to wrap every Bar argument into a Bar instance...
<br><br>> >> Gasp, this is going too far...
<br><br>> >>
<br><br>> >> What about functions returning such alias?
<br><br>> >> Function returning struct by value allocate a struct, but it's not the
<br><br>> >> case here, FFI plugin is clever enough to recognize an atomic type
<br><br>> alias
<br><br>> >> and simply return a Smalltalk Integer...
<br><br>> >> So it's not Bar all the way down, I have to manually wrap Bar...
<br><br>> >> This is not consistent.
<br><br>> >> If we return an int, we must accept an int, otherwise, we cannot chain
<br><br>> >> FFI calls...
<br><br>> >>
<br><br>> >> I could use the lightweight alias instead, but there was another usage
<br><br>> >> where it was convenient to use ExternalTypeAlias: enum.
<br><br>> >> Indeed, I simply defined all enum symbols as class side method. For
<br><br>> >> example for enum bar { beg=0, mid=1, end=2 }; I simply create a type
<br><br>> Bar
<br><br>> >> aliasing int and having class side methods beg ^0, mid ^1, end ^2.
<br><br>> >> This way, I normally can pass a Bar mid to an external function.
<br><br>> >> (currently, we must defne mid ^Bar new value: 1; yourself)
<br><br>> >>
<br><br>> >> If I use a simpler alias, where are those ids going to?
<br><br>> >>
<br><br>> >> IMO we cannot keep statu quo in the plugin, we gotta to make the
<br><br>> >> inputs/outputs behave consistently.
<br><br>> >> The choice is this one:
<br><br>> >> - should a function expecting an ExternalTypeAlias of atomic type
<br><br>> accept
<br><br>> >> an immediate value of compatible type? (the permissive implementation)
<br><br>> >> - should a function returning an ExternalTypeAlias of atomic type
<br><br>> >> instantiate the Alias? (the typesafe implementation, with higher
<br><br>> runtime
<br><br>> >> cost due to pressure on GC)
<br><br>> >>
<br><br>> >>
<br><br>> >>
<br><br>> >>
<br><br>> Err, I move this thread to Opensmalltalk VM dev, because it is not Squeak
<br><br>> specific!
<br><br>>
<br><br>>
<br><br>>
<br><br>> Le ven. 19 juin 2020 à 15:10, Nicolas Cellier <><br>> <a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>> a écrit :
<br><br>>
<br><br>>> Hi Marcel,
<br><br>>> thanks for your vote. Anyone else?
<br><br>>>
<br><br>>> Le ven. 19 juin 2020 à 09:30, Marcel Taeumel <u></u> a
<br><br>>> écrit :
<br><br>>>
<br><br>>>>
<br><br>>>> Hi Nicolas, hi all! :-)
<br><br>>>>
<br><br>>>> > Type alias are subclasses of ExternalStructure.
<br><br>>>>
<br><br>>>> And with it, they get their own instance of ExternalType, which is
<br><br>>>> managed in the classVar StructTypes. Let's call them struct type.
<br><br>>>>
<br><br>>>> > Now we have another mean by adding a class side method in ExternalType.
<br><br>>>>
<br><br>>>> That kind of aliasing is for compile-time type referencing only -- such
<br><br>>>> as in FFI calls (i.e. <u></u> pragmas) or struct-field definitions
<br><br>>>> (i.e. #fields). (And soon <u></u> if I have the first version of
<br><br>>>> FFI-Callback to show you :)
<br><br>>>>
<br><br>>>> Because those aliases become fully transparent after compiling the
<br><br>>>> method for the FFI call into an instance of ExternalLibraryFunction and
<br><br>>>> after compiling the field specs of external structures into compiledSpec
<br><br>>>> for struct types.
<br><br>>>>
<br><br>>>> For example, on my machine in 32-bit Squeak, a 'size_t' becomes a
<br><br>>>> 'long', which is Squeak FFI term for 'int'. :-) See #ffiLongVsInt. And
<br><br>>>> 'int32_t' also becomes a 'long'. So, it just translates C vocabulary into
<br><br>>>> Squeak FFI vocabulary. Nothing more. Nothing less.
<br><br>>>>
<br><br>>>> Having that, please do not use class-side methods in ExternalType to
<br><br>>>> alias a (complex) struct type. Use them only for renaming atomic types.
<br><br>>>> Please. We may want to add checks to enforce that.
<br><br>>>>
<br><br>>>> I suppose that this discussion focuses on type aliases that are
<br><br>>>> represented as subclasses of ExternalStructure (or ExternalTypeAlias) and
<br><br>>>> thus get their own struct type. So, let's ignore this other mechanism for
<br><br>>>> now.
<br><br>>>>
<br><br>>>> > This has one advantage: type safety. You cannot pass a Foo to a
<br><br>>>> function expecting a Bar, even if they both alias int...
<br><br>>>>
<br><br>>>> Yes! So, we are now talking about type aliases to atomic types. The
<br><br>>>> struct type you get when aliasing a 'char' as Foo, for example, looks like
<br><br>>>> this:
<br><br>>>>
<br><br>>>> (...forgive my "creative" representation of WordArray here...)
<br><br>>>>
<br><br>>>> compiledSpec: 0A 04 00 01
<br><br>>>> referentClass: Foo
<br><br>>>>
<br><br>>>> How does the atomic type for 'char' look like?
<br><br>>>>
<br><br>>>> compiledSpec: 0A 04 00 01
<br><br>>>> referentClass: nil
<br><br>>>>
<br><br>>>> Consequently, argument coercing will fail if you pass a 'char' when a
<br><br>>>> 'Foo' is expected. Because the referentClass does not match --- even if the
<br><br>>>> compiledSpec does match.
<br><br>>>>
<br><br>>>> > But this has one major drawback: with current FFI, you cannot pass a
<br><br>>>> Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias for
<br><br>>>> an integer type...
<br><br>>>>
<br><br>>>> That's correct. If the origin of such an argument is from within the
<br><br>>>> image, you have to wrap it. But when returned from another call ... well
<br><br>>>> ... that wrapping should have happened in the plugin ... But read on! :-)
<br><br>>>>
<br><br>>>> > FFI plugin is clever enough to recognize an atomic type alias and
<br><br>>>> simply return a Smalltalk Integer...
<br><br>>>> > So it's not Bar all the way down, I have to manually wrap Bar...
<br><br>>>> > This is not consistent.
<br><br>>>>
<br><br>>>> Uhh! That's a bug. The FFI Plugin must take a look at referentClass
<br><br>>>> because it does so for argument coercing.
<br><br>>>>
<br><br>>>> Now I understand why the code generation of struct-field accessors was
<br><br>>>> so apparently broken for me. I changed that so that having an alias type as
<br><br>>>> part of a larger struct will automatically wrap that for you into the alias
<br><br>>>> structure. :-) Even if you are just aliasing a plain 'int' ... or 'long'
<br><br>>>> ... Ha ha. ;-) #ffiLongVsInt.
<br><br>>>>
<br><br>>>> It should be "Bar all the way down". Definitely.
<br><br>>>>
<br><br>>>> > If we return an int, we must accept an int, otherwise, we cannot chain
<br><br>>>> FFI calls...
<br><br>>>>
<br><br>>>> Well, not for type aliases. I would like keep the path of type safety
<br><br>>>> here, you mentioned above.
<br><br>>>>
<br><br>>>> Just fix the FFI plugin to respect referentClass when packaging the
<br><br>>>> return value.
<br><br>>>>
<br><br>>>> > I could use the lightweight alias instead, [...]
<br><br>>>>
<br><br>>>> Please don't. See above. Those "lightweight alias" are for renaming
<br><br>>>> atomic types only. Let's keep it simple and try to address you concern here
<br><br>>>> with struct types and ExternalTypeAlias. :-)
<br><br>>>>
<br><br>>>> > there was another usage where it was convenient to use
<br><br>>>> ExternalTypeAlias: enum.
<br><br>>>> > Indeed, I simply defined all enum symbols as class side method.
<br><br>>>>
<br><br>>>> I like that! It reads like a next step for ExternalPool. Once you have
<br><br>>>> extracted the constant values from header files, and once you have those
<br><br>>>> values for your platform in classVars in your external pool, use that pool
<br><br>>>> to define enums through ExternalStructure.
<br><br>>>>
<br><br>>>> Like this:
<br><br>>>>
<br><br>>>> ExternalTypeAlias subclass: #MyEnumBar
<br><br>>>> instanceVariableNames: ''
<br><br>>>> classVariableNames: ''
<br><br>>>> poolDictionaries: 'HDF5Pool'
<br><br>>>> category: 'HDF5'
<br><br>>>>
<br><br>>>> MyEnumBar class >> #poolFields
<br><br>>>>    "
<br><br>>>>    self defineFields.
<br><br>>>>    "
<br><br>>>>    ^ #(
<br><br>>>>       (beg HDF5_BEG)
<br><br>>>>       (mid HDF5_MID)
<br><br>>>>       (end HDF5_END)
<br><br>>>>    )
<br><br>>>>
<br><br>>>> Note that HDF5_BEG etc. are from the HDF5Pool, which is managed via
<br><br>>>> external-pool definitions. See class comment in ExternalPool to get started.
<br><br>>>>
<br><br>>>> Note that #poolFields would translate to class-side methods as you
<br><br>>>> suggested.
<br><br>>>>
<br><br>>>> > If I use a simpler alias, where are those ids going to?
<br><br>>>>
<br><br>>>> Please don't. See above. :-)
<br><br>>>>
<br><br>>>> > IMO we cannot keep status quo in the plugin, we gotta to make the
<br><br>>>> inputs/outputs behave consistently.
<br><br>>>>
<br><br>>>> Absolutely!
<br><br>>>>
<br><br>>>> > - should a function expecting an ExternalTypeAlias of atomic type
<br><br>>>> accept an immediate value of compatible type?
<br><br>>>>
<br><br>>>> Considering type safety, I think not.
<br><br>>>>
<br><br>>>> Well ... since this is a convenience issue and thus not about saving
<br><br>>>> run-time cost ... What about adding a callback into the image to react on
<br><br>>>> FFIErrorCoercionFailed? Maybe the selector for such a callback could be
<br><br>>>> stored in ExternalLibraryFunction since this is accessible to the plugin
<br><br>>>> anyway?
<br><br>>>>
<br><br>>>> <u></u>
<br><br>>>>
<br><br>>>> ... would it be possible? Like that #doesNotUnderstand: callback?
<br><br>>>>
<br><br>>>> I would love to do ad-hoc packaging of arguments. (Also thinking about
<br><br>>>> FFI-Callback. But ignore me here :)
<br><br>>>>
<br><br>>>> > - should a function returning an ExternalTypeAlias of atomic type
<br><br>>>> instantiate the Alias?
<br><br>>>>
<br><br>>>> Yes, please! See above.
<br><br>>>>
<br><br>>>> If you want to trade type safety in for a performance gain, just use a
<br><br>>>> "lightweight alias" as explained above. And package that return value
<br><br>>>> manually.
<br><br>>>>
<br><br>>>> Best,
<br><br>>>> Marcel
<br><br>>>>
<br><br>>>>
<br><br>>>> Am 18.06.2020 21:03:53 schrieb Nicolas Cellier <><br>>>> <a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>>:
<br><br>>>>
<br><br>>>> Hi all,
<br><br>>>> following the question of Marcel about usage of ExternalTypeAlias:
<br><br>>>>
<br><br>>>> Type alias are subclasses of ExternalStructure.
<br><br>>>> I used them in HDF5 because there was no other means to define an alias
<br><br>>>> at the time.
<br><br>>>> Now we have another mean by adding a class side method in ExternalType
<br><br>>>> (I would have rather imagined the usage of some annotation to get a
<br><br>>>> declarative style)
<br><br>>>>
<br><br>>>> This has one advantage: type safety. You cannot pass a Foo to a function
<br><br>>>> expecting a Bar, even if they both alias int...
<br><br>>>>
<br><br>>>> But this has one major drawback: with current FFI, you cannot pass a
<br><br>>>> Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias for
<br><br>>>> an integer type...
<br><br>>>>
<br><br>>>> Thus, you have to wrap every Bar argument into a Bar instance...
<br><br>>>> Gasp, this is going too far...
<br><br>>>>
<br><br>>>> What about functions returning such alias?
<br><br>>>> Function returning struct by value allocate a struct, but it's not the
<br><br>>>> case here, FFI plugin is clever enough to recognize an atomic type alias
<br><br>>>> and simply return a Smalltalk Integer...
<br><br>>>> So it's not Bar all the way down, I have to manually wrap Bar...
<br><br>>>> This is not consistent.
<br><br>>>> If we return an int, we must accept an int, otherwise, we cannot chain
<br><br>>>> FFI calls...
<br><br>>>>
<br><br>>>> I could use the lightweight alias instead, but there was another usage
<br><br>>>> where it was convenient to use ExternalTypeAlias: enum.
<br><br>>>> Indeed, I simply defined all enum symbols as class side method. For
<br><br>>>> example for enum bar { beg=0, mid=1, end=2 }; I simply create a type Bar
<br><br>>>> aliasing int and having class side methods beg ^0, mid ^1, end ^2.
<br><br>>>> This way, I normally can pass a Bar mid to an external function.
<br><br>>>> (currently, we must defne mid ^Bar new value: 1; yourself)
<br><br>>>>
<br><br>>>> If I use a simpler alias, where are those ids going to?
<br><br>>>>
<br><br>>>> IMO we cannot keep statu quo in the plugin, we gotta to make the
<br><br>>>> inputs/outputs behave consistently.
<br><br>>>>  The choice is this one:
<br><br>>>> - should a function expecting an ExternalTypeAlias of atomic type accept
<br><br>>>> an immediate value of compatible type? (the permissive implementation)
<br><br>>>> - should a function returning an ExternalTypeAlias of atomic type
<br><br>>>> instantiate the Alias? (the typesafe implementation, with higher runtime
<br><br>>>> cost due to pressure on GC)
<br><br>>>>
<br><br>>>>
<br><br>>>>
<br><br>>>>
<br><br>>>>
<br><br>>>>
<br><br>>>>
<br><br>>>
<br><br>>
<br><br><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le ven. 19 juin 2020 à 16:16, Marcel Taeumel <<a href="mailto:marcel.taeumel@hpi.de" target="_blank">marcel.taeumel@hpi.de</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left: 1px solid rgb(204,204,204);padding-left: 1ex;min-width: 500px"> <div id="gmail-m_-455482905057329652gmail-m_5957770884654581966__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: rgb(0,0,0)">
<br><br>                                        
<br><br>                                        
<br><br>                                            
<br><br>                                        
<br><br>                                        
<br><br>                                        <div><span style="font-size: 13.3333px">> </span><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">Err, I move this thread to Opensmalltalk VM dev, because it is not Squeak </span><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">specific! </span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px">Absolutely. :-)</span></div><div><span style="font-family: Arial,Helvetica,sans-serif;font-size: 13px"><br></span></div><div><span style="font-size: 13.3333px">(On a side note: I think that ExternalData should always have a pointer type in its "type" field unless "handle" is a ByteArray instead of ExternalAddress)</span><div style="font-size: 13.3333px"><br></div></div></div></blockquote><div>Let me disagree here...</div><div>The type should better describe the contents of the handle (be it an ExternalAddress or a zone of object memory (ByteArray)).</div><div>Typically, if you have an external (global) variable of type foo (extern foo my_var;) then the natural type is foo.</div><div>Having foo*, would mean that we handle an array of foo*, or a pointer to foo* (foo**).<br></div><div>This way, we make no difference between type surrogates (ExternalStruct/ExternalTypeAlias) and other atomic cases.<br></div><div><br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left: 1px solid rgb(204,204,204);padding-left: 1ex;min-width: 500px"><div id="gmail-m_-455482905057329652gmail-m_5957770884654581966__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: rgb(0,0,0)"><div><div style="font-size: 13.3333px"></div><div style="font-size: 13.3333px">(On another side note: I also think that "handle" in ExternalData should never ever be an atomic Smalltalk object (Integer or Float) but always either ByteArray or ExternalAddress. ... which makes it different from what "handle" can be in ExternalStructs that represent type aliases. :-)<br><div><br></div></div></div></div></blockquote><div>Yes!</div><div>It could be an ExternalAddress, or a zone of object memory (ByteArray) or an Alien as we may want to support that too.</div><div>I'd be inclined to support doubleByte, word, and doubleWord arrays if the size of atomic type matches</div><div>(Typically passing a DoubleByteArray, WordArray, a Float32Array, a Float64Array to a short */int */float*/double*).</div><div><br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left: 1px solid rgb(204,204,204);padding-left: 1ex;min-width: 500px"><div id="gmail-m_-455482905057329652gmail-m_5957770884654581966__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: rgb(0,0,0)"><div><div style="font-size: 13.3333px"><div></div><div>Best,</div><div>Marcel</div></div></div><div></div>
<br><br>                                        
<br><br>                                        <blockquote type="cite" style="border-left-style: solid;border-width: 1px;margin-top: 20px;margin-left: 0px;padding-left: 10px;min-width: 500px">
<br><br>                        <p style="color:rgb(170,170,170);margin-top:10px">Am 19.06.2020 16:03:10 schrieb Nicolas Cellier <<a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>>:</p><div style="font-family:Arial,Helvetica,sans-serif"> Err, I move this thread to Opensmalltalk VM dev, because it is not Squeak
<br><br><br>specific!
<br><br><br>
<br><br><br>
<br><br><br>
<br><br><br>Le ven. 19 juin 2020 à 15:10, Nicolas Cellier <><br><a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>> a écrit :
<br><br><br>
<br><br><br>> Hi Marcel,
<br><br><br>> thanks for your vote. Anyone else?
<br><br><br>>
<br><br><br>> Le ven. 19 juin 2020 à 09:30, Marcel Taeumel <u></u> a
<br><br><br>> écrit :
<br><br><br>>
<br><br><br>>> Hi Nicolas, hi all! :-)
<br><br><br>>>
<br><br><br>>> > Type alias are subclasses of ExternalStructure.
<br><br><br>>>
<br><br><br>>> And with it, they get their own instance of ExternalType, which is
<br><br><br>>> managed in the classVar StructTypes. Let's call them struct type.
<br><br><br>>>
<br><br><br>>> > Now we have another mean by adding a class side method in ExternalType.
<br><br><br>>>
<br><br><br>>> That kind of aliasing is for compile-time type referencing only -- such
<br><br><br>>> as in FFI calls (i.e. <u></u> pragmas) or struct-field definitions
<br><br><br>>> (i.e. #fields). (And soon <u></u> if I have the first version of
<br><br><br>>> FFI-Callback to show you :)
<br><br><br>>>
<br><br><br>>> Because those aliases become fully transparent after compiling the method
<br><br><br>>> for the FFI call into an instance of ExternalLibraryFunction and after
<br><br><br>>> compiling the field specs of external structures into compiledSpec for
<br><br><br>>> struct types.
<br><br><br>>>
<br><br><br>>> For example, on my machine in 32-bit Squeak, a 'size_t' becomes a 'long',
<br><br><br>>> which is Squeak FFI term for 'int'. :-) See #ffiLongVsInt. And 'int32_t'
<br><br><br>>> also becomes a 'long'. So, it just translates C vocabulary into Squeak FFI
<br><br><br>>> vocabulary. Nothing more. Nothing less.
<br><br><br>>>
<br><br><br>>> Having that, please do not use class-side methods in ExternalType to
<br><br><br>>> alias a (complex) struct type. Use them only for renaming atomic types.
<br><br><br>>> Please. We may want to add checks to enforce that.
<br><br><br>>>
<br><br><br>>> I suppose that this discussion focuses on type aliases that are
<br><br><br>>> represented as subclasses of ExternalStructure (or ExternalTypeAlias) and
<br><br><br>>> thus get their own struct type. So, let's ignore this other mechanism for
<br><br><br>>> now.
<br><br><br>>>
<br><br><br>>> > This has one advantage: type safety. You cannot pass a Foo to a
<br><br><br>>> function expecting a Bar, even if they both alias int...
<br><br><br>>>
<br><br><br>>> Yes! So, we are now talking about type aliases to atomic types. The
<br><br><br>>> struct type you get when aliasing a 'char' as Foo, for example, looks like
<br><br><br>>> this:
<br><br><br>>>
<br><br><br>>> (...forgive my "creative" representation of WordArray here...)
<br><br><br>>>
<br><br><br>>> compiledSpec: 0A 04 00 01
<br><br><br>>> referentClass: Foo
<br><br><br>>>
<br><br><br>>> How does the atomic type for 'char' look like?
<br><br><br>>>
<br><br><br>>> compiledSpec: 0A 04 00 01
<br><br><br>>> referentClass: nil
<br><br><br>>>
<br><br><br>>> Consequently, argument coercing will fail if you pass a 'char' when a
<br><br><br>>> 'Foo' is expected. Because the referentClass does not match --- even if the
<br><br><br>>> compiledSpec does match.
<br><br><br>>>
<br><br><br>>> > But this has one major drawback: with current FFI, you cannot pass a
<br><br><br>>> Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias for
<br><br><br>>> an integer type...
<br><br><br>>>
<br><br><br>>> That's correct. If the origin of such an argument is from within the
<br><br><br>>> image, you have to wrap it. But when returned from another call ... well
<br><br><br>>> ... that wrapping should have happened in the plugin ... But read on! :-)
<br><br><br>>>
<br><br><br>>> > FFI plugin is clever enough to recognize an atomic type alias and
<br><br><br>>> simply return a Smalltalk Integer...
<br><br><br>>> > So it's not Bar all the way down, I have to manually wrap Bar...
<br><br><br>>> > This is not consistent.
<br><br><br>>>
<br><br><br>>> Uhh! That's a bug. The FFI Plugin must take a look at referentClass
<br><br><br>>> because it does so for argument coercing.
<br><br><br>>>
<br><br><br>>> Now I understand why the code generation of struct-field accessors was so
<br><br><br>>> apparently broken for me. I changed that so that having an alias type as
<br><br><br>>> part of a larger struct will automatically wrap that for you into the alias
<br><br><br>>> structure. :-) Even if you are just aliasing a plain 'int' ... or 'long'
<br><br><br>>> ... Ha ha. ;-) #ffiLongVsInt.
<br><br><br>>>
<br><br><br>>> It should be "Bar all the way down". Definitely.
<br><br><br>>>
<br><br><br>>> > If we return an int, we must accept an int, otherwise, we cannot chain
<br><br><br>>> FFI calls...
<br><br><br>>>
<br><br><br>>> Well, not for type aliases. I would like keep the path of type safety
<br><br><br>>> here, you mentioned above.
<br><br><br>>>
<br><br><br>>> Just fix the FFI plugin to respect referentClass when packaging the
<br><br><br>>> return value.
<br><br><br>>>
<br><br><br>>> > I could use the lightweight alias instead, [...]
<br><br><br>>>
<br><br><br>>> Please don't. See above. Those "lightweight alias" are for renaming
<br><br><br>>> atomic types only. Let's keep it simple and try to address you concern here
<br><br><br>>> with struct types and ExternalTypeAlias. :-)
<br><br><br>>>
<br><br><br>>> > there was another usage where it was convenient to use
<br><br><br>>> ExternalTypeAlias: enum.
<br><br><br>>> > Indeed, I simply defined all enum symbols as class side method.
<br><br><br>>>
<br><br><br>>> I like that! It reads like a next step for ExternalPool. Once you have
<br><br><br>>> extracted the constant values from header files, and once you have those
<br><br><br>>> values for your platform in classVars in your external pool, use that pool
<br><br><br>>> to define enums through ExternalStructure.
<br><br><br>>>
<br><br><br>>> Like this:
<br><br><br>>>
<br><br><br>>> ExternalTypeAlias subclass: #MyEnumBar
<br><br><br>>> instanceVariableNames: ''
<br><br><br>>> classVariableNames: ''
<br><br><br>>> poolDictionaries: 'HDF5Pool'
<br><br><br>>> category: 'HDF5'
<br><br><br>>>
<br><br><br>>> MyEnumBar class >> #poolFields
<br><br><br>>>    "
<br><br><br>>>    self defineFields.
<br><br><br>>>    "
<br><br><br>>>    ^ #(
<br><br><br>>>       (beg HDF5_BEG)
<br><br><br>>>       (mid HDF5_MID)
<br><br><br>>>       (end HDF5_END)
<br><br><br>>>    )
<br><br><br>>>
<br><br><br>>> Note that HDF5_BEG etc. are from the HDF5Pool, which is managed via
<br><br><br>>> external-pool definitions. See class comment in ExternalPool to get started.
<br><br><br>>>
<br><br><br>>> Note that #poolFields would translate to class-side methods as you
<br><br><br>>> suggested.
<br><br><br>>>
<br><br><br>>> > If I use a simpler alias, where are those ids going to?
<br><br><br>>>
<br><br><br>>> Please don't. See above. :-)
<br><br><br>>>
<br><br><br>>> > IMO we cannot keep status quo in the plugin, we gotta to make the
<br><br><br>>> inputs/outputs behave consistently.
<br><br><br>>>
<br><br><br>>> Absolutely!
<br><br><br>>>
<br><br><br>>> > - should a function expecting an ExternalTypeAlias of atomic type
<br><br><br>>> accept an immediate value of compatible type?
<br><br><br>>>
<br><br><br>>> Considering type safety, I think not.
<br><br><br>>>
<br><br><br>>> Well ... since this is a convenience issue and thus not about saving
<br><br><br>>> run-time cost ... What about adding a callback into the image to react on
<br><br><br>>> FFIErrorCoercionFailed? Maybe the selector for such a callback could be
<br><br><br>>> stored in ExternalLibraryFunction since this is accessible to the plugin
<br><br><br>>> anyway?
<br><br><br>>>
<br><br><br>>> <u></u>
<br><br><br>>>
<br><br><br>>> ... would it be possible? Like that #doesNotUnderstand: callback?
<br><br><br>>>
<br><br><br>>> I would love to do ad-hoc packaging of arguments. (Also thinking about
<br><br><br>>> FFI-Callback. But ignore me here :)
<br><br><br>>>
<br><br><br>>> > - should a function returning an ExternalTypeAlias of atomic type
<br><br><br>>> instantiate the Alias?
<br><br><br>>>
<br><br><br>>> Yes, please! See above.
<br><br><br>>>
<br><br><br>>> If you want to trade type safety in for a performance gain, just use a
<br><br><br>>> "lightweight alias" as explained above. And package that return value
<br><br><br>>> manually.
<br><br><br>>>
<br><br><br>>> Best,
<br><br><br>>> Marcel
<br><br><br>>>
<br><br><br>>> Am 18.06.2020 21:03:53 schrieb Nicolas Cellier <><br>>> <a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>>:
<br><br><br>>> Hi all,
<br><br><br>>> following the question of Marcel about usage of ExternalTypeAlias:
<br><br><br>>>
<br><br><br>>> Type alias are subclasses of ExternalStructure.
<br><br><br>>> I used them in HDF5 because there was no other means to define an alias
<br><br><br>>> at the time.
<br><br><br>>> Now we have another mean by adding a class side method in ExternalType (I
<br><br><br>>> would have rather imagined the usage of some annotation to get a
<br><br><br>>> declarative style)
<br><br><br>>>
<br><br><br>>> This has one advantage: type safety. You cannot pass a Foo to a function
<br><br><br>>> expecting a Bar, even if they both alias int...
<br><br><br>>>
<br><br><br>>> But this has one major drawback: with current FFI, you cannot pass a
<br><br><br>>> Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias for
<br><br><br>>> an integer type...
<br><br><br>>>
<br><br><br>>> Thus, you have to wrap every Bar argument into a Bar instance...
<br><br><br>>> Gasp, this is going too far...
<br><br><br>>>
<br><br><br>>> What about functions returning such alias?
<br><br><br>>> Function returning struct by value allocate a struct, but it's not the
<br><br><br>>> case here, FFI plugin is clever enough to recognize an atomic type alias
<br><br><br>>> and simply return a Smalltalk Integer...
<br><br><br>>> So it's not Bar all the way down, I have to manually wrap Bar...
<br><br><br>>> This is not consistent.
<br><br><br>>> If we return an int, we must accept an int, otherwise, we cannot chain
<br><br><br>>> FFI calls...
<br><br><br>>>
<br><br><br>>> I could use the lightweight alias instead, but there was another usage
<br><br><br>>> where it was convenient to use ExternalTypeAlias: enum.
<br><br><br>>> Indeed, I simply defined all enum symbols as class side method. For
<br><br><br>>> example for enum bar { beg=0, mid=1, end=2 }; I simply create a type Bar
<br><br><br>>> aliasing int and having class side methods beg ^0, mid ^1, end ^2.
<br><br><br>>> This way, I normally can pass a Bar mid to an external function.
<br><br><br>>> (currently, we must defne mid ^Bar new value: 1; yourself)
<br><br><br>>>
<br><br><br>>> If I use a simpler alias, where are those ids going to?
<br><br><br>>>
<br><br><br>>> IMO we cannot keep statu quo in the plugin, we gotta to make the
<br><br><br>>> inputs/outputs behave consistently.
<br><br><br>>>  The choice is this one:
<br><br><br>>> - should a function expecting an ExternalTypeAlias of atomic type accept
<br><br><br>>> an immediate value of compatible type? (the permissive implementation)
<br><br><br>>> - should a function returning an ExternalTypeAlias of atomic type
<br><br><br>>> instantiate the Alias? (the typesafe implementation, with higher runtime
<br><br><br>>> cost due to pressure on GC)
<br><br><br>>>
<br><br><br>>>
<br><br><br>>>
<br><br><br>>>
<br><br><br><div dir="ltr"><div>Err, I move this thread to Opensmalltalk VM dev, because it is not Squeak specific!</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le ven. 19 juin 2020 à 15:10, Nicolas Cellier <<a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left: 1px solid rgb(204,204,204);padding-left: 1ex;min-width: 500px"><div dir="ltr"><div>Hi Marcel,</div><div>thanks for your vote. Anyone else?<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le ven. 19 juin 2020 à 09:30, Marcel Taeumel <<a href="mailto:marcel.taeumel@hpi.de" target="_blank">marcel.taeumel@hpi.de</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex;border-left: 1px solid rgb(204,204,204);padding-left: 1ex;min-width: 500px"><div><div id="gmail-m_-455482905057329652gmail-m_5957770884654581966gmail-m_-4730415257224521194gmail-m_-6005643300885061035__MailbirdStyleContent" style="font-size: 10pt;font-family: Arial;color: rgb(0,0,0)">
<br><br><br>                                        <div>Hi Nicolas, hi all! :-)</div><div><br></div><div>> Type alias are subclasses of ExternalStructure.</div><div><br></div><div>And with it, they get their own instance of ExternalType, which is managed in the classVar StructTypes. Let's call them struct type.</div><div><br></div><div>> Now we have another mean by adding a class side method in ExternalType.</div><div><br></div><div>That kind of aliasing is for compile-time type referencing only -- such as in FFI calls (i.e. <apicall:...> pragmas) or struct-field definitions (i.e. #fields). (And soon <callback: ...> if I have the first version of FFI-Callback to show you :)</div><div><br></div><div>Because those aliases become fully transparent after compiling the method for the FFI call into an instance of ExternalLibraryFunction and after compiling the field specs of external structures into compiledSpec for struct types. </div><div><br></div><div>For example, on my machine in 32-bit Squeak, a 'size_t' becomes a 'long', which is Squeak FFI term for 'int'. :-) See #ffiLongVsInt. And 'int32_t' also becomes a 'long'. So, it just translates C vocabulary into Squeak FFI vocabulary. Nothing more. Nothing less.</div><div><br></div><div>Having that, please do not use class-side methods in ExternalType to alias a (complex) struct type. Use them only for renaming atomic types. Please. We may want to add checks to enforce that.</div><div><br></div><div>I suppose that this discussion focuses on type aliases that are represented as subclasses of ExternalStructure (or ExternalTypeAlias) and thus get their own struct type. So, let's ignore this other mechanism for now.</div><div><br></div><div>> This has one advantage: type safety. You cannot pass a Foo to a function expecting a Bar, even if they both alias int...</div><div><br></div><div>Yes! So, we are now talking about type aliases to atomic types. The struct type you get when aliasing a 'char' as Foo, for example, looks like this:</div><div><br></div><div>(...forgive my "creative" representation of WordArray here...)</div><div><br></div><div>compiledSpec: 0A 04 00 01</div><div>referentClass: Foo</div><div><br></div><div>How does the atomic type for 'char' look like?</div><div><br></div><div>compiledSpec: 0A 04 00 01</div><div>referentClass: nil</div><div><br></div><div>Consequently, argument coercing will fail if you pass a 'char' when a 'Foo' is expected. Because the referentClass does not match --- even if the compiledSpec does match.</div><div><br></div><div>> But this has one major drawback: with current FFI, you cannot pass a Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias for an integer type...</div><div><br></div><div>That's correct. If the origin of such an argument is from within the image, you have to wrap it. But when returned from another call ... well ... that wrapping should have happened in the plugin ... But read on! :-)</div><div><br></div><div>> FFI plugin is clever enough to recognize an atomic type alias and simply return a Smalltalk Integer...</div><div>> So it's not Bar all the way down, I have to manually wrap Bar...</div><div>> This is not consistent.</div><div><br></div><div>Uhh! That's a bug. The FFI Plugin must take a look at referentClass because it does so for argument coercing.</div><div><br></div><div>Now I understand why the code generation of struct-field accessors was so apparently broken for me. I changed that so that having an alias type as part of a larger struct will automatically wrap that for you into the alias structure. :-) Even if you are just aliasing a plain 'int' ... or 'long' ... Ha ha. ;-) #ffiLongVsInt.</div><div><br></div><div>It should be "Bar all the way down". Definitely.</div><div><br></div><div>> If we return an int, we must accept an int, otherwise, we cannot chain FFI calls...</div><div><br></div><div>Well, not for type aliases. I would like keep the path of type safety here, you mentioned above.</div><div><br></div><div>Just fix the FFI plugin to respect referentClass when packaging the return value.</div><div><br></div><div>> I could use the lightweight alias instead, [...]</div><div><br></div><div>Please don't. See above. Those "lightweight alias" are for renaming atomic types only. Let's keep it simple and try to address you concern here with struct types and ExternalTypeAlias. :-)</div><div><br></div><div>> there was another usage where it was convenient to use ExternalTypeAlias: enum.</div><div>> Indeed, I simply defined all enum symbols as class side method. </div><div><br></div><div>I like that! It reads like a next step for ExternalPool. Once you have extracted the constant values from header files, and once you have those values for your platform in classVars in your external pool, use that pool to define enums through ExternalStructure.</div><div><br></div><div>Like this:</div><div><br></div><div>ExternalTypeAlias subclass: #MyEnumBar</div><div><span style="white-space:pre-wrap">  </span>instanceVariableNames: ''</div><div><span style="white-space:pre-wrap">        </span>classVariableNames: ''</div><div><span style="white-space:pre-wrap">   </span>poolDictionaries: 'HDF5Pool'</div><div><span style="white-space:pre-wrap">     </span>category: 'HDF5'</div><div><br></div><div>MyEnumBar class >> #poolFields</div><div>   "</div><div>   self defineFields.</div><div>   "</div><div>   ^ #(</div><div>      (beg HDF5_BEG)</div><div>      (mid HDF5_MID)</div><div>      (end HDF5_END)</div><div>   )</div><div><br></div><div>Note that HDF5_BEG etc. are from the HDF5Pool, which is managed via external-pool definitions. See class comment in ExternalPool to get started.</div><div><br></div><div>Note that #poolFields would translate to class-side methods as you suggested.</div><div><br></div><div>> If I use a simpler alias, where are those ids going to?</div><div><br></div><div>Please don't. See above. :-)</div><div><br></div><div>> IMO we cannot keep status quo in the plugin, we gotta to make the inputs/outputs behave consistently.</div><div><br></div><div>Absolutely!</div><div><br></div><div>> - should a function expecting an ExternalTypeAlias of atomic type accept an immediate value of compatible type?</div><div><br></div><div>Considering type safety, I think not.</div><div><br></div><div>Well ... since this is a convenience issue and thus not about saving run-time cost ... What about adding a callback into the image to react on FFIErrorCoercionFailed? Maybe the selector for such a callback could be stored in ExternalLibraryFunction since this is accessible to the plugin anyway?</div><div><br></div><div><apicall: void 'foo' (MyInt YourInt in) ifFailCoerceVia: #wrapInt: ></div><div><br></div><div>... would it be possible? Like that #doesNotUnderstand: callback?</div><div><br></div><div>I would love to do ad-hoc packaging of arguments. (Also thinking about FFI-Callback. But ignore me here :)</div><div><br></div><div>> - should a function returning an ExternalTypeAlias of atomic type instantiate the Alias?</div><div><br></div><div>Yes, please! See above.</div><div><br></div><div>If you want to trade type safety in for a performance gain, just use a "lightweight alias" as explained above. And package that return value manually.</div><div><br></div><div>Best,</div><div>Marcel</div><div></div><blockquote type="cite" style="border-left-style: solid;border-width: 1px;margin-top: 20px;margin-left: 0px;padding-left: 10px;min-width: 500px">
<br><br><br>                        <p style="color:rgb(170,170,170);margin-top:10px">Am 18.06.2020 21:03:53 schrieb Nicolas Cellier <<a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>>:</p><div style="font-family:Arial,Helvetica,sans-serif">
<br><br><br><div dir="ltr"><div>Hi all,</div><div>following the question of Marcel about usage of ExternalTypeAlias:</div><div><br></div><div>Type alias are subclasses of ExternalStructure.<br></div><div>I used them in HDF5 because there was no other means to define an alias at the time.</div><div>Now we have another mean by adding a class side method in ExternalType (I would have rather imagined the usage of some annotation to get a declarative style)</div><div><br></div><div>This has one advantage: type safety. You cannot pass a Foo to a function expecting a Bar, even if they both alias int...</div><div><br></div><div>But this has one major drawback: with current FFI, you cannot pass a Smalltalk Integer, to a method expecting a Bar, even if Bar is an alias for an integer type...</div><div><br></div><div>Thus, you have to wrap every Bar argument into a Bar instance...<br></div><div>Gasp, this is going too far...</div><div><br></div><div>What about functions returning such alias?</div><div>Function returning struct by value allocate a struct, but it's not the case here, FFI plugin is clever enough to recognize an atomic type alias and simply return a Smalltalk Integer...</div><div>So it's not Bar all the way down, I have to manually wrap Bar...</div><div>This is not consistent.</div><div>If we return an int, we must accept an int, otherwise, we cannot chain FFI calls...</div><div><br></div><div>I could use the lightweight alias instead, but there was another usage where it was convenient to use ExternalTypeAlias: enum.</div><div>Indeed, I simply defined all enum symbols as class side method. For example for enum bar { beg=0, mid=1, end=2 }; I simply create a type Bar aliasing int and having class side methods beg ^0, mid ^1, end ^2.</div><div>This way, I normally can pass a Bar mid to an external function.</div><div>(currently, we must defne mid ^Bar new value: 1; yourself)<br></div><div><br></div><div>If I use a simpler alias, where are those ids going to?</div><div><br></div><div><div>IMO we cannot keep statu quo in the plugin, we gotta to make the inputs/outputs behave consistently.</div></div> The choice is this one:<br><div>- should a function expecting an ExternalTypeAlias of atomic type accept an immediate value of compatible type? (the permissive implementation)<br></div><div>- should a function returning an ExternalTypeAlias of atomic type instantiate the Alias? (the typesafe implementation, with higher runtime cost due to pressure on GC)<br></div><div><br></div><div><br></div></div>
<br><br><br></div></blockquote>
<br><br><br>                                        </div></div><br>
<br><br><br></blockquote></div>
<br><br><br></blockquote></div>
<br><br><br><u></u><u></u><u></u><u></u></div></blockquote></div></blockquote></div></div>
<br><br><u></u><u></u><u></u><u></u><u></u></div></blockquote></div></div></blockquote>
<br>                                        </div></div></blockquote>
<br>                                        </div></div></blockquote>
<br>                                        </div></div></blockquote>
<br>                                        </div></div></blockquote></div>
<br></apicall:></callback:></apicall:...></marcel.taeumel@hpi.de></marcel.taeumel@hpi.de></marcel.taeumel@hpi.de><!--#[--><!--#[--><!--@00f3a3ee,--><!--@00f3a3ee,--><!--#[--><!--1312301580,--></handle,></marcel.taeumel@hpi.de></marcel.taeumel@hpi.de></marcel.taeumel@hpi.de><!--@00f3a3ee,--><!--@00f3a3ee,--></marcel.taeumel@hpi.de></div></blockquote>
                                        </div></div></blockquote></div>