Kernel-Classes.st 24 KB

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