Kernel-Classes.st 28 KB

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