kernel.st 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853
  1. nil subclass: #Object
  2. instanceVariableNames: ''
  3. category: 'Kernel'!
  4. !Object methodsFor: 'accessing'!
  5. basicAt: aString
  6. ^{'
  7. var value = self[aString];
  8. if(typeof(value) == ''undefined'') {
  9. return nil;
  10. } else {
  11. return value;
  12. }
  13. '}
  14. !
  15. basicAt: aString put: anObject
  16. ^{'return self[aString] = anObject'}
  17. !
  18. basicDelete: aString
  19. {'delete self[aString]'}
  20. !
  21. class
  22. ^{'return self.klass'}
  23. !
  24. instVarAt: aString
  25. ^{'
  26. var value = self[''@''+aString];
  27. if(typeof(value) == ''undefined'') {
  28. return nil;
  29. } else {
  30. return value;
  31. }
  32. '}
  33. !
  34. instVarAt: aString put: anObject
  35. ^{'self[''@'' + aString] = anObject'}
  36. !
  37. size
  38. self error: 'Object not indexable'
  39. !
  40. yourself
  41. ^self
  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. asJavascript
  55. ^self asString
  56. !
  57. asString
  58. ^self printString
  59. ! !
  60. !Object methodsFor: 'copying'!
  61. copy
  62. ^self shallowCopy postCopy
  63. !
  64. deepCopy
  65. {'
  66. var copy = self.klass._new();
  67. for(var i in self) {
  68. if(/^@.+/.test(i)) {
  69. copy[i] = self[i]._deepCopy();
  70. }
  71. }
  72. return copy;
  73. '}.
  74. !
  75. postCopy
  76. !
  77. shallowCopy
  78. ^{'
  79. var copy = self.klass._new();
  80. for(var i in self) {
  81. if(/^@.+/.test(i)) {
  82. copy[i] = self[i];
  83. }
  84. }
  85. return copy;
  86. '}
  87. ! !
  88. !Object methodsFor: 'error handling'!
  89. error: aString
  90. Error signal: aString
  91. !
  92. perform: aSymbol
  93. ^self perform: aSymbol withArguments: #()
  94. !
  95. perform: aSymbol withArguments: aCollection
  96. ^self basicPerform: aSymbol asSelector withArguments: aCollection
  97. !
  98. basicPerform: aSymbol
  99. ^self basicPerform: aSymbol withArguments: #()
  100. !
  101. basicPerform: aSymbol withArguments: aCollection
  102. ^{'return self[aSymbol].apply(self, aCollection);'}
  103. !
  104. shouldNotImplement
  105. self error: 'This method should not be implemented in ', self class name
  106. !
  107. subclassResponsibility
  108. self error: 'This method is a responsibility of a subclass'
  109. !
  110. try: aBlock catch: anotherBlock
  111. {'try{aBlock()} catch(e) {anotherBlock(e)}'}
  112. ! !
  113. !Object methodsFor: 'initialization'!
  114. initialize
  115. ! !
  116. !Object methodsFor: 'message handling'!
  117. perform: aSymbol
  118. ^self perform: aSymbol withArguments: #()
  119. ! !
  120. !Object methodsFor: 'printing'!
  121. printNl
  122. {'console.log(self)'}
  123. !
  124. printString
  125. ^'a ', self class name
  126. ! !
  127. !Object methodsFor: 'testing'!
  128. ifNil: aBlock
  129. ^self
  130. !
  131. ifNil: aBlock ifNotNil: anotherBlock
  132. ^anotherBlock value
  133. !
  134. ifNotNil: aBlock
  135. ^aBlock value
  136. !
  137. ifNotNil: aBlock ifNil: anotherBlock
  138. ^aBlock value
  139. !
  140. isClass
  141. ^false
  142. !
  143. isKindOf: aClass
  144. ^(self isMemberOf: aClass)
  145. ifTrue: [true]
  146. ifFalse: [self class inheritsFrom: aClass]
  147. !
  148. isMemberOf: aClass
  149. ^self class = aClass
  150. !
  151. isMetaclass
  152. ^false
  153. !
  154. isNil
  155. ^false
  156. !
  157. isNumber
  158. ^false
  159. !
  160. isParseFailure
  161. ^false
  162. !
  163. isString
  164. ^false
  165. !
  166. notNil
  167. ^self isNil not
  168. ! !
  169. !Object methodsFor: '*Canvas'!
  170. appendToBrush: aTagBrush
  171. aTagBrush append: self asString
  172. ! !
  173. Object subclass: #Smalltalk
  174. instanceVariableNames: ''
  175. category: 'Kernel'!
  176. Smalltalk class instanceVariableNames: 'current'!
  177. !Smalltalk class methodsFor: 'accessing'!
  178. current
  179. ^{'return smalltalk'}
  180. ! !
  181. !Smalltalk methodsFor: 'accessing'!
  182. classes
  183. ^{'return self.classes()'}
  184. ! !
  185. Object subclass: #Behavior
  186. instanceVariableNames: ''
  187. category: 'Kernel'!
  188. !Behavior methodsFor: 'accessing'!
  189. addCompiledMethod: aMethod
  190. {'self.fn.prototype[aMethod.selector._asSelector()] = aMethod.fn;
  191. self.fn.prototype.methods[aMethod.selector] = aMethod'}
  192. !
  193. removeCompiledMethod: aMethod
  194. {'delete self.fn.prototype[aMethod.selector._asSelector()];
  195. delete self.fn.prototype.methods[aMethod.selector]'}
  196. !
  197. allSubclasses
  198. | result |
  199. result := self subclasses.
  200. self subclasses do: [:each |
  201. result addAll: each allSubclasses].
  202. ^result
  203. !
  204. instanceVariableNames
  205. ^{'return self.iVarNames'}
  206. !
  207. methodDictionary
  208. ^{'var dict = smalltalk.Dictionary._new();
  209. var methods = self.fn.prototype.methods;
  210. for(var i in methods) {
  211. if(methods[i].selector) {
  212. dict._at_put_(methods[i].selector, methods[i]);
  213. }
  214. };
  215. return dict'}
  216. !
  217. methodsFor: aString
  218. ^ClassCategoryReader new
  219. class: self category: aString;
  220. yourself
  221. !
  222. comment
  223. ^(self basicAt: 'comment') ifNil: ['']
  224. !
  225. comment: aString
  226. self basicAt: 'comment' put: aString
  227. !
  228. commentStamp
  229. ^ClassCommentReader new
  230. class: self;
  231. yourself
  232. !
  233. name
  234. ^{'return self.className || nil'}
  235. !
  236. prototype
  237. ^{'return self.fn.prototype'}
  238. !
  239. subclasses
  240. ^{'return smalltalk.subclasses(self)'}
  241. !
  242. superclass
  243. ^{'return self.superclass || nil'}
  244. !
  245. withAllSubclasses
  246. ^(Array with: self) addAll: self allSubclasses; yourself
  247. ! !
  248. !Behavior methodsFor: 'instance creation'!
  249. basicNew
  250. ^{'return new self.fn();'}
  251. !
  252. new
  253. ^self basicNew
  254. initialize;
  255. yourself
  256. ! !
  257. Behavior subclass: #Class
  258. instanceVariableNames: ''
  259. category: 'Kernel'!
  260. !Class methodsFor: 'accessing'!
  261. category
  262. ^{'return self.category'}
  263. !
  264. category: aString
  265. {'self.category = aString'}
  266. ! !
  267. !Class methodsFor: 'class creation'!
  268. subclass: aString instanceVariableNames: anotherString
  269. ^self subclass: aString instanceVariableNames: anotherString category: nil
  270. !
  271. subclass: aString instanceVariableNames: aString2 category: aString3
  272. ^ClassBuilder new
  273. superclass: self subclass: aString instanceVariableNames: aString2 category: aString3
  274. ! !
  275. !Class methodsFor: 'printing'!
  276. printString
  277. ^self name
  278. ! !
  279. !Class methodsFor: 'testing'!
  280. isClass
  281. ^true
  282. ! !
  283. Behavior subclass: #Metaclass
  284. instanceVariableNames: ''
  285. category: 'Kernel'!
  286. !Metaclass methodsFor: 'accessing'!
  287. instanceClass
  288. ^{'return self.instanceClass'}
  289. !
  290. instanceVariableNames: aCollection
  291. ClassBuilder new
  292. class: self instanceVariableNames: aCollection
  293. ! !
  294. !Metaclass methodsFor: 'printing'!
  295. printString
  296. ^self instanceClass name, ' class'
  297. ! !
  298. !Metaclass methodsFor: 'testing'!
  299. isMetaclass
  300. ^true
  301. ! !
  302. Object subclass: #CompiledMethod
  303. instanceVariableNames: ''
  304. category: 'Kernel'!
  305. !CompiledMethod methodsFor: 'accessing'!
  306. category
  307. ^(self basicAt: 'category') ifNil: ['']
  308. !
  309. category: aString
  310. self basicAt: 'category' put: aString
  311. !
  312. fn
  313. ^self basicAt: 'fn'
  314. !
  315. fn: aBlock
  316. self basicAt: 'fn' put: aBlock
  317. !
  318. selector
  319. ^self basicAt: 'selector'
  320. !
  321. selector: aString
  322. self basicAt: 'selector' put: aString
  323. !
  324. source
  325. ^(self basicAt: 'source') ifNil: ['']
  326. !
  327. source: aString
  328. self basicAt: 'source' put: aString
  329. ! !
  330. Object subclass: #Number
  331. instanceVariableNames: ''
  332. category: 'Kernel'!
  333. !Number methodsFor: 'arithmetic'!
  334. * aNumber
  335. ^{'return self * aNumber'}
  336. !
  337. + aNumber
  338. ^{'return self + aNumber'}
  339. !
  340. - aNumber
  341. ^{'return self - aNumber'}
  342. !
  343. / aNumber
  344. ^{'return self / aNumber'}
  345. !
  346. max: aNumber
  347. ^{'return Math.max(self, aNumber);'}
  348. !
  349. min: aNumber
  350. ^{'return Math.min(self, aNumber);'}
  351. ! !
  352. !Number methodsFor: 'comparing'!
  353. < aNumber
  354. ^{'return self < aNumber'}
  355. !
  356. <= aNumber
  357. ^{'return self <= aNumber'}
  358. !
  359. = aNumber
  360. ^{'return Number(self) == aNumber'}
  361. !
  362. > aNumber
  363. ^{'return self > aNumber'}
  364. !
  365. >= aNumber
  366. ^{'return self >= aNumber'}
  367. ! !
  368. !Number methodsFor: 'converting'!
  369. asJavascript
  370. ^'(', self printString, ')'
  371. !
  372. asString
  373. ^self printString
  374. !
  375. rounded
  376. ^{'return Math.round(self);'}
  377. !
  378. to: aNumber
  379. | array first last count |
  380. first := self truncated.
  381. last := aNumber truncated + 1.
  382. count := 1.
  383. (first <= last) ifFalse: [self error: 'Wrong interval'].
  384. array := Array new.
  385. (last - first) timesRepeat: [
  386. array at: count put: first.
  387. count := count + 1.
  388. first := first + 1].
  389. ^array
  390. !
  391. atRandom
  392. ^(Random new next * self) truncated + 1
  393. !
  394. truncated
  395. ^{'return Math.floor(self);'}
  396. ! !
  397. !Number methodsFor: 'enumerating'!
  398. timesRepeat: aBlock
  399. | integer count |
  400. integer := self truncated.
  401. count := 1.
  402. [count > self] whileFalse: [
  403. aBlock value.
  404. count := count + 1]
  405. !
  406. to: aNumber do: aBlock
  407. ^(self to: aNumber) do: aBlock
  408. ! !
  409. !Number methodsFor: 'interval'!
  410. clearInterval
  411. {'clearInterval(Number(self))'}
  412. ! !
  413. !Number methodsFor: 'printing'!
  414. printString
  415. ^{'return String(self)'}
  416. ! !
  417. !Number methodsFor: 'testing'!
  418. isNumber
  419. ^true
  420. ! !
  421. Object subclass: #Random
  422. instanceVariableNames: ''
  423. category: 'Kernel'
  424. !
  425. !Random methodsFor: 'accessing'!
  426. next
  427. ^{'return Math.random()'}
  428. !
  429. next: anInteger
  430. ^(1 to: anInteger) collect: [:each | self next]
  431. ! !
  432. Object subclass: #BlockClosure
  433. instanceVariableNames: ''
  434. category: 'Kernel'!
  435. !BlockClosure methodsFor: 'accessing'!
  436. compiledSource
  437. ^{'return self.toString()'}
  438. ! !
  439. !BlockClosure methodsFor: 'controlling'!
  440. whileFalse: aBlock
  441. {'while(!!self()) {aBlock()}'}
  442. !
  443. whileTrue: aBlock
  444. {'while(self()) {aBlock()}'}
  445. ! !
  446. !BlockClosure methodsFor: 'error handling'!
  447. on: anErrorClass do: aBlock
  448. self try: self catch: [:error |
  449. (error isKindOf: anErrorClass)
  450. ifTrue: [aBlock value]
  451. ifFalse: [error signal]]
  452. ! !
  453. !BlockClosure methodsFor: 'evaluating'!
  454. value
  455. ^{'return self();'}
  456. !
  457. value: anArg
  458. ^{'return self(anArg);'}
  459. !
  460. value: firstArg value: secondArg
  461. ^{'return self(firstArg, secondArg);'}
  462. !
  463. value: firstArg value: secondArg value: thirdArg
  464. ^{'return self(firstArg, secondArg, thirdArg);'}
  465. !
  466. valueWithPossibleArguments: aCollection
  467. ^{'return self.apply(null, aCollection);'}
  468. ! !
  469. !BlockClosure methodsFor: 'timeout/interval'!
  470. valueWithTimeout: aNumber
  471. {'setTimeout(self, aNumber)'}
  472. !
  473. valueWithInterval: aNumber
  474. ^{'return setInterval(self, aNumber)'}
  475. ! !
  476. !BlockClosure methodsFor: '*JQuery'!
  477. appendToJQuery: aJQuery
  478. | canvas |
  479. canvas := HTMLCanvas new.
  480. self value: canvas.
  481. aJQuery append: canvas
  482. ! !
  483. !BlockClosure methodsFor: '*Canvas'!
  484. appendToBrush: aTagBrush
  485. aTagBrush appendBlock: self
  486. ! !
  487. Object subclass: #Boolean
  488. instanceVariableNames: ''
  489. category: 'Kernel'!
  490. !Boolean methodsFor: 'comparing'!
  491. = aBoolean
  492. ^{'return Boolean(self == true) == aBoolean'}
  493. ! !
  494. !Boolean methodsFor: 'controlling'!
  495. and: aBlock
  496. ^self = true
  497. ifTrue: aBlock
  498. ifFalse: [false]
  499. !
  500. ifFalse: aBlock
  501. ^self ifTrue: [] ifFalse: aBlock
  502. !
  503. ifFalse: aBlock ifTrue: anotherBlock
  504. ^self ifTrue: anotherBlock ifFalse: aBlock
  505. !
  506. ifTrue: aBlock
  507. ^self ifTrue: aBlock ifFalse: []
  508. !
  509. ifTrue: aBlock ifFalse: anotherBlock
  510. ^{'
  511. if(self == true) {
  512. return aBlock();
  513. } else {
  514. return anotherBlock();
  515. }
  516. '}
  517. !
  518. not
  519. ^self = false
  520. !
  521. or: aBlock
  522. ^self = true
  523. ifTrue: [true]
  524. ifFalse: aBlock
  525. ! !
  526. !Boolean methodsFor: 'copying'!
  527. deepCopy
  528. ^self
  529. !
  530. shallowCopy
  531. ^self
  532. ! !
  533. !Boolean methodsFor: 'printing'!
  534. printString
  535. ^{'return self.toString()'}
  536. ! !
  537. Object subclass: #UndefinedObject
  538. instanceVariableNames: ''
  539. category: 'Kernel'!
  540. !UndefinedObject class methodsFor: 'instance creation'!
  541. new
  542. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  543. ! !
  544. !UndefinedObject methodsFor: 'class creation'!
  545. subclass: aString instanceVariableNames: anotherString
  546. ^self subclass: aString instanceVariableNames: anotherString category: nil
  547. !
  548. subclass: aString instanceVariableNames: aString2 category: aString3
  549. ^ClassBuilder new
  550. superclass: self subclass: aString instanceVariableNames: aString2 category: aString3
  551. ! !
  552. !UndefinedObject methodsFor: 'copying'!
  553. deepCopy
  554. ^self
  555. !
  556. shallowCopy
  557. ^self
  558. ! !
  559. !UndefinedObject methodsFor: 'printing'!
  560. printString
  561. ^'nil'
  562. ! !
  563. !UndefinedObject methodsFor: 'testing'!
  564. ifNil: aBlock
  565. ^self ifNil: aBlock ifNotNil: []
  566. !
  567. ifNil: aBlock ifNotNil: anotherBlock
  568. ^aBlock value
  569. !
  570. ifNotNil: aBlock
  571. ^self
  572. !
  573. ifNotNil: aBlock ifNil: anotherBlock
  574. ^anotherBlock value
  575. !
  576. isNil
  577. ^true
  578. !
  579. notNil
  580. ^false
  581. ! !
  582. Object subclass: #Error
  583. instanceVariableNames: 'messageText'
  584. category: 'Kernel'!
  585. !Error class methodsFor: 'instance creation'!
  586. signal: aString
  587. ^self new
  588. messageText: aString;
  589. signal
  590. ! !
  591. !Error methodsFor: 'accessing'!
  592. messageText
  593. ^messageText
  594. !
  595. messageText: aString
  596. messageText := aString
  597. ! !
  598. !Error methodsFor: 'signaling'!
  599. signal
  600. ^{'throw({smalltalkError: self})'}
  601. ! !
  602. Object subclass: #Collection
  603. instanceVariableNames: ''
  604. category: 'Kernel'!
  605. !Collection class methodsFor: 'accessing'!
  606. streamClass
  607. ^Stream
  608. ! !
  609. !Collection class methodsFor: 'instance creation'!
  610. with: anObject
  611. ^self new
  612. add: anObject;
  613. yourself
  614. !
  615. with: anObject with: anotherObject
  616. ^self new
  617. add: anObject;
  618. add: anotherObject;
  619. yourself
  620. !
  621. with: firstObject with: secondObject with: thirdObject
  622. ^self new
  623. add: firstObject;
  624. add: secondObject;
  625. add: thirdObject;
  626. yourself
  627. !
  628. withAll: aCollection
  629. ^self new
  630. addAll: aCollection;
  631. yourself
  632. ! !
  633. !Collection methodsFor: 'accessing'!
  634. at: anIndex
  635. ^self at: anIndex ifAbsent: [
  636. self errorNotFound]
  637. !
  638. at: anIndex ifAbsent: aBlock
  639. self subclassResponsibility
  640. !
  641. at: anIndex put: anObject
  642. self subclassResponsibility
  643. !
  644. first
  645. ^self at: 1
  646. !
  647. fourth
  648. ^self at: 4
  649. !
  650. last
  651. ^self at: self size
  652. !
  653. readStream
  654. ^self stream
  655. !
  656. second
  657. ^self at: 2
  658. !
  659. size
  660. self subclassResponsibility
  661. !
  662. stream
  663. ^self streamClass on: self
  664. !
  665. streamClass
  666. ^self class streamClass
  667. !
  668. third
  669. ^self at: 3
  670. !
  671. writeStream
  672. ^self stream
  673. ! !
  674. !Collection methodsFor: 'adding/removing'!
  675. add: anObject
  676. self subclassResponsibility
  677. !
  678. addAll: aCollection
  679. aCollection do: [:each |
  680. self add: each].
  681. ^aCollection
  682. !
  683. remove: anObject
  684. self subclassResponsibility
  685. ! !
  686. !Collection methodsFor: 'converting'!
  687. asArray
  688. | array index |
  689. array := Array new.
  690. index := 0.
  691. self do: [:each |
  692. index := index + 1.
  693. array at: index put: each].
  694. ^array
  695. ! !
  696. !Collection methodsFor: 'copying'!
  697. , aCollection
  698. ^self copy
  699. addAll: aCollection;
  700. yourself
  701. !
  702. copyFrom: anIndex to: anotherIndex
  703. self subclassResponsibility
  704. !
  705. copyWith: anObject
  706. ^self copy add: anObject; yourself
  707. !
  708. copyWithAll: aCollection
  709. ^self copy addAll: aCollection; yourself
  710. ! !
  711. !Collection methodsFor: 'enumerating'!
  712. collect: aBlock
  713. | stream |
  714. stream := self class new writeStream.
  715. self do: [:each |
  716. stream nextPut: (aBlock value: each)].
  717. ^stream contents
  718. !
  719. detect: aBlock
  720. ^self detect: aBlock ifNone: [self errorNotFound]
  721. !
  722. detect: aBlock ifNone: anotherBlock
  723. ^{'
  724. for(var i = 0; i < self.length; i++)
  725. if(aBlock(self[i]))
  726. return self[i];
  727. return anotherBlock();
  728. '}
  729. !
  730. do: aBlock
  731. {'for(var i=0;i<self.length;i++){aBlock(self[i]);}'}
  732. !
  733. do: aBlock separatedBy: anotherBlock
  734. | first |
  735. first := true.
  736. self do: [:each |
  737. first
  738. ifTrue: [first := false]
  739. ifFalse: [anotherBlock value].
  740. aBlock value: each]
  741. !
  742. inject: anObject into: aBlock
  743. | result |
  744. result := anObject.
  745. self do: [:each |
  746. result := aBlock value: result value: each].
  747. ^result
  748. !
  749. reject: aBlock
  750. ^self select: [:each | (aBlock value: each) = false]
  751. !
  752. select: aBlock
  753. | stream |
  754. stream := self class new writeStream.
  755. self do: [:each |
  756. (aBlock value: each) ifTrue: [
  757. stream nextPut: each]].
  758. ^stream contents
  759. ! !
  760. !Collection methodsFor: 'error handling'!
  761. errorNotFound
  762. self error: 'Object is not in the collection'
  763. ! !
  764. !Collection methodsFor: 'testing'!
  765. includes: anObject
  766. ^{'
  767. var i = self.length;
  768. while (i--) {
  769. if (self[i].__eq(anObject)) {
  770. return true;
  771. }
  772. }
  773. return false;
  774. '}
  775. !
  776. isEmpty
  777. ^self size = 0
  778. !
  779. notEmpty
  780. ^self isEmpty not
  781. ! !
  782. Collection subclass: #String
  783. instanceVariableNames: ''
  784. category: 'Kernel'!
  785. !String class methodsFor: 'accessing'!
  786. cr
  787. ^{'{return ''\n''};'}
  788. !
  789. lf
  790. ^{'return ''\r'';'}
  791. !
  792. space
  793. ^{'return '' '';'}
  794. !
  795. streamClass
  796. ^StringStream
  797. !
  798. tab
  799. ^{'return ''\t'';'}
  800. ! !
  801. !String class methodsFor: 'instance creation'!
  802. fromString: aString
  803. ^{'return new self.fn(aString);'}
  804. ! !
  805. !String methodsFor: 'accessing'!
  806. at: anIndex
  807. ^{'return self[anIndex - 1] || nil;'}
  808. !
  809. at: anIndex ifAbsent: aBlock
  810. (self at: anIndex) ifNil: [aBlock]
  811. !
  812. at: anIndex put: anObject
  813. self errorReadOnly
  814. !
  815. escaped
  816. ^{'return escape(self)'}
  817. !
  818. size
  819. ^{'return self.length'}
  820. !
  821. unescaped
  822. ^{'return unescape(self)'}
  823. ! !
  824. !String methodsFor: 'adding'!
  825. add: anObject
  826. self errorReadOnly
  827. !
  828. remove: anObject
  829. self errorReadOnly
  830. ! !
  831. !String methodsFor: 'comparing'!
  832. = aString
  833. ^{'return String(self) == aString'}
  834. !
  835. > aString
  836. ^{'return String(self) > aString'}
  837. !
  838. < aString
  839. ^{'return String(self) < aString'}
  840. !
  841. >= aString
  842. ^{'return String(self) >= aString'}
  843. !
  844. <= aString
  845. ^{'return String(self) <= aString'}
  846. ! !
  847. !String methodsFor: 'converting'!
  848. asCharacterParser
  849. ^PPCharacterParser new string: self
  850. !
  851. asChoiceParser
  852. ^PPChoiceParser withAll: (self asArray collect: [:each | each asParser])
  853. !
  854. asJavascript
  855. ^{'
  856. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  857. return "unescape(\"" + escape(self) + "\")";
  858. else
  859. return "\"" + self + "\"";
  860. '}
  861. !
  862. asNumber
  863. ^{'return Number(self);'}
  864. !
  865. asParser
  866. ^PPStringParser new string: self
  867. !
  868. asSelector
  869. | selector |
  870. selector := '_', self.
  871. selector := selector replace: ':' with: '_'.
  872. selector := selector replace: '[+]' with: '_plus'.
  873. selector := selector replace: '-' with: '_minus'.
  874. selector := selector replace: '[*]' with: '_star'.
  875. selector := selector replace: '[/]' with: '_slash'.
  876. selector := selector replace: '>' with: '_gt'.
  877. selector := selector replace: '<' with: '_lt'.
  878. selector := selector replace: '=' with: '_eq'.
  879. selector := selector replace: ',' with: '_comma'.
  880. selector := selector replace: '[@]' with: '_at'.
  881. ^selector
  882. !
  883. asString
  884. ^self
  885. !
  886. tokenize: aString
  887. ^{'return self.split(aString)'}
  888. ! !
  889. !String methodsFor: 'copying'!
  890. , aString
  891. ^{'return self + aString'}
  892. !
  893. copyFrom: anIndex to: anotherIndex
  894. ^{'return self.substring(anIndex - 1, anotherIndex);'}
  895. !
  896. deepCopy
  897. ^self shallowCopy
  898. !
  899. shallowCopy
  900. ^self class fromString: self
  901. ! !
  902. !String methodsFor: 'error handling'!
  903. errorReadOnly
  904. self error: 'Object is read-only'
  905. ! !
  906. !String methodsFor: 'printing'!
  907. printNl
  908. {'console.log(self)'}
  909. !
  910. printString
  911. ^'''', self, ''''
  912. ! !
  913. !String methodsFor: 'regular expressions'!
  914. match: aRegexp
  915. ^{'return self.search(aRegexp) !!= -1'}
  916. !
  917. replace: aString with: anotherString
  918. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  919. !
  920. replaceRegexp: aRegexp with: aString
  921. ^{'return self.replace(aRegexp, aString);'}
  922. ! !
  923. !String methodsFor: 'testing'!
  924. isString
  925. ^true
  926. ! !
  927. !String methodsFor: '*JQuery'!
  928. asJQuery
  929. ^JQuery fromString: self
  930. !
  931. appendToJQuery: aJQuery
  932. {'aJQuery._appendElement_(String(self))'}
  933. ! !
  934. !String methodsFor: '*Canvas'!
  935. appendToBrush: aTagBrush
  936. aTagBrush appendString: self
  937. ! !
  938. Object subclass: #RegularExpression
  939. instanceVariableNames: ''
  940. category: 'Kernel'!
  941. !RegularExpression class methodsFor: 'instance creation'!
  942. fromString: aString
  943. ^self fromString: aString flag: ''
  944. !
  945. fromString: aString flag: anotherString
  946. ^{'return new RegExp(aString, anotherString);'}
  947. ! !
  948. !RegularExpression methodsFor: 'evaluating'!
  949. compile: aString
  950. ^{'return self.compile(aString);'}
  951. !
  952. exec: aString
  953. ^{'return self.exec(aString);'}
  954. !
  955. test: aString
  956. ^{'return self.test(aString);'}
  957. ! !
  958. Collection subclass: #Array
  959. instanceVariableNames: ''
  960. category: 'Kernel'!
  961. !Array methodsFor: 'accessing'!
  962. at: anIndex ifAbsent: aBlock
  963. ^{'
  964. var value = self[anIndex - 1];
  965. if(value === undefined) {
  966. return aBlock();
  967. } else {
  968. return value;
  969. }
  970. '}
  971. !
  972. at: anIndex put: anObject
  973. ^{'return self[anIndex - 1] = anObject'}
  974. !
  975. size
  976. ^{'return self.length'}
  977. ! !
  978. !Array methodsFor: 'adding'!
  979. add: anObject
  980. ^{'self.push(anObject); return anObject;'}
  981. !
  982. addLast: anObject
  983. ^self add: anObject
  984. !
  985. remove: anObject
  986. {'for(var i=0;i<self.length;i++) {
  987. if(self[i] == anObject) {
  988. self.splice(i,1);
  989. break;
  990. }
  991. }'}
  992. ! !
  993. !Array methodsFor: 'converting'!
  994. asJavascript
  995. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  996. ! !
  997. !Array methodsFor: 'copying'!
  998. deepCopy
  999. | newCollection |
  1000. newCollection := self class new.
  1001. self do: [:each | newCollection add: each deepCopy].
  1002. ^newCollection
  1003. !
  1004. shallowCopy
  1005. | newCollection |
  1006. newCollection := self class new.
  1007. self do: [:each | newCollection add: each].
  1008. ^newCollection
  1009. !
  1010. copyFrom: anIndex to: anotherIndex
  1011. | array |
  1012. array := self class new.
  1013. anIndex to: anotherIndex do: [:each |
  1014. array add: (self at: each)].
  1015. ^array
  1016. ! !
  1017. !Array methodsFor: 'enumerating'!
  1018. sort
  1019. ^self copy basicPerform: 'sort'
  1020. !
  1021. sort: aBlock
  1022. ^self copy basicPerform: 'sort' withArguments: (Array with: aBlock)
  1023. !
  1024. join: aString
  1025. ^{'return self.join(aString);'}
  1026. ! !
  1027. Object subclass: #Association
  1028. instanceVariableNames: 'key value'
  1029. category: 'Kernel'!
  1030. !Association class methodsFor: 'instance creation'!
  1031. key: aKey value: aValue
  1032. ^self new
  1033. key: aKey;
  1034. value: aValue;
  1035. yourself
  1036. ! !
  1037. !Association methodsFor: 'accessing'!
  1038. key
  1039. ^key
  1040. !
  1041. key: aKey
  1042. key := aKey
  1043. !
  1044. value
  1045. ^value
  1046. !
  1047. value: aValue
  1048. value := aValue
  1049. ! !
  1050. !Association methodsFor: 'comparing'!
  1051. = anAssociation
  1052. ^self class = anAssociation class and: [
  1053. self key = anAssociation key and: [
  1054. self value = anAssociation value]]
  1055. ! !
  1056. Collection subclass: #Dictionary
  1057. instanceVariableNames: 'keys'
  1058. category: 'Kernel'!
  1059. !Dictionary methodsFor: 'accessing'!
  1060. associations
  1061. | associations |
  1062. associations := #().
  1063. keys do: [:each |
  1064. associations add: (Association key: each value: (self at: each))].
  1065. ^associations
  1066. !
  1067. at: aKey ifAbsent: aBlock
  1068. ^(self basicAt: aKey) ifNil: aBlock
  1069. !
  1070. at: aKey ifAbsentPut: aBlock
  1071. ^self at: aKey ifAbsent: [
  1072. self at: aKey put: aBlock value]
  1073. !
  1074. at: aKey ifPresent: aBlock
  1075. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  1076. !
  1077. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  1078. ^(self basicAt: aKey)
  1079. ifNil: anotherBlock
  1080. ifNotNil: [aBlock value: (self at: aKey)]
  1081. !
  1082. at: aKey put: aValue
  1083. (keys includes: aKey) ifFalse: [keys add: aKey].
  1084. ^self basicAt: aKey put: aValue
  1085. !
  1086. keys
  1087. ^keys copy
  1088. !
  1089. size
  1090. ^keys size
  1091. !
  1092. values
  1093. ^keys collect: [:each | self at: each]
  1094. ! !
  1095. !Dictionary methodsFor: 'adding/removing'!
  1096. add: anAssociation
  1097. self at: anAssociation key put: anAssociation value
  1098. !
  1099. addAll: aDictionary
  1100. super addAll: aDictionary associations.
  1101. ^aDictionary
  1102. !
  1103. remove: aKey
  1104. self removeKey: aKey
  1105. !
  1106. removeKey: aKey
  1107. keys remove: aKey
  1108. ! !
  1109. !Dictionary methodsFor: 'comparing'!
  1110. = aDictionary
  1111. self class = aDictionary class ifFalse: [^false].
  1112. self associationsDo: [:assoc |
  1113. (aDictionary at: assoc key ifAbsent: [^false]) = assoc value
  1114. ifFalse: [^false]].
  1115. ^true
  1116. ! !
  1117. !Dictionary methodsFor: 'copying'!
  1118. , aCollection
  1119. self shouldNotImplement
  1120. !
  1121. copyFrom: anIndex to: anotherIndex
  1122. self shouldNotImplement
  1123. !
  1124. shallowCopy
  1125. | copy |
  1126. copy := self class new.
  1127. self associationsDo: [:each |
  1128. copy at: each key put: each value].
  1129. ^copy
  1130. ! !
  1131. !Dictionary methodsFor: 'enumerating'!
  1132. associationsDo: aBlock
  1133. self associations do: aBlock
  1134. !
  1135. collect: aBlock
  1136. | newDict |
  1137. newDict := self class new.
  1138. self keysAndValuesDo: [:key :value |
  1139. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1140. ^newDict
  1141. !
  1142. detect: aBlock ifNone: anotherBlock
  1143. ^self values detect: aBlock ifNone: anotherBlock
  1144. !
  1145. do: aBlock
  1146. self values do: aBlock
  1147. !
  1148. includes: anObject
  1149. ^self values includes: anObject
  1150. !
  1151. keysAndValuesDo: aBlock
  1152. self associationsDo: [:each |
  1153. aBlock value: each key value: each value]
  1154. !
  1155. select: aBlock
  1156. | newDict |
  1157. newDict := self class new.
  1158. self keysAndValuesDo: [:key :value |
  1159. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1160. ^newDict
  1161. ! !
  1162. !Dictionary methodsFor: 'initialization'!
  1163. initialize
  1164. super initialize.
  1165. keys := #()
  1166. ! !
  1167. Object subclass: #ClassBuilder
  1168. instanceVariableNames: ''
  1169. category: 'Kernel'!
  1170. !ClassBuilder methodsFor: 'class creation'!
  1171. class: aClass instanceVariableNames: aString
  1172. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  1173. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  1174. self setupClass: aClass
  1175. !
  1176. superclass: aClass subclass: aString
  1177. self superclass: aClass subclass: aString instanceVariableNames: '' category: nil
  1178. !
  1179. superclass: aClass subclass: aString instanceVariableNames: aString2 category: aString3
  1180. | newClass |
  1181. newClass := self addSubclassOf: aClass named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2).
  1182. self setupClass: newClass.
  1183. newClass category: (aString3 ifNil: ['unclassified'])
  1184. ! !
  1185. !ClassBuilder methodsFor: 'private'!
  1186. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  1187. ^{'smalltalk.addClass(aString, aClass, aCollection);
  1188. return smalltalk[aString]'}
  1189. !
  1190. instanceVariableNamesFor: aString
  1191. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  1192. !
  1193. setupClass: aClass
  1194. {'smalltalk.init(aClass);'}
  1195. ! !
  1196. Object subclass: #ClassCategoryReader
  1197. instanceVariableNames: 'class category chunkParser'
  1198. category: 'Kernel'!
  1199. !ClassCategoryReader methodsFor: 'accessing'!
  1200. class: aClass category: aString
  1201. class := aClass.
  1202. category := aString
  1203. ! !
  1204. !ClassCategoryReader methodsFor: 'fileIn'!
  1205. scanFrom: aStream
  1206. | nextChunk |
  1207. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  1208. nextChunk isEmptyChunk ifFalse: [
  1209. self compileMethod: nextChunk contents.
  1210. self scanFrom: aStream].
  1211. ! !
  1212. !ClassCategoryReader methodsFor: 'initialization'!
  1213. initialize
  1214. super initialize.
  1215. chunkParser := ChunkParser new.
  1216. ! !
  1217. !ClassCategoryReader methodsFor: 'private'!
  1218. compileMethod: aString
  1219. | method |
  1220. method := Compiler new load: aString forClass: class.
  1221. method category: category.
  1222. class addCompiledMethod: method
  1223. ! !
  1224. Object subclass: #ClassCommentReader
  1225. instanceVariableNames: 'class chunkParser'
  1226. category: 'Kernel'!
  1227. !ClassCommentReader methodsFor: 'accessing'!
  1228. class: aClass
  1229. class := aClass
  1230. ! !
  1231. !ClassCommentReader methodsFor: 'fileIn'!
  1232. scanFrom: aStream
  1233. | nextChunk |
  1234. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  1235. nextChunk isEmptyChunk ifFalse: [
  1236. self setComment: nextChunk contents].
  1237. ! !
  1238. !ClassCommentReader methodsFor: 'initialization'!
  1239. initialize
  1240. super initialize.
  1241. chunkParser := ChunkParser new.
  1242. ! !
  1243. !ClassCommentReader methodsFor: 'private'!
  1244. setComment: aString
  1245. class comment: aString
  1246. ! !
  1247. Object subclass: #Stream
  1248. instanceVariableNames: 'collection position streamSize'
  1249. category: 'Kernel'!
  1250. !Stream class methodsFor: 'instance creation'!
  1251. on: aCollection
  1252. ^self new
  1253. setCollection: aCollection;
  1254. setStreamSize: aCollection size;
  1255. yourself
  1256. ! !
  1257. !Stream methodsFor: 'accessing'!
  1258. collection
  1259. ^collection
  1260. !
  1261. contents
  1262. ^self collection
  1263. copyFrom: 1
  1264. to: self streamSize
  1265. !
  1266. position
  1267. ^position ifNil: [position := 0]
  1268. !
  1269. position: anInteger
  1270. position := anInteger
  1271. !
  1272. setCollection: aCollection
  1273. collection := aCollection
  1274. !
  1275. setStreamSize: anInteger
  1276. streamSize := anInteger
  1277. !
  1278. size
  1279. ^self streamSize
  1280. !
  1281. streamSize
  1282. ^streamSize
  1283. ! !
  1284. !Stream methodsFor: 'actions'!
  1285. close
  1286. !
  1287. flush
  1288. !
  1289. reset
  1290. self position: 0
  1291. !
  1292. resetContents
  1293. self reset.
  1294. self setStreamSize: 0
  1295. ! !
  1296. !Stream methodsFor: 'enumerating'!
  1297. do: aBlock
  1298. [self atEnd] whileFalse: [aBlock value: self next]
  1299. ! !
  1300. !Stream methodsFor: 'positioning'!
  1301. setToEnd
  1302. self position: self size
  1303. !
  1304. skip: anInteger
  1305. self position: ((self position + anInteger) min: self size max: 0)
  1306. ! !
  1307. !Stream methodsFor: 'reading'!
  1308. next
  1309. self position: self position + 1.
  1310. ^collection at: self position
  1311. !
  1312. next: anInteger
  1313. | tempCollection |
  1314. tempCollection := self collection class new.
  1315. anInteger timesRepeat: [
  1316. self atEnd ifFalse: [
  1317. tempCollection add: self next]].
  1318. ^tempCollection
  1319. !
  1320. peek
  1321. ^self atEnd ifFalse: [
  1322. self collection at: self position + 1]
  1323. ! !
  1324. !Stream methodsFor: 'testing'!
  1325. atEnd
  1326. ^self position = self size
  1327. !
  1328. atStart
  1329. ^self position = 0
  1330. !
  1331. isEmpty
  1332. ^self size = 0
  1333. ! !
  1334. !Stream methodsFor: 'writing'!
  1335. nextPut: anObject
  1336. self position: self position + 1.
  1337. self collection at: self position put: anObject.
  1338. self setStreamSize: (self streamSize max: self position)
  1339. !
  1340. nextPutAll: aCollection
  1341. aCollection do: [:each |
  1342. self nextPut: each]
  1343. ! !
  1344. Stream subclass: #StringStream
  1345. instanceVariableNames: ''
  1346. category: 'Kernel'!
  1347. !StringStream methodsFor: 'reading'!
  1348. next: anInteger
  1349. | tempCollection |
  1350. tempCollection := self collection class new.
  1351. anInteger timesRepeat: [
  1352. self atEnd ifFalse: [
  1353. tempCollection := tempCollection, self next]].
  1354. ^tempCollection
  1355. ! !
  1356. !StringStream methodsFor: 'writing'!
  1357. nextPut: aString
  1358. self nextPutAll: aString
  1359. !
  1360. nextPutAll: aString
  1361. self setCollection:
  1362. (self collection copyFrom: 1 to: self position),
  1363. aString,
  1364. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  1365. self position: self position + aString size.
  1366. self setStreamSize: (self streamSize max: self position)
  1367. ! !