kernel.st 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  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. truncated
  392. ^{'return Math.floor(self);'}
  393. ! !
  394. !Number methodsFor: 'enumerating'!
  395. timesRepeat: aBlock
  396. | integer count |
  397. integer := self truncated.
  398. count := 1.
  399. [count > self] whileFalse: [
  400. aBlock value.
  401. count := count + 1]
  402. !
  403. to: aNumber do: aBlock
  404. ^(self to: aNumber) do: aBlock
  405. ! !
  406. !Number methodsFor: 'printing'!
  407. printString
  408. ^{'return String(self)'}
  409. ! !
  410. !Number methodsFor: 'testing'!
  411. isNumber
  412. ^true
  413. ! !
  414. Object subclass: #BlockClosure
  415. instanceVariableNames: ''
  416. category: 'Kernel'!
  417. !BlockClosure methodsFor: 'accessing'!
  418. compiledSource
  419. ^{'return self.toString()'}
  420. ! !
  421. !BlockClosure methodsFor: 'controlling'!
  422. whileFalse: aBlock
  423. {'while(!!self()) {aBlock()}'}
  424. !
  425. whileTrue: aBlock
  426. {'while(self()) {aBlock()}'}
  427. ! !
  428. !BlockClosure methodsFor: 'error handling'!
  429. on: anErrorClass do: aBlock
  430. self try: self catch: [:error |
  431. (error isKindOf: anErrorClass)
  432. ifTrue: [aBlock value]
  433. ifFalse: [error signal]]
  434. ! !
  435. !BlockClosure methodsFor: 'evaluating'!
  436. value
  437. ^{'return self();'}
  438. !
  439. value: anArg
  440. ^{'return self(anArg);'}
  441. !
  442. value: firstArg value: secondArg
  443. ^{'return self(firstArg, secondArg);'}
  444. !
  445. value: firstArg value: secondArg value: thirdArg
  446. ^{'return self(firstArg, secondArg, thirdArg);'}
  447. !
  448. valueWithPossibleArguments: aCollection
  449. ^{'return self.apply(null, aCollection);'}
  450. ! !
  451. !BlockClosure methodsFor: '*JQuery'!
  452. appendToJQuery: aJQuery
  453. | canvas |
  454. canvas := HTMLCanvas new.
  455. self value: canvas.
  456. aJQuery append: canvas
  457. ! !
  458. !BlockClosure methodsFor: '*Canvas'!
  459. appendToBrush: aTagBrush
  460. aTagBrush appendBlock: self
  461. ! !
  462. Object subclass: #Boolean
  463. instanceVariableNames: ''
  464. category: 'Kernel'!
  465. !Boolean methodsFor: 'comparing'!
  466. = aBoolean
  467. ^{'return Boolean(self == true) == aBoolean'}
  468. ! !
  469. !Boolean methodsFor: 'controlling'!
  470. and: aBlock
  471. ^self = true
  472. ifTrue: aBlock
  473. ifFalse: [false]
  474. !
  475. ifFalse: aBlock
  476. ^self ifTrue: [] ifFalse: aBlock
  477. !
  478. ifFalse: aBlock ifTrue: anotherBlock
  479. ^self ifTrue: anotherBlock ifFalse: aBlock
  480. !
  481. ifTrue: aBlock
  482. ^self ifTrue: aBlock ifFalse: []
  483. !
  484. ifTrue: aBlock ifFalse: anotherBlock
  485. ^{'
  486. if(self == true) {
  487. return aBlock();
  488. } else {
  489. return anotherBlock();
  490. }
  491. '}
  492. !
  493. not
  494. ^self = false
  495. !
  496. or: aBlock
  497. ^self = true
  498. ifTrue: [true]
  499. ifFalse: aBlock
  500. ! !
  501. !Boolean methodsFor: 'copying'!
  502. deepCopy
  503. ^self
  504. !
  505. shallowCopy
  506. ^self
  507. ! !
  508. !Boolean methodsFor: 'printing'!
  509. printString
  510. ^{'return self.toString()'}
  511. ! !
  512. Object subclass: #UndefinedObject
  513. instanceVariableNames: ''
  514. category: 'Kernel'!
  515. !UndefinedObject class methodsFor: 'instance creation'!
  516. new
  517. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  518. ! !
  519. !UndefinedObject methodsFor: 'class creation'!
  520. subclass: aString instanceVariableNames: anotherString
  521. ^self subclass: aString instanceVariableNames: anotherString category: nil
  522. !
  523. subclass: aString instanceVariableNames: aString2 category: aString3
  524. ^ClassBuilder new
  525. superclass: self subclass: aString instanceVariableNames: aString2 category: aString3
  526. ! !
  527. !UndefinedObject methodsFor: 'copying'!
  528. deepCopy
  529. ^self
  530. !
  531. shallowCopy
  532. ^self
  533. ! !
  534. !UndefinedObject methodsFor: 'printing'!
  535. printString
  536. ^'nil'
  537. ! !
  538. !UndefinedObject methodsFor: 'testing'!
  539. ifNil: aBlock
  540. ^self ifNil: aBlock ifNotNil: []
  541. !
  542. ifNil: aBlock ifNotNil: anotherBlock
  543. ^aBlock value
  544. !
  545. ifNotNil: aBlock
  546. ^self
  547. !
  548. ifNotNil: aBlock ifNil: anotherBlock
  549. ^anotherBlock value
  550. !
  551. isNil
  552. ^true
  553. !
  554. notNil
  555. ^false
  556. ! !
  557. Object subclass: #Error
  558. instanceVariableNames: 'messageText'
  559. category: 'Kernel'!
  560. !Error class methodsFor: 'instance creation'!
  561. signal: aString
  562. ^self new
  563. messageText: aString;
  564. signal
  565. ! !
  566. !Error methodsFor: 'accessing'!
  567. messageText
  568. ^messageText
  569. !
  570. messageText: aString
  571. messageText := aString
  572. ! !
  573. !Error methodsFor: 'signaling'!
  574. signal
  575. ^{'throw({smalltalkError: self})'}
  576. ! !
  577. Object subclass: #Collection
  578. instanceVariableNames: ''
  579. category: 'Kernel'!
  580. !Collection class methodsFor: 'accessing'!
  581. streamClass
  582. ^Stream
  583. ! !
  584. !Collection class methodsFor: 'instance creation'!
  585. with: anObject
  586. ^self new
  587. add: anObject;
  588. yourself
  589. !
  590. with: anObject with: anotherObject
  591. ^self new
  592. add: anObject;
  593. add: anotherObject;
  594. yourself
  595. !
  596. with: firstObject with: secondObject with: thirdObject
  597. ^self new
  598. add: firstObject;
  599. add: secondObject;
  600. add: thirdObject;
  601. yourself
  602. !
  603. withAll: aCollection
  604. ^self new
  605. addAll: aCollection;
  606. yourself
  607. ! !
  608. !Collection methodsFor: 'accessing'!
  609. at: anIndex
  610. ^self at: anIndex ifAbsent: [
  611. self errorNotFound]
  612. !
  613. at: anIndex ifAbsent: aBlock
  614. self subclassResponsibility
  615. !
  616. at: anIndex put: anObject
  617. self subclassResponsibility
  618. !
  619. first
  620. ^self at: 1
  621. !
  622. fourth
  623. ^self at: 4
  624. !
  625. last
  626. ^self at: self size
  627. !
  628. readStream
  629. ^self stream
  630. !
  631. second
  632. ^self at: 2
  633. !
  634. size
  635. self subclassResponsibility
  636. !
  637. stream
  638. ^self streamClass on: self
  639. !
  640. streamClass
  641. ^self class streamClass
  642. !
  643. third
  644. ^self at: 3
  645. !
  646. writeStream
  647. ^self stream
  648. ! !
  649. !Collection methodsFor: 'adding/removing'!
  650. add: anObject
  651. self subclassResponsibility
  652. !
  653. addAll: aCollection
  654. aCollection do: [:each |
  655. self add: each].
  656. ^aCollection
  657. !
  658. remove: anObject
  659. self subclassResponsibility
  660. ! !
  661. !Collection methodsFor: 'converting'!
  662. asArray
  663. | array index |
  664. array := Array new.
  665. index := 0.
  666. self do: [:each |
  667. index := index + 1.
  668. array at: index put: each].
  669. ^array
  670. ! !
  671. !Collection methodsFor: 'copying'!
  672. , aCollection
  673. ^self copy
  674. addAll: aCollection;
  675. yourself
  676. !
  677. copyFrom: anIndex to: anotherIndex
  678. self subclassResponsibility
  679. !
  680. copyWith: anObject
  681. ^self copy add: anObject; yourself
  682. !
  683. copyWithAll: aCollection
  684. ^self copy addAll: aCollection; yourself
  685. ! !
  686. !Collection methodsFor: 'enumerating'!
  687. collect: aBlock
  688. | stream |
  689. stream := self class new writeStream.
  690. self do: [:each |
  691. stream nextPut: (aBlock value: each)].
  692. ^stream contents
  693. !
  694. detect: aBlock
  695. ^self detect: aBlock ifNone: [self errorNotFound]
  696. !
  697. detect: aBlock ifNone: anotherBlock
  698. ^{'
  699. for(var i = 0; i < self.length; i++)
  700. if(aBlock(self[i]))
  701. return self[i];
  702. return anotherBlock();
  703. '}
  704. !
  705. do: aBlock
  706. {'for(var i=0;i<self.length;i++){aBlock(self[i]);}'}
  707. !
  708. do: aBlock separatedBy: anotherBlock
  709. | first |
  710. first := true.
  711. self do: [:each |
  712. first
  713. ifTrue: [first := false]
  714. ifFalse: [anotherBlock value].
  715. aBlock value: each]
  716. !
  717. inject: anObject into: aBlock
  718. | result |
  719. result := anObject.
  720. self do: [:each |
  721. result := aBlock value: result value: each].
  722. ^result
  723. !
  724. reject: aBlock
  725. ^self select: [:each | (aBlock value: each) = false]
  726. !
  727. select: aBlock
  728. | stream |
  729. stream := self class new writeStream.
  730. self do: [:each |
  731. (aBlock value: each) ifTrue: [
  732. stream nextPut: each]].
  733. ^stream contents
  734. ! !
  735. !Collection methodsFor: 'error handling'!
  736. errorNotFound
  737. self error: 'Object is not in the collection'
  738. ! !
  739. !Collection methodsFor: 'testing'!
  740. includes: anObject
  741. ^{'
  742. var i = self.length;
  743. while (i--) {
  744. if (self[i].__eq(anObject)) {
  745. return true;
  746. }
  747. }
  748. return false;
  749. '}
  750. !
  751. isEmpty
  752. ^self size = 0
  753. !
  754. notEmpty
  755. ^self isEmpty not
  756. ! !
  757. Collection subclass: #String
  758. instanceVariableNames: ''
  759. category: 'Kernel'!
  760. !String class methodsFor: 'accessing'!
  761. cr
  762. ^{'{return ''\n''};'}
  763. !
  764. lf
  765. ^{'return ''\r'';'}
  766. !
  767. space
  768. ^{'return '' '';'}
  769. !
  770. streamClass
  771. ^StringStream
  772. !
  773. tab
  774. ^{'return ''\t'';'}
  775. ! !
  776. !String class methodsFor: 'instance creation'!
  777. fromString: aString
  778. ^{'return new self.fn(aString);'}
  779. ! !
  780. !String methodsFor: 'accessing'!
  781. at: anIndex
  782. ^{'return self[anIndex - 1] || nil;'}
  783. !
  784. at: anIndex ifAbsent: aBlock
  785. (self at: anIndex) ifNil: [aBlock]
  786. !
  787. at: anIndex put: anObject
  788. self errorReadOnly
  789. !
  790. escaped
  791. ^{'return escape(self)'}
  792. !
  793. size
  794. ^{'return self.length'}
  795. !
  796. unescaped
  797. ^{'return unescape(self)'}
  798. ! !
  799. !String methodsFor: 'adding'!
  800. add: anObject
  801. self errorReadOnly
  802. !
  803. remove: anObject
  804. self errorReadOnly
  805. ! !
  806. !String methodsFor: 'comparing'!
  807. = aString
  808. ^{'return String(self) == aString'}
  809. !
  810. > aString
  811. ^{'return String(self) > aString'}
  812. !
  813. < aString
  814. ^{'return String(self) < aString'}
  815. !
  816. >= aString
  817. ^{'return String(self) >= aString'}
  818. !
  819. <= aString
  820. ^{'return String(self) <= aString'}
  821. ! !
  822. !String methodsFor: 'converting'!
  823. asCharacterParser
  824. ^PPCharacterParser new string: self
  825. !
  826. asChoiceParser
  827. ^PPChoiceParser withAll: (self asArray collect: [:each | each asParser])
  828. !
  829. asJavascript
  830. ^{'
  831. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  832. return "unescape(\"" + escape(self) + "\")";
  833. else
  834. return "\"" + self + "\"";
  835. '}
  836. !
  837. asNumber
  838. ^{'return Number(self);'}
  839. !
  840. asParser
  841. ^PPStringParser new string: self
  842. !
  843. asSelector
  844. | selector |
  845. selector := '_', self.
  846. selector := selector replace: ':' with: '_'.
  847. selector := selector replace: '[+]' with: '_plus'.
  848. selector := selector replace: '-' with: '_minus'.
  849. selector := selector replace: '[*]' with: '_star'.
  850. selector := selector replace: '[/]' with: '_slash'.
  851. selector := selector replace: '>' with: '_gt'.
  852. selector := selector replace: '<' with: '_lt'.
  853. selector := selector replace: '=' with: '_eq'.
  854. selector := selector replace: ',' with: '_comma'.
  855. selector := selector replace: '[@]' with: '_at'.
  856. ^selector
  857. !
  858. asString
  859. ^self
  860. !
  861. tokenize: aString
  862. ^{'return self.split(aString)'}
  863. ! !
  864. !String methodsFor: 'copying'!
  865. , aString
  866. ^{'return self + aString'}
  867. !
  868. copyFrom: anIndex to: anotherIndex
  869. ^{'return self.substring(anIndex - 1, anotherIndex);'}
  870. !
  871. deepCopy
  872. ^self shallowCopy
  873. !
  874. shallowCopy
  875. ^self class fromString: self
  876. ! !
  877. !String methodsFor: 'error handling'!
  878. errorReadOnly
  879. self error: 'Object is read-only'
  880. ! !
  881. !String methodsFor: 'printing'!
  882. printNl
  883. {'console.log(self)'}
  884. !
  885. printString
  886. ^'''', self, ''''
  887. ! !
  888. !String methodsFor: 'regular expressions'!
  889. match: aRegexp
  890. ^{'return self.search(aRegexp) !!= -1'}
  891. !
  892. replace: aString with: anotherString
  893. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  894. !
  895. replaceRegexp: aRegexp with: aString
  896. ^{'return self.replace(aRegexp, aString);'}
  897. ! !
  898. !String methodsFor: 'testing'!
  899. isString
  900. ^true
  901. ! !
  902. !String methodsFor: '*JQuery'!
  903. asJQuery
  904. ^JQuery fromString: self
  905. !
  906. appendToJQuery: aJQuery
  907. {'aJQuery._appendElement_(String(self))'}
  908. ! !
  909. !String methodsFor: '*Canvas'!
  910. appendToBrush: aTagBrush
  911. aTagBrush appendString: self
  912. ! !
  913. Object subclass: #RegularExpression
  914. instanceVariableNames: ''
  915. category: 'Kernel'!
  916. !RegularExpression class methodsFor: 'instance creation'!
  917. fromString: aString
  918. ^self fromString: aString flag: ''
  919. !
  920. fromString: aString flag: anotherString
  921. ^{'return new RegExp(aString, anotherString);'}
  922. ! !
  923. !RegularExpression methodsFor: 'evaluating'!
  924. compile: aString
  925. ^{'return self.compile(aString);'}
  926. !
  927. exec: aString
  928. ^{'return self.exec(aString);'}
  929. !
  930. test: aString
  931. ^{'return self.test(aString);'}
  932. ! !
  933. Collection subclass: #Array
  934. instanceVariableNames: ''
  935. category: 'Kernel'!
  936. !Array methodsFor: 'accessing'!
  937. at: anIndex ifAbsent: aBlock
  938. ^{'
  939. var value = self[anIndex - 1];
  940. if(value === undefined) {
  941. return aBlock();
  942. } else {
  943. return value;
  944. }
  945. '}
  946. !
  947. at: anIndex put: anObject
  948. ^{'return self[anIndex - 1] = anObject'}
  949. !
  950. size
  951. ^{'return self.length'}
  952. ! !
  953. !Array methodsFor: 'adding'!
  954. add: anObject
  955. ^{'self.push(anObject); return anObject;'}
  956. !
  957. addLast: anObject
  958. ^self add: anObject
  959. !
  960. remove: anObject
  961. {'for(var i=0;i<self.length;i++) {
  962. if(self[i] == anObject) {
  963. self.splice(i,1);
  964. break;
  965. }
  966. }'}
  967. ! !
  968. !Array methodsFor: 'converting'!
  969. asJavascript
  970. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  971. ! !
  972. !Array methodsFor: 'copying'!
  973. deepCopy
  974. | newCollection |
  975. newCollection := self class new.
  976. self do: [:each | newCollection add: each deepCopy].
  977. ^newCollection
  978. !
  979. shallowCopy
  980. | newCollection |
  981. newCollection := self class new.
  982. self do: [:each | newCollection add: each].
  983. ^newCollection
  984. !
  985. copyFrom: anIndex to: anotherIndex
  986. | array |
  987. array := self class new.
  988. anIndex to: anotherIndex do: [:each |
  989. array add: (self at: each)].
  990. ^array
  991. ! !
  992. !Array methodsFor: 'enumerating'!
  993. sort
  994. ^self copy basicPerform: 'sort'
  995. !
  996. sort: aBlock
  997. ^self copy basicPerform: 'sort' withArguments: (Array with: aBlock)
  998. !
  999. join: aString
  1000. ^{'return self.join(aString);'}
  1001. ! !
  1002. Object subclass: #Association
  1003. instanceVariableNames: 'key value'
  1004. category: 'Kernel'!
  1005. !Association class methodsFor: 'instance creation'!
  1006. key: aKey value: aValue
  1007. ^self new
  1008. key: aKey;
  1009. value: aValue;
  1010. yourself
  1011. ! !
  1012. !Association methodsFor: 'accessing'!
  1013. key
  1014. ^key
  1015. !
  1016. key: aKey
  1017. key := aKey
  1018. !
  1019. value
  1020. ^value
  1021. !
  1022. value: aValue
  1023. value := aValue
  1024. ! !
  1025. !Association methodsFor: 'comparing'!
  1026. = anAssociation
  1027. ^self class = anAssociation class and: [
  1028. self key = anAssociation key and: [
  1029. self value = anAssociation value]]
  1030. ! !
  1031. Collection subclass: #Dictionary
  1032. instanceVariableNames: 'keys'
  1033. category: 'Kernel'!
  1034. !Dictionary methodsFor: 'accessing'!
  1035. associations
  1036. | associations |
  1037. associations := #().
  1038. keys do: [:each |
  1039. associations add: (Association key: each value: (self at: each))].
  1040. ^associations
  1041. !
  1042. at: aKey ifAbsent: aBlock
  1043. ^(self basicAt: aKey) ifNil: aBlock
  1044. !
  1045. at: aKey ifAbsentPut: aBlock
  1046. ^self at: aKey ifAbsent: [
  1047. self at: aKey put: aBlock value]
  1048. !
  1049. at: aKey ifPresent: aBlock
  1050. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  1051. !
  1052. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  1053. ^(self basicAt: aKey)
  1054. ifNil: anotherBlock
  1055. ifNotNil: [aBlock value: (self at: aKey)]
  1056. !
  1057. at: aKey put: aValue
  1058. (keys includes: aKey) ifFalse: [keys add: aKey].
  1059. ^self basicAt: aKey put: aValue
  1060. !
  1061. keys
  1062. ^keys copy
  1063. !
  1064. size
  1065. ^keys size
  1066. !
  1067. values
  1068. ^keys collect: [:each | self at: each]
  1069. ! !
  1070. !Dictionary methodsFor: 'adding/removing'!
  1071. add: anAssociation
  1072. self at: anAssociation key put: anAssociation value
  1073. !
  1074. addAll: aDictionary
  1075. super addAll: aDictionary associations.
  1076. ^aDictionary
  1077. !
  1078. remove: aKey
  1079. self removeKey: aKey
  1080. !
  1081. removeKey: aKey
  1082. keys remove: aKey
  1083. ! !
  1084. !Dictionary methodsFor: 'comparing'!
  1085. = aDictionary
  1086. self class = aDictionary class ifFalse: [^false].
  1087. self associationsDo: [:assoc |
  1088. (aDictionary at: assoc key ifAbsent: [^false]) = assoc value
  1089. ifFalse: [^false]].
  1090. ^true
  1091. ! !
  1092. !Dictionary methodsFor: 'copying'!
  1093. , aCollection
  1094. self shouldNotImplement
  1095. !
  1096. copyFrom: anIndex to: anotherIndex
  1097. self shouldNotImplement
  1098. !
  1099. shallowCopy
  1100. | copy |
  1101. copy := self class new.
  1102. self associationsDo: [:each |
  1103. copy at: each key put: each value].
  1104. ^copy
  1105. ! !
  1106. !Dictionary methodsFor: 'enumerating'!
  1107. associationsDo: aBlock
  1108. self associations do: aBlock
  1109. !
  1110. collect: aBlock
  1111. | newDict |
  1112. newDict := self class new.
  1113. self keysAndValuesDo: [:key :value |
  1114. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1115. ^newDict
  1116. !
  1117. detect: aBlock ifNone: anotherBlock
  1118. ^self values detect: aBlock ifNone: anotherBlock
  1119. !
  1120. do: aBlock
  1121. self values do: aBlock
  1122. !
  1123. includes: anObject
  1124. ^self values includes: anObject
  1125. !
  1126. keysAndValuesDo: aBlock
  1127. self associationsDo: [:each |
  1128. aBlock value: each key value: each value]
  1129. !
  1130. select: aBlock
  1131. | newDict |
  1132. newDict := self class new.
  1133. self keysAndValuesDo: [:key :value |
  1134. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1135. ^newDict
  1136. ! !
  1137. !Dictionary methodsFor: 'initialization'!
  1138. initialize
  1139. super initialize.
  1140. keys := #()
  1141. ! !
  1142. Object subclass: #ClassBuilder
  1143. instanceVariableNames: ''
  1144. category: 'Kernel'!
  1145. !ClassBuilder methodsFor: 'class creation'!
  1146. class: aClass instanceVariableNames: aString
  1147. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  1148. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  1149. self setupClass: aClass
  1150. !
  1151. superclass: aClass subclass: aString
  1152. self superclass: aClass subclass: aString instanceVariableNames: '' category: nil
  1153. !
  1154. superclass: aClass subclass: aString instanceVariableNames: aString2 category: aString3
  1155. | newClass |
  1156. newClass := self addSubclassOf: aClass named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2).
  1157. self setupClass: newClass.
  1158. newClass category: (aString3 ifNil: ['unclassified'])
  1159. ! !
  1160. !ClassBuilder methodsFor: 'private'!
  1161. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  1162. ^{'smalltalk.addClass(aString, aClass, aCollection);
  1163. return smalltalk[aString]'}
  1164. !
  1165. instanceVariableNamesFor: aString
  1166. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  1167. !
  1168. setupClass: aClass
  1169. {'smalltalk.init(aClass);'}
  1170. ! !
  1171. Object subclass: #ClassCategoryReader
  1172. instanceVariableNames: 'class category chunkParser'
  1173. category: 'Kernel'!
  1174. !ClassCategoryReader methodsFor: 'accessing'!
  1175. class: aClass category: aString
  1176. class := aClass.
  1177. category := aString
  1178. ! !
  1179. !ClassCategoryReader methodsFor: 'fileIn'!
  1180. scanFrom: aStream
  1181. | nextChunk |
  1182. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  1183. nextChunk isEmptyChunk ifFalse: [
  1184. self compileMethod: nextChunk contents.
  1185. self scanFrom: aStream].
  1186. ! !
  1187. !ClassCategoryReader methodsFor: 'initialization'!
  1188. initialize
  1189. super initialize.
  1190. chunkParser := ChunkParser new.
  1191. ! !
  1192. !ClassCategoryReader methodsFor: 'private'!
  1193. compileMethod: aString
  1194. | method |
  1195. method := Compiler new load: aString forClass: class.
  1196. method category: category.
  1197. class addCompiledMethod: method
  1198. ! !
  1199. Object subclass: #ClassCommentReader
  1200. instanceVariableNames: 'class chunkParser'
  1201. category: 'Kernel'!
  1202. !ClassCommentReader methodsFor: 'accessing'!
  1203. class: aClass
  1204. class := aClass
  1205. ! !
  1206. !ClassCommentReader methodsFor: 'fileIn'!
  1207. scanFrom: aStream
  1208. | nextChunk |
  1209. nextChunk := (chunkParser emptyChunk / chunkParser chunk) parse: aStream.
  1210. nextChunk isEmptyChunk ifFalse: [
  1211. self setComment: nextChunk contents].
  1212. ! !
  1213. !ClassCommentReader methodsFor: 'initialization'!
  1214. initialize
  1215. super initialize.
  1216. chunkParser := ChunkParser new.
  1217. ! !
  1218. !ClassCommentReader methodsFor: 'private'!
  1219. setComment: aString
  1220. class comment: aString
  1221. ! !
  1222. Object subclass: #Stream
  1223. instanceVariableNames: 'collection position streamSize'
  1224. category: 'Kernel'!
  1225. !Stream class methodsFor: 'instance creation'!
  1226. on: aCollection
  1227. ^self new
  1228. setCollection: aCollection;
  1229. setStreamSize: aCollection size;
  1230. yourself
  1231. ! !
  1232. !Stream methodsFor: 'accessing'!
  1233. collection
  1234. ^collection
  1235. !
  1236. contents
  1237. ^self collection
  1238. copyFrom: 1
  1239. to: self streamSize
  1240. !
  1241. position
  1242. ^position ifNil: [position := 0]
  1243. !
  1244. position: anInteger
  1245. position := anInteger
  1246. !
  1247. setCollection: aCollection
  1248. collection := aCollection
  1249. !
  1250. setStreamSize: anInteger
  1251. streamSize := anInteger
  1252. !
  1253. size
  1254. ^self streamSize
  1255. !
  1256. streamSize
  1257. ^streamSize
  1258. ! !
  1259. !Stream methodsFor: 'actions'!
  1260. close
  1261. !
  1262. flush
  1263. !
  1264. reset
  1265. self position: 0
  1266. !
  1267. resetContents
  1268. self reset.
  1269. self setStreamSize: 0
  1270. ! !
  1271. !Stream methodsFor: 'enumerating'!
  1272. do: aBlock
  1273. [self atEnd] whileFalse: [aBlock value: self next]
  1274. ! !
  1275. !Stream methodsFor: 'positioning'!
  1276. setToEnd
  1277. self position: self size
  1278. !
  1279. skip: anInteger
  1280. self position: ((self position + anInteger) min: self size max: 0)
  1281. ! !
  1282. !Stream methodsFor: 'reading'!
  1283. next
  1284. self position: self position + 1.
  1285. ^collection at: self position
  1286. !
  1287. next: anInteger
  1288. | tempCollection |
  1289. tempCollection := self collection class new.
  1290. anInteger timesRepeat: [
  1291. self atEnd ifFalse: [
  1292. tempCollection add: self next]].
  1293. ^tempCollection
  1294. !
  1295. peek
  1296. ^self atEnd ifFalse: [
  1297. self collection at: self position + 1]
  1298. ! !
  1299. !Stream methodsFor: 'testing'!
  1300. atEnd
  1301. ^self position = self size
  1302. !
  1303. atStart
  1304. ^self position = 0
  1305. !
  1306. isEmpty
  1307. ^self size = 0
  1308. ! !
  1309. !Stream methodsFor: 'writing'!
  1310. nextPut: anObject
  1311. self position: self position + 1.
  1312. self collection at: self position put: anObject.
  1313. self setStreamSize: (self streamSize max: self position)
  1314. !
  1315. nextPutAll: aCollection
  1316. aCollection do: [:each |
  1317. self nextPut: each]
  1318. ! !
  1319. Stream subclass: #StringStream
  1320. instanceVariableNames: ''
  1321. category: 'Kernel'!
  1322. !StringStream methodsFor: 'reading'!
  1323. next: anInteger
  1324. | tempCollection |
  1325. tempCollection := self collection class new.
  1326. anInteger timesRepeat: [
  1327. self atEnd ifFalse: [
  1328. tempCollection := tempCollection, self next]].
  1329. ^tempCollection
  1330. ! !
  1331. !StringStream methodsFor: 'writing'!
  1332. nextPut: aString
  1333. self nextPutAll: aString
  1334. !
  1335. nextPutAll: aString
  1336. self setCollection:
  1337. (self collection copyFrom: 1 to: self position),
  1338. aString,
  1339. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  1340. self position: self position + aString size.
  1341. self setStreamSize: (self streamSize max: self position)
  1342. ! !