Kernel-Classes.st 28 KB

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