Kernel-Classes.st 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  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: (self slotsFromInstanceVariablesString: aString)
  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: (self slotsFromInstanceVariablesString: aString)
  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. slotsFromInstanceVariablesString: aString
  551. ^ (aString tokenize: ' ') reject: [ :each | each isEmpty ]
  552. !
  553. traitComposition
  554. ^ (self basicAt: 'traitComposition')
  555. ifNil: [ #() ]
  556. ifNotNil: [ :aCollection | aCollection collect: [ :each | TraitTransformation fromJSON: each ] ]
  557. !
  558. traitCompositionDefinition
  559. ^ self traitComposition ifNotEmpty: [ :traitComposition |
  560. String streamContents: [ :str |
  561. str write: '{'.
  562. traitComposition
  563. do: [ :each | str write: each definition ]
  564. separatedBy: [ str write: '. ' ].
  565. str write: '}' ] ]
  566. ! !
  567. !TBehaviorProvider methodsFor: 'compiling'!
  568. addCompiledMethod: aMethod
  569. | oldMethod announcement |
  570. oldMethod := self methodDictionary
  571. at: aMethod selector
  572. ifAbsent: [ nil ].
  573. self basicAddCompiledMethod: aMethod.
  574. announcement := oldMethod
  575. ifNil: [
  576. MethodAdded new
  577. method: aMethod;
  578. yourself ]
  579. ifNotNil: [
  580. MethodModified new
  581. oldMethod: oldMethod;
  582. method: aMethod;
  583. yourself ].
  584. SystemAnnouncer current
  585. announce: announcement
  586. !
  587. compile: aString protocol: anotherString
  588. ^ Compiler new
  589. install: aString
  590. forClass: self
  591. protocol: anotherString
  592. !
  593. recompile
  594. ^ Compiler new recompile: self
  595. !
  596. removeCompiledMethod: aMethod
  597. self basicRemoveCompiledMethod: aMethod.
  598. SystemAnnouncer current
  599. announce: (MethodRemoved new
  600. method: aMethod;
  601. yourself)
  602. !
  603. setTraitComposition: aTraitComposition
  604. <inlineJS: '$core.setTraitComposition(aTraitComposition._asJavaScriptObject(), self)'>
  605. ! !
  606. !TBehaviorProvider methodsFor: 'enumerating'!
  607. protocolsDo: aBlock
  608. "Execute aBlock for each method protocol with
  609. its collection of methods in the sort order of protocol name."
  610. | methodsByProtocol |
  611. methodsByProtocol := HashedCollection new.
  612. self methodDictionary valuesDo: [ :m |
  613. (methodsByProtocol at: m protocol ifAbsentPut: [ Array new ])
  614. add: m ].
  615. self protocols do: [ :protocol |
  616. aBlock value: protocol value: (methodsByProtocol at: protocol) ]
  617. ! !
  618. !TBehaviorProvider methodsFor: 'private'!
  619. basicAddCompiledMethod: aMethod
  620. <inlineJS: '$core.addMethod(aMethod, self)'>
  621. !
  622. basicRemoveCompiledMethod: aMethod
  623. <inlineJS: '$core.removeMethod(aMethod,self)'>
  624. ! !
  625. !TBehaviorProvider methodsFor: 'testing'!
  626. includesSelector: aString
  627. ^ self methodDictionary includesKey: aString
  628. ! !
  629. Trait named: #TMasterBehavior
  630. package: 'Kernel-Classes'!
  631. !TMasterBehavior commentStamp!
  632. I am the behavior on the instance-side of the browser.
  633. I define things like package, category, name, comment etc.
  634. as opposed to derived behaviors (metaclass, class trait, ...)
  635. that relate to me.!
  636. !TMasterBehavior methodsFor: 'accessing'!
  637. category
  638. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  639. !
  640. classTag
  641. "Every master behavior should define a class tag."
  642. ^ self subclassResponsibility
  643. !
  644. comment
  645. ^ (self basicAt: 'comment') ifNil: [ '' ]
  646. !
  647. comment: aString
  648. self basicAt: 'comment' put: aString.
  649. SystemAnnouncer current
  650. announce: (ClassCommentChanged new
  651. theClass: self;
  652. yourself)
  653. !
  654. definedMethods
  655. "Answers methods of me and derived 'meta' part if present"
  656. | methods |
  657. methods := self methods.
  658. self theMetaClass
  659. ifNil: [ ^ methods ]
  660. ifNotNil: [ :meta | ^ methods, meta methods ]
  661. !
  662. enterOrganization
  663. Smalltalk ifNotNil: [
  664. (self basicAt: 'category')
  665. ifNil: [ self basicPackage: nil ]
  666. ifNotNil: [ :category |
  667. "Amber has 1-1 correspondence between cat and pkg, atm"
  668. self basicPackage: (Package named: category).
  669. self package organization addElement: self ] ]
  670. !
  671. leaveOrganization
  672. Smalltalk ifNotNil: [
  673. self package organization removeElement: self ]
  674. !
  675. name
  676. <inlineJS: 'return self.name'>
  677. !
  678. package: aPackage
  679. | oldPackage |
  680. self package = aPackage ifTrue: [ ^ self ].
  681. oldPackage := self package.
  682. self
  683. leaveOrganization;
  684. basicAt: 'category' put: aPackage name;
  685. enterOrganization.
  686. SystemAnnouncer current announce: (ClassMoved new
  687. theClass: self;
  688. oldPackage: oldPackage;
  689. yourself)
  690. !
  691. theNonMetaClass
  692. ^ self
  693. ! !
  694. !TMasterBehavior methodsFor: 'browsing'!
  695. browse
  696. Finder findClass: self
  697. ! !
  698. !TMasterBehavior methodsFor: 'converting'!
  699. asJavaScriptSource
  700. ^ '$globals.', self name
  701. ! !
  702. Object subclass: #Trait
  703. instanceVariableNames: 'organization package traitUsers'
  704. package: 'Kernel-Classes'!
  705. !Trait methodsFor: 'accessing'!
  706. basicOrganization
  707. ^ organization
  708. !
  709. basicOrganization: aClassOrganizer
  710. organization := aClassOrganizer
  711. !
  712. basicPackage: aPackage
  713. package := aPackage
  714. !
  715. classTag
  716. ^ 'trait'
  717. !
  718. definition
  719. ^ String streamContents: [ :stream | stream
  720. write: 'Trait named: '; printSymbol: self name; lf;
  721. write: (self traitCompositionDefinition ifNotEmpty: [ :tcd | { String tab. 'uses: '. tcd. String lf }]);
  722. tab; write: 'package: '; print: self category ]
  723. !
  724. package
  725. ^ package
  726. !
  727. theMetaClass
  728. ^ nil
  729. !
  730. traitUsers
  731. ^ traitUsers copy
  732. ! !
  733. !Trait methodsFor: 'composition'!
  734. - anArray
  735. ^ self asTraitTransformation - anArray
  736. !
  737. @ anArrayOfAssociations
  738. ^ self asTraitTransformation @ anArrayOfAssociations
  739. ! !
  740. !Trait methodsFor: 'converting'!
  741. asTraitComposition
  742. ^ self asTraitTransformation asTraitComposition
  743. !
  744. asTraitTransformation
  745. ^ TraitTransformation on: self
  746. ! !
  747. !Trait class methodsFor: 'instance creation'!
  748. named: aString package: anotherString
  749. ^ ClassBuilder new addTraitNamed: aString package: anotherString
  750. !
  751. named: aString uses: aTraitCompositionDescription package: anotherString
  752. | trait |
  753. trait := self named: aString package: anotherString.
  754. trait setTraitComposition: aTraitCompositionDescription asTraitComposition.
  755. ^ trait
  756. ! !
  757. Object subclass: #TraitTransformation
  758. instanceVariableNames: 'trait aliases exclusions'
  759. package: 'Kernel-Classes'!
  760. !TraitTransformation commentStamp!
  761. I am a single step in trait composition.
  762. I represent one trait including its aliases and exclusions.!
  763. !TraitTransformation methodsFor: 'accessing'!
  764. addAliases: anArrayOfAssociations
  765. anArrayOfAssociations do: [ :each |
  766. | key |
  767. key := each key.
  768. aliases at: key
  769. ifPresent: [ self error: 'Cannot use same alias name twice.' ]
  770. ifAbsent: [ aliases at: key put: each value ] ].
  771. ^ anArrayOfAssociations
  772. !
  773. addExclusions: anArray
  774. exclusions addAll: anArray.
  775. ^ anArray
  776. !
  777. aliases
  778. ^ aliases
  779. !
  780. definition
  781. ^ String streamContents: [ :str |
  782. str print: self trait.
  783. self aliases ifNotEmpty: [ :al |
  784. str write: ' @ {'.
  785. al associations
  786. do: [ :each | str printSymbol: each key; write: ' -> '; printSymbol: each value ]
  787. separatedBy: [ str write: '. ' ].
  788. str write: '}' ].
  789. self exclusions ifNotEmpty: [ :ex |
  790. str write: ' - #('.
  791. ex asArray sorted
  792. do: [ :each | str write: each symbolPrintString allButFirst ]
  793. separatedBy: [ str space ].
  794. str write: ')' ] ]
  795. !
  796. exclusions
  797. ^ exclusions
  798. !
  799. trait
  800. ^ trait
  801. !
  802. trait: anObject
  803. trait := anObject
  804. ! !
  805. !TraitTransformation methodsFor: 'composition'!
  806. - anArray
  807. ^ self copy addExclusions: anArray; yourself
  808. !
  809. @ anArrayOfAssociations
  810. ^ self copy addAliases: anArrayOfAssociations; yourself
  811. ! !
  812. !TraitTransformation methodsFor: 'converting'!
  813. asJavaScriptObject
  814. ^ #{
  815. 'trait' -> self trait.
  816. 'aliases' -> self aliases.
  817. 'exclusions' -> self exclusions asArray sorted }
  818. !
  819. asJavaScriptSource
  820. ^ String streamContents: [ :str | str write: {
  821. '{trait: '. self trait asJavaScriptSource.
  822. self aliases ifNotEmpty: [ :al |
  823. {', aliases: '. al asJSONString} ].
  824. self exclusions ifNotEmpty: [ :ex |
  825. {', exclusions: '. ex asArray sorted asJavaScriptSource} ].
  826. '}' } ]
  827. !
  828. asTraitComposition
  829. ^ { self }
  830. !
  831. asTraitTransformation
  832. ^ self
  833. ! !
  834. !TraitTransformation methodsFor: 'copying'!
  835. postCopy
  836. aliases := aliases copy.
  837. exclusions := exclusions copy
  838. ! !
  839. !TraitTransformation methodsFor: 'initialization'!
  840. initialize
  841. super initialize.
  842. aliases := #{}.
  843. exclusions := Set new.
  844. trait := nil
  845. ! !
  846. !TraitTransformation class methodsFor: 'instance creation'!
  847. fromJSON: aJSObject
  848. ^ super new
  849. trait: (aJSObject at: #trait);
  850. addAliases: (Smalltalk readJSObject: (aJSObject at: #aliases ifAbsent: [#{}])) associations;
  851. addExclusions: (aJSObject at: #exclusions ifAbsent: [#()]);
  852. yourself
  853. !
  854. on: aTrait
  855. ^ super new trait: aTrait; yourself
  856. ! !
  857. Behavior setTraitComposition: {TBehaviorDefaults. TBehaviorProvider} asTraitComposition!
  858. Class setTraitComposition: {TMasterBehavior. TSubclassable} asTraitComposition!
  859. Trait setTraitComposition: {TBehaviorDefaults. TBehaviorProvider. TMasterBehavior} asTraitComposition!
  860. ! !
  861. !Array methodsFor: '*Kernel-Classes'!
  862. asTraitComposition
  863. "not implemented yet, noop atm"
  864. ^ self collect: [ :each | each asTraitTransformation ]
  865. ! !