Kernel.st 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  1. nil subclass: #Object
  2. instanceVariableNames: ''
  3. category: 'Kernel'!
  4. !Object methodsFor: 'accessing'!
  5. yourself
  6. ^self
  7. !
  8. class
  9. <return self.klass>
  10. !
  11. size
  12. self error: 'Object not indexable'
  13. !
  14. instVarAt: aString
  15. <return self['@'+aString]>
  16. !
  17. instVarAt: aString put: anObject
  18. <self['@' + aString] = anObject>
  19. !
  20. basicAt: aString
  21. <return self[aString]>
  22. !
  23. basicAt: aString put: anObject
  24. <return self[aString] = anObject>
  25. !
  26. basicDelete: aString
  27. <delete self[aString]>
  28. ! !
  29. !Object methodsFor: 'comparing'!
  30. = anObject
  31. <return self == anObject>
  32. !
  33. ~= anObject
  34. ^(self = anObject) == false
  35. ! !
  36. !Object methodsFor: 'converting'!
  37. -> anObject
  38. ^Association key: self value: anObject
  39. !
  40. asString
  41. ^self printString
  42. !
  43. asJavascript
  44. ^self asString
  45. !
  46. asJSON
  47. <return JSON.stringify(self._asJSONObject())>
  48. !
  49. asJSONObject
  50. | object |
  51. object := Object new.
  52. self class instanceVariableNames do: [:each |
  53. object basicAt: each put: (self instVarAt: each) asJSONObject].
  54. ^object
  55. ! !
  56. !Object methodsFor: 'copying'!
  57. copy
  58. ^self shallowCopy postCopy
  59. !
  60. shallowCopy
  61. <
  62. var copy = self.klass._new();
  63. for(var i in self) {
  64. if(/^@.+/.test(i)) {
  65. copy[i] = self[i];
  66. }
  67. }
  68. return copy;
  69. >
  70. !
  71. deepCopy
  72. <
  73. var copy = self.klass._new();
  74. for(var i in self) {
  75. if(/^@.+/.test(i)) {
  76. copy[i] = self[i]._deepCopy();
  77. }
  78. }
  79. return copy;
  80. >
  81. !
  82. postCopy
  83. ! !
  84. !Object methodsFor: 'error handling'!
  85. error: aString
  86. Error signal: aString
  87. !
  88. subclassResponsibility
  89. self error: 'This method is a responsibility of a subclass'
  90. !
  91. shouldNotImplement
  92. self error: 'This method should not be implemented in ', self class name
  93. !
  94. try: aBlock catch: anotherBlock
  95. <try{aBlock()} catch(e) {anotherBlock(e)}>
  96. !
  97. doesNotUnderstand: aMessage
  98. MessageNotUnderstood new
  99. receiver: self;
  100. message: aMessage;
  101. signal
  102. !
  103. halt
  104. self error: 'Halt encountered'
  105. ! !
  106. !Object methodsFor: 'initialization'!
  107. initialize
  108. ! !
  109. !Object methodsFor: 'message handling'!
  110. perform: aSymbol
  111. ^self perform: aSymbol withArguments: #()
  112. !
  113. perform: aSymbol withArguments: aCollection
  114. ^self basicPerform: aSymbol asSelector withArguments: aCollection
  115. !
  116. basicPerform: aSymbol
  117. ^self basicPerform: aSymbol withArguments: #()
  118. !
  119. basicPerform: aSymbol withArguments: aCollection
  120. <return self[aSymbol].apply(self, aCollection);>
  121. ! !
  122. !Object methodsFor: 'printing'!
  123. printString
  124. ^'a ', self class name
  125. !
  126. printNl
  127. <console.log(self)>
  128. ! !
  129. !Object methodsFor: 'testing'!
  130. isKindOf: aClass
  131. ^(self isMemberOf: aClass)
  132. ifTrue: [true]
  133. ifFalse: [self class inheritsFrom: aClass]
  134. !
  135. isMemberOf: aClass
  136. ^self class = aClass
  137. !
  138. ifNil: aBlock
  139. ^self
  140. !
  141. ifNil: aBlock ifNotNil: anotherBlock
  142. ^anotherBlock value
  143. !
  144. ifNotNil: aBlock
  145. ^aBlock value
  146. !
  147. ifNotNil: aBlock ifNil: anotherBlock
  148. ^aBlock value
  149. !
  150. isNil
  151. ^false
  152. !
  153. notNil
  154. ^self isNil not
  155. !
  156. isClass
  157. ^false
  158. !
  159. isMetaclass
  160. ^false
  161. !
  162. isNumber
  163. ^false
  164. !
  165. isString
  166. ^false
  167. !
  168. isParseFailure
  169. ^false
  170. ! !
  171. !Object class methodsFor: 'initialization'!
  172. initialize
  173. "no op"
  174. ! !
  175. Object subclass: #Smalltalk
  176. instanceVariableNames: ''
  177. category: 'Kernel'!
  178. !Smalltalk methodsFor: 'accessing'!
  179. classes
  180. <return self.classes()>
  181. !
  182. readJSON: anObject
  183. <return self.readJSObject(anObject)>
  184. !
  185. at: aString
  186. <return self[aString]>
  187. !
  188. removeClass: aClass
  189. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  190. aClass methodDictionary values do: [:each |
  191. aClass removeCompiledMethod: each].
  192. aClass class methodDictionary values do: [:each |
  193. aClass class removeCompiledMethod: each].
  194. self basicDelete: aClass name
  195. ! !
  196. Smalltalk class instanceVariableNames: 'current'!
  197. !Smalltalk class methodsFor: 'accessing'!
  198. current
  199. <return smalltalk>
  200. ! !
  201. Object subclass: #Behavior
  202. instanceVariableNames: ''
  203. category: 'Kernel'!
  204. !Behavior methodsFor: 'accessing'!
  205. name
  206. <return self.className || nil>
  207. !
  208. superclass
  209. <return self.superclass || nil>
  210. !
  211. subclasses
  212. <return smalltalk.subclasses(self)>
  213. !
  214. allSubclasses
  215. | result |
  216. result := self subclasses.
  217. self subclasses do: [:each |
  218. result addAll: each allSubclasses].
  219. ^result
  220. !
  221. withAllSubclasses
  222. ^(Array with: self) addAll: self allSubclasses; yourself
  223. !
  224. prototype
  225. <return self.fn.prototype>
  226. !
  227. methodDictionary
  228. <var dict = smalltalk.Dictionary._new();
  229. var methods = self.fn.prototype.methods;
  230. for(var i in methods) {
  231. if(methods[i].selector) {
  232. dict._at_put_(methods[i].selector, methods[i]);
  233. }
  234. };
  235. return dict>
  236. !
  237. methodsFor: aString
  238. ^ClassCategoryReader new
  239. class: self category: aString;
  240. yourself
  241. !
  242. addCompiledMethod: aMethod
  243. <smalltalk.addMethod(aMethod.selector._asSelector(), aMethod, self)>
  244. !
  245. instanceVariableNames
  246. <return self.iVarNames>
  247. !
  248. comment
  249. ^(self basicAt: 'comment') ifNil: ['']
  250. !
  251. comment: aString
  252. self basicAt: 'comment' put: aString
  253. !
  254. commentStamp
  255. ^ClassCommentReader new
  256. class: self;
  257. yourself
  258. !
  259. removeCompiledMethod: aMethod
  260. <delete self.fn.prototype[aMethod.selector._asSelector()];
  261. delete self.fn.prototype.methods[aMethod.selector];
  262. smalltalk.init(self);>
  263. !
  264. protocols
  265. | protocols |
  266. protocols := Array new.
  267. self methodDictionary do: [:each |
  268. (protocols includes: each category) ifFalse: [
  269. protocols add: each category]].
  270. ^protocols sort
  271. !
  272. protocolsDo: aBlock
  273. "Execute aBlock for each method category with
  274. its collection of methods in the sort order of category name."
  275. | methodsByCategory |
  276. methodsByCategory := Dictionary new.
  277. self methodDictionary values do: [:m |
  278. (methodsByCategory at: m category ifAbsentPut: [Array new])
  279. add: m].
  280. self protocols do: [:category |
  281. aBlock value: category value: (methodsByCategory at: category)]
  282. !
  283. allInstanceVariableNames
  284. | result |
  285. result := self instanceVariableNames copy.
  286. self superclass ifNotNil: [
  287. result addAll: self superclass allInstanceVariableNames].
  288. ^result
  289. !
  290. methodAt: aString
  291. <return smalltalk.methods(self)[aString]>
  292. !
  293. methodsFor: aString stamp: aStamp
  294. "Added for compatibility, right now ignores stamp."
  295. ^self methodsFor: aString
  296. !
  297. commentStamp: aStamp prior: prior
  298. "Stamp and prior ignored right now."
  299. ^self commentStamp
  300. ! !
  301. !Behavior methodsFor: 'instance creation'!
  302. new
  303. ^self basicNew initialize
  304. !
  305. basicNew
  306. <return new self.fn()>
  307. !
  308. inheritsFrom: aClass
  309. ^aClass allSubclasses includes: self
  310. ! !
  311. Behavior subclass: #Class
  312. instanceVariableNames: ''
  313. category: 'Kernel'!
  314. !Class methodsFor: 'accessing'!
  315. category
  316. <return self.category>
  317. !
  318. category: aString
  319. <self.category = aString>
  320. !
  321. rename: aString
  322. <
  323. smalltalk[aString] = self;
  324. delete smalltalk[self.className];
  325. self.className = aString;
  326. >
  327. ! !
  328. !Class methodsFor: 'class creation'!
  329. subclass: aString instanceVariableNames: anotherString
  330. ^self subclass: aString instanceVariableNames: anotherString category: nil
  331. !
  332. subclass: aString instanceVariableNames: aString2 category: aString3
  333. ^ClassBuilder new
  334. superclass: self subclass: aString instanceVariableNames: aString2 category: aString3
  335. !
  336. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  337. "Just ignore class variables and pools. Added for compatibility."
  338. ^self subclass: aString instanceVariableNames: aString2 category: aString3
  339. ! !
  340. !Class methodsFor: 'printing'!
  341. printString
  342. ^self name
  343. ! !
  344. !Class methodsFor: 'testing'!
  345. isClass
  346. ^true
  347. ! !
  348. Behavior subclass: #Metaclass
  349. instanceVariableNames: ''
  350. category: 'Kernel'!
  351. !Metaclass methodsFor: 'accessing'!
  352. instanceClass
  353. <return self.instanceClass>
  354. !
  355. instanceVariableNames: aCollection
  356. ClassBuilder new
  357. class: self instanceVariableNames: aCollection
  358. ! !
  359. !Metaclass methodsFor: 'printing'!
  360. printString
  361. ^self instanceClass name, ' class'
  362. ! !
  363. !Metaclass methodsFor: 'testing'!
  364. isMetaclass
  365. ^true
  366. ! !
  367. Object subclass: #CompiledMethod
  368. instanceVariableNames: ''
  369. category: 'Kernel'!
  370. !CompiledMethod methodsFor: 'accessing'!
  371. source
  372. ^(self basicAt: 'source') ifNil: ['']
  373. !
  374. source: aString
  375. self basicAt: 'source' put: aString
  376. !
  377. category
  378. ^(self basicAt: 'category') ifNil: ['']
  379. !
  380. category: aString
  381. self basicAt: 'category' put: aString
  382. !
  383. selector
  384. ^self basicAt: 'selector'
  385. !
  386. selector: aString
  387. self basicAt: 'selector' put: aString
  388. !
  389. fn
  390. ^self basicAt: 'fn'
  391. !
  392. fn: aBlock
  393. self basicAt: 'fn' put: aBlock
  394. !
  395. messageSends
  396. ^self basicAt: 'messageSends'
  397. !
  398. methodClass
  399. ^self basicAt: 'methodClass'
  400. !
  401. referencedClasses
  402. ^self basicAt: 'referencedClasses'
  403. ! !
  404. Object subclass: #Number
  405. instanceVariableNames: ''
  406. category: 'Kernel'!
  407. !Number methodsFor: 'arithmetic'!
  408. + aNumber
  409. <return self + aNumber>
  410. !
  411. - aNumber
  412. <return self - aNumber>
  413. !
  414. * aNumber
  415. <return self * aNumber>
  416. !
  417. / aNumber
  418. <return self / aNumber>
  419. !
  420. max: aNumber
  421. <return Math.max(self, aNumber);>
  422. !
  423. min: aNumber
  424. <return Math.min(self, aNumber);>
  425. !
  426. modulo: aNumber
  427. <return self % aNumber>
  428. ! !
  429. !Number methodsFor: 'comparing'!
  430. = aNumber
  431. <return Number(self) == aNumber>
  432. !
  433. > aNumber
  434. <return self >> aNumber>
  435. !
  436. < aNumber
  437. <return self < aNumber>
  438. !
  439. >= aNumber
  440. <return self >>= aNumber>
  441. !
  442. <= aNumber
  443. <return self <= aNumber>
  444. ! !
  445. !Number methodsFor: 'converting'!
  446. rounded
  447. <return Math.round(self);>
  448. !
  449. truncated
  450. <return Math.floor(self);>
  451. !
  452. to: aNumber
  453. | array first last count |
  454. first := self truncated.
  455. last := aNumber truncated + 1.
  456. count := 1.
  457. (first <= last) ifFalse: [self error: 'Wrong interval'].
  458. array := Array new.
  459. (last - first) timesRepeat: [
  460. array at: count put: first.
  461. count := count + 1.
  462. first := first + 1].
  463. ^array
  464. !
  465. asString
  466. ^self printString
  467. !
  468. asJavascript
  469. ^'(', self printString, ')'
  470. !
  471. atRandom
  472. ^(Random new next * self) truncated + 1
  473. !
  474. @ aNumber
  475. ^Point x: self y: aNumber
  476. !
  477. asPoint
  478. ^Point x: self y: self
  479. !
  480. asJSONObject
  481. ^self
  482. ! !
  483. !Number methodsFor: 'enumerating'!
  484. timesRepeat: aBlock
  485. | integer count |
  486. integer := self truncated.
  487. count := 1.
  488. [count > self] whileFalse: [
  489. aBlock value.
  490. count := count + 1]
  491. !
  492. to: aNumber do: aBlock
  493. ^(self to: aNumber) do: aBlock
  494. ! !
  495. !Number methodsFor: 'printing'!
  496. printString
  497. <return String(self)>
  498. ! !
  499. !Number methodsFor: 'testing'!
  500. isNumber
  501. ^true
  502. !
  503. even
  504. ^ 0 = (self modulo: 2)
  505. !
  506. odd
  507. ^ self even not
  508. ! !
  509. !Number methodsFor: 'timeouts/intervals'!
  510. clearInterval
  511. <clearInterval(Number(self))>
  512. !
  513. clearTimeout
  514. <clearTimeout(Number(self))>
  515. ! !
  516. !Number class methodsFor: 'instance creation'!
  517. pi
  518. <return Math.PI>
  519. ! !
  520. Object subclass: #BlockClosure
  521. instanceVariableNames: ''
  522. category: 'Kernel'!
  523. !BlockClosure methodsFor: 'accessing'!
  524. compiledSource
  525. <return self.toString()>
  526. ! !
  527. !BlockClosure methodsFor: 'controlling'!
  528. whileTrue: aBlock
  529. <while(self()) {aBlock()}>
  530. !
  531. whileFalse: aBlock
  532. <while(!!self()) {aBlock()}>
  533. ! !
  534. !BlockClosure methodsFor: 'error handling'!
  535. on: anErrorClass do: aBlock
  536. self try: self catch: [:error |
  537. (error isKindOf: anErrorClass)
  538. ifTrue: [aBlock value: error]
  539. ifFalse: [error signal]]
  540. ! !
  541. !BlockClosure methodsFor: 'evaluating'!
  542. value
  543. <return self();>
  544. !
  545. value: anArg
  546. <return self(anArg);>
  547. !
  548. value: firstArg value: secondArg
  549. <return self(firstArg, secondArg);>
  550. !
  551. value: firstArg value: secondArg value: thirdArg
  552. <return self(firstArg, secondArg, thirdArg);>
  553. !
  554. valueWithPossibleArguments: aCollection
  555. <return self.apply(null, aCollection);>
  556. ! !
  557. !BlockClosure methodsFor: 'printing'!
  558. printString
  559. ^ String streamContents: [:aStream|
  560. aStream
  561. nextPutAll: super printString;
  562. nextPutAll: '(';
  563. nextPutAll: self compiledSource;
  564. nextPutAll: ')';
  565. cr.
  566. ]
  567. ! !
  568. !BlockClosure methodsFor: 'timeout/interval'!
  569. valueWithTimeout: aNumber
  570. <return setTimeout(self, aNumber)>
  571. !
  572. valueWithInterval: aNumber
  573. <return setInterval(self, aNumber)>
  574. ! !
  575. Object subclass: #Boolean
  576. instanceVariableNames: ''
  577. category: 'Kernel'!
  578. !Boolean methodsFor: 'comparing'!
  579. = aBoolean
  580. <return Boolean(self == true) == aBoolean>
  581. !
  582. asJSONObject
  583. ^self
  584. ! !
  585. !Boolean methodsFor: 'controlling'!
  586. ifTrue: aBlock
  587. ^self ifTrue: aBlock ifFalse: []
  588. !
  589. ifFalse: aBlock
  590. ^self ifTrue: [] ifFalse: aBlock
  591. !
  592. ifFalse: aBlock ifTrue: anotherBlock
  593. ^self ifTrue: anotherBlock ifFalse: aBlock
  594. !
  595. ifTrue: aBlock ifFalse: anotherBlock
  596. <
  597. if(self == true) {
  598. return aBlock();
  599. } else {
  600. return anotherBlock();
  601. }
  602. >
  603. !
  604. and: aBlock
  605. ^self = true
  606. ifTrue: aBlock
  607. ifFalse: [false]
  608. !
  609. or: aBlock
  610. ^self = true
  611. ifTrue: [true]
  612. ifFalse: aBlock
  613. !
  614. not
  615. ^self = false
  616. !
  617. & aBoolean
  618. <
  619. if(self == true) {
  620. return aBoolean;
  621. } else {
  622. return false;
  623. }
  624. >
  625. !
  626. | aBoolean
  627. <
  628. if(self == true) {
  629. return true;
  630. } else {
  631. return aBoolean;
  632. }
  633. >
  634. ! !
  635. !Boolean methodsFor: 'copying'!
  636. shallowCopy
  637. ^self
  638. !
  639. deepCopy
  640. ^self
  641. ! !
  642. !Boolean methodsFor: 'printing'!
  643. printString
  644. <return self.toString()>
  645. ! !
  646. Object subclass: #Date
  647. instanceVariableNames: ''
  648. category: 'Kernel'!
  649. !Date commentStamp!
  650. The Date class is used to work with dates and times.!
  651. !Date methodsFor: 'accessing'!
  652. year
  653. <return self.getFullYear()>
  654. !
  655. month
  656. <return self.getMonth() + 1>
  657. !
  658. month: aNumber
  659. <self.setMonth(aNumber - 1)>
  660. !
  661. day
  662. ^self dayOfWeek
  663. !
  664. dayOfWeek
  665. <return self.getDay() + 1>
  666. !
  667. dayOfWeek: aNumber
  668. <return self.setDay(aNumber - 1)>
  669. !
  670. day: aNumber
  671. self day: aNumber
  672. !
  673. year: aNumber
  674. <self.setFullYear(aNumber)>
  675. !
  676. dayOfMonth
  677. <return self.getDate()>
  678. !
  679. dayOfMonth: aNumber
  680. <self.setDate(aNumber)>
  681. !
  682. time
  683. <return self.getTime()>
  684. !
  685. time: aNumber
  686. <self.setTime(aNumber)>
  687. !
  688. hours: aNumber
  689. <self.setHours(aNumber)>
  690. !
  691. minutes: aNumber
  692. <self.setMinutes(aNumber)>
  693. !
  694. seconds: aNumber
  695. <self.setSeconds(aNumber)>
  696. !
  697. milliseconds: aNumber
  698. <self.setMilliseconds(aNumber)>
  699. !
  700. hours
  701. <return self.getHours()>
  702. !
  703. minutes
  704. <return self.getMinutes()>
  705. !
  706. seconds
  707. <return self.getSeconds()>
  708. !
  709. milliseconds
  710. <return self.getMilliseconds()>
  711. ! !
  712. !Date methodsFor: 'arithmetic'!
  713. - aDate
  714. <return self - aDate>
  715. !
  716. + aDate
  717. <return self + aDate>
  718. ! !
  719. !Date methodsFor: 'comparing'!
  720. < aDate
  721. <return self < aDate>
  722. !
  723. > aDate
  724. <return self >> aDate>
  725. !
  726. <= aDate
  727. <self <= aDate>
  728. !
  729. >= aDate
  730. <self >>= aDate>
  731. ! !
  732. !Date methodsFor: 'converting'!
  733. asString
  734. <return self.toString()>
  735. !
  736. asMilliseconds
  737. ^self time
  738. !
  739. asDateString
  740. <return self.toDateString()>
  741. !
  742. asTimeString
  743. <return self.toTimeString()>
  744. !
  745. asLocaleString
  746. <return self.toLocaleString()>
  747. !
  748. asNumber
  749. ^self asMilliseconds
  750. !
  751. asJSONObject
  752. ^self
  753. ! !
  754. !Date methodsFor: 'printing'!
  755. printString
  756. ^self asString
  757. ! !
  758. !Date class methodsFor: 'instance creation'!
  759. new: anObject
  760. <return new Date(anObject)>
  761. !
  762. fromString: aString
  763. "Example: Date fromString('2011/04/15 00:00:00')"
  764. ^self new: aString
  765. !
  766. fromSeconds: aNumber
  767. ^self fromMilliseconds: aNumber * 1000
  768. !
  769. fromMilliseconds: aNumber
  770. ^self new: aNumber
  771. !
  772. today
  773. ^self new
  774. !
  775. now
  776. ^self today
  777. !
  778. millisecondsToRun: aBlock
  779. | t |
  780. t := Date now.
  781. aBlock value.
  782. ^Date now - t
  783. ! !
  784. Object subclass: #UndefinedObject
  785. instanceVariableNames: ''
  786. category: 'Kernel'!
  787. !UndefinedObject methodsFor: 'class creation'!
  788. subclass: aString instanceVariableNames: anotherString
  789. ^self subclass: aString instanceVariableNames: anotherString category: nil
  790. !
  791. subclass: aString instanceVariableNames: aString2 category: aString3
  792. ^ClassBuilder new
  793. superclass: self subclass: aString instanceVariableNames: aString2 category: aString3
  794. ! !
  795. !UndefinedObject methodsFor: 'copying'!
  796. shallowCopy
  797. ^self
  798. !
  799. deepCopy
  800. ^self
  801. ! !
  802. !UndefinedObject methodsFor: 'printing'!
  803. printString
  804. ^'nil'
  805. ! !
  806. !UndefinedObject methodsFor: 'testing'!
  807. ifNil: aBlock
  808. ^self ifNil: aBlock ifNotNil: []
  809. !
  810. ifNotNil: aBlock
  811. ^self
  812. !
  813. ifNil: aBlock ifNotNil: anotherBlock
  814. ^aBlock value
  815. !
  816. ifNotNil: aBlock ifNil: anotherBlock
  817. ^anotherBlock value
  818. !
  819. isNil
  820. ^true
  821. !
  822. notNil
  823. ^false
  824. ! !
  825. !UndefinedObject class methodsFor: 'instance creation'!
  826. new
  827. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  828. ! !
  829. Object subclass: #Collection
  830. instanceVariableNames: ''
  831. category: 'Kernel'!
  832. !Collection methodsFor: 'accessing'!
  833. size
  834. self subclassResponsibility
  835. !
  836. readStream
  837. ^self stream
  838. !
  839. writeStream
  840. ^self stream
  841. !
  842. stream
  843. ^self streamClass on: self
  844. !
  845. streamClass
  846. ^self class streamClass
  847. ! !
  848. !Collection methodsFor: 'adding/removing'!
  849. add: anObject
  850. self subclassResponsibility
  851. !
  852. addAll: aCollection
  853. aCollection do: [:each |
  854. self add: each].
  855. ^aCollection
  856. !
  857. remove: anObject
  858. self subclassResponsibility
  859. ! !
  860. !Collection methodsFor: 'converting'!
  861. asArray
  862. | array index |
  863. array := Array new.
  864. index := 0.
  865. self do: [:each |
  866. index := index + 1.
  867. array at: index put: each].
  868. ^array
  869. ! !
  870. !Collection methodsFor: 'copying'!
  871. , aCollection
  872. ^self copy
  873. addAll: aCollection;
  874. yourself
  875. !
  876. copyWith: anObject
  877. ^self copy add: anObject; yourself
  878. !
  879. copyWithAll: aCollection
  880. ^self copy addAll: aCollection; yourself
  881. ! !
  882. !Collection methodsFor: 'enumerating'!
  883. do: aBlock
  884. <for(var i=0;i<self.length;i++){aBlock(self[i]);}>
  885. !
  886. collect: aBlock
  887. | newCollection |
  888. newCollection := self class new.
  889. self do: [:each |
  890. newCollection add: (aBlock value: each)].
  891. ^newCollection
  892. !
  893. detect: aBlock
  894. ^self detect: aBlock ifNone: [self errorNotFound]
  895. !
  896. detect: aBlock ifNone: anotherBlock
  897. <
  898. for(var i = 0; i < self.length; i++)
  899. if(aBlock(self[i]))
  900. return self[i];
  901. return anotherBlock();
  902. >
  903. !
  904. do: aBlock separatedBy: anotherBlock
  905. | first |
  906. first := true.
  907. self do: [:each |
  908. first
  909. ifTrue: [first := false]
  910. ifFalse: [anotherBlock value].
  911. aBlock value: each]
  912. !
  913. inject: anObject into: aBlock
  914. | result |
  915. result := anObject.
  916. self do: [:each |
  917. result := aBlock value: result value: each].
  918. ^result
  919. !
  920. reject: aBlock
  921. ^self select: [:each | (aBlock value: each) = false]
  922. !
  923. select: aBlock
  924. | stream |
  925. stream := self class new writeStream.
  926. self do: [:each |
  927. (aBlock value: each) ifTrue: [
  928. stream nextPut: each]].
  929. ^stream contents
  930. ! !
  931. !Collection methodsFor: 'error handling'!
  932. errorNotFound
  933. self error: 'Object is not in the collection'
  934. ! !
  935. !Collection methodsFor: 'testing'!
  936. includes: anObject
  937. <
  938. var i = self.length;
  939. while (i--) {
  940. if (smalltalk.send(self[i], "__eq", [anObject])) {return true;}
  941. }
  942. return false
  943. >
  944. !
  945. notEmpty
  946. ^self isEmpty not
  947. !
  948. isEmpty
  949. ^self size = 0
  950. ! !
  951. !Collection class methodsFor: 'accessing'!
  952. streamClass
  953. ^Stream
  954. ! !
  955. !Collection class methodsFor: 'instance creation'!
  956. with: anObject
  957. ^self new
  958. add: anObject;
  959. yourself
  960. !
  961. with: anObject with: anotherObject
  962. ^self new
  963. add: anObject;
  964. add: anotherObject;
  965. yourself
  966. !
  967. with: firstObject with: secondObject with: thirdObject
  968. ^self new
  969. add: firstObject;
  970. add: secondObject;
  971. add: thirdObject;
  972. yourself
  973. !
  974. withAll: aCollection
  975. ^self new
  976. addAll: aCollection;
  977. yourself
  978. ! !
  979. Collection subclass: #SequenceableCollection
  980. instanceVariableNames: ''
  981. category: 'Kernel'!
  982. !SequenceableCollection methodsFor: 'accessing'!
  983. at: anIndex
  984. ^self at: anIndex ifAbsent: [
  985. self errorNotFound]
  986. !
  987. at: anIndex ifAbsent: aBlock
  988. self subclassResponsibility
  989. !
  990. at: anIndex put: anObject
  991. self subclassResponsibility
  992. !
  993. first
  994. ^self at: 1
  995. !
  996. fourth
  997. ^self at: 4
  998. !
  999. last
  1000. ^self at: self size
  1001. !
  1002. second
  1003. ^self at: 2
  1004. !
  1005. third
  1006. ^self at: 3
  1007. !
  1008. allButFirst
  1009. ^self copyFrom: 2 to: self size
  1010. !
  1011. allButLast
  1012. ^self copyFrom: 1 to: self size - 1
  1013. !
  1014. indexOf: anObject
  1015. ^self indexOf: anObject ifAbsent: [self errorNotFound]
  1016. !
  1017. indexOf: anObject ifAbsent: aBlock
  1018. <
  1019. for(var i=0;i<self.length;i++){
  1020. if(self[i].__eq(anObject)) {return i+1}
  1021. }
  1022. return aBlock();
  1023. >
  1024. ! !
  1025. !SequenceableCollection methodsFor: 'adding'!
  1026. removeLast
  1027. self remove: self last
  1028. !
  1029. addLast: anObject
  1030. self add: anObject
  1031. ! !
  1032. !SequenceableCollection methodsFor: 'copying'!
  1033. copyFrom: anIndex to: anotherIndex
  1034. self subclassResponsibility
  1035. ! !
  1036. !SequenceableCollection methodsFor: 'enumerating'!
  1037. withIndexDo: aBlock
  1038. <for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
  1039. ! !
  1040. SequenceableCollection subclass: #String
  1041. instanceVariableNames: ''
  1042. category: 'Kernel'!
  1043. !String methodsFor: 'accessing'!
  1044. size
  1045. <return self.length>
  1046. !
  1047. at: anIndex
  1048. <return self[anIndex - 1]>
  1049. !
  1050. at: anIndex put: anObject
  1051. self errorReadOnly
  1052. !
  1053. at: anIndex ifAbsent: aBlock
  1054. (self at: anIndex) ifNil: [aBlock]
  1055. !
  1056. escaped
  1057. <return escape(self)>
  1058. !
  1059. unescaped
  1060. <return unescape(self)>
  1061. !
  1062. asciiValue
  1063. <return self.charCodeAt(0);>
  1064. ! !
  1065. !String methodsFor: 'adding'!
  1066. add: anObject
  1067. self errorReadOnly
  1068. !
  1069. remove: anObject
  1070. self errorReadOnly
  1071. ! !
  1072. !String methodsFor: 'comparing'!
  1073. = aString
  1074. <return String(self) == aString>
  1075. !
  1076. > aString
  1077. <return String(self) >> aString>
  1078. !
  1079. < aString
  1080. <return String(self) < aString>
  1081. !
  1082. >= aString
  1083. <return String(self) >>= aString>
  1084. !
  1085. <= aString
  1086. <return String(self) <= aString>
  1087. ! !
  1088. !String methodsFor: 'converting'!
  1089. asSelector
  1090. "If you change this method, change smalltalk.convertSelector too (see js/boot.js file)"
  1091. | selector |
  1092. selector := '_', self.
  1093. selector := selector replace: ':' with: '_'.
  1094. selector := selector replace: '[+]' with: '_plus'.
  1095. selector := selector replace: '-' with: '_minus'.
  1096. selector := selector replace: '[*]' with: '_star'.
  1097. selector := selector replace: '[/]' with: '_slash'.
  1098. selector := selector replace: '>' with: '_gt'.
  1099. selector := selector replace: '<' with: '_lt'.
  1100. selector := selector replace: '=' with: '_eq'.
  1101. selector := selector replace: ',' with: '_comma'.
  1102. selector := selector replace: '[@]' with: '_at'.
  1103. ^selector
  1104. !
  1105. asJavascript
  1106. <
  1107. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1108. return "unescape(\"" + escape(self) + "\")";
  1109. else
  1110. return "\"" + self + "\"";
  1111. >
  1112. !
  1113. tokenize: aString
  1114. <return self.split(aString)>
  1115. !
  1116. asString
  1117. ^self
  1118. !
  1119. asNumber
  1120. <return Number(self)>
  1121. !
  1122. asParser
  1123. ^PPStringParser new string: self
  1124. !
  1125. asChoiceParser
  1126. ^PPChoiceParser withAll: (self asArray collect: [:each | each asParser])
  1127. !
  1128. asCharacterParser
  1129. ^PPCharacterParser new string: self
  1130. !
  1131. asJSONObject
  1132. ^self
  1133. !
  1134. asLowercase
  1135. <return self.toLowerCase()>
  1136. !
  1137. asUppercase
  1138. <return self.toUpperCase()>
  1139. ! !
  1140. !String methodsFor: 'copying'!
  1141. , aString
  1142. <return self + aString>
  1143. !
  1144. copyFrom: anIndex to: anotherIndex
  1145. <return self.substring(anIndex - 1, anotherIndex)>
  1146. !
  1147. shallowCopy
  1148. ^self class fromString: self
  1149. !
  1150. deepCopy
  1151. ^self shallowCopy
  1152. ! !
  1153. !String methodsFor: 'error handling'!
  1154. errorReadOnly
  1155. self error: 'Object is read-only'
  1156. ! !
  1157. !String methodsFor: 'printing'!
  1158. printString
  1159. ^'''', self, ''''
  1160. !
  1161. printNl
  1162. <console.log(self)>
  1163. ! !
  1164. !String methodsFor: 'regular expressions'!
  1165. replace: aString with: anotherString
  1166. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1167. !
  1168. replaceRegexp: aRegexp with: aString
  1169. <return self.replace(aRegexp, aString)>
  1170. !
  1171. match: aRegexp
  1172. <return self.search(aRegexp) !!= -1>
  1173. !
  1174. trimLeft: separators
  1175. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1176. !
  1177. trimRight: separators
  1178. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1179. !
  1180. trimLeft
  1181. ^self trimLeft: '\s'
  1182. !
  1183. trimRight
  1184. ^self trimRight: '\s'
  1185. !
  1186. trimBoth
  1187. ^self trimBoth: '\s'
  1188. !
  1189. trimBoth: separators
  1190. ^(self trimLeft: separators) trimRight: separators
  1191. ! !
  1192. !String methodsFor: 'split join'!
  1193. join: aCollection
  1194. ^ String
  1195. streamContents: [:stream | aCollection
  1196. do: [:each | stream nextPutAll: each asString]
  1197. separatedBy: [stream nextPutAll: self]]
  1198. ! !
  1199. !String methodsFor: 'testing'!
  1200. isString
  1201. ^true
  1202. !
  1203. includesSubString: subString
  1204. < return self.indexOf(subString) !!= -1 >
  1205. ! !
  1206. !String class methodsFor: 'accessing'!
  1207. streamClass
  1208. ^StringStream
  1209. !
  1210. cr
  1211. <return '\r'>
  1212. !
  1213. lf
  1214. <return '\n'>
  1215. !
  1216. space
  1217. <return ' '>
  1218. !
  1219. tab
  1220. <return '\t'>
  1221. !
  1222. crlf
  1223. <return '\r\n'>
  1224. ! !
  1225. !String class methodsFor: 'instance creation'!
  1226. fromString: aString
  1227. <return new self.fn(aString)>
  1228. !
  1229. streamContents: blockWithArg
  1230. |stream|
  1231. stream := (self streamClass on: String new).
  1232. blockWithArg value: stream.
  1233. ^ stream contents
  1234. !
  1235. value: aUTFCharCode
  1236. <return String.fromCharCode(aUTFCharCode);>
  1237. ! !
  1238. SequenceableCollection subclass: #Array
  1239. instanceVariableNames: ''
  1240. category: 'Kernel'!
  1241. !Array methodsFor: 'accessing'!
  1242. size
  1243. <return self.length>
  1244. !
  1245. at: anIndex put: anObject
  1246. <return self[anIndex - 1] = anObject>
  1247. !
  1248. at: anIndex ifAbsent: aBlock
  1249. <
  1250. var value = self[anIndex - 1];
  1251. if(value === undefined) {
  1252. return aBlock();
  1253. } else {
  1254. return value;
  1255. }
  1256. >
  1257. ! !
  1258. !Array methodsFor: 'adding/removing'!
  1259. add: anObject
  1260. <self.push(anObject); return anObject;>
  1261. !
  1262. remove: anObject
  1263. <
  1264. for(var i=0;i<self.length;i++) {
  1265. if(self[i] == anObject) {
  1266. self.splice(i,1);
  1267. break;
  1268. }
  1269. }
  1270. >
  1271. !
  1272. removeFrom: aNumber to: anotherNumber
  1273. <self.splice(aNumber - 1,anotherNumber - 1)>
  1274. ! !
  1275. !Array methodsFor: 'converting'!
  1276. asJavascript
  1277. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  1278. !
  1279. asJSONObject
  1280. ^self collect: [:each | each asJSONObject]
  1281. ! !
  1282. !Array methodsFor: 'copying'!
  1283. shallowCopy
  1284. | newCollection |
  1285. newCollection := self class new.
  1286. self do: [:each | newCollection add: each].
  1287. ^newCollection
  1288. !
  1289. deepCopy
  1290. | newCollection |
  1291. newCollection := self class new.
  1292. self do: [:each | newCollection add: each deepCopy].
  1293. ^newCollection
  1294. !
  1295. copyFrom: anIndex to: anotherIndex
  1296. | array |
  1297. array := self class new.
  1298. anIndex to: anotherIndex do: [:each |
  1299. array add: (self at: each)].
  1300. ^array
  1301. ! !
  1302. !Array methodsFor: 'enumerating'!
  1303. join: aString
  1304. <return self.join(aString)>
  1305. !
  1306. sort
  1307. ^self basicPerform: 'sort'
  1308. !
  1309. sort: aBlock
  1310. <
  1311. return self.sort(function(a, b) {
  1312. if(aBlock(a,b)) {return 1} else {return -1}
  1313. })
  1314. >
  1315. !
  1316. sorted
  1317. ^self copy sort
  1318. !
  1319. sorted: aBlock
  1320. ^self copy sorted: aBlock
  1321. !
  1322. printString
  1323. | str |
  1324. str := '' writeStream.
  1325. str nextPutAll: super printString, ' ('.
  1326. self
  1327. do: [:each | str nextPutAll: each printString]
  1328. separatedBy: [str nextPutAll: ' '].
  1329. str nextPutAll: ')'.
  1330. ^str contents
  1331. ! !
  1332. Object subclass: #RegularExpression
  1333. instanceVariableNames: ''
  1334. category: 'Kernel'!
  1335. !RegularExpression methodsFor: 'evaluating'!
  1336. compile: aString
  1337. <return self.compile(aString)>
  1338. !
  1339. exec: aString
  1340. <return self.exec(aString) || nil>
  1341. !
  1342. test: aString
  1343. <return self.test(aString)>
  1344. ! !
  1345. !RegularExpression class methodsFor: 'instance creation'!
  1346. fromString: aString flag: anotherString
  1347. <return new RegExp(aString, anotherString)>
  1348. !
  1349. fromString: aString
  1350. ^self fromString: aString flag: ''
  1351. ! !
  1352. Object subclass: #Error
  1353. instanceVariableNames: 'messageText'
  1354. category: 'Kernel'!
  1355. !Error methodsFor: 'accessing'!
  1356. messageText
  1357. ^messageText
  1358. !
  1359. messageText: aString
  1360. messageText := aString
  1361. !
  1362. context
  1363. <return self.context>
  1364. ! !
  1365. !Error methodsFor: 'signaling'!
  1366. signal
  1367. <self.context = thisContext; self.smalltalkError = true; throw(self)>
  1368. ! !
  1369. !Error class methodsFor: 'instance creation'!
  1370. signal: aString
  1371. ^self new
  1372. messageText: aString;
  1373. signal
  1374. ! !
  1375. Object subclass: #MethodContext
  1376. instanceVariableNames: ''
  1377. category: 'Kernel'!
  1378. !MethodContext methodsFor: 'accessing'!
  1379. receiver
  1380. <return self.receiver>
  1381. !
  1382. selector
  1383. <return smalltalk.convertSelector(self.selector)>
  1384. !
  1385. home
  1386. <return self.homeContext>
  1387. !
  1388. temps
  1389. <return self.temps>
  1390. !
  1391. printString
  1392. ^super printString, '(', self asString, ')'
  1393. !
  1394. asString
  1395. ^self receiver class printString, ' >> ', self selector
  1396. ! !
  1397. Object subclass: #Association
  1398. instanceVariableNames: 'key value'
  1399. category: 'Kernel'!
  1400. !Association methodsFor: 'accessing'!
  1401. key: aKey
  1402. key := aKey
  1403. !
  1404. key
  1405. ^key
  1406. !
  1407. value: aValue
  1408. value := aValue
  1409. !
  1410. value
  1411. ^value
  1412. ! !
  1413. !Association methodsFor: 'comparing'!
  1414. = anAssociation
  1415. ^self class = anAssociation class and: [
  1416. self key = anAssociation key and: [
  1417. self value = anAssociation value]]
  1418. ! !
  1419. !Association class methodsFor: 'instance creation'!
  1420. key: aKey value: aValue
  1421. ^self new
  1422. key: aKey;
  1423. value: aValue;
  1424. yourself
  1425. ! !
  1426. Collection subclass: #Dictionary
  1427. instanceVariableNames: 'keys'
  1428. category: 'Kernel'!
  1429. !Dictionary methodsFor: 'accessing'!
  1430. size
  1431. ^keys size
  1432. !
  1433. associations
  1434. | associations |
  1435. associations := #().
  1436. keys do: [:each |
  1437. associations add: (Association key: each value: (self at: each))].
  1438. ^associations
  1439. !
  1440. keys
  1441. ^keys copy
  1442. !
  1443. values
  1444. ^keys collect: [:each | self at: each]
  1445. !
  1446. at: aKey put: aValue
  1447. (keys includes: aKey) ifFalse: [keys add: aKey].
  1448. ^self basicAt: aKey put: aValue
  1449. !
  1450. at: aKey ifAbsent: aBlock
  1451. ^(self keys includes: aKey)
  1452. ifTrue: [self basicAt: aKey]
  1453. ifFalse: aBlock
  1454. !
  1455. at: aKey ifAbsentPut: aBlock
  1456. ^self at: aKey ifAbsent: [
  1457. self at: aKey put: aBlock value]
  1458. !
  1459. at: aKey ifPresent: aBlock
  1460. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  1461. !
  1462. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  1463. ^(self basicAt: aKey)
  1464. ifNil: anotherBlock
  1465. ifNotNil: [aBlock value: (self at: aKey)]
  1466. !
  1467. at: aKey
  1468. ^self at: aKey ifAbsent: [self errorNotFound]
  1469. ! !
  1470. !Dictionary methodsFor: 'adding/removing'!
  1471. add: anAssociation
  1472. self at: anAssociation key put: anAssociation value
  1473. !
  1474. addAll: aDictionary
  1475. super addAll: aDictionary associations.
  1476. ^aDictionary
  1477. !
  1478. remove: aKey
  1479. self removeKey: aKey
  1480. !
  1481. removeKey: aKey
  1482. keys remove: aKey
  1483. ! !
  1484. !Dictionary methodsFor: 'comparing'!
  1485. = aDictionary
  1486. self class = aDictionary class ifFalse: [^false].
  1487. self associationsDo: [:assoc |
  1488. (aDictionary at: assoc key ifAbsent: [^false]) = assoc value
  1489. ifFalse: [^false]].
  1490. ^true
  1491. ! !
  1492. !Dictionary methodsFor: 'converting'!
  1493. asJSONObject
  1494. | object |
  1495. object := Object new.
  1496. self keysAndValuesDo: [:key :value |
  1497. object basicAt: key put: value asJSONObject].
  1498. ^object
  1499. ! !
  1500. !Dictionary methodsFor: 'copying'!
  1501. shallowCopy
  1502. | copy |
  1503. copy := self class new.
  1504. self associationsDo: [:each |
  1505. copy at: each key put: each value].
  1506. ^copy
  1507. !
  1508. , aCollection
  1509. self shouldNotImplement
  1510. !
  1511. copyFrom: anIndex to: anotherIndex
  1512. self shouldNotImplement
  1513. ! !
  1514. !Dictionary methodsFor: 'enumerating'!
  1515. associationsDo: aBlock
  1516. self associations do: aBlock
  1517. !
  1518. keysAndValuesDo: aBlock
  1519. self associationsDo: [:each |
  1520. aBlock value: each key value: each value]
  1521. !
  1522. do: aBlock
  1523. self values do: aBlock
  1524. !
  1525. select: aBlock
  1526. | newDict |
  1527. newDict := self class new.
  1528. self keysAndValuesDo: [:key :value |
  1529. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1530. ^newDict
  1531. !
  1532. collect: aBlock
  1533. | newDict |
  1534. newDict := self class new.
  1535. self keysAndValuesDo: [:key :value |
  1536. newDict at: key put: (aBlock value: value)].
  1537. ^newDict
  1538. !
  1539. detect: aBlock ifNone: anotherBlock
  1540. ^self values detect: aBlock ifNone: anotherBlock
  1541. !
  1542. includes: anObject
  1543. ^self values includes: anObject
  1544. ! !
  1545. !Dictionary methodsFor: 'initialization'!
  1546. initialize
  1547. super initialize.
  1548. keys := #()
  1549. ! !
  1550. !Dictionary methodsFor: 'printing'!
  1551. printString
  1552. ^ String streamContents: [:aStream|
  1553. aStream
  1554. nextPutAll: super printString;
  1555. nextPutAll: '('.
  1556. self associations
  1557. do: [:anAssociation|
  1558. aStream
  1559. nextPutAll: anAssociation key printString;
  1560. nextPutAll: ' -> ';
  1561. nextPutAll: anAssociation value printString]
  1562. separatedBy: [aStream nextPutAll: ' , '].
  1563. aStream nextPutAll: ')'.
  1564. ]
  1565. ! !
  1566. Object subclass: #ClassBuilder
  1567. instanceVariableNames: ''
  1568. category: 'Kernel'!
  1569. !ClassBuilder methodsFor: 'class creation'!
  1570. superclass: aClass subclass: aString
  1571. self superclass: aClass subclass: aString instanceVariableNames: '' category: nil
  1572. !
  1573. superclass: aClass subclass: aString instanceVariableNames: aString2 category: aString3
  1574. | newClass |
  1575. newClass := self addSubclassOf: aClass named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2).
  1576. self setupClass: newClass.
  1577. newClass category: (aString3 ifNil: ['unclassified'])
  1578. !
  1579. class: aClass instanceVariableNames: aString
  1580. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  1581. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  1582. self setupClass: aClass
  1583. ! !
  1584. !ClassBuilder methodsFor: 'private'!
  1585. instanceVariableNamesFor: aString
  1586. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  1587. !
  1588. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  1589. <smalltalk.addClass(aString, aClass, aCollection);
  1590. return smalltalk[aString]>
  1591. !
  1592. setupClass: aClass
  1593. <smalltalk.init(aClass);>
  1594. ! !
  1595. Object subclass: #ClassCategoryReader
  1596. instanceVariableNames: 'class category chunkParser'
  1597. category: 'Kernel'!
  1598. !ClassCategoryReader methodsFor: 'accessing'!
  1599. class: aClass category: aString
  1600. class := aClass.
  1601. category := aString
  1602. ! !
  1603. !ClassCategoryReader methodsFor: 'fileIn'!
  1604. scanFrom: aStream
  1605. | nextChunk |
  1606. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  1607. nextChunk isEmptyChunk ifFalse: [
  1608. self compileMethod: nextChunk contents.
  1609. self scanFrom: aStream].
  1610. ! !
  1611. !ClassCategoryReader methodsFor: 'initialization'!
  1612. initialize
  1613. super initialize.
  1614. chunkParser := ChunkParser new.
  1615. ! !
  1616. !ClassCategoryReader methodsFor: 'private'!
  1617. compileMethod: aString
  1618. | method |
  1619. method := Compiler new load: aString forClass: class.
  1620. method category: category.
  1621. class addCompiledMethod: method
  1622. ! !
  1623. Object subclass: #Stream
  1624. instanceVariableNames: 'collection position streamSize'
  1625. category: 'Kernel'!
  1626. !Stream methodsFor: 'accessing'!
  1627. collection
  1628. ^collection
  1629. !
  1630. setCollection: aCollection
  1631. collection := aCollection
  1632. !
  1633. position
  1634. ^position ifNil: [position := 0]
  1635. !
  1636. position: anInteger
  1637. position := anInteger
  1638. !
  1639. streamSize
  1640. ^streamSize
  1641. !
  1642. setStreamSize: anInteger
  1643. streamSize := anInteger
  1644. !
  1645. contents
  1646. ^self collection
  1647. copyFrom: 1
  1648. to: self streamSize
  1649. !
  1650. size
  1651. ^self streamSize
  1652. ! !
  1653. !Stream methodsFor: 'actions'!
  1654. reset
  1655. self position: 0
  1656. !
  1657. close
  1658. !
  1659. flush
  1660. !
  1661. resetContents
  1662. self reset.
  1663. self setStreamSize: 0
  1664. ! !
  1665. !Stream methodsFor: 'enumerating'!
  1666. do: aBlock
  1667. [self atEnd] whileFalse: [aBlock value: self next]
  1668. ! !
  1669. !Stream methodsFor: 'positioning'!
  1670. setToEnd
  1671. self position: self size
  1672. !
  1673. skip: anInteger
  1674. self position: ((self position + anInteger) min: self size max: 0)
  1675. ! !
  1676. !Stream methodsFor: 'reading'!
  1677. next
  1678. self position: self position + 1.
  1679. ^collection at: self position
  1680. !
  1681. next: anInteger
  1682. | tempCollection |
  1683. tempCollection := self collection class new.
  1684. anInteger timesRepeat: [
  1685. self atEnd ifFalse: [
  1686. tempCollection add: self next]].
  1687. ^tempCollection
  1688. !
  1689. peek
  1690. ^self atEnd ifFalse: [
  1691. self collection at: self position + 1]
  1692. ! !
  1693. !Stream methodsFor: 'testing'!
  1694. atEnd
  1695. ^self position = self size
  1696. !
  1697. atStart
  1698. ^self position = 0
  1699. !
  1700. isEmpty
  1701. ^self size = 0
  1702. ! !
  1703. !Stream methodsFor: 'writing'!
  1704. nextPut: anObject
  1705. self position: self position + 1.
  1706. self collection at: self position put: anObject.
  1707. self setStreamSize: (self streamSize max: self position)
  1708. !
  1709. nextPutAll: aCollection
  1710. aCollection do: [:each |
  1711. self nextPut: each]
  1712. ! !
  1713. !Stream class methodsFor: 'instance creation'!
  1714. on: aCollection
  1715. ^self new
  1716. setCollection: aCollection;
  1717. setStreamSize: aCollection size;
  1718. yourself
  1719. ! !
  1720. Stream subclass: #StringStream
  1721. instanceVariableNames: ''
  1722. category: 'Kernel'!
  1723. !StringStream methodsFor: 'reading'!
  1724. next: anInteger
  1725. | tempCollection |
  1726. tempCollection := self collection class new.
  1727. anInteger timesRepeat: [
  1728. self atEnd ifFalse: [
  1729. tempCollection := tempCollection, self next]].
  1730. ^tempCollection
  1731. ! !
  1732. !StringStream methodsFor: 'writing'!
  1733. nextPut: aString
  1734. self nextPutAll: aString
  1735. !
  1736. nextPutAll: aString
  1737. self setCollection:
  1738. (self collection copyFrom: 1 to: self position),
  1739. aString,
  1740. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  1741. self position: self position + aString size.
  1742. self setStreamSize: (self streamSize max: self position)
  1743. !
  1744. cr
  1745. ^self nextPutAll: String cr
  1746. !
  1747. crlf
  1748. ^self nextPutAll: String crlf
  1749. !
  1750. lf
  1751. ^self nextPutAll: String lf
  1752. !
  1753. space
  1754. self nextPut: ' '
  1755. ! !
  1756. Object subclass: #ClassCommentReader
  1757. instanceVariableNames: 'class chunkParser'
  1758. category: 'Kernel'!
  1759. !ClassCommentReader methodsFor: 'accessing'!
  1760. class: aClass
  1761. class := aClass
  1762. ! !
  1763. !ClassCommentReader methodsFor: 'fileIn'!
  1764. scanFrom: aStream
  1765. | nextChunk |
  1766. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  1767. nextChunk isEmptyChunk ifFalse: [
  1768. self setComment: nextChunk contents].
  1769. ! !
  1770. !ClassCommentReader methodsFor: 'initialization'!
  1771. initialize
  1772. super initialize.
  1773. chunkParser := ChunkParser new.
  1774. ! !
  1775. !ClassCommentReader methodsFor: 'private'!
  1776. setComment: aString
  1777. class comment: aString
  1778. ! !
  1779. Object subclass: #Random
  1780. instanceVariableNames: ''
  1781. category: 'Kernel'!
  1782. !Random methodsFor: 'accessing'!
  1783. next
  1784. <return Math.random()>
  1785. !
  1786. next: anInteger
  1787. ^1 to: anInteger collect: [:each | self next]
  1788. ! !
  1789. Object subclass: #Point
  1790. instanceVariableNames: 'x y'
  1791. category: 'Kernel'!
  1792. !Point methodsFor: 'accessing'!
  1793. x
  1794. ^x
  1795. !
  1796. y
  1797. ^y
  1798. !
  1799. y: aNumber
  1800. y := aNumber
  1801. !
  1802. x: aNumber
  1803. x := aNumber
  1804. ! !
  1805. !Point methodsFor: 'arithmetic'!
  1806. * aPoint
  1807. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  1808. !
  1809. + aPoint
  1810. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  1811. !
  1812. - aPoint
  1813. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  1814. !
  1815. / aPoint
  1816. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1817. ! !
  1818. !Point methodsFor: 'converting'!
  1819. asPoint
  1820. ^self
  1821. ! !
  1822. !Point class methodsFor: 'instance creation'!
  1823. x: aNumber y: anotherNumber
  1824. ^self new
  1825. x: aNumber;
  1826. y: anotherNumber;
  1827. yourself
  1828. ! !
  1829. Object subclass: #Message
  1830. instanceVariableNames: 'selector arguments'
  1831. category: 'Kernel'!
  1832. !Message methodsFor: 'accessing'!
  1833. selector
  1834. ^selector
  1835. !
  1836. selector: aString
  1837. selector := aString
  1838. !
  1839. arguments: anArray
  1840. arguments := anArray
  1841. !
  1842. arguments
  1843. ^arguments
  1844. ! !
  1845. !Message class methodsFor: 'instance creation'!
  1846. selector: aString arguments: anArray
  1847. ^self new
  1848. selector: aString;
  1849. arguments: anArray;
  1850. yourself
  1851. ! !
  1852. Error subclass: #MessageNotUnderstood
  1853. instanceVariableNames: 'message receiver'
  1854. category: 'Kernel'!
  1855. !MessageNotUnderstood methodsFor: 'accessing'!
  1856. message
  1857. ^message
  1858. !
  1859. message: aMessage
  1860. message := aMessage
  1861. !
  1862. receiver
  1863. ^receiver
  1864. !
  1865. receiver: anObject
  1866. receiver := anObject
  1867. !
  1868. messageText
  1869. ^self receiver asString, ' does not understand #', self message selector
  1870. ! !
  1871. Object subclass: #ErrorHandler
  1872. instanceVariableNames: ''
  1873. category: 'Kernel'!
  1874. !ErrorHandler methodsFor: 'error handling'!
  1875. handleError: anError
  1876. anError context ifNotNil: [self logErrorContext: anError context].
  1877. self logError: anError
  1878. ! !
  1879. !ErrorHandler methodsFor: 'private'!
  1880. logContext: aContext
  1881. aContext home ifNotNil: [
  1882. self logContext: aContext home].
  1883. self log: aContext receiver asString, '>>', aContext selector
  1884. !
  1885. logErrorContext: aContext
  1886. aContext ifNotNil: [
  1887. aContext home ifNotNil: [
  1888. self logContext: aContext home]]
  1889. !
  1890. logError: anError
  1891. self log: anError messageText
  1892. !
  1893. log: aString
  1894. console log: aString
  1895. ! !
  1896. ErrorHandler class instanceVariableNames: 'current'!
  1897. !ErrorHandler class methodsFor: 'accessing'!
  1898. current
  1899. ^current
  1900. !
  1901. setCurrent: anHandler
  1902. current := anHandler
  1903. ! !
  1904. !ErrorHandler class methodsFor: 'initialization'!
  1905. initialize
  1906. self register
  1907. !
  1908. register
  1909. ErrorHandler setCurrent: self new
  1910. ! !