[squeak-dev] Advice from Plugin gurus
Igor Stasenko
siguctua at gmail.com
Mon Sep 22 08:00:40 UTC 2008
2008/9/22 Gerardo Richarte <gera at corest.com>:
> Hi all, I'm working on a plugin (for SqueakNOS), and I have a primitive
> for which I had coded a first version, and I wanted to hear comments
> from people with deeper Squeak VM and plugin knowledge. I think it's
> ugly, but I'm more worried on whether it's dangerous or not.
>
> some context:
>
> There's a native function I need to call (I called it 'aPrintf' in
> the code below) with a specific calling convention:
>
> It takes only one argument: an array
> the first entry in the array is apointer to a C string
> the other elements are determined by the first element.
>
> What I want is to provide a generic interface to this aPrintf.
>
> The code below will walk the array, converting every element in the
> array to either an integer (integerValueOf) or a direct pointer
> (firstIndexableField).
>
> And I want to be able to read the results left in the array.
>
> I understand that I won't be able to read non-integer elements from the
> array after coming back from this primitive, but that's fine. I plan to
> keep a copy of all non-integer elements in Smalltalk variables,
> including strings and other buffers.
>
> What I'd like to know is what experts think of this... like, will it
> break anything, like the garbage collector? is there any other simple
> way of doing this?
>
> The primitive is:
> primitiveaPrintf: anArray
> | answer length element |
> self primitive: 'primitiveOFWCallout' parameters: #(Array).
>
> length := interpreterProxy slotSizeOf: anArray cPtrAsOop.
>
> answer := 0.
>
> "convert everything to native format"
> 0 to: length-1 do: [:i |
> element := anArray at: i.
> (interpreterProxy isIntegerObject: element)
> ifTrue: [anArray at: i put: (interpreterProxy
> integerValueOf: element)]
> ifFalse: [anArray at: i put: (interpreterProxy
> firstIndexableField: element)]
> ].
>
> self cCode: 'printf(anArray)'.
>
> "convert everything to integers. oops will be lost, but that's fine.
> The client should mantain a reference"
> 0 to: length-1 do: [:i |
> element := anArray at: i.
> anArray at: i put: (interpreterProxy positive32BitIntegerFor:
> element)
> ].
>
> answer = 0
> ifTrue: [^ self]
> ifFalse: [interpreterProxy primitiveFail].
>
>
> thanks a lot!
> richie
>
>
Hello Gera!
I wondering , why you prefer using string formatting in C, while in
smalltalk you have own? You can easily format strings in smalltalk,
and print only the resulting string w/o need in inventing safe schemes
or conversion.
Here is my primitive, which simply writes byte array to stdout:
writeToStdout
"Primitive.
Writes given bytearray to stdout"
| bufoop bufptr bufsize |
self var: 'bufptr' type: 'void *'.
self export: true.
bufoop := self stackValue: 0.
(self isBytes: bufoop)
ifFalse: [^ self primitiveFail].
bufptr := self firstIndexableField: bufoop.
bufsize := self byteSizeOf: bufoop.
self cPerform: (self cIdentifier: #fwrite) with: bufptr with: bufsize
with: 1 with: (self cIdentifier: #stdout).
self pop:1
---
note, #cPerform: , #cIdentifier is existing only in HydraVM, but it
can be replaced with cCode: 'fwrite(..blabla..)'
I'm using this primitive for debugging, and eventually it will be removed.
--
Best regards,
Igor Stasenko AKA sig.
More information about the Squeak-dev
mailing list
|