2015-10-08 14:26:04 +00:00
|
|
|
#include 'Apex.st'.
|
|
|
|
#include 'Object.st'.
|
|
|
|
#include 'UndefinedObject.st'.
|
|
|
|
#include 'Class.st'.
|
|
|
|
#include 'Boolean.st'.
|
2015-07-01 15:01:39 +00:00
|
|
|
|
|
|
|
|
2015-10-08 14:26:04 +00:00
|
|
|
#class Error(Object)
|
|
|
|
{
|
|
|
|
#method(#class) signal: aString
|
2015-07-01 15:01:39 +00:00
|
|
|
{
|
2015-10-08 14:26:04 +00:00
|
|
|
"accept an arbitary object instead of a string.
|
|
|
|
the object can be sent displayString for string conversion"
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#class Magnitude(Object)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#class Association(Magnitude)
|
|
|
|
{
|
|
|
|
#dcl key value.
|
|
|
|
}
|
|
|
|
|
|
|
|
#class Character(Magnitude)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#class Number(Magnitude)
|
|
|
|
{
|
|
|
|
#method add: aNumber
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #integerAdd>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method + aNumber
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #integerAdd>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method - aNumber
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #integerSub>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method * aNumber
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #integerMul>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method = aNumber
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #integerEQ>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method < aNumber
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #integerLT>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method > aNumber
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #integerGT>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-08 14:26:04 +00:00
|
|
|
#method to: end by: step do: aBlock
|
2015-07-01 15:01:39 +00:00
|
|
|
{
|
2015-10-08 14:26:04 +00:00
|
|
|
| i |
|
2015-07-01 15:01:39 +00:00
|
|
|
|
2015-10-08 14:26:04 +00:00
|
|
|
i := self.
|
|
|
|
(step > 0)
|
|
|
|
ifTrue: [
|
|
|
|
[ i <= end ] whileTrue: [
|
|
|
|
aBlock value: i.
|
|
|
|
i := i + step.
|
|
|
|
]
|
|
|
|
]
|
|
|
|
ifFalse: [
|
|
|
|
[ i >= end ] whileTrue: [
|
|
|
|
aBlock value: i.
|
|
|
|
i := i - step.
|
|
|
|
]
|
|
|
|
].
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-08 14:26:04 +00:00
|
|
|
#method to: end do: aBlock
|
2015-07-01 15:01:39 +00:00
|
|
|
{
|
2015-10-08 14:26:04 +00:00
|
|
|
^self to: end by: 1 do: aBlock.
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 14:26:04 +00:00
|
|
|
#class SmallInteger(Number)
|
2015-07-14 13:35:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-08 14:26:04 +00:00
|
|
|
#include 'Collection.st'.
|
|
|
|
#include 'Collection-ByteArray.st'.
|
|
|
|
#include 'Collection-Array.st'.
|
|
|
|
#include 'Collection-Set.st'.
|
2015-07-01 15:01:39 +00:00
|
|
|
|
|
|
|
|
2015-07-14 13:35:18 +00:00
|
|
|
#class(#pointer) Context(Apex)
|
2015-07-01 15:01:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#class(#pointer) MethodContext(Context)
|
|
|
|
{
|
|
|
|
#dcl sender ip sp ntmprs method receiver home origin.
|
|
|
|
|
|
|
|
#method pc
|
|
|
|
{
|
|
|
|
^ip
|
|
|
|
}
|
|
|
|
|
|
|
|
#method pc: anInteger
|
|
|
|
{
|
|
|
|
ip := anInteger.
|
|
|
|
"sp := sp - 1." "whould this always work??? "
|
|
|
|
}
|
|
|
|
|
|
|
|
#method sp
|
|
|
|
{
|
|
|
|
^sp.
|
|
|
|
|
|
|
|
}
|
|
|
|
#method sp: anInteger
|
|
|
|
{
|
|
|
|
sp := anInteger.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method pc: aPC sp: aSP
|
|
|
|
{
|
|
|
|
ip := aPC.
|
|
|
|
sp := aSP.
|
|
|
|
##sp := sp - 1.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#class(#pointer) BlockContext(Context)
|
|
|
|
{
|
|
|
|
#dcl caller ip sp ntmprs nargs source home origin.
|
|
|
|
|
|
|
|
#method value
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #blockValue>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method value: a
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #blockValue>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method value: a value: b
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #blockValue>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method value: a value: b value: c
|
|
|
|
{
|
2015-10-04 16:21:31 +00:00
|
|
|
<primitive: #blockValue>
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method whileTrue: aBlock
|
|
|
|
{
|
|
|
|
## http://stackoverflow.com/questions/2500483/is-there-a-way-in-a-message-only-language-to-define-a-whiletrue-message-without
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## ^(self value) ifTrue: [aBlock value. self whileTrue: aBlock].
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## less block context before whileTrue: is recursively sent.
|
|
|
|
## whileTrue: is sent in a method context.
|
|
|
|
## (self value) ifFalse: [^nil].
|
|
|
|
## aBlock value.
|
|
|
|
## self whileTrue: aBlock.
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
| pc sp xsp |
|
|
|
|
|
|
|
|
sp := thisContext sp.
|
|
|
|
sp := sp - 1. "decrement sp by 1 becuase thisContext pushed above affects the sp method"
|
|
|
|
pc := thisContext pc.
|
|
|
|
self value ifFalse: [ ^nil "^self" ].
|
|
|
|
aBlock value.
|
|
|
|
##thisContext pc: pc - 3 sp: sp.
|
|
|
|
##thisContext pc: pc + 2 sp: sp.
|
|
|
|
thisContext pc: pc + 1 sp: sp.
|
|
|
|
## this +2 or - 3 above is dependent on the byte code instruction size used for 'store'
|
|
|
|
## +2 to skip STORE_INTO_TEMP(pc) and POP_STACKTOP.
|
|
|
|
## TODO: make it independent of the byte code size
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## #<label>:
|
|
|
|
## thisContext pc: #<label> sp: sp.
|
|
|
|
##
|
|
|
|
## | pc |
|
|
|
|
## pc := thisContext pc.
|
|
|
|
## ^self value ifTrue: [aBlock value. thisContext pc: pc]
|
|
|
|
|
|
|
|
## ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
## self value ifTrue: [ aBlock value. thisContext restart. ].
|
|
|
|
}
|
|
|
|
|
|
|
|
#method pc
|
|
|
|
{
|
|
|
|
^ip
|
|
|
|
}
|
|
|
|
|
|
|
|
#method pc: anInteger
|
|
|
|
{
|
|
|
|
ip := anInteger.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method sp
|
|
|
|
{
|
|
|
|
^sp
|
|
|
|
}
|
|
|
|
|
|
|
|
#method sp: anInteger
|
|
|
|
{
|
|
|
|
sp := anInteger.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method restart
|
|
|
|
{
|
|
|
|
ip := source pc.
|
|
|
|
}
|
2015-10-08 14:26:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"------ TODO: -------------------------------------"
|
|
|
|
#method on: anError do: anExceptionBlock
|
|
|
|
{
|
|
|
|
"TODO: handle if anError is an ErrorSet .."
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ensure: aBlock
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#method ifCurtailed: aBlock
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
"------ TODO: -------------------------------------"
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#class(#pointer) CompiledMethod(Object)
|
|
|
|
{
|
2015-10-08 14:26:04 +00:00
|
|
|
#dcl owner preamble preamble_data_1 preamble_data_2 ntmprs nargs code source.
|
2015-07-01 15:01:39 +00:00
|
|
|
}
|
|
|
|
|
2015-10-03 15:29:03 +00:00
|
|
|
|
|
|
|
#class FFI(Object)
|
|
|
|
{
|
|
|
|
#dcl name handle funcs.
|
|
|
|
|
|
|
|
#method(#class) new: aString
|
|
|
|
{
|
|
|
|
^self new open: aString.
|
|
|
|
}
|
|
|
|
|
|
|
|
#method open: aString
|
|
|
|
{
|
|
|
|
self.funcs := Dictionary new.
|
|
|
|
self.name := aString.
|
|
|
|
self.handle := self privateOpen: self.name.
|
2015-10-08 14:26:04 +00:00
|
|
|
|
|
|
|
"[ self.handle := self privateOpen: self.name ]
|
|
|
|
on: Error do: [
|
|
|
|
]
|
|
|
|
on: XError do: [
|
|
|
|
]."
|
|
|
|
|
|
|
|
^self.
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method close
|
|
|
|
{
|
|
|
|
self privateClose: self.handle.
|
|
|
|
self.handle := nil.
|
|
|
|
}
|
|
|
|
|
2015-10-04 16:21:31 +00:00
|
|
|
#method call: aFunctionName withSig: aString withArgs: anArray
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
|
|
|
| f |
|
|
|
|
|
2015-10-04 16:21:31 +00:00
|
|
|
## f := self.funcs at: aFunctionName.
|
|
|
|
## f isNil ifTrue: [
|
|
|
|
## f := self privateGetSymbol: aFunctionName in: self.handle.
|
|
|
|
## f isNil ifTrue: [ self error: 'No such function' ].
|
|
|
|
## self.funcs at: aFunctionName put: f.
|
|
|
|
## ].
|
|
|
|
f := self privateGetSymbol: aFunctionName in: self.handle.
|
2015-10-08 14:26:04 +00:00
|
|
|
f isNil ifTrue: [ self error: 'No such function' ].
|
2015-10-03 15:29:03 +00:00
|
|
|
|
2015-10-04 16:21:31 +00:00
|
|
|
^self privateCall: f withSig: aString withArgs: anArray
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method privateOpen: aString
|
|
|
|
{
|
|
|
|
<primitive: #ffiOpen>
|
2015-10-08 14:26:04 +00:00
|
|
|
^nil. ## TODO: Error signal: 'can not open'
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#method privateClose: aHandle
|
|
|
|
{
|
|
|
|
<primitive: #ffiClose>
|
|
|
|
}
|
|
|
|
|
2015-10-04 16:21:31 +00:00
|
|
|
#method privateCall: aSymbol withSig: aString withArgs: anArray
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
|
|
|
<primitive: #ffiCall>
|
|
|
|
}
|
|
|
|
|
|
|
|
#method privateGetSymbol: aString in: aHandle
|
|
|
|
{
|
|
|
|
<primitive: #ffiGetSym>
|
2015-10-08 14:26:04 +00:00
|
|
|
^nil.
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|