Kernel-Classes.st 28 KB

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