Kernel-Classes.st 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. self setupClass: aClass.
  431. SystemAnnouncer current
  432. announce: (ClassDefinitionChanged new
  433. theClass: aClass;
  434. yourself)
  435. !
  436. superclass: aClass subclass: className
  437. ^ self superclass: aClass subclass: className instanceVariableNames: '' package: nil
  438. !
  439. superclass: aClass subclass: className instanceVariableNames: ivarNames package: packageName
  440. | newClass |
  441. newClass := self addSubclassOf: aClass
  442. named: className instanceVariableNames: (self instanceVariableNamesFor: ivarNames)
  443. package: (packageName ifNil: [ 'unclassified' ]).
  444. self setupClass: newClass.
  445. SystemAnnouncer current
  446. announce: (ClassAdded new
  447. theClass: newClass;
  448. yourself).
  449. ^ newClass
  450. ! !
  451. !ClassBuilder methodsFor: 'class migration'!
  452. migrateClass: aClass superclass: anotherClass
  453. ^ self
  454. migrateClassNamed: aClass name
  455. superclass: anotherClass
  456. instanceVariableNames: aClass instanceVariableNames
  457. package: aClass package name
  458. !
  459. migrateClassNamed: className superclass: aClass instanceVariableNames: aCollection package: packageName
  460. | oldClass newClass tmp |
  461. tmp := 'new*', className.
  462. oldClass := Smalltalk globals at: className.
  463. newClass := self
  464. addSubclassOf: aClass
  465. named: tmp
  466. instanceVariableNames: aCollection
  467. package: packageName.
  468. self basicSwapClassNames: oldClass with: newClass.
  469. [ self copyClass: oldClass to: newClass ]
  470. on: Error
  471. do: [ :exception |
  472. self
  473. basicSwapClassNames: oldClass with: newClass;
  474. basicRemoveClass: newClass.
  475. exception resignal ].
  476. self
  477. rawRenameClass: oldClass to: tmp;
  478. rawRenameClass: newClass to: className.
  479. oldClass subclasses
  480. do: [ :each | self migrateClass: each superclass: newClass ].
  481. self basicRemoveClass: oldClass.
  482. SystemAnnouncer current announce: (ClassMigrated new
  483. theClass: newClass;
  484. oldClass: oldClass;
  485. yourself).
  486. ^ newClass
  487. !
  488. renameClass: aClass to: className
  489. self basicRenameClass: aClass to: className.
  490. "Recompile the class to fix potential issues with super sends"
  491. aClass recompile.
  492. SystemAnnouncer current
  493. announce: (ClassRenamed new
  494. theClass: aClass;
  495. yourself)
  496. ! !
  497. !ClassBuilder methodsFor: 'copying'!
  498. copyClass: aClass named: className
  499. | newClass |
  500. newClass := self
  501. addSubclassOf: aClass superclass
  502. named: className
  503. instanceVariableNames: aClass instanceVariableNames
  504. package: aClass package name.
  505. self copyClass: aClass to: newClass.
  506. SystemAnnouncer current
  507. announce: (ClassAdded new
  508. theClass: newClass;
  509. yourself).
  510. ^ newClass
  511. !
  512. copyClass: aClass to: anotherClass
  513. anotherClass comment: aClass comment.
  514. aClass methodDictionary valuesDo: [ :each |
  515. Compiler new install: each source forClass: anotherClass protocol: each protocol ].
  516. self basicClass: anotherClass class instanceVariables: aClass class instanceVariableNames.
  517. aClass class methodDictionary valuesDo: [ :each |
  518. Compiler new install: each source forClass: anotherClass class protocol: each protocol ].
  519. self setupClass: anotherClass
  520. ! !
  521. !ClassBuilder methodsFor: 'method definition'!
  522. installMethod: aCompiledMethod forClass: aBehavior protocol: aString
  523. aCompiledMethod protocol: aString.
  524. aBehavior addCompiledMethod: aCompiledMethod.
  525. ^ aCompiledMethod
  526. ! !
  527. !ClassBuilder methodsFor: 'private'!
  528. basicAddSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  529. <
  530. return $core.addClass(aString, aClass, aCollection, packageName);
  531. >
  532. !
  533. basicClass: aClass instanceVariableNames: aString
  534. self basicClass: aClass instanceVariables: (self instanceVariableNamesFor: aString)
  535. !
  536. basicClass: aClass instanceVariables: aCollection
  537. aClass isMetaclass ifFalse: [ self error: aClass name, ' is not a metaclass' ].
  538. aClass basicAt: 'iVarNames' put: aCollection
  539. !
  540. basicRemoveClass: aClass
  541. <$core.removeClass(aClass)>
  542. !
  543. basicRenameClass: aClass to: aString
  544. <
  545. $globals[aString] = aClass;
  546. delete $globals[aClass.className];
  547. aClass.className = aString;
  548. >
  549. !
  550. basicSwapClassNames: aClass with: anotherClass
  551. <
  552. var tmp = aClass.className;
  553. aClass.className = anotherClass.className;
  554. anotherClass.className = tmp;
  555. >
  556. !
  557. rawRenameClass: aClass to: aString
  558. <
  559. $globals[aString] = aClass;
  560. >
  561. ! !
  562. !ClassBuilder methodsFor: 'public'!
  563. setupClass: aClass
  564. <$core.init(aClass);>
  565. ! !
  566. Object subclass: #ClassSorterNode
  567. instanceVariableNames: 'theClass level nodes'
  568. package: 'Kernel-Classes'!
  569. !ClassSorterNode commentStamp!
  570. I provide an algorithm for sorting classes alphabetically.
  571. See [Issue #143](https://lolg.it/amber/amber/issues/143).!
  572. !ClassSorterNode methodsFor: 'accessing'!
  573. getNodesFrom: aCollection
  574. | children others |
  575. children := #().
  576. others := #().
  577. aCollection do: [ :each |
  578. (each superclass = self theClass)
  579. ifTrue: [ children add: each ]
  580. ifFalse: [ others add: each ]].
  581. nodes:= children collect: [ :each |
  582. ClassSorterNode on: each classes: others level: self level + 1 ]
  583. !
  584. level
  585. ^ level
  586. !
  587. level: anInteger
  588. level := anInteger
  589. !
  590. nodes
  591. ^ nodes
  592. !
  593. theClass
  594. ^ theClass
  595. !
  596. theClass: aClass
  597. theClass := aClass
  598. ! !
  599. !ClassSorterNode methodsFor: 'visiting'!
  600. traverseClassesWith: aCollection
  601. "sort classes alphabetically Issue #143"
  602. aCollection add: self theClass.
  603. (self nodes sorted: [ :a :b | a theClass name <= b theClass name ]) do: [ :aNode |
  604. aNode traverseClassesWith: aCollection ].
  605. ! !
  606. !ClassSorterNode class methodsFor: 'instance creation'!
  607. on: aClass classes: aCollection level: anInteger
  608. ^ self new
  609. theClass: aClass;
  610. level: anInteger;
  611. getNodesFrom: aCollection;
  612. yourself
  613. ! !