Kernel-Classes.st 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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. traitCompositionDefinition
  106. ^ self traitComposition ifNotEmpty: [ :traitComposition |
  107. String streamContents: [ :str |
  108. str write: '{'.
  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 traitCompositionDefinition ifNotEmpty: [ :tcd | { String tab. 'uses: '. tcd. 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 traitCompositionDefinition
  413. ifEmpty: [' ']
  414. ifNotEmpty: [ :tcd | { String lf. String tab. 'uses: '. tcd. String lf. String tab }]);
  415. write: 'instanceVariableNames: ';
  416. print: (' ' join: self instanceVariableNames) ]
  417. !
  418. instanceClass
  419. <inlineJS: 'return self.instanceClass'>
  420. !
  421. instanceVariableNames: aCollection
  422. ClassBuilder new
  423. class: self instanceVariableNames: aCollection.
  424. ^ self
  425. !
  426. name
  427. ^ self instanceClass name, ' class'
  428. !
  429. package
  430. ^ self instanceClass package
  431. !
  432. subclasses
  433. <inlineJS: 'return $core.metaSubclasses(self)'>
  434. !
  435. theMetaClass
  436. ^ self
  437. !
  438. theNonMetaClass
  439. ^ self instanceClass
  440. !
  441. uses: aTraitCompositionDescription instanceVariableNames: aCollection
  442. | metaclass |
  443. metaclass := self instanceVariableNames: aCollection.
  444. metaclass setTraitComposition: aTraitCompositionDescription asTraitComposition.
  445. ^ metaclass
  446. ! !
  447. !Metaclass methodsFor: 'converting'!
  448. asJavascript
  449. ^ '$globals.', self instanceClass name, '.klass'
  450. ! !
  451. !Metaclass methodsFor: 'testing'!
  452. isMetaclass
  453. ^ true
  454. ! !
  455. BehaviorBody subclass: #Trait
  456. instanceVariableNames: ''
  457. package: 'Kernel-Classes'!
  458. !Trait methodsFor: 'IDE compatibility'!
  459. allSubclassesDo: aBlock
  460. !
  461. superclass
  462. ^ nil
  463. ! !
  464. !Trait methodsFor: 'accessing'!
  465. category
  466. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  467. !
  468. classTag
  469. ^ 'trait'
  470. !
  471. definition
  472. ^ String streamContents: [ :stream | stream
  473. write: 'Trait named: '; printSymbol: self name; lf;
  474. write: (self traitCompositionDefinition ifNotEmpty: [ :tcd | { String tab. 'uses: '. tcd. String lf }]);
  475. tab; write: 'package: '; print: self category ]
  476. !
  477. package
  478. ^ self basicAt: 'pkg'
  479. !
  480. package: aPackage
  481. | oldPackage |
  482. self package = aPackage ifTrue: [ ^ self ].
  483. oldPackage := self package.
  484. self basicAt: 'pkg' put: aPackage.
  485. oldPackage organization removeElement: self.
  486. aPackage organization addElement: self.
  487. SystemAnnouncer current announce: (ClassMoved new
  488. theClass: self;
  489. oldPackage: oldPackage;
  490. yourself)
  491. !
  492. theMetaClass
  493. ^ nil
  494. !
  495. theNonMetaClass
  496. ^ self
  497. !
  498. traitUsers
  499. ^ (self basicAt: 'traitUsers') copy
  500. ! !
  501. !Trait methodsFor: 'compiler compatibility'!
  502. allInstanceVariableNames
  503. ^ #()
  504. ! !
  505. !Trait methodsFor: 'composition'!
  506. - anArray
  507. ^ self asTraitTransformation - anArray
  508. !
  509. @ anArrayOfAssociations
  510. ^ self asTraitTransformation @ anArrayOfAssociations
  511. ! !
  512. !Trait methodsFor: 'converting'!
  513. asJavascript
  514. ^ '$globals.', self name
  515. !
  516. asTraitComposition
  517. ^ self asTraitTransformation asTraitComposition
  518. !
  519. asTraitTransformation
  520. ^ TraitTransformation on: self
  521. ! !
  522. !Trait class methodsFor: 'instance creation'!
  523. named: aString package: anotherString
  524. ^ ClassBuilder new addTraitNamed: aString package: anotherString
  525. !
  526. named: aString uses: aTraitCompositionDescription package: anotherString
  527. | trait |
  528. trait := self named: aString package: anotherString.
  529. trait setTraitComposition: aTraitCompositionDescription asTraitComposition.
  530. ^ trait
  531. ! !
  532. Object subclass: #ClassBuilder
  533. instanceVariableNames: ''
  534. package: 'Kernel-Classes'!
  535. !ClassBuilder commentStamp!
  536. I am responsible for compiling new classes or modifying existing classes in the system.
  537. Rather than using me directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  538. !ClassBuilder methodsFor: 'accessing'!
  539. instanceVariableNamesFor: aString
  540. ^ (aString tokenize: ' ') reject: [ :each | each isEmpty ]
  541. ! !
  542. !ClassBuilder methodsFor: 'class definition'!
  543. addSubclassOf: aClass named: className instanceVariableNames: aCollection package: packageName
  544. | theClass thePackage |
  545. theClass := Smalltalk globals at: className.
  546. thePackage := Package named: packageName.
  547. theClass ifNotNil: [
  548. theClass package: thePackage.
  549. theClass superclass == aClass
  550. ifFalse: [ ^ self
  551. migrateClassNamed: className
  552. superclass: aClass
  553. instanceVariableNames: aCollection
  554. package: packageName ]
  555. ifTrue: [ ^ theClass recompile; yourself ] ].
  556. ^ self
  557. basicAddSubclassOf: aClass
  558. named: className
  559. instanceVariableNames: aCollection
  560. package: packageName
  561. !
  562. addTraitNamed: traitName package: packageName
  563. | theTrait thePackage |
  564. theTrait := Smalltalk globals at: traitName.
  565. thePackage := Package named: packageName.
  566. theTrait ifNotNil: [ ^ theTrait package: thePackage; recompile; yourself ].
  567. ^ self
  568. basicAddTraitNamed: traitName
  569. package: packageName
  570. !
  571. class: aClass instanceVariableNames: ivarNames
  572. self basicClass: aClass instanceVariableNames: ivarNames.
  573. SystemAnnouncer current
  574. announce: (ClassDefinitionChanged new
  575. theClass: aClass;
  576. yourself)
  577. !
  578. superclass: aClass subclass: className
  579. ^ self superclass: aClass subclass: className instanceVariableNames: '' package: nil
  580. !
  581. superclass: aClass subclass: className instanceVariableNames: ivarNames package: packageName
  582. | newClass |
  583. newClass := self addSubclassOf: aClass
  584. named: className instanceVariableNames: (self instanceVariableNamesFor: ivarNames)
  585. package: (packageName ifNil: [ 'unclassified' ]).
  586. SystemAnnouncer current
  587. announce: (ClassAdded new
  588. theClass: newClass;
  589. yourself).
  590. ^ newClass
  591. ! !
  592. !ClassBuilder methodsFor: 'class migration'!
  593. migrateClass: aClass superclass: anotherClass
  594. ^ self
  595. migrateClassNamed: aClass name
  596. superclass: anotherClass
  597. instanceVariableNames: aClass instanceVariableNames
  598. package: aClass package name
  599. !
  600. migrateClassNamed: className superclass: aClass instanceVariableNames: aCollection package: packageName
  601. | oldClass newClass tmp |
  602. tmp := 'new*', className.
  603. oldClass := Smalltalk globals at: className.
  604. newClass := self
  605. addSubclassOf: aClass
  606. named: tmp
  607. instanceVariableNames: aCollection
  608. package: packageName.
  609. self basicSwapClassNames: oldClass with: newClass.
  610. [ self copyClass: oldClass to: newClass ]
  611. on: Error
  612. do: [ :exception |
  613. self
  614. basicSwapClassNames: oldClass with: newClass;
  615. basicRemoveClass: newClass.
  616. exception resignal ].
  617. self
  618. rawRenameClass: oldClass to: tmp;
  619. rawRenameClass: newClass to: className.
  620. oldClass subclasses
  621. do: [ :each | self migrateClass: each superclass: newClass ].
  622. self basicRemoveClass: oldClass.
  623. SystemAnnouncer current announce: (ClassMigrated new
  624. theClass: newClass;
  625. oldClass: oldClass;
  626. yourself).
  627. ^ newClass
  628. !
  629. renameClass: aClass to: className
  630. self basicRenameClass: aClass to: className.
  631. "Recompile the class to fix potential issues with super sends"
  632. aClass recompile.
  633. SystemAnnouncer current
  634. announce: (ClassRenamed new
  635. theClass: aClass;
  636. yourself)
  637. ! !
  638. !ClassBuilder methodsFor: 'copying'!
  639. copyClass: aClass named: className
  640. | newClass |
  641. newClass := self
  642. addSubclassOf: aClass superclass
  643. named: className
  644. instanceVariableNames: aClass instanceVariableNames
  645. package: aClass package name.
  646. self copyClass: aClass to: newClass.
  647. SystemAnnouncer current
  648. announce: (ClassAdded new
  649. theClass: newClass;
  650. yourself).
  651. ^ newClass
  652. !
  653. copyClass: aClass to: anotherClass
  654. anotherClass comment: aClass comment.
  655. aClass methodDictionary valuesDo: [ :each |
  656. each methodClass = aClass ifTrue: [
  657. Compiler new install: each source forClass: anotherClass protocol: each protocol ] ].
  658. self basicClass: anotherClass class instanceVariables: aClass class instanceVariableNames.
  659. aClass class methodDictionary valuesDo: [ :each |
  660. each methodClass = aClass class ifTrue: [
  661. Compiler new install: each source forClass: anotherClass class protocol: each protocol ] ]
  662. ! !
  663. !ClassBuilder methodsFor: 'method definition'!
  664. installMethod: aCompiledMethod forClass: aBehavior protocol: aString
  665. aCompiledMethod protocol: aString.
  666. aBehavior addCompiledMethod: aCompiledMethod.
  667. ^ aCompiledMethod
  668. ! !
  669. !ClassBuilder methodsFor: 'private'!
  670. basicAddSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  671. <inlineJS: '
  672. return $core.addClass(aString, aClass, aCollection, packageName);
  673. '>
  674. !
  675. basicAddTraitNamed: aString package: anotherString
  676. <inlineJS: 'return $core.addTrait(aString, anotherString)'>
  677. !
  678. basicClass: aClass instanceVariableNames: aString
  679. self basicClass: aClass instanceVariables: (self instanceVariableNamesFor: aString)
  680. !
  681. basicClass: aClass instanceVariables: aCollection
  682. aClass isMetaclass ifFalse: [ self error: aClass name, ' is not a metaclass' ].
  683. aClass basicAt: 'iVarNames' put: aCollection
  684. !
  685. basicRemoveClass: aClass
  686. <inlineJS: '$core.removeClass(aClass)'>
  687. !
  688. basicRenameClass: aClass to: aString
  689. <inlineJS: '
  690. $globals[aString] = aClass;
  691. delete $globals[aClass.className];
  692. aClass.className = aString;
  693. '>
  694. !
  695. basicSwapClassNames: aClass with: anotherClass
  696. <inlineJS: '
  697. var tmp = aClass.className;
  698. aClass.className = anotherClass.className;
  699. anotherClass.className = tmp;
  700. '>
  701. !
  702. rawRenameClass: aClass to: aString
  703. <inlineJS: '
  704. $globals[aString] = aClass;
  705. '>
  706. ! !
  707. !ClassBuilder methodsFor: 'public'!
  708. setupClass: aClass
  709. self deprecatedAPI: 'Classes are now auto-inited.'
  710. ! !
  711. Object subclass: #ClassSorterNode
  712. instanceVariableNames: 'theClass level nodes'
  713. package: 'Kernel-Classes'!
  714. !ClassSorterNode commentStamp!
  715. I provide an algorithm for sorting classes alphabetically.
  716. See [Issue #143](https://lolg.it/amber/amber/issues/143).!
  717. !ClassSorterNode methodsFor: 'accessing'!
  718. getNodesFrom: aCollection
  719. | children others |
  720. children := #().
  721. others := #().
  722. aCollection do: [ :each |
  723. (each superclass = self theClass)
  724. ifTrue: [ children add: each ]
  725. ifFalse: [ others add: each ]].
  726. nodes:= children collect: [ :each |
  727. ClassSorterNode on: each classes: others level: self level + 1 ]
  728. !
  729. level
  730. ^ level
  731. !
  732. level: anInteger
  733. level := anInteger
  734. !
  735. nodes
  736. ^ nodes
  737. !
  738. theClass
  739. ^ theClass
  740. !
  741. theClass: aClass
  742. theClass := aClass
  743. ! !
  744. !ClassSorterNode methodsFor: 'visiting'!
  745. traverseClassesWith: aCollection
  746. "sort classes alphabetically Issue #143"
  747. aCollection add: self theClass.
  748. (self nodes sorted: [ :a :b | a theClass name <= b theClass name ]) do: [ :aNode |
  749. aNode traverseClassesWith: aCollection ].
  750. ! !
  751. !ClassSorterNode class methodsFor: 'instance creation'!
  752. on: aClass classes: aCollection level: anInteger
  753. ^ self new
  754. theClass: aClass;
  755. level: anInteger;
  756. getNodesFrom: aCollection;
  757. yourself
  758. ! !
  759. Object subclass: #TraitTransformation
  760. instanceVariableNames: 'trait aliases exclusions'
  761. package: 'Kernel-Classes'!
  762. !TraitTransformation commentStamp!
  763. I am a single step in trait composition.
  764. I represent one trait including its aliases and exclusions.!
  765. !TraitTransformation methodsFor: 'accessing'!
  766. addAliases: anArrayOfAssociations
  767. anArrayOfAssociations do: [ :each |
  768. | key |
  769. key := each key.
  770. aliases at: key
  771. ifPresent: [ self error: 'Cannot use same alias name twice.' ]
  772. ifAbsent: [ aliases at: key put: each value ] ].
  773. ^ anArrayOfAssociations
  774. !
  775. addExclusions: anArray
  776. exclusions addAll: anArray.
  777. ^ anArray
  778. !
  779. aliases
  780. ^ aliases
  781. !
  782. definition
  783. ^ String streamContents: [ :str |
  784. str print: self trait.
  785. self aliases ifNotEmpty: [ :al |
  786. str write: ' @ {'.
  787. al associations
  788. do: [ :each | str printSymbol: each key; write: ' -> '; printSymbol: each value ]
  789. separatedBy: [ str write: '. ' ].
  790. str write: '}' ].
  791. self exclusions ifNotEmpty: [ :ex |
  792. str write: ' - #('.
  793. ex asArray sorted
  794. do: [ :each | str write: each symbolPrintString allButFirst ]
  795. separatedBy: [ str space ].
  796. str write: ')' ] ]
  797. !
  798. exclusions
  799. ^ exclusions
  800. !
  801. trait
  802. ^ trait
  803. !
  804. trait: anObject
  805. trait := anObject
  806. ! !
  807. !TraitTransformation methodsFor: 'composition'!
  808. - anArray
  809. ^ self copy addExclusions: anArray; yourself
  810. !
  811. @ anArrayOfAssociations
  812. ^ self copy addAliases: anArrayOfAssociations; yourself
  813. ! !
  814. !TraitTransformation methodsFor: 'converting'!
  815. asJSON
  816. ^ #{
  817. 'trait' -> self trait.
  818. 'aliases' -> self aliases.
  819. 'exclusions' -> self exclusions asArray sorted }
  820. !
  821. asJavascript
  822. ^ String streamContents: [ :str | str write: {
  823. '{trait: ' . self trait asJavascript.
  824. self aliases ifNotEmpty: [ :al |
  825. {', aliases: '. al asJSONString} ].
  826. self exclusions ifNotEmpty: [ :ex |
  827. {', exclusions: '. ex asArray sorted asJavascript} ].
  828. '}' } ]
  829. !
  830. asTraitComposition
  831. ^ { self }
  832. !
  833. asTraitTransformation
  834. ^ self
  835. ! !
  836. !TraitTransformation methodsFor: 'copying'!
  837. postCopy
  838. aliases := aliases copy.
  839. exclusions := exclusions copy
  840. ! !
  841. !TraitTransformation methodsFor: 'initialization'!
  842. initialize
  843. super initialize.
  844. aliases := #{}.
  845. exclusions := Set new.
  846. trait := nil
  847. ! !
  848. !TraitTransformation class methodsFor: 'instance creation'!
  849. fromJSON: aJSObject
  850. ^ super new
  851. trait: (aJSObject at: #trait);
  852. addAliases: (Smalltalk readJSObject: (aJSObject at: #aliases ifAbsent: [#{}])) associations;
  853. addExclusions: (aJSObject at: #exclusions ifAbsent: [#()]);
  854. yourself
  855. !
  856. on: aTrait
  857. ^ super new trait: aTrait; yourself
  858. ! !
  859. !Array methodsFor: '*Kernel-Classes'!
  860. asTraitComposition
  861. "not implemented yet, noop atm"
  862. ^ self collect: [ :each | each asTraitTransformation ]
  863. ! !
  864. !UndefinedObject methodsFor: '*Kernel-Classes'!
  865. subclass: aString
  866. "Kept for file-in compatibility."
  867. ^ self subclass: aString instanceVariableNames: '' package: nil
  868. !
  869. subclass: aString instanceVariableNames: anotherString
  870. "Kept for file-in compatibility."
  871. ^ self subclass: aString instanceVariableNames: anotherString package: nil
  872. !
  873. subclass: aString instanceVariableNames: aString2 category: aString3
  874. "Kept for file-in compatibility."
  875. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  876. !
  877. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  878. "Kept for file-in compatibility. ignores class variables and pools."
  879. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  880. !
  881. subclass: aString instanceVariableNames: aString2 package: aString3
  882. ^ ClassBuilder new
  883. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  884. !
  885. subclass: aString uses: aTraitCompositionDescription
  886. "Kept for file-in compatibility."
  887. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: '' package: nil
  888. !
  889. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: anotherString
  890. "Kept for file-in compatibility."
  891. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: anotherString package: nil
  892. !
  893. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 category: aString3
  894. "Kept for file-in compatibility."
  895. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  896. !
  897. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  898. "Kept for file-in compatibility. ignores class variables and pools."
  899. ^ self subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  900. !
  901. subclass: aString uses: aTraitCompositionDescription instanceVariableNames: aString2 package: aString3
  902. | cls |
  903. cls := self subclass: aString instanceVariableNames: aString2 package: aString3.
  904. cls setTraitComposition: aTraitCompositionDescription asTraitComposition.
  905. ^ cls
  906. ! !