Kernel.st 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512
  1. Smalltalk current createPackage: 'Kernel' properties: #{}!
  2. nil subclass: #Object
  3. instanceVariableNames: ''
  4. category: 'Kernel'!
  5. !Object methodsFor: 'accessing'!
  6. yourself
  7. ^self
  8. !
  9. class
  10. <return self.klass>
  11. !
  12. size
  13. self error: 'Object not indexable'
  14. !
  15. instVarAt: aString
  16. <return self['@'+aString]>
  17. !
  18. instVarAt: aString put: anObject
  19. <self['@' + aString] = anObject>
  20. !
  21. basicAt: aString
  22. <return self[aString]>
  23. !
  24. basicAt: aString put: anObject
  25. <return self[aString] = anObject>
  26. !
  27. basicDelete: aString
  28. <delete self[aString]>
  29. ! !
  30. !Object methodsFor: 'comparing'!
  31. = anObject
  32. ^self == anObject
  33. !
  34. ~= anObject
  35. ^(self = anObject) = false
  36. !
  37. == anObject
  38. <return self === anObject>
  39. !
  40. ~~ anObject
  41. ^(self == anObject) = false
  42. ! !
  43. !Object methodsFor: 'converting'!
  44. -> anObject
  45. ^Association key: self value: anObject
  46. !
  47. asString
  48. ^self printString
  49. !
  50. asJavascript
  51. ^self asString
  52. !
  53. asJSON
  54. <return JSON.stringify(self._asJSONObject())>
  55. !
  56. asJSONObject
  57. | object |
  58. object := Object new.
  59. self class instanceVariableNames do: [:each |
  60. object basicAt: each put: (self instVarAt: each) asJSONObject].
  61. ^object
  62. ! !
  63. !Object methodsFor: 'copying'!
  64. copy
  65. ^self shallowCopy postCopy
  66. !
  67. shallowCopy
  68. <
  69. var copy = self.klass._new();
  70. for(var i in self) {
  71. if(/^@.+/.test(i)) {
  72. copy[i] = self[i];
  73. }
  74. }
  75. return copy;
  76. >
  77. !
  78. deepCopy
  79. <
  80. var copy = self.klass._new();
  81. for(var i in self) {
  82. if(/^@.+/.test(i)) {
  83. copy[i] = self[i]._deepCopy();
  84. }
  85. }
  86. return copy;
  87. >
  88. !
  89. postCopy
  90. ! !
  91. !Object methodsFor: 'error handling'!
  92. error: aString
  93. Error signal: aString
  94. !
  95. subclassResponsibility
  96. self error: 'This method is a responsibility of a subclass'
  97. !
  98. shouldNotImplement
  99. self error: 'This method should not be implemented in ', self class name
  100. !
  101. try: aBlock catch: anotherBlock
  102. <try{aBlock()} catch(e) {anotherBlock(e)}>
  103. !
  104. doesNotUnderstand: aMessage
  105. MessageNotUnderstood new
  106. receiver: self;
  107. message: aMessage;
  108. signal
  109. !
  110. halt
  111. self error: 'Halt encountered'
  112. !
  113. deprecatedAPI
  114. "Just a simple way to deprecate methods.
  115. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  116. but it could in the future."
  117. console warn: thisContext home asString, ' is deprecated!!'
  118. ! !
  119. !Object methodsFor: 'initialization'!
  120. initialize
  121. ! !
  122. !Object methodsFor: 'message handling'!
  123. perform: aSymbol
  124. ^self perform: aSymbol withArguments: #()
  125. !
  126. perform: aSymbol withArguments: aCollection
  127. ^self basicPerform: aSymbol asSelector withArguments: aCollection
  128. !
  129. basicPerform: aSymbol
  130. ^self basicPerform: aSymbol withArguments: #()
  131. !
  132. basicPerform: aSymbol withArguments: aCollection
  133. <return self[aSymbol].apply(self, aCollection);>
  134. ! !
  135. !Object methodsFor: 'printing'!
  136. printString
  137. ^'a ', self class name
  138. !
  139. printNl
  140. <console.log(self)>
  141. !
  142. log: aString block: aBlock
  143. | result |
  144. console log: aString, ' time: ', (Date millisecondsToRun: [result := aBlock value]) printString.
  145. ^result
  146. !
  147. storeString
  148. "Answer a String representation of the receiver from which the receiver
  149. can be reconstructed."
  150. ^ String streamContents: [:s | self storeOn: s]
  151. !
  152. storeOn: aStream
  153. aStream nextPutAll: self printString
  154. ! !
  155. !Object methodsFor: 'testing'!
  156. isKindOf: aClass
  157. ^(self isMemberOf: aClass)
  158. ifTrue: [true]
  159. ifFalse: [self class inheritsFrom: aClass]
  160. !
  161. isMemberOf: aClass
  162. ^self class = aClass
  163. !
  164. ifNil: aBlock
  165. "inlined in the Compiler"
  166. ^self
  167. !
  168. ifNil: aBlock ifNotNil: anotherBlock
  169. "inlined in the Compiler"
  170. ^anotherBlock value
  171. !
  172. ifNotNil: aBlock
  173. "inlined in the Compiler"
  174. ^aBlock value
  175. !
  176. ifNotNil: aBlock ifNil: anotherBlock
  177. "inlined in the Compiler"
  178. ^aBlock value
  179. !
  180. isNil
  181. ^false
  182. !
  183. notNil
  184. ^self isNil not
  185. !
  186. isClass
  187. ^false
  188. !
  189. isMetaclass
  190. ^false
  191. !
  192. isNumber
  193. ^false
  194. !
  195. isString
  196. ^false
  197. !
  198. isParseFailure
  199. ^false
  200. ! !
  201. !Object class methodsFor: 'initialization'!
  202. initialize
  203. "no op"
  204. ! !
  205. Object subclass: #Smalltalk
  206. instanceVariableNames: ''
  207. category: 'Kernel'!
  208. !Smalltalk methodsFor: 'accessing'!
  209. classes
  210. <return self.classes()>
  211. !
  212. at: aString
  213. <return self[aString]>
  214. !
  215. basicParse: aString
  216. <return smalltalk.parser.parse(aString)>
  217. !
  218. parse: aString
  219. | result |
  220. self try: [result := self basicParse: aString] catch: [:ex | (self parseError: ex parsing: aString) signal].
  221. ^result
  222. !
  223. parseError: anException parsing: aString
  224. | row col message lines badLine code |
  225. <row = anException.line;
  226. col = anException.column;
  227. message = anException.message;>.
  228. lines := aString lines.
  229. badLine := lines at: row.
  230. badLine := (badLine copyFrom: 1 to: col - 1), ' ===>', (badLine copyFrom: col to: badLine size).
  231. lines at: row put: badLine.
  232. code := String streamContents: [:s |
  233. lines withIndexDo: [:l :i |
  234. s nextPutAll: i asString, ': ', l, String lf]].
  235. ^ Error new messageText: ('Parse error on line ' , row , ' column ' , col , ' : ' , message , ' Below is code with line numbers and ===> marker inserted:' , String lf, code)
  236. !
  237. reservedWords
  238. "JavaScript reserved words"
  239. <return self.reservedWords>
  240. !
  241. readJSObject: anObject
  242. <return self.readJSObject(anObject)>
  243. ! !
  244. !Smalltalk methodsFor: 'classes'!
  245. removeClass: aClass
  246. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  247. aClass methodDictionary values do: [:each |
  248. aClass removeCompiledMethod: each].
  249. aClass class methodDictionary values do: [:each |
  250. aClass class removeCompiledMethod: each].
  251. self basicDelete: aClass name
  252. ! !
  253. !Smalltalk methodsFor: 'packages'!
  254. packages
  255. "Return all Package instances in the system."
  256. <return self.packages.all()>
  257. !
  258. packageAt: packageName
  259. <return self.packages[packageName]>
  260. !
  261. packageAt: packageName ifAbsent: aBlock
  262. ^(self packageAt: packageName) ifNil: aBlock
  263. !
  264. removePackage: packageName
  265. "Removes a package and all its classes."
  266. | pkg |
  267. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  268. pkg classes do: [:each |
  269. self removeClass: each].
  270. self deletePackage: packageName
  271. !
  272. renamePackage: packageName to: newName
  273. "Rename a package."
  274. | pkg |
  275. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  276. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  277. <smalltalk.packages[newName] = smalltalk.packages[packageName]>.
  278. pkg name: newName.
  279. self deletePackage: packageName.
  280. ! !
  281. !Smalltalk methodsFor: 'private'!
  282. createPackage: packageName
  283. "Create and bind a new package with given name and return it."
  284. <return smalltalk.addPackage(packageName, nil)>
  285. !
  286. deletePackage: packageName
  287. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  288. To remove a package, use #removePackage instead."
  289. <delete smalltalk.packages[packageName]>
  290. !
  291. createPackage: packageName properties: aDict
  292. "Create and bind a new package with given name and return it."
  293. | object |
  294. <object = {};>.
  295. aDict keysAndValuesDo: [:key :value |
  296. <object[key] = value>.
  297. ].
  298. <return smalltalk.addPackage(packageName, object)>
  299. ! !
  300. Smalltalk class instanceVariableNames: 'current'!
  301. !Smalltalk class methodsFor: 'accessing'!
  302. current
  303. <return smalltalk>
  304. ! !
  305. Object subclass: #Package
  306. instanceVariableNames: ''
  307. category: 'Kernel'!
  308. !Package commentStamp!
  309. 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.
  310. 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.
  311. Packages are manipulated through "Smalltalk current", like for example finding one based on a name:
  312. Smalltalk current packageAt: 'Kernel'
  313. ...but you can also use:
  314. Package named: 'Kernel'
  315. 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
  316. 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
  317. String where the method category "*IDE" defines #inspectOn: which thus is a method belonging to the IDE package.!
  318. !Package methodsFor: 'accessing'!
  319. name
  320. <return self.pkgName>
  321. !
  322. name: aString
  323. <return self.pkgName = aString>
  324. !
  325. dependencies
  326. ^self propertyAt: 'dependencies' ifAbsent: [#()]
  327. !
  328. dependencies: anArray
  329. ^self propertyAt: 'dependencies' put: anArray
  330. !
  331. properties
  332. ^Smalltalk current readJSObject: (self basicAt: 'properties')
  333. !
  334. properties: aDict
  335. "We store it as a javascript object."
  336. | object |
  337. <object = {};>.
  338. aDict keysAndValuesDo: [:key :value |
  339. <object[key] = value>.
  340. ].
  341. <return self.properties = object>
  342. ! !
  343. !Package methodsFor: 'classes'!
  344. classes
  345. "We need to do a reverse scan."
  346. ^Smalltalk current classes select: [:c | c package == self]
  347. ! !
  348. !Package methodsFor: 'printing'!
  349. printString
  350. ^self name
  351. ! !
  352. !Package methodsFor: 'private'!
  353. propertiesAsJSON
  354. <return JSON.stringify(self.properties)>
  355. !
  356. jsProperties
  357. <return self.properties>
  358. !
  359. jsProperties: aJSObject
  360. <return self.properties = aJSObject>
  361. ! !
  362. !Package methodsFor: 'properties'!
  363. propertyAt: key
  364. <return self.properties[key]>
  365. !
  366. propertyAt: key put: value
  367. <return self.properties[key] = value>
  368. !
  369. propertyAt: key ifAbsent: block
  370. ^(self propertyAt: key) ifNil: [block value]
  371. ! !
  372. !Package class methodsFor: 'not yet classified'!
  373. named: aPackageName
  374. ^Smalltalk current packageAt: aPackageName
  375. !
  376. named: aPackageName ifAbsent: aBlock
  377. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  378. ! !
  379. Object subclass: #Behavior
  380. instanceVariableNames: ''
  381. category: 'Kernel'!
  382. !Behavior methodsFor: 'accessing'!
  383. name
  384. <return self.className || nil>
  385. !
  386. superclass
  387. <return self.superclass || nil>
  388. !
  389. subclasses
  390. <return smalltalk.subclasses(self)>
  391. !
  392. allSubclasses
  393. | result |
  394. result := self subclasses.
  395. self subclasses do: [:each |
  396. result addAll: each allSubclasses].
  397. ^result
  398. !
  399. withAllSubclasses
  400. ^(Array with: self) addAll: self allSubclasses; yourself
  401. !
  402. prototype
  403. <return self.fn.prototype>
  404. !
  405. methodDictionary
  406. <var dict = smalltalk.Dictionary._new();
  407. var methods = self.fn.prototype.methods;
  408. for(var i in methods) {
  409. if(methods[i].selector) {
  410. dict._at_put_(methods[i].selector, methods[i]);
  411. }
  412. };
  413. return dict>
  414. !
  415. methodsFor: aString
  416. ^ClassCategoryReader new
  417. class: self category: aString;
  418. yourself
  419. !
  420. addCompiledMethod: aMethod
  421. <smalltalk.addMethod(aMethod.selector._asSelector(), aMethod, self)>
  422. !
  423. instanceVariableNames
  424. <return self.iVarNames>
  425. !
  426. comment
  427. ^(self basicAt: 'comment') ifNil: ['']
  428. !
  429. comment: aString
  430. self basicAt: 'comment' put: aString
  431. !
  432. commentStamp
  433. ^ClassCommentReader new
  434. class: self;
  435. yourself
  436. !
  437. removeCompiledMethod: aMethod
  438. <delete self.fn.prototype[aMethod.selector._asSelector()];
  439. delete self.fn.prototype.methods[aMethod.selector];
  440. smalltalk.init(self);>
  441. !
  442. protocols
  443. | protocols |
  444. protocols := Array new.
  445. self methodDictionary do: [:each |
  446. (protocols includes: each category) ifFalse: [
  447. protocols add: each category]].
  448. ^protocols sort
  449. !
  450. protocolsDo: aBlock
  451. "Execute aBlock for each method category with
  452. its collection of methods in the sort order of category name."
  453. | methodsByCategory |
  454. methodsByCategory := Dictionary new.
  455. self methodDictionary values do: [:m |
  456. (methodsByCategory at: m category ifAbsentPut: [Array new])
  457. add: m].
  458. self protocols do: [:category |
  459. aBlock value: category value: (methodsByCategory at: category)]
  460. !
  461. allInstanceVariableNames
  462. | result |
  463. result := self instanceVariableNames copy.
  464. self superclass ifNotNil: [
  465. result addAll: self superclass allInstanceVariableNames].
  466. ^result
  467. !
  468. methodAt: aString
  469. <return smalltalk.methods(self)[aString]>
  470. !
  471. methodsFor: aString stamp: aStamp
  472. "Added for compatibility, right now ignores stamp."
  473. ^self methodsFor: aString
  474. !
  475. commentStamp: aStamp prior: prior
  476. ^self commentStamp
  477. ! !
  478. !Behavior methodsFor: 'compiling'!
  479. compile: aString
  480. self compile: aString category: ''
  481. !
  482. compile: aString category: anotherString
  483. | method |
  484. method := Compiler new load: aString forClass: self.
  485. method category: anotherString.
  486. self addCompiledMethod: method
  487. ! !
  488. !Behavior methodsFor: 'instance creation'!
  489. new
  490. ^self basicNew initialize
  491. !
  492. basicNew
  493. <return new self.fn()>
  494. !
  495. inheritsFrom: aClass
  496. ^aClass allSubclasses includes: self
  497. ! !
  498. Behavior subclass: #Class
  499. instanceVariableNames: ''
  500. category: 'Kernel'!
  501. !Class methodsFor: 'accessing'!
  502. category
  503. ^self package ifNil: ['Unclassified'] ifNotNil: [self package name]
  504. !
  505. rename: aString
  506. <
  507. smalltalk[aString] = self;
  508. delete smalltalk[self.className];
  509. self.className = aString;
  510. >
  511. !
  512. package
  513. <return self.pkg>
  514. !
  515. package: aPackage
  516. <self.pkg = aPackage>
  517. ! !
  518. !Class methodsFor: 'class creation'!
  519. subclass: aString instanceVariableNames: anotherString
  520. "Kept for compatibility."
  521. ^self subclass: aString instanceVariableNames: anotherString package: nil
  522. !
  523. subclass: aString instanceVariableNames: aString2 category: aString3
  524. "Kept for compatibility."
  525. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  526. !
  527. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  528. "Just ignore class variables and pools. Added for compatibility."
  529. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  530. !
  531. subclass: aString instanceVariableNames: aString2 package: aString3
  532. ^ClassBuilder new
  533. superclass: self subclass: aString instanceVariableNames: aString2 package: aString3
  534. ! !
  535. !Class methodsFor: 'printing'!
  536. printString
  537. ^self name
  538. ! !
  539. !Class methodsFor: 'testing'!
  540. isClass
  541. ^true
  542. ! !
  543. Behavior subclass: #Metaclass
  544. instanceVariableNames: ''
  545. category: 'Kernel'!
  546. !Metaclass methodsFor: 'accessing'!
  547. instanceClass
  548. <return self.instanceClass>
  549. !
  550. instanceVariableNames: aCollection
  551. ClassBuilder new
  552. class: self instanceVariableNames: aCollection
  553. ! !
  554. !Metaclass methodsFor: 'printing'!
  555. printString
  556. ^self instanceClass name, ' class'
  557. ! !
  558. !Metaclass methodsFor: 'testing'!
  559. isMetaclass
  560. ^true
  561. ! !
  562. Object subclass: #CompiledMethod
  563. instanceVariableNames: ''
  564. category: 'Kernel'!
  565. !CompiledMethod methodsFor: 'accessing'!
  566. source
  567. ^(self basicAt: 'source') ifNil: ['']
  568. !
  569. source: aString
  570. self basicAt: 'source' put: aString
  571. !
  572. category
  573. ^(self basicAt: 'category') ifNil: ['']
  574. !
  575. category: aString
  576. self basicAt: 'category' put: aString
  577. !
  578. selector
  579. ^self basicAt: 'selector'
  580. !
  581. selector: aString
  582. self basicAt: 'selector' put: aString
  583. !
  584. fn
  585. ^self basicAt: 'fn'
  586. !
  587. fn: aBlock
  588. self basicAt: 'fn' put: aBlock
  589. !
  590. messageSends
  591. ^self basicAt: 'messageSends'
  592. !
  593. methodClass
  594. ^self basicAt: 'methodClass'
  595. !
  596. referencedClasses
  597. ^self basicAt: 'referencedClasses'
  598. !
  599. arguments
  600. <return self.args || []>
  601. ! !
  602. Object subclass: #Number
  603. instanceVariableNames: ''
  604. category: 'Kernel'!
  605. !Number methodsFor: ''!
  606. ! !
  607. !Number methodsFor: 'arithmetic'!
  608. + aNumber
  609. "Inlined in the Compiler"
  610. <return self + aNumber>
  611. !
  612. - aNumber
  613. "Inlined in the Compiler"
  614. <return self - aNumber>
  615. !
  616. * aNumber
  617. "Inlined in the Compiler"
  618. <return self * aNumber>
  619. !
  620. / aNumber
  621. "Inlined in the Compiler"
  622. <return self / aNumber>
  623. !
  624. max: aNumber
  625. <return Math.max(self, aNumber);>
  626. !
  627. min: aNumber
  628. <return Math.min(self, aNumber);>
  629. !
  630. negated
  631. ^0 - self
  632. !
  633. \\ aNumber
  634. <return self % aNumber>
  635. !
  636. sqrt
  637. <return Math.sqrt(self)>
  638. !
  639. squared
  640. ^self * self
  641. ! !
  642. !Number methodsFor: 'comparing'!
  643. = aNumber
  644. aNumber class = self class ifFalse: [^false].
  645. <return Number(self) == aNumber>
  646. !
  647. > aNumber
  648. "Inlined in the Compiler"
  649. <return self >> aNumber>
  650. !
  651. < aNumber
  652. "Inlined in the Compiler"
  653. <return self < aNumber>
  654. !
  655. >= aNumber
  656. "Inlined in the Compiler"
  657. <return self >>= aNumber>
  658. !
  659. <= aNumber
  660. "Inlined in the Compiler"
  661. <return self <= aNumber>
  662. !
  663. == aNumber
  664. aNumber class = self class ifFalse: [^false].
  665. <return Number(self) === Number(aNumber)>
  666. ! !
  667. !Number methodsFor: 'converting'!
  668. rounded
  669. <return Math.round(self);>
  670. !
  671. truncated
  672. |result|
  673. self >= 0
  674. ifTrue: [<result = Math.floor(self);>]
  675. ifFalse: [<result = (Math.floor(self * (-1)) * (-1));>].
  676. ^ result
  677. !
  678. to: aNumber
  679. | array first last count |
  680. first := self truncated.
  681. last := aNumber truncated + 1.
  682. count := 1.
  683. array := Array new.
  684. (last - first) timesRepeat: [
  685. array at: count put: first.
  686. count := count + 1.
  687. first := first + 1].
  688. ^array
  689. !
  690. asString
  691. ^self printString
  692. !
  693. asJavascript
  694. ^'(', self printString, ')'
  695. !
  696. atRandom
  697. ^(Random new next * self) truncated + 1
  698. !
  699. @ aNumber
  700. ^Point x: self y: aNumber
  701. !
  702. asPoint
  703. ^Point x: self y: self
  704. !
  705. asJSONObject
  706. ^self
  707. !
  708. to: stop by: step
  709. | array value pos |
  710. value := self.
  711. array := Array new.
  712. pos := 1.
  713. step = 0 ifTrue: [self error: 'step must be non-zero'].
  714. step < 0
  715. ifTrue: [[ value >= stop ] whileTrue: [
  716. array at: pos put: value.
  717. pos := pos + 1.
  718. value := value + step]]
  719. ifFalse: [[ value <= stop ] whileTrue: [
  720. array at: pos put: value.
  721. pos := pos + 1.
  722. value := value + step]].
  723. ^array
  724. ! !
  725. !Number methodsFor: 'copying'!
  726. deepCopy
  727. ^self copy
  728. !
  729. copy
  730. ^self
  731. ! !
  732. !Number methodsFor: 'enumerating'!
  733. timesRepeat: aBlock
  734. | integer count |
  735. integer := self truncated.
  736. count := 1.
  737. [count > self] whileFalse: [
  738. aBlock value.
  739. count := count + 1]
  740. !
  741. to: stop do: aBlock
  742. "Evaluate aBlock for each number from self to aNumber."
  743. | nextValue |
  744. nextValue := self.
  745. [nextValue <= stop]
  746. whileTrue:
  747. [aBlock value: nextValue.
  748. nextValue := nextValue + 1]
  749. !
  750. to: stop by: step do: aBlock
  751. | value |
  752. value := self.
  753. step = 0 ifTrue: [self error: 'step must be non-zero'].
  754. step < 0
  755. ifTrue: [[ value >= stop ] whileTrue: [
  756. aBlock value: value.
  757. value := value + step]]
  758. ifFalse: [[ value <= stop ] whileTrue: [
  759. aBlock value: value.
  760. value := value + step]]
  761. ! !
  762. !Number methodsFor: 'printing'!
  763. printString
  764. <return String(self)>
  765. !
  766. printShowingDecimalPlaces: placesDesired
  767. <return self.toFixed(placesDesired)>
  768. ! !
  769. !Number methodsFor: 'testing'!
  770. isNumber
  771. ^true
  772. !
  773. even
  774. ^ 0 = (self \\ 2)
  775. !
  776. odd
  777. ^ self even not
  778. ! !
  779. !Number methodsFor: 'timeouts/intervals'!
  780. clearInterval
  781. <clearInterval(Number(self))>
  782. !
  783. clearTimeout
  784. <clearTimeout(Number(self))>
  785. ! !
  786. !Number class methodsFor: 'instance creation'!
  787. pi
  788. <return Math.PI>
  789. ! !
  790. Object subclass: #BlockClosure
  791. instanceVariableNames: ''
  792. category: 'Kernel'!
  793. !BlockClosure methodsFor: 'accessing'!
  794. compiledSource
  795. <return self.toString()>
  796. !
  797. numArgs
  798. <return self.length>
  799. ! !
  800. !BlockClosure methodsFor: 'controlling'!
  801. whileTrue: aBlock
  802. "inlined in the Compiler"
  803. <while(self()) {aBlock()}>
  804. !
  805. whileFalse: aBlock
  806. "inlined in the Compiler"
  807. <while(!!self()) {aBlock()}>
  808. !
  809. whileFalse
  810. "inlined in the Compiler"
  811. self whileFalse: []
  812. !
  813. whileTrue
  814. "inlined in the Compiler"
  815. self whileTrue: []
  816. ! !
  817. !BlockClosure methodsFor: 'error handling'!
  818. on: anErrorClass do: aBlock
  819. self try: self catch: [:error |
  820. (error isKindOf: anErrorClass)
  821. ifTrue: [aBlock value: error]
  822. ifFalse: [error signal]]
  823. ! !
  824. !BlockClosure methodsFor: 'evaluating'!
  825. value
  826. "inlined in the Compiler"
  827. <return self();>
  828. !
  829. value: anArg
  830. "inlined in the Compiler"
  831. <return self(anArg);>
  832. !
  833. value: firstArg value: secondArg
  834. "inlined in the Compiler"
  835. <return self(firstArg, secondArg);>
  836. !
  837. value: firstArg value: secondArg value: thirdArg
  838. "inlined in the Compiler"
  839. <return self(firstArg, secondArg, thirdArg);>
  840. !
  841. valueWithPossibleArguments: aCollection
  842. <return self.apply(null, aCollection);>
  843. !
  844. new
  845. "Use the receiver as a JS constructor.
  846. *Do not* use this method to instanciate Smalltalk objects!!"
  847. <return new self()>
  848. !
  849. applyTo: anObject arguments: aCollection
  850. <return self.apply(anObject, aCollection)>
  851. !
  852. timeToRun
  853. "Answer the number of milliseconds taken to execute this block."
  854. ^ Date millisecondsToRun: self
  855. !
  856. ensure: aBlock
  857. | success |
  858. success := false.
  859. [self value. success := true. aBlock value]
  860. on: Error
  861. do: [:ex |
  862. success ifFalse: [aBlock value].
  863. ex signal]
  864. ! !
  865. !BlockClosure methodsFor: 'timeout/interval'!
  866. valueWithTimeout: aNumber
  867. <return setTimeout(self, aNumber)>
  868. !
  869. valueWithInterval: aNumber
  870. <return setInterval(self, aNumber)>
  871. ! !
  872. Object subclass: #Boolean
  873. instanceVariableNames: ''
  874. category: 'Kernel'!
  875. !Boolean methodsFor: 'comparing'!
  876. = aBoolean
  877. aBoolean class = self class ifFalse: [^false].
  878. <return Boolean(self == true) == aBoolean>
  879. !
  880. == aBoolean
  881. aBoolean class = self class ifFalse: [^false].
  882. <return Boolean(self == true) === Boolean(aBoolean == true)>
  883. ! !
  884. !Boolean methodsFor: 'controlling'!
  885. ifTrue: aBlock
  886. "inlined in the Compiler"
  887. ^self ifTrue: aBlock ifFalse: []
  888. !
  889. ifFalse: aBlock
  890. "inlined in the Compiler"
  891. ^self ifTrue: [] ifFalse: aBlock
  892. !
  893. ifFalse: aBlock ifTrue: anotherBlock
  894. "inlined in the Compiler"
  895. ^self ifTrue: anotherBlock ifFalse: aBlock
  896. !
  897. ifTrue: aBlock ifFalse: anotherBlock
  898. "inlined in the Compiler"
  899. <
  900. if(self == true) {
  901. return aBlock();
  902. } else {
  903. return anotherBlock();
  904. }
  905. >
  906. !
  907. and: aBlock
  908. ^self = true
  909. ifTrue: aBlock
  910. ifFalse: [false]
  911. !
  912. or: aBlock
  913. ^self = true
  914. ifTrue: [true]
  915. ifFalse: aBlock
  916. !
  917. not
  918. ^self = false
  919. !
  920. & aBoolean
  921. <
  922. if(self == true) {
  923. return aBoolean;
  924. } else {
  925. return false;
  926. }
  927. >
  928. !
  929. | aBoolean
  930. <
  931. if(self == true) {
  932. return true;
  933. } else {
  934. return aBoolean;
  935. }
  936. >
  937. ! !
  938. !Boolean methodsFor: 'converting'!
  939. asJSONObject
  940. ^self
  941. ! !
  942. !Boolean methodsFor: 'copying'!
  943. shallowCopy
  944. ^self
  945. !
  946. deepCopy
  947. ^self
  948. ! !
  949. !Boolean methodsFor: 'printing'!
  950. printString
  951. <return self.toString()>
  952. ! !
  953. Object subclass: #Date
  954. instanceVariableNames: ''
  955. category: 'Kernel'!
  956. !Date commentStamp!
  957. The Date class is used to work with dates and times.!
  958. !Date methodsFor: 'accessing'!
  959. year
  960. <return self.getFullYear()>
  961. !
  962. month
  963. <return self.getMonth() + 1>
  964. !
  965. month: aNumber
  966. <self.setMonth(aNumber - 1)>
  967. !
  968. day
  969. ^self dayOfWeek
  970. !
  971. dayOfWeek
  972. <return self.getDay() + 1>
  973. !
  974. dayOfWeek: aNumber
  975. <return self.setDay(aNumber - 1)>
  976. !
  977. day: aNumber
  978. self day: aNumber
  979. !
  980. year: aNumber
  981. <self.setFullYear(aNumber)>
  982. !
  983. dayOfMonth
  984. <return self.getDate()>
  985. !
  986. dayOfMonth: aNumber
  987. <self.setDate(aNumber)>
  988. !
  989. time
  990. <return self.getTime()>
  991. !
  992. time: aNumber
  993. <self.setTime(aNumber)>
  994. !
  995. hours: aNumber
  996. <self.setHours(aNumber)>
  997. !
  998. minutes: aNumber
  999. <self.setMinutes(aNumber)>
  1000. !
  1001. seconds: aNumber
  1002. <self.setSeconds(aNumber)>
  1003. !
  1004. milliseconds: aNumber
  1005. <self.setMilliseconds(aNumber)>
  1006. !
  1007. hours
  1008. <return self.getHours()>
  1009. !
  1010. minutes
  1011. <return self.getMinutes()>
  1012. !
  1013. seconds
  1014. <return self.getSeconds()>
  1015. !
  1016. milliseconds
  1017. <return self.getMilliseconds()>
  1018. ! !
  1019. !Date methodsFor: 'arithmetic'!
  1020. - aDate
  1021. <return self - aDate>
  1022. !
  1023. + aDate
  1024. <return self + aDate>
  1025. ! !
  1026. !Date methodsFor: 'comparing'!
  1027. < aDate
  1028. <return self < aDate>
  1029. !
  1030. > aDate
  1031. <return self >> aDate>
  1032. !
  1033. <= aDate
  1034. <return self <= aDate>
  1035. !
  1036. >= aDate
  1037. <return self >>= aDate>
  1038. ! !
  1039. !Date methodsFor: 'converting'!
  1040. asString
  1041. <return self.toString()>
  1042. !
  1043. asMilliseconds
  1044. ^self time
  1045. !
  1046. asDateString
  1047. <return self.toDateString()>
  1048. !
  1049. asTimeString
  1050. <return self.toTimeString()>
  1051. !
  1052. asLocaleString
  1053. <return self.toLocaleString()>
  1054. !
  1055. asNumber
  1056. ^self asMilliseconds
  1057. !
  1058. asJSONObject
  1059. ^self
  1060. ! !
  1061. !Date methodsFor: 'printing'!
  1062. printString
  1063. ^self asString
  1064. ! !
  1065. !Date class methodsFor: 'instance creation'!
  1066. new: anObject
  1067. <return new Date(anObject)>
  1068. !
  1069. fromString: aString
  1070. "Example: Date fromString('2011/04/15 00:00:00')"
  1071. ^self new: aString
  1072. !
  1073. fromSeconds: aNumber
  1074. ^self fromMilliseconds: aNumber * 1000
  1075. !
  1076. fromMilliseconds: aNumber
  1077. ^self new: aNumber
  1078. !
  1079. today
  1080. ^self new
  1081. !
  1082. now
  1083. ^self today
  1084. !
  1085. millisecondsToRun: aBlock
  1086. | t |
  1087. t := Date now.
  1088. aBlock value.
  1089. ^Date now - t
  1090. ! !
  1091. Object subclass: #UndefinedObject
  1092. instanceVariableNames: ''
  1093. category: 'Kernel'!
  1094. !UndefinedObject methodsFor: 'class creation'!
  1095. subclass: aString instanceVariableNames: anotherString
  1096. ^self subclass: aString instanceVariableNames: anotherString package: nil
  1097. !
  1098. subclass: aString instanceVariableNames: aString2 category: aString3
  1099. "Kept for compatibility."
  1100. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  1101. !
  1102. subclass: aString instanceVariableNames: aString2 package: aString3
  1103. ^ClassBuilder new
  1104. superclass: self subclass: aString instanceVariableNames: aString2 package: aString3
  1105. ! !
  1106. !UndefinedObject methodsFor: 'copying'!
  1107. shallowCopy
  1108. ^self
  1109. !
  1110. deepCopy
  1111. ^self
  1112. ! !
  1113. !UndefinedObject methodsFor: 'printing'!
  1114. printString
  1115. ^'nil'
  1116. ! !
  1117. !UndefinedObject methodsFor: 'testing'!
  1118. ifNil: aBlock
  1119. "inlined in the Compiler"
  1120. ^self ifNil: aBlock ifNotNil: []
  1121. !
  1122. ifNotNil: aBlock
  1123. "inlined in the Compiler"
  1124. ^self
  1125. !
  1126. ifNil: aBlock ifNotNil: anotherBlock
  1127. "inlined in the Compiler"
  1128. ^aBlock value
  1129. !
  1130. ifNotNil: aBlock ifNil: anotherBlock
  1131. "inlined in the Compiler"
  1132. ^anotherBlock value
  1133. !
  1134. isNil
  1135. ^true
  1136. !
  1137. notNil
  1138. ^false
  1139. ! !
  1140. !UndefinedObject class methodsFor: 'instance creation'!
  1141. new
  1142. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1143. ! !
  1144. Object subclass: #Collection
  1145. instanceVariableNames: ''
  1146. category: 'Kernel'!
  1147. !Collection methodsFor: 'accessing'!
  1148. size
  1149. self subclassResponsibility
  1150. !
  1151. readStream
  1152. ^self stream
  1153. !
  1154. writeStream
  1155. ^self stream
  1156. !
  1157. stream
  1158. ^self streamClass on: self
  1159. !
  1160. streamClass
  1161. ^self class streamClass
  1162. ! !
  1163. !Collection methodsFor: 'adding/removing'!
  1164. add: anObject
  1165. self subclassResponsibility
  1166. !
  1167. addAll: aCollection
  1168. aCollection do: [:each |
  1169. self add: each].
  1170. ^aCollection
  1171. !
  1172. remove: anObject
  1173. self subclassResponsibility
  1174. ! !
  1175. !Collection methodsFor: 'converting'!
  1176. asArray
  1177. | array index |
  1178. array := Array new.
  1179. index := 0.
  1180. self do: [:each |
  1181. index := index + 1.
  1182. array at: index put: each].
  1183. ^array
  1184. !
  1185. asSet
  1186. ^Set withAll: self
  1187. ! !
  1188. !Collection methodsFor: 'copying'!
  1189. , aCollection
  1190. ^self copy
  1191. addAll: aCollection;
  1192. yourself
  1193. !
  1194. copyWith: anObject
  1195. ^self copy add: anObject; yourself
  1196. !
  1197. copyWithAll: aCollection
  1198. ^self copy addAll: aCollection; yourself
  1199. !
  1200. copyWithoutAll: aCollection
  1201. "Answer a copy of the receiver that does not contain any elements
  1202. equal to those in aCollection."
  1203. ^ self reject: [:each | aCollection includes: each]
  1204. ! !
  1205. !Collection methodsFor: 'enumerating'!
  1206. do: aBlock
  1207. <for(var i=0;i<self.length;i++){aBlock(self[i]);}>
  1208. !
  1209. collect: aBlock
  1210. | newCollection |
  1211. newCollection := self class new.
  1212. self do: [:each |
  1213. newCollection add: (aBlock value: each)].
  1214. ^newCollection
  1215. !
  1216. detect: aBlock
  1217. ^self detect: aBlock ifNone: [self errorNotFound]
  1218. !
  1219. detect: aBlock ifNone: anotherBlock
  1220. <
  1221. for(var i = 0; i < self.length; i++)
  1222. if(aBlock(self[i]))
  1223. return self[i];
  1224. return anotherBlock();
  1225. >
  1226. !
  1227. do: aBlock separatedBy: anotherBlock
  1228. | first |
  1229. first := true.
  1230. self do: [:each |
  1231. first
  1232. ifTrue: [first := false]
  1233. ifFalse: [anotherBlock value].
  1234. aBlock value: each]
  1235. !
  1236. inject: anObject into: aBlock
  1237. | result |
  1238. result := anObject.
  1239. self do: [:each |
  1240. result := aBlock value: result value: each].
  1241. ^result
  1242. !
  1243. reject: aBlock
  1244. ^self select: [:each | (aBlock value: each) = false]
  1245. !
  1246. select: aBlock
  1247. | stream |
  1248. stream := self class new writeStream.
  1249. self do: [:each |
  1250. (aBlock value: each) ifTrue: [
  1251. stream nextPut: each]].
  1252. ^stream contents
  1253. ! !
  1254. !Collection methodsFor: 'error handling'!
  1255. errorNotFound
  1256. self error: 'Object is not in the collection'
  1257. ! !
  1258. !Collection methodsFor: 'testing'!
  1259. includes: anObject
  1260. <
  1261. var i = self.length;
  1262. while (i--) {
  1263. if (smalltalk.send(self[i], "__eq", [anObject])) {return true;}
  1264. }
  1265. return false
  1266. >
  1267. !
  1268. notEmpty
  1269. ^self isEmpty not
  1270. !
  1271. isEmpty
  1272. ^self size = 0
  1273. !
  1274. ifNotEmpty: aBlock
  1275. self notEmpty ifTrue: aBlock.
  1276. !
  1277. ifEmpty: aBlock
  1278. self isEmpty ifTrue: aBlock.
  1279. ! !
  1280. !Collection class methodsFor: 'accessing'!
  1281. streamClass
  1282. ^Stream
  1283. ! !
  1284. !Collection class methodsFor: 'instance creation'!
  1285. with: anObject
  1286. ^self new
  1287. add: anObject;
  1288. yourself
  1289. !
  1290. with: anObject with: anotherObject
  1291. ^self new
  1292. add: anObject;
  1293. add: anotherObject;
  1294. yourself
  1295. !
  1296. with: firstObject with: secondObject with: thirdObject
  1297. ^self new
  1298. add: firstObject;
  1299. add: secondObject;
  1300. add: thirdObject;
  1301. yourself
  1302. !
  1303. withAll: aCollection
  1304. ^self new
  1305. addAll: aCollection;
  1306. yourself
  1307. ! !
  1308. Collection subclass: #SequenceableCollection
  1309. instanceVariableNames: ''
  1310. category: 'Kernel'!
  1311. !SequenceableCollection methodsFor: 'accessing'!
  1312. at: anIndex
  1313. ^self at: anIndex ifAbsent: [
  1314. self errorNotFound]
  1315. !
  1316. at: anIndex ifAbsent: aBlock
  1317. self subclassResponsibility
  1318. !
  1319. at: anIndex put: anObject
  1320. self subclassResponsibility
  1321. !
  1322. first
  1323. ^self at: 1
  1324. !
  1325. fourth
  1326. ^self at: 4
  1327. !
  1328. last
  1329. ^self at: self size
  1330. !
  1331. second
  1332. ^self at: 2
  1333. !
  1334. third
  1335. ^self at: 3
  1336. !
  1337. allButFirst
  1338. ^self copyFrom: 2 to: self size
  1339. !
  1340. allButLast
  1341. ^self copyFrom: 1 to: self size - 1
  1342. !
  1343. indexOf: anObject
  1344. ^self indexOf: anObject ifAbsent: [self errorNotFound]
  1345. !
  1346. indexOf: anObject ifAbsent: aBlock
  1347. <
  1348. for(var i=0;i<self.length;i++){
  1349. if(self[i].__eq(anObject)) {return i+1}
  1350. }
  1351. return aBlock();
  1352. >
  1353. !
  1354. indexOf: anObject startingAt: start ifAbsent: aBlock
  1355. <
  1356. for(var i=start-1;i<self.length;i++){
  1357. if(self[i].__eq(anObject)) {return i+1}
  1358. }
  1359. return aBlock();
  1360. >
  1361. !
  1362. indexOf: anObject startingAt: start
  1363. "Answer the index of the first occurence of anElement after start
  1364. within the receiver. If the receiver does not contain anElement,
  1365. answer 0."
  1366. ^self indexOf: anObject startingAt: start ifAbsent: [0]
  1367. !
  1368. atRandom
  1369. ^ self at: self size atRandom
  1370. ! !
  1371. !SequenceableCollection methodsFor: 'adding'!
  1372. removeLast
  1373. self remove: self last
  1374. !
  1375. addLast: anObject
  1376. self add: anObject
  1377. ! !
  1378. !SequenceableCollection methodsFor: 'converting'!
  1379. reversed
  1380. self subclassResponsibility
  1381. ! !
  1382. !SequenceableCollection methodsFor: 'copying'!
  1383. copyFrom: anIndex to: anotherIndex
  1384. self subclassResponsibility
  1385. ! !
  1386. !SequenceableCollection methodsFor: 'enumerating'!
  1387. withIndexDo: aBlock
  1388. <for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
  1389. ! !
  1390. SequenceableCollection subclass: #String
  1391. instanceVariableNames: ''
  1392. category: 'Kernel'!
  1393. !String methodsFor: 'accessing'!
  1394. size
  1395. <return self.length>
  1396. !
  1397. at: anIndex
  1398. <return self[anIndex - 1]>
  1399. !
  1400. at: anIndex put: anObject
  1401. self errorReadOnly
  1402. !
  1403. at: anIndex ifAbsent: aBlock
  1404. (self at: anIndex) ifNil: [aBlock]
  1405. !
  1406. escaped
  1407. <return escape(self)>
  1408. !
  1409. unescaped
  1410. <return unescape(self)>
  1411. !
  1412. asciiValue
  1413. <return self.charCodeAt(0);>
  1414. ! !
  1415. !String methodsFor: 'adding'!
  1416. add: anObject
  1417. self errorReadOnly
  1418. !
  1419. remove: anObject
  1420. self errorReadOnly
  1421. ! !
  1422. !String methodsFor: 'comparing'!
  1423. = aString
  1424. aString class = self class ifFalse: [^false].
  1425. <return String(self) == aString>
  1426. !
  1427. > aString
  1428. <return String(self) >> aString>
  1429. !
  1430. < aString
  1431. <return String(self) < aString>
  1432. !
  1433. >= aString
  1434. <return String(self) >>= aString>
  1435. !
  1436. <= aString
  1437. <return String(self) <= aString>
  1438. !
  1439. == aString
  1440. aString class = self class ifFalse: [^false].
  1441. <return String(self) === String(aString)>
  1442. ! !
  1443. !String methodsFor: 'converting'!
  1444. asSelector
  1445. "If you change this method, change smalltalk.convertSelector too (see js/boot.js file)"
  1446. | selector |
  1447. selector := '_', self.
  1448. selector := selector replace: ':' with: '_'.
  1449. selector := selector replace: '[+]' with: '_plus'.
  1450. selector := selector replace: '-' with: '_minus'.
  1451. selector := selector replace: '[*]' with: '_star'.
  1452. selector := selector replace: '[/]' with: '_slash'.
  1453. selector := selector replace: '>' with: '_gt'.
  1454. selector := selector replace: '<' with: '_lt'.
  1455. selector := selector replace: '=' with: '_eq'.
  1456. selector := selector replace: ',' with: '_comma'.
  1457. selector := selector replace: '[@]' with: '_at'.
  1458. ^selector
  1459. !
  1460. asJavascript
  1461. <
  1462. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1463. return "unescape(\"" + escape(self) + "\")";
  1464. else
  1465. return "\"" + self + "\"";
  1466. >
  1467. !
  1468. tokenize: aString
  1469. <return self.split(aString)>
  1470. !
  1471. asString
  1472. ^self
  1473. !
  1474. asNumber
  1475. <return Number(self)>
  1476. !
  1477. asJSONObject
  1478. ^self
  1479. !
  1480. asLowercase
  1481. <return self.toLowerCase()>
  1482. !
  1483. asUppercase
  1484. <return self.toUpperCase()>
  1485. !
  1486. reversed
  1487. <return self.split("").reverse().join("")>
  1488. !
  1489. asJavaScriptSelector
  1490. ^(self asSelector replace: '^_' with: '') replace: '_.*' with: ''.
  1491. ! !
  1492. !String methodsFor: 'copying'!
  1493. , aString
  1494. <return self + aString>
  1495. !
  1496. copyFrom: anIndex to: anotherIndex
  1497. <return self.substring(anIndex - 1, anotherIndex)>
  1498. !
  1499. shallowCopy
  1500. ^self class fromString: self
  1501. !
  1502. deepCopy
  1503. ^self shallowCopy
  1504. ! !
  1505. !String methodsFor: 'error handling'!
  1506. errorReadOnly
  1507. self error: 'Object is read-only'
  1508. ! !
  1509. !String methodsFor: 'printing'!
  1510. printString
  1511. ^'''', self, ''''
  1512. !
  1513. printNl
  1514. <console.log(self)>
  1515. ! !
  1516. !String methodsFor: 'regular expressions'!
  1517. replace: aString with: anotherString
  1518. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1519. !
  1520. replaceRegexp: aRegexp with: aString
  1521. <return self.replace(aRegexp, aString)>
  1522. !
  1523. match: aRegexp
  1524. <return self.search(aRegexp) !!= -1>
  1525. !
  1526. trimLeft: separators
  1527. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1528. !
  1529. trimRight: separators
  1530. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1531. !
  1532. trimLeft
  1533. ^self trimLeft: '\s'
  1534. !
  1535. trimRight
  1536. ^self trimRight: '\s'
  1537. !
  1538. trimBoth
  1539. ^self trimBoth: '\s'
  1540. !
  1541. trimBoth: separators
  1542. ^(self trimLeft: separators) trimRight: separators
  1543. ! !
  1544. !String methodsFor: 'split join'!
  1545. join: aCollection
  1546. ^ String
  1547. streamContents: [:stream | aCollection
  1548. do: [:each | stream nextPutAll: each asString]
  1549. separatedBy: [stream nextPutAll: self]]
  1550. !
  1551. lineIndicesDo: aBlock
  1552. "execute aBlock with 3 arguments for each line:
  1553. - start index of line
  1554. - end index of line without line delimiter
  1555. - end index of line including line delimiter(s) CR, LF or CRLF"
  1556. | cr lf start sz nextLF nextCR |
  1557. start := 1.
  1558. sz := self size.
  1559. cr := String cr.
  1560. nextCR := self indexOf: cr startingAt: 1.
  1561. lf := String lf.
  1562. nextLF := self indexOf: lf startingAt: 1.
  1563. [ start <= sz ] whileTrue: [
  1564. (nextLF = 0 and: [ nextCR = 0 ])
  1565. ifTrue: [ "No more CR, nor LF, the string is over"
  1566. aBlock value: start value: sz value: sz.
  1567. ^self ].
  1568. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1569. ifTrue: [ "Found a LF"
  1570. aBlock value: start value: nextLF - 1 value: nextLF.
  1571. start := 1 + nextLF.
  1572. nextLF := self indexOf: lf startingAt: start ]
  1573. ifFalse: [ 1 + nextCR = nextLF
  1574. ifTrue: [ "Found a CR-LF pair"
  1575. aBlock value: start value: nextCR - 1 value: nextLF.
  1576. start := 1 + nextLF.
  1577. nextCR := self indexOf: cr startingAt: start.
  1578. nextLF := self indexOf: lf startingAt: start ]
  1579. ifFalse: [ "Found a CR"
  1580. aBlock value: start value: nextCR - 1 value: nextCR.
  1581. start := 1 + nextCR.
  1582. nextCR := self indexOf: cr startingAt: start ]]]
  1583. !
  1584. linesDo: aBlock
  1585. "Execute aBlock with each line in this string. The terminating line
  1586. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1587. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1588. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  1589. !
  1590. lines
  1591. "Answer an array of lines composing this receiver without the line ending delimiters."
  1592. | lines |
  1593. lines := Array new.
  1594. self linesDo: [:aLine | lines add: aLine].
  1595. ^lines
  1596. !
  1597. lineNumber: anIndex
  1598. "Answer a string containing the characters in the given line number."
  1599. | lineCount |
  1600. lineCount := 0.
  1601. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1602. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  1603. ^nil
  1604. ! !
  1605. !String methodsFor: 'testing'!
  1606. isString
  1607. ^true
  1608. !
  1609. includesSubString: subString
  1610. < return self.indexOf(subString) !!= -1 >
  1611. ! !
  1612. !String class methodsFor: 'accessing'!
  1613. streamClass
  1614. ^StringStream
  1615. !
  1616. cr
  1617. <return '\r'>
  1618. !
  1619. lf
  1620. <return '\n'>
  1621. !
  1622. space
  1623. <return ' '>
  1624. !
  1625. tab
  1626. <return '\t'>
  1627. !
  1628. crlf
  1629. <return '\r\n'>
  1630. ! !
  1631. !String class methodsFor: 'instance creation'!
  1632. fromString: aString
  1633. <return new self.fn(aString)>
  1634. !
  1635. streamContents: blockWithArg
  1636. |stream|
  1637. stream := (self streamClass on: String new).
  1638. blockWithArg value: stream.
  1639. ^ stream contents
  1640. !
  1641. value: aUTFCharCode
  1642. <return String.fromCharCode(aUTFCharCode);>
  1643. ! !
  1644. SequenceableCollection subclass: #Array
  1645. instanceVariableNames: ''
  1646. category: 'Kernel'!
  1647. !Array methodsFor: 'accessing'!
  1648. size
  1649. <return self.length>
  1650. !
  1651. at: anIndex put: anObject
  1652. <return self[anIndex - 1] = anObject>
  1653. !
  1654. at: anIndex ifAbsent: aBlock
  1655. <
  1656. var value = self[anIndex - 1];
  1657. if(value === undefined) {
  1658. return aBlock();
  1659. } else {
  1660. return value;
  1661. }
  1662. >
  1663. ! !
  1664. !Array methodsFor: 'adding/removing'!
  1665. add: anObject
  1666. <self.push(anObject); return anObject;>
  1667. !
  1668. remove: anObject
  1669. <
  1670. for(var i=0;i<self.length;i++) {
  1671. if(self[i] == anObject) {
  1672. self.splice(i,1);
  1673. break;
  1674. }
  1675. }
  1676. >
  1677. !
  1678. removeFrom: aNumber to: anotherNumber
  1679. <self.splice(aNumber - 1,anotherNumber - 1)>
  1680. ! !
  1681. !Array methodsFor: 'comparing'!
  1682. = aCollection
  1683. (self class = aCollection class and: [
  1684. self size = aCollection size]) ifFalse: [^false].
  1685. self withIndexDo: [:each :i |
  1686. (aCollection at: i) = each ifFalse: [^false]].
  1687. ^true
  1688. ! !
  1689. !Array methodsFor: 'converting'!
  1690. asJavascript
  1691. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  1692. !
  1693. asJSONObject
  1694. ^self collect: [:each | each asJSONObject]
  1695. !
  1696. reversed
  1697. <return self._copy().reverse()>
  1698. ! !
  1699. !Array methodsFor: 'copying'!
  1700. shallowCopy
  1701. | newCollection |
  1702. newCollection := self class new.
  1703. self do: [:each | newCollection add: each].
  1704. ^newCollection
  1705. !
  1706. deepCopy
  1707. | newCollection |
  1708. newCollection := self class new.
  1709. self do: [:each | newCollection add: each deepCopy].
  1710. ^newCollection
  1711. !
  1712. copyFrom: anIndex to: anotherIndex
  1713. | array |
  1714. array := self class new.
  1715. anIndex to: anotherIndex do: [:each |
  1716. array add: (self at: each)].
  1717. ^array
  1718. ! !
  1719. !Array methodsFor: 'enumerating'!
  1720. join: aString
  1721. <return self.join(aString)>
  1722. !
  1723. sort
  1724. ^self basicPerform: 'sort'
  1725. !
  1726. sort: aBlock
  1727. <
  1728. return self.sort(function(a, b) {
  1729. if(aBlock(a,b)) {return -1} else {return 1}
  1730. })
  1731. >
  1732. !
  1733. sorted
  1734. ^self copy sort
  1735. !
  1736. sorted: aBlock
  1737. ^self copy sort: aBlock
  1738. !
  1739. printString
  1740. | str |
  1741. str := '' writeStream.
  1742. str nextPutAll: super printString, ' ('.
  1743. self
  1744. do: [:each | str nextPutAll: each printString]
  1745. separatedBy: [str nextPutAll: ' '].
  1746. str nextPutAll: ')'.
  1747. ^str contents
  1748. ! !
  1749. Object subclass: #RegularExpression
  1750. instanceVariableNames: ''
  1751. category: 'Kernel'!
  1752. !RegularExpression methodsFor: 'evaluating'!
  1753. compile: aString
  1754. <return self.compile(aString)>
  1755. !
  1756. exec: aString
  1757. <return self.exec(aString) || nil>
  1758. !
  1759. test: aString
  1760. <return self.test(aString)>
  1761. ! !
  1762. !RegularExpression class methodsFor: 'instance creation'!
  1763. fromString: aString flag: anotherString
  1764. <return new RegExp(aString, anotherString)>
  1765. !
  1766. fromString: aString
  1767. ^self fromString: aString flag: ''
  1768. ! !
  1769. Object subclass: #Error
  1770. instanceVariableNames: 'messageText'
  1771. category: 'Kernel'!
  1772. !Error methodsFor: 'accessing'!
  1773. messageText
  1774. ^messageText
  1775. !
  1776. messageText: aString
  1777. messageText := aString
  1778. !
  1779. context
  1780. <return self.context>
  1781. !
  1782. jsStack
  1783. <return self.stack>
  1784. ! !
  1785. !Error methodsFor: 'signaling'!
  1786. signal
  1787. <self.context = smalltalk.getThisContext(); self.smalltalkError = true; throw(self)>
  1788. ! !
  1789. !Error methodsFor: 'testing'!
  1790. isSmalltalkError
  1791. <return self.smalltalkError === true>
  1792. ! !
  1793. !Error class methodsFor: 'instance creation'!
  1794. signal: aString
  1795. ^self new
  1796. messageText: aString;
  1797. signal
  1798. ! !
  1799. Object subclass: #MethodContext
  1800. instanceVariableNames: ''
  1801. category: 'Kernel'!
  1802. !MethodContext methodsFor: 'accessing'!
  1803. receiver
  1804. <return self.receiver>
  1805. !
  1806. selector
  1807. <return smalltalk.convertSelector(self.selector)>
  1808. !
  1809. home
  1810. <return self.homeContext>
  1811. !
  1812. temps
  1813. <return self.temps>
  1814. !
  1815. printString
  1816. ^super printString, '(', self asString, ')'
  1817. !
  1818. asString
  1819. ^self receiver class printString, ' >> ', self selector
  1820. ! !
  1821. Object subclass: #Association
  1822. instanceVariableNames: 'key value'
  1823. category: 'Kernel'!
  1824. !Association methodsFor: 'accessing'!
  1825. key: aKey
  1826. key := aKey
  1827. !
  1828. key
  1829. ^key
  1830. !
  1831. value: aValue
  1832. value := aValue
  1833. !
  1834. value
  1835. ^value
  1836. ! !
  1837. !Association methodsFor: 'comparing'!
  1838. = anAssociation
  1839. ^self class = anAssociation class and: [
  1840. self key = anAssociation key and: [
  1841. self value = anAssociation value]]
  1842. !
  1843. storeOn: aStream
  1844. "Store in the format (key->value)"
  1845. "aStream nextPutAll: '('."
  1846. key storeOn: aStream.
  1847. aStream nextPutAll: '->'.
  1848. value storeOn: aStream.
  1849. "aStream nextPutAll: ')'"
  1850. ! !
  1851. !Association class methodsFor: 'instance creation'!
  1852. key: aKey value: aValue
  1853. ^self new
  1854. key: aKey;
  1855. value: aValue;
  1856. yourself
  1857. ! !
  1858. Collection subclass: #Dictionary
  1859. instanceVariableNames: 'keys'
  1860. category: 'Kernel'!
  1861. !Dictionary methodsFor: 'accessing'!
  1862. size
  1863. ^keys size
  1864. !
  1865. associations
  1866. | associations |
  1867. associations := #().
  1868. keys do: [:each |
  1869. associations add: (Association key: each value: (self at: each))].
  1870. ^associations
  1871. !
  1872. keys
  1873. ^keys copy
  1874. !
  1875. values
  1876. ^keys collect: [:each | self at: each]
  1877. !
  1878. at: aKey put: aValue
  1879. (keys includes: aKey) ifFalse: [keys add: aKey].
  1880. ^self basicAt: aKey put: aValue
  1881. !
  1882. at: aKey ifAbsent: aBlock
  1883. ^(self keys includes: aKey)
  1884. ifTrue: [self basicAt: aKey]
  1885. ifFalse: aBlock
  1886. !
  1887. at: aKey ifAbsentPut: aBlock
  1888. ^self at: aKey ifAbsent: [
  1889. self at: aKey put: aBlock value]
  1890. !
  1891. at: aKey ifPresent: aBlock
  1892. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  1893. !
  1894. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  1895. ^(self basicAt: aKey)
  1896. ifNil: anotherBlock
  1897. ifNotNil: [aBlock value: (self at: aKey)]
  1898. !
  1899. at: aKey
  1900. ^self at: aKey ifAbsent: [self errorNotFound]
  1901. ! !
  1902. !Dictionary methodsFor: 'adding/removing'!
  1903. add: anAssociation
  1904. self at: anAssociation key put: anAssociation value
  1905. !
  1906. addAll: aDictionary
  1907. super addAll: aDictionary associations.
  1908. ^aDictionary
  1909. !
  1910. remove: aKey
  1911. self removeKey: aKey
  1912. !
  1913. removeKey: aKey
  1914. keys remove: aKey
  1915. ! !
  1916. !Dictionary methodsFor: 'comparing'!
  1917. = aDictionary
  1918. self class = aDictionary class ifFalse: [^false].
  1919. self size = aDictionary size ifFalse: [^false].
  1920. ^self associations = aDictionary associations
  1921. ! !
  1922. !Dictionary methodsFor: 'converting'!
  1923. asJSONObject
  1924. | object |
  1925. object := Object new.
  1926. self keysAndValuesDo: [:key :value |
  1927. object basicAt: key put: value asJSONObject].
  1928. ^object
  1929. ! !
  1930. !Dictionary methodsFor: 'copying'!
  1931. shallowCopy
  1932. | copy |
  1933. copy := self class new.
  1934. self associationsDo: [:each |
  1935. copy at: each key put: each value].
  1936. ^copy
  1937. !
  1938. , aCollection
  1939. self shouldNotImplement
  1940. !
  1941. copyFrom: anIndex to: anotherIndex
  1942. self shouldNotImplement
  1943. ! !
  1944. !Dictionary methodsFor: 'enumerating'!
  1945. associationsDo: aBlock
  1946. self associations do: aBlock
  1947. !
  1948. keysAndValuesDo: aBlock
  1949. self associationsDo: [:each |
  1950. aBlock value: each key value: each value]
  1951. !
  1952. do: aBlock
  1953. self values do: aBlock
  1954. !
  1955. select: aBlock
  1956. | newDict |
  1957. newDict := self class new.
  1958. self keysAndValuesDo: [:key :value |
  1959. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1960. ^newDict
  1961. !
  1962. collect: aBlock
  1963. | newDict |
  1964. newDict := self class new.
  1965. self keysAndValuesDo: [:key :value |
  1966. newDict at: key put: (aBlock value: value)].
  1967. ^newDict
  1968. !
  1969. detect: aBlock ifNone: anotherBlock
  1970. ^self values detect: aBlock ifNone: anotherBlock
  1971. !
  1972. includes: anObject
  1973. ^self values includes: anObject
  1974. ! !
  1975. !Dictionary methodsFor: 'initialization'!
  1976. initialize
  1977. super initialize.
  1978. keys := #()
  1979. ! !
  1980. !Dictionary methodsFor: 'printing'!
  1981. printString
  1982. ^ String streamContents: [:aStream|
  1983. aStream
  1984. nextPutAll: super printString;
  1985. nextPutAll: '('.
  1986. self associations
  1987. do: [:anAssociation|
  1988. aStream
  1989. nextPutAll: anAssociation key printString;
  1990. nextPutAll: ' -> ';
  1991. nextPutAll: anAssociation value printString]
  1992. separatedBy: [aStream nextPutAll: ' , '].
  1993. aStream nextPutAll: ')'.
  1994. ]
  1995. !
  1996. storeOn: aStream
  1997. aStream nextPutAll: '#{'.
  1998. self associations
  1999. do: [:each | each storeOn: aStream]
  2000. separatedBy: [ aStream nextPutAll: '. '].
  2001. aStream nextPutAll: '}'
  2002. ! !
  2003. !Dictionary class methodsFor: 'instance creation'!
  2004. fromPairs: aCollection
  2005. | dict |
  2006. dict := self new.
  2007. aCollection do: [:each | dict add: each].
  2008. ^dict
  2009. ! !
  2010. Object subclass: #ClassBuilder
  2011. instanceVariableNames: ''
  2012. category: 'Kernel'!
  2013. !ClassBuilder methodsFor: 'class creation'!
  2014. superclass: aClass subclass: aString
  2015. ^self superclass: aClass subclass: aString instanceVariableNames: '' package: nil
  2016. !
  2017. class: aClass instanceVariableNames: aString
  2018. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  2019. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  2020. self setupClass: aClass
  2021. !
  2022. superclass: aClass subclass: aString instanceVariableNames: aString2 package: aString3
  2023. | newClass |
  2024. newClass := self addSubclassOf: aClass
  2025. named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2)
  2026. package: (aString3 ifNil: ['unclassified']).
  2027. self setupClass: newClass.
  2028. ^newClass
  2029. ! !
  2030. !ClassBuilder methodsFor: 'private'!
  2031. instanceVariableNamesFor: aString
  2032. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  2033. !
  2034. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  2035. <smalltalk.addClass(aString, aClass, aCollection);
  2036. return smalltalk[aString]>
  2037. !
  2038. setupClass: aClass
  2039. <smalltalk.init(aClass);>
  2040. !
  2041. addSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  2042. <smalltalk.addClass(aString, aClass, aCollection, packageName);
  2043. return smalltalk[aString]>
  2044. !
  2045. copyClass: aClass named: aString
  2046. | newClass |
  2047. newClass := self
  2048. addSubclassOf: aClass superclass
  2049. named: aString
  2050. instanceVariableNames: aClass instanceVariableNames
  2051. package: aClass package name.
  2052. self setupClass: newClass.
  2053. aClass methodDictionary values do: [:each |
  2054. newClass addCompiledMethod: (Compiler new load: each source forClass: newClass).
  2055. (newClass methodDictionary at: each selector) category: each category].
  2056. aClass class methodDictionary values do: [:each |
  2057. newClass class addCompiledMethod: (Compiler new load: each source forClass: newClass class).
  2058. (newClass class methodDictionary at: each selector) category: each category].
  2059. self setupClass: newClass.
  2060. ^newClass
  2061. ! !
  2062. Object subclass: #ClassCategoryReader
  2063. instanceVariableNames: 'class category chunkParser'
  2064. category: 'Kernel'!
  2065. !ClassCategoryReader methodsFor: 'accessing'!
  2066. class: aClass category: aString
  2067. class := aClass.
  2068. category := aString
  2069. ! !
  2070. !ClassCategoryReader methodsFor: 'fileIn'!
  2071. scanFrom: aChunkParser
  2072. | chunk |
  2073. [chunk := aChunkParser nextChunk.
  2074. chunk isEmpty] whileFalse: [
  2075. self compileMethod: chunk]
  2076. ! !
  2077. !ClassCategoryReader methodsFor: 'initialization'!
  2078. initialize
  2079. super initialize.
  2080. chunkParser := ChunkParser new.
  2081. ! !
  2082. !ClassCategoryReader methodsFor: 'private'!
  2083. compileMethod: aString
  2084. | method |
  2085. method := Compiler new load: aString forClass: class.
  2086. method category: category.
  2087. class addCompiledMethod: method
  2088. ! !
  2089. Object subclass: #Stream
  2090. instanceVariableNames: 'collection position streamSize'
  2091. category: 'Kernel'!
  2092. !Stream methodsFor: 'accessing'!
  2093. collection
  2094. ^collection
  2095. !
  2096. setCollection: aCollection
  2097. collection := aCollection
  2098. !
  2099. position
  2100. ^position ifNil: [position := 0]
  2101. !
  2102. position: anInteger
  2103. position := anInteger
  2104. !
  2105. streamSize
  2106. ^streamSize
  2107. !
  2108. setStreamSize: anInteger
  2109. streamSize := anInteger
  2110. !
  2111. contents
  2112. ^self collection
  2113. copyFrom: 1
  2114. to: self streamSize
  2115. !
  2116. size
  2117. ^self streamSize
  2118. ! !
  2119. !Stream methodsFor: 'actions'!
  2120. reset
  2121. self position: 0
  2122. !
  2123. close
  2124. !
  2125. flush
  2126. !
  2127. resetContents
  2128. self reset.
  2129. self setStreamSize: 0
  2130. ! !
  2131. !Stream methodsFor: 'enumerating'!
  2132. do: aBlock
  2133. [self atEnd] whileFalse: [aBlock value: self next]
  2134. ! !
  2135. !Stream methodsFor: 'positioning'!
  2136. setToEnd
  2137. self position: self size
  2138. !
  2139. skip: anInteger
  2140. self position: ((self position + anInteger) min: self size max: 0)
  2141. ! !
  2142. !Stream methodsFor: 'reading'!
  2143. next
  2144. self position: self position + 1.
  2145. ^collection at: self position
  2146. !
  2147. next: anInteger
  2148. | tempCollection |
  2149. tempCollection := self collection class new.
  2150. anInteger timesRepeat: [
  2151. self atEnd ifFalse: [
  2152. tempCollection add: self next]].
  2153. ^tempCollection
  2154. !
  2155. peek
  2156. ^self atEnd ifFalse: [
  2157. self collection at: self position + 1]
  2158. ! !
  2159. !Stream methodsFor: 'testing'!
  2160. atEnd
  2161. ^self position = self size
  2162. !
  2163. atStart
  2164. ^self position = 0
  2165. !
  2166. isEmpty
  2167. ^self size = 0
  2168. ! !
  2169. !Stream methodsFor: 'writing'!
  2170. nextPut: anObject
  2171. self position: self position + 1.
  2172. self collection at: self position put: anObject.
  2173. self setStreamSize: (self streamSize max: self position)
  2174. !
  2175. nextPutAll: aCollection
  2176. aCollection do: [:each |
  2177. self nextPut: each]
  2178. ! !
  2179. !Stream class methodsFor: 'instance creation'!
  2180. on: aCollection
  2181. ^self new
  2182. setCollection: aCollection;
  2183. setStreamSize: aCollection size;
  2184. yourself
  2185. ! !
  2186. Stream subclass: #StringStream
  2187. instanceVariableNames: ''
  2188. category: 'Kernel'!
  2189. !StringStream methodsFor: 'reading'!
  2190. next: anInteger
  2191. | tempCollection |
  2192. tempCollection := self collection class new.
  2193. anInteger timesRepeat: [
  2194. self atEnd ifFalse: [
  2195. tempCollection := tempCollection, self next]].
  2196. ^tempCollection
  2197. ! !
  2198. !StringStream methodsFor: 'writing'!
  2199. nextPut: aString
  2200. self nextPutAll: aString
  2201. !
  2202. nextPutAll: aString
  2203. self setCollection:
  2204. (self collection copyFrom: 1 to: self position),
  2205. aString,
  2206. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  2207. self position: self position + aString size.
  2208. self setStreamSize: (self streamSize max: self position)
  2209. !
  2210. cr
  2211. ^self nextPutAll: String cr
  2212. !
  2213. crlf
  2214. ^self nextPutAll: String crlf
  2215. !
  2216. lf
  2217. ^self nextPutAll: String lf
  2218. !
  2219. space
  2220. self nextPut: ' '
  2221. ! !
  2222. Object subclass: #ClassCommentReader
  2223. instanceVariableNames: 'class chunkParser'
  2224. category: 'Kernel'!
  2225. !ClassCommentReader methodsFor: 'accessing'!
  2226. class: aClass
  2227. class := aClass
  2228. ! !
  2229. !ClassCommentReader methodsFor: 'fileIn'!
  2230. scanFrom: aChunkParser
  2231. | chunk |
  2232. chunk := aChunkParser nextChunk.
  2233. chunk isEmpty ifFalse: [
  2234. self setComment: chunk].
  2235. ! !
  2236. !ClassCommentReader methodsFor: 'initialization'!
  2237. initialize
  2238. super initialize.
  2239. chunkParser := ChunkParser new.
  2240. ! !
  2241. !ClassCommentReader methodsFor: 'private'!
  2242. setComment: aString
  2243. class comment: aString
  2244. ! !
  2245. Object subclass: #Random
  2246. instanceVariableNames: ''
  2247. category: 'Kernel'!
  2248. !Random methodsFor: 'accessing'!
  2249. next
  2250. <return Math.random()>
  2251. !
  2252. next: anInteger
  2253. ^(1 to: anInteger) collect: [:each | self next]
  2254. ! !
  2255. Object subclass: #Point
  2256. instanceVariableNames: 'x y'
  2257. category: 'Kernel'!
  2258. !Point methodsFor: 'accessing'!
  2259. x
  2260. ^x
  2261. !
  2262. y
  2263. ^y
  2264. !
  2265. y: aNumber
  2266. y := aNumber
  2267. !
  2268. x: aNumber
  2269. x := aNumber
  2270. ! !
  2271. !Point methodsFor: 'arithmetic'!
  2272. * aPoint
  2273. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  2274. !
  2275. + aPoint
  2276. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  2277. !
  2278. - aPoint
  2279. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  2280. !
  2281. / aPoint
  2282. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  2283. !
  2284. = aPoint
  2285. ^aPoint class = self class and: [
  2286. (aPoint x = self x) & (aPoint y = self y)]
  2287. ! !
  2288. !Point methodsFor: 'converting'!
  2289. asPoint
  2290. ^self
  2291. ! !
  2292. !Point class methodsFor: 'instance creation'!
  2293. x: aNumber y: anotherNumber
  2294. ^self new
  2295. x: aNumber;
  2296. y: anotherNumber;
  2297. yourself
  2298. ! !
  2299. Object subclass: #Message
  2300. instanceVariableNames: 'selector arguments'
  2301. category: 'Kernel'!
  2302. !Message methodsFor: 'accessing'!
  2303. selector
  2304. ^selector
  2305. !
  2306. selector: aString
  2307. selector := aString
  2308. !
  2309. arguments: anArray
  2310. arguments := anArray
  2311. !
  2312. arguments
  2313. ^arguments
  2314. ! !
  2315. !Message methodsFor: 'printing'!
  2316. printString
  2317. ^ String streamContents: [:aStream|
  2318. aStream
  2319. nextPutAll: super printString;
  2320. nextPutAll: '(';
  2321. nextPutAll: selector;
  2322. nextPutAll: ')' ]
  2323. ! !
  2324. !Message class methodsFor: 'instance creation'!
  2325. selector: aString arguments: anArray
  2326. ^self new
  2327. selector: aString;
  2328. arguments: anArray;
  2329. yourself
  2330. ! !
  2331. Error subclass: #MessageNotUnderstood
  2332. instanceVariableNames: 'message receiver'
  2333. category: 'Kernel'!
  2334. !MessageNotUnderstood methodsFor: 'accessing'!
  2335. message
  2336. ^message
  2337. !
  2338. message: aMessage
  2339. message := aMessage
  2340. !
  2341. receiver
  2342. ^receiver
  2343. !
  2344. receiver: anObject
  2345. receiver := anObject
  2346. !
  2347. messageText
  2348. ^self receiver asString, ' does not understand #', self message selector
  2349. ! !
  2350. Object subclass: #ErrorHandler
  2351. instanceVariableNames: ''
  2352. category: 'Kernel'!
  2353. !ErrorHandler methodsFor: 'error handling'!
  2354. handleError: anError
  2355. anError context ifNotNil: [self logErrorContext: anError context].
  2356. self logError: anError
  2357. ! !
  2358. !ErrorHandler methodsFor: 'private'!
  2359. logContext: aContext
  2360. aContext home ifNotNil: [
  2361. self logContext: aContext home].
  2362. self log: aContext receiver asString, '>>', aContext selector
  2363. !
  2364. logErrorContext: aContext
  2365. aContext ifNotNil: [
  2366. aContext home ifNotNil: [
  2367. self logContext: aContext home]]
  2368. !
  2369. logError: anError
  2370. self log: anError messageText
  2371. !
  2372. log: aString
  2373. console log: aString
  2374. ! !
  2375. ErrorHandler class instanceVariableNames: 'current'!
  2376. !ErrorHandler class methodsFor: 'accessing'!
  2377. current
  2378. ^current ifNil: [current := self new]
  2379. !
  2380. setCurrent: anHandler
  2381. current := anHandler
  2382. ! !
  2383. !ErrorHandler class methodsFor: 'initialization'!
  2384. initialize
  2385. self register
  2386. !
  2387. register
  2388. ErrorHandler setCurrent: self new
  2389. ! !
  2390. Object subclass: #JSObjectProxy
  2391. instanceVariableNames: 'jsObject'
  2392. category: 'Kernel'!
  2393. !JSObjectProxy methodsFor: 'accessing'!
  2394. jsObject: aJSObject
  2395. jsObject := aJSObject
  2396. !
  2397. jsObject
  2398. ^jsObject
  2399. !
  2400. at: aString
  2401. <return self['@jsObject'][aString]>
  2402. !
  2403. at: aString put: anObject
  2404. <self['@jsObject'][aString] = anObject>
  2405. ! !
  2406. !JSObjectProxy methodsFor: 'proxy'!
  2407. printString
  2408. ^self jsObject toString
  2409. !
  2410. inspectOn: anInspector
  2411. | variables |
  2412. variables := Dictionary new.
  2413. variables at: '#self' put: self jsObject.
  2414. anInspector setLabel: self printString.
  2415. <for(var i in self['@jsObject']) {
  2416. variables._at_put_(i, self['@jsObject'][i]);
  2417. }>.
  2418. anInspector setVariables: variables
  2419. !
  2420. doesNotUnderstand: aMessage
  2421. | obj selector jsSelector arguments |
  2422. obj := self jsObject.
  2423. selector := aMessage selector.
  2424. jsSelector := selector asJavaScriptSelector.
  2425. arguments := aMessage arguments.
  2426. <if(obj[jsSelector] !!= undefined) {return smalltalk.send(obj, jsSelector, arguments)}>.
  2427. super doesNotUnderstand: aMessage
  2428. ! !
  2429. !JSObjectProxy class methodsFor: 'instance creation'!
  2430. on: aJSObject
  2431. ^self new
  2432. jsObject: aJSObject;
  2433. yourself
  2434. ! !
  2435. Collection subclass: #Set
  2436. instanceVariableNames: 'elements'
  2437. category: 'Kernel'!
  2438. !Set methodsFor: 'accessing'!
  2439. size
  2440. ^elements size
  2441. ! !
  2442. !Set methodsFor: 'adding/removing'!
  2443. add: anObject
  2444. <
  2445. var found;
  2446. for(var i in self['@elements']) {
  2447. if(anObject == self['@elements'][i]) {
  2448. found = true;
  2449. break;
  2450. }
  2451. }
  2452. if(!!found) {self['@elements'].push(anObject)}
  2453. >
  2454. !
  2455. remove: anObject
  2456. elements remove: anObject
  2457. ! !
  2458. !Set methodsFor: 'comparing'!
  2459. = aCollection
  2460. ^self class = aCollection class and: [
  2461. elements = aCollection asArray]
  2462. ! !
  2463. !Set methodsFor: 'converting'!
  2464. asArray
  2465. ^elements copy
  2466. ! !
  2467. !Set methodsFor: 'enumerating'!
  2468. detect: aBlock ifNone: anotherBlock
  2469. ^elements detect: aBlock ifNone: anotherBlock
  2470. !
  2471. do: aBlock
  2472. elements do: aBlock
  2473. ! !
  2474. !Set methodsFor: 'initialization'!
  2475. initialize
  2476. super initialize.
  2477. elements := #()
  2478. ! !
  2479. !Set methodsFor: 'testing'!
  2480. includes: anObject
  2481. ^elements includes: anObject
  2482. ! !
  2483. Object subclass: #Transcript
  2484. instanceVariableNames: 'textarea'
  2485. category: 'Kernel'!
  2486. Transcript class instanceVariableNames: 'current'!
  2487. !Transcript class methodsFor: 'instance creation'!
  2488. open
  2489. self current open
  2490. !
  2491. new
  2492. self shouldNotImplement
  2493. !
  2494. current
  2495. ^current
  2496. !
  2497. register: aTranscript
  2498. current := aTranscript
  2499. ! !
  2500. !Transcript class methodsFor: 'printing'!
  2501. show: anObject
  2502. self current show: anObject
  2503. !
  2504. cr
  2505. self current show: String cr
  2506. !
  2507. clear
  2508. self current clear
  2509. ! !
  2510. Object subclass: #ConsoleTranscript
  2511. instanceVariableNames: 'textarea'
  2512. category: 'Kernel'!
  2513. !ConsoleTranscript methodsFor: 'actions'!
  2514. open
  2515. ! !
  2516. !ConsoleTranscript methodsFor: 'printing'!
  2517. clear
  2518. "no op"
  2519. !
  2520. cr
  2521. "no op"
  2522. !
  2523. show: anObject
  2524. | string |
  2525. string := anObject asString.
  2526. <console.log(String(string))>
  2527. ! !
  2528. !ConsoleTranscript class methodsFor: 'initialization'!
  2529. initialize
  2530. Transcript register: self new
  2531. ! !
  2532. Dictionary subclass: #Dictionary2
  2533. instanceVariableNames: 'keys'
  2534. category: 'Kernel'!
  2535. !Dictionary2 methodsFor: 'accessing'!
  2536. size
  2537. ^keys size
  2538. !
  2539. associations
  2540. | associations |
  2541. associations := #().
  2542. keys do: [:each |
  2543. associations add: (Association key: each value: (self at: each))].
  2544. ^associations
  2545. !
  2546. keys
  2547. ^keys copy
  2548. !
  2549. values
  2550. ^keys collect: [:each | self at: each]
  2551. !
  2552. at: aKey put: aValue
  2553. (keys includes: aKey) ifFalse: [keys add: aKey].
  2554. ^self basicAt: aKey put: aValue
  2555. !
  2556. at: aKey ifAbsent: aBlock
  2557. ^(self keys includes: aKey)
  2558. ifTrue: [self basicAt: aKey]
  2559. ifFalse: aBlock
  2560. !
  2561. at: aKey ifAbsentPut: aBlock
  2562. ^self at: aKey ifAbsent: [
  2563. self at: aKey put: aBlock value]
  2564. !
  2565. at: aKey ifPresent: aBlock
  2566. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  2567. !
  2568. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  2569. ^(self basicAt: aKey)
  2570. ifNil: anotherBlock
  2571. ifNotNil: [aBlock value: (self at: aKey)]
  2572. !
  2573. at: aKey
  2574. ^self at: aKey ifAbsent: [self errorNotFound]
  2575. ! !
  2576. !Dictionary2 methodsFor: 'adding/removing'!
  2577. add: anAssociation
  2578. self at: anAssociation key put: anAssociation value
  2579. !
  2580. addAll: aDictionary
  2581. super addAll: aDictionary associations.
  2582. ^aDictionary
  2583. !
  2584. remove: aKey
  2585. self removeKey: aKey
  2586. !
  2587. removeKey: aKey
  2588. keys remove: aKey
  2589. ! !
  2590. !Dictionary2 methodsFor: 'comparing'!
  2591. = aDictionary
  2592. self class = aDictionary class ifFalse: [^false].
  2593. self size = aDictionary size ifFalse: [^false].
  2594. ^self associations = aDictionary associations
  2595. ! !
  2596. !Dictionary2 methodsFor: 'converting'!
  2597. asJSONObject
  2598. | object |
  2599. object := Object new.
  2600. self keysAndValuesDo: [:key :value |
  2601. object basicAt: key put: value asJSONObject].
  2602. ^object
  2603. ! !
  2604. !Dictionary2 methodsFor: 'copying'!
  2605. shallowCopy
  2606. | copy |
  2607. copy := self class new.
  2608. self associationsDo: [:each |
  2609. copy at: each key put: each value].
  2610. ^copy
  2611. !
  2612. , aCollection
  2613. self shouldNotImplement
  2614. !
  2615. copyFrom: anIndex to: anotherIndex
  2616. self shouldNotImplement
  2617. ! !
  2618. !Dictionary2 methodsFor: 'enumerating'!
  2619. associationsDo: aBlock
  2620. self associations do: aBlock
  2621. !
  2622. keysAndValuesDo: aBlock
  2623. self associationsDo: [:each |
  2624. aBlock value: each key value: each value]
  2625. !
  2626. do: aBlock
  2627. self values do: aBlock
  2628. !
  2629. select: aBlock
  2630. | newDict |
  2631. newDict := self class new.
  2632. self keysAndValuesDo: [:key :value |
  2633. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  2634. ^newDict
  2635. !
  2636. collect: aBlock
  2637. | newDict |
  2638. newDict := self class new.
  2639. self keysAndValuesDo: [:key :value |
  2640. newDict at: key put: (aBlock value: value)].
  2641. ^newDict
  2642. !
  2643. detect: aBlock ifNone: anotherBlock
  2644. ^self values detect: aBlock ifNone: anotherBlock
  2645. !
  2646. includes: anObject
  2647. ^self values includes: anObject
  2648. ! !
  2649. !Dictionary2 methodsFor: 'initialization'!
  2650. initialize
  2651. super initialize.
  2652. keys := #()
  2653. ! !
  2654. !Dictionary2 methodsFor: 'printing'!
  2655. printString
  2656. ^ String streamContents: [:aStream|
  2657. aStream
  2658. nextPutAll: super printString;
  2659. nextPutAll: '('.
  2660. self associations
  2661. do: [:anAssociation|
  2662. aStream
  2663. nextPutAll: anAssociation key printString;
  2664. nextPutAll: ' -> ';
  2665. nextPutAll: anAssociation value printString]
  2666. separatedBy: [aStream nextPutAll: ' , '].
  2667. aStream nextPutAll: ')'.
  2668. ]
  2669. !
  2670. storeOn: aStream
  2671. aStream nextPutAll: '#{'.
  2672. self associations
  2673. do: [:each | each storeOn: aStream]
  2674. separatedBy: [ aStream nextPutAll: '. '].
  2675. aStream nextPutAll: '}'
  2676. ! !