Kernel.st 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303
  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. trimLeft: separators
  1115. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1116. !
  1117. trimRight: separators
  1118. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1119. !
  1120. trimLeft
  1121. ^self trimLeft: '\s'
  1122. !
  1123. trimRight
  1124. ^self trimRight: '\s'
  1125. !
  1126. trimBoth
  1127. ^self trimBoth: '\s'
  1128. !
  1129. trimBoth: separators
  1130. ^(self trimLeft: separators) trimRight: separators
  1131. ! !
  1132. !String methodsFor: 'testing'!
  1133. isString
  1134. ^true
  1135. ! !
  1136. !String class methodsFor: 'accessing'!
  1137. streamClass
  1138. ^StringStream
  1139. !
  1140. cr
  1141. {'return ''\r'';'}
  1142. !
  1143. lf
  1144. {'return ''\n'';'}
  1145. !
  1146. space
  1147. {'return '' '';'}
  1148. !
  1149. tab
  1150. {'return ''\t'';'}
  1151. !
  1152. crlf
  1153. {'return ''\r\n'';'}
  1154. ! !
  1155. !String class methodsFor: 'instance creation'!
  1156. fromString: aString
  1157. {'return new self.fn(aString);'}
  1158. ! !
  1159. SequenceableCollection subclass: #Array
  1160. instanceVariableNames: ''
  1161. category: 'Kernel'!
  1162. !Array methodsFor: 'accessing'!
  1163. size
  1164. {'return self.length'}
  1165. !
  1166. at: anIndex put: anObject
  1167. {'return self[anIndex - 1] = anObject'}
  1168. !
  1169. at: anIndex ifAbsent: aBlock
  1170. {'
  1171. var value = self[anIndex - 1];
  1172. if(value === undefined) {
  1173. return aBlock();
  1174. } else {
  1175. return value;
  1176. }
  1177. '}
  1178. ! !
  1179. !Array methodsFor: 'adding/removing'!
  1180. add: anObject
  1181. {'self.push(anObject); return anObject;'}
  1182. !
  1183. remove: anObject
  1184. {'
  1185. for(var i=0;i<self.length;i++) {
  1186. if(self[i] == anObject) {
  1187. self.splice(i,1);
  1188. break;
  1189. }
  1190. }
  1191. '}
  1192. !
  1193. removeFrom: aNumber to: anotherNumber
  1194. {'self.splice(aNumber - 1,anotherNumber - 1)'}
  1195. ! !
  1196. !Array methodsFor: 'converting'!
  1197. asJavascript
  1198. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  1199. !
  1200. asJSONObject
  1201. ^self collect: [:each | each asJSONObject]
  1202. ! !
  1203. !Array methodsFor: 'copying'!
  1204. shallowCopy
  1205. | newCollection |
  1206. newCollection := self class new.
  1207. self do: [:each | newCollection add: each].
  1208. ^newCollection
  1209. !
  1210. deepCopy
  1211. | newCollection |
  1212. newCollection := self class new.
  1213. self do: [:each | newCollection add: each deepCopy].
  1214. ^newCollection
  1215. !
  1216. copyFrom: anIndex to: anotherIndex
  1217. | array |
  1218. array := self class new.
  1219. anIndex to: anotherIndex do: [:each |
  1220. array add: (self at: each)].
  1221. ^array
  1222. ! !
  1223. !Array methodsFor: 'enumerating'!
  1224. join: aString
  1225. {'return self.join(aString);'}
  1226. !
  1227. sort
  1228. ^self basicPerform: 'sort'
  1229. !
  1230. sort: aBlock
  1231. {'
  1232. return self.sort(function(a, b) {
  1233. if(aBlock(a,b)) {return 1} else {return -1}
  1234. })
  1235. '}
  1236. !
  1237. sorted
  1238. ^self copy sort
  1239. !
  1240. sorted: aBlock
  1241. ^self copy sorted: aBlock
  1242. ! !
  1243. Object subclass: #RegularExpression
  1244. instanceVariableNames: ''
  1245. category: 'Kernel'!
  1246. !RegularExpression methodsFor: 'evaluating'!
  1247. compile: aString
  1248. {'return self.compile(aString);'}
  1249. !
  1250. exec: aString
  1251. {'return self.exec(aString) || nil'}
  1252. !
  1253. test: aString
  1254. {'return self.test(aString);'}
  1255. ! !
  1256. !RegularExpression class methodsFor: 'instance creation'!
  1257. fromString: aString flag: anotherString
  1258. {'return new RegExp(aString, anotherString);'}
  1259. !
  1260. fromString: aString
  1261. ^self fromString: aString flag: ''
  1262. ! !
  1263. Object subclass: #Error
  1264. instanceVariableNames: 'messageText'
  1265. category: 'Kernel'!
  1266. !Error methodsFor: 'accessing'!
  1267. messageText
  1268. ^messageText
  1269. !
  1270. messageText: aString
  1271. messageText := aString
  1272. ! !
  1273. !Error methodsFor: 'signaling'!
  1274. signal
  1275. {'console.log(self._messageText()); throw(self)'}
  1276. ! !
  1277. !Error class methodsFor: 'instance creation'!
  1278. signal: aString
  1279. ^self new
  1280. messageText: aString;
  1281. signal
  1282. ! !
  1283. Object subclass: #Association
  1284. instanceVariableNames: 'key value'
  1285. category: 'Kernel'!
  1286. !Association methodsFor: 'accessing'!
  1287. key: aKey
  1288. key := aKey
  1289. !
  1290. key
  1291. ^key
  1292. !
  1293. value: aValue
  1294. value := aValue
  1295. !
  1296. value
  1297. ^value
  1298. ! !
  1299. !Association methodsFor: 'comparing'!
  1300. = anAssociation
  1301. ^self class = anAssociation class and: [
  1302. self key = anAssociation key and: [
  1303. self value = anAssociation value]]
  1304. ! !
  1305. !Association class methodsFor: 'instance creation'!
  1306. key: aKey value: aValue
  1307. ^self new
  1308. key: aKey;
  1309. value: aValue;
  1310. yourself
  1311. ! !
  1312. Collection subclass: #Dictionary
  1313. instanceVariableNames: 'keys'
  1314. category: 'Kernel'!
  1315. !Dictionary methodsFor: 'accessing'!
  1316. size
  1317. ^keys size
  1318. !
  1319. associations
  1320. | associations |
  1321. associations := #().
  1322. keys do: [:each |
  1323. associations add: (Association key: each value: (self at: each))].
  1324. ^associations
  1325. !
  1326. keys
  1327. ^keys copy
  1328. !
  1329. values
  1330. ^keys collect: [:each | self at: each]
  1331. !
  1332. at: aKey put: aValue
  1333. (keys includes: aKey) ifFalse: [keys add: aKey].
  1334. ^self basicAt: aKey put: aValue
  1335. !
  1336. at: aKey ifAbsent: aBlock
  1337. ^(self keys includes: aKey)
  1338. ifTrue: [self basicAt: aKey]
  1339. ifFalse: aBlock
  1340. !
  1341. at: aKey ifAbsentPut: aBlock
  1342. ^self at: aKey ifAbsent: [
  1343. self at: aKey put: aBlock value]
  1344. !
  1345. at: aKey ifPresent: aBlock
  1346. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  1347. !
  1348. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  1349. ^(self basicAt: aKey)
  1350. ifNil: anotherBlock
  1351. ifNotNil: [aBlock value: (self at: aKey)]
  1352. !
  1353. at: aKey
  1354. ^self at: aKey ifAbsent: [self errorNotFound]
  1355. ! !
  1356. !Dictionary methodsFor: 'adding/removing'!
  1357. add: anAssociation
  1358. self at: anAssociation key put: anAssociation value
  1359. !
  1360. addAll: aDictionary
  1361. super addAll: aDictionary associations.
  1362. ^aDictionary
  1363. !
  1364. remove: aKey
  1365. self removeKey: aKey
  1366. !
  1367. removeKey: aKey
  1368. keys remove: aKey
  1369. ! !
  1370. !Dictionary methodsFor: 'comparing'!
  1371. = aDictionary
  1372. self class = aDictionary class ifFalse: [^false].
  1373. self associationsDo: [:assoc |
  1374. (aDictionary at: assoc key ifAbsent: [^false]) = assoc value
  1375. ifFalse: [^false]].
  1376. ^true
  1377. ! !
  1378. !Dictionary methodsFor: 'converting'!
  1379. asJSONObject
  1380. | object |
  1381. object := Object new.
  1382. self keysAndValuesDo: [:key :value |
  1383. object basicAt: key put: value asJSONObject].
  1384. ^object
  1385. ! !
  1386. !Dictionary methodsFor: 'copying'!
  1387. shallowCopy
  1388. | copy |
  1389. copy := self class new.
  1390. self associationsDo: [:each |
  1391. copy at: each key put: each value].
  1392. ^copy
  1393. !
  1394. , aCollection
  1395. self shouldNotImplement
  1396. !
  1397. copyFrom: anIndex to: anotherIndex
  1398. self shouldNotImplement
  1399. ! !
  1400. !Dictionary methodsFor: 'enumerating'!
  1401. associationsDo: aBlock
  1402. self associations do: aBlock
  1403. !
  1404. keysAndValuesDo: aBlock
  1405. self associationsDo: [:each |
  1406. aBlock value: each key value: each value]
  1407. !
  1408. do: aBlock
  1409. self values do: aBlock
  1410. !
  1411. select: aBlock
  1412. | newDict |
  1413. newDict := self class new.
  1414. self keysAndValuesDo: [:key :value |
  1415. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1416. ^newDict
  1417. !
  1418. collect: aBlock
  1419. | newDict |
  1420. newDict := self class new.
  1421. self keysAndValuesDo: [:key :value |
  1422. newDict at: key put: (aBlock value: value)].
  1423. ^newDict
  1424. !
  1425. detect: aBlock ifNone: anotherBlock
  1426. ^self values detect: aBlock ifNone: anotherBlock
  1427. !
  1428. includes: anObject
  1429. ^self values includes: anObject
  1430. ! !
  1431. !Dictionary methodsFor: 'initialization'!
  1432. initialize
  1433. super initialize.
  1434. keys := #()
  1435. ! !
  1436. Object subclass: #ClassBuilder
  1437. instanceVariableNames: ''
  1438. category: 'Kernel'!
  1439. !ClassBuilder methodsFor: 'class creation'!
  1440. superclass: aClass subclass: aString
  1441. self superclass: aClass subclass: aString instanceVariableNames: '' category: nil
  1442. !
  1443. superclass: aClass subclass: aString instanceVariableNames: aString2 category: aString3
  1444. | newClass |
  1445. newClass := self addSubclassOf: aClass named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2).
  1446. self setupClass: newClass.
  1447. newClass category: (aString3 ifNil: ['unclassified'])
  1448. !
  1449. class: aClass instanceVariableNames: aString
  1450. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  1451. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  1452. self setupClass: aClass
  1453. ! !
  1454. !ClassBuilder methodsFor: 'private'!
  1455. instanceVariableNamesFor: aString
  1456. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  1457. !
  1458. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  1459. {'smalltalk.addClass(aString, aClass, aCollection);
  1460. return smalltalk[aString]'}
  1461. !
  1462. setupClass: aClass
  1463. {'smalltalk.init(aClass);'}
  1464. ! !
  1465. Object subclass: #ClassCategoryReader
  1466. instanceVariableNames: 'class category chunkParser'
  1467. category: 'Kernel'!
  1468. !ClassCategoryReader methodsFor: 'accessing'!
  1469. class: aClass category: aString
  1470. class := aClass.
  1471. category := aString
  1472. ! !
  1473. !ClassCategoryReader methodsFor: 'fileIn'!
  1474. scanFrom: aStream
  1475. | nextChunk |
  1476. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  1477. nextChunk isEmptyChunk ifFalse: [
  1478. self compileMethod: nextChunk contents.
  1479. self scanFrom: aStream].
  1480. ! !
  1481. !ClassCategoryReader methodsFor: 'initialization'!
  1482. initialize
  1483. super initialize.
  1484. chunkParser := ChunkParser new.
  1485. ! !
  1486. !ClassCategoryReader methodsFor: 'private'!
  1487. compileMethod: aString
  1488. | method |
  1489. method := Compiler new load: aString forClass: class.
  1490. method category: category.
  1491. class addCompiledMethod: method
  1492. ! !
  1493. Object subclass: #Stream
  1494. instanceVariableNames: 'collection position streamSize'
  1495. category: 'Kernel'!
  1496. !Stream methodsFor: 'accessing'!
  1497. collection
  1498. ^collection
  1499. !
  1500. setCollection: aCollection
  1501. collection := aCollection
  1502. !
  1503. position
  1504. ^position ifNil: [position := 0]
  1505. !
  1506. position: anInteger
  1507. position := anInteger
  1508. !
  1509. streamSize
  1510. ^streamSize
  1511. !
  1512. setStreamSize: anInteger
  1513. streamSize := anInteger
  1514. !
  1515. contents
  1516. ^self collection
  1517. copyFrom: 1
  1518. to: self streamSize
  1519. !
  1520. size
  1521. ^self streamSize
  1522. ! !
  1523. !Stream methodsFor: 'actions'!
  1524. reset
  1525. self position: 0
  1526. !
  1527. close
  1528. !
  1529. flush
  1530. !
  1531. resetContents
  1532. self reset.
  1533. self setStreamSize: 0
  1534. ! !
  1535. !Stream methodsFor: 'enumerating'!
  1536. do: aBlock
  1537. [self atEnd] whileFalse: [aBlock value: self next]
  1538. ! !
  1539. !Stream methodsFor: 'positioning'!
  1540. setToEnd
  1541. self position: self size
  1542. !
  1543. skip: anInteger
  1544. self position: ((self position + anInteger) min: self size max: 0)
  1545. ! !
  1546. !Stream methodsFor: 'reading'!
  1547. next
  1548. self position: self position + 1.
  1549. ^collection at: self position
  1550. !
  1551. next: anInteger
  1552. | tempCollection |
  1553. tempCollection := self collection class new.
  1554. anInteger timesRepeat: [
  1555. self atEnd ifFalse: [
  1556. tempCollection add: self next]].
  1557. ^tempCollection
  1558. !
  1559. peek
  1560. ^self atEnd ifFalse: [
  1561. self collection at: self position + 1]
  1562. ! !
  1563. !Stream methodsFor: 'testing'!
  1564. atEnd
  1565. ^self position = self size
  1566. !
  1567. atStart
  1568. ^self position = 0
  1569. !
  1570. isEmpty
  1571. ^self size = 0
  1572. ! !
  1573. !Stream methodsFor: 'writing'!
  1574. nextPut: anObject
  1575. self position: self position + 1.
  1576. self collection at: self position put: anObject.
  1577. self setStreamSize: (self streamSize max: self position)
  1578. !
  1579. nextPutAll: aCollection
  1580. aCollection do: [:each |
  1581. self nextPut: each]
  1582. ! !
  1583. !Stream class methodsFor: 'instance creation'!
  1584. on: aCollection
  1585. ^self new
  1586. setCollection: aCollection;
  1587. setStreamSize: aCollection size;
  1588. yourself
  1589. ! !
  1590. Stream subclass: #StringStream
  1591. instanceVariableNames: ''
  1592. category: 'Kernel'!
  1593. !StringStream methodsFor: 'reading'!
  1594. next: anInteger
  1595. | tempCollection |
  1596. tempCollection := self collection class new.
  1597. anInteger timesRepeat: [
  1598. self atEnd ifFalse: [
  1599. tempCollection := tempCollection, self next]].
  1600. ^tempCollection
  1601. ! !
  1602. !StringStream methodsFor: 'writing'!
  1603. nextPut: aString
  1604. self nextPutAll: aString
  1605. !
  1606. nextPutAll: aString
  1607. self setCollection:
  1608. (self collection copyFrom: 1 to: self position),
  1609. aString,
  1610. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  1611. self position: self position + aString size.
  1612. self setStreamSize: (self streamSize max: self position)
  1613. !
  1614. cr
  1615. ^self nextPutAll: String cr
  1616. !
  1617. crlf
  1618. ^self nextPutAll: String crlf
  1619. !
  1620. lf
  1621. ^self nextPutAll: String lf
  1622. ! !
  1623. Object subclass: #ClassCommentReader
  1624. instanceVariableNames: 'class chunkParser'
  1625. category: 'Kernel'!
  1626. !ClassCommentReader methodsFor: 'accessing'!
  1627. class: aClass
  1628. class := aClass
  1629. ! !
  1630. !ClassCommentReader methodsFor: 'fileIn'!
  1631. scanFrom: aStream
  1632. | nextChunk |
  1633. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  1634. nextChunk isEmptyChunk ifFalse: [
  1635. self setComment: nextChunk contents].
  1636. ! !
  1637. !ClassCommentReader methodsFor: 'initialization'!
  1638. initialize
  1639. super initialize.
  1640. chunkParser := ChunkParser new.
  1641. ! !
  1642. !ClassCommentReader methodsFor: 'private'!
  1643. setComment: aString
  1644. class comment: aString
  1645. ! !
  1646. Object subclass: #Random
  1647. instanceVariableNames: ''
  1648. category: 'Kernel'!
  1649. !Random methodsFor: 'accessing'!
  1650. next
  1651. {'return Math.random()'}
  1652. !
  1653. next: anInteger
  1654. ^1 to: anInteger collect: [:each | self next]
  1655. ! !
  1656. Object subclass: #Point
  1657. instanceVariableNames: 'x y'
  1658. category: 'Kernel'!
  1659. !Point methodsFor: 'accessing'!
  1660. x
  1661. ^x
  1662. !
  1663. y
  1664. ^y
  1665. !
  1666. y: aNumber
  1667. y := aNumber
  1668. !
  1669. x: aNumber
  1670. x := aNumber
  1671. ! !
  1672. !Point methodsFor: 'arithmetic'!
  1673. * aPoint
  1674. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  1675. !
  1676. + aPoint
  1677. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  1678. !
  1679. - aPoint
  1680. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  1681. !
  1682. / aPoint
  1683. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  1684. ! !
  1685. !Point methodsFor: 'converting'!
  1686. asPoint
  1687. ^self
  1688. ! !
  1689. !Point class methodsFor: 'instance creation'!
  1690. x: aNumber y: anotherNumber
  1691. ^self new
  1692. x: aNumber;
  1693. y: anotherNumber;
  1694. yourself
  1695. ! !
  1696. Object subclass: #Message
  1697. instanceVariableNames: 'selector arguments'
  1698. category: 'Kernel'!
  1699. !Message methodsFor: 'accessing'!
  1700. selector
  1701. ^selector
  1702. !
  1703. selector: aString
  1704. selector := aString
  1705. !
  1706. arguments: anArray
  1707. arguments := anArray
  1708. !
  1709. arguments
  1710. ^arguments
  1711. ! !
  1712. !Message class methodsFor: 'instance creation'!
  1713. selector: aString arguments: anArray
  1714. ^self new
  1715. selector: aString;
  1716. arguments: anArray;
  1717. yourself
  1718. ! !
  1719. Error subclass: #MessageNotUnderstood
  1720. instanceVariableNames: 'message receiver'
  1721. category: 'Kernel'!
  1722. !MessageNotUnderstood methodsFor: 'accessing'!
  1723. message
  1724. ^message
  1725. !
  1726. message: aMessage
  1727. message := aMessage
  1728. !
  1729. receiver
  1730. ^receiver
  1731. !
  1732. receiver: anObject
  1733. receiver := anObject
  1734. !
  1735. messageText
  1736. ^self receiver asString, ' does not understand #', self message selector
  1737. ! !