Kernel-Classes.st 24 KB

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