Kernel-Classes.st 17 KB

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