Kernel-Classes.st 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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: ' instanceVariableNames: ';
  409. print: (' ' join: self instanceVariableNames) ]
  410. !
  411. instanceClass
  412. <inlineJS: 'return self.instanceClass'>
  413. !
  414. instanceVariableNames: aCollection
  415. ClassBuilder new
  416. class: self instanceVariableNames: aCollection
  417. !
  418. name
  419. ^ self instanceClass name, ' class'
  420. !
  421. package
  422. ^ self instanceClass package
  423. !
  424. subclasses
  425. <inlineJS: 'return $core.metaSubclasses(self)'>
  426. !
  427. theMetaClass
  428. ^ self
  429. !
  430. theNonMetaClass
  431. ^ self instanceClass
  432. ! !
  433. !Metaclass methodsFor: 'converting'!
  434. asJavascript
  435. ^ '$globals.', self instanceClass name, '.klass'
  436. ! !
  437. !Metaclass methodsFor: 'testing'!
  438. isMetaclass
  439. ^ true
  440. ! !
  441. BehaviorBody subclass: #Trait
  442. instanceVariableNames: ''
  443. package: 'Kernel-Classes'!
  444. !Trait methodsFor: 'IDE compatibility'!
  445. allSubclassesDo: aBlock
  446. !
  447. superclass
  448. ^ nil
  449. ! !
  450. !Trait methodsFor: 'accessing'!
  451. category
  452. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  453. !
  454. classTag
  455. ^ 'trait'
  456. !
  457. definition
  458. ^ String streamContents: [ :stream | stream
  459. write: 'Trait named: '; printSymbol: self name; lf;
  460. tab; write: 'package: '; print: self category ]
  461. !
  462. package
  463. ^ self basicAt: 'pkg'
  464. !
  465. theMetaClass
  466. ^ nil
  467. !
  468. theNonMetaClass
  469. ^ self
  470. !
  471. traitUsers
  472. ^ (self basicAt: 'traitUsers') copy
  473. ! !
  474. !Trait methodsFor: 'compiler compatibility'!
  475. allInstanceVariableNames
  476. ^ #()
  477. ! !
  478. !Trait methodsFor: 'composition'!
  479. - anArray
  480. ^ self asTraitTransformation - anArray
  481. !
  482. @ anArrayOfAssociations
  483. ^ self asTraitTransformation @ anArrayOfAssociations
  484. ! !
  485. !Trait methodsFor: 'converting'!
  486. asJavascript
  487. ^ '$globals.', self name
  488. !
  489. asTraitComposition
  490. ^ self asTraitTransformation asTraitComposition
  491. !
  492. asTraitTransformation
  493. ^ TraitTransformation on: self
  494. ! !
  495. !Trait class methodsFor: 'instance creation'!
  496. named: aString package: anotherString
  497. <inlineJS: 'return $core.addTrait(aString, anotherString)'>
  498. ! !
  499. Object subclass: #ClassBuilder
  500. instanceVariableNames: ''
  501. package: 'Kernel-Classes'!
  502. !ClassBuilder commentStamp!
  503. I am responsible for compiling new classes or modifying existing classes in the system.
  504. Rather than using me directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  505. !ClassBuilder methodsFor: 'accessing'!
  506. instanceVariableNamesFor: aString
  507. ^ (aString tokenize: ' ') reject: [ :each | each isEmpty ]
  508. ! !
  509. !ClassBuilder methodsFor: 'class definition'!
  510. addSubclassOf: aClass named: className instanceVariableNames: aCollection package: packageName
  511. | theClass thePackage |
  512. theClass := Smalltalk globals at: className.
  513. thePackage := Package named: packageName.
  514. theClass ifNotNil: [
  515. theClass package: thePackage.
  516. theClass superclass == aClass ifFalse: [
  517. ^ self
  518. migrateClassNamed: className
  519. superclass: aClass
  520. instanceVariableNames: aCollection
  521. package: packageName ] ].
  522. ^ self
  523. basicAddSubclassOf: aClass
  524. named: className
  525. instanceVariableNames: aCollection
  526. package: packageName
  527. !
  528. class: aClass instanceVariableNames: ivarNames
  529. self basicClass: aClass instanceVariableNames: ivarNames.
  530. SystemAnnouncer current
  531. announce: (ClassDefinitionChanged new
  532. theClass: aClass;
  533. yourself)
  534. !
  535. superclass: aClass subclass: className
  536. ^ self superclass: aClass subclass: className instanceVariableNames: '' package: nil
  537. !
  538. superclass: aClass subclass: className instanceVariableNames: ivarNames package: packageName
  539. | newClass |
  540. newClass := self addSubclassOf: aClass
  541. named: className instanceVariableNames: (self instanceVariableNamesFor: ivarNames)
  542. package: (packageName ifNil: [ 'unclassified' ]).
  543. SystemAnnouncer current
  544. announce: (ClassAdded new
  545. theClass: newClass;
  546. yourself).
  547. ^ newClass
  548. ! !
  549. !ClassBuilder methodsFor: 'class migration'!
  550. migrateClass: aClass superclass: anotherClass
  551. ^ self
  552. migrateClassNamed: aClass name
  553. superclass: anotherClass
  554. instanceVariableNames: aClass instanceVariableNames
  555. package: aClass package name
  556. !
  557. migrateClassNamed: className superclass: aClass instanceVariableNames: aCollection package: packageName
  558. | oldClass newClass tmp |
  559. tmp := 'new*', className.
  560. oldClass := Smalltalk globals at: className.
  561. newClass := self
  562. addSubclassOf: aClass
  563. named: tmp
  564. instanceVariableNames: aCollection
  565. package: packageName.
  566. self basicSwapClassNames: oldClass with: newClass.
  567. [ self copyClass: oldClass to: newClass ]
  568. on: Error
  569. do: [ :exception |
  570. self
  571. basicSwapClassNames: oldClass with: newClass;
  572. basicRemoveClass: newClass.
  573. exception resignal ].
  574. self
  575. rawRenameClass: oldClass to: tmp;
  576. rawRenameClass: newClass to: className.
  577. oldClass subclasses
  578. do: [ :each | self migrateClass: each superclass: newClass ].
  579. self basicRemoveClass: oldClass.
  580. SystemAnnouncer current announce: (ClassMigrated new
  581. theClass: newClass;
  582. oldClass: oldClass;
  583. yourself).
  584. ^ newClass
  585. !
  586. renameClass: aClass to: className
  587. self basicRenameClass: aClass to: className.
  588. "Recompile the class to fix potential issues with super sends"
  589. aClass recompile.
  590. SystemAnnouncer current
  591. announce: (ClassRenamed new
  592. theClass: aClass;
  593. yourself)
  594. ! !
  595. !ClassBuilder methodsFor: 'copying'!
  596. copyClass: aClass named: className
  597. | newClass |
  598. newClass := self
  599. addSubclassOf: aClass superclass
  600. named: className
  601. instanceVariableNames: aClass instanceVariableNames
  602. package: aClass package name.
  603. self copyClass: aClass to: newClass.
  604. SystemAnnouncer current
  605. announce: (ClassAdded new
  606. theClass: newClass;
  607. yourself).
  608. ^ newClass
  609. !
  610. copyClass: aClass to: anotherClass
  611. anotherClass comment: aClass comment.
  612. aClass methodDictionary valuesDo: [ :each |
  613. Compiler new install: each source forClass: anotherClass protocol: each protocol ].
  614. self basicClass: anotherClass class instanceVariables: aClass class instanceVariableNames.
  615. aClass class methodDictionary valuesDo: [ :each |
  616. Compiler new install: each source forClass: anotherClass class protocol: each protocol ]
  617. ! !
  618. !ClassBuilder methodsFor: 'method definition'!
  619. installMethod: aCompiledMethod forClass: aBehavior protocol: aString
  620. aCompiledMethod protocol: aString.
  621. aBehavior addCompiledMethod: aCompiledMethod.
  622. ^ aCompiledMethod
  623. ! !
  624. !ClassBuilder methodsFor: 'private'!
  625. basicAddSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  626. <inlineJS: '
  627. return $core.addClass(aString, aClass, aCollection, packageName);
  628. '>
  629. !
  630. basicClass: aClass instanceVariableNames: aString
  631. self basicClass: aClass instanceVariables: (self instanceVariableNamesFor: aString)
  632. !
  633. basicClass: aClass instanceVariables: aCollection
  634. aClass isMetaclass ifFalse: [ self error: aClass name, ' is not a metaclass' ].
  635. aClass basicAt: 'iVarNames' put: aCollection
  636. !
  637. basicRemoveClass: aClass
  638. <inlineJS: '$core.removeClass(aClass)'>
  639. !
  640. basicRenameClass: aClass to: aString
  641. <inlineJS: '
  642. $globals[aString] = aClass;
  643. delete $globals[aClass.className];
  644. aClass.className = aString;
  645. '>
  646. !
  647. basicSwapClassNames: aClass with: anotherClass
  648. <inlineJS: '
  649. var tmp = aClass.className;
  650. aClass.className = anotherClass.className;
  651. anotherClass.className = tmp;
  652. '>
  653. !
  654. rawRenameClass: aClass to: aString
  655. <inlineJS: '
  656. $globals[aString] = aClass;
  657. '>
  658. ! !
  659. !ClassBuilder methodsFor: 'public'!
  660. setupClass: aClass
  661. self deprecatedAPI: 'Classes are now auto-inited.'
  662. ! !
  663. Object subclass: #ClassSorterNode
  664. instanceVariableNames: 'theClass level nodes'
  665. package: 'Kernel-Classes'!
  666. !ClassSorterNode commentStamp!
  667. I provide an algorithm for sorting classes alphabetically.
  668. See [Issue #143](https://lolg.it/amber/amber/issues/143).!
  669. !ClassSorterNode methodsFor: 'accessing'!
  670. getNodesFrom: aCollection
  671. | children others |
  672. children := #().
  673. others := #().
  674. aCollection do: [ :each |
  675. (each superclass = self theClass)
  676. ifTrue: [ children add: each ]
  677. ifFalse: [ others add: each ]].
  678. nodes:= children collect: [ :each |
  679. ClassSorterNode on: each classes: others level: self level + 1 ]
  680. !
  681. level
  682. ^ level
  683. !
  684. level: anInteger
  685. level := anInteger
  686. !
  687. nodes
  688. ^ nodes
  689. !
  690. theClass
  691. ^ theClass
  692. !
  693. theClass: aClass
  694. theClass := aClass
  695. ! !
  696. !ClassSorterNode methodsFor: 'visiting'!
  697. traverseClassesWith: aCollection
  698. "sort classes alphabetically Issue #143"
  699. aCollection add: self theClass.
  700. (self nodes sorted: [ :a :b | a theClass name <= b theClass name ]) do: [ :aNode |
  701. aNode traverseClassesWith: aCollection ].
  702. ! !
  703. !ClassSorterNode class methodsFor: 'instance creation'!
  704. on: aClass classes: aCollection level: anInteger
  705. ^ self new
  706. theClass: aClass;
  707. level: anInteger;
  708. getNodesFrom: aCollection;
  709. yourself
  710. ! !
  711. Object subclass: #TraitTransformation
  712. instanceVariableNames: 'trait aliases exclusions'
  713. package: 'Kernel-Classes'!
  714. !TraitTransformation commentStamp!
  715. I am a single step in trait composition.
  716. I represent one trait including its aliases and exclusions.!
  717. !TraitTransformation methodsFor: 'accessing'!
  718. addAliases: anArrayOfAssociations
  719. anArrayOfAssociations do: [ :each |
  720. | key |
  721. key := each key.
  722. aliases at: key
  723. ifPresent: [ self error: 'Cannot use same alias name twice.' ]
  724. ifAbsent: [ aliases at: key put: each value ] ].
  725. ^ anArrayOfAssociations
  726. !
  727. addExclusions: anArray
  728. exclusions addAll: anArray.
  729. ^ anArray
  730. !
  731. aliases
  732. ^ aliases
  733. !
  734. definition
  735. ^ String streamContents: [ :str |
  736. str print: self trait.
  737. self aliases ifNotEmpty: [ :al |
  738. str write: ' @ {'.
  739. al associations
  740. do: [ :each | str printSymbol: each key; write: ' -> '; printSymbol: each value ]
  741. separatedBy: [ str write: '. ' ].
  742. str write: '}' ].
  743. self exclusions ifNotEmpty: [ :ex |
  744. str write: ' - #('.
  745. ex asArray sorted
  746. do: [ :each | str write: each symbolPrintString allButFirst ]
  747. separatedBy: [ str space ].
  748. str write: ')' ] ]
  749. !
  750. exclusions
  751. ^ exclusions
  752. !
  753. trait
  754. ^ trait
  755. !
  756. trait: anObject
  757. trait := anObject
  758. ! !
  759. !TraitTransformation methodsFor: 'composition'!
  760. - anArray
  761. ^ self copy addExclusions: anArray; yourself
  762. !
  763. @ anArrayOfAssociations
  764. ^ self copy addAliases: anArrayOfAssociations; yourself
  765. ! !
  766. !TraitTransformation methodsFor: 'converting'!
  767. asJSON
  768. ^ #{
  769. 'trait' -> self trait.
  770. 'aliases' -> self aliases.
  771. 'exclusions' -> self exclusions asArray sorted }
  772. !
  773. asJavascript
  774. ^ String streamContents: [ :str | str write: {
  775. '{trait: ' . self trait asJavascript.
  776. self aliases ifNotEmpty: [ :al |
  777. {', aliases: '. al asJSONString} ].
  778. self exclusions ifNotEmpty: [ :ex |
  779. {', exclusions: '. ex asArray sorted asJavascript} ].
  780. '}' } ]
  781. !
  782. asTraitComposition
  783. ^ { self }
  784. !
  785. asTraitTransformation
  786. ^ self
  787. ! !
  788. !TraitTransformation methodsFor: 'copying'!
  789. postCopy
  790. aliases := aliases copy.
  791. exclusions := exclusions copy
  792. ! !
  793. !TraitTransformation methodsFor: 'initialization'!
  794. initialize
  795. super initialize.
  796. aliases := #{}.
  797. exclusions := Set new.
  798. trait := nil
  799. ! !
  800. !TraitTransformation class methodsFor: 'instance creation'!
  801. fromJSON: aJSObject
  802. ^ super new
  803. trait: (aJSObject at: #trait);
  804. addAliases: (Smalltalk readJSObject: (aJSObject at: #aliases ifAbsent: [#{}])) associations;
  805. addExclusions: (aJSObject at: #exclusions ifAbsent: [#()]);
  806. yourself
  807. !
  808. on: aTrait
  809. ^ super new trait: aTrait; yourself
  810. ! !
  811. !Array methodsFor: '*Kernel-Classes'!
  812. asTraitComposition
  813. "not implemented yet, noop atm"
  814. ^ self collect: [ :each | each asTraitTransformation ]
  815. ! !
  816. !UndefinedObject methodsFor: '*Kernel-Classes'!
  817. subclass: aString
  818. "Kept for file-in compatibility."
  819. ^ self subclass: aString instanceVariableNames: '' package: nil
  820. !
  821. subclass: aString instanceVariableNames: anotherString
  822. "Kept for file-in compatibility."
  823. ^ self subclass: aString instanceVariableNames: anotherString package: nil
  824. !
  825. subclass: aString instanceVariableNames: aString2 category: aString3
  826. "Kept for file-in compatibility."
  827. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  828. !
  829. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  830. "Kept for file-in compatibility. ignores class variables and pools."
  831. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  832. !
  833. subclass: aString instanceVariableNames: aString2 package: aString3
  834. ^ ClassBuilder new
  835. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  836. !
  837. subclass: aString uses: aTraitCompositionDescription
  838. "Kept for file-in compatibility."
  839. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: '' package: nil
  840. !
  841. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: anotherString
  842. "Kept for file-in compatibility."
  843. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: anotherString package: nil
  844. !
  845. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 category: aString3
  846. "Kept for file-in compatibility."
  847. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  848. !
  849. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  850. "Kept for file-in compatibility. ignores class variables and pools."
  851. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  852. !
  853. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  854. | cls |
  855. cls := self subclass: aString instanceVariableNames: aString2 package: aString3.
  856. cls setTraitComposition: aTraitCompositionDescription asTraitComposition.
  857. ^ cls
  858. ! !