123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659 |
- Smalltalk current createPackage: 'Kernel-Objects' properties: #{}!
- nil subclass: #Object
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !Object commentStamp!
- *Object is the root of the Smalltalk class system*. All classes in the system are subclasses of Object.
- Object provides default behavior common to all normal objects, such as:
- - access
- - copying
- - comparison
- - error handling
- - message sending
- - reflection
- Also utility messages that all objects should respond to are defined here.
- Object has no instance variable.
- ##Access
- Instance variables can be accessed with `#instVarAt:` and `#instVarAt:put:`. `Object >> instanceVariableNames` answers a collection of all instance variable names.
- Accessing JavaScript properties of an object is done through `#basicAt:`, `#basicAt:put:` and `basicDelete:`.
- ##Copying
- Copying an object is handled by `#copy` and `#deepCopy`. The first one performs a shallow copy of the receiver, while the second one performs a deep copy.
- The hook method `#postCopy` can be overriden in subclasses to copy fields as necessary to complete the full copy. It will be sent by the copy of the receiver.
- ##Comparison
- Objects understand equality `#=` and identity `#==` comparison.
- ##Error handling
- - `#halt` is the typical message to use for inserting breakpoints during debugging.
- - `#error:` throws a generic error exception
- - `#doesNotUnderstand:` handles the fact that there was an attempt to send the given message to the receiver but the receiver does not understand this message.
- Overriding this message can be useful to implement proxies for example.!
- !Object methodsFor: 'accessing'!
- basicAt: aString
- <return self[aString]>
- !
- basicAt: aString put: anObject
- <return self[aString] = anObject>
- !
- basicDelete: aString
- <delete self[aString]; return aString>
- !
- class
- <return self.klass>
- !
- identityHash
- <
- var hash=self.identityHash;
- if (hash) return hash;
- hash=smalltalk.nextId();
- Object.defineProperty(self, 'identityHash', {value:hash});
- return hash;
- >
- !
- instVarAt: aSymbol
- | varname |
- varname := aSymbol asString.
- <return self['@'+varname]>
- !
- instVarAt: aSymbol put: anObject
- | varname |
- varname := aSymbol asString.
- <self['@' + varname] = anObject>
- !
- size
- self error: 'Object not indexable'
- !
- value
- <return self.valueOf()>
- !
- yourself
- ^ self
- ! !
- !Object methodsFor: 'comparing'!
- = anObject
- ^self == anObject
- !
- == anObject
- ^self identityHash = anObject identityHash
- !
- ~= anObject
- ^(self = anObject) = false
- !
- ~~ anObject
- ^(self == anObject) = false
- ! !
- !Object methodsFor: 'converting'!
- -> anObject
- ^Association key: self value: anObject
- !
- asJSON
- | variables |
- variables := HashedCollection new.
- self class allInstanceVariableNames do: [:each |
- variables at: each put: (self instVarAt: each) asJSON].
- ^variables
- !
- asJSONString
- ^JSON stringify: self asJSON
- !
- asJavascript
- ^self asString
- !
- asString
- ^self printString
- ! !
- !Object methodsFor: 'copying'!
- copy
- ^self shallowCopy postCopy
- !
- deepCopy
- <
- var copy = self.klass._new();
- for(var i in self) {
- if(/^@.+/.test(i)) {
- copy[i] = self[i]._deepCopy();
- }
- }
- return copy;
- >
- !
- postCopy
- !
- shallowCopy
- <
- var copy = self.klass._new();
- for(var i in self) {
- if(/^@.+/.test(i)) {
- copy[i] = self[i];
- }
- }
- return copy;
- >
- ! !
- !Object methodsFor: 'error handling'!
- deprecatedAPI
- "Just a simple way to deprecate methods.
- #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
- but it could in the future."
- console warn: thisContext home asString, ' is deprecated!! (in ', thisContext home home asString, ')'
- !
- doesNotUnderstand: aMessage
- MessageNotUnderstood new
- receiver: self;
- message: aMessage;
- signal
- !
- error: aString
- Error signal: aString
- !
- halt
- self error: 'Halt encountered'
- !
- shouldNotImplement
- self error: 'This method should not be implemented in ', self class name
- !
- subclassResponsibility
- self error: 'This method is a responsibility of a subclass'
- !
- throw: anObject
- < throw anObject >
- !
- try: aBlock catch: anotherBlock
- <try{return aBlock()} catch(e) {return anotherBlock(e)}>
- ! !
- !Object methodsFor: 'initialization'!
- initialize
- ! !
- !Object methodsFor: 'message handling'!
- basicPerform: aSymbol
- ^self basicPerform: aSymbol withArguments: #()
- !
- basicPerform: aSymbol withArguments: aCollection
- <return self[aSymbol].apply(self, aCollection);>
- !
- perform: aSymbol
- ^self perform: aSymbol withArguments: #()
- !
- perform: aSymbol withArguments: aCollection
- | selector |
- selector := aSymbol asSelector.
- <return smalltalk.send(self, selector, aCollection)>
- ! !
- !Object methodsFor: 'printing'!
- log: aString block: aBlock
- | result |
- console log: aString, ' time: ', (Date millisecondsToRun: [result := aBlock value]) printString.
- ^result
- !
- printNl
- <console.log(self)>
- !
- printString
- ^'a ', self class name
- !
- storeOn: aStream
- aStream nextPutAll: self printString
- !
- storeString
- "Answer a String representation of the receiver from which the receiver
- can be reconstructed."
- ^ String streamContents: [:s | self storeOn: s]
- ! !
- !Object methodsFor: 'testing'!
- ifNil: aBlock
- "inlined in the Compiler"
- ^self
- !
- ifNil: aBlock ifNotNil: anotherBlock
- "inlined in the Compiler"
- ^anotherBlock value
- !
- ifNotNil: aBlock
- "inlined in the Compiler"
- ^aBlock value
- !
- ifNotNil: aBlock ifNil: anotherBlock
- "inlined in the Compiler"
- ^aBlock value
- !
- isClass
- ^false
- !
- isKindOf: aClass
- ^(self isMemberOf: aClass)
- ifTrue: [true]
- ifFalse: [self class inheritsFrom: aClass]
- !
- isMemberOf: aClass
- ^self class = aClass
- !
- isMetaclass
- ^false
- !
- isNil
- ^false
- !
- isNumber
- ^false
- !
- isParseFailure
- ^false
- !
- isString
- ^false
- !
- isSymbol
- ^false
- !
- notNil
- ^self isNil not
- !
- respondsTo: aSelector
- ^self class canUnderstand: aSelector
- ! !
- !Object class methodsFor: 'initialization'!
- initialize
- "no op"
- ! !
- Object subclass: #Boolean
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !Boolean commentStamp!
- Boolean wraps the JavaScript `Boolean()` constructor. The `true` and `false` objects are the JavaScript boolean objects.
- Boolean defines the protocol for logic testing operations and conditional control structures for the logical values.
- Boolean instances are weither `true` or `false`.!
- !Boolean methodsFor: 'comparing'!
- = aBoolean
- aBoolean class = self class ifFalse: [^false].
- <return Boolean(self == true) == aBoolean>
- !
- == aBoolean
- ^self = aBoolean
- ! !
- !Boolean methodsFor: 'controlling'!
- & aBoolean
- <
- if(self == true) {
- return aBoolean;
- } else {
- return false;
- }
- >
- !
- and: aBlock
- ^self = true
- ifTrue: aBlock
- ifFalse: [false]
- !
- ifFalse: aBlock
- "inlined in the Compiler"
- ^self ifTrue: [] ifFalse: aBlock
- !
- ifFalse: aBlock ifTrue: anotherBlock
- "inlined in the Compiler"
- ^self ifTrue: anotherBlock ifFalse: aBlock
- !
- ifTrue: aBlock
- "inlined in the Compiler"
- ^self ifTrue: aBlock ifFalse: []
- !
- ifTrue: aBlock ifFalse: anotherBlock
- "inlined in the Compiler"
- <
- if(self == true) {
- return aBlock();
- } else {
- return anotherBlock();
- }
- >
- !
- not
- ^self = false
- !
- or: aBlock
- ^self = true
- ifTrue: [true]
- ifFalse: aBlock
- !
- | aBoolean
- <
- if(self == true) {
- return true;
- } else {
- return aBoolean;
- }
- >
- ! !
- !Boolean methodsFor: 'converting'!
- asJSON
- ^self
- ! !
- !Boolean methodsFor: 'copying'!
- deepCopy
- ^self
- !
- shallowCopy
- ^self
- ! !
- !Boolean methodsFor: 'printing'!
- printString
- <return self.toString()>
- ! !
- Object subclass: #Date
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !Date commentStamp!
- The Date class is used to work with dates and times. Therefore `Date today` and `Date now` are both valid in
- Amber and answer the same date object.
- Date wraps the `Date()` JavaScript constructor, and Smalltalk date objects are JavaScript date objects.!
- !Date methodsFor: 'accessing'!
- day
- ^self dayOfWeek
- !
- day: aNumber
- self dayOfWeek: aNumber
- !
- dayOfMonth
- <return self.getDate()>
- !
- dayOfMonth: aNumber
- <self.setDate(aNumber)>
- !
- dayOfWeek
- <return self.getDay() + 1>
- !
- dayOfWeek: aNumber
- <return self.setDay(aNumber - 1)>
- !
- hours
- <return self.getHours()>
- !
- hours: aNumber
- <self.setHours(aNumber)>
- !
- milliseconds
- <return self.getMilliseconds()>
- !
- milliseconds: aNumber
- <self.setMilliseconds(aNumber)>
- !
- minutes
- <return self.getMinutes()>
- !
- minutes: aNumber
- <self.setMinutes(aNumber)>
- !
- month
- <return self.getMonth() + 1>
- !
- month: aNumber
- <self.setMonth(aNumber - 1)>
- !
- seconds
- <return self.getSeconds()>
- !
- seconds: aNumber
- <self.setSeconds(aNumber)>
- !
- time
- <return self.getTime()>
- !
- time: aNumber
- <self.setTime(aNumber)>
- !
- year
- <return self.getFullYear()>
- !
- year: aNumber
- <self.setFullYear(aNumber)>
- ! !
- !Date methodsFor: 'arithmetic'!
- + aDate
- <return self + aDate>
- !
- - aDate
- <return self - aDate>
- ! !
- !Date methodsFor: 'comparing'!
- < aDate
- <return self < aDate>
- !
- <= aDate
- <return self <= aDate>
- !
- > aDate
- <return self >> aDate>
- !
- >= aDate
- <return self >>= aDate>
- ! !
- !Date methodsFor: 'converting'!
- asDateString
- <return self.toDateString()>
- !
- asLocaleString
- <return self.toLocaleString()>
- !
- asMilliseconds
- ^self time
- !
- asNumber
- ^self asMilliseconds
- !
- asString
- <return self.toString()>
- !
- asTimeString
- <return self.toTimeString()>
- ! !
- !Date methodsFor: 'printing'!
- printString
- ^self asString
- ! !
- !Date class methodsFor: 'instance creation'!
- fromMilliseconds: aNumber
- ^self new: aNumber
- !
- fromSeconds: aNumber
- ^self fromMilliseconds: aNumber * 1000
- !
- fromString: aString
- "Example: Date fromString('2011/04/15 00:00:00')"
- ^self new: aString
- !
- millisecondsToRun: aBlock
- | t |
- t := Date now.
- aBlock value.
- ^Date now - t
- !
- new: anObject
- <return new Date(anObject)>
- !
- now
- ^self today
- !
- today
- ^self new
- ! !
- Object subclass: #JSObjectProxy
- instanceVariableNames: 'jsObject'
- package: 'Kernel-Objects'!
- !JSObjectProxy commentStamp!
- JSObjectProxy handles sending messages to JavaScript object, therefore accessing JavaScript objects from Amber is transparent.
- JSOjbectProxy makes intensive use of `#doesNotUnderstand:`.
- ## Examples
- JSObjectProxy objects are instanciated by Amber when a Smalltalk message is sent to a JavaScript object.
- window alert: 'hello world'.
- window inspect.
- (window jQuery: 'body') append: 'hello world'
- Smalltalk messages sends are converted to JavaScript function calls or object property access _(in this order)_. If n one of them match, a `MessageNotUnderstood` error will be thrown.
- ## Message conversion rules
- - `someUser name` becomes `someUser.name`
- - `someUser name: 'John'` becomes `someUser name = "John"`
- - `console log: 'hello world'` becomes `console.log('hello world')`
- - `(window jQuery: 'foo') css: 'background' color: 'red'` becomes `window.jQuery('foo').css('background', 'red')`
- __Note:__ For keyword-based messages, only the first keyword is kept: `window foo: 1 bar: 2` is equivalent to `window foo: 1 baz: 2`.!
- !JSObjectProxy methodsFor: 'accessing'!
- at: aSymbol
- | attr |
- attr := aSymbol asString.
- <return self['@jsObject'][attr]>
- !
- at: aSymbol put: anObject
- | attr |
- attr := aSymbol asString.
- <self['@jsObject'][attr] = anObject>
- !
- jsObject
- ^jsObject
- !
- jsObject: aJSObject
- jsObject := aJSObject
- ! !
- !JSObjectProxy methodsFor: 'proxy'!
- doesNotUnderstand: aMessage
- | obj selector jsSelector arguments |
- obj := self jsObject.
- selector := aMessage selector.
- jsSelector := selector asJavaScriptSelector.
- arguments := aMessage arguments.
- <if(jsSelector in obj) {return smalltalk.send(obj, jsSelector, arguments)}>.
- super doesNotUnderstand: aMessage
- !
- inspectOn: anInspector
- | variables |
- variables := Dictionary new.
- variables at: '#self' put: self jsObject.
- anInspector setLabel: self printString.
- <for(var i in self['@jsObject']) {
- variables._at_put_(i, self['@jsObject'][i]);
- }>.
- anInspector setVariables: variables
- !
- printString
- ^self jsObject toString
- ! !
- !JSObjectProxy class methodsFor: 'instance creation'!
- on: aJSObject
- ^self new
- jsObject: aJSObject;
- yourself
- ! !
- Object subclass: #Number
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !Number commentStamp!
- Number holds the most general methods for dealing with numbers.
- Number is directly mapped to JavaScript Number.
- Most arithmetic methods like `#+` `#/` `#-` `#max:` are directly inlined into javascript.
- ##Enumerating
- A Number can be used to evaluate a Block a fixed number of times:
- 5 timesRepeat: [Transcript show: 'This will be printed 5 times'; cr].
-
- 1 to: 5 do: [:aNumber| Transcript show: aNumber asString; cr].
-
- 1 to: 10 by: 2 do: [:aNumber| Transcript show: aNumber asString; cr].!
- !Number methodsFor: 'accessing'!
- identityHash
- ^self asString, 'n'
- ! !
- !Number methodsFor: 'arithmetic'!
- * aNumber
- "Inlined in the Compiler"
- <return self * aNumber>
- !
- + aNumber
- "Inlined in the Compiler"
- <return self + aNumber>
- !
- - aNumber
- "Inlined in the Compiler"
- <return self - aNumber>
- !
- / aNumber
- "Inlined in the Compiler"
- <return self / aNumber>
- !
- \\ aNumber
- <return self % aNumber>
- !
- abs
- ^ <Math.abs(self);>
- !
- max: aNumber
- <return Math.max(self, aNumber);>
- !
- min: aNumber
- <return Math.min(self, aNumber);>
- !
- negated
- ^0 - self
- !
- sqrt
- <return Math.sqrt(self)>
- !
- squared
- ^self * self
- ! !
- !Number methodsFor: 'comparing'!
- < aNumber
- "Inlined in the Compiler"
- <return self < aNumber>
- !
- <= aNumber
- "Inlined in the Compiler"
- <return self <= aNumber>
- !
- = aNumber
- aNumber isNumber ifFalse: [^false].
- <return Number(self) == aNumber>
- !
- > aNumber
- "Inlined in the Compiler"
- <return self >> aNumber>
- !
- >= aNumber
- "Inlined in the Compiler"
- <return self >>= aNumber>
- ! !
- !Number methodsFor: 'converting'!
- & aNumber
- <return self & aNumber>
- !
- @ aNumber
- ^Point x: self y: aNumber
- !
- asJSON
- ^self
- !
- asJavascript
- ^'(', self printString, ')'
- !
- asPoint
- ^Point x: self y: self
- !
- asString
- ^self printString
- !
- atRandom
- ^(Random new next * self) truncated + 1
- !
- rounded
- <return Math.round(self);>
- !
- to: aNumber
- | array first last count |
- first := self truncated.
- last := aNumber truncated + 1.
- count := 1.
- array := Array new.
- (last - first) timesRepeat: [
- array at: count put: first.
- count := count + 1.
- first := first + 1].
- ^array
- !
- to: stop by: step
- | array value pos |
- value := self.
- array := Array new.
- pos := 1.
- step = 0 ifTrue: [self error: 'step must be non-zero'].
- step < 0
- ifTrue: [[ value >= stop ] whileTrue: [
- array at: pos put: value.
- pos := pos + 1.
- value := value + step]]
- ifFalse: [[ value <= stop ] whileTrue: [
- array at: pos put: value.
- pos := pos + 1.
- value := value + step]].
- ^array
- !
- truncated
- |result|
- self >= 0
- ifTrue: [<result = Math.floor(self);>]
- ifFalse: [<result = (Math.floor(self * (-1)) * (-1));>].
- ^ result
- !
- | aNumber
- <return self | aNumber>
- ! !
- !Number methodsFor: 'copying'!
- copy
- ^self
- !
- deepCopy
- ^self copy
- ! !
- !Number methodsFor: 'enumerating'!
- timesRepeat: aBlock
- | integer count |
- integer := self truncated.
- count := 1.
- [count > self] whileFalse: [
- aBlock value.
- count := count + 1]
- !
- to: stop by: step do: aBlock
- | value |
- value := self.
- step = 0 ifTrue: [self error: 'step must be non-zero'].
- step < 0
- ifTrue: [[ value >= stop ] whileTrue: [
- aBlock value: value.
- value := value + step]]
- ifFalse: [[ value <= stop ] whileTrue: [
- aBlock value: value.
- value := value + step]]
- !
- to: stop do: aBlock
- "Evaluate aBlock for each number from self to aNumber."
- | nextValue |
- nextValue := self.
- [nextValue <= stop]
- whileTrue:
- [aBlock value: nextValue.
- nextValue := nextValue + 1]
- ! !
- !Number methodsFor: 'printing'!
- printShowingDecimalPlaces: placesDesired
- <return self.toFixed(placesDesired)>
- !
- printString
- <return String(self)>
- ! !
- !Number methodsFor: 'testing'!
- even
- ^ 0 = (self \\ 2)
- !
- isNumber
- ^true
- !
- isZero
- ^self = 0
- !
- negative
- "Answer whether the receiver is mathematically negative."
- ^ self < 0
- !
- odd
- ^ self even not
- !
- positive
- "Answer whether the receiver is positive or equal to 0. (ST-80 protocol)."
- ^ self >= 0
- ! !
- !Number class methodsFor: 'instance creation'!
- pi
- <return Math.PI>
- ! !
- Object subclass: #Organizer
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !Organizer methodsFor: 'accessing'!
- addElement: anObject
- <self.addElement(anObject)>
- !
- elements
- ^ (self basicAt: 'elements') copy
- !
- removeElement: anObject
- <self.removeElement(anObject)>
- ! !
- Object subclass: #Package
- instanceVariableNames: 'commitPathJs commitPathSt'
- package: 'Kernel-Objects'!
- !Package commentStamp!
- A Package is similar to a "class category" typically found in other Smalltalks like Pharo or Squeak. Amber does not have class categories anymore, it had in the beginning but now each class in the system knows which package it belongs to.
- A Package has a name, an Array of "requires", a comment and a Dictionary with other optional key value attributes. A Package can also be queried for its classes, but it will then resort to a reverse scan of all classes to find them.
- Packages are manipulated through "Smalltalk current", like for example finding one based on a name:
- Smalltalk current packageAt: 'Kernel'
- ...but you can also use:
- Package named: 'Kernel'
- A Package differs slightly from a Monticello package which can span multiple class categories using a naming convention based on hyphenation. But just as in Monticello a Package supports "class extensions" so a Package
- can define behaviors in foreign classes using a naming convention for method categories where the category starts with an asterisk and then the name of the owning package follows. This can easily be seen in for example class
- String where the method category "*IDE" defines #inspectOn: which thus is a method belonging to the IDE package.
- You can fetch a package from the server:
- Package fetch: 'Additional-Examples'!
- !Package methodsFor: 'accessing'!
- commitPathJs
- ^ commitPathJs ifNil: [self class defaultCommitPathJs]
- !
- commitPathJs: aString
- commitPathJs := aString
- !
- commitPathSt
- ^ commitPathSt ifNil: [self class defaultCommitPathSt]
- !
- commitPathSt: aString
- commitPathSt := aString
- !
- dependencies
- ^self propertyAt: 'dependencies' ifAbsent: [#()]
- !
- dependencies: anArray
- ^self propertyAt: 'dependencies' put: anArray
- !
- name
- <return self.pkgName>
- !
- name: aString
- <self.pkgName = aString>
- !
- organization
- ^ self basicAt: 'organization'
- !
- properties
- ^Smalltalk current readJSObject: (self basicAt: 'properties')
- !
- properties: aDict
- "We store it as a javascript object."
-
- | object |
- <object = {};>.
- aDict keysAndValuesDo: [:key :value |
- <object[key] = value>.
- ].
- <return self.properties = object>
- ! !
- !Package methodsFor: 'classes'!
- classes
- ^ self organization elements
- !
- sortedClasses
- "Answer all classes in the receiver, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)."
- ^self class sortedClasses: self classes
- ! !
- !Package methodsFor: 'printing'!
- printString
- ^self name
- ! !
- !Package methodsFor: 'private'!
- jsProperties
- <return self.properties>
- !
- jsProperties: aJSObject
- <return self.properties = aJSObject>
- !
- propertiesAsJSON
- <return JSON.stringify(self.properties)>
- ! !
- !Package methodsFor: 'properties'!
- propertyAt: key
- <return self.properties[key]>
- !
- propertyAt: key ifAbsent: block
- ^(self propertyAt: key) ifNil: [block value]
- !
- propertyAt: key put: value
- <return self.properties[key] = value>
- ! !
- Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
- !Package class methodsFor: 'commit paths'!
- commitPathsFromLoader
- | js st |
- <var cp = smalltalk['@@commitPath'];
- if (cp) { js = cp.js; st = cp.st; }>.
- js ifNotNil: [ self defaultCommitPathJs: js ].
- st ifNotNil: [ self defaultCommitPathSt: st ].
- !
- defaultCommitPathJs
- ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
- !
- defaultCommitPathJs: aString
- defaultCommitPathJs := aString
- !
- defaultCommitPathSt
- ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
- !
- defaultCommitPathSt: aString
- defaultCommitPathSt := aString
- !
- resetCommitPaths
- defaultCommitPathJs := nil.
- defaultCommitPathSt := nil.
- ! !
- !Package class methodsFor: 'initialization'!
- initialize
- super initialize.
- self commitPathsFromLoader
- ! !
- !Package class methodsFor: 'loading-storing'!
- commitToLocalStorage: aPackageName
- | key sourceCode |
- key := 'smalltalk.packages.' , aPackageName.
- sourceCode := Exporter new exportPackage: aPackageName.
- <localStorage[key] = escape(sourceCode)>
- !
- fetch: aPackageName
- self fetch: aPackageName prefix: self defaultCommitPathJs, '/'
- !
- fetch: aPackageName prefix: aPrefix
- jQuery getScript: (aPrefix , aPackageName , '.js') onSuccess: [ Package init: aPackageName ]
- !
- init: aPackageName
- (smalltalk classes select: [ :each | <each.pkg.pkgName == aPackageName> ])
- do: [ :each | <smalltalk.init(each)> ];
- do: [ :each | each initialize ]
- ! !
- !Package class methodsFor: 'not yet classified'!
- named: aPackageName
- ^Smalltalk current packageAt: aPackageName
- !
- named: aPackageName ifAbsent: aBlock
- ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
- ! !
- !Package class methodsFor: 'sorting'!
- sortedClasses: classes
- "Answer classes, sorted by superclass/subclasses and by class name for common subclasses (Issue #143)"
- | children others nodes expandedClasses |
- children := #().
- others := #().
- classes do: [:each |
- (classes includes: each superclass)
- ifFalse: [children add: each]
- ifTrue: [others add: each]].
- nodes := children collect: [:each |
- ClassSorterNode on: each classes: others level: 0].
- nodes := nodes sorted: [:a :b | a theClass name <= b theClass name ].
- expandedClasses := Array new.
- nodes do: [:aNode |
- aNode traverseClassesWith: expandedClasses].
- ^expandedClasses
- ! !
- Object subclass: #Point
- instanceVariableNames: 'x y'
- package: 'Kernel-Objects'!
- !Point commentStamp!
- A `Point` represents an x-y pair of numbers usually designating a geometric coordinate.
- Points are traditionally created using the binary `#@` message to a number:
- 100@120
- Points can then be arithmetically manipulated:
- 100@100 + (10@10)
- ...or for example:
- (100@100) * 2
- **NOTE:** Creating a Point with a negative y-value will need a space after `@` in order to avoid a parsing error:
- 100@ -100 "but 100@-100 would not parse"
- Amber does not have much behavior in this class out-of-the-box.!
- !Point methodsFor: 'accessing'!
- x
- ^x
- !
- x: aNumber
- x := aNumber
- !
- y
- ^y
- !
- y: aNumber
- y := aNumber
- ! !
- !Point methodsFor: 'arithmetic'!
- * aPoint
- ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
- !
- + aPoint
- ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
- !
- - aPoint
- ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
- !
- / aPoint
- ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
- !
- = aPoint
- ^aPoint class = self class and: [
- (aPoint x = self x) & (aPoint y = self y)]
- ! !
- !Point methodsFor: 'converting'!
- asPoint
- ^self
- ! !
- !Point methodsFor: 'printing'!
- printString
- "Print receiver in classic x@y notation."
- ^String streamContents: [:stream |
- stream nextPutAll: x printString, '@'.
- (y notNil and: [y negative])
- ifTrue: [
- "Avoid ambiguous @- construct"
- stream space].
- stream nextPutAll: y printString]
- ! !
- !Point methodsFor: 'transforming'!
- translateBy: delta
- "Answer a Point translated by delta (an instance of Point)."
- ^(delta x + x) @ (delta y + y)
- ! !
- !Point class methodsFor: 'instance creation'!
- x: aNumber y: anotherNumber
- ^self new
- x: aNumber;
- y: anotherNumber;
- yourself
- ! !
- Object subclass: #Random
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !Random commentStamp!
- `Random` is a random number generator and is implemented as a trivial wrapper around javascript `Math.random()` and is used like this:
- Random new next
- This will return a float x where x < 1 and x > 0. If you want a random integer from 1 to 10 you can use `#atRandom`
- 10 atRandom
- ...and if you want a random number in a specific interval this also works:
- (3 to: 7) atRandom
- ...but be aware that `#to:` does not create an Interval as in other Smalltalk implementations but in fact an `Array` of numbers, so it's better to use:
- 5 atRandom + 2
- Since `#atRandom` is implemented in `SequencableCollection` you can easy pick an element at random:
- #('a' 'b' 'c') atRandom
- ...or perhaps a letter from a `String`:
- 'abc' atRandom
- Since Amber does not have Characters this will return a `String` of length 1 like for example `'b'`.!
- !Random methodsFor: 'accessing'!
- next
- <return Math.random()>
- !
- next: anInteger
- ^(1 to: anInteger) collect: [:each | self next]
- ! !
- Object subclass: #Smalltalk
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !Smalltalk commentStamp!
- Smalltalk has only one instance, accessed with `Smalltalk current`.
- It represents the global JavaScript variable `smalltalk` declared in `js/boot.js`.
- The `smalltalk` object holds all class and packages defined in the system.
- ## Classes
- Classes can be accessed using the following methods:
- - `#classes` answers the full list of Smalltalk classes in the system
- - `#at:` answers a specific class of `nil`
- ## Packages
- Packages can be accessed using the following methods:
- - `#packages` answers the full list of packages
- - `#packageAt:` answers a specific class of `nil`
- __note:__ classes and packages are accessed using strings, not symbols
- ## Parsing
- The `#parse:` method is used to parse Smalltalk source code.
- It requires the `Compiler` package and the `js/parser.js` parser file in order to work!
- !Smalltalk methodsFor: 'accessing'!
- at: aSymbol
- <return self[aSymbol._asString()]>
- !
- basicParse: aString
- <return smalltalk.parser.parse(aString)>
- !
- parse: aString
- | result |
- self try: [result := self basicParse: aString] catch: [:ex | (self parseError: ex parsing: aString) signal].
- ^result
- !
- parseError: anException parsing: aString
- | row col message lines badLine code |
- <row = anException.line;
- col = anException.column;
- message = anException.message;>.
- lines := aString lines.
- badLine := lines at: row.
- badLine := (badLine copyFrom: 1 to: col - 1), ' ===>', (badLine copyFrom: col to: badLine size).
- lines at: row put: badLine.
- code := String streamContents: [:s |
- lines withIndexDo: [:l :i |
- s nextPutAll: i asString, ': ', l, String lf]].
- ^ ParseError new messageText: ('Parse error on line ' , row , ' column ' , col , ' : ' , message , ' Below is code with line numbers and ===> marker inserted:' , String lf, code)
- !
- readJSObject: anObject
- <return self.readJSObject(anObject)>
- !
- reservedWords
- "JavaScript reserved words"
- <return self.reservedWords>
- !
- send: aSelector to: anObject arguments: aCollection
- | selector |
- selector := aSelector asString asSelector.
- <return self.send(anObject, selector, aCollection)>
- ! !
- !Smalltalk methodsFor: 'classes'!
- classes
- <return self.classes()>
- !
- removeClass: aClass
- aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
-
- aClass methodDictionary values do: [:each |
- aClass removeCompiledMethod: each].
-
- aClass class methodDictionary values do: [:each |
- aClass class removeCompiledMethod: each].
-
- self deleteClass: aClass.
-
- SystemAnnouncer current
- announce: (ClassRemoved new
- theClass: aClass;
- yourself)
- ! !
- !Smalltalk methodsFor: 'packages'!
- packageAt: packageName
- <return self.packages[packageName]>
- !
- packageAt: packageName ifAbsent: aBlock
- ^(self packageAt: packageName) ifNil: aBlock
- !
- packages
- "Return all Package instances in the system."
- <return self.packages.all()>
- !
- pseudoVariableNames
- ^ #('self' 'super' 'nil' 'true' 'false' 'thisContext')
- !
- removePackage: packageName
- "Removes a package and all its classes."
- | pkg |
- pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
- pkg classes do: [:each |
- self removeClass: each].
- self deletePackage: packageName
- !
- renamePackage: packageName to: newName
- "Rename a package."
- | pkg |
- pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
- (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
- <smalltalk.packages[newName] = smalltalk.packages[packageName]>.
- pkg name: newName.
- self deletePackage: packageName.
- ! !
- !Smalltalk methodsFor: 'private'!
- createPackage: packageName
- "Create and bind a new package with given name and return it."
- <return smalltalk.addPackage(packageName, nil)>
- !
- createPackage: packageName properties: aDict
- "Create and bind a new package with given name and return it."
- | object |
- <object = {};>.
- aDict keysAndValuesDo: [:key :value |
- <object[key] = value>.
- ].
- <return smalltalk.addPackage(packageName, object)>
- !
- deleteClass: aClass
- "Deletes a class by deleting its binding only. Use #removeClass instead"
-
- <self.removeClass(aClass)>
- !
- deletePackage: packageName
- "Deletes a package by deleting its binding, but does not check if it contains classes etc.
- To remove a package, use #removePackage instead."
- <delete smalltalk.packages[packageName]>
- ! !
- Smalltalk class instanceVariableNames: 'current'!
- !Smalltalk class methodsFor: 'accessing'!
- current
- <return smalltalk>
- ! !
- Object subclass: #Timeout
- instanceVariableNames: 'rawTimeout'
- package: 'Kernel-Objects'!
- !Timeout commentStamp!
- I am wrapping the returns from set{Timeout,Interval}.
- Number suffices in browsers, but node.js returns an object.!
- !Timeout methodsFor: 'accessing'!
- rawTimeout: anObject
- rawTimeout := anObject
- ! !
- !Timeout methodsFor: 'timeout/interval'!
- clearInterval
- <
- var interval = self["@rawTimeout"];
- clearInterval(interval);
- >
- !
- clearTimeout
- <
- var timeout = self["@rawTimeout"];
- clearTimeout(timeout);
- >
- ! !
- !Timeout class methodsFor: 'instance creation'!
- on: anObject
- ^self new rawTimeout: anObject; yourself
- ! !
- Object subclass: #UndefinedObject
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !UndefinedObject commentStamp!
- UndefinedObject describes the behavior of its sole instance, `nil`. `nil` represents a prior value for variables that have not been initialized, or for results which are meaningless.
- `nil` is the Smalltalk representation of the `undefined` JavaScript object.!
- !UndefinedObject methodsFor: 'class creation'!
- subclass: aString instanceVariableNames: anotherString
- ^self subclass: aString instanceVariableNames: anotherString package: nil
- !
- subclass: aString instanceVariableNames: aString2 category: aString3
- "Kept for compatibility."
- self deprecatedAPI.
- ^self subclass: aString instanceVariableNames: aString2 package: aString3
- !
- subclass: aString instanceVariableNames: aString2 package: aString3
- ^ClassBuilder new
- superclass: self subclass: aString instanceVariableNames: aString2 package: aString3
- ! !
- !UndefinedObject methodsFor: 'converting'!
- asJSON
- ^null
- ! !
- !UndefinedObject methodsFor: 'copying'!
- deepCopy
- ^self
- !
- shallowCopy
- ^self
- ! !
- !UndefinedObject methodsFor: 'printing'!
- printString
- ^'nil'
- ! !
- !UndefinedObject methodsFor: 'testing'!
- ifNil: aBlock
- "inlined in the Compiler"
- ^self ifNil: aBlock ifNotNil: []
- !
- ifNil: aBlock ifNotNil: anotherBlock
- "inlined in the Compiler"
- ^aBlock value
- !
- ifNotNil: aBlock
- "inlined in the Compiler"
- ^self
- !
- ifNotNil: aBlock ifNil: anotherBlock
- "inlined in the Compiler"
- ^anotherBlock value
- !
- isNil
- ^true
- !
- notNil
- ^false
- ! !
- !UndefinedObject class methodsFor: 'instance creation'!
- new
- self error: 'You cannot create new instances of UndefinedObject. Use nil'
- ! !
|