Kernel-Classes.st 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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 slots
  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. SystemAnnouncer current announce: (ClassRenamed new
  341. theClass: oldClass;
  342. yourself).
  343. exception pass ].
  344. self
  345. rawRenameClass: oldClass to: tmp;
  346. rawRenameClass: newClass to: className.
  347. oldClass subclasses
  348. do: [ :each | self migrateClass: each superclass: newClass ].
  349. self basicRemoveClass: oldClass.
  350. SystemAnnouncer current announce: (ClassMigrated new
  351. theClass: newClass;
  352. oldClass: oldClass;
  353. yourself).
  354. ^ newClass
  355. !
  356. renameClass: aClass to: className
  357. self basicRenameClass: aClass to: className.
  358. "Recompile the class to fix potential issues with super sends"
  359. aClass recompile.
  360. SystemAnnouncer current
  361. announce: (ClassRenamed new
  362. theClass: aClass;
  363. yourself)
  364. ! !
  365. !ClassBuilder methodsFor: 'copying'!
  366. copyClass: aClass named: className
  367. | newClass |
  368. newClass := self
  369. addSubclassOf: aClass superclass
  370. named: className
  371. slots: aClass slots copy
  372. package: aClass package name.
  373. self copyClass: aClass to: newClass.
  374. SystemAnnouncer current
  375. announce: (ClassAdded new
  376. theClass: newClass;
  377. yourself).
  378. ^ newClass
  379. !
  380. copyClass: aClass to: anotherClass
  381. anotherClass comment: aClass comment.
  382. aClass methodDictionary valuesDo: [ :each |
  383. each origin = aClass ifTrue: [
  384. Compiler new install: each source forClass: anotherClass protocol: each protocol ] ].
  385. anotherClass setTraitComposition: aClass traitComposition.
  386. self basicClass: anotherClass class slots: aClass class slots copy.
  387. aClass class methodDictionary valuesDo: [ :each |
  388. each origin = aClass class ifTrue: [
  389. Compiler new install: each source forClass: anotherClass class protocol: each protocol ] ].
  390. anotherClass class setTraitComposition: aClass class traitComposition
  391. ! !
  392. !ClassBuilder methodsFor: 'private'!
  393. basicAddSubclassOf: aClass named: aString slots: aCollection package: packageName
  394. <inlineJS: '
  395. var klass = $core.addClass(aString, aClass, packageName);
  396. $core.setSlots(klass, aCollection);
  397. return klass;
  398. '>
  399. !
  400. basicAddTraitNamed: aString package: anotherString
  401. <inlineJS: 'return $core.addTrait(aString, anotherString)'>
  402. !
  403. basicClass: aClass slots: aCollection
  404. aClass isMetaclass ifFalse: [ self error: aClass name, ' is not a metaclass' ].
  405. Smalltalk core setSlots: aClass to: aCollection
  406. !
  407. basicRemoveClass: aClass
  408. <inlineJS: '$core.removeClass(aClass)'>
  409. !
  410. basicRenameClass: aClass to: aString
  411. <inlineJS: '
  412. $globals[aString] = aClass;
  413. delete $globals[aClass.name];
  414. aClass.name = aString;
  415. '>
  416. !
  417. basicSwapClassNames: aClass with: anotherClass
  418. <inlineJS: '
  419. var tmp = aClass.name;
  420. aClass.name = anotherClass.name;
  421. anotherClass.name = tmp;
  422. '>
  423. !
  424. rawRenameClass: aClass to: aString
  425. <inlineJS: '
  426. $globals[aString] = aClass;
  427. '>
  428. ! !
  429. !ClassBuilder class methodsFor: 'as yet unclassified'!
  430. sortClasses: aCollection
  431. | root members |
  432. root := {nil. {}}.
  433. members := HashedCollection new.
  434. aCollection do: [ :each | members at: each name put: {each. {}} ].
  435. (aCollection asArray sorted: [ :a :b | a name <= b name ]) do: [ :each |
  436. | target |
  437. target := members
  438. at: (each superclass ifNotNil: [ :superklass | superklass name ])
  439. ifAbsent: [ root ].
  440. target second add: (members at: each name) ].
  441. ^ root second
  442. ! !
  443. Trait named: #TBehaviorDefaults
  444. package: 'Kernel-Classes'!
  445. !TBehaviorDefaults methodsFor: 'accessing'!
  446. name
  447. ^ nil
  448. !
  449. slots
  450. "Default for non-classes; to be able to send #slots to any class / trait."
  451. ^ #()
  452. !
  453. superclass
  454. "Default for non-classes; to be able to send #superclass to any class / trait."
  455. ^ nil
  456. !
  457. traitUsers
  458. "Default for non-traits; to be able to send #traitUsers to any class / trait"
  459. ^ #()
  460. ! !
  461. !TBehaviorDefaults methodsFor: 'enumerating'!
  462. allSubclassesDo: aBlock
  463. "Default for non-classes; to be able to send #allSubclassesDo: to any class / trait."
  464. !
  465. includingPossibleMetaDo: aBlock
  466. "Default for non-classes."
  467. aBlock value: self
  468. ! !
  469. !TBehaviorDefaults methodsFor: 'printing'!
  470. printOn: aStream
  471. self name
  472. ifNil: [ super printOn: aStream ]
  473. ifNotNil: [ :name | aStream nextPutAll: name ]
  474. ! !
  475. Trait named: #TBehaviorProvider
  476. package: 'Kernel-Classes'!
  477. !TBehaviorProvider commentStamp!
  478. I have method dictionary, slots and organization.!
  479. !TBehaviorProvider methodsFor: 'accessing'!
  480. >> aString
  481. ^ self methodAt: aString
  482. !
  483. allInstanceVariableNames
  484. ^ self allSlots select: #isString
  485. !
  486. allSlotNames
  487. ^ self allSlots
  488. !
  489. allSlots
  490. | result |
  491. result := self slots copy.
  492. self superclass ifNotNil: [ :s | result addAll: s allSlots ].
  493. ^ result
  494. !
  495. instanceVariableNames
  496. ^ self slots select: #isString
  497. !
  498. methodAt: aString
  499. ^ self methodDictionary at: aString
  500. !
  501. methodDictionary
  502. <inlineJS: 'var dict = $globals.HashedCollection._new();
  503. var methods = self.methods;
  504. Object.keys(methods).forEach(function(i) {
  505. if(methods[i].selector) {
  506. dict._at_put_(methods[i].selector, methods[i]);
  507. }
  508. });
  509. return dict'>
  510. !
  511. methodOrganizationEnter: aMethod andLeave: oldMethod
  512. aMethod ifNotNil: [
  513. self organization addElement: aMethod protocol ].
  514. oldMethod ifNotNil: [
  515. self removeProtocolIfEmpty: oldMethod protocol ]
  516. !
  517. methodTemplate
  518. ^ String streamContents: [ :stream | stream
  519. write: 'messageSelectorAndArgumentNames'; lf;
  520. tab; write: '"comment stating purpose of message"'; lf;
  521. lf;
  522. tab; write: '| temporary variable names |'; lf;
  523. tab; write: 'statements' ]
  524. !
  525. methods
  526. ^ self methodDictionary values
  527. !
  528. methodsInProtocol: aString
  529. ^ self methods select: [ :each | each protocol = aString ]
  530. !
  531. organization
  532. ^ self basicOrganization ifNil: [
  533. self basicOrganization: (ClassOrganizer on: self).
  534. self basicOrganization ]
  535. !
  536. ownMethods
  537. "Answer the methods of the receiver that are not package extensions
  538. nor obtained via trait composition"
  539. ^ (self ownProtocols
  540. inject: OrderedCollection new
  541. into: [ :acc :each | acc, (self ownMethodsInProtocol: each) ])
  542. sorted: [ :a :b | a selector <= b selector ]
  543. !
  544. ownMethodsInProtocol: aString
  545. ^ (self methodsInProtocol: aString) select: [ :each | each origin = self ]
  546. !
  547. ownProtocols
  548. "Answer the protocols of the receiver that are not package extensions"
  549. ^ self protocols reject: [ :each |
  550. each match: '^\*' ]
  551. !
  552. packageOfProtocol: aString
  553. "Answer the package the method of receiver belongs to:
  554. - if it is an extension method, answer the corresponding package
  555. - else answer the receiver's package"
  556. (aString beginsWith: '*') ifFalse: [
  557. ^ self package ].
  558. ^ Package
  559. named: aString allButFirst
  560. ifAbsent: [ nil ]
  561. !
  562. protocols
  563. ^ self organization elements asArray sorted
  564. !
  565. removeProtocolIfEmpty: aString
  566. self methods
  567. detect: [ :each | each protocol = aString ]
  568. ifNone: [ self organization removeElement: aString ]
  569. !
  570. selectors
  571. ^ self methodDictionary keys
  572. !
  573. slotNames
  574. ^ self slots
  575. !
  576. traitComposition
  577. ^ (self basicAt: 'traitComposition')
  578. ifNil: [ #() ]
  579. ifNotNil: [ :aCollection | aCollection collect: [ :each | TraitTransformation fromJSON: each ] ]
  580. !
  581. traitCompositionDefinition
  582. ^ self traitComposition ifNotEmpty: [ :traitComposition |
  583. String streamContents: [ :str |
  584. str write: '{'.
  585. traitComposition
  586. do: [ :each | str write: each definition ]
  587. separatedBy: [ str write: '. ' ].
  588. str write: '}' ] ]
  589. ! !
  590. !TBehaviorProvider methodsFor: 'compiling'!
  591. addCompiledMethod: aMethod
  592. | oldMethod announcement |
  593. oldMethod := self methodDictionary
  594. at: aMethod selector
  595. ifAbsent: [ nil ].
  596. self basicAddCompiledMethod: aMethod.
  597. announcement := oldMethod
  598. ifNil: [
  599. MethodAdded new
  600. method: aMethod;
  601. yourself ]
  602. ifNotNil: [
  603. MethodModified new
  604. oldMethod: oldMethod;
  605. method: aMethod;
  606. yourself ].
  607. SystemAnnouncer current
  608. announce: announcement
  609. !
  610. compile: aString protocol: anotherString
  611. ^ Compiler new
  612. install: aString
  613. forClass: self
  614. protocol: anotherString
  615. !
  616. recompile
  617. ^ Compiler new recompile: self
  618. !
  619. removeCompiledMethod: aMethod
  620. self basicRemoveCompiledMethod: aMethod.
  621. SystemAnnouncer current
  622. announce: (MethodRemoved new
  623. method: aMethod;
  624. yourself)
  625. !
  626. setTraitComposition: aTraitComposition
  627. <inlineJS: '$core.setTraitComposition(aTraitComposition._asJavaScriptObject(), self)'>
  628. ! !
  629. !TBehaviorProvider methodsFor: 'enumerating'!
  630. protocolsDo: aBlock
  631. "Execute aBlock for each method protocol with
  632. its collection of methods in the sort order of protocol name."
  633. | methodsByProtocol |
  634. methodsByProtocol := HashedCollection new.
  635. self methodDictionary valuesDo: [ :m |
  636. (methodsByProtocol at: m protocol ifAbsentPut: [ Array new ])
  637. add: m ].
  638. self protocols do: [ :protocol |
  639. aBlock value: protocol value: (methodsByProtocol at: protocol) ]
  640. ! !
  641. !TBehaviorProvider methodsFor: 'private'!
  642. basicAddCompiledMethod: aMethod
  643. <inlineJS: '$core.addMethod(aMethod, self)'>
  644. !
  645. basicRemoveCompiledMethod: aMethod
  646. <inlineJS: '$core.removeMethod(aMethod,self)'>
  647. ! !
  648. !TBehaviorProvider methodsFor: 'testing'!
  649. includesSelector: aString
  650. ^ self methodDictionary includesKey: aString
  651. ! !
  652. Trait named: #TMasterBehavior
  653. package: 'Kernel-Classes'!
  654. !TMasterBehavior commentStamp!
  655. I am the behavior on the instance-side of the browser.
  656. I define things like package, category, name, comment etc.
  657. as opposed to derived behaviors (metaclass, class trait, ...)
  658. that relate to me.!
  659. !TMasterBehavior methodsFor: 'accessing'!
  660. category
  661. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  662. !
  663. classTag
  664. "Every master behavior should define a class tag."
  665. ^ self subclassResponsibility
  666. !
  667. comment
  668. ^ (self basicAt: 'comment') ifNil: [ '' ]
  669. !
  670. comment: aString
  671. self basicAt: 'comment' put: aString.
  672. SystemAnnouncer current
  673. announce: (ClassCommentChanged new
  674. theClass: self;
  675. yourself)
  676. !
  677. definedMethods
  678. "Answers methods of me and derived 'meta' part if present"
  679. | methods |
  680. methods := self methods.
  681. self theMetaClass
  682. ifNil: [ ^ methods ]
  683. ifNotNil: [ :meta | ^ methods, meta methods ]
  684. !
  685. enterOrganization
  686. Smalltalk ifNotNil: [
  687. (self basicAt: 'category')
  688. ifNil: [ self basicPackage: nil ]
  689. ifNotNil: [ :category |
  690. "Amber has 1-1 correspondence between cat and pkg, atm"
  691. self basicPackage: (Package named: category).
  692. self package organization addElement: self ] ]
  693. !
  694. leaveOrganization
  695. Smalltalk ifNotNil: [
  696. self package organization removeElement: self ]
  697. !
  698. name
  699. <inlineJS: 'return self.name'>
  700. !
  701. package: aPackage
  702. | oldPackage |
  703. self package = aPackage ifTrue: [ ^ self ].
  704. oldPackage := self package.
  705. self
  706. leaveOrganization;
  707. basicAt: 'category' put: aPackage name;
  708. enterOrganization.
  709. SystemAnnouncer current announce: (ClassMoved new
  710. theClass: self;
  711. oldPackage: oldPackage;
  712. yourself)
  713. !
  714. theNonMetaClass
  715. ^ self
  716. ! !
  717. !TMasterBehavior methodsFor: 'browsing'!
  718. browse
  719. Finder findClass: self
  720. ! !
  721. !TMasterBehavior methodsFor: 'converting'!
  722. asJavaScriptSource
  723. ^ '$globals.', self name
  724. ! !
  725. Object subclass: #Trait
  726. slots: {#organization. #package. #traitUsers}
  727. package: 'Kernel-Classes'!
  728. !Trait methodsFor: 'accessing'!
  729. basicOrganization
  730. ^ organization
  731. !
  732. basicOrganization: aClassOrganizer
  733. organization := aClassOrganizer
  734. !
  735. basicPackage: aPackage
  736. package := aPackage
  737. !
  738. classTag
  739. ^ 'trait'
  740. !
  741. definition
  742. ^ String streamContents: [ :stream | stream
  743. write: 'Trait named: '; printSymbol: self name; lf;
  744. write: (self traitCompositionDefinition ifNotEmpty: [ :tcd | { String tab. 'uses: '. tcd. String lf }]);
  745. tab; write: 'package: '; print: self category ]
  746. !
  747. package
  748. ^ package
  749. !
  750. theMetaClass
  751. ^ nil
  752. !
  753. traitUsers
  754. ^ traitUsers copy
  755. ! !
  756. !Trait methodsFor: 'composition'!
  757. - anArray
  758. ^ self asTraitTransformation - anArray
  759. !
  760. @ anArrayOfAssociations
  761. ^ self asTraitTransformation @ anArrayOfAssociations
  762. ! !
  763. !Trait methodsFor: 'converting'!
  764. asTraitComposition
  765. ^ self asTraitTransformation asTraitComposition
  766. !
  767. asTraitTransformation
  768. ^ TraitTransformation on: self
  769. ! !
  770. !Trait class methodsFor: 'instance creation'!
  771. named: aString package: anotherString
  772. ^ ClassBuilder new addTraitNamed: aString package: anotherString
  773. !
  774. named: aString uses: aTraitCompositionDescription package: anotherString
  775. | trait |
  776. trait := self named: aString package: anotherString.
  777. trait setTraitComposition: aTraitCompositionDescription asTraitComposition.
  778. ^ trait
  779. ! !
  780. Object subclass: #TraitTransformation
  781. slots: {#trait. #aliases. #exclusions}
  782. package: 'Kernel-Classes'!
  783. !TraitTransformation commentStamp!
  784. I am a single step in trait composition.
  785. I represent one trait including its aliases and exclusions.!
  786. !TraitTransformation methodsFor: 'accessing'!
  787. addAliases: anArrayOfAssociations
  788. anArrayOfAssociations do: [ :each |
  789. | key |
  790. key := each key.
  791. aliases at: key
  792. ifPresent: [ self error: 'Cannot use same alias name twice.' ]
  793. ifAbsent: [ aliases at: key put: each value ] ].
  794. ^ anArrayOfAssociations
  795. !
  796. addExclusions: anArray
  797. exclusions addAll: anArray.
  798. ^ anArray
  799. !
  800. aliases
  801. ^ aliases
  802. !
  803. definition
  804. ^ String streamContents: [ :str |
  805. str print: self trait.
  806. self aliases ifNotEmpty: [ :al |
  807. str write: ' @ {'.
  808. al associations
  809. do: [ :each | str printSymbol: each key; write: ' -> '; printSymbol: each value ]
  810. separatedBy: [ str write: '. ' ].
  811. str write: '}' ].
  812. self exclusions ifNotEmpty: [ :ex |
  813. str write: ' - #('.
  814. ex asArray sorted
  815. do: [ :each | str write: each symbolPrintString allButFirst ]
  816. separatedBy: [ str space ].
  817. str write: ')' ] ]
  818. !
  819. exclusions
  820. ^ exclusions
  821. !
  822. trait
  823. ^ trait
  824. !
  825. trait: anObject
  826. trait := anObject
  827. ! !
  828. !TraitTransformation methodsFor: 'composition'!
  829. - anArray
  830. ^ self copy addExclusions: anArray; yourself
  831. !
  832. @ anArrayOfAssociations
  833. ^ self copy addAliases: anArrayOfAssociations; yourself
  834. ! !
  835. !TraitTransformation methodsFor: 'converting'!
  836. asJavaScriptObject
  837. ^ #{
  838. 'trait' -> self trait.
  839. 'aliases' -> self aliases.
  840. 'exclusions' -> self exclusions asArray sorted }
  841. !
  842. asJavaScriptSource
  843. ^ String streamContents: [ :str | str write: {
  844. '{trait: '. self trait asJavaScriptSource.
  845. self aliases ifNotEmpty: [ :al |
  846. {', aliases: '. al asJSONString} ].
  847. self exclusions ifNotEmpty: [ :ex |
  848. {', exclusions: '. ex asArray sorted asJavaScriptSource} ].
  849. '}' } ]
  850. !
  851. asTraitComposition
  852. ^ { self }
  853. !
  854. asTraitTransformation
  855. ^ self
  856. ! !
  857. !TraitTransformation methodsFor: 'copying'!
  858. postCopy
  859. aliases := aliases copy.
  860. exclusions := exclusions copy
  861. ! !
  862. !TraitTransformation methodsFor: 'initialization'!
  863. initialize
  864. super initialize.
  865. aliases := #{}.
  866. exclusions := Set new.
  867. trait := nil
  868. ! !
  869. !TraitTransformation class methodsFor: 'instance creation'!
  870. fromJSON: aJSObject
  871. ^ super new
  872. trait: (aJSObject at: #trait);
  873. addAliases: (Smalltalk readJSObject: (aJSObject at: #aliases ifAbsent: [#{}])) associations;
  874. addExclusions: (aJSObject at: #exclusions ifAbsent: [#()]);
  875. yourself
  876. !
  877. on: aTrait
  878. ^ super new trait: aTrait; yourself
  879. ! !
  880. Behavior setTraitComposition: {TBehaviorDefaults. TBehaviorProvider} asTraitComposition!
  881. Class setTraitComposition: {TMasterBehavior. TSubclassable} asTraitComposition!
  882. Trait setTraitComposition: {TBehaviorDefaults. TBehaviorProvider. TMasterBehavior} asTraitComposition!
  883. ! !
  884. !Array methodsFor: '*Kernel-Classes'!
  885. asTraitComposition
  886. "not implemented yet, noop atm"
  887. ^ self collect: [ :each | each asTraitTransformation ]
  888. ! !
  889. !String methodsFor: '*Kernel-Classes'!
  890. instanceVariablesStringAsSlotList
  891. ^ (self tokenize: ' ') reject: [ :each | each isEmpty ]
  892. ! !