Kernel-Classes.st 24 KB

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