Kernel-Classes.st 26 KB

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