Kernel-Classes.st 26 KB

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