1
0

Kernel.st 39 KB

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