Kernel-Classes.st 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. Smalltalk createPackage: 'Kernel-Classes'!
  2. Object subclass: #BehaviorBody
  3. instanceVariableNames: ''
  4. package: 'Kernel-Classes'!
  5. !BehaviorBody commentStamp!
  6. I am the superclass of all behaviors.
  7. My instances hold the method dictionary.
  8. I also provides methods for compiling methods and examining the method dictionary.!
  9. !BehaviorBody methodsFor: 'accessing'!
  10. >> aString
  11. ^ self methodAt: aString
  12. !
  13. comment
  14. ^ (self basicAt: 'comment') ifNil: [ '' ]
  15. !
  16. comment: aString
  17. self basicAt: 'comment' put: aString.
  18. SystemAnnouncer current
  19. announce: (ClassCommentChanged new
  20. theClass: self;
  21. yourself)
  22. !
  23. definition
  24. ^ ''
  25. !
  26. methodAt: aString
  27. ^ self methodDictionary at: aString
  28. !
  29. methodDictionary
  30. <inlineJS: 'var dict = $globals.HashedCollection._new();
  31. var methods = self.methods;
  32. Object.keys(methods).forEach(function(i) {
  33. if(methods[i].selector) {
  34. dict._at_put_(methods[i].selector, methods[i]);
  35. }
  36. });
  37. return dict'>
  38. !
  39. methodTemplate
  40. ^ String streamContents: [ :stream | stream
  41. write: 'messageSelectorAndArgumentNames'; lf;
  42. tab; write: '"comment stating purpose of message"'; lf;
  43. lf;
  44. tab; write: '| temporary variable names |'; lf;
  45. tab; write: 'statements' ]
  46. !
  47. methods
  48. ^ self methodDictionary values
  49. !
  50. methodsInProtocol: aString
  51. ^ self methods select: [ :each | each protocol = aString ]
  52. !
  53. name
  54. <inlineJS: 'return self.className || nil'>
  55. !
  56. organization
  57. ^ self basicAt: 'organization'
  58. !
  59. ownMethods
  60. "Answer the methods of the receiver that are not package extensions"
  61. ^ (self ownProtocols
  62. inject: OrderedCollection new
  63. into: [ :acc :each | acc, (self methodsInProtocol: each) ])
  64. sorted: [ :a :b | a selector <= b selector ]
  65. !
  66. ownProtocols
  67. "Answer the protocols of the receiver that are not package extensions"
  68. ^ self protocols reject: [ :each |
  69. each match: '^\*' ]
  70. !
  71. packageOfProtocol: aString
  72. "Answer the package the method of receiver belongs to:
  73. - if it is an extension method, answer the corresponding package
  74. - else answer the receiver's package"
  75. (aString beginsWith: '*') ifFalse: [
  76. ^ self package ].
  77. ^ Package
  78. named: aString allButFirst
  79. ifAbsent: [ nil ]
  80. !
  81. protocols
  82. ^ self organization elements sorted
  83. !
  84. removeProtocolIfEmpty: aString
  85. self methods
  86. detect: [ :each | each protocol = aString ]
  87. ifNone: [ self organization removeElement: aString ]
  88. !
  89. selectors
  90. ^ self methodDictionary keys
  91. !
  92. theMetaClass
  93. self subclassResponsibility
  94. !
  95. theNonMetaClass
  96. self subclassResponsibility
  97. !
  98. traitComposition
  99. ^ (self basicAt: 'traitComposition') collect: [ :each | TraitTransformation fromJSON: each ]
  100. !
  101. usesDefinition
  102. ^ self traitComposition ifNotEmpty: [ :traitComposition |
  103. String streamContents: [ :str |
  104. str write: 'uses: {'.
  105. traitComposition
  106. do: [ :each | str write: each definition ]
  107. separatedBy: [ str write: '. ' ].
  108. str write: '}' ] ]
  109. ! !
  110. !BehaviorBody methodsFor: 'compiling'!
  111. addCompiledMethod: aMethod
  112. | oldMethod announcement |
  113. oldMethod := self methodDictionary
  114. at: aMethod selector
  115. ifAbsent: [ nil ].
  116. (self protocols includes: aMethod protocol)
  117. ifFalse: [ self organization addElement: aMethod protocol ].
  118. self basicAddCompiledMethod: aMethod.
  119. oldMethod ifNotNil: [
  120. self removeProtocolIfEmpty: oldMethod protocol ].
  121. announcement := oldMethod
  122. ifNil: [
  123. MethodAdded new
  124. method: aMethod;
  125. yourself ]
  126. ifNotNil: [
  127. MethodModified new
  128. oldMethod: oldMethod;
  129. method: aMethod;
  130. yourself ].
  131. SystemAnnouncer current
  132. announce: announcement
  133. !
  134. compile: aString protocol: anotherString
  135. ^ Compiler new
  136. install: aString
  137. forClass: self
  138. protocol: anotherString
  139. !
  140. recompile
  141. ^ Compiler new recompile: self
  142. !
  143. removeCompiledMethod: aMethod
  144. self basicRemoveCompiledMethod: aMethod.
  145. self removeProtocolIfEmpty: aMethod protocol.
  146. SystemAnnouncer current
  147. announce: (MethodRemoved new
  148. method: aMethod;
  149. yourself)
  150. !
  151. setTraitComposition: aTraitComposition
  152. <inlineJS: '$core.setTraitComposition(aTraitComposition._asJSON(), self)'>
  153. ! !
  154. !BehaviorBody methodsFor: 'enumerating'!
  155. protocolsDo: aBlock
  156. "Execute aBlock for each method protocol with
  157. its collection of methods in the sort order of protocol name."
  158. | methodsByProtocol |
  159. methodsByProtocol := HashedCollection new.
  160. self methodDictionary valuesDo: [ :m |
  161. (methodsByProtocol at: m protocol ifAbsentPut: [ Array new ])
  162. add: m ].
  163. self protocols do: [ :protocol |
  164. aBlock value: protocol value: (methodsByProtocol at: protocol) ]
  165. ! !
  166. !BehaviorBody methodsFor: 'printing'!
  167. printOn: aStream
  168. self name
  169. ifNil: [ super printOn: aStream ]
  170. ifNotNil: [ :name | aStream nextPutAll: name ]
  171. ! !
  172. !BehaviorBody methodsFor: 'private'!
  173. basicAddCompiledMethod: aMethod
  174. <inlineJS: '$core.addMethod(aMethod, self)'>
  175. !
  176. basicRemoveCompiledMethod: aMethod
  177. <inlineJS: '$core.removeMethod(aMethod,self)'>
  178. ! !
  179. !BehaviorBody methodsFor: 'testing'!
  180. includesSelector: aString
  181. ^ self methodDictionary includesKey: aString
  182. ! !
  183. BehaviorBody subclass: #Behavior
  184. instanceVariableNames: ''
  185. package: 'Kernel-Classes'!
  186. !Behavior commentStamp!
  187. I am the superclass of all class objects.
  188. In addition to BehaviorBody, I define superclass/subclass relationships and instantiation.
  189. I define the protocol for creating instances of a class with `#basicNew` and `#new` (see `boot.js` for class constructors details).
  190. My instances know about the subclass/superclass relationships between classes and contain the description that instances are created from.
  191. I also provide iterating over the class hierarchy.!
  192. !Behavior methodsFor: 'accessing'!
  193. allInstanceVariableNames
  194. | result |
  195. result := self instanceVariableNames copy.
  196. self superclass ifNotNil: [
  197. result addAll: self superclass allInstanceVariableNames ].
  198. ^ result
  199. !
  200. allSelectors
  201. ^ self allSuperclasses
  202. inject: self selectors
  203. into: [ :acc :each | acc addAll: each selectors; yourself ]
  204. !
  205. allSubclasses
  206. "Answer an collection of the receiver's and the receiver's descendent's subclasses. "
  207. ^ Array streamContents: [ :str | self allSubclassesDo: [ :each | str nextPut: each ] ]
  208. !
  209. allSuperclasses
  210. self superclass ifNil: [ ^ #() ].
  211. ^ (OrderedCollection with: self superclass)
  212. addAll: self superclass allSuperclasses;
  213. yourself
  214. !
  215. definition
  216. ^ ''
  217. !
  218. instanceVariableNames
  219. <inlineJS: 'return self.iVarNames'>
  220. !
  221. javascriptConstructor
  222. "Answer the JS constructor used to instantiate. See boot.js"
  223. <inlineJS: 'return self.fn'>
  224. !
  225. javascriptConstructor: aJavaScriptFunction
  226. "Set the JS constructor used to instantiate.
  227. See the JS counter-part in boot.js `$core.setClassConstructor'"
  228. <inlineJS: '$core.setClassConstructor(self, aJavaScriptFunction);'>
  229. !
  230. lookupSelector: selector
  231. "Look up the given selector in my methodDictionary.
  232. Return the corresponding method if found.
  233. Otherwise chase the superclass chain and try again.
  234. Return nil if no method is found."
  235. | lookupClass |
  236. lookupClass := self.
  237. [ lookupClass = nil ] whileFalse: [
  238. (lookupClass includesSelector: selector)
  239. ifTrue: [ ^ lookupClass methodAt: selector ].
  240. lookupClass := lookupClass superclass ].
  241. ^ nil
  242. !
  243. prototype
  244. <inlineJS: 'return self.fn.prototype'>
  245. !
  246. subclasses
  247. self subclassResponsibility
  248. !
  249. superclass
  250. <inlineJS: 'return self.superclass'>
  251. !
  252. theMetaClass
  253. self subclassResponsibility
  254. !
  255. theNonMetaClass
  256. self subclassResponsibility
  257. !
  258. withAllSubclasses
  259. ^ (Array with: self) addAll: self allSubclasses; yourself
  260. ! !
  261. !Behavior methodsFor: 'enumerating'!
  262. allSubclassesDo: aBlock
  263. "Evaluate the argument, aBlock, for each of the receiver's subclasses."
  264. <inlineJS: '$core.traverseClassTree(self, function(subclass) {
  265. if (subclass !!== self) aBlock._value_(subclass);
  266. })'>
  267. ! !
  268. !Behavior methodsFor: 'instance creation'!
  269. basicNew
  270. <inlineJS: 'return new self.fn()'>
  271. !
  272. new
  273. ^ self basicNew initialize
  274. ! !
  275. !Behavior methodsFor: 'testing'!
  276. canUnderstand: aSelector
  277. ^ (self includesSelector: aSelector asString) or: [
  278. self superclass notNil and: [ self superclass canUnderstand: aSelector ]]
  279. !
  280. includesBehavior: aClass
  281. ^ self == aClass or: [
  282. self inheritsFrom: aClass ]
  283. !
  284. inheritsFrom: aClass
  285. self superclass ifNil: [ ^ false ].
  286. ^ aClass == self superclass or: [
  287. self superclass inheritsFrom: aClass ]
  288. !
  289. isBehavior
  290. ^ true
  291. ! !
  292. Behavior subclass: #Class
  293. instanceVariableNames: ''
  294. package: 'Kernel-Classes'!
  295. !Class commentStamp!
  296. I am __the__ class object.
  297. My instances are the classes of the system.
  298. Class creation is done throught a `ClassBuilder` instance.!
  299. !Class methodsFor: 'accessing'!
  300. category
  301. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  302. !
  303. classTag
  304. "Returns a tag or general category for this class.
  305. Typically used to help tools do some reflection.
  306. Helios, for example, uses this to decide what icon the class should display."
  307. ^ 'class'
  308. !
  309. definition
  310. ^ String streamContents: [ :stream | stream
  311. print: self superclass; write: ' subclass: '; printSymbol: self name; lf;
  312. write: (self usesDefinition ifNotEmpty: [ :uses | { String tab. uses. String lf }]);
  313. tab; write: 'instanceVariableNames: '; print: (' ' join: self instanceVariableNames); lf;
  314. tab; write: 'package: '; print: self category ]
  315. !
  316. package
  317. ^ self basicAt: 'pkg'
  318. !
  319. package: aPackage
  320. | oldPackage |
  321. self package = aPackage ifTrue: [ ^ self ].
  322. oldPackage := self package.
  323. self basicAt: 'pkg' put: aPackage.
  324. oldPackage organization removeElement: self.
  325. aPackage organization addElement: self.
  326. SystemAnnouncer current announce: (ClassMoved new
  327. theClass: self;
  328. oldPackage: oldPackage;
  329. yourself)
  330. !
  331. rename: aString
  332. ClassBuilder new renameClass: self to: aString
  333. !
  334. subclasses
  335. <inlineJS: 'return self.subclasses._copy()'>
  336. !
  337. theMetaClass
  338. ^ self class
  339. !
  340. theNonMetaClass
  341. ^ self
  342. ! !
  343. !Class methodsFor: 'browsing'!
  344. browse
  345. Finder findClass: self
  346. ! !
  347. !Class methodsFor: 'class creation'!
  348. subclass: aString
  349. "Kept for file-in compatibility."
  350. ^ self subclass: aString instanceVariableNames: '' package: nil
  351. !
  352. subclass: aString instanceVariableNames: anotherString
  353. "Kept for file-in compatibility."
  354. ^ self subclass: aString instanceVariableNames: anotherString package: nil
  355. !
  356. subclass: aString instanceVariableNames: aString2 category: aString3
  357. "Kept for file-in compatibility."
  358. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  359. !
  360. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  361. "Kept for file-in compatibility. ignores class variables and pools."
  362. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  363. !
  364. subclass: aString instanceVariableNames: aString2 package: aString3
  365. ^ ClassBuilder new
  366. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  367. !
  368. subclass: aString uses: aTraitCompositionDescription
  369. "Kept for file-in compatibility."
  370. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: '' package: nil
  371. !
  372. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: anotherString
  373. "Kept for file-in compatibility."
  374. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: anotherString package: nil
  375. !
  376. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 category: aString3
  377. "Kept for file-in compatibility."
  378. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  379. !
  380. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  381. "Kept for file-in compatibility. ignores class variables and pools."
  382. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  383. !
  384. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  385. | cls |
  386. cls := self subclass: aString instanceVariableNames: aString2 package: aString3.
  387. cls setTraitComposition: aTraitCompositionDescription asTraitComposition.
  388. ^ cls
  389. ! !
  390. !Class methodsFor: 'converting'!
  391. asJavascript
  392. ^ '$globals.', self name
  393. ! !
  394. !Class methodsFor: 'testing'!
  395. isClass
  396. ^ true
  397. ! !
  398. Behavior subclass: #Metaclass
  399. instanceVariableNames: ''
  400. package: 'Kernel-Classes'!
  401. !Metaclass commentStamp!
  402. I am the root of the class hierarchy.
  403. My instances are metaclasses, one for each real class, and have a single instance, which they hold onto: the class that they are the metaclass of.!
  404. !Metaclass methodsFor: 'accessing'!
  405. definition
  406. ^ String streamContents: [ :stream | stream
  407. print: self;
  408. write: (self usesDefinition ifEmpty: [' '] ifNotEmpty: [ :uses | { String lf. String tab. uses. String lf. String tab }]);
  409. write: 'instanceVariableNames: ';
  410. print: (' ' join: self instanceVariableNames) ]
  411. !
  412. instanceClass
  413. <inlineJS: 'return self.instanceClass'>
  414. !
  415. instanceVariableNames: aCollection
  416. ClassBuilder new
  417. class: self instanceVariableNames: aCollection.
  418. ^ self
  419. !
  420. name
  421. ^ self instanceClass name, ' class'
  422. !
  423. package
  424. ^ self instanceClass package
  425. !
  426. subclasses
  427. <inlineJS: 'return $core.metaSubclasses(self)'>
  428. !
  429. theMetaClass
  430. ^ self
  431. !
  432. theNonMetaClass
  433. ^ self instanceClass
  434. !
  435. uses: aTraitCompositionDescription instanceVariableNames: aCollection
  436. | metaclass |
  437. metaclass := self instanceVariableNames: aCollection.
  438. metaclass setTraitComposition: aTraitCompositionDescription asTraitComposition.
  439. ^ metaclass
  440. ! !
  441. !Metaclass methodsFor: 'converting'!
  442. asJavascript
  443. ^ '$globals.', self instanceClass name, '.klass'
  444. ! !
  445. !Metaclass methodsFor: 'testing'!
  446. isMetaclass
  447. ^ true
  448. ! !
  449. BehaviorBody subclass: #Trait
  450. instanceVariableNames: ''
  451. package: 'Kernel-Classes'!
  452. !Trait methodsFor: 'IDE compatibility'!
  453. allSubclassesDo: aBlock
  454. !
  455. superclass
  456. ^ nil
  457. ! !
  458. !Trait methodsFor: 'accessing'!
  459. category
  460. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  461. !
  462. classTag
  463. ^ 'trait'
  464. !
  465. definition
  466. ^ String streamContents: [ :stream | stream
  467. write: 'Trait named: '; printSymbol: self name; lf;
  468. write: (self usesDefinition ifNotEmpty: [ :uses | { String tab. uses. String lf }]);
  469. tab; write: 'package: '; print: self category ]
  470. !
  471. package
  472. ^ self basicAt: 'pkg'
  473. !
  474. theMetaClass
  475. ^ nil
  476. !
  477. theNonMetaClass
  478. ^ self
  479. !
  480. traitUsers
  481. ^ (self basicAt: 'traitUsers') copy
  482. ! !
  483. !Trait methodsFor: 'compiler compatibility'!
  484. allInstanceVariableNames
  485. ^ #()
  486. ! !
  487. !Trait methodsFor: 'composition'!
  488. - anArray
  489. ^ self asTraitTransformation - anArray
  490. !
  491. @ anArrayOfAssociations
  492. ^ self asTraitTransformation @ anArrayOfAssociations
  493. ! !
  494. !Trait methodsFor: 'converting'!
  495. asJavascript
  496. ^ '$globals.', self name
  497. !
  498. asTraitComposition
  499. ^ self asTraitTransformation asTraitComposition
  500. !
  501. asTraitTransformation
  502. ^ TraitTransformation on: self
  503. ! !
  504. !Trait class methodsFor: 'instance creation'!
  505. named: aString package: anotherString
  506. <inlineJS: 'return $core.addTrait(aString, anotherString)'>
  507. !
  508. named: aString uses: aTraitCompositionDescription package: anotherString
  509. | trait |
  510. trait := self named: aString package: anotherString.
  511. trait setTraitComposition: aTraitCompositionDescription asTraitComposition.
  512. ^ trait
  513. ! !
  514. Object subclass: #ClassBuilder
  515. instanceVariableNames: ''
  516. package: 'Kernel-Classes'!
  517. !ClassBuilder commentStamp!
  518. I am responsible for compiling new classes or modifying existing classes in the system.
  519. Rather than using me directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  520. !ClassBuilder methodsFor: 'accessing'!
  521. instanceVariableNamesFor: aString
  522. ^ (aString tokenize: ' ') reject: [ :each | each isEmpty ]
  523. ! !
  524. !ClassBuilder methodsFor: 'class definition'!
  525. addSubclassOf: aClass named: className instanceVariableNames: aCollection package: packageName
  526. | theClass thePackage |
  527. theClass := Smalltalk globals at: className.
  528. thePackage := Package named: packageName.
  529. theClass ifNotNil: [
  530. theClass package: thePackage.
  531. theClass superclass == aClass ifFalse: [
  532. ^ self
  533. migrateClassNamed: className
  534. superclass: aClass
  535. instanceVariableNames: aCollection
  536. package: packageName ] ].
  537. ^ self
  538. basicAddSubclassOf: aClass
  539. named: className
  540. instanceVariableNames: aCollection
  541. package: packageName
  542. !
  543. class: aClass instanceVariableNames: ivarNames
  544. self basicClass: aClass instanceVariableNames: ivarNames.
  545. SystemAnnouncer current
  546. announce: (ClassDefinitionChanged new
  547. theClass: aClass;
  548. yourself)
  549. !
  550. superclass: aClass subclass: className
  551. ^ self superclass: aClass subclass: className instanceVariableNames: '' package: nil
  552. !
  553. superclass: aClass subclass: className instanceVariableNames: ivarNames package: packageName
  554. | newClass |
  555. newClass := self addSubclassOf: aClass
  556. named: className instanceVariableNames: (self instanceVariableNamesFor: ivarNames)
  557. package: (packageName ifNil: [ 'unclassified' ]).
  558. SystemAnnouncer current
  559. announce: (ClassAdded new
  560. theClass: newClass;
  561. yourself).
  562. ^ newClass
  563. ! !
  564. !ClassBuilder methodsFor: 'class migration'!
  565. migrateClass: aClass superclass: anotherClass
  566. ^ self
  567. migrateClassNamed: aClass name
  568. superclass: anotherClass
  569. instanceVariableNames: aClass instanceVariableNames
  570. package: aClass package name
  571. !
  572. migrateClassNamed: className superclass: aClass instanceVariableNames: aCollection package: packageName
  573. | oldClass newClass tmp |
  574. tmp := 'new*', className.
  575. oldClass := Smalltalk globals at: className.
  576. newClass := self
  577. addSubclassOf: aClass
  578. named: tmp
  579. instanceVariableNames: aCollection
  580. package: packageName.
  581. self basicSwapClassNames: oldClass with: newClass.
  582. [ self copyClass: oldClass to: newClass ]
  583. on: Error
  584. do: [ :exception |
  585. self
  586. basicSwapClassNames: oldClass with: newClass;
  587. basicRemoveClass: newClass.
  588. exception resignal ].
  589. self
  590. rawRenameClass: oldClass to: tmp;
  591. rawRenameClass: newClass to: className.
  592. oldClass subclasses
  593. do: [ :each | self migrateClass: each superclass: newClass ].
  594. self basicRemoveClass: oldClass.
  595. SystemAnnouncer current announce: (ClassMigrated new
  596. theClass: newClass;
  597. oldClass: oldClass;
  598. yourself).
  599. ^ newClass
  600. !
  601. renameClass: aClass to: className
  602. self basicRenameClass: aClass to: className.
  603. "Recompile the class to fix potential issues with super sends"
  604. aClass recompile.
  605. SystemAnnouncer current
  606. announce: (ClassRenamed new
  607. theClass: aClass;
  608. yourself)
  609. ! !
  610. !ClassBuilder methodsFor: 'copying'!
  611. copyClass: aClass named: className
  612. | newClass |
  613. newClass := self
  614. addSubclassOf: aClass superclass
  615. named: className
  616. instanceVariableNames: aClass instanceVariableNames
  617. package: aClass package name.
  618. self copyClass: aClass to: newClass.
  619. SystemAnnouncer current
  620. announce: (ClassAdded new
  621. theClass: newClass;
  622. yourself).
  623. ^ newClass
  624. !
  625. copyClass: aClass to: anotherClass
  626. anotherClass comment: aClass comment.
  627. aClass methodDictionary valuesDo: [ :each |
  628. Compiler new install: each source forClass: anotherClass protocol: each protocol ].
  629. self basicClass: anotherClass class instanceVariables: aClass class instanceVariableNames.
  630. aClass class methodDictionary valuesDo: [ :each |
  631. Compiler new install: each source forClass: anotherClass class protocol: each protocol ]
  632. ! !
  633. !ClassBuilder methodsFor: 'method definition'!
  634. installMethod: aCompiledMethod forClass: aBehavior protocol: aString
  635. aCompiledMethod protocol: aString.
  636. aBehavior addCompiledMethod: aCompiledMethod.
  637. ^ aCompiledMethod
  638. ! !
  639. !ClassBuilder methodsFor: 'private'!
  640. basicAddSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  641. <inlineJS: '
  642. return $core.addClass(aString, aClass, aCollection, packageName);
  643. '>
  644. !
  645. basicClass: aClass instanceVariableNames: aString
  646. self basicClass: aClass instanceVariables: (self instanceVariableNamesFor: aString)
  647. !
  648. basicClass: aClass instanceVariables: aCollection
  649. aClass isMetaclass ifFalse: [ self error: aClass name, ' is not a metaclass' ].
  650. aClass basicAt: 'iVarNames' put: aCollection
  651. !
  652. basicRemoveClass: aClass
  653. <inlineJS: '$core.removeClass(aClass)'>
  654. !
  655. basicRenameClass: aClass to: aString
  656. <inlineJS: '
  657. $globals[aString] = aClass;
  658. delete $globals[aClass.className];
  659. aClass.className = aString;
  660. '>
  661. !
  662. basicSwapClassNames: aClass with: anotherClass
  663. <inlineJS: '
  664. var tmp = aClass.className;
  665. aClass.className = anotherClass.className;
  666. anotherClass.className = tmp;
  667. '>
  668. !
  669. rawRenameClass: aClass to: aString
  670. <inlineJS: '
  671. $globals[aString] = aClass;
  672. '>
  673. ! !
  674. !ClassBuilder methodsFor: 'public'!
  675. setupClass: aClass
  676. self deprecatedAPI: 'Classes are now auto-inited.'
  677. ! !
  678. Object subclass: #ClassSorterNode
  679. instanceVariableNames: 'theClass level nodes'
  680. package: 'Kernel-Classes'!
  681. !ClassSorterNode commentStamp!
  682. I provide an algorithm for sorting classes alphabetically.
  683. See [Issue #143](https://lolg.it/amber/amber/issues/143).!
  684. !ClassSorterNode methodsFor: 'accessing'!
  685. getNodesFrom: aCollection
  686. | children others |
  687. children := #().
  688. others := #().
  689. aCollection do: [ :each |
  690. (each superclass = self theClass)
  691. ifTrue: [ children add: each ]
  692. ifFalse: [ others add: each ]].
  693. nodes:= children collect: [ :each |
  694. ClassSorterNode on: each classes: others level: self level + 1 ]
  695. !
  696. level
  697. ^ level
  698. !
  699. level: anInteger
  700. level := anInteger
  701. !
  702. nodes
  703. ^ nodes
  704. !
  705. theClass
  706. ^ theClass
  707. !
  708. theClass: aClass
  709. theClass := aClass
  710. ! !
  711. !ClassSorterNode methodsFor: 'visiting'!
  712. traverseClassesWith: aCollection
  713. "sort classes alphabetically Issue #143"
  714. aCollection add: self theClass.
  715. (self nodes sorted: [ :a :b | a theClass name <= b theClass name ]) do: [ :aNode |
  716. aNode traverseClassesWith: aCollection ].
  717. ! !
  718. !ClassSorterNode class methodsFor: 'instance creation'!
  719. on: aClass classes: aCollection level: anInteger
  720. ^ self new
  721. theClass: aClass;
  722. level: anInteger;
  723. getNodesFrom: aCollection;
  724. yourself
  725. ! !
  726. Object subclass: #TraitTransformation
  727. instanceVariableNames: 'trait aliases exclusions'
  728. package: 'Kernel-Classes'!
  729. !TraitTransformation commentStamp!
  730. I am a single step in trait composition.
  731. I represent one trait including its aliases and exclusions.!
  732. !TraitTransformation methodsFor: 'accessing'!
  733. addAliases: anArrayOfAssociations
  734. anArrayOfAssociations do: [ :each |
  735. | key |
  736. key := each key.
  737. aliases at: key
  738. ifPresent: [ self error: 'Cannot use same alias name twice.' ]
  739. ifAbsent: [ aliases at: key put: each value ] ].
  740. ^ anArrayOfAssociations
  741. !
  742. addExclusions: anArray
  743. exclusions addAll: anArray.
  744. ^ anArray
  745. !
  746. aliases
  747. ^ aliases
  748. !
  749. definition
  750. ^ String streamContents: [ :str |
  751. str print: self trait.
  752. self aliases ifNotEmpty: [ :al |
  753. str write: ' @ {'.
  754. al associations
  755. do: [ :each | str printSymbol: each key; write: ' -> '; printSymbol: each value ]
  756. separatedBy: [ str write: '. ' ].
  757. str write: '}' ].
  758. self exclusions ifNotEmpty: [ :ex |
  759. str write: ' - #('.
  760. ex asArray sorted
  761. do: [ :each | str write: each symbolPrintString allButFirst ]
  762. separatedBy: [ str space ].
  763. str write: ')' ] ]
  764. !
  765. exclusions
  766. ^ exclusions
  767. !
  768. trait
  769. ^ trait
  770. !
  771. trait: anObject
  772. trait := anObject
  773. ! !
  774. !TraitTransformation methodsFor: 'composition'!
  775. - anArray
  776. ^ self copy addExclusions: anArray; yourself
  777. !
  778. @ anArrayOfAssociations
  779. ^ self copy addAliases: anArrayOfAssociations; yourself
  780. ! !
  781. !TraitTransformation methodsFor: 'converting'!
  782. asJSON
  783. ^ #{
  784. 'trait' -> self trait.
  785. 'aliases' -> self aliases.
  786. 'exclusions' -> self exclusions asArray sorted }
  787. !
  788. asJavascript
  789. ^ String streamContents: [ :str | str write: {
  790. '{trait: ' . self trait asJavascript.
  791. self aliases ifNotEmpty: [ :al |
  792. {', aliases: '. al asJSONString} ].
  793. self exclusions ifNotEmpty: [ :ex |
  794. {', exclusions: '. ex asArray sorted asJavascript} ].
  795. '}' } ]
  796. !
  797. asTraitComposition
  798. ^ { self }
  799. !
  800. asTraitTransformation
  801. ^ self
  802. ! !
  803. !TraitTransformation methodsFor: 'copying'!
  804. postCopy
  805. aliases := aliases copy.
  806. exclusions := exclusions copy
  807. ! !
  808. !TraitTransformation methodsFor: 'initialization'!
  809. initialize
  810. super initialize.
  811. aliases := #{}.
  812. exclusions := Set new.
  813. trait := nil
  814. ! !
  815. !TraitTransformation class methodsFor: 'instance creation'!
  816. fromJSON: aJSObject
  817. ^ super new
  818. trait: (aJSObject at: #trait);
  819. addAliases: (Smalltalk readJSObject: (aJSObject at: #aliases ifAbsent: [#{}])) associations;
  820. addExclusions: (aJSObject at: #exclusions ifAbsent: [#()]);
  821. yourself
  822. !
  823. on: aTrait
  824. ^ super new trait: aTrait; yourself
  825. ! !
  826. !Array methodsFor: '*Kernel-Classes'!
  827. asTraitComposition
  828. "not implemented yet, noop atm"
  829. ^ self collect: [ :each | each asTraitTransformation ]
  830. ! !
  831. !UndefinedObject methodsFor: '*Kernel-Classes'!
  832. subclass: aString
  833. "Kept for file-in compatibility."
  834. ^ self subclass: aString instanceVariableNames: '' package: nil
  835. !
  836. subclass: aString instanceVariableNames: anotherString
  837. "Kept for file-in compatibility."
  838. ^ self subclass: aString instanceVariableNames: anotherString package: nil
  839. !
  840. subclass: aString instanceVariableNames: aString2 category: aString3
  841. "Kept for file-in compatibility."
  842. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  843. !
  844. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  845. "Kept for file-in compatibility. ignores class variables and pools."
  846. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  847. !
  848. subclass: aString instanceVariableNames: aString2 package: aString3
  849. ^ ClassBuilder new
  850. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  851. !
  852. subclass: aString uses: aTraitCompositionDescription
  853. "Kept for file-in compatibility."
  854. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: '' package: nil
  855. !
  856. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: anotherString
  857. "Kept for file-in compatibility."
  858. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: anotherString package: nil
  859. !
  860. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 category: aString3
  861. "Kept for file-in compatibility."
  862. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  863. !
  864. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  865. "Kept for file-in compatibility. ignores class variables and pools."
  866. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  867. !
  868. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  869. | cls |
  870. cls := self subclass: aString instanceVariableNames: aString2 package: aString3.
  871. cls setTraitComposition: aTraitCompositionDescription asTraitComposition.
  872. ^ cls
  873. ! !