Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
934 lines (789 sloc) 28.7 KB
(* Copyright (c) 2001-2015 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*)
class Kernel vmMirror: vmMirror = Object <: Value (
| private vmMirror = vmMirror.
private ObjectSlot = Object.
private ValueSlot = Value.
|)(
private class Top = ()()
public class Thing = Top ()(
protected error: msg = ( vmMirror printNewline: 'ERROR: ' + msg. vmMirror exit: 1 )
(* Error recovering *)
protected doesNotUnderstand: selector arguments: arguments = (
(* vmMirror printStackTrace: nil.
error: 'Method ' + selector + ' not found in class ' + (vmMirror mirrorClassName: self).
*)
MessageNotUnderstood signalWith: self selector: selector arguments: arguments.
)
protected escapedBlock: block = (
error: 'Block has escaped and cannot be executed'
)
(* Abstract method support *)
protected subclassResponsibility = (
error: 'This method is abstract and should be overridden'
)
(* Evaluating *)
public value = ( ^ self )
(* Converting, Convenience *)
public , element = ( ^ (Vector new append: self) append: element )
public hashcode = ( ^ vmMirror objHashcode: self )
(* Newspeak requirements
This works around restrictions by the absense of a global scope,
we use inheritance instead. *)
public Object = ( ^ ObjectSlot )
public Value = ( ^ ValueSlot )
public kernel = ( ^ outer Kernel )
(* Nil *)
public isNil = ( ^ false )
public notNil = ( ^ true )
public ifNil: aBlock = ( ^ self )
public ifNotNil: aBlock = ( ^ aBlock value )
public ifNil: noGoBlock ifNotNil: goBlock = ( ^ goBlock value: self )
(* Debugging *)
public print = ( self asString print )
public println = ( vmMirror printNewline: self asString )
public halt = ( vmMirror halt: self )
public asString = ( ^ 'instance of ' + (vmMirror objClassName: self) )
)
public class Object = Thing ()
(
public == other = ( ^ vmMirror object: self identicalTo: other )
public ~= other = ( ^ (self == other) not )
)
public class Value = Thing ()
(
public = other = ( ^ vmMirror value: self sameAs: other )
public <> other = ( ^ (self = other) not )
public copy = ( ^ self )
)
(* Transfer objects are handled almost identically to normal objects.
They are mutable. The main difference to normal objects is that they are
passed by copy when send to other actors.
Thus, transfer objects are useful to pass structured data to other
objects that cannot be guaranteed to satisfy the semantics of Values.
When sent in a message, transfer objects are deeply copied.
The deep copy ends when reaching Values, normal Objects, or far references.
References to values are left unchanged.
References to objects are turned into far references.
Far references are replaced with direct references when the receiving actor
is the owner of the far-referenced object.
*)
public class TransferObject = Object ()()
public class Class = Object ()
(
(* Allocation *)
public new = ( self error: 'never execute should be:' (* ^ (vmMirror instantiate: self) initialize`new *) )
)
public class Metaclass = Class ()()
public class Boolean = Value ()
(
(* Conditional evaluation *)
public ifTrue: trueBlock ifFalse: falseBlock = (
self ifTrue: [ ^ trueBlock value ].
self ifFalse: [ ^ falseBlock value ].
)
(* Logical operations *)
public || boolean = ( ^ self or: boolean )
public && boolean = ( ^ self and: boolean )
)
public class True = Boolean ()
(
(* Converting *)
public asString = ( ^ 'true' )
(* Conditional evaluation *)
public ifTrue: block = ( ^ block value )
public ifFalse: block = ( ^ nil )
(* Logical operations *)
public not = ( ^ false )
public or: block = ( ^ true )
public and: block = ( ^ block value )
)
public class False = Boolean ()
(
(* Converting *)
public asString = ( ^'false' )
(* Conditional evaluation *)
public ifTrue: block = ( ^ nil )
public ifFalse: block = ( ^ block value )
(* Logical operations *)
public not = ( ^ true )
public or: block = ( ^ block value )
public and: block = ( ^ false )
)
public class Nil = Value ()
(
(* Converting *)
public asString = ( ^ 'nil' )
(* Comparing *)
public isNil = ( ^ true )
public notNil = ( ^ false )
(* Convenience *)
public == other = ( ^ vmMirror object: self identicalTo: other )
public ~= other = ( ^ (self == other) not )
public ifNil: aBlock = ( ^ aBlock value )
public ifNotNil: aBlock = ( ^ self )
public ifNil: goBlock ifNotNil: noGoBlock = ( ^ goBlock value )
)
public class Integer = Value ()
(
(* Arithmetic *)
public + argument = ( ^ vmMirror int: self add: argument )
public - argument = ( ^ vmMirror int: self subtract: argument )
public * argument = ( ^ vmMirror int: self multiply: argument )
public / argument = ( ^ vmMirror int: self divideBy: argument )
public // argument = ( ^ vmMirror int: self divideDouble: argument )
public % argument = ( ^ vmMirror int: self modulo: argument ) (* modulo with sign of divisor *)
public rem: argument=( ^ vmMirror int: self reminder: argument ) (* modulo with sign of dividend *)
public abs = ( ^ (self < 0) ifTrue: (0 - self) ifFalse: self )
public sqrt = ( ^ vmMirror intSqrt: self )
public negated = ( ^ 0 - self )
public round = ( ^ self ) (* added to be polymorphic with Double *)
(* Bit operations *)
public & argument = ( ^ vmMirror int: self bitAnd: argument )
public << argument = ( ^ vmMirror int: self leftShift: argument )
public >>> argument= ( ^ vmMirror int: self unsignedRightShift: argument )
public bitXor: argument = ( ^ vmMirror int: self bitXor: argument )
(* Comparing *)
public = argument = ( ^ vmMirror int: self equals: argument )
public ~= argument = ( ^ (self = argument) not )
public < argument = ( ^ vmMirror int: self lessThan: argument )
public > argument = ( ^ (self >= argument) and: [ self <> argument ] )
public >= argument = ( ^ (self < argument) not )
public <= argument = ( ^ (self < argument) or: [ self = argument ] )
public negative = ( ^ self < 0 )
public between: a and: b = ( ^ (self > a) and: [ self < b ] )
public compareTo: other = ( ^ self < other ifTrue: [ - 1 ] ifFalse: [ self = other ifTrue: [ 0 ] ifFalse: [ 1 ] ] )
(* Converting *)
public asString = ( ^ vmMirror intAsString: self )
public as32BitSignedValue = ( ^ vmMirror intAs32BitSignedValue: self ) (* returns an int, with the value that a signed 32-bit integer would have *)
public as32BitUnsignedValue = ( ^ vmMirror intAs32BitUnsignedValue: self ) (* returns an int, with the value that a unsigned 32-bit integer would have *)
public hashcode = ( ^ self )
(* Iterating *)
public to: limit do: block = (
self to: limit by: 1 do: block
)
public to: limit by: step do: block = (
| i |
i := self.
[ i <= limit ] whileTrue: [ block value: i. i := i + step ]
)
public downTo: limit do: block = (
self downTo: limit by: 1 do: block
)
public downTo: limit by: step do: block = (
| i |
i := self.
[ i >= limit ] whileTrue: [ block value: i. i := i - step ]
)
(* More Iterations *)
public timesRepeat: block = (
1 to: self do: [ :i | block value ]
)
(* Range Creation *)
public to: upper = (
| range |
range := Array new: upper - self + 1.
self to: upper do: [ :i | range at: i put: i ].
^ range
)
public min: otherInt = (
self < otherInt ifTrue: [ ^ self ] ifFalse: [ ^ otherInt ]
)
public max: otherInt = (
self < otherInt ifTrue: [ ^ otherInt ] ifFalse: [ ^ self ]
)
) : (
public fromString: aString = ( ^ vmMirror intFromString: aString )
)
public class Double = Value ()
(
(* Arithmetic *)
public + argument = ( ^ vmMirror double: self add: argument )
public - argument = ( ^ vmMirror double: self subtract: argument )
public * argument = ( ^ vmMirror double: self multiply: argument )
public // argument = ( ^ vmMirror double: self divideDouble: argument )
public % argument = ( ^ vmMirror double: self modulo: argument )
public abs = ( ^ (self < 0.0) ifTrue: (0.0 - self) ifFalse: self )
public cos = ( ^ vmMirror doubleCos: self )
public sin = ( ^ vmMirror doubleSin: self )
public exp = ( ^ vmMirror doubleExp: self )
public log = ( ^ vmMirror doubleLog: self )
public sqrt = ( ^ vmMirror doubleSqrt: self )
public negated = ( ^ 0.0 - self )
public round = ( ^ vmMirror doubleRound: self )
(* Comparing *)
public = argument = ( ^ vmMirror double: self equals: argument )
public < argument = ( ^ vmMirror double: self lessThan: argument )
public > argument = ( ^ (self >= argument) and: [ self <> argument ] )
public >= argument = ( ^ (self < argument) not )
public <= argument = ( ^ (self < argument) or: [ self = argument ] )
public negative = ( ^ self < 0.0 )
public between: a and: b = ( ^ (self > a) and: [ self < b ] )
(* Converting *)
public asString = ( ^ vmMirror doubleAsString: self )
public asInteger = ( ^ vmMirror doubleAsInteger: self )
(* Iterating *)
public to: limit do: block = (
| i |
i := self.
[ i <= limit ] whileTrue: [ block value: i. i := i + 1.0 ]
)
public downTo: limit do: block = (
| i |
i := self.
[ i >= limit ] whileTrue: [ block value: i. i := i - 1.0 ]
)
) : (
public PositiveInfinity = ( ^ vmMirror doublePositiveInfinity: nil )
)
public class String = Value ()
(
(* Concatenate: returns a new string object *)
public concatenate: argument = ( ^ vmMirror string: self concat: argument )
public + argument = ( ^ self concatenate: argument asString )
(* Converting *)
public asString = ( ^ self )
public asSymbol = ( ^ vmMirror stringAsSymbol: self )
public hashcode = ( ^ vmMirror stringHashcode: self )
(* Info *)
public length = ( ^ vmMirror stringLength: self )
(* Comparing *)
public = argument = ( ^ vmMirror string: self equals: argument )
(* substring: from 'start' to (and including) 'end'. *)
private primSubstringFrom: start to: end = ( ^ vmMirror string: self
substringFrom: start to: end )
public substringFrom: start to: end = (
((end <= self length) && (start > 0) && (start <= end))
ifTrue: [^ self primSubstringFrom: start to: end]
ifFalse: [
| idx |
(end > self length) ifTrue: [idx := end].
(start < 0) || (start > end) ifTrue: [idx := start].
IndexOutOfBounds signalWith: self index: idx.
]
)
public beginsWith: prefix = (
self length < prefix length ifTrue: [ ^ false ].
1 to: prefix length do: [:i |
((self charAt: i) = (prefix charAt: i)) ifFalse: [ ^ false ].
].
^ true
)
public endsWith: suffix = (
| l sufL |
l := self length.
sufL := suffix length.
l < sufL ifTrue: [ ^ false ].
1 to: sufL do: [:i |
(self charAt: l - sufL + i) = (suffix charAt: i) ifFalse: [ ^ false ]
].
^ true
)
public includes: aString = (
| l |
l := aString length.
l > self length ifTrue: [ ^ false ].
l = self length ifTrue: [
^ aString = self ].
1 to: self length - l + 1 do: [:i |
(self primSubstringFrom: i to: i + l - 1) = aString ifTrue: [ ^ true ].
].
^ false
)
public indexOf: aString = (
^ self indexOf: aString startingAt: 1
)
public indexOf: aString startingAt: start = (
| l |
l := aString length.
start + l > (self length + 1) ifTrue: [ ^ 0 ].
start to: self length - l + 1 do: [:i |
(self primSubstringFrom: i to: i + l - 1) = aString ifTrue: [ ^ i ].
].
^ 0
)
public split: split = (
| start newStart result |
self length < split length ifTrue: [ ^ Array new: self ].
start := 1.
result := Vector new.
[start > 0] whileTrue: [
newStart := self indexOf: split startingAt: start.
newStart = 0
ifTrue: [
result append: (self primSubstringFrom: start to: self length).
^ result ]
ifFalse: [
result append: (self primSubstringFrom: start to: newStart - 1).
start := newStart + split length. ].
].
^ result
)
public asInteger = (
^ Integer fromString: self
)
public charAt: argument = (
^ self substringFrom: argument to: argument
)
(* Printing *)
public print = ( vmMirror printString: self )
)
public class Symbol = String ()
(
(* Converting *)
public asString = ( ^ vmMirror symbolAsString: self )
public asSymbol = ( ^ self )
(* Printing *)
public print = ( '#' print. super print )
)
public class ArrayReadMixin = ()(
public first = ( ^ at: 1 )
public last = ( ^ at: size )
(* Iterating *)
public do: block = ( self doIndexes: [ :i |
block value: (self at: i) ] )
public doIndexes: block = ( 1 to: self size do: [:i |
block value: i. ] )
public from: start to: end do: block = ( | i |
start to: end do: [:i |
block value: (self at: i) ] )
public inject: sub into: aBlock = ( | next |
next := sub.
self do: [ :e | next := aBlock value: next with: e ].
^ next
)
(* Numerical *)
public sum = ( ^ self inject: 0 into: [ :sub :elem | sub + elem ] )
public average = ( ^ self sum / self size )
(* Filter *)
public select: aBlock = (
| newCollection |
newCollection := Vector new: self size.
self do: [:each | (aBlock value: each)
ifTrue: [newCollection append: each]].
^ newCollection
)
public reject: aBlock = (
^ self select: [:element | (aBlock value: element) == false ]
)
public union: aCollection = (
| new |
new := Set new.
new addAll: self.
new addAll: aCollection.
^ new
)
(* Containment/inclusion check *)
public contains: element = ( self do: [ :e | e = element ifTrue: [ ^ true ] ].
^ false )
public indexOf: element = (
self doIndexes: [ :i | (self at: i) = element ifTrue: [ ^ i ]].
^ nil
)
public lastIndexOf: element = (
self size downTo: 1 do: [: i | (self at: i) = element ifTrue: [ ^ i ]].
^ nil
)
(* Collection *)
public collect: aBlock = ( (* also known as 'map', we use it for
ValueArray and Array, and both times create a Array, because we can't be
sure that the block will return a Value *)
| result |
result := Array new: self size.
self doIndexes: [ :i | result at: i put: (aBlock value: (self at: i)) ].
^ result
)
)
public class Array = Object <: ArrayReadMixin ()
(
(* Accessing *)
public at: index = ( ^ vmMirror array: self at: index )
public at: index put: value = ( ^ vmMirror array: self at: index put: value )
public size = ( ^ vmMirror arraySize: self )
public putAll: block = ( self doIndexes: [ :i |
self at: i put: block value ] )
(* Copying (inclusively) *)
public copyFrom: start to: end = ( | result i |
result := Array new: end - start + 1.
i := 1.
self from: start to: end do: [ :e |
result at: i put: e.
i := i + 1 ].
^ result
)
public copyFrom: start = ( ^ self copyFrom: start to: self size )
(* Copy range to other collection *)
public copyFrom: start to: end to: collection = (
start to: end do: [:i |
collection at: i put: (self at: i) (* TODO: look up bug??? self should not be necessary here *)
]
)
public replaceFrom: start to: stop with: replacement startingAt: repStart = (
(* This destructively replaces elements from start to stop in the
receiver starting at index, repStart, in the sequenceable collection,
replacementCollection. Answer the receiver. No range checks are
performed. *)
| index repOff |
repOff := repStart - start.
index := start - 1.
[(index := index + 1) <= stop]
whileTrue: [self at: index put: (replacement at: repOff + index)]
)
public copy = ( ^ self copyFrom: 1 )
) : (
(* Allocation *)
public new = ( ^ self new: 0 )
public new: size = ( ^ vmMirror array: self new: size )
public new: size withAll: block = ( ^ (self new: size) putAll: block )
(* Convenience *)
public with: a = (
| arr |
arr := self new: 1.
arr at: 1 put: a.
^ arr
)
public with: a with: b = (
| arr |
arr := self new: 2.
arr at: 1 put: a.
arr at: 2 put: b.
^ arr
)
public with: a with: b with: c = (
| arr |
arr := self new: 3.
arr at: 1 put: a.
arr at: 2 put: b.
arr at: 3 put: c.
^ arr
)
)
public class TransferArray new: size = Array new: size <: TransferObject ()(
) : (
public new: size withAll: block = ( ^ (self new: size) putAll: block )
)
public class ValueArray new: size withAll: aBlock = Value <: ArrayReadMixin (
(* Conceptually: 1 to: size do: [:i | at: i put: (aBlock value: i) ] *)
)(
(* Accessing *)
public at: index = ( ^ vmMirror array: self at: index )
public size = ( ^ vmMirror arraySize: self )
public copy = ( ^ self )
):(
public new: size withAll: aBlock = ( vmMirror valueArrayNew: size withAll: aBlock )
)
public class Block = Object ()
(
(* Evaluation *)
public value = ( ^ vmMirror blockValue: self )
(* Looping *)
public whileFalse: block = (
[ self value not ] whileTrue: block
)
public whileTrue: block = (
self value ifFalse: [ ^ nil ].
block value.
self restart
)
(* Exception support *)
public on: exception do: aBlock = (
^ vmMirror exceptionDo: self catch: exception onException: aBlock
)
public ensure: aBlock = (
^ vmMirror exceptionDo: self ensure: aBlock
)
(* TODO: this feels still like old legacy, it's not how it works...
on the other hand, seeing the #whileTrue: semantics is useful.
so, replacing it directly with a primitive call isn't nice either *)
(* Restarting *)
public restart = ( ^ vmMirror blockRestart: self )
)
protected class Block1 = Block ()()
protected class Block2 = Block ()
(
(* Evaluating *)
public value = ( self value: nil )
public value: argument = ( ^ vmMirror blockValue: self with: argument )
)
protected class Block3 = Block ()
(
(* Evaluating *)
public value = ( self value: nil with: nil )
public value: arg = ( self value: arg with: nil )
public value: arg1 with: arg2 = ( ^ vmMirror blockValue: self with: arg1 with: arg2 )
)
public class Pair withKey: aKey andValue: aValue = (
| public key ::= aKey.
public value ::= aValue. |
) (
(* Printing *)
print = ( '[' print. key print. '=>' print. value print. ']' print )
println = ( self print. '' println )
)
public class Vector new: size = (
| private firstIdx ::= 1.
private lastIdx ::= 1.
private storage ::= Array new: size. |
) (
(* Accessing *)
public at: index = (
self checkIndex: index.
^ storage at: index
)
public at: index put: value = (
self checkIndex: index.
storage at: index put: value.
)
public first = ( ^ isEmpty ifTrue: [nil] ifFalse: [storage at: firstIdx] )
(* Iterating *)
public do: block = (
firstIdx to: lastIdx - 1 do: [ :i | block value: (storage at: i) ]
)
public doIndexes: block = (
firstIdx to: lastIdx - 1 do: block
)
public collect: aBlock = ( (* also known as 'map' *)
| result |
result := Vector new: self size.
self doIndexes: [ :i |
result append: (aBlock value: (self at: i)) ].
^ result
)
public select: aBlock = (
| newCollection |
newCollection := Vector new: self size.
self do: [:each | (aBlock value: each)
ifTrue: [newCollection append: each]].
^ newCollection
)
(* Adding *)
public , element = ( ^ self append: element )
public append: element = (
(lastIdx > storage size) ifTrue: [
(* Need to expand capacity first *)
| newStorage |
newStorage := Array new: 2 * storage size.
storage doIndexes: [ :i | newStorage at: i put: (storage at: i) ].
storage := newStorage. ].
storage at: lastIdx put: element.
lastIdx := lastIdx + 1.
^ self
)
public appendAll: collection = (
collection do: [:e | append: e ]
)
(* Removing *)
public remove = (
lastIdx > firstIdx
ifTrue: [ lastIdx := lastIdx - 1. ^storage at: lastIdx ]
ifFalse: [
EmptyCollectionException signalWith: self idx: lastIdx ]
)
public remove: object = (
| newArray newLast found |
newArray := Array new: self capacity.
newLast := 1.
found := false.
self do: [ :it |
it == object
ifTrue: [ found := true ]
ifFalse: [
newArray at: newLast put: it.
newLast := newLast + 1.
]
].
storage := newArray.
lastIdx := newLast.
firstIdx := 1.
^ found
)
(* Preserves capacity *)
public removeAll = (
storage := Array new: storage size.
)
public contains: anObject = (
^ storage contains: anObject
)
public anySatisfy: aBlock = (
do: [:it | (aBlock value: it) ifTrue: [^ true]].
^ false
)
public detect: aBlock ifNone: defaultBlock = (
self do: [:e | (aBlock value: e) ifTrue: [ ^ e ]].
^ defaultBlock value
)
(* Printing *)
public println = (
'Vector[' print.
firstIdx print. ', ' print.
lastIdx print. ': ' print.
self do: [ :it | '(' print. it print. ')' print ].
']' println
)
(* Sizing *)
public isEmpty = ( ^ lastIdx = firstIdx )
public size = ( ^ lastIdx - firstIdx )
public capacity = ( ^ storage size )
(* Conversion *)
public asArray = ( | arr |
arr := Array new: self size.
self doIndexes: [ :i | arr at: i put: (self at: i) ].
^arr
)
checkIndex: index = (
(firstIdx <= index) && (index < lastIdx)
ifFalse: [
IndexOutOfBounds signalWith: self index: index
].
)
public removeFirst = (
self isEmpty ifTrue: [ self error: 'Vector is empty' ].
firstIdx := firstIdx + 1.
^ storage at: firstIdx - 1
)
sort: i to: j with: sortBlock = (
(* Sort elements i through j of self to be non-descending according to
sortBlock. *)
| di dij dj tt ij k l n |
sortBlock isNil ifTrue: [ ^ self defaultSort: i to: j ].
(* The prefix d means the data at that index. *)
(n := j + 1 - i) <= 1 ifTrue: [ ^ self ]. (* Nothing to sort. *)
(* Sort di,dj. *)
di := storage at: i.
dj := storage at: j.
(* i.e., should di precede dj? *)
(sortBlock value: di with: dj) ifFalse: [
storage swap: i with: j.
tt := di.
di := dj.
dj := tt
].
n > 2 ifTrue: [ (* More than two elements. *)
ij := (i + j) / 2. (* ij is the midpoint of i and j. *)
dij := storage at: ij. (* Sort di,dij,dj. Make dij be their median. *)
(sortBlock value: di with: dij)
ifTrue: [ (* i.e. should di precede dij? *)
(sortBlock value: dij with: dj) ifFalse: [ (* i.e., should dij precede dj? *)
storage swap: j with: ij.
dij := dj]]
ifFalse: [ (* i.e. di should come after dij *)
storage swap: i with: ij.
dij := di].
n > 3 ifTrue: [
(* More than three elements.
Find k>i and l<j such that dk,dij,dl are in reverse order.
Swap k and l. Repeat this procedure until k and l pass each other. *)
k := i.
l := j.
[ [l := l - 1. k <= l and: [sortBlock value: dij with: (storage at: l)]]
whileTrue. (* i.e. while dl succeeds dij *)
[k := k + 1. k <= l and: [sortBlock value: (storage at: k) with: dij]]
whileTrue. (* i.e. while dij succeeds dk *)
k <= l]
whileTrue:
[ storage swap: k with: l ].
(* Now l<k (either 1 or 2 less), and di through dl are all less than or equal to dk
through dj. Sort those two segments. *)
self sort: i to: l with: sortBlock.
self sort: k to: j with: sortBlock
]
]
)
public sort: aBlock = (
(* Make the argument, aBlock, be the criterion for ordering elements of
the receiver.
sortBlocks with side effects may not work right *)
self size > 0 ifTrue: [
self sort: firstIdx
to: lastIdx - 1
with: aBlock
]
)
(* Conversion *)
public asSet = (
^ Set new addAll: self
)
) : (
public new = ( ^ self new: 50 )
public with: elem = (
| newVector |
newVector := self new: 1.
newVector append: elem.
^ newVector
)
)
public class Exception = ()(
(* Exception does not have any state on purpose.
It is good practice to have distinct exceptions for separate issues.
Thus, subclassing is encouraged. *)
public signal = ( vmMirror signalException: self )
):(
public signal = (
^ self new signal
)
)
public class IndexOutOfBounds signalWith: obj index: idx = Exception (
| public target = obj.
public index = idx. |
self signal
)()
public class EmptyCollectionException signalWith: obj index: idx = Exception (
| public target = obj.
public index = idx. |
self signal
)()
public class MessageNotUnderstood signalWith: obj selector: sel arguments: args = Exception (
| public receiver = obj.
public selector = sel.
public arguments = args. |
self signal.
)(
public asString = (
^ 'MessageNotUnderstood(' + (vmMirror objClassName: receiver) + '>>#' + selector + ')'
)
)
(* Exception signaled when a Value is instantiated referring to a mutable object. *)
public class NotAValue signalWith: aClass = Exception (
| public class = aClass. |
(* We can't refer here to the object that is instantiated, because we do not
want to expose illegal objects.
WARNING: it is still possible that illegal objects escape, because
initializers can do what ever they want, and we check afterwards. *)
self signal.
)(
public asString = (
^ 'NotAValue(' + (vmMirror mirrorAClassesName: class) + ')'
)
)
public class TransferObjectsCannotBeValues signalWith: aClass = Exception (
| public class = aClass. |
self signal.
)(
public asString = (
^ 'TransferObjectsCannotBeValues(' + (vmMirror mirrorAClassesName: class) + ')'
)
)
(* Short cut from VM. Likely to be removed when optimized. *)
private signalNotAValueWith: aClass = (
NotAValue signalWith: aClass
)
(* Short cut from VM. Likely to be removed when optimized. *)
private signalTOCannotBeValues: aClass = (
TransferObjectsCannotBeValues signalWith: aClass
)
)