Kernel-Classes.st 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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. 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 slots copy.
  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. allSlotNames
  484. ^ self allSlots
  485. !
  486. allSlots
  487. | result |
  488. result := self slots copy.
  489. self superclass ifNotNil: [ :s | result addAll: s allSlots ].
  490. ^ result
  491. !
  492. instanceVariableNames
  493. ^ self slots select: #isString
  494. !
  495. methodAt: aString
  496. ^ self methodDictionary at: aString
  497. !
  498. methodDictionary
  499. <inlineJS: 'var dict = $globals.HashedCollection._new();
  500. var methods = self.methods;
  501. Object.keys(methods).forEach(function(i) {
  502. if(methods[i].selector) {
  503. dict._at_put_(methods[i].selector, methods[i]);
  504. }
  505. });
  506. return dict'>
  507. !
  508. methodOrganizationEnter: aMethod andLeave: oldMethod
  509. aMethod ifNotNil: [
  510. self organization addElement: aMethod protocol ].
  511. oldMethod ifNotNil: [
  512. self removeProtocolIfEmpty: oldMethod protocol ]
  513. !
  514. methodTemplate
  515. ^ String streamContents: [ :stream | stream
  516. write: 'messageSelectorAndArgumentNames'; lf;
  517. tab; write: '"comment stating purpose of message"'; lf;
  518. lf;
  519. tab; write: '| temporary variable names |'; lf;
  520. tab; write: 'statements' ]
  521. !
  522. methods
  523. ^ self methodDictionary values
  524. !
  525. methodsInProtocol: aString
  526. ^ self methods select: [ :each | each protocol = aString ]
  527. !
  528. organization
  529. ^ self basicOrganization ifNil: [
  530. self basicOrganization: (ClassOrganizer on: self).
  531. self basicOrganization ]
  532. !
  533. ownMethods
  534. "Answer the methods of the receiver that are not package extensions
  535. nor obtained via trait composition"
  536. ^ (self ownProtocols
  537. inject: OrderedCollection new
  538. into: [ :acc :each | acc, (self ownMethodsInProtocol: each) ])
  539. sorted: [ :a :b | a selector <= b selector ]
  540. !
  541. ownMethodsInProtocol: aString
  542. ^ (self methodsInProtocol: aString) select: [ :each | each origin = self ]
  543. !
  544. ownProtocols
  545. "Answer the protocols of the receiver that are not package extensions"
  546. ^ self protocols reject: [ :each |
  547. each match: '^\*' ]
  548. !
  549. packageOfProtocol: aString
  550. "Answer the package the method of receiver belongs to:
  551. - if it is an extension method, answer the corresponding package
  552. - else answer the receiver's package"
  553. (aString beginsWith: '*') ifFalse: [
  554. ^ self package ].
  555. ^ Package
  556. named: aString allButFirst
  557. ifAbsent: [ nil ]
  558. !
  559. protocols
  560. ^ self organization elements asArray sorted
  561. !
  562. removeProtocolIfEmpty: aString
  563. self methods
  564. detect: [ :each | each protocol = aString ]
  565. ifNone: [ self organization removeElement: aString ]
  566. !
  567. selectors
  568. ^ self methodDictionary keys
  569. !
  570. slotNames
  571. ^ self slots
  572. !
  573. traitComposition
  574. ^ (self basicAt: 'traitComposition')
  575. ifNil: [ #() ]
  576. ifNotNil: [ :aCollection | aCollection collect: [ :each | TraitTransformation fromJSON: each ] ]
  577. !
  578. traitCompositionDefinition
  579. ^ self traitComposition ifNotEmpty: [ :traitComposition |
  580. String streamContents: [ :str |
  581. str write: '{'.
  582. traitComposition
  583. do: [ :each | str write: each definition ]
  584. separatedBy: [ str write: '. ' ].
  585. str write: '}' ] ]
  586. ! !
  587. !TBehaviorProvider methodsFor: 'compiling'!
  588. addCompiledMethod: aMethod
  589. | oldMethod announcement |
  590. oldMethod := self methodDictionary
  591. at: aMethod selector
  592. ifAbsent: [ nil ].
  593. self basicAddCompiledMethod: aMethod.
  594. announcement := oldMethod
  595. ifNil: [
  596. MethodAdded new
  597. method: aMethod;
  598. yourself ]
  599. ifNotNil: [
  600. MethodModified new
  601. oldMethod: oldMethod;
  602. method: aMethod;
  603. yourself ].
  604. SystemAnnouncer current
  605. announce: announcement
  606. !
  607. compile: aString protocol: anotherString
  608. ^ Compiler new
  609. install: aString
  610. forClass: self
  611. protocol: anotherString
  612. !
  613. recompile
  614. ^ Compiler new recompile: self
  615. !
  616. removeCompiledMethod: aMethod
  617. self basicRemoveCompiledMethod: aMethod.
  618. SystemAnnouncer current
  619. announce: (MethodRemoved new
  620. method: aMethod;
  621. yourself)
  622. !
  623. setTraitComposition: aTraitComposition
  624. <inlineJS: '$core.setTraitComposition(aTraitComposition._asJavaScriptObject(), self)'>
  625. ! !
  626. !TBehaviorProvider methodsFor: 'enumerating'!
  627. protocolsDo: aBlock
  628. "Execute aBlock for each method protocol with
  629. its collection of methods in the sort order of protocol name."
  630. | methodsByProtocol |
  631. methodsByProtocol := HashedCollection new.
  632. self methodDictionary valuesDo: [ :m |
  633. (methodsByProtocol at: m protocol ifAbsentPut: [ Array new ])
  634. add: m ].
  635. self protocols do: [ :protocol |
  636. aBlock value: protocol value: (methodsByProtocol at: protocol) ]
  637. ! !
  638. !TBehaviorProvider methodsFor: 'private'!
  639. basicAddCompiledMethod: aMethod
  640. <inlineJS: '$core.addMethod(aMethod, self)'>
  641. !
  642. basicRemoveCompiledMethod: aMethod
  643. <inlineJS: '$core.removeMethod(aMethod,self)'>
  644. ! !
  645. !TBehaviorProvider methodsFor: 'testing'!
  646. includesSelector: aString
  647. ^ self methodDictionary includesKey: aString
  648. ! !
  649. Trait named: #TMasterBehavior
  650. package: 'Kernel-Classes'!
  651. !TMasterBehavior commentStamp!
  652. I am the behavior on the instance-side of the browser.
  653. I define things like package, category, name, comment etc.
  654. as opposed to derived behaviors (metaclass, class trait, ...)
  655. that relate to me.!
  656. !TMasterBehavior methodsFor: 'accessing'!
  657. category
  658. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  659. !
  660. classTag
  661. "Every master behavior should define a class tag."
  662. ^ self subclassResponsibility
  663. !
  664. comment
  665. ^ (self basicAt: 'comment') ifNil: [ '' ]
  666. !
  667. comment: aString
  668. self basicAt: 'comment' put: aString.
  669. SystemAnnouncer current
  670. announce: (ClassCommentChanged new
  671. theClass: self;
  672. yourself)
  673. !
  674. definedMethods
  675. "Answers methods of me and derived 'meta' part if present"
  676. | methods |
  677. methods := self methods.
  678. self theMetaClass
  679. ifNil: [ ^ methods ]
  680. ifNotNil: [ :meta | ^ methods, meta methods ]
  681. !
  682. enterOrganization
  683. Smalltalk ifNotNil: [
  684. (self basicAt: 'category')
  685. ifNil: [ self basicPackage: nil ]
  686. ifNotNil: [ :category |
  687. "Amber has 1-1 correspondence between cat and pkg, atm"
  688. self basicPackage: (Package named: category).
  689. self package organization addElement: self ] ]
  690. !
  691. leaveOrganization
  692. Smalltalk ifNotNil: [
  693. self package organization removeElement: self ]
  694. !
  695. name
  696. <inlineJS: 'return self.name'>
  697. !
  698. package: aPackage
  699. | oldPackage |
  700. self package = aPackage ifTrue: [ ^ self ].
  701. oldPackage := self package.
  702. self
  703. leaveOrganization;
  704. basicAt: 'category' put: aPackage name;
  705. enterOrganization.
  706. SystemAnnouncer current announce: (ClassMoved new
  707. theClass: self;
  708. oldPackage: oldPackage;
  709. yourself)
  710. !
  711. theNonMetaClass
  712. ^ self
  713. ! !
  714. !TMasterBehavior methodsFor: 'browsing'!
  715. browse
  716. Finder findClass: self
  717. ! !
  718. !TMasterBehavior methodsFor: 'converting'!
  719. asJavaScriptSource
  720. ^ '$globals.', self name
  721. ! !
  722. Object subclass: #Trait
  723. slots: {#organization. #package. #traitUsers}
  724. package: 'Kernel-Classes'!
  725. !Trait methodsFor: 'accessing'!
  726. basicOrganization
  727. ^ organization
  728. !
  729. basicOrganization: aClassOrganizer
  730. organization := aClassOrganizer
  731. !
  732. basicPackage: aPackage
  733. package := aPackage
  734. !
  735. classTag
  736. ^ 'trait'
  737. !
  738. definition
  739. ^ String streamContents: [ :stream | stream
  740. write: 'Trait named: '; printSymbol: self name; lf;
  741. write: (self traitCompositionDefinition ifNotEmpty: [ :tcd | { String tab. 'uses: '. tcd. String lf }]);
  742. tab; write: 'package: '; print: self category ]
  743. !
  744. package
  745. ^ package
  746. !
  747. theMetaClass
  748. ^ nil
  749. !
  750. traitUsers
  751. ^ traitUsers copy
  752. ! !
  753. !Trait methodsFor: 'composition'!
  754. - anArray
  755. ^ self asTraitTransformation - anArray
  756. !
  757. @ anArrayOfAssociations
  758. ^ self asTraitTransformation @ anArrayOfAssociations
  759. ! !
  760. !Trait methodsFor: 'converting'!
  761. asTraitComposition
  762. ^ self asTraitTransformation asTraitComposition
  763. !
  764. asTraitTransformation
  765. ^ TraitTransformation on: self
  766. ! !
  767. !Trait class methodsFor: 'instance creation'!
  768. named: aString package: anotherString
  769. ^ ClassBuilder new addTraitNamed: aString package: anotherString
  770. !
  771. named: aString uses: aTraitCompositionDescription package: anotherString
  772. | trait |
  773. trait := self named: aString package: anotherString.
  774. trait setTraitComposition: aTraitCompositionDescription asTraitComposition.
  775. ^ trait
  776. ! !
  777. Object subclass: #TraitTransformation
  778. slots: {#trait. #aliases. #exclusions}
  779. package: 'Kernel-Classes'!
  780. !TraitTransformation commentStamp!
  781. I am a single step in trait composition.
  782. I represent one trait including its aliases and exclusions.!
  783. !TraitTransformation methodsFor: 'accessing'!
  784. addAliases: anArrayOfAssociations
  785. anArrayOfAssociations do: [ :each |
  786. | key |
  787. key := each key.
  788. aliases at: key
  789. ifPresent: [ self error: 'Cannot use same alias name twice.' ]
  790. ifAbsent: [ aliases at: key put: each value ] ].
  791. ^ anArrayOfAssociations
  792. !
  793. addExclusions: anArray
  794. exclusions addAll: anArray.
  795. ^ anArray
  796. !
  797. aliases
  798. ^ aliases
  799. !
  800. definition
  801. ^ String streamContents: [ :str |
  802. str print: self trait.
  803. self aliases ifNotEmpty: [ :al |
  804. str write: ' @ {'.
  805. al associations
  806. do: [ :each | str printSymbol: each key; write: ' -> '; printSymbol: each value ]
  807. separatedBy: [ str write: '. ' ].
  808. str write: '}' ].
  809. self exclusions ifNotEmpty: [ :ex |
  810. str write: ' - #('.
  811. ex asArray sorted
  812. do: [ :each | str write: each symbolPrintString allButFirst ]
  813. separatedBy: [ str space ].
  814. str write: ')' ] ]
  815. !
  816. exclusions
  817. ^ exclusions
  818. !
  819. trait
  820. ^ trait
  821. !
  822. trait: anObject
  823. trait := anObject
  824. ! !
  825. !TraitTransformation methodsFor: 'composition'!
  826. - anArray
  827. ^ self copy addExclusions: anArray; yourself
  828. !
  829. @ anArrayOfAssociations
  830. ^ self copy addAliases: anArrayOfAssociations; yourself
  831. ! !
  832. !TraitTransformation methodsFor: 'converting'!
  833. asJavaScriptObject
  834. ^ #{
  835. 'trait' -> self trait.
  836. 'aliases' -> self aliases.
  837. 'exclusions' -> self exclusions asArray sorted }
  838. !
  839. asJavaScriptSource
  840. ^ String streamContents: [ :str | str write: {
  841. '{trait: '. self trait asJavaScriptSource.
  842. self aliases ifNotEmpty: [ :al |
  843. {', aliases: '. al asJSONString} ].
  844. self exclusions ifNotEmpty: [ :ex |
  845. {', exclusions: '. ex asArray sorted asJavaScriptSource} ].
  846. '}' } ]
  847. !
  848. asTraitComposition
  849. ^ { self }
  850. !
  851. asTraitTransformation
  852. ^ self
  853. ! !
  854. !TraitTransformation methodsFor: 'copying'!
  855. postCopy
  856. aliases := aliases copy.
  857. exclusions := exclusions copy
  858. ! !
  859. !TraitTransformation methodsFor: 'initialization'!
  860. initialize
  861. super initialize.
  862. aliases := #{}.
  863. exclusions := Set new.
  864. trait := nil
  865. ! !
  866. !TraitTransformation class methodsFor: 'instance creation'!
  867. fromJSON: aJSObject
  868. ^ super new
  869. trait: (aJSObject at: #trait);
  870. addAliases: (Smalltalk readJSObject: (aJSObject at: #aliases ifAbsent: [#{}])) associations;
  871. addExclusions: (aJSObject at: #exclusions ifAbsent: [#()]);
  872. yourself
  873. !
  874. on: aTrait
  875. ^ super new trait: aTrait; yourself
  876. ! !
  877. Behavior setTraitComposition: {TBehaviorDefaults. TBehaviorProvider} asTraitComposition!
  878. Class setTraitComposition: {TMasterBehavior. TSubclassable} asTraitComposition!
  879. Trait setTraitComposition: {TBehaviorDefaults. TBehaviorProvider. TMasterBehavior} asTraitComposition!
  880. ! !
  881. !Array methodsFor: '*Kernel-Classes'!
  882. asTraitComposition
  883. "not implemented yet, noop atm"
  884. ^ self collect: [ :each | each asTraitTransformation ]
  885. ! !
  886. !String methodsFor: '*Kernel-Classes'!
  887. instanceVariablesStringAsSlotList
  888. ^ (self tokenize: ' ') reject: [ :each | each isEmpty ]
  889. ! !