Kernel.st 56 KB

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