Primitive help needed: object from signed-32bit int
Mark A. Schwenk
mas at wellthot.com
Tue Feb 9 19:16:11 UTC 1999
I am writing some primitives and need to pass a signed 32-bit long from
C back into the Smalltalk object world. Inspired by the following
method:
!Interpreter methodsFor: 'primitive support'!
positive32BitIntegerFor: integerValue
| newLargeInteger |
"Note - integerValue is interpreted as POSITIVE, eg, as the
result of
Bitmap>at:, or integer>bitAnd:."
(integerValue >= 0 and: [self isIntegerValue: integerValue])
ifTrue: [^ self integerObjectOf: integerValue].
newLargeInteger _
self instantiateSmallClass: (self splObj:
ClassLargePositiveInteger)
sizeInBytes: 8
fill: 0.
self storeByte: 3 ofObject: newLargeInteger
withValue: ((integerValue >> 24) bitAnd: 16rFF).
self storeByte: 2 ofObject: newLargeInteger
withValue: ((integerValue >> 16) bitAnd: 16rFF).
self storeByte: 1 ofObject: newLargeInteger
withValue: ((integerValue >> 8) bitAnd: 16rFF).
self storeByte: 0 ofObject: newLargeInteger
withValue: (integerValue bitAnd: 16rFF).
^ newLargeInteger! !
I attempted to write a version that would work with both negative and
positive longs, but I couldn't find an example of how to instantiate a
non-special class from the primitive world.
!Interpreter methodsFor: 'primitive support' stamp: 'mas 2/9/1999
13:09'!
signed32BitIntegerFor: integerValue
| newLargeInteger theClass |
self isIntegerValue: integerValue
ifTrue: [^ self integerObjectOf: integerValue].
integerValue > 0
ifTrue: [theClass _ self splObj:
ClassLargePositiveInteger]
ifFalse: [theClass _ self "?????what goes here?????"].
newLargeInteger _ self instantiateSmallClass: theClass
sizeInBytes: 8 fill: 0.
self storeByte: 3 ofObject: newLargeInteger
withValue: ((integerValue >> 24) bitAnd: 16rFF).
self storeByte: 2 ofObject: newLargeInteger
withValue: ((integerValue >> 16) bitAnd: 16rFF).
self storeByte: 1 ofObject: newLargeInteger
withValue: ((integerValue >> 8) bitAnd: 16rFF).
self storeByte: 0 ofObject: newLargeInteger
withValue: (integerValue bitAnd: 16rFF).
^ newLargeInteger! !
Any helpful hints would be greatly appreciated. Thanks!
-Mark Schwenk
More information about the Squeak-dev
mailing list
|