[Vm-beginners] Return strings in numeric primitives

Igor Stasenko siguctua at gmail.com
Sat Jun 30 17:35:40 UTC 2012


On 30 June 2012 18:58, David Leonhardt <davidleonhardt at gmail.com> wrote:
> For more details.
>
> With this code, I get a string and return it.
>
> self var: #stringObj declareC: 'int stringObj'.
> self var: #stringPtr declareC: 'char *stringPtr'.
> self var: #stringSize declareC: 'int stringSize'.
> stringObj := objectMemory stackValue: 0.
> stringSize := objectMemory stSizeOf: stringObj.
> stringPtr := self cCoerce: (objectMemory arrayValueOf: stringObj) to: 'char
> *'.
>
> But I can't replace stringObj := 'Hello World'. Why?
>
>
Wait, you want to read from string object, or create a string object
from C string?

For "returning" a string, first, you should instantiate an instance of
ByteString, and then copy characters there.

// instantiating

stringOop := interpreterProxy instantiateClass: interpreterProxy
classByteString indexableSize: size.

// getting a pointer to first character in bytestring instance
<var: firstBytePtr type: 'char*'>
firstBytePtr := interpreterProxy firstIndexableField: stringOop

// copying to/from
0 to: size-1 do:[ :i | firstBytePtr at: i put: (....) ]

>
> 2012/6/30 David Leonhardt <davidleonhardt at gmail.com>
>>
>> Hey.
>>
>> I'm doing my own numeric primitive.
>> I could return numeric values, doing something like:
>>
>>                    self pop: 1 thenPush: ( objectMemory integerObjectOf:
>> 21 ).
>>
>> What I couldn't do is find a way to return a string, for example 'Hello
>> World'.
>>
>>
>> Thanks,
>> David.
>
>
>
> _______________________________________________
> VM-beginners mailing list
> VM-beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/vm-beginners
>



-- 
Best regards,
Igor Stasenko.


More information about the VM-beginners mailing list