Kernel-Classes.st 25 KB

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