Kernel.st 34 KB

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