On 26 Apr 2015, at 7:29 , Max Leske <maxleske@gmail.com> wrote:
Hi guys
I have a problem with NBExternalArray. AFAICT the generated assembly should be able to handle arbitrary elements in the array (as long as they are all of the same type). The operations required to read / write an element are given by the external type specified for the collection (e.g âcharâ or âintâ). All of this works wonderfully for char, int, NBExternalAddress etc. But Iâm trying to get it working for strings. So here is the code that I would expect to work:
arrayClass := NBExternalArray anonymousSubclassInitElementType: 'String'. array := arrayClass new: 1. array at: 1 put: âfoo'. array at: 1. ââ> should procude âfooâ, but produces arbitrary stringsâ
I can make it work with NBExternalAddress of course but I donât want to if I can avoid it.
Iâd appreciate it if someone (Nicolai?) could take a look at NBExternalArray>>emitRead / emitWrite and see if thereâs an easy solution. Especially for strings I donât think it should be too hard since all thatâs needed is already present in the string handling facilities.
Cheers, Max
There's a solution, but I doubt you could call it simple. NBExternalArray is meant to hold const sized objects, so is allocated with the proper amount of slots up front. If you want it to hold pointers to variably sized objects (like strings), since we can't pin the objects, they need to be copied (well, would have to do that anyways for Strings, which need it'd need to do dynamic allocation (and freeing when appropriate), so emitWrite would become quite a bit more complicated, + necessary object finalization would have to occur. You *can* work around it, by using an element type that is externally allocated, but it adds quite a bit of boilerplate, here's the workspace PoC code: string := 'herruâ¬'. bytes := ZnUTF8Encoder new encodeString: string. mem := NativeBoost allocate: bytes size + 1. NativeBoost memCopy: bytes to: mem size: bytes size. arrClass := NBExternalArray anonymousSubclassInitElementType: 'char *'. arr := arrClass new: 1. arr at: 1 put: mem. ZnUTF8Encoder new decodeBytes: (arr at:1) readString asByteArray Cheers, Henry