Kernel-Classes.st 26 KB

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