Kernel.st 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492
  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. !Error methodsFor: 'signaling'!
  1774. signal
  1775. <self.context = smalltalk.getThisContext(); self.smalltalkError = true; throw(self)>
  1776. ! !
  1777. !Error class methodsFor: 'instance creation'!
  1778. signal: aString
  1779. ^self new
  1780. messageText: aString;
  1781. signal
  1782. ! !
  1783. Object subclass: #MethodContext
  1784. instanceVariableNames: ''
  1785. category: 'Kernel'!
  1786. !MethodContext methodsFor: 'accessing'!
  1787. receiver
  1788. <return self.receiver>
  1789. !
  1790. selector
  1791. <return smalltalk.convertSelector(self.selector)>
  1792. !
  1793. home
  1794. <return self.homeContext>
  1795. !
  1796. temps
  1797. <return self.temps>
  1798. !
  1799. printString
  1800. ^super printString, '(', self asString, ')'
  1801. !
  1802. asString
  1803. ^self receiver class printString, ' >> ', self selector
  1804. ! !
  1805. Object subclass: #Association
  1806. instanceVariableNames: 'key value'
  1807. category: 'Kernel'!
  1808. !Association methodsFor: 'accessing'!
  1809. key: aKey
  1810. key := aKey
  1811. !
  1812. key
  1813. ^key
  1814. !
  1815. value: aValue
  1816. value := aValue
  1817. !
  1818. value
  1819. ^value
  1820. ! !
  1821. !Association methodsFor: 'comparing'!
  1822. = anAssociation
  1823. ^self class = anAssociation class and: [
  1824. self key = anAssociation key and: [
  1825. self value = anAssociation value]]
  1826. !
  1827. storeOn: aStream
  1828. "Store in the format (key->value)"
  1829. "aStream nextPutAll: '('."
  1830. key storeOn: aStream.
  1831. aStream nextPutAll: '->'.
  1832. value storeOn: aStream.
  1833. "aStream nextPutAll: ')'"
  1834. ! !
  1835. !Association class methodsFor: 'instance creation'!
  1836. key: aKey value: aValue
  1837. ^self new
  1838. key: aKey;
  1839. value: aValue;
  1840. yourself
  1841. ! !
  1842. Collection subclass: #Dictionary
  1843. instanceVariableNames: 'keys'
  1844. category: 'Kernel'!
  1845. !Dictionary methodsFor: 'accessing'!
  1846. size
  1847. ^keys size
  1848. !
  1849. associations
  1850. | associations |
  1851. associations := #().
  1852. keys do: [:each |
  1853. associations add: (Association key: each value: (self at: each))].
  1854. ^associations
  1855. !
  1856. keys
  1857. ^keys copy
  1858. !
  1859. values
  1860. ^keys collect: [:each | self at: each]
  1861. !
  1862. at: aKey put: aValue
  1863. (keys includes: aKey) ifFalse: [keys add: aKey].
  1864. ^self basicAt: aKey put: aValue
  1865. !
  1866. at: aKey ifAbsent: aBlock
  1867. ^(self keys includes: aKey)
  1868. ifTrue: [self basicAt: aKey]
  1869. ifFalse: aBlock
  1870. !
  1871. at: aKey ifAbsentPut: aBlock
  1872. ^self at: aKey ifAbsent: [
  1873. self at: aKey put: aBlock value]
  1874. !
  1875. at: aKey ifPresent: aBlock
  1876. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  1877. !
  1878. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  1879. ^(self basicAt: aKey)
  1880. ifNil: anotherBlock
  1881. ifNotNil: [aBlock value: (self at: aKey)]
  1882. !
  1883. at: aKey
  1884. ^self at: aKey ifAbsent: [self errorNotFound]
  1885. ! !
  1886. !Dictionary methodsFor: 'adding/removing'!
  1887. add: anAssociation
  1888. self at: anAssociation key put: anAssociation value
  1889. !
  1890. addAll: aDictionary
  1891. super addAll: aDictionary associations.
  1892. ^aDictionary
  1893. !
  1894. remove: aKey
  1895. self removeKey: aKey
  1896. !
  1897. removeKey: aKey
  1898. keys remove: aKey
  1899. ! !
  1900. !Dictionary methodsFor: 'comparing'!
  1901. = aDictionary
  1902. self class = aDictionary class ifFalse: [^false].
  1903. self size = aDictionary size ifFalse: [^false].
  1904. ^self associations = aDictionary associations
  1905. ! !
  1906. !Dictionary methodsFor: 'converting'!
  1907. asJSONObject
  1908. | object |
  1909. object := Object new.
  1910. self keysAndValuesDo: [:key :value |
  1911. object basicAt: key put: value asJSONObject].
  1912. ^object
  1913. ! !
  1914. !Dictionary methodsFor: 'copying'!
  1915. shallowCopy
  1916. | copy |
  1917. copy := self class new.
  1918. self associationsDo: [:each |
  1919. copy at: each key put: each value].
  1920. ^copy
  1921. !
  1922. , aCollection
  1923. self shouldNotImplement
  1924. !
  1925. copyFrom: anIndex to: anotherIndex
  1926. self shouldNotImplement
  1927. ! !
  1928. !Dictionary methodsFor: 'enumerating'!
  1929. associationsDo: aBlock
  1930. self associations do: aBlock
  1931. !
  1932. keysAndValuesDo: aBlock
  1933. self associationsDo: [:each |
  1934. aBlock value: each key value: each value]
  1935. !
  1936. do: aBlock
  1937. self values do: aBlock
  1938. !
  1939. select: aBlock
  1940. | newDict |
  1941. newDict := self class new.
  1942. self keysAndValuesDo: [:key :value |
  1943. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  1944. ^newDict
  1945. !
  1946. collect: aBlock
  1947. | newDict |
  1948. newDict := self class new.
  1949. self keysAndValuesDo: [:key :value |
  1950. newDict at: key put: (aBlock value: value)].
  1951. ^newDict
  1952. !
  1953. detect: aBlock ifNone: anotherBlock
  1954. ^self values detect: aBlock ifNone: anotherBlock
  1955. !
  1956. includes: anObject
  1957. ^self values includes: anObject
  1958. ! !
  1959. !Dictionary methodsFor: 'initialization'!
  1960. initialize
  1961. super initialize.
  1962. keys := #()
  1963. ! !
  1964. !Dictionary methodsFor: 'printing'!
  1965. printString
  1966. ^ String streamContents: [:aStream|
  1967. aStream
  1968. nextPutAll: super printString;
  1969. nextPutAll: '('.
  1970. self associations
  1971. do: [:anAssociation|
  1972. aStream
  1973. nextPutAll: anAssociation key printString;
  1974. nextPutAll: ' -> ';
  1975. nextPutAll: anAssociation value printString]
  1976. separatedBy: [aStream nextPutAll: ' , '].
  1977. aStream nextPutAll: ')'.
  1978. ]
  1979. !
  1980. storeOn: aStream
  1981. aStream nextPutAll: '#{'.
  1982. self associations
  1983. do: [:each | each storeOn: aStream]
  1984. separatedBy: [ aStream nextPutAll: '. '].
  1985. aStream nextPutAll: '}'
  1986. ! !
  1987. !Dictionary class methodsFor: 'instance creation'!
  1988. fromPairs: aCollection
  1989. | dict |
  1990. dict := self new.
  1991. aCollection do: [:each | dict add: each].
  1992. ^dict
  1993. ! !
  1994. Object subclass: #ClassBuilder
  1995. instanceVariableNames: ''
  1996. category: 'Kernel'!
  1997. !ClassBuilder methodsFor: 'class creation'!
  1998. superclass: aClass subclass: aString
  1999. ^self superclass: aClass subclass: aString instanceVariableNames: '' package: nil
  2000. !
  2001. class: aClass instanceVariableNames: aString
  2002. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  2003. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  2004. self setupClass: aClass
  2005. !
  2006. superclass: aClass subclass: aString instanceVariableNames: aString2 package: aString3
  2007. | newClass |
  2008. newClass := self addSubclassOf: aClass
  2009. named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2)
  2010. package: (aString3 ifNil: ['unclassified']).
  2011. self setupClass: newClass.
  2012. ^newClass
  2013. ! !
  2014. !ClassBuilder methodsFor: 'private'!
  2015. instanceVariableNamesFor: aString
  2016. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  2017. !
  2018. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  2019. <smalltalk.addClass(aString, aClass, aCollection);
  2020. return smalltalk[aString]>
  2021. !
  2022. setupClass: aClass
  2023. <smalltalk.init(aClass);>
  2024. !
  2025. addSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  2026. <smalltalk.addClass(aString, aClass, aCollection, packageName);
  2027. return smalltalk[aString]>
  2028. !
  2029. copyClass: aClass named: aString
  2030. | newClass |
  2031. newClass := self
  2032. addSubclassOf: aClass superclass
  2033. named: aString
  2034. instanceVariableNames: aClass instanceVariableNames
  2035. package: aClass package name.
  2036. self setupClass: newClass.
  2037. aClass methodDictionary values do: [:each |
  2038. newClass addCompiledMethod: (Compiler new load: each source forClass: newClass).
  2039. (newClass methodDictionary at: each selector) category: each category].
  2040. aClass class methodDictionary values do: [:each |
  2041. newClass class addCompiledMethod: (Compiler new load: each source forClass: newClass class).
  2042. (newClass class methodDictionary at: each selector) category: each category].
  2043. self setupClass: newClass.
  2044. ^newClass
  2045. ! !
  2046. Object subclass: #ClassCategoryReader
  2047. instanceVariableNames: 'class category chunkParser'
  2048. category: 'Kernel'!
  2049. !ClassCategoryReader methodsFor: 'accessing'!
  2050. class: aClass category: aString
  2051. class := aClass.
  2052. category := aString
  2053. ! !
  2054. !ClassCategoryReader methodsFor: 'fileIn'!
  2055. scanFrom: aChunkParser
  2056. | chunk |
  2057. [chunk := aChunkParser nextChunk.
  2058. chunk isEmpty] whileFalse: [
  2059. self compileMethod: chunk]
  2060. ! !
  2061. !ClassCategoryReader methodsFor: 'initialization'!
  2062. initialize
  2063. super initialize.
  2064. chunkParser := ChunkParser new.
  2065. ! !
  2066. !ClassCategoryReader methodsFor: 'private'!
  2067. compileMethod: aString
  2068. | method |
  2069. method := Compiler new load: aString forClass: class.
  2070. method category: category.
  2071. class addCompiledMethod: method
  2072. ! !
  2073. Object subclass: #Stream
  2074. instanceVariableNames: 'collection position streamSize'
  2075. category: 'Kernel'!
  2076. !Stream methodsFor: 'accessing'!
  2077. collection
  2078. ^collection
  2079. !
  2080. setCollection: aCollection
  2081. collection := aCollection
  2082. !
  2083. position
  2084. ^position ifNil: [position := 0]
  2085. !
  2086. position: anInteger
  2087. position := anInteger
  2088. !
  2089. streamSize
  2090. ^streamSize
  2091. !
  2092. setStreamSize: anInteger
  2093. streamSize := anInteger
  2094. !
  2095. contents
  2096. ^self collection
  2097. copyFrom: 1
  2098. to: self streamSize
  2099. !
  2100. size
  2101. ^self streamSize
  2102. ! !
  2103. !Stream methodsFor: 'actions'!
  2104. reset
  2105. self position: 0
  2106. !
  2107. close
  2108. !
  2109. flush
  2110. !
  2111. resetContents
  2112. self reset.
  2113. self setStreamSize: 0
  2114. ! !
  2115. !Stream methodsFor: 'enumerating'!
  2116. do: aBlock
  2117. [self atEnd] whileFalse: [aBlock value: self next]
  2118. ! !
  2119. !Stream methodsFor: 'positioning'!
  2120. setToEnd
  2121. self position: self size
  2122. !
  2123. skip: anInteger
  2124. self position: ((self position + anInteger) min: self size max: 0)
  2125. ! !
  2126. !Stream methodsFor: 'reading'!
  2127. next
  2128. self position: self position + 1.
  2129. ^collection at: self position
  2130. !
  2131. next: anInteger
  2132. | tempCollection |
  2133. tempCollection := self collection class new.
  2134. anInteger timesRepeat: [
  2135. self atEnd ifFalse: [
  2136. tempCollection add: self next]].
  2137. ^tempCollection
  2138. !
  2139. peek
  2140. ^self atEnd ifFalse: [
  2141. self collection at: self position + 1]
  2142. ! !
  2143. !Stream methodsFor: 'testing'!
  2144. atEnd
  2145. ^self position = self size
  2146. !
  2147. atStart
  2148. ^self position = 0
  2149. !
  2150. isEmpty
  2151. ^self size = 0
  2152. ! !
  2153. !Stream methodsFor: 'writing'!
  2154. nextPut: anObject
  2155. self position: self position + 1.
  2156. self collection at: self position put: anObject.
  2157. self setStreamSize: (self streamSize max: self position)
  2158. !
  2159. nextPutAll: aCollection
  2160. aCollection do: [:each |
  2161. self nextPut: each]
  2162. ! !
  2163. !Stream class methodsFor: 'instance creation'!
  2164. on: aCollection
  2165. ^self new
  2166. setCollection: aCollection;
  2167. setStreamSize: aCollection size;
  2168. yourself
  2169. ! !
  2170. Stream subclass: #StringStream
  2171. instanceVariableNames: ''
  2172. category: 'Kernel'!
  2173. !StringStream methodsFor: 'reading'!
  2174. next: anInteger
  2175. | tempCollection |
  2176. tempCollection := self collection class new.
  2177. anInteger timesRepeat: [
  2178. self atEnd ifFalse: [
  2179. tempCollection := tempCollection, self next]].
  2180. ^tempCollection
  2181. ! !
  2182. !StringStream methodsFor: 'writing'!
  2183. nextPut: aString
  2184. self nextPutAll: aString
  2185. !
  2186. nextPutAll: aString
  2187. self setCollection:
  2188. (self collection copyFrom: 1 to: self position),
  2189. aString,
  2190. (self collection copyFrom: (self position + 1 + aString size) to: self collection size).
  2191. self position: self position + aString size.
  2192. self setStreamSize: (self streamSize max: self position)
  2193. !
  2194. cr
  2195. ^self nextPutAll: String cr
  2196. !
  2197. crlf
  2198. ^self nextPutAll: String crlf
  2199. !
  2200. lf
  2201. ^self nextPutAll: String lf
  2202. !
  2203. space
  2204. self nextPut: ' '
  2205. ! !
  2206. Object subclass: #ClassCommentReader
  2207. instanceVariableNames: 'class chunkParser'
  2208. category: 'Kernel'!
  2209. !ClassCommentReader methodsFor: 'accessing'!
  2210. class: aClass
  2211. class := aClass
  2212. ! !
  2213. !ClassCommentReader methodsFor: 'fileIn'!
  2214. scanFrom: aChunkParser
  2215. | chunk |
  2216. chunk := aChunkParser nextChunk.
  2217. chunk isEmpty ifFalse: [
  2218. self setComment: chunk].
  2219. ! !
  2220. !ClassCommentReader methodsFor: 'initialization'!
  2221. initialize
  2222. super initialize.
  2223. chunkParser := ChunkParser new.
  2224. ! !
  2225. !ClassCommentReader methodsFor: 'private'!
  2226. setComment: aString
  2227. class comment: aString
  2228. ! !
  2229. Object subclass: #Random
  2230. instanceVariableNames: ''
  2231. category: 'Kernel'!
  2232. !Random methodsFor: 'accessing'!
  2233. next
  2234. <return Math.random()>
  2235. !
  2236. next: anInteger
  2237. ^(1 to: anInteger) collect: [:each | self next]
  2238. ! !
  2239. Object subclass: #Point
  2240. instanceVariableNames: 'x y'
  2241. category: 'Kernel'!
  2242. !Point methodsFor: 'accessing'!
  2243. x
  2244. ^x
  2245. !
  2246. y
  2247. ^y
  2248. !
  2249. y: aNumber
  2250. y := aNumber
  2251. !
  2252. x: aNumber
  2253. x := aNumber
  2254. ! !
  2255. !Point methodsFor: 'arithmetic'!
  2256. * aPoint
  2257. ^Point x: self x * aPoint asPoint x y: self y * aPoint asPoint y
  2258. !
  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. ^aPoint class = self class and: [
  2270. (aPoint x = self x) & (aPoint y = self y)]
  2271. ! !
  2272. !Point methodsFor: 'converting'!
  2273. asPoint
  2274. ^self
  2275. ! !
  2276. !Point class methodsFor: 'instance creation'!
  2277. x: aNumber y: anotherNumber
  2278. ^self new
  2279. x: aNumber;
  2280. y: anotherNumber;
  2281. yourself
  2282. ! !
  2283. Object subclass: #Message
  2284. instanceVariableNames: 'selector arguments'
  2285. category: 'Kernel'!
  2286. !Message methodsFor: 'accessing'!
  2287. selector
  2288. ^selector
  2289. !
  2290. selector: aString
  2291. selector := aString
  2292. !
  2293. arguments: anArray
  2294. arguments := anArray
  2295. !
  2296. arguments
  2297. ^arguments
  2298. ! !
  2299. !Message methodsFor: 'printing'!
  2300. printString
  2301. ^ String streamContents: [:aStream|
  2302. aStream
  2303. nextPutAll: super printString;
  2304. nextPutAll: '(';
  2305. nextPutAll: selector;
  2306. nextPutAll: ')' ]
  2307. ! !
  2308. !Message class methodsFor: 'instance creation'!
  2309. selector: aString arguments: anArray
  2310. ^self new
  2311. selector: aString;
  2312. arguments: anArray;
  2313. yourself
  2314. ! !
  2315. Error subclass: #MessageNotUnderstood
  2316. instanceVariableNames: 'message receiver'
  2317. category: 'Kernel'!
  2318. !MessageNotUnderstood methodsFor: 'accessing'!
  2319. message
  2320. ^message
  2321. !
  2322. message: aMessage
  2323. message := aMessage
  2324. !
  2325. receiver
  2326. ^receiver
  2327. !
  2328. receiver: anObject
  2329. receiver := anObject
  2330. !
  2331. messageText
  2332. ^self receiver asString, ' does not understand #', self message selector
  2333. ! !
  2334. Object subclass: #ErrorHandler
  2335. instanceVariableNames: ''
  2336. category: 'Kernel'!
  2337. !ErrorHandler methodsFor: 'error handling'!
  2338. handleError: anError
  2339. anError context ifNotNil: [self logErrorContext: anError context].
  2340. self logError: anError
  2341. ! !
  2342. !ErrorHandler methodsFor: 'private'!
  2343. logContext: aContext
  2344. aContext home ifNotNil: [
  2345. self logContext: aContext home].
  2346. self log: aContext receiver asString, '>>', aContext selector
  2347. !
  2348. logErrorContext: aContext
  2349. aContext ifNotNil: [
  2350. aContext home ifNotNil: [
  2351. self logContext: aContext home]]
  2352. !
  2353. logError: anError
  2354. self log: anError messageText
  2355. !
  2356. log: aString
  2357. console log: aString
  2358. ! !
  2359. ErrorHandler class instanceVariableNames: 'current'!
  2360. !ErrorHandler class methodsFor: 'accessing'!
  2361. current
  2362. ^current ifNil: [current := self new]
  2363. !
  2364. setCurrent: anHandler
  2365. current := anHandler
  2366. ! !
  2367. !ErrorHandler class methodsFor: 'initialization'!
  2368. initialize
  2369. self register
  2370. !
  2371. register
  2372. ErrorHandler setCurrent: self new
  2373. ! !
  2374. Object subclass: #JSObjectProxy
  2375. instanceVariableNames: 'jsObject'
  2376. category: 'Kernel'!
  2377. !JSObjectProxy methodsFor: 'accessing'!
  2378. jsObject: aJSObject
  2379. jsObject := aJSObject
  2380. !
  2381. jsObject
  2382. ^jsObject
  2383. !
  2384. at: aString
  2385. <return self['@jsObject'][aString]>
  2386. !
  2387. at: aString put: anObject
  2388. <self['@jsObject'][aString] = anObject>
  2389. ! !
  2390. !JSObjectProxy methodsFor: 'proxy'!
  2391. printString
  2392. ^self jsObject toString
  2393. !
  2394. inspectOn: anInspector
  2395. | variables |
  2396. variables := Dictionary new.
  2397. variables at: '#self' put: self jsObject.
  2398. anInspector setLabel: self printString.
  2399. <for(var i in self['@jsObject']) {
  2400. variables._at_put_(i, self['@jsObject'][i]);
  2401. }>.
  2402. anInspector setVariables: variables
  2403. !
  2404. doesNotUnderstand: aMessage
  2405. | obj selector jsSelector arguments |
  2406. obj := self jsObject.
  2407. selector := aMessage selector.
  2408. jsSelector := selector asJavaScriptSelector.
  2409. arguments := aMessage arguments.
  2410. <if(obj[jsSelector] !!= undefined) {return smalltalk.send(obj, jsSelector, arguments)}>.
  2411. super doesNotUnderstand: aMessage
  2412. ! !
  2413. !JSObjectProxy class methodsFor: 'instance creation'!
  2414. on: aJSObject
  2415. ^self new
  2416. jsObject: aJSObject;
  2417. yourself
  2418. ! !
  2419. Collection subclass: #Set
  2420. instanceVariableNames: 'elements'
  2421. category: 'Kernel'!
  2422. !Set methodsFor: 'accessing'!
  2423. size
  2424. ^elements size
  2425. ! !
  2426. !Set methodsFor: 'adding/removing'!
  2427. add: anObject
  2428. <
  2429. var found;
  2430. for(var i in self['@elements']) {
  2431. if(anObject == self['@elements'][i]) {
  2432. found = true;
  2433. break;
  2434. }
  2435. }
  2436. if(!!found) {self['@elements'].push(anObject)}
  2437. >
  2438. !
  2439. remove: anObject
  2440. elements remove: anObject
  2441. ! !
  2442. !Set methodsFor: 'comparing'!
  2443. = aCollection
  2444. ^self class = aCollection class and: [
  2445. elements = aCollection asArray]
  2446. ! !
  2447. !Set methodsFor: 'converting'!
  2448. asArray
  2449. ^elements copy
  2450. ! !
  2451. !Set methodsFor: 'enumerating'!
  2452. detect: aBlock ifNone: anotherBlock
  2453. ^elements detect: aBlock ifNone: anotherBlock
  2454. !
  2455. do: aBlock
  2456. elements do: aBlock
  2457. ! !
  2458. !Set methodsFor: 'initialization'!
  2459. initialize
  2460. super initialize.
  2461. elements := #()
  2462. ! !
  2463. !Set methodsFor: 'testing'!
  2464. includes: anObject
  2465. ^elements includes: anObject
  2466. ! !
  2467. Object subclass: #Transcript
  2468. instanceVariableNames: 'textarea'
  2469. category: 'Kernel'!
  2470. Transcript class instanceVariableNames: 'current'!
  2471. !Transcript class methodsFor: 'instance creation'!
  2472. open
  2473. self current open
  2474. !
  2475. new
  2476. self shouldNotImplement
  2477. !
  2478. current
  2479. ^current
  2480. !
  2481. register: aTranscript
  2482. current := aTranscript
  2483. ! !
  2484. !Transcript class methodsFor: 'printing'!
  2485. show: anObject
  2486. self current show: anObject
  2487. !
  2488. cr
  2489. self current show: String cr
  2490. !
  2491. clear
  2492. self current clear
  2493. ! !
  2494. Object subclass: #ConsoleTranscript
  2495. instanceVariableNames: 'textarea'
  2496. category: 'Kernel'!
  2497. !ConsoleTranscript methodsFor: 'actions'!
  2498. open
  2499. ! !
  2500. !ConsoleTranscript methodsFor: 'printing'!
  2501. clear
  2502. "no op"
  2503. !
  2504. cr
  2505. "no op"
  2506. !
  2507. show: anObject
  2508. | string |
  2509. string := anObject asString.
  2510. <console.log(String(string))>
  2511. ! !
  2512. !ConsoleTranscript class methodsFor: 'initialization'!
  2513. initialize
  2514. Transcript register: self new
  2515. ! !
  2516. Dictionary subclass: #Dictionary2
  2517. instanceVariableNames: 'keys'
  2518. category: 'Kernel'!
  2519. !Dictionary2 methodsFor: 'accessing'!
  2520. size
  2521. ^keys size
  2522. !
  2523. associations
  2524. | associations |
  2525. associations := #().
  2526. keys do: [:each |
  2527. associations add: (Association key: each value: (self at: each))].
  2528. ^associations
  2529. !
  2530. keys
  2531. ^keys copy
  2532. !
  2533. values
  2534. ^keys collect: [:each | self at: each]
  2535. !
  2536. at: aKey put: aValue
  2537. (keys includes: aKey) ifFalse: [keys add: aKey].
  2538. ^self basicAt: aKey put: aValue
  2539. !
  2540. at: aKey ifAbsent: aBlock
  2541. ^(self keys includes: aKey)
  2542. ifTrue: [self basicAt: aKey]
  2543. ifFalse: aBlock
  2544. !
  2545. at: aKey ifAbsentPut: aBlock
  2546. ^self at: aKey ifAbsent: [
  2547. self at: aKey put: aBlock value]
  2548. !
  2549. at: aKey ifPresent: aBlock
  2550. ^(self basicAt: aKey) ifNotNil: [aBlock value: (self at: aKey)]
  2551. !
  2552. at: aKey ifPresent: aBlock ifAbsent: anotherBlock
  2553. ^(self basicAt: aKey)
  2554. ifNil: anotherBlock
  2555. ifNotNil: [aBlock value: (self at: aKey)]
  2556. !
  2557. at: aKey
  2558. ^self at: aKey ifAbsent: [self errorNotFound]
  2559. ! !
  2560. !Dictionary2 methodsFor: 'adding/removing'!
  2561. add: anAssociation
  2562. self at: anAssociation key put: anAssociation value
  2563. !
  2564. addAll: aDictionary
  2565. super addAll: aDictionary associations.
  2566. ^aDictionary
  2567. !
  2568. remove: aKey
  2569. self removeKey: aKey
  2570. !
  2571. removeKey: aKey
  2572. keys remove: aKey
  2573. ! !
  2574. !Dictionary2 methodsFor: 'comparing'!
  2575. = aDictionary
  2576. self class = aDictionary class ifFalse: [^false].
  2577. self size = aDictionary size ifFalse: [^false].
  2578. ^self associations = aDictionary associations
  2579. ! !
  2580. !Dictionary2 methodsFor: 'converting'!
  2581. asJSONObject
  2582. | object |
  2583. object := Object new.
  2584. self keysAndValuesDo: [:key :value |
  2585. object basicAt: key put: value asJSONObject].
  2586. ^object
  2587. ! !
  2588. !Dictionary2 methodsFor: 'copying'!
  2589. shallowCopy
  2590. | copy |
  2591. copy := self class new.
  2592. self associationsDo: [:each |
  2593. copy at: each key put: each value].
  2594. ^copy
  2595. !
  2596. , aCollection
  2597. self shouldNotImplement
  2598. !
  2599. copyFrom: anIndex to: anotherIndex
  2600. self shouldNotImplement
  2601. ! !
  2602. !Dictionary2 methodsFor: 'enumerating'!
  2603. associationsDo: aBlock
  2604. self associations do: aBlock
  2605. !
  2606. keysAndValuesDo: aBlock
  2607. self associationsDo: [:each |
  2608. aBlock value: each key value: each value]
  2609. !
  2610. do: aBlock
  2611. self values do: aBlock
  2612. !
  2613. select: aBlock
  2614. | newDict |
  2615. newDict := self class new.
  2616. self keysAndValuesDo: [:key :value |
  2617. (aBlock value: value) ifTrue: [newDict at: key put: value]].
  2618. ^newDict
  2619. !
  2620. collect: aBlock
  2621. | newDict |
  2622. newDict := self class new.
  2623. self keysAndValuesDo: [:key :value |
  2624. newDict at: key put: (aBlock value: value)].
  2625. ^newDict
  2626. !
  2627. detect: aBlock ifNone: anotherBlock
  2628. ^self values detect: aBlock ifNone: anotherBlock
  2629. !
  2630. includes: anObject
  2631. ^self values includes: anObject
  2632. ! !
  2633. !Dictionary2 methodsFor: 'initialization'!
  2634. initialize
  2635. super initialize.
  2636. keys := #()
  2637. ! !
  2638. !Dictionary2 methodsFor: 'printing'!
  2639. printString
  2640. ^ String streamContents: [:aStream|
  2641. aStream
  2642. nextPutAll: super printString;
  2643. nextPutAll: '('.
  2644. self associations
  2645. do: [:anAssociation|
  2646. aStream
  2647. nextPutAll: anAssociation key printString;
  2648. nextPutAll: ' -> ';
  2649. nextPutAll: anAssociation value printString]
  2650. separatedBy: [aStream nextPutAll: ' , '].
  2651. aStream nextPutAll: ')'.
  2652. ]
  2653. !
  2654. storeOn: aStream
  2655. aStream nextPutAll: '#{'.
  2656. self associations
  2657. do: [:each | each storeOn: aStream]
  2658. separatedBy: [ aStream nextPutAll: '. '].
  2659. aStream nextPutAll: '}'
  2660. ! !