Kernel.st 36 KB

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