Plugin & strings

David T. Lewis lewis at mail.msen.com
Wed Dec 8 22:34:48 UTC 1999


On Wed, Dec 08, 1999 at 04:22:35PM +0100, rob.van.den.berg at philips.com wrote:
> Hi,
> 
> As I want to interface Squeak with an PCI card running an RTOS (Mercury card with MCOS), I need to create a plugin for the communication. ( A second step ofcourse is porting Squeak to this RTOS) my first approach is just to exchange strings.(e.g. MyClass 
> new doIt: 'aString', which returns a string).  However looking at both Andy Greenberg's Pluggable Primitives doc on minnow and the plugin code for FloatArray etc. I didn't understand how to do this. Can anyone give me any pointers?
>

Here's an example of a primitive which allocates and returns a string. You
can also pass a string as a parameter on the stack, which is safer from a
memory management point of view.  I've heard that doing the allocation in the
primitive can get you into trouble if you run out of object memory, so use
this with caution and add some error handling.

In your example, you would probably want to pass one string "out of"
Squeak. This would be a parameter which you pass on the stack. The
string which you answer back as a result would either be allocated
in the primitive (as in the attached example), or it would have been
pre-allocated and passed as a second parameter.

Dave

----- example follows -----

!UnixOSProcessAccessor methodsFor: 'primitives - OS process access' stamp: 'dtl 10/26/1999 11:21'!
primitiveArgumentAt
	"Answer a string containing the OS process argument at index (an Integer) in the
	argument list."

	| index aString start argVec argCnt len argStrLen |
	self var: 'start' declareC: 'unsigned char *start'.
	self var: 'argCnt' declareC: 'extern int argCnt'.
	self var: 'argVec' declareC: 'extern char **argVec'.
	index _ interpreterProxy stackIntegerValue: 0.
	((index > argCnt) | (index < 1))
	ifTrue: [ interpreterProxy primitiveFail ]
	ifFalse: [
		index := index - 1.	"Map to C convention where array index starts at 0."
		argStrLen _ self cCode: 'strlen(argVec[index])'.
		aString _ interpreterProxy
			instantiateClass: interpreterProxy classString
			indexableSize: argStrLen.
		start _ interpreterProxy arrayValueOf: aString.
		len _ interpreterProxy sizeOfSTArrayFromCPrimitive: start.
		self cCode: '(char *)strncpy(start, argVec[index], len)'.
		interpreterProxy pop: 2; push: aString ]





More information about the Squeak-dev mailing list