Kernel.st 60 KB

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