Kernel-Classes.st 25 KB

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