functionality of Socket>>socketError
David T. Lewis
lewis at mail.msen.com
Wed Aug 25 22:53:33 UTC 2004
On Wed, Aug 25, 2004 at 01:13:10PM -0700, John M McIntosh wrote:
> Let me turn that around and suggest code in Smalltalk versus in C
> primitives, then anyone can participate. Not that one can't suggest VM
> changes, it's
> just way easier for all parties if it's in the image...
>
> englishErrorForSocketPrims: anErrorCode
> ^EnglishErrorCodes for: anErrorCode hint: #SocketPrims
>
> where hint tells me something about the primitive being used, thus I've
> a handle on the fact it's from the SocketPrims
>
> The just do something like
> platform _ Smalltalk platformName.
> platform = 'Mac OS' ifTrue:
> [Smalltalk osVersion asInteger >= 1000
> ifTrue: [stuff for osx ]
> ifFalse: [stuff for os 9 and earlier]].
Good point, but note also that strerror() and strerror_r() may already
do everything that is necessary. I'm not sure if they are available on
all platforms, but I think that Unix, Windows, and OS/X would be covered.
For example, a non-thread-safe implementation could look like this
(excerpted from some unreleased OSProcessPlugin code):
primitiveErrorMessageAt: errno
"Answer a string describing an error message"
| index errMessage p |
self export: true.
self var: 'p' declareC: 'char *p'.
index _ interpreterProxy stackIntegerValue: 0.
p _ self cCoerce: (self cCode: 'strerror(index)' inSmalltalk: ['']) to: 'char *'.
errMessage _ self stringFromCString: p.
interpreterProxy pop: 2; push: errMessage
stringFromCString: aCString
"Answer a new String copied from a null-terminated C string.
Caution: This may invoke the garbage collector."
| len newString stringPtr |
self var: 'aCString' declareC: 'char *aCString'.
self var: 'stringPtr' declareC: 'char *stringPtr'.
len _ self cCode: 'strlen(aCString)' inSmalltalk: [0].
newString _ interpreterProxy
instantiateClass: interpreterProxy classString
indexableSize: len.
stringPtr _ interpreterProxy arrayValueOf: newString.
self cCode: '(char *)strncpy(stringPtr, aCString, len)'.
^ newString
Dave
More information about the Squeak-dev
mailing list
|