12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907 |
- Smalltalk current createPackage: 'Kernel-Objects'!
- 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: aString
- < return self['@'+aString] >
- !
- instVarAt: aString put: anObject
- < self['@' + aString] = 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
- !
- test
- | a |
- a := 1.
- self halt
- ! !
- !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: aString
- ^self basicPerform: aString withArguments: #()
- !
- basicPerform: aString withArguments: aCollection
- <return self[aString].apply(self, aCollection);>
- !
- perform: aString
- ^self perform: aString withArguments: #()
- !
- perform: aString withArguments: aCollection
- <return smalltalk.send(self, aString._asSelector(), aCollection)>
- ! !
- !Object methodsFor: 'printing'!
- printOn: aStream
- "Append to the aStream, a string representing the receiver."
-
- aStream nextPutAll: (self class name first isVowel
- ifTrue: [ 'an ' ]
- ifFalse: [ 'a ' ]).
- aStream nextPutAll: self class name
- !
- printString
- "Answer a String representation of the receiver."
- ^ String streamContents: [ :stream | self printOn: stream ]
- ! !
- !Object methodsFor: 'streaming'!
- putOn: aStream
- aStream nextPut: self
- ! !
- !Object methodsFor: 'testing'!
- ifNil: aBlock
- "inlined in the Compiler"
- ^self
- !
- ifNil: aBlock ifNotNil: anotherBlock
- "inlined in the Compiler"
- ^anotherBlock value: self
- !
- ifNotNil: aBlock
- "inlined in the Compiler"
- ^aBlock value: self
- !
- ifNotNil: aBlock ifNil: anotherBlock
- "inlined in the Compiler"
- ^aBlock value: self
- !
- isBehavior
- ^ false
- !
- isBoolean
- ^ false
- !
- isClass
- ^false
- !
- isCompiledMethod
- ^ false
- !
- isImmutable
- ^ false
- !
- isKindOf: aClass
- ^(self isMemberOf: aClass)
- ifTrue: [true]
- ifFalse: [self class inheritsFrom: aClass]
- !
- isMemberOf: aClass
- ^self class = aClass
- !
- isMetaclass
- ^false
- !
- isNil
- ^false
- !
- isNumber
- ^false
- !
- isPackage
- ^ 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
- <
- if(!! aBoolean._isBoolean || !! aBoolean._isBoolean()) {
- return 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
- !
- asString
- < return self.toString() >
- ! !
- !Boolean methodsFor: 'copying'!
- deepCopy
- ^self
- !
- shallowCopy
- ^self
- ! !
- !Boolean methodsFor: 'printing'!
- printOn: aStream
- aStream nextPutAll: self asString
- ! !
- !Boolean methodsFor: 'testing'!
- isBoolean
- ^ true
- !
- isImmutable
- ^ true
- ! !
- 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'!
- printOn: aStream
- aStream nextPutAll: 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: #Environment
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !Environment commentStamp!
- Abstract class defining common behavior for local and remote environments!
- !Environment methodsFor: 'accessing'!
- allSelectors
- ^ (Smalltalk current at: 'allSelectors') value
- !
- availableClassNames
- ^ Smalltalk current classes
- collect: [ :each | each name ]
- !
- availablePackageNames
- ^ Smalltalk current packages
- collect: [ :each | each name ]
- !
- availableProtocolsFor: aClass
- | protocols |
-
- protocols := aClass protocols.
- aClass superclass ifNotNil: [ protocols addAll: (self availableProtocolsFor: aClass superclass) ].
- ^ protocols asSet asArray
- !
- classBuilder
- ^ ClassBuilder new
- !
- classNamed: aString
- ^ (Smalltalk current at: aString asSymbol)
- ifNil: [ self error: 'Invalid class name' ]
- !
- classes
- ^ Smalltalk current classes
- !
- packages
- ^ Smalltalk current packages
- !
- systemAnnouncer
- ^ (Smalltalk current at: #SystemAnnouncer) current
- ! !
- !Environment methodsFor: 'actions'!
- commitPackage: aPackage
- aPackage commit
- !
- copyClass: aClass to: aClassName
- (Smalltalk current at: aClassName)
- ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
-
- ClassBuilder new copyClass: aClass named: aClassName
- !
- eval: aString on: aReceiver
- | compiler |
- compiler := Compiler new.
- [ compiler parseExpression: aString ] on: Error do: [ :ex |
- ^ window alert: ex messageText ].
- ^ compiler evaluateExpression: aString on: aReceiver
- !
- moveClass: aClass toPackage: aPackageName
- | package |
-
- package := Package named: aPackageName.
- package ifNil: [ self error: 'Invalid package name' ].
- package == aClass package ifTrue: [ ^ self ].
-
- aClass package: package
- !
- moveMethod: aMethod toClass: aClassName
- | destinationClass |
-
- destinationClass := Smalltalk current at: aClassName asSymbol.
- destinationClass ifNil: [ self error: 'Invalid class name' ].
- destinationClass == aMethod methodClass ifTrue: [ ^ self ].
-
- destinationClass
- compile: aMethod source
- category: aMethod protocol.
- aMethod methodClass
- removeCompiledMethod: aMethod
- !
- moveMethod: aMethod toProtocol: aProtocol
- aMethod category: aProtocol
- !
- removeClass: aClass
- Smalltalk current removeClass: aClass
- !
- removeMethod: aMethod
- aMethod methodClass removeCompiledMethod: aMethod
- !
- removeProtocol: aString from: aClass
- (aClass methods
- select: [ :each | each protocol = aString ])
- do: [ :each | aClass removeCompiledMethod: each ]
- !
- renameClass: aClass to: aClassName
- (Smalltalk current at: aClassName)
- ifNotNil: [ self error: 'A class named ', aClassName, ' already exists' ].
-
- ClassBuilder new renameClass: aClass to: aClassName
- !
- renameProtocol: aString to: anotherString in: aClass
- (aClass methods
- select: [ :each | each protocol = aString ])
- do: [ :each | each protocol: anotherString ]
- ! !
- !Environment methodsFor: 'compiling'!
- addInstVarNamed: aString to: aClass
- self classBuilder
- addSubclassOf: aClass superclass
- named: aClass name
- instanceVariableNames: (aClass instanceVariableNames copy add: aString; yourself)
- package: aClass package name
- !
- compileClassComment: aString for: aClass
- aClass comment: aString
- !
- compileClassDefinition: aString
- self eval: aString on: DoIt new
- !
- compileMethod: sourceCode for: class protocol: protocol
- ^ class
- compile: sourceCode
- category: protocol
- ! !
- 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: aString
- <return self['@jsObject'][aString]>
- !
- at: aString ifAbsent: aBlock
- "return the aString property or evaluate aBlock if the property is not defined on the object"
- <
- var obj = self['@jsObject'];
- return aString in obj ? obj[aString] : aBlock();
- >
- !
- at: aString ifPresent: aBlock
- "return the evaluation of aBlock with the value if the property is defined or return nil"
- <
- var obj = self['@jsObject'];
- return aString in obj ? aBlock(obj[aString]) : nil;
- >
- !
- at: aString ifPresent: aBlock ifAbsent: anotherBlock
- "return the evaluation of aBlock with the value if the property is defined
- or return value of anotherBlock"
- <
- var obj = self['@jsObject'];
- return aString in obj ? aBlock(obj[aString]) : anotherBlock();
- >
- !
- at: aString put: anObject
- <self['@jsObject'][aString] = anObject>
- !
- jsObject
- ^jsObject
- !
- jsObject: aJSObject
- jsObject := aJSObject
- !
- lookupProperty: aString
- "Looks up a property in JS object.
- Answer the property if it is present, or nil if it is not present."
-
- <return aString in self._jsObject() ? aString : nil>
- !
- value
- "if attribute 'value' exists on the JS object return it,
- otherwise return the result of Object>>value."
-
- ^ self
- at: 'value'
- ifAbsent: [ super value ]
- ! !
- !JSObjectProxy methodsFor: 'enumerating'!
- keysAndValuesDo: aBlock
- <
- var o = self['@jsObject'];
- for(var i in o) {
- aBlock(i, o[i]);
- }
- >
- ! !
- !JSObjectProxy methodsFor: 'printing'!
- printOn: aStream
- aStream nextPutAll: self jsObject toString
- ! !
- !JSObjectProxy methodsFor: 'proxy'!
- addObjectVariablesTo: aDictionary
- <
- for(var i in self['@jsObject']) {
- aDictionary._at_put_(i, self['@jsObject'][i]);
- }
- >
- !
- doesNotUnderstand: aMessage
- ^ (self lookupProperty: aMessage selector asJavaScriptSelector)
- ifNil: [ super doesNotUnderstand: aMessage ]
- ifNotNil: [ :jsSelector |
- self
- forwardMessage: jsSelector
- withArguments: aMessage arguments ]
- !
- forwardMessage: aString withArguments: anArray
- <
- return smalltalk.send(self._jsObject(), aString, anArray);
- >
- !
- inspectOn: anInspector
- | variables |
- variables := Dictionary new.
- variables at: '#self' put: self jsObject.
- anInspector setLabel: self printString.
- self addObjectVariablesTo: variables.
- anInspector setVariables: variables
- ! !
- !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
- <return 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
- <
- if(!! aNumber._isNumber || !! aNumber._isNumber()) {
- return 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
- < return String(self) >
- !
- 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
- <
- if(self >>= 0) {
- return Math.floor(self);
- } else {
- return Math.floor(self * (-1)) * (-1);
- };
- >
- !
- | aNumber
- <return self | aNumber>
- ! !
- !Number methodsFor: 'copying'!
- copy
- ^self
- !
- deepCopy
- ^self copy
- ! !
- !Number methodsFor: 'enumerating'!
- timesRepeat: aBlock
- | count |
- 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'!
- printOn: aStream
- aStream nextPutAll: self asString
- !
- printShowingDecimalPlaces: placesDesired
- <return self.toFixed(placesDesired)>
- ! !
- !Number methodsFor: 'testing'!
- IsImmutable
- ^ true
- !
- 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.elements.addElement(anObject)>
- !
- elements
- ^ (self basicAt: 'elements') copy
- !
- removeElement: anObject
- <self.elements.removeElement(anObject)>
- ! !
- Organizer subclass: #ClassOrganizer
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- !ClassOrganizer methodsFor: 'accessing'!
- addElement: aString
- super addElement: aString.
- SystemAnnouncer current announce: (ProtocolAdded new
- protocol: aString;
- theClass: self theClass;
- yourself)
- !
- removeElement: aString
- super removeElement: aString.
- SystemAnnouncer current announce: (ProtocolRemoved new
- protocol: aString;
- theClass: self theClass;
- yourself)
- !
- theClass
- < return self.theClass >
- ! !
- Organizer subclass: #PackageOrganizer
- instanceVariableNames: ''
- package: 'Kernel-Objects'!
- 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
- !
- name
- <return self.pkgName>
- !
- name: aString
- <self.pkgName = aString>
- !
- organization
- ^ self basicAt: 'organization'
- ! !
- !Package methodsFor: 'classes'!
- classes
- ^ self organization elements
- !
- setupClasses
- self classes
- do: [ :each | ClassBuilder new setupClass: each ];
- do: [ :each | each initialize ]
- !
- 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'!
- printOn: aStream
- super printOn: aStream.
- aStream
- nextPutAll: ' (';
- nextPutAll: self name;
- nextPutAll: ')'
- ! !
- !Package methodsFor: 'testing'!
- isPackage
- ^ true
- ! !
- Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
- !Package class methodsFor: 'accessing'!
- named: aPackageName
- ^Smalltalk current packageAt: aPackageName
- !
- named: aPackageName ifAbsent: aBlock
- ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
- ! !
- !Package class methodsFor: 'commit paths'!
- commitPathsFromLoader
- <
- var commitPath = typeof amber !!== 'undefined' && amber.commitPath;
- if (!!commitPath) return;
- if (commitPath.js) self._defaultCommitPathJs_(commitPath.js);
- if (commitPath.st) self._defaultCommitPathSt_(commitPath.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'!
- load: aPackageName
- self load: aPackageName prefix: self defaultCommitPathJs, '/'
- !
- load: aPackageName prefix: aPrefix
- jQuery
- getScript: (aPrefix , aPackageName , '.js')
- onSuccess: [
- (Package named: aPackageName) setupClasses ]
- ! !
- !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'!
- printOn: aStream
- "Print receiver in classic x@y notation."
- x printOn: aStream.
-
- aStream nextPutAll: '@'.
- (y notNil and: [y negative]) ifTrue: [
- "Avoid ambiguous @- construct"
- aStream space ].
-
- y printOn: aStream
- ! !
- !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: aString
- <return self[aString]>
- !
- parse: aString
- | result |
- self try: [result := self basicParse: aString] catch: [:ex | (self parseError: ex parsing: aString) signal].
- ^result
- !
- readJSObject: anObject
- <return self.readJSObject(anObject)>
- !
- reservedWords
- "JavaScript reserved words"
- <return self.reservedWords>
- !
- version
- "Answer the version string of Amber"
-
- ^ '0.10'
- ! !
- !Smalltalk methodsFor: 'classes'!
- classes
- <return self.classes()>
- !
- deleteClass: aClass
- "Deletes a class by deleting its binding only. Use #removeClass instead"
-
- <self.removeClass(aClass)>
- !
- removeClass: aClass
- aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
-
- self deleteClass: aClass.
-
- SystemAnnouncer current
- announce: (ClassRemoved new
- theClass: aClass;
- yourself)
- ! !
- !Smalltalk methodsFor: 'error handling'!
- asSmalltalkException: anObject
- "A JavaScript exception may be thrown.
- We then need to convert it back to a Smalltalk object"
-
- ^ ((self isSmalltalkObject: anObject) and: [ anObject isKindOf: Error ])
- ifTrue: [ anObject ]
- ifFalse: [ JavaScriptException on: anObject ]
- !
- parseError: anException parsing: aString
- ^ ParseError new messageText: 'Parse error on line ', (anException basicAt: 'line') ,' column ' , (anException basicAt: 'column') ,' : Unexpected character ', (anException basicAt: 'found')
- ! !
- !Smalltalk methodsFor: 'packages'!
- createPackage: packageName
- "Create and bind a new package with given name and return it."
- <return smalltalk.addPackage(packageName)>
- !
- 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]>
- !
- 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].
- (self basicAt: 'packages') at: newName put: pkg.
- pkg name: newName.
- self deletePackage: packageName.
- ! !
- !Smalltalk methodsFor: 'private'!
- basicParse: aString
- <return smalltalk.parser.parse(aString)>
- !
- createPackage: packageName properties: aDict
- "Needed to import .st files: they begin with this call."
- self deprecatedAPI.
-
- aDict isEmpty ifFalse: [ self error: 'createPackage:properties: called with nonempty properties' ].
- ^ self createPackage: packageName
- ! !
- !Smalltalk methodsFor: 'testing'!
- isSmalltalkObject: anObject
- "Consider anObject a Smalltalk object if it has a 'klass' property.
- Note that this may be unaccurate"
-
- <return typeof anObject.klass !!== 'undefined'>
- ! !
- 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 asString instanceVariableNames: aString2 package: aString3
- ! !
- !UndefinedObject methodsFor: 'converting'!
- asJSON
- ^null
- ! !
- !UndefinedObject methodsFor: 'copying'!
- deepCopy
- ^self
- !
- shallowCopy
- ^self
- ! !
- !UndefinedObject methodsFor: 'printing'!
- printOn: aStream
- aStream nextPutAll: '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
- !
- isImmutable
- ^ true
- !
- isNil
- ^true
- !
- notNil
- ^false
- ! !
- !UndefinedObject class methodsFor: 'instance creation'!
- new
- self error: 'You cannot create new instances of UndefinedObject. Use nil'
- ! !
- !String methodsFor: '*Kernel-Objects'!
- asJavaScriptSelector
- "Return first keyword of the selector, without trailing colon."
- ^self replace: '^([a-zA-Z0-9]*).*$' with: '$1'
- ! !
|