Kernel-Classes.st 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. Smalltalk createPackage: 'Kernel-Classes'!
  2. Object subclass: #Behavior
  3. instanceVariableNames: ''
  4. package: 'Kernel-Classes'!
  5. !Behavior commentStamp!
  6. I am the superclass of all class objects.
  7. I define the protocol for creating instances of a class with `#basicNew` and `#new` (see `boot.js` for class constructors details).
  8. My instances know about the subclass/superclass relationships between classes, contain the description that instances are created from,
  9. and hold the method dictionary that's associated with each class.
  10. I also provides methods for compiling methods, examining the method dictionary, and iterating over the class hierarchy.!
  11. !Behavior methodsFor: 'accessing'!
  12. >> aString
  13. ^ self methodAt: aString
  14. !
  15. allInstanceVariableNames
  16. | result |
  17. result := self instanceVariableNames copy.
  18. self superclass ifNotNil: [
  19. result addAll: self superclass allInstanceVariableNames ].
  20. ^ result
  21. !
  22. allSelectors
  23. ^ self allSuperclasses
  24. inject: self selectors
  25. into: [ :acc :each | acc addAll: each selectors; yourself ]
  26. !
  27. allSubclasses
  28. "Answer an collection of the receiver's and the receiver's descendent's subclasses. "
  29. ^ Array streamContents: [ :str | self allSubclassesDo: [ :each | str nextPut: each ] ]
  30. !
  31. allSuperclasses
  32. self superclass ifNil: [ ^ #() ].
  33. ^ (OrderedCollection with: self superclass)
  34. addAll: self superclass allSuperclasses;
  35. yourself
  36. !
  37. comment
  38. ^ (self basicAt: 'comment') ifNil: [ '' ]
  39. !
  40. comment: aString
  41. self basicAt: 'comment' put: aString.
  42. SystemAnnouncer current
  43. announce: (ClassCommentChanged new
  44. theClass: self;
  45. yourself)
  46. !
  47. definition
  48. ^ ''
  49. !
  50. instanceVariableNames
  51. <return self.iVarNames>
  52. !
  53. javascriptConstructor
  54. "Answer the JS constructor used to instantiate. See boot.js"
  55. <return self.fn>
  56. !
  57. javascriptConstructor: aJavaScriptFunction
  58. "Set the JS constructor used to instantiate.
  59. See the JS counter-part in boot.js `$core.setClassConstructor'"
  60. <$core.setClassConstructor(self, aJavaScriptFunction);>
  61. !
  62. lookupSelector: selector
  63. "Look up the given selector in my methodDictionary.
  64. Return the corresponding method if found.
  65. Otherwise chase the superclass chain and try again.
  66. Return nil if no method is found."
  67. | lookupClass |
  68. lookupClass := self.
  69. [ lookupClass = nil ] whileFalse: [
  70. (lookupClass includesSelector: selector)
  71. ifTrue: [ ^ lookupClass methodAt: selector ].
  72. lookupClass := lookupClass superclass ].
  73. ^ nil
  74. !
  75. methodAt: aString
  76. ^ self methodDictionary at: aString
  77. !
  78. methodDictionary
  79. <var dict = $globals.HashedCollection._new();
  80. var methods = self.methods;
  81. Object.keys(methods).forEach(function(i) {
  82. if(methods[i].selector) {
  83. dict._at_put_(methods[i].selector, methods[i]);
  84. }
  85. });
  86. return dict>
  87. !
  88. methodTemplate
  89. ^ String streamContents: [ :stream |
  90. stream
  91. nextPutAll: 'messageSelectorAndArgumentNames';
  92. nextPutAll: String lf, String tab;
  93. nextPutAll: '"comment stating purpose of message"';
  94. nextPutAll: String lf, String lf, String tab;
  95. nextPutAll: '| temporary variable names |';
  96. nextPutAll: String lf, String tab;
  97. nextPutAll: 'statements' ]
  98. !
  99. methods
  100. ^ self methodDictionary values
  101. !
  102. methodsInProtocol: aString
  103. ^ self methods select: [ :each | each protocol = aString ]
  104. !
  105. name
  106. <return self.className || nil>
  107. !
  108. organization
  109. ^ self basicAt: 'organization'
  110. !
  111. ownMethods
  112. "Answer the methods of the receiver that are not package extensions"
  113. ^ (self ownProtocols
  114. inject: OrderedCollection new
  115. into: [ :acc :each | acc, (self methodsInProtocol: each) ])
  116. sorted: [ :a :b | a selector <= b selector ]
  117. !
  118. ownProtocols
  119. "Answer the protocols of the receiver that are not package extensions"
  120. ^ self protocols reject: [ :each |
  121. each match: '^\*' ]
  122. !
  123. packageOfProtocol: aString
  124. "Answer the package the method of receiver belongs to:
  125. - if it is an extension method, answer the corresponding package
  126. - else answer the receiver's package"
  127. (aString beginsWith: '*') ifFalse: [
  128. ^ self package ].
  129. ^ Package
  130. named: aString allButFirst
  131. ifAbsent: [ nil ]
  132. !
  133. protocols
  134. ^ self organization elements sorted
  135. !
  136. prototype
  137. <return self.fn.prototype>
  138. !
  139. removeProtocolIfEmpty: aString
  140. self methods
  141. detect: [ :each | each protocol = aString ]
  142. ifNone: [ self organization removeElement: aString ]
  143. !
  144. selectors
  145. ^ self methodDictionary keys
  146. !
  147. subclasses
  148. self subclassResponsibility
  149. !
  150. superclass
  151. <return self.superclass>
  152. !
  153. theMetaClass
  154. self subclassResponsibility
  155. !
  156. theNonMetaClass
  157. self subclassResponsibility
  158. !
  159. withAllSubclasses
  160. ^ (Array with: self) addAll: self allSubclasses; yourself
  161. ! !
  162. !Behavior methodsFor: 'compiling'!
  163. addCompiledMethod: aMethod
  164. | oldMethod announcement |
  165. oldMethod := self methodDictionary
  166. at: aMethod selector
  167. ifAbsent: [ nil ].
  168. (self protocols includes: aMethod protocol)
  169. ifFalse: [ self organization addElement: aMethod protocol ].
  170. self basicAddCompiledMethod: aMethod.
  171. oldMethod ifNotNil: [
  172. self removeProtocolIfEmpty: oldMethod protocol ].
  173. announcement := oldMethod
  174. ifNil: [
  175. MethodAdded new
  176. method: aMethod;
  177. yourself ]
  178. ifNotNil: [
  179. MethodModified new
  180. oldMethod: oldMethod;
  181. method: aMethod;
  182. yourself ].
  183. SystemAnnouncer current
  184. announce: announcement
  185. !
  186. compile: aString protocol: anotherString
  187. ^ Compiler new
  188. install: aString
  189. forClass: self
  190. protocol: anotherString
  191. !
  192. recompile
  193. ^ Compiler new recompile: self
  194. !
  195. removeCompiledMethod: aMethod
  196. self basicRemoveCompiledMethod: aMethod.
  197. self removeProtocolIfEmpty: aMethod protocol.
  198. SystemAnnouncer current
  199. announce: (MethodRemoved new
  200. method: aMethod;
  201. yourself)
  202. ! !
  203. !Behavior methodsFor: 'enumerating'!
  204. allSubclassesDo: aBlock
  205. "Evaluate the argument, aBlock, for each of the receiver's subclasses."
  206. <$core.traverseClassTree(self, function(subclass) {
  207. if (subclass !!== self) aBlock._value_(subclass);
  208. })>
  209. !
  210. protocolsDo: aBlock
  211. "Execute aBlock for each method protocol with
  212. its collection of methods in the sort order of protocol name."
  213. | methodsByProtocol |
  214. methodsByProtocol := HashedCollection new.
  215. self methodDictionary valuesDo: [ :m |
  216. (methodsByProtocol at: m protocol ifAbsentPut: [ Array new ])
  217. add: m ].
  218. self protocols do: [ :protocol |
  219. aBlock value: protocol value: (methodsByProtocol at: protocol) ]
  220. ! !
  221. !Behavior methodsFor: 'instance creation'!
  222. basicNew
  223. <return new self.fn()>
  224. !
  225. new
  226. ^ self basicNew initialize
  227. ! !
  228. !Behavior methodsFor: 'private'!
  229. basicAddCompiledMethod: aMethod
  230. <$core.addMethod(aMethod, self)>
  231. !
  232. basicRemoveCompiledMethod: aMethod
  233. <$core.removeMethod(aMethod,self)>
  234. ! !
  235. !Behavior methodsFor: 'testing'!
  236. canUnderstand: aSelector
  237. ^ (self includesSelector: aSelector asString) or: [
  238. self superclass notNil and: [ self superclass canUnderstand: aSelector ]]
  239. !
  240. includesBehavior: aClass
  241. ^ self == aClass or: [
  242. self inheritsFrom: aClass ]
  243. !
  244. includesSelector: aString
  245. ^ self methodDictionary includesKey: aString
  246. !
  247. inheritsFrom: aClass
  248. self superclass ifNil: [ ^ false ].
  249. ^ aClass == self superclass or: [
  250. self superclass inheritsFrom: aClass ]
  251. !
  252. isBehavior
  253. ^ true
  254. ! !
  255. Behavior subclass: #Class
  256. instanceVariableNames: ''
  257. package: 'Kernel-Classes'!
  258. !Class commentStamp!
  259. I am __the__ class object.
  260. My instances are the classes of the system.
  261. Class creation is done throught a `ClassBuilder` instance.!
  262. !Class methodsFor: 'accessing'!
  263. category
  264. ^ self package ifNil: [ 'Unclassified' ] ifNotNil: [ self package name ]
  265. !
  266. classTag
  267. "Returns a tag or general category for this class.
  268. Typically used to help tools do some reflection.
  269. Helios, for example, uses this to decide what icon the class should display."
  270. ^ 'class'
  271. !
  272. definition
  273. ^ String streamContents: [ :stream |
  274. stream
  275. nextPutAll: self superclass asString;
  276. nextPutAll: ' subclass: #';
  277. nextPutAll: self name;
  278. nextPutAll: String lf, String tab;
  279. nextPutAll: 'instanceVariableNames: '''.
  280. self instanceVariableNames
  281. do: [ :each | stream nextPutAll: each ]
  282. separatedBy: [ stream nextPutAll: ' ' ].
  283. stream
  284. nextPutAll: '''', String lf, String tab;
  285. nextPutAll: 'package: ''';
  286. nextPutAll: self category;
  287. nextPutAll: '''' ]
  288. !
  289. package
  290. ^ self basicAt: 'pkg'
  291. !
  292. package: aPackage
  293. | oldPackage |
  294. self package = aPackage ifTrue: [ ^ self ].
  295. oldPackage := self package.
  296. self basicAt: 'pkg' put: aPackage.
  297. oldPackage organization removeElement: self.
  298. aPackage organization addElement: self.
  299. SystemAnnouncer current announce: (ClassMoved new
  300. theClass: self;
  301. oldPackage: oldPackage;
  302. yourself)
  303. !
  304. rename: aString
  305. ClassBuilder new renameClass: self to: aString
  306. !
  307. subclasses
  308. <return self.subclasses._copy()>
  309. !
  310. theMetaClass
  311. ^ self class
  312. !
  313. theNonMetaClass
  314. ^ self
  315. ! !
  316. !Class methodsFor: 'browsing'!
  317. browse
  318. Finder findClass: self
  319. ! !
  320. !Class methodsFor: 'class creation'!
  321. subclass: aString instanceVariableNames: anotherString
  322. "Kept for file-in compatibility."
  323. ^ self subclass: aString instanceVariableNames: anotherString package: nil
  324. !
  325. subclass: aString instanceVariableNames: aString2 category: aString3
  326. "Kept for file-in compatibility."
  327. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  328. !
  329. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  330. "Kept for file-in compatibility. ignores class variables and pools."
  331. ^ self subclass: aString instanceVariableNames: aString2 package: aString3
  332. !
  333. subclass: aString instanceVariableNames: aString2 package: aString3
  334. ^ ClassBuilder new
  335. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  336. ! !
  337. !Class methodsFor: 'converting'!
  338. asJavascript
  339. ^ '$globals.', self name
  340. ! !
  341. !Class methodsFor: 'printing'!
  342. printOn: aStream
  343. aStream nextPutAll: self name
  344. ! !
  345. !Class methodsFor: 'testing'!
  346. isClass
  347. ^ true
  348. ! !
  349. Behavior subclass: #Metaclass
  350. instanceVariableNames: ''
  351. package: 'Kernel-Classes'!
  352. !Metaclass commentStamp!
  353. I am the root of the class hierarchy.
  354. 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.!
  355. !Metaclass methodsFor: 'accessing'!
  356. definition
  357. ^ String streamContents: [ :stream |
  358. stream
  359. nextPutAll: self asString;
  360. nextPutAll: ' instanceVariableNames: '''.
  361. self instanceVariableNames
  362. do: [ :each | stream nextPutAll: each ]
  363. separatedBy: [ stream nextPutAll: ' ' ].
  364. stream nextPutAll: '''' ]
  365. !
  366. instanceClass
  367. <return self.instanceClass>
  368. !
  369. instanceVariableNames: aCollection
  370. ClassBuilder new
  371. class: self instanceVariableNames: aCollection
  372. !
  373. package
  374. ^ self instanceClass package
  375. !
  376. subclasses
  377. <return $core.metaSubclasses(self)>
  378. !
  379. theMetaClass
  380. ^ self
  381. !
  382. theNonMetaClass
  383. ^ self instanceClass
  384. ! !
  385. !Metaclass methodsFor: 'converting'!
  386. asJavascript
  387. ^ '$globals.', self instanceClass name, '.klass'
  388. ! !
  389. !Metaclass methodsFor: 'printing'!
  390. printOn: aStream
  391. aStream
  392. nextPutAll: self instanceClass name;
  393. nextPutAll: ' class'
  394. ! !
  395. !Metaclass methodsFor: 'testing'!
  396. isMetaclass
  397. ^ true
  398. ! !
  399. Object subclass: #ClassBuilder
  400. instanceVariableNames: ''
  401. package: 'Kernel-Classes'!
  402. !ClassBuilder commentStamp!
  403. I am responsible for compiling new classes or modifying existing classes in the system.
  404. Rather than using me directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  405. !ClassBuilder methodsFor: 'accessing'!
  406. instanceVariableNamesFor: aString
  407. ^ (aString tokenize: ' ') reject: [ :each | each isEmpty ]
  408. ! !
  409. !ClassBuilder methodsFor: 'class definition'!
  410. addSubclassOf: aClass named: className instanceVariableNames: aCollection package: packageName
  411. | theClass thePackage |
  412. theClass := Smalltalk globals at: className.
  413. thePackage := Package named: packageName.
  414. theClass ifNotNil: [
  415. theClass package: thePackage.
  416. theClass superclass == aClass ifFalse: [
  417. ^ self
  418. migrateClassNamed: className
  419. superclass: aClass
  420. instanceVariableNames: aCollection
  421. package: packageName ] ].
  422. ^ self
  423. basicAddSubclassOf: aClass
  424. named: className
  425. instanceVariableNames: aCollection
  426. package: packageName
  427. !
  428. class: aClass instanceVariableNames: ivarNames
  429. self basicClass: aClass instanceVariableNames: ivarNames.
  430. SystemAnnouncer current
  431. announce: (ClassDefinitionChanged new
  432. theClass: aClass;
  433. yourself)
  434. !
  435. superclass: aClass subclass: className
  436. ^ self superclass: aClass subclass: className instanceVariableNames: '' package: nil
  437. !
  438. superclass: aClass subclass: className instanceVariableNames: ivarNames package: packageName
  439. | newClass |
  440. newClass := self addSubclassOf: aClass
  441. named: className instanceVariableNames: (self instanceVariableNamesFor: ivarNames)
  442. package: (packageName ifNil: [ 'unclassified' ]).
  443. SystemAnnouncer current
  444. announce: (ClassAdded new
  445. theClass: newClass;
  446. yourself).
  447. ^ newClass
  448. ! !
  449. !ClassBuilder methodsFor: 'class migration'!
  450. migrateClass: aClass superclass: anotherClass
  451. ^ self
  452. migrateClassNamed: aClass name
  453. superclass: anotherClass
  454. instanceVariableNames: aClass instanceVariableNames
  455. package: aClass package name
  456. !
  457. migrateClassNamed: className superclass: aClass instanceVariableNames: aCollection package: packageName
  458. | oldClass newClass tmp |
  459. tmp := 'new*', className.
  460. oldClass := Smalltalk globals at: className.
  461. newClass := self
  462. addSubclassOf: aClass
  463. named: tmp
  464. instanceVariableNames: aCollection
  465. package: packageName.
  466. self basicSwapClassNames: oldClass with: newClass.
  467. [ self copyClass: oldClass to: newClass ]
  468. on: Error
  469. do: [ :exception |
  470. self
  471. basicSwapClassNames: oldClass with: newClass;
  472. basicRemoveClass: newClass.
  473. exception resignal ].
  474. self
  475. rawRenameClass: oldClass to: tmp;
  476. rawRenameClass: newClass to: className.
  477. oldClass subclasses
  478. do: [ :each | self migrateClass: each superclass: newClass ].
  479. self basicRemoveClass: oldClass.
  480. SystemAnnouncer current announce: (ClassMigrated new
  481. theClass: newClass;
  482. oldClass: oldClass;
  483. yourself).
  484. ^ newClass
  485. !
  486. renameClass: aClass to: className
  487. self basicRenameClass: aClass to: className.
  488. "Recompile the class to fix potential issues with super sends"
  489. aClass recompile.
  490. SystemAnnouncer current
  491. announce: (ClassRenamed new
  492. theClass: aClass;
  493. yourself)
  494. ! !
  495. !ClassBuilder methodsFor: 'copying'!
  496. copyClass: aClass named: className
  497. | newClass |
  498. newClass := self
  499. addSubclassOf: aClass superclass
  500. named: className
  501. instanceVariableNames: aClass instanceVariableNames
  502. package: aClass package name.
  503. self copyClass: aClass to: newClass.
  504. SystemAnnouncer current
  505. announce: (ClassAdded new
  506. theClass: newClass;
  507. yourself).
  508. ^ newClass
  509. !
  510. copyClass: aClass to: anotherClass
  511. anotherClass comment: aClass comment.
  512. aClass methodDictionary valuesDo: [ :each |
  513. Compiler new install: each source forClass: anotherClass protocol: each protocol ].
  514. self basicClass: anotherClass class instanceVariables: aClass class instanceVariableNames.
  515. aClass class methodDictionary valuesDo: [ :each |
  516. Compiler new install: each source forClass: anotherClass class protocol: each protocol ]
  517. ! !
  518. !ClassBuilder methodsFor: 'method definition'!
  519. installMethod: aCompiledMethod forClass: aBehavior protocol: aString
  520. aCompiledMethod protocol: aString.
  521. aBehavior addCompiledMethod: aCompiledMethod.
  522. ^ aCompiledMethod
  523. ! !
  524. !ClassBuilder methodsFor: 'private'!
  525. basicAddSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  526. <
  527. return $core.addClass(aString, aClass, aCollection, packageName);
  528. >
  529. !
  530. basicClass: aClass instanceVariableNames: aString
  531. self basicClass: aClass instanceVariables: (self instanceVariableNamesFor: aString)
  532. !
  533. basicClass: aClass instanceVariables: aCollection
  534. aClass isMetaclass ifFalse: [ self error: aClass name, ' is not a metaclass' ].
  535. aClass basicAt: 'iVarNames' put: aCollection
  536. !
  537. basicRemoveClass: aClass
  538. <$core.removeClass(aClass)>
  539. !
  540. basicRenameClass: aClass to: aString
  541. <
  542. $globals[aString] = aClass;
  543. delete $globals[aClass.className];
  544. aClass.className = aString;
  545. >
  546. !
  547. basicSwapClassNames: aClass with: anotherClass
  548. <
  549. var tmp = aClass.className;
  550. aClass.className = anotherClass.className;
  551. anotherClass.className = tmp;
  552. >
  553. !
  554. rawRenameClass: aClass to: aString
  555. <
  556. $globals[aString] = aClass;
  557. >
  558. ! !
  559. !ClassBuilder methodsFor: 'public'!
  560. setupClass: aClass
  561. self deprecatedAPI: 'Classes are now auto-inited.'
  562. ! !
  563. Object subclass: #ClassSorterNode
  564. instanceVariableNames: 'theClass level nodes'
  565. package: 'Kernel-Classes'!
  566. !ClassSorterNode commentStamp!
  567. I provide an algorithm for sorting classes alphabetically.
  568. See [Issue #143](https://lolg.it/amber/amber/issues/143).!
  569. !ClassSorterNode methodsFor: 'accessing'!
  570. getNodesFrom: aCollection
  571. | children others |
  572. children := #().
  573. others := #().
  574. aCollection do: [ :each |
  575. (each superclass = self theClass)
  576. ifTrue: [ children add: each ]
  577. ifFalse: [ others add: each ]].
  578. nodes:= children collect: [ :each |
  579. ClassSorterNode on: each classes: others level: self level + 1 ]
  580. !
  581. level
  582. ^ level
  583. !
  584. level: anInteger
  585. level := anInteger
  586. !
  587. nodes
  588. ^ nodes
  589. !
  590. theClass
  591. ^ theClass
  592. !
  593. theClass: aClass
  594. theClass := aClass
  595. ! !
  596. !ClassSorterNode methodsFor: 'visiting'!
  597. traverseClassesWith: aCollection
  598. "sort classes alphabetically Issue #143"
  599. aCollection add: self theClass.
  600. (self nodes sorted: [ :a :b | a theClass name <= b theClass name ]) do: [ :aNode |
  601. aNode traverseClassesWith: aCollection ].
  602. ! !
  603. !ClassSorterNode class methodsFor: 'instance creation'!
  604. on: aClass classes: aCollection level: anInteger
  605. ^ self new
  606. theClass: aClass;
  607. level: anInteger;
  608. getNodesFrom: aCollection;
  609. yourself
  610. ! !