'From Squeak3.11alpha of 22 May 2011 [latest update: #11440] on 24 May 2011 at 10:53:36 pm'! InterpreterPlugin subclass: #RandPlugin instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Junk'! !RandPlugin commentStamp: 'dtl 5/24/2011 21:46' prior: 0! RandPlugin demonstrates how to call the C lib rand() function as a primitive.! !RandPlugin methodsFor: 'primitives' stamp: 'dtl 5/24/2011 22:52'! primitiveRand "Call the rand() clib function to obtain a positive int in the range 0 through 16r7FFFFFFF. This will be the value of the Integer that we want to return from this primitive. Use #positive32BitIntegerFor: to convert this value into an object reference, which will be either a SmallInteger or a LargePositiveInteger. Pop a value from the stack (representing self), then push the integer value to be returned. Sender of this primitive will receive the integer object as the result of calling the primitive. Note, #code:inSmalltalk: provides a means of implementing the equivalent of rand() in Smalltalk. When running this primitive in Smalltalk (i.e. the interpreter simulator), the Smalltalk block will be executed rather than the rand() clib function." | nextRand | nextRand := self cCode: 'rand()' inSmalltalk: [(Random new next * 16r7FFFFFFF) asInteger]. self pop: 1 thenPush: (self positive32BitIntegerFor: nextRand)! ! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! RandPlugin class instanceVariableNames: ''! !RandPlugin class methodsFor: 'primitive access' stamp: 'dtl 5/24/2011 21:33'! next "RandPlugin next" self primitiveFailed! !