Kernel.st 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553
  1. Smalltalk current createPackage: 'Kernel' properties: #{}!
  2. nil subclass: #Object
  3. instanceVariableNames: ''
  4. category: 'Kernel'!
  5. !Object methodsFor: 'accessing'!
  6. yourself
  7. ^self
  8. !
  9. class
  10. <return self.klass>
  11. !
  12. size
  13. self error: 'Object not indexable'
  14. !
  15. instVarAt: aString
  16. <return self['@'+aString]>
  17. !
  18. instVarAt: aString put: anObject
  19. <self['@' + aString] = anObject>
  20. !
  21. basicAt: aString
  22. <return self[aString]>
  23. !
  24. basicAt: aString put: anObject
  25. <return self[aString] = anObject>
  26. !
  27. basicDelete: aString
  28. <delete self[aString]>
  29. ! !
  30. !Object methodsFor: 'comparing'!
  31. = anObject
  32. ^self == anObject
  33. !
  34. ~= anObject
  35. ^(self = anObject) = false
  36. !
  37. == anObject
  38. <return self === anObject>
  39. !
  40. ~~ anObject
  41. ^(self == anObject) = false
  42. ! !
  43. !Object methodsFor: 'converting'!
  44. -> anObject
  45. ^Association key: self value: anObject
  46. !
  47. asString
  48. ^self printString
  49. !
  50. asJavascript
  51. ^self asString
  52. !
  53. asJSON
  54. <return JSON.stringify(self._asJSONObject())>
  55. !
  56. asJSONObject
  57. | object |
  58. object := Object new.
  59. self class instanceVariableNames do: [:each |
  60. object basicAt: each put: (self instVarAt: each) asJSONObject].
  61. ^object
  62. ! !
  63. !Object methodsFor: 'copying'!
  64. copy
  65. ^self shallowCopy postCopy
  66. !
  67. shallowCopy
  68. <
  69. var copy = self.klass._new();
  70. for(var i in self) {
  71. if(/^@.+/.test(i)) {
  72. copy[i] = self[i];
  73. }
  74. }
  75. return copy;
  76. >
  77. !
  78. deepCopy
  79. <
  80. var copy = self.klass._new();
  81. for(var i in self) {
  82. if(/^@.+/.test(i)) {
  83. copy[i] = self[i]._deepCopy();
  84. }
  85. }
  86. return copy;
  87. >
  88. !
  89. postCopy
  90. ! !
  91. !Object methodsFor: 'error handling'!
  92. error: aString
  93. Error signal: aString
  94. !
  95. subclassResponsibility
  96. self error: 'This method is a responsibility of a subclass'
  97. !
  98. shouldNotImplement
  99. self error: 'This method should not be implemented in ', self class name
  100. !
  101. try: aBlock catch: anotherBlock
  102. <try{aBlock()} catch(e) {anotherBlock(e)}>
  103. !
  104. doesNotUnderstand: aMessage
  105. MessageNotUnderstood new
  106. receiver: self;
  107. message: aMessage;
  108. signal
  109. !
  110. halt
  111. self error: 'Halt encountered'
  112. !
  113. deprecatedAPI
  114. "Just a simple way to deprecate methods.
  115. #deprecatedAPI is in the 'error handling' protocol even if it doesn't throw an error,
  116. but it could in the future."
  117. console warn: thisContext home asString, ' is deprecated!!'
  118. ! !
  119. !Object methodsFor: 'initialization'!
  120. initialize
  121. ! !
  122. !Object methodsFor: 'message handling'!
  123. perform: aSymbol
  124. ^self perform: aSymbol withArguments: #()
  125. !
  126. perform: aSymbol withArguments: aCollection
  127. ^self basicPerform: aSymbol asSelector withArguments: aCollection
  128. !
  129. basicPerform: aSymbol
  130. ^self basicPerform: aSymbol withArguments: #()
  131. !
  132. basicPerform: aSymbol withArguments: aCollection
  133. <return self[aSymbol].apply(self, aCollection);>
  134. ! !
  135. !Object methodsFor: 'printing'!
  136. printString
  137. ^'a ', self class name
  138. !
  139. printNl
  140. <console.log(self)>
  141. !
  142. log: aString block: aBlock
  143. | result |
  144. console log: aString, ' time: ', (Date millisecondsToRun: [result := aBlock value]) printString.
  145. ^result
  146. !
  147. storeString
  148. "Answer a String representation of the receiver from which the receiver
  149. can be reconstructed."
  150. ^ String streamContents: [:s | self storeOn: s]
  151. !
  152. storeOn: aStream
  153. aStream nextPutAll: self printString
  154. ! !
  155. !Object methodsFor: 'testing'!
  156. isKindOf: aClass
  157. ^(self isMemberOf: aClass)
  158. ifTrue: [true]
  159. ifFalse: [self class inheritsFrom: aClass]
  160. !
  161. isMemberOf: aClass
  162. ^self class = aClass
  163. !
  164. ifNil: aBlock
  165. "inlined in the Compiler"
  166. ^self
  167. !
  168. ifNil: aBlock ifNotNil: anotherBlock
  169. "inlined in the Compiler"
  170. ^anotherBlock value
  171. !
  172. ifNotNil: aBlock
  173. "inlined in the Compiler"
  174. ^aBlock value
  175. !
  176. ifNotNil: aBlock ifNil: anotherBlock
  177. "inlined in the Compiler"
  178. ^aBlock value
  179. !
  180. isNil
  181. ^false
  182. !
  183. notNil
  184. ^self isNil not
  185. !
  186. isClass
  187. ^false
  188. !
  189. isMetaclass
  190. ^false
  191. !
  192. isNumber
  193. ^false
  194. !
  195. isString
  196. ^false
  197. !
  198. isParseFailure
  199. ^false
  200. ! !
  201. !Object class methodsFor: 'initialization'!
  202. initialize
  203. "no op"
  204. ! !
  205. Object subclass: #Smalltalk
  206. instanceVariableNames: ''
  207. category: 'Kernel'!
  208. !Smalltalk methodsFor: 'accessing'!
  209. classes
  210. <return self.classes()>
  211. !
  212. at: aString
  213. <return self[aString]>
  214. !
  215. basicParse: aString
  216. <return smalltalk.parser.parse(aString)>
  217. !
  218. parse: aString
  219. | result |
  220. self try: [result := self basicParse: aString] catch: [:ex | (self parseError: ex parsing: aString) signal].
  221. ^result
  222. !
  223. parseError: anException parsing: aString
  224. | row col message lines badLine code |
  225. <row = anException.line;
  226. col = anException.column;
  227. message = anException.message;>.
  228. lines := aString lines.
  229. badLine := lines at: row.
  230. badLine := (badLine copyFrom: 1 to: col - 1), ' ===>', (badLine copyFrom: col to: badLine size).
  231. lines at: row put: badLine.
  232. code := String streamContents: [:s |
  233. lines withIndexDo: [:l :i |
  234. s nextPutAll: i asString, ': ', l, String lf]].
  235. ^ Error new messageText: ('Parse error on line ' , row , ' column ' , col , ' : ' , message , ' Below is code with line numbers and ===> marker inserted:' , String lf, code)
  236. !
  237. reservedWords
  238. "JavaScript reserved words"
  239. <return self.reservedWords>
  240. !
  241. readJSObject: anObject
  242. <return self.readJSObject(anObject)>
  243. ! !
  244. !Smalltalk methodsFor: 'classes'!
  245. removeClass: aClass
  246. aClass isMetaclass ifTrue: [self error: aClass asString, ' is a Metaclass and cannot be removed!!'].
  247. aClass methodDictionary values do: [:each |
  248. aClass removeCompiledMethod: each].
  249. aClass class methodDictionary values do: [:each |
  250. aClass class removeCompiledMethod: each].
  251. self basicDelete: aClass name
  252. ! !
  253. !Smalltalk methodsFor: 'packages'!
  254. packages
  255. "Return all Package instances in the system."
  256. <return self.packages.all()>
  257. !
  258. packageAt: packageName
  259. <return self.packages[packageName]>
  260. !
  261. packageAt: packageName ifAbsent: aBlock
  262. ^(self packageAt: packageName) ifNil: aBlock
  263. !
  264. removePackage: packageName
  265. "Removes a package and all its classes."
  266. | pkg |
  267. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  268. pkg classes do: [:each |
  269. self removeClass: each].
  270. self deletePackage: packageName
  271. !
  272. renamePackage: packageName to: newName
  273. "Rename a package."
  274. | pkg |
  275. pkg := self packageAt: packageName ifAbsent: [self error: 'Missing package: ', packageName].
  276. (self packageAt: newName) ifNotNil: [self error: 'Already exists a package called: ', newName].
  277. <smalltalk.packages[newName] = smalltalk.packages[packageName]>.
  278. pkg name: newName.
  279. self deletePackage: packageName.
  280. ! !
  281. !Smalltalk methodsFor: 'private'!
  282. createPackage: packageName
  283. "Create and bind a new package with given name and return it."
  284. <return smalltalk.addPackage(packageName, nil)>
  285. !
  286. deletePackage: packageName
  287. "Deletes a package by deleting its binding, but does not check if it contains classes etc.
  288. To remove a package, use #removePackage instead."
  289. <delete smalltalk.packages[packageName]>
  290. !
  291. createPackage: packageName properties: aDict
  292. "Create and bind a new package with given name and return it."
  293. | object |
  294. <object = {};>.
  295. aDict keysAndValuesDo: [:key :value |
  296. <object[key] = value>.
  297. ].
  298. <return smalltalk.addPackage(packageName, object)>
  299. ! !
  300. Smalltalk class instanceVariableNames: 'current'!
  301. !Smalltalk class methodsFor: 'accessing'!
  302. current
  303. <return smalltalk>
  304. ! !
  305. Object subclass: #Package
  306. instanceVariableNames: 'commitPathJs commitPathSt'
  307. category: 'Kernel'!
  308. !Package commentStamp!
  309. A Package is similar to a "class category" typically found in other Smalltalks like Pharo or Squeak. Amber does not have class categories anymore, it had in the beginning but now each class in the system knows which package it belongs to.
  310. A Package has a name, an Array of "requires", a comment and a Dictionary with other optional key value attributes. A Package can also be queried for its classes, but it will then resort to a reverse scan of all classes to find them.
  311. Packages are manipulated through "Smalltalk current", like for example finding one based on a name:
  312. Smalltalk current packageAt: 'Kernel'
  313. ...but you can also use:
  314. Package named: 'Kernel'
  315. A Package differs slightly from a Monticello package which can span multiple class categories using a naming convention based on hyphenation. But just as in Monticello a Package supports "class extensions" so a Package
  316. can define behaviors in foreign classes using a naming convention for method categories where the category starts with an asterisk and then the name of the owning package follows. This can easily be seen in for example class
  317. String where the method category "*IDE" defines #inspectOn: which thus is a method belonging to the IDE package.!
  318. !Package methodsFor: 'accessing'!
  319. name
  320. <return self.pkgName>
  321. !
  322. name: aString
  323. <self.pkgName = aString>
  324. !
  325. commitPathJs
  326. ^ commitPathJs ifNil: [self class defaultCommitPathJs]
  327. !
  328. commitPathSt
  329. ^ commitPathSt ifNil: [self class defaultCommitPathSt]
  330. !
  331. commitPathJs: aString
  332. commitPathJs := aString
  333. !
  334. commitPathSt: aString
  335. commitPathSt := aString
  336. !
  337. dependencies
  338. ^self propertyAt: 'dependencies' ifAbsent: [#()]
  339. !
  340. dependencies: anArray
  341. ^self propertyAt: 'dependencies' put: anArray
  342. !
  343. properties
  344. ^Smalltalk current readJSObject: (self basicAt: 'properties')
  345. !
  346. properties: aDict
  347. "We store it as a javascript object."
  348. | object |
  349. <object = {};>.
  350. aDict keysAndValuesDo: [:key :value |
  351. <object[key] = value>.
  352. ].
  353. <return self.properties = object>
  354. ! !
  355. !Package methodsFor: 'classes'!
  356. classes
  357. "We need to do a reverse scan."
  358. ^Smalltalk current classes select: [:c | c package == self]
  359. ! !
  360. !Package methodsFor: 'printing'!
  361. printString
  362. ^self name
  363. ! !
  364. Package class instanceVariableNames: 'defaultCommitPathJs defaultCommitPathSt'!
  365. !Package methodsFor: 'private'!
  366. propertiesAsJSON
  367. <return JSON.stringify(self.properties)>
  368. !
  369. jsProperties
  370. <return self.properties>
  371. !
  372. jsProperties: aJSObject
  373. <return self.properties = aJSObject>
  374. ! !
  375. !Package methodsFor: 'properties'!
  376. propertyAt: key
  377. <return self.properties[key]>
  378. !
  379. propertyAt: key put: value
  380. <return self.properties[key] = value>
  381. !
  382. propertyAt: key ifAbsent: block
  383. ^(self propertyAt: key) ifNil: [block value]
  384. ! !
  385. !Package class methodsFor: 'accessing'!
  386. named: aPackageName
  387. ^Smalltalk current packageAt: aPackageName
  388. !
  389. named: aPackageName ifAbsent: aBlock
  390. ^Smalltalk current packageAt: aPackageName ifAbsent: aBlock
  391. !
  392. defaultCommitPathSt
  393. ^ defaultCommitPathSt ifNil: [ defaultCommitPathSt := 'st']
  394. !
  395. defaultCommitPathJs
  396. ^ defaultCommitPathJs ifNil: [ defaultCommitPathJs := 'js']
  397. !
  398. defaultCommitPathSt: aString
  399. defaultCommitPathSt := aString
  400. !
  401. defaultCommitPathJs: aString
  402. defaultCommitPathJs := aString
  403. ! !
  404. !Package class methodsFor: 'reset'!
  405. resetCommitPaths
  406. defaultCommitPathJs := nil.
  407. defaultCommitPathSt := nil.
  408. ! !
  409. Object subclass: #Behavior
  410. instanceVariableNames: ''
  411. category: 'Kernel'!
  412. !Behavior methodsFor: 'accessing'!
  413. name
  414. <return self.className || nil>
  415. !
  416. superclass
  417. <return self.superclass || nil>
  418. !
  419. subclasses
  420. <return smalltalk.subclasses(self)>
  421. !
  422. allSubclasses
  423. | result |
  424. result := self subclasses.
  425. self subclasses do: [:each |
  426. result addAll: each allSubclasses].
  427. ^result
  428. !
  429. withAllSubclasses
  430. ^(Array with: self) addAll: self allSubclasses; yourself
  431. !
  432. prototype
  433. <return self.fn.prototype>
  434. !
  435. methodDictionary
  436. <var dict = smalltalk.Dictionary._new();
  437. var methods = self.fn.prototype.methods;
  438. for(var i in methods) {
  439. if(methods[i].selector) {
  440. dict._at_put_(methods[i].selector, methods[i]);
  441. }
  442. };
  443. return dict>
  444. !
  445. methodsFor: aString
  446. ^ClassCategoryReader new
  447. class: self category: aString;
  448. yourself
  449. !
  450. addCompiledMethod: aMethod
  451. <smalltalk.addMethod(aMethod.selector._asSelector(), aMethod, self)>
  452. !
  453. instanceVariableNames
  454. <return self.iVarNames>
  455. !
  456. comment
  457. ^(self basicAt: 'comment') ifNil: ['']
  458. !
  459. comment: aString
  460. self basicAt: 'comment' put: aString
  461. !
  462. commentStamp
  463. ^ClassCommentReader new
  464. class: self;
  465. yourself
  466. !
  467. removeCompiledMethod: aMethod
  468. <delete self.fn.prototype[aMethod.selector._asSelector()];
  469. delete self.fn.prototype.methods[aMethod.selector];
  470. smalltalk.init(self);>
  471. !
  472. protocols
  473. | protocols |
  474. protocols := Array new.
  475. self methodDictionary do: [:each |
  476. (protocols includes: each category) ifFalse: [
  477. protocols add: each category]].
  478. ^protocols sort
  479. !
  480. protocolsDo: aBlock
  481. "Execute aBlock for each method category with
  482. its collection of methods in the sort order of category name."
  483. | methodsByCategory |
  484. methodsByCategory := Dictionary new.
  485. self methodDictionary values do: [:m |
  486. (methodsByCategory at: m category ifAbsentPut: [Array new])
  487. add: m].
  488. self protocols do: [:category |
  489. aBlock value: category value: (methodsByCategory at: category)]
  490. !
  491. allInstanceVariableNames
  492. | result |
  493. result := self instanceVariableNames copy.
  494. self superclass ifNotNil: [
  495. result addAll: self superclass allInstanceVariableNames].
  496. ^result
  497. !
  498. methodAt: aString
  499. <return smalltalk.methods(self)[aString]>
  500. !
  501. methodsFor: aString stamp: aStamp
  502. "Added for compatibility, right now ignores stamp."
  503. ^self methodsFor: aString
  504. !
  505. commentStamp: aStamp prior: prior
  506. ^self commentStamp
  507. ! !
  508. !Behavior methodsFor: 'compiling'!
  509. compile: aString
  510. self compile: aString category: ''
  511. !
  512. compile: aString category: anotherString
  513. | method |
  514. method := Compiler new load: aString forClass: self.
  515. method category: anotherString.
  516. self addCompiledMethod: method
  517. ! !
  518. !Behavior methodsFor: 'instance creation'!
  519. new
  520. ^self basicNew initialize
  521. !
  522. basicNew
  523. <return new self.fn()>
  524. !
  525. inheritsFrom: aClass
  526. ^aClass allSubclasses includes: self
  527. ! !
  528. Behavior subclass: #Class
  529. instanceVariableNames: ''
  530. category: 'Kernel'!
  531. !Class methodsFor: 'accessing'!
  532. category
  533. ^self package ifNil: ['Unclassified'] ifNotNil: [self package name]
  534. !
  535. rename: aString
  536. <
  537. smalltalk[aString] = self;
  538. delete smalltalk[self.className];
  539. self.className = aString;
  540. >
  541. !
  542. package
  543. <return self.pkg>
  544. !
  545. package: aPackage
  546. <self.pkg = aPackage>
  547. ! !
  548. !Class methodsFor: 'class creation'!
  549. subclass: aString instanceVariableNames: anotherString
  550. "Kept for compatibility."
  551. ^self subclass: aString instanceVariableNames: anotherString package: nil
  552. !
  553. subclass: aString instanceVariableNames: aString2 category: aString3
  554. "Kept for compatibility."
  555. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  556. !
  557. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  558. "Just ignore class variables and pools. Added for compatibility."
  559. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  560. !
  561. subclass: aString instanceVariableNames: aString2 package: aString3
  562. ^ClassBuilder new
  563. superclass: self subclass: aString instanceVariableNames: aString2 package: aString3
  564. ! !
  565. !Class methodsFor: 'printing'!
  566. printString
  567. ^self name
  568. ! !
  569. !Class methodsFor: 'testing'!
  570. isClass
  571. ^true
  572. ! !
  573. Behavior subclass: #Metaclass
  574. instanceVariableNames: ''
  575. category: 'Kernel'!
  576. !Metaclass methodsFor: 'accessing'!
  577. instanceClass
  578. <return self.instanceClass>
  579. !
  580. instanceVariableNames: aCollection
  581. ClassBuilder new
  582. class: self instanceVariableNames: aCollection
  583. ! !
  584. !Metaclass methodsFor: 'printing'!
  585. printString
  586. ^self instanceClass name, ' class'
  587. ! !
  588. !Metaclass methodsFor: 'testing'!
  589. isMetaclass
  590. ^true
  591. ! !
  592. Object subclass: #CompiledMethod
  593. instanceVariableNames: ''
  594. category: 'Kernel'!
  595. !CompiledMethod methodsFor: 'accessing'!
  596. source
  597. ^(self basicAt: 'source') ifNil: ['']
  598. !
  599. source: aString
  600. self basicAt: 'source' put: aString
  601. !
  602. category
  603. ^(self basicAt: 'category') ifNil: ['']
  604. !
  605. category: aString
  606. self basicAt: 'category' put: aString
  607. !
  608. selector
  609. ^self basicAt: 'selector'
  610. !
  611. selector: aString
  612. self basicAt: 'selector' put: aString
  613. !
  614. fn
  615. ^self basicAt: 'fn'
  616. !
  617. fn: aBlock
  618. self basicAt: 'fn' put: aBlock
  619. !
  620. messageSends
  621. ^self basicAt: 'messageSends'
  622. !
  623. methodClass
  624. ^self basicAt: 'methodClass'
  625. !
  626. referencedClasses
  627. ^self basicAt: 'referencedClasses'
  628. !
  629. arguments
  630. <return self.args || []>
  631. ! !
  632. Object subclass: #Number
  633. instanceVariableNames: ''
  634. category: 'Kernel'!
  635. !Number methodsFor: ''!
  636. ! !
  637. !Number methodsFor: 'arithmetic'!
  638. + aNumber
  639. "Inlined in the Compiler"
  640. <return self + aNumber>
  641. !
  642. - aNumber
  643. "Inlined in the Compiler"
  644. <return self - aNumber>
  645. !
  646. * aNumber
  647. "Inlined in the Compiler"
  648. <return self * aNumber>
  649. !
  650. / aNumber
  651. "Inlined in the Compiler"
  652. <return self / aNumber>
  653. !
  654. max: aNumber
  655. <return Math.max(self, aNumber);>
  656. !
  657. min: aNumber
  658. <return Math.min(self, aNumber);>
  659. !
  660. negated
  661. ^0 - self
  662. !
  663. \\ aNumber
  664. <return self % aNumber>
  665. !
  666. sqrt
  667. <return Math.sqrt(self)>
  668. !
  669. squared
  670. ^self * self
  671. ! !
  672. !Number methodsFor: 'comparing'!
  673. = aNumber
  674. aNumber class = self class ifFalse: [^false].
  675. <return Number(self) == aNumber>
  676. !
  677. > aNumber
  678. "Inlined in the Compiler"
  679. <return self >> aNumber>
  680. !
  681. < aNumber
  682. "Inlined in the Compiler"
  683. <return self < aNumber>
  684. !
  685. >= aNumber
  686. "Inlined in the Compiler"
  687. <return self >>= aNumber>
  688. !
  689. <= aNumber
  690. "Inlined in the Compiler"
  691. <return self <= aNumber>
  692. !
  693. == aNumber
  694. aNumber class = self class ifFalse: [^false].
  695. <return Number(self) === Number(aNumber)>
  696. ! !
  697. !Number methodsFor: 'converting'!
  698. rounded
  699. <return Math.round(self);>
  700. !
  701. truncated
  702. |result|
  703. self >= 0
  704. ifTrue: [<result = Math.floor(self);>]
  705. ifFalse: [<result = (Math.floor(self * (-1)) * (-1));>].
  706. ^ result
  707. !
  708. to: aNumber
  709. | array first last count |
  710. first := self truncated.
  711. last := aNumber truncated + 1.
  712. count := 1.
  713. array := Array new.
  714. (last - first) timesRepeat: [
  715. array at: count put: first.
  716. count := count + 1.
  717. first := first + 1].
  718. ^array
  719. !
  720. asString
  721. ^self printString
  722. !
  723. asJavascript
  724. ^'(', self printString, ')'
  725. !
  726. atRandom
  727. ^(Random new next * self) truncated + 1
  728. !
  729. @ aNumber
  730. ^Point x: self y: aNumber
  731. !
  732. asPoint
  733. ^Point x: self y: self
  734. !
  735. asJSONObject
  736. ^self
  737. !
  738. to: stop by: step
  739. | array value pos |
  740. value := self.
  741. array := Array new.
  742. pos := 1.
  743. step = 0 ifTrue: [self error: 'step must be non-zero'].
  744. step < 0
  745. ifTrue: [[ value >= stop ] whileTrue: [
  746. array at: pos put: value.
  747. pos := pos + 1.
  748. value := value + step]]
  749. ifFalse: [[ value <= stop ] whileTrue: [
  750. array at: pos put: value.
  751. pos := pos + 1.
  752. value := value + step]].
  753. ^array
  754. ! !
  755. !Number methodsFor: 'copying'!
  756. deepCopy
  757. ^self copy
  758. !
  759. copy
  760. ^self
  761. ! !
  762. !Number methodsFor: 'enumerating'!
  763. timesRepeat: aBlock
  764. | integer count |
  765. integer := self truncated.
  766. count := 1.
  767. [count > self] whileFalse: [
  768. aBlock value.
  769. count := count + 1]
  770. !
  771. to: stop do: aBlock
  772. "Evaluate aBlock for each number from self to aNumber."
  773. | nextValue |
  774. nextValue := self.
  775. [nextValue <= stop]
  776. whileTrue:
  777. [aBlock value: nextValue.
  778. nextValue := nextValue + 1]
  779. !
  780. to: stop by: step do: aBlock
  781. | value |
  782. value := self.
  783. step = 0 ifTrue: [self error: 'step must be non-zero'].
  784. step < 0
  785. ifTrue: [[ value >= stop ] whileTrue: [
  786. aBlock value: value.
  787. value := value + step]]
  788. ifFalse: [[ value <= stop ] whileTrue: [
  789. aBlock value: value.
  790. value := value + step]]
  791. ! !
  792. !Number methodsFor: 'printing'!
  793. printString
  794. <return String(self)>
  795. !
  796. printShowingDecimalPlaces: placesDesired
  797. <return self.toFixed(placesDesired)>
  798. ! !
  799. !Number methodsFor: 'testing'!
  800. isNumber
  801. ^true
  802. !
  803. even
  804. ^ 0 = (self \\ 2)
  805. !
  806. odd
  807. ^ self even not
  808. ! !
  809. !Number methodsFor: 'timeouts/intervals'!
  810. clearInterval
  811. <clearInterval(Number(self))>
  812. !
  813. clearTimeout
  814. <clearTimeout(Number(self))>
  815. ! !
  816. !Number class methodsFor: 'instance creation'!
  817. pi
  818. <return Math.PI>
  819. ! !
  820. Object subclass: #BlockClosure
  821. instanceVariableNames: ''
  822. category: 'Kernel'!
  823. !BlockClosure methodsFor: 'accessing'!
  824. compiledSource
  825. <return self.toString()>
  826. !
  827. numArgs
  828. <return self.length>
  829. ! !
  830. !BlockClosure methodsFor: 'controlling'!
  831. whileTrue: aBlock
  832. "inlined in the Compiler"
  833. <while(self()) {aBlock()}>
  834. !
  835. whileFalse: aBlock
  836. "inlined in the Compiler"
  837. <while(!!self()) {aBlock()}>
  838. !
  839. whileFalse
  840. "inlined in the Compiler"
  841. self whileFalse: []
  842. !
  843. whileTrue
  844. "inlined in the Compiler"
  845. self whileTrue: []
  846. ! !
  847. !BlockClosure methodsFor: 'error handling'!
  848. on: anErrorClass do: aBlock
  849. self try: self catch: [:error |
  850. (error isKindOf: anErrorClass)
  851. ifTrue: [aBlock value: error]
  852. ifFalse: [error signal]]
  853. ! !
  854. !BlockClosure methodsFor: 'evaluating'!
  855. value
  856. "inlined in the Compiler"
  857. <return self();>
  858. !
  859. value: anArg
  860. "inlined in the Compiler"
  861. <return self(anArg);>
  862. !
  863. value: firstArg value: secondArg
  864. "inlined in the Compiler"
  865. <return self(firstArg, secondArg);>
  866. !
  867. value: firstArg value: secondArg value: thirdArg
  868. "inlined in the Compiler"
  869. <return self(firstArg, secondArg, thirdArg);>
  870. !
  871. valueWithPossibleArguments: aCollection
  872. <return self.apply(null, aCollection);>
  873. !
  874. new
  875. "Use the receiver as a JS constructor.
  876. *Do not* use this method to instanciate Smalltalk objects!!"
  877. <return new self()>
  878. !
  879. applyTo: anObject arguments: aCollection
  880. <return self.apply(anObject, aCollection)>
  881. !
  882. timeToRun
  883. "Answer the number of milliseconds taken to execute this block."
  884. ^ Date millisecondsToRun: self
  885. !
  886. ensure: aBlock
  887. | success |
  888. success := false.
  889. [self value. success := true. aBlock value]
  890. on: Error
  891. do: [:ex |
  892. success ifFalse: [aBlock value].
  893. ex signal]
  894. ! !
  895. !BlockClosure methodsFor: 'timeout/interval'!
  896. valueWithTimeout: aNumber
  897. <return setTimeout(self, aNumber)>
  898. !
  899. valueWithInterval: aNumber
  900. <return setInterval(self, aNumber)>
  901. ! !
  902. Object subclass: #Boolean
  903. instanceVariableNames: ''
  904. category: 'Kernel'!
  905. !Boolean methodsFor: 'comparing'!
  906. = aBoolean
  907. aBoolean class = self class ifFalse: [^false].
  908. <return Boolean(self == true) == aBoolean>
  909. !
  910. == aBoolean
  911. aBoolean class = self class ifFalse: [^false].
  912. <return Boolean(self == true) === Boolean(aBoolean == true)>
  913. ! !
  914. !Boolean methodsFor: 'controlling'!
  915. ifTrue: aBlock
  916. "inlined in the Compiler"
  917. ^self ifTrue: aBlock ifFalse: []
  918. !
  919. ifFalse: aBlock
  920. "inlined in the Compiler"
  921. ^self ifTrue: [] ifFalse: aBlock
  922. !
  923. ifFalse: aBlock ifTrue: anotherBlock
  924. "inlined in the Compiler"
  925. ^self ifTrue: anotherBlock ifFalse: aBlock
  926. !
  927. ifTrue: aBlock ifFalse: anotherBlock
  928. "inlined in the Compiler"
  929. <
  930. if(self == true) {
  931. return aBlock();
  932. } else {
  933. return anotherBlock();
  934. }
  935. >
  936. !
  937. and: aBlock
  938. ^self = true
  939. ifTrue: aBlock
  940. ifFalse: [false]
  941. !
  942. or: aBlock
  943. ^self = true
  944. ifTrue: [true]
  945. ifFalse: aBlock
  946. !
  947. not
  948. ^self = false
  949. !
  950. & aBoolean
  951. <
  952. if(self == true) {
  953. return aBoolean;
  954. } else {
  955. return false;
  956. }
  957. >
  958. !
  959. | aBoolean
  960. <
  961. if(self == true) {
  962. return true;
  963. } else {
  964. return aBoolean;
  965. }
  966. >
  967. ! !
  968. !Boolean methodsFor: 'converting'!
  969. asJSONObject
  970. ^self
  971. ! !
  972. !Boolean methodsFor: 'copying'!
  973. shallowCopy
  974. ^self
  975. !
  976. deepCopy
  977. ^self
  978. ! !
  979. !Boolean methodsFor: 'printing'!
  980. printString
  981. <return self.toString()>
  982. ! !
  983. Object subclass: #Date
  984. instanceVariableNames: ''
  985. category: 'Kernel'!
  986. !Date commentStamp!
  987. The Date class is used to work with dates and times.!
  988. !Date methodsFor: 'accessing'!
  989. year
  990. <return self.getFullYear()>
  991. !
  992. month
  993. <return self.getMonth() + 1>
  994. !
  995. month: aNumber
  996. <self.setMonth(aNumber - 1)>
  997. !
  998. day
  999. ^self dayOfWeek
  1000. !
  1001. dayOfWeek
  1002. <return self.getDay() + 1>
  1003. !
  1004. dayOfWeek: aNumber
  1005. <return self.setDay(aNumber - 1)>
  1006. !
  1007. day: aNumber
  1008. self day: aNumber
  1009. !
  1010. year: aNumber
  1011. <self.setFullYear(aNumber)>
  1012. !
  1013. dayOfMonth
  1014. <return self.getDate()>
  1015. !
  1016. dayOfMonth: aNumber
  1017. <self.setDate(aNumber)>
  1018. !
  1019. time
  1020. <return self.getTime()>
  1021. !
  1022. time: aNumber
  1023. <self.setTime(aNumber)>
  1024. !
  1025. hours: aNumber
  1026. <self.setHours(aNumber)>
  1027. !
  1028. minutes: aNumber
  1029. <self.setMinutes(aNumber)>
  1030. !
  1031. seconds: aNumber
  1032. <self.setSeconds(aNumber)>
  1033. !
  1034. milliseconds: aNumber
  1035. <self.setMilliseconds(aNumber)>
  1036. !
  1037. hours
  1038. <return self.getHours()>
  1039. !
  1040. minutes
  1041. <return self.getMinutes()>
  1042. !
  1043. seconds
  1044. <return self.getSeconds()>
  1045. !
  1046. milliseconds
  1047. <return self.getMilliseconds()>
  1048. ! !
  1049. !Date methodsFor: 'arithmetic'!
  1050. - aDate
  1051. <return self - aDate>
  1052. !
  1053. + aDate
  1054. <return self + aDate>
  1055. ! !
  1056. !Date methodsFor: 'comparing'!
  1057. < aDate
  1058. <return self < aDate>
  1059. !
  1060. > aDate
  1061. <return self >> aDate>
  1062. !
  1063. <= aDate
  1064. <return self <= aDate>
  1065. !
  1066. >= aDate
  1067. <return self >>= aDate>
  1068. ! !
  1069. !Date methodsFor: 'converting'!
  1070. asString
  1071. <return self.toString()>
  1072. !
  1073. asMilliseconds
  1074. ^self time
  1075. !
  1076. asDateString
  1077. <return self.toDateString()>
  1078. !
  1079. asTimeString
  1080. <return self.toTimeString()>
  1081. !
  1082. asLocaleString
  1083. <return self.toLocaleString()>
  1084. !
  1085. asNumber
  1086. ^self asMilliseconds
  1087. !
  1088. asJSONObject
  1089. ^self
  1090. ! !
  1091. !Date methodsFor: 'printing'!
  1092. printString
  1093. ^self asString
  1094. ! !
  1095. !Date class methodsFor: 'instance creation'!
  1096. new: anObject
  1097. <return new Date(anObject)>
  1098. !
  1099. fromString: aString
  1100. "Example: Date fromString('2011/04/15 00:00:00')"
  1101. ^self new: aString
  1102. !
  1103. fromSeconds: aNumber
  1104. ^self fromMilliseconds: aNumber * 1000
  1105. !
  1106. fromMilliseconds: aNumber
  1107. ^self new: aNumber
  1108. !
  1109. today
  1110. ^self new
  1111. !
  1112. now
  1113. ^self today
  1114. !
  1115. millisecondsToRun: aBlock
  1116. | t |
  1117. t := Date now.
  1118. aBlock value.
  1119. ^Date now - t
  1120. ! !
  1121. Object subclass: #UndefinedObject
  1122. instanceVariableNames: ''
  1123. category: 'Kernel'!
  1124. !UndefinedObject methodsFor: 'class creation'!
  1125. subclass: aString instanceVariableNames: anotherString
  1126. ^self subclass: aString instanceVariableNames: anotherString package: nil
  1127. !
  1128. subclass: aString instanceVariableNames: aString2 category: aString3
  1129. "Kept for compatibility."
  1130. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  1131. !
  1132. subclass: aString instanceVariableNames: aString2 package: aString3
  1133. ^ClassBuilder new
  1134. superclass: self subclass: aString instanceVariableNames: aString2 package: aString3
  1135. ! !
  1136. !UndefinedObject methodsFor: 'copying'!
  1137. shallowCopy
  1138. ^self
  1139. !
  1140. deepCopy
  1141. ^self
  1142. ! !
  1143. !UndefinedObject methodsFor: 'printing'!
  1144. printString
  1145. ^'nil'
  1146. ! !
  1147. !UndefinedObject methodsFor: 'testing'!
  1148. ifNil: aBlock
  1149. "inlined in the Compiler"
  1150. ^self ifNil: aBlock ifNotNil: []
  1151. !
  1152. ifNotNil: aBlock
  1153. "inlined in the Compiler"
  1154. ^self
  1155. !
  1156. ifNil: aBlock ifNotNil: anotherBlock
  1157. "inlined in the Compiler"
  1158. ^aBlock value
  1159. !
  1160. ifNotNil: aBlock ifNil: anotherBlock
  1161. "inlined in the Compiler"
  1162. ^anotherBlock value
  1163. !
  1164. isNil
  1165. ^true
  1166. !
  1167. notNil
  1168. ^false
  1169. ! !
  1170. !UndefinedObject class methodsFor: 'instance creation'!
  1171. new
  1172. self error: 'You cannot create new instances of UndefinedObject. Use nil'
  1173. ! !
  1174. Object subclass: #Collection
  1175. instanceVariableNames: ''
  1176. category: 'Kernel'!
  1177. !Collection methodsFor: 'accessing'!
  1178. size
  1179. self subclassResponsibility
  1180. !
  1181. readStream
  1182. ^self stream
  1183. !
  1184. writeStream
  1185. ^self stream
  1186. !
  1187. stream
  1188. ^self streamClass on: self
  1189. !
  1190. streamClass
  1191. ^self class streamClass
  1192. ! !
  1193. !Collection methodsFor: 'adding/removing'!
  1194. add: anObject
  1195. self subclassResponsibility
  1196. !
  1197. addAll: aCollection
  1198. aCollection do: [:each |
  1199. self add: each].
  1200. ^aCollection
  1201. !
  1202. remove: anObject
  1203. self subclassResponsibility
  1204. ! !
  1205. !Collection methodsFor: 'converting'!
  1206. asArray
  1207. | array index |
  1208. array := Array new.
  1209. index := 0.
  1210. self do: [:each |
  1211. index := index + 1.
  1212. array at: index put: each].
  1213. ^array
  1214. !
  1215. asSet
  1216. ^Set withAll: self
  1217. ! !
  1218. !Collection methodsFor: 'copying'!
  1219. , aCollection
  1220. ^self copy
  1221. addAll: aCollection;
  1222. yourself
  1223. !
  1224. copyWith: anObject
  1225. ^self copy add: anObject; yourself
  1226. !
  1227. copyWithAll: aCollection
  1228. ^self copy addAll: aCollection; yourself
  1229. !
  1230. copyWithoutAll: aCollection
  1231. "Answer a copy of the receiver that does not contain any elements
  1232. equal to those in aCollection."
  1233. ^ self reject: [:each | aCollection includes: each]
  1234. ! !
  1235. !Collection methodsFor: 'enumerating'!
  1236. do: aBlock
  1237. <for(var i=0;i<self.length;i++){aBlock(self[i]);}>
  1238. !
  1239. collect: aBlock
  1240. | newCollection |
  1241. newCollection := self class new.
  1242. self do: [:each |
  1243. newCollection add: (aBlock value: each)].
  1244. ^newCollection
  1245. !
  1246. detect: aBlock
  1247. ^self detect: aBlock ifNone: [self errorNotFound]
  1248. !
  1249. detect: aBlock ifNone: anotherBlock
  1250. <
  1251. for(var i = 0; i < self.length; i++)
  1252. if(aBlock(self[i]))
  1253. return self[i];
  1254. return anotherBlock();
  1255. >
  1256. !
  1257. do: aBlock separatedBy: anotherBlock
  1258. | first |
  1259. first := true.
  1260. self do: [:each |
  1261. first
  1262. ifTrue: [first := false]
  1263. ifFalse: [anotherBlock value].
  1264. aBlock value: each]
  1265. !
  1266. inject: anObject into: aBlock
  1267. | result |
  1268. result := anObject.
  1269. self do: [:each |
  1270. result := aBlock value: result value: each].
  1271. ^result
  1272. !
  1273. reject: aBlock
  1274. ^self select: [:each | (aBlock value: each) = false]
  1275. !
  1276. select: aBlock
  1277. | stream |
  1278. stream := self class new writeStream.
  1279. self do: [:each |
  1280. (aBlock value: each) ifTrue: [
  1281. stream nextPut: each]].
  1282. ^stream contents
  1283. ! !
  1284. !Collection methodsFor: 'error handling'!
  1285. errorNotFound
  1286. self error: 'Object is not in the collection'
  1287. ! !
  1288. !Collection methodsFor: 'testing'!
  1289. includes: anObject
  1290. <
  1291. var i = self.length;
  1292. while (i--) {
  1293. if (smalltalk.send(self[i], "__eq", [anObject])) {return true;}
  1294. }
  1295. return false
  1296. >
  1297. !
  1298. notEmpty
  1299. ^self isEmpty not
  1300. !
  1301. isEmpty
  1302. ^self size = 0
  1303. !
  1304. ifNotEmpty: aBlock
  1305. self notEmpty ifTrue: aBlock.
  1306. !
  1307. ifEmpty: aBlock
  1308. self isEmpty ifTrue: aBlock.
  1309. ! !
  1310. !Collection class methodsFor: 'accessing'!
  1311. streamClass
  1312. ^Stream
  1313. ! !
  1314. !Collection class methodsFor: 'instance creation'!
  1315. with: anObject
  1316. ^self new
  1317. add: anObject;
  1318. yourself
  1319. !
  1320. with: anObject with: anotherObject
  1321. ^self new
  1322. add: anObject;
  1323. add: anotherObject;
  1324. yourself
  1325. !
  1326. with: firstObject with: secondObject with: thirdObject
  1327. ^self new
  1328. add: firstObject;
  1329. add: secondObject;
  1330. add: thirdObject;
  1331. yourself
  1332. !
  1333. withAll: aCollection
  1334. ^self new
  1335. addAll: aCollection;
  1336. yourself
  1337. ! !
  1338. Collection subclass: #SequenceableCollection
  1339. instanceVariableNames: ''
  1340. category: 'Kernel'!
  1341. !SequenceableCollection methodsFor: 'accessing'!
  1342. at: anIndex
  1343. ^self at: anIndex ifAbsent: [
  1344. self errorNotFound]
  1345. !
  1346. at: anIndex ifAbsent: aBlock
  1347. self subclassResponsibility
  1348. !
  1349. at: anIndex put: anObject
  1350. self subclassResponsibility
  1351. !
  1352. first
  1353. ^self at: 1
  1354. !
  1355. fourth
  1356. ^self at: 4
  1357. !
  1358. last
  1359. ^self at: self size
  1360. !
  1361. second
  1362. ^self at: 2
  1363. !
  1364. third
  1365. ^self at: 3
  1366. !
  1367. allButFirst
  1368. ^self copyFrom: 2 to: self size
  1369. !
  1370. allButLast
  1371. ^self copyFrom: 1 to: self size - 1
  1372. !
  1373. indexOf: anObject
  1374. ^self indexOf: anObject ifAbsent: [self errorNotFound]
  1375. !
  1376. indexOf: anObject ifAbsent: aBlock
  1377. <
  1378. for(var i=0;i<self.length;i++){
  1379. if(self[i].__eq(anObject)) {return i+1}
  1380. }
  1381. return aBlock();
  1382. >
  1383. !
  1384. indexOf: anObject startingAt: start ifAbsent: aBlock
  1385. <
  1386. for(var i=start-1;i<self.length;i++){
  1387. if(self[i].__eq(anObject)) {return i+1}
  1388. }
  1389. return aBlock();
  1390. >
  1391. !
  1392. indexOf: anObject startingAt: start
  1393. "Answer the index of the first occurence of anElement after start
  1394. within the receiver. If the receiver does not contain anElement,
  1395. answer 0."
  1396. ^self indexOf: anObject startingAt: start ifAbsent: [0]
  1397. !
  1398. atRandom
  1399. ^ self at: self size atRandom
  1400. ! !
  1401. !SequenceableCollection methodsFor: 'adding'!
  1402. removeLast
  1403. self remove: self last
  1404. !
  1405. addLast: anObject
  1406. self add: anObject
  1407. ! !
  1408. !SequenceableCollection methodsFor: 'converting'!
  1409. reversed
  1410. self subclassResponsibility
  1411. ! !
  1412. !SequenceableCollection methodsFor: 'copying'!
  1413. copyFrom: anIndex to: anotherIndex
  1414. self subclassResponsibility
  1415. ! !
  1416. !SequenceableCollection methodsFor: 'enumerating'!
  1417. withIndexDo: aBlock
  1418. <for(var i=0;i<self.length;i++){aBlock(self[i], i+1);}>
  1419. ! !
  1420. SequenceableCollection subclass: #String
  1421. instanceVariableNames: ''
  1422. category: 'Kernel'!
  1423. !String methodsFor: 'accessing'!
  1424. size
  1425. <return self.length>
  1426. !
  1427. at: anIndex
  1428. <return self[anIndex - 1]>
  1429. !
  1430. at: anIndex put: anObject
  1431. self errorReadOnly
  1432. !
  1433. at: anIndex ifAbsent: aBlock
  1434. (self at: anIndex) ifNil: [aBlock]
  1435. !
  1436. escaped
  1437. <return escape(self)>
  1438. !
  1439. unescaped
  1440. <return unescape(self)>
  1441. !
  1442. asciiValue
  1443. <return self.charCodeAt(0);>
  1444. ! !
  1445. !String methodsFor: 'adding'!
  1446. add: anObject
  1447. self errorReadOnly
  1448. !
  1449. remove: anObject
  1450. self errorReadOnly
  1451. ! !
  1452. !String methodsFor: 'comparing'!
  1453. = aString
  1454. aString class = self class ifFalse: [^false].
  1455. <return String(self) == aString>
  1456. !
  1457. > aString
  1458. <return String(self) >> aString>
  1459. !
  1460. < aString
  1461. <return String(self) < aString>
  1462. !
  1463. >= aString
  1464. <return String(self) >>= aString>
  1465. !
  1466. <= aString
  1467. <return String(self) <= aString>
  1468. !
  1469. == aString
  1470. aString class = self class ifFalse: [^false].
  1471. <return String(self) === String(aString)>
  1472. ! !
  1473. !String methodsFor: 'converting'!
  1474. asSelector
  1475. "If you change this method, change smalltalk.convertSelector too (see js/boot.js file)"
  1476. | selector |
  1477. selector := '_', self.
  1478. selector := selector replace: ':' with: '_'.
  1479. selector := selector replace: '[+]' with: '_plus'.
  1480. selector := selector replace: '-' with: '_minus'.
  1481. selector := selector replace: '[*]' with: '_star'.
  1482. selector := selector replace: '[/]' with: '_slash'.
  1483. selector := selector replace: '>' with: '_gt'.
  1484. selector := selector replace: '<' with: '_lt'.
  1485. selector := selector replace: '=' with: '_eq'.
  1486. selector := selector replace: ',' with: '_comma'.
  1487. selector := selector replace: '[@]' with: '_at'.
  1488. ^selector
  1489. !
  1490. asJavascript
  1491. <
  1492. if(self.search(/^[a-zA-Z0-9_:.$ ]*$/) == -1)
  1493. return "unescape(\"" + escape(self) + "\")";
  1494. else
  1495. return "\"" + self + "\"";
  1496. >
  1497. !
  1498. tokenize: aString
  1499. <return self.split(aString)>
  1500. !
  1501. asString
  1502. ^self
  1503. !
  1504. asNumber
  1505. <return Number(self)>
  1506. !
  1507. asJSONObject
  1508. ^self
  1509. !
  1510. asLowercase
  1511. <return self.toLowerCase()>
  1512. !
  1513. asUppercase
  1514. <return self.toUpperCase()>
  1515. !
  1516. reversed
  1517. <return self.split("").reverse().join("")>
  1518. !
  1519. asJavaScriptSelector
  1520. ^(self asSelector replace: '^_' with: '') replace: '_.*' with: ''.
  1521. ! !
  1522. !String methodsFor: 'copying'!
  1523. , aString
  1524. <return self + aString>
  1525. !
  1526. copyFrom: anIndex to: anotherIndex
  1527. <return self.substring(anIndex - 1, anotherIndex)>
  1528. !
  1529. shallowCopy
  1530. ^self class fromString: self
  1531. !
  1532. deepCopy
  1533. ^self shallowCopy
  1534. ! !
  1535. !String methodsFor: 'error handling'!
  1536. errorReadOnly
  1537. self error: 'Object is read-only'
  1538. ! !
  1539. !String methodsFor: 'printing'!
  1540. printString
  1541. ^'''', self, ''''
  1542. !
  1543. printNl
  1544. <console.log(self)>
  1545. ! !
  1546. !String methodsFor: 'regular expressions'!
  1547. replace: aString with: anotherString
  1548. ^self replaceRegexp: (RegularExpression fromString: aString flag: 'g') with: anotherString
  1549. !
  1550. replaceRegexp: aRegexp with: aString
  1551. <return self.replace(aRegexp, aString)>
  1552. !
  1553. match: aRegexp
  1554. <return self.search(aRegexp) !!= -1>
  1555. !
  1556. trimLeft: separators
  1557. ^self replaceRegexp: (RegularExpression fromString: '^[', separators, ']+' flag: 'g') with: ''
  1558. !
  1559. trimRight: separators
  1560. ^self replaceRegexp: (RegularExpression fromString: '[', separators, ']+$' flag: 'g') with: ''
  1561. !
  1562. trimLeft
  1563. ^self trimLeft: '\s'
  1564. !
  1565. trimRight
  1566. ^self trimRight: '\s'
  1567. !
  1568. trimBoth
  1569. ^self trimBoth: '\s'
  1570. !
  1571. trimBoth: separators
  1572. ^(self trimLeft: separators) trimRight: separators
  1573. ! !
  1574. !String methodsFor: 'split join'!
  1575. join: aCollection
  1576. ^ String
  1577. streamContents: [:stream | aCollection
  1578. do: [:each | stream nextPutAll: each asString]
  1579. separatedBy: [stream nextPutAll: self]]
  1580. !
  1581. lineIndicesDo: aBlock
  1582. "execute aBlock with 3 arguments for each line:
  1583. - start index of line
  1584. - end index of line without line delimiter
  1585. - end index of line including line delimiter(s) CR, LF or CRLF"
  1586. | cr lf start sz nextLF nextCR |
  1587. start := 1.
  1588. sz := self size.
  1589. cr := String cr.
  1590. nextCR := self indexOf: cr startingAt: 1.
  1591. lf := String lf.
  1592. nextLF := self indexOf: lf startingAt: 1.
  1593. [ start <= sz ] whileTrue: [
  1594. (nextLF = 0 and: [ nextCR = 0 ])
  1595. ifTrue: [ "No more CR, nor LF, the string is over"
  1596. aBlock value: start value: sz value: sz.
  1597. ^self ].
  1598. (nextCR = 0 or: [ 0 < nextLF and: [ nextLF < nextCR ] ])
  1599. ifTrue: [ "Found a LF"
  1600. aBlock value: start value: nextLF - 1 value: nextLF.
  1601. start := 1 + nextLF.
  1602. nextLF := self indexOf: lf startingAt: start ]
  1603. ifFalse: [ 1 + nextCR = nextLF
  1604. ifTrue: [ "Found a CR-LF pair"
  1605. aBlock value: start value: nextCR - 1 value: nextLF.
  1606. start := 1 + nextLF.
  1607. nextCR := self indexOf: cr startingAt: start.
  1608. nextLF := self indexOf: lf startingAt: start ]
  1609. ifFalse: [ "Found a CR"
  1610. aBlock value: start value: nextCR - 1 value: nextCR.
  1611. start := 1 + nextCR.
  1612. nextCR := self indexOf: cr startingAt: start ]]]
  1613. !
  1614. linesDo: aBlock
  1615. "Execute aBlock with each line in this string. The terminating line
  1616. delimiters CR, LF or CRLF pairs are not included in what is passed to aBlock"
  1617. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1618. aBlock value: (self copyFrom: start to: endWithoutDelimiters)]
  1619. !
  1620. lines
  1621. "Answer an array of lines composing this receiver without the line ending delimiters."
  1622. | lines |
  1623. lines := Array new.
  1624. self linesDo: [:aLine | lines add: aLine].
  1625. ^lines
  1626. !
  1627. lineNumber: anIndex
  1628. "Answer a string containing the characters in the given line number."
  1629. | lineCount |
  1630. lineCount := 0.
  1631. self lineIndicesDo: [:start :endWithoutDelimiters :end |
  1632. (lineCount := lineCount + 1) = anIndex ifTrue: [^self copyFrom: start to: endWithoutDelimiters]].
  1633. ^nil
  1634. ! !
  1635. !String methodsFor: 'testing'!
  1636. isString
  1637. ^true
  1638. !
  1639. includesSubString: subString
  1640. < return self.indexOf(subString) !!= -1 >
  1641. ! !
  1642. !String class methodsFor: 'accessing'!
  1643. streamClass
  1644. ^StringStream
  1645. !
  1646. cr
  1647. <return '\r'>
  1648. !
  1649. lf
  1650. <return '\n'>
  1651. !
  1652. space
  1653. <return ' '>
  1654. !
  1655. tab
  1656. <return '\t'>
  1657. !
  1658. crlf
  1659. <return '\r\n'>
  1660. ! !
  1661. !String class methodsFor: 'instance creation'!
  1662. fromString: aString
  1663. <return new self.fn(aString)>
  1664. !
  1665. streamContents: blockWithArg
  1666. |stream|
  1667. stream := (self streamClass on: String new).
  1668. blockWithArg value: stream.
  1669. ^ stream contents
  1670. !
  1671. value: aUTFCharCode
  1672. <return String.fromCharCode(aUTFCharCode);>
  1673. ! !
  1674. SequenceableCollection subclass: #Array
  1675. instanceVariableNames: ''
  1676. category: 'Kernel'!
  1677. !Array methodsFor: 'accessing'!
  1678. size
  1679. <return self.length>
  1680. !
  1681. at: anIndex put: anObject
  1682. <return self[anIndex - 1] = anObject>
  1683. !
  1684. at: anIndex ifAbsent: aBlock
  1685. <
  1686. var value = self[anIndex - 1];
  1687. if(value === undefined) {
  1688. return aBlock();
  1689. } else {
  1690. return value;
  1691. }
  1692. >
  1693. ! !
  1694. !Array methodsFor: 'adding/removing'!
  1695. add: anObject
  1696. <self.push(anObject); return anObject;>
  1697. !
  1698. remove: anObject
  1699. <
  1700. for(var i=0;i<self.length;i++) {
  1701. if(self[i] == anObject) {
  1702. self.splice(i,1);
  1703. break;
  1704. }
  1705. }
  1706. >
  1707. !
  1708. removeFrom: aNumber to: anotherNumber
  1709. <self.splice(aNumber - 1,anotherNumber - 1)>
  1710. ! !
  1711. !Array methodsFor: 'comparing'!
  1712. = aCollection
  1713. (self class = aCollection class and: [
  1714. self size = aCollection size]) ifFalse: [^false].
  1715. self withIndexDo: [:each :i |
  1716. (aCollection at: i) = each ifFalse: [^false]].
  1717. ^true
  1718. ! !
  1719. !Array methodsFor: 'converting'!
  1720. asJavascript
  1721. ^'[', ((self collect: [:each | each asJavascript]) join: ', '), ']'
  1722. !
  1723. asJSONObject
  1724. ^self collect: [:each | each asJSONObject]
  1725. !
  1726. reversed
  1727. <return self._copy().reverse()>
  1728. ! !
  1729. !Array methodsFor: 'copying'!
  1730. shallowCopy
  1731. | newCollection |
  1732. newCollection := self class new.
  1733. self do: [:each | newCollection add: each].
  1734. ^newCollection
  1735. !
  1736. deepCopy
  1737. | newCollection |
  1738. newCollection := self class new.
  1739. self do: [:each | newCollection add: each deepCopy].
  1740. ^newCollection
  1741. !
  1742. copyFrom: anIndex to: anotherIndex
  1743. | array |
  1744. array := self class new.
  1745. anIndex to: anotherIndex do: [:each |
  1746. array add: (self at: each)].
  1747. ^array
  1748. ! !
  1749. !Array methodsFor: 'enumerating'!
  1750. join: aString
  1751. <return self.join(aString)>
  1752. !
  1753. sort
  1754. ^self basicPerform: 'sort'
  1755. !
  1756. sort: aBlock
  1757. <
  1758. return self.sort(function(a, b) {
  1759. if(aBlock(a,b)) {return -1} else {return 1}
  1760. })
  1761. >
  1762. !
  1763. sorted
  1764. ^self copy sort
  1765. !
  1766. sorted: aBlock
  1767. ^self copy sort: aBlock
  1768. !
  1769. printString
  1770. | str |
  1771. str := '' writeStream.
  1772. str nextPutAll: super printString, ' ('.
  1773. self
  1774. do: [:each | str nextPutAll: each printString]
  1775. separatedBy: [str nextPutAll: ' '].
  1776. str nextPutAll: ')'.
  1777. ^str contents
  1778. ! !
  1779. Object subclass: #RegularExpression
  1780. instanceVariableNames: ''
  1781. category: 'Kernel'!
  1782. !RegularExpression methodsFor: 'evaluating'!
  1783. compile: aString
  1784. <return self.compile(aString)>
  1785. !
  1786. exec: aString
  1787. <return self.exec(aString) || nil>
  1788. !
  1789. test: aString
  1790. <return self.test(aString)>
  1791. ! !
  1792. !RegularExpression class methodsFor: 'instance creation'!
  1793. fromString: aString flag: anotherString
  1794. <return new RegExp(aString, anotherString)>
  1795. !
  1796. fromString: aString
  1797. ^self fromString: aString flag: ''
  1798. ! !
  1799. Object subclass: #Error
  1800. instanceVariableNames: 'messageText'
  1801. category: 'Kernel'!
  1802. !Error methodsFor: 'accessing'!
  1803. messageText
  1804. ^messageText
  1805. !
  1806. messageText: aString
  1807. messageText := aString
  1808. !
  1809. context
  1810. <return self.context>
  1811. !
  1812. jsStack
  1813. <return self.stack>
  1814. ! !
  1815. !Error methodsFor: 'signaling'!
  1816. signal
  1817. <self.context = smalltalk.getThisContext(); self.smalltalkError = true; throw(self)>
  1818. ! !
  1819. !Error methodsFor: 'testing'!
  1820. isSmalltalkError
  1821. <return self.smalltalkError === true>
  1822. ! !
  1823. !Error class methodsFor: 'instance creation'!
  1824. signal: aString
  1825. ^self new
  1826. messageText: aString;
  1827. signal
  1828. ! !
  1829. Object subclass: #MethodContext
  1830. instanceVariableNames: ''
  1831. category: 'Kernel'!
  1832. !MethodContext methodsFor: 'accessing'!
  1833. receiver
  1834. <return self.receiver>
  1835. !
  1836. selector
  1837. <return smalltalk.convertSelector(self.selector)>
  1838. !
  1839. home
  1840. <return self.homeContext>
  1841. !
  1842. temps
  1843. <return self.temps>
  1844. !
  1845. printString
  1846. ^super printString, '(', self asString, ')'
  1847. !
  1848. asString
  1849. ^self receiver class printString, ' >> ', self selector
  1850. ! !
  1851. Object subclass: #Association
  1852. instanceVariableNames: 'key value'
  1853. category: 'Kernel'!
  1854. !Association methodsFor: 'accessing'!
  1855. key: aKey
  1856. key := aKey
  1857. !
  1858. key
  1859. ^key
  1860. !
  1861. value: aValue
  1862. value := aValue
  1863. !
  1864. value
  1865. ^value
  1866. ! !
  1867. !Association methodsFor: 'comparing'!
  1868. = anAssociation
  1869. ^self class = anAssociation class and: [
  1870. self key = anAssociation key and: [
  1871. self value = anAssociation value]]
  1872. !
  1873. storeOn: aStream
  1874. "Store in the format (key->value)"
  1875. "aStream nextPutAll: '('."
  1876. key storeOn: aStream.
  1877. aStream nextPutAll: '->'.
  1878. value storeOn: aStream.
  1879. "aStream nextPutAll: ')'"
  1880. ! !
  1881. !Association class methodsFor: 'instance creation'!
  1882. key: aKey value: aValue
  1883. ^self new
  1884. key: aKey;
  1885. value: aValue;
  1886. yourself
  1887. ! !
  1888. Collection subclass: #Dictionary
  1889. instanceVariableNames: 'keys'
  1890. category: 'Kernel'!
  1891. !Dictionary methodsFor: 'accessing'!
  1892. size
  1893. ^keys size
  1894. !
  1895. associations
  1896. | associations |
  1897. associations := #().
  1898. keys do: [:each |
  1899. associations add: (Association key: each value: (self at: each))].
  1900. ^associations
  1901. !
  1902. keys
  1903. ^keys copy
  1904. !
  1905. values
  1906. ^keys collect: [:each | self at: each]
  1907. !
  1908. at: aKey put: aValue
  1909. (keys includes: aKey) ifFalse: [keys add: aKey].
  1910. ^self basicAt: aKey put: aValue
  1911. !
  1912. at: aKey ifAbsent: aBlock
  1913. ^(self keys includes: aKey)
  1914. ifTrue: [self basicAt: aKey]
  1915. ifFalse: aBlock
  1916. !
  1917. at: aKey ifAbsentPut: aBlock
  1918. ^self at: aKey ifAbsent: [
  1919. self at: aKey put: aBlock value]
  1920. !
  1921. at: aKey ifPresent: aBlock
  1922. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  1923. !
  1924. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  1925. ^(self basicAt: aKey)
  1926. ifNil: anotherBlock
  1927. ifNotNil: [aBlock value: (self at: aKey)]
  1928. !
  1929. at: aKey
  1930. ^self at: aKey ifAbsent: [self errorNotFound]
  1931. ! !
  1932. !Dictionary methodsFor: 'adding/removing'!
  1933. add: anAssociation
  1934. self at: anAssociation key put: anAssociation value
  1935. !
  1936. addAll: aDictionary
  1937. super addAll: aDictionary associations.
  1938. ^aDictionary
  1939. !
  1940. remove: aKey
  1941. self removeKey: aKey
  1942. !
  1943. removeKey: aKey
  1944. keys remove: aKey
  1945. ! !
  1946. !Dictionary methodsFor: 'comparing'!
  1947. = aDictionary
  1948. self class = aDictionary class ifFalse: [^false].
  1949. self size = aDictionary size ifFalse: [^false].
  1950. ^self associations = aDictionary associations
  1951. ! !
  1952. !Dictionary methodsFor: 'converting'!
  1953. asJSONObject
  1954. | object |
  1955. object := Object new.
  1956. self keysAndValuesDo: [:key :value |
  1957. object basicAt: key put: value asJSONObject].
  1958. ^object
  1959. ! !
  1960. !Dictionary methodsFor: 'copying'!
  1961. shallowCopy
  1962. | copy |
  1963. copy := self class new.
  1964. self associationsDo: [:each |
  1965. copy at: each key put: each value].
  1966. ^copy
  1967. !
  1968. , aCollection
  1969. self shouldNotImplement
  1970. !
  1971. copyFrom: anIndex to: anotherIndex
  1972. self shouldNotImplement
  1973. ! !
  1974. !Dictionary methodsFor: 'enumerating'!
  1975. associationsDo: aBlock
  1976. self associations do: aBlock
  1977. !
  1978. keysAndValuesDo: aBlock
  1979. self associationsDo: [:each |
  1980. aBlock value: each key value: each value]
  1981. !
  1982. do: aBlock
  1983. self values do: aBlock
  1984. !
  1985. select: aBlock
  1986. | newDict |
  1987. newDict := self class new.
  1988. self keysAndValuesDo: [:key :value |
  1989. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1990. ^newDict
  1991. !
  1992. collect: aBlock
  1993. | newDict |
  1994. newDict := self class new.
  1995. self keysAndValuesDo: [:key :value |
  1996. newDict at: key put: (aBlock value: value)].
  1997. ^newDict
  1998. !
  1999. detect: aBlock ifNone: anotherBlock
  2000. ^self values detect: aBlock ifNone: anotherBlock
  2001. !
  2002. includes: anObject
  2003. ^self values includes: anObject
  2004. ! !
  2005. !Dictionary methodsFor: 'initialization'!
  2006. initialize
  2007. super initialize.
  2008. keys := #()
  2009. ! !
  2010. !Dictionary methodsFor: 'printing'!
  2011. printString
  2012. ^ String streamContents: [:aStream|
  2013. aStream
  2014. nextPutAll: super printString;
  2015. nextPutAll: '('.
  2016. self associations
  2017. do: [:anAssociation|
  2018. aStream
  2019. nextPutAll: anAssociation key printString;
  2020. nextPutAll: ' -> ';
  2021. nextPutAll: anAssociation value printString]
  2022. separatedBy: [aStream nextPutAll: ' , '].
  2023. aStream nextPutAll: ')'.
  2024. ]
  2025. !
  2026. storeOn: aStream
  2027. aStream nextPutAll: '#{'.
  2028. self associations
  2029. do: [:each | each storeOn: aStream]
  2030. separatedBy: [ aStream nextPutAll: '. '].
  2031. aStream nextPutAll: '}'
  2032. ! !
  2033. !Dictionary class methodsFor: 'instance creation'!
  2034. fromPairs: aCollection
  2035. | dict |
  2036. dict := self new.
  2037. aCollection do: [:each | dict add: each].
  2038. ^dict
  2039. ! !
  2040. Object subclass: #ClassBuilder
  2041. instanceVariableNames: ''
  2042. category: 'Kernel'!
  2043. !ClassBuilder methodsFor: 'class creation'!
  2044. superclass: aClass subclass: aString
  2045. ^self superclass: aClass subclass: aString instanceVariableNames: '' package: nil
  2046. !
  2047. class: aClass instanceVariableNames: aString
  2048. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  2049. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  2050. self setupClass: aClass
  2051. !
  2052. superclass: aClass subclass: aString instanceVariableNames: aString2 package: aString3
  2053. | newClass |
  2054. newClass := self addSubclassOf: aClass
  2055. named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2)
  2056. package: (aString3 ifNil: ['unclassified']).
  2057. self setupClass: newClass.
  2058. ^newClass
  2059. ! !
  2060. !ClassBuilder methodsFor: 'private'!
  2061. instanceVariableNamesFor: aString
  2062. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  2063. !
  2064. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  2065. <smalltalk.addClass(aString, aClass, aCollection);
  2066. return smalltalk[aString]>
  2067. !
  2068. setupClass: aClass
  2069. <smalltalk.init(aClass);>
  2070. !
  2071. addSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  2072. <smalltalk.addClass(aString, aClass, aCollection, packageName);
  2073. return smalltalk[aString]>
  2074. !
  2075. copyClass: aClass named: aString
  2076. | newClass |
  2077. newClass := self
  2078. addSubclassOf: aClass superclass
  2079. named: aString
  2080. instanceVariableNames: aClass instanceVariableNames
  2081. package: aClass package name.
  2082. self setupClass: newClass.
  2083. aClass methodDictionary values do: [:each |
  2084. newClass addCompiledMethod: (Compiler new load: each source forClass: newClass).
  2085. (newClass methodDictionary at: each selector) category: each category].
  2086. aClass class methodDictionary values do: [:each |
  2087. newClass class addCompiledMethod: (Compiler new load: each source forClass: newClass class).
  2088. (newClass class methodDictionary at: each selector) category: each category].
  2089. self setupClass: newClass.
  2090. ^newClass
  2091. ! !
  2092. Object subclass: #ClassCategoryReader
  2093. instanceVariableNames: 'class category chunkParser'
  2094. category: 'Kernel'!
  2095. !ClassCategoryReader methodsFor: 'accessing'!
  2096. class: aClass category: aString
  2097. class := aClass.
  2098. category := aString
  2099. ! !
  2100. !ClassCategoryReader methodsFor: 'fileIn'!
  2101. scanFrom: aChunkParser
  2102. | chunk |
  2103. [chunk := aChunkParser nextChunk.
  2104. chunk isEmpty] whileFalse: [
  2105. self compileMethod: chunk]
  2106. ! !
  2107. !ClassCategoryReader methodsFor: 'initialization'!
  2108. initialize
  2109. super initialize.
  2110. chunkParser := ChunkParser new.
  2111. ! !
  2112. !ClassCategoryReader methodsFor: 'private'!
  2113. compileMethod: aString
  2114. | method |
  2115. method := Compiler new load: aString forClass: class.
  2116. method category: category.
  2117. class addCompiledMethod: method
  2118. ! !
  2119. Object subclass: #Stream
  2120. instanceVariableNames: 'collection position streamSize'
  2121. category: 'Kernel'!
  2122. !Stream methodsFor: 'accessing'!
  2123. collection
  2124. ^collection
  2125. !
  2126. setCollection: aCollection
  2127. collection := aCollection
  2128. !
  2129. position
  2130. ^position ifNil: [position := 0]
  2131. !
  2132. position: anInteger
  2133. position := anInteger
  2134. !
  2135. streamSize
  2136. ^streamSize
  2137. !
  2138. setStreamSize: anInteger
  2139. streamSize := anInteger
  2140. !
  2141. contents
  2142. ^self collection
  2143. copyFrom: 1
  2144. to: self streamSize
  2145. !
  2146. size
  2147. ^self streamSize
  2148. ! !
  2149. !Stream methodsFor: 'actions'!
  2150. reset
  2151. self position: 0
  2152. !
  2153. close
  2154. !
  2155. flush
  2156. !
  2157. resetContents
  2158. self reset.
  2159. self setStreamSize: 0
  2160. ! !
  2161. !Stream methodsFor: 'enumerating'!
  2162. do: aBlock
  2163. [self atEnd] whileFalse: [aBlock value: self next]
  2164. ! !
  2165. !Stream methodsFor: 'positioning'!
  2166. setToEnd
  2167. self position: self size
  2168. !
  2169. skip: anInteger
  2170. self position: ((self position + anInteger) min: self size max: 0)
  2171. ! !
  2172. !Stream methodsFor: 'reading'!
  2173. next
  2174. self position: self position + 1.
  2175. ^collection at: self position
  2176. !
  2177. next: anInteger
  2178. | tempCollection |
  2179. tempCollection := self collection class new.
  2180. anInteger timesRepeat: [
  2181. self atEnd ifFalse: [
  2182. tempCollection add: self next]].
  2183. ^tempCollection
  2184. !
  2185. peek
  2186. ^self atEnd ifFalse: [
  2187. self collection at: self position + 1]
  2188. ! !
  2189. !Stream methodsFor: 'testing'!
  2190. atEnd
  2191. ^self position = self size
  2192. !
  2193. atStart
  2194. ^self position = 0
  2195. !
  2196. isEmpty
  2197. ^self size = 0
  2198. ! !
  2199. !Stream methodsFor: 'writing'!
  2200. nextPut: anObject
  2201. self position: self position + 1.
  2202. self collection at: self position put: anObject.
  2203. self setStreamSize: (self streamSize max: self position)
  2204. !
  2205. nextPutAll: aCollection
  2206. aCollection do: [:each |
  2207. self nextPut: each]
  2208. ! !
  2209. !Stream class methodsFor: 'instance creation'!
  2210. on: aCollection
  2211. ^self new
  2212. setCollection: aCollection;
  2213. setStreamSize: aCollection size;
  2214. yourself
  2215. ! !
  2216. Stream subclass: #StringStream
  2217. instanceVariableNames: ''
  2218. category: 'Kernel'!
  2219. !StringStream methodsFor: 'reading'!
  2220. next: anInteger
  2221. | tempCollection |
  2222. tempCollection := self collection class new.
  2223. anInteger timesRepeat: [
  2224. self atEnd ifFalse: [
  2225. tempCollection := tempCollection, self next]].
  2226. ^tempCollection
  2227. ! !
  2228. !StringStream methodsFor: 'writing'!
  2229. nextPut: aString
  2230. self nextPutAll: aString
  2231. !
  2232. nextPutAll: aString
  2233. self setCollection:
  2234. (self collection copyFrom: 1 to: self position),
  2235. aString,
  2236. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  2237. self position: self position + aString size.
  2238. self setStreamSize: (self streamSize max: self position)
  2239. !
  2240. cr
  2241. ^self nextPutAll: String cr
  2242. !
  2243. crlf
  2244. ^self nextPutAll: String crlf
  2245. !
  2246. lf
  2247. ^self nextPutAll: String lf
  2248. !
  2249. space
  2250. self nextPut: ' '
  2251. ! !
  2252. Object subclass: #ClassCommentReader
  2253. instanceVariableNames: 'class chunkParser'
  2254. category: 'Kernel'!
  2255. !ClassCommentReader methodsFor: 'accessing'!
  2256. class: aClass
  2257. class := aClass
  2258. ! !
  2259. !ClassCommentReader methodsFor: 'fileIn'!
  2260. scanFrom: aChunkParser
  2261. | chunk |
  2262. chunk := aChunkParser nextChunk.
  2263. chunk isEmpty ifFalse: [
  2264. self setComment: chunk].
  2265. ! !
  2266. !ClassCommentReader methodsFor: 'initialization'!
  2267. initialize
  2268. super initialize.
  2269. chunkParser := ChunkParser new.
  2270. ! !
  2271. !ClassCommentReader methodsFor: 'private'!
  2272. setComment: aString
  2273. class comment: aString
  2274. ! !
  2275. Object subclass: #Random
  2276. instanceVariableNames: ''
  2277. category: 'Kernel'!
  2278. !Random methodsFor: 'accessing'!
  2279. next
  2280. <return Math.random()>
  2281. !
  2282. next: anInteger
  2283. ^(1 to: anInteger) collect: [:each | self next]
  2284. ! !
  2285. Object subclass: #Point
  2286. instanceVariableNames: 'x y'
  2287. category: 'Kernel'!
  2288. !Point methodsFor: 'accessing'!
  2289. x
  2290. ^x
  2291. !
  2292. y
  2293. ^y
  2294. !
  2295. y: aNumber
  2296. y := aNumber
  2297. !
  2298. x: aNumber
  2299. x := aNumber
  2300. ! !
  2301. !Point methodsFor: 'arithmetic'!
  2302. * aPoint
  2303. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  2304. !
  2305. + aPoint
  2306. ^Point x: self x + aPoint asPoint x y: self y + aPoint asPoint y
  2307. !
  2308. - aPoint
  2309. ^Point x: self x - aPoint asPoint x y: self y - aPoint asPoint y
  2310. !
  2311. / aPoint
  2312. ^Point x: self x / aPoint asPoint x y: self y / aPoint asPoint y
  2313. !
  2314. = aPoint
  2315. ^aPoint class = self class and: [
  2316. (aPoint x = self x) & (aPoint y = self y)]
  2317. ! !
  2318. !Point methodsFor: 'converting'!
  2319. asPoint
  2320. ^self
  2321. ! !
  2322. !Point class methodsFor: 'instance creation'!
  2323. x: aNumber y: anotherNumber
  2324. ^self new
  2325. x: aNumber;
  2326. y: anotherNumber;
  2327. yourself
  2328. ! !
  2329. Object subclass: #Message
  2330. instanceVariableNames: 'selector arguments'
  2331. category: 'Kernel'!
  2332. !Message methodsFor: 'accessing'!
  2333. selector
  2334. ^selector
  2335. !
  2336. selector: aString
  2337. selector := aString
  2338. !
  2339. arguments: anArray
  2340. arguments := anArray
  2341. !
  2342. arguments
  2343. ^arguments
  2344. ! !
  2345. !Message methodsFor: 'printing'!
  2346. printString
  2347. ^ String streamContents: [:aStream|
  2348. aStream
  2349. nextPutAll: super printString;
  2350. nextPutAll: '(';
  2351. nextPutAll: selector;
  2352. nextPutAll: ')' ]
  2353. ! !
  2354. !Message class methodsFor: 'instance creation'!
  2355. selector: aString arguments: anArray
  2356. ^self new
  2357. selector: aString;
  2358. arguments: anArray;
  2359. yourself
  2360. ! !
  2361. Error subclass: #MessageNotUnderstood
  2362. instanceVariableNames: 'message receiver'
  2363. category: 'Kernel'!
  2364. !MessageNotUnderstood methodsFor: 'accessing'!
  2365. message
  2366. ^message
  2367. !
  2368. message: aMessage
  2369. message := aMessage
  2370. !
  2371. receiver
  2372. ^receiver
  2373. !
  2374. receiver: anObject
  2375. receiver := anObject
  2376. !
  2377. messageText
  2378. ^self receiver asString, ' does not understand #', self message selector
  2379. ! !
  2380. Object subclass: #ErrorHandler
  2381. instanceVariableNames: ''
  2382. category: 'Kernel'!
  2383. !ErrorHandler methodsFor: 'error handling'!
  2384. handleError: anError
  2385. anError context ifNotNil: [self logErrorContext: anError context].
  2386. self logError: anError
  2387. ! !
  2388. !ErrorHandler methodsFor: 'private'!
  2389. logContext: aContext
  2390. aContext home ifNotNil: [
  2391. self logContext: aContext home].
  2392. self log: aContext receiver asString, '>>', aContext selector
  2393. !
  2394. logErrorContext: aContext
  2395. aContext ifNotNil: [
  2396. aContext home ifNotNil: [
  2397. self logContext: aContext home]]
  2398. !
  2399. logError: anError
  2400. self log: anError messageText
  2401. !
  2402. log: aString
  2403. console log: aString
  2404. ! !
  2405. ErrorHandler class instanceVariableNames: 'current'!
  2406. !ErrorHandler class methodsFor: 'accessing'!
  2407. current
  2408. ^current ifNil: [current := self new]
  2409. !
  2410. setCurrent: anHandler
  2411. current := anHandler
  2412. ! !
  2413. !ErrorHandler class methodsFor: 'initialization'!
  2414. initialize
  2415. self register
  2416. !
  2417. register
  2418. ErrorHandler setCurrent: self new
  2419. ! !
  2420. Object subclass: #JSObjectProxy
  2421. instanceVariableNames: 'jsObject'
  2422. category: 'Kernel'!
  2423. !JSObjectProxy methodsFor: 'accessing'!
  2424. jsObject: aJSObject
  2425. jsObject := aJSObject
  2426. !
  2427. jsObject
  2428. ^jsObject
  2429. !
  2430. at: aString
  2431. <return self['@jsObject'][aString]>
  2432. !
  2433. at: aString put: anObject
  2434. <self['@jsObject'][aString] = anObject>
  2435. ! !
  2436. !JSObjectProxy methodsFor: 'proxy'!
  2437. printString
  2438. ^self jsObject toString
  2439. !
  2440. inspectOn: anInspector
  2441. | variables |
  2442. variables := Dictionary new.
  2443. variables at: '#self' put: self jsObject.
  2444. anInspector setLabel: self printString.
  2445. <for(var i in self['@jsObject']) {
  2446. variables._at_put_(i, self['@jsObject'][i]);
  2447. }>.
  2448. anInspector setVariables: variables
  2449. !
  2450. doesNotUnderstand: aMessage
  2451. | obj selector jsSelector arguments |
  2452. obj := self jsObject.
  2453. selector := aMessage selector.
  2454. jsSelector := selector asJavaScriptSelector.
  2455. arguments := aMessage arguments.
  2456. <if(obj[jsSelector] !!= undefined) {return smalltalk.send(obj, jsSelector, arguments)}>.
  2457. super doesNotUnderstand: aMessage
  2458. ! !
  2459. !JSObjectProxy class methodsFor: 'instance creation'!
  2460. on: aJSObject
  2461. ^self new
  2462. jsObject: aJSObject;
  2463. yourself
  2464. ! !
  2465. Collection subclass: #Set
  2466. instanceVariableNames: 'elements'
  2467. category: 'Kernel'!
  2468. !Set methodsFor: 'accessing'!
  2469. size
  2470. ^elements size
  2471. ! !
  2472. !Set methodsFor: 'adding/removing'!
  2473. add: anObject
  2474. <
  2475. var found;
  2476. for(var i in self['@elements']) {
  2477. if(anObject == self['@elements'][i]) {
  2478. found = true;
  2479. break;
  2480. }
  2481. }
  2482. if(!!found) {self['@elements'].push(anObject)}
  2483. >
  2484. !
  2485. remove: anObject
  2486. elements remove: anObject
  2487. ! !
  2488. !Set methodsFor: 'comparing'!
  2489. = aCollection
  2490. ^self class = aCollection class and: [
  2491. elements = aCollection asArray]
  2492. ! !
  2493. !Set methodsFor: 'converting'!
  2494. asArray
  2495. ^elements copy
  2496. ! !
  2497. !Set methodsFor: 'enumerating'!
  2498. detect: aBlock ifNone: anotherBlock
  2499. ^elements detect: aBlock ifNone: anotherBlock
  2500. !
  2501. do: aBlock
  2502. elements do: aBlock
  2503. ! !
  2504. !Set methodsFor: 'initialization'!
  2505. initialize
  2506. super initialize.
  2507. elements := #()
  2508. ! !
  2509. !Set methodsFor: 'testing'!
  2510. includes: anObject
  2511. ^elements includes: anObject
  2512. ! !
  2513. Object subclass: #Transcript
  2514. instanceVariableNames: 'textarea'
  2515. category: 'Kernel'!
  2516. Transcript class instanceVariableNames: 'current'!
  2517. !Transcript class methodsFor: 'instance creation'!
  2518. open
  2519. self current open
  2520. !
  2521. new
  2522. self shouldNotImplement
  2523. !
  2524. current
  2525. ^current
  2526. !
  2527. register: aTranscript
  2528. current := aTranscript
  2529. ! !
  2530. !Transcript class methodsFor: 'printing'!
  2531. show: anObject
  2532. self current show: anObject
  2533. !
  2534. cr
  2535. self current show: String cr
  2536. !
  2537. clear
  2538. self current clear
  2539. ! !
  2540. Object subclass: #ConsoleTranscript
  2541. instanceVariableNames: 'textarea'
  2542. category: 'Kernel'!
  2543. !ConsoleTranscript methodsFor: 'actions'!
  2544. open
  2545. ! !
  2546. !ConsoleTranscript methodsFor: 'printing'!
  2547. clear
  2548. "no op"
  2549. !
  2550. cr
  2551. "no op"
  2552. !
  2553. show: anObject
  2554. | string |
  2555. string := anObject asString.
  2556. <console.log(String(string))>
  2557. ! !
  2558. !ConsoleTranscript class methodsFor: 'initialization'!
  2559. initialize
  2560. Transcript register: self new
  2561. ! !
  2562. Dictionary subclass: #Dictionary2
  2563. instanceVariableNames: 'keys'
  2564. category: 'Kernel'!
  2565. !Dictionary2 methodsFor: 'accessing'!
  2566. size
  2567. ^keys size
  2568. !
  2569. associations
  2570. | associations |
  2571. associations := #().
  2572. keys do: [:each |
  2573. associations add: (Association key: each value: (self at: each))].
  2574. ^associations
  2575. !
  2576. keys
  2577. ^keys copy
  2578. !
  2579. values
  2580. ^keys collect: [:each | self at: each]
  2581. !
  2582. at: aKey put: aValue
  2583. (keys includes: aKey) ifFalse: [keys add: aKey].
  2584. ^self basicAt: aKey put: aValue
  2585. !
  2586. at: aKey ifAbsent: aBlock
  2587. ^(self keys includes: aKey)
  2588. ifTrue: [self basicAt: aKey]
  2589. ifFalse: aBlock
  2590. !
  2591. at: aKey ifAbsentPut: aBlock
  2592. ^self at: aKey ifAbsent: [
  2593. self at: aKey put: aBlock value]
  2594. !
  2595. at: aKey ifPresent: aBlock
  2596. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  2597. !
  2598. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  2599. ^(self basicAt: aKey)
  2600. ifNil: anotherBlock
  2601. ifNotNil: [aBlock value: (self at: aKey)]
  2602. !
  2603. at: aKey
  2604. ^self at: aKey ifAbsent: [self errorNotFound]
  2605. ! !
  2606. !Dictionary2 methodsFor: 'adding/removing'!
  2607. add: anAssociation
  2608. self at: anAssociation key put: anAssociation value
  2609. !
  2610. addAll: aDictionary
  2611. super addAll: aDictionary associations.
  2612. ^aDictionary
  2613. !
  2614. remove: aKey
  2615. self removeKey: aKey
  2616. !
  2617. removeKey: aKey
  2618. keys remove: aKey
  2619. ! !
  2620. !Dictionary2 methodsFor: 'comparing'!
  2621. = aDictionary
  2622. self class = aDictionary class ifFalse: [^false].
  2623. self size = aDictionary size ifFalse: [^false].
  2624. ^self associations = aDictionary associations
  2625. ! !
  2626. !Dictionary2 methodsFor: 'converting'!
  2627. asJSONObject
  2628. | object |
  2629. object := Object new.
  2630. self keysAndValuesDo: [:key :value |
  2631. object basicAt: key put: value asJSONObject].
  2632. ^object
  2633. ! !
  2634. !Dictionary2 methodsFor: 'copying'!
  2635. shallowCopy
  2636. | copy |
  2637. copy := self class new.
  2638. self associationsDo: [:each |
  2639. copy at: each key put: each value].
  2640. ^copy
  2641. !
  2642. , aCollection
  2643. self shouldNotImplement
  2644. !
  2645. copyFrom: anIndex to: anotherIndex
  2646. self shouldNotImplement
  2647. ! !
  2648. !Dictionary2 methodsFor: 'enumerating'!
  2649. associationsDo: aBlock
  2650. self associations do: aBlock
  2651. !
  2652. keysAndValuesDo: aBlock
  2653. self associationsDo: [:each |
  2654. aBlock value: each key value: each value]
  2655. !
  2656. do: aBlock
  2657. self values do: aBlock
  2658. !
  2659. select: aBlock
  2660. | newDict |
  2661. newDict := self class new.
  2662. self keysAndValuesDo: [:key :value |
  2663. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  2664. ^newDict
  2665. !
  2666. collect: aBlock
  2667. | newDict |
  2668. newDict := self class new.
  2669. self keysAndValuesDo: [:key :value |
  2670. newDict at: key put: (aBlock value: value)].
  2671. ^newDict
  2672. !
  2673. detect: aBlock ifNone: anotherBlock
  2674. ^self values detect: aBlock ifNone: anotherBlock
  2675. !
  2676. includes: anObject
  2677. ^self values includes: anObject
  2678. ! !
  2679. !Dictionary2 methodsFor: 'initialization'!
  2680. initialize
  2681. super initialize.
  2682. keys := #()
  2683. ! !
  2684. !Dictionary2 methodsFor: 'printing'!
  2685. printString
  2686. ^ String streamContents: [:aStream|
  2687. aStream
  2688. nextPutAll: super printString;
  2689. nextPutAll: '('.
  2690. self associations
  2691. do: [:anAssociation|
  2692. aStream
  2693. nextPutAll: anAssociation key printString;
  2694. nextPutAll: ' -> ';
  2695. nextPutAll: anAssociation value printString]
  2696. separatedBy: [aStream nextPutAll: ' , '].
  2697. aStream nextPutAll: ')'.
  2698. ]
  2699. !
  2700. storeOn: aStream
  2701. aStream nextPutAll: '#{'.
  2702. self associations
  2703. do: [:each | each storeOn: aStream]
  2704. separatedBy: [ aStream nextPutAll: '. '].
  2705. aStream nextPutAll: '}'
  2706. ! !