[Vm-beginners] Return strings in numeric primitives

David T. Lewis lewis at mail.msen.com
Mon Jul 2 04:15:57 UTC 2012


On Sun, Jul 01, 2012 at 06:01:23PM -0300, David Leonhardt wrote:
> mmm.... it seems like I don't understand how to try with strings...
> ok, I need something more basic or more information.
> Someone know where can I read about how to try strings en SLang?
> 

When you create a new String object to return from your primitive,
that object is part of the object memory managed by the VM. If you
have the string "hello world" in some C code for your primitive,
that string is not part of the object memory managed by the VM.
It is just a null-terminated array of characters somewhere in the
C program, allocated in memory somewhere outside of the Squeak
object memory. This means that if you want your primitive to return
a string, you should have your primitive create a new object of
type String, copy the data from the C string into the data portion
of the String object, and return that object as the result of the
primitive.

Thus in order for a primitive to answer the string "hello world",
the primitive should create a new instance of a String object with
length 11. This object will (inside the object memory) have a header
that indicates the type of object, and enough data space to hold
11 bytes of char data from a C string. If you copy "hello world"
from the C string into the data area of the new String object, then
you will have a 'hello world' String object in the Smalltalk object
memory. You can put that object on the stack to return it as the
result of a primitive call, and the result will be a primitive
that answers the string 'hello world'.

> 
> Are differences between how to try strings on numeric primitives and named
> primitives? Because I'm doing proves with numeric primitives.
> 

No, there is no difference between numeric primitives and named
primitives. These are just two different ways of calling a primitive
from the image. If you write a primitive to answer the string
'hello world', you will write it the same way regardless of whether
it is a numbered primitive or a named primitive. But in general you
should use named primitives whenever possible.

Dave



More information about the VM-beginners mailing list