1
0

Kernel-Classes.st 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. Smalltalk current createPackage: 'Kernel-Classes'!
  2. Object subclass: #Behavior
  3. instanceVariableNames: ''
  4. package: 'Kernel-Classes'!
  5. !Behavior commentStamp!
  6. Behavior is the superclass of all class objects.
  7. It defines the protocol for creating instances of a class with `#basicNew` and `#new` (see `boot.js` for class constructors details).
  8. 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. Behavior 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. protocolsDo: aBlock
  112. "Execute aBlock for each method category with
  113. its collection of methods in the sort order of category name."
  114. | methodsByCategory |
  115. methodsByCategory := HashedCollection new.
  116. self methodDictionary values do: [:m |
  117. (methodsByCategory at: m category ifAbsentPut: [Array new])
  118. add: m].
  119. self protocols do: [:category |
  120. aBlock value: category value: (methodsByCategory at: category)]
  121. !
  122. prototype
  123. <return self.fn.prototype>
  124. !
  125. selectors
  126. ^ self methodDictionary keys
  127. !
  128. subclasses
  129. <return smalltalk.subclasses(self)>
  130. !
  131. superclass
  132. <return self.superclass || nil>
  133. !
  134. theMetaClass
  135. ^ self class
  136. !
  137. theNonMetaClass
  138. ^ self
  139. !
  140. withAllSubclasses
  141. ^(Array with: self) addAll: self allSubclasses; yourself
  142. ! !
  143. !Behavior methodsFor: 'compiling'!
  144. addCompiledMethod: aMethod
  145. | oldMethod announcement |
  146. oldMethod := self methodDictionary
  147. at: aMethod selector
  148. ifAbsent: [ nil ].
  149. (self protocols includes: aMethod protocol)
  150. ifFalse: [ self organization addElement: aMethod protocol ].
  151. self basicAddCompiledMethod: aMethod.
  152. announcement := oldMethod
  153. ifNil: [
  154. MethodAdded new
  155. method: aMethod;
  156. yourself ]
  157. ifNotNil: [
  158. MethodModified new
  159. oldMethod: oldMethod;
  160. method: aMethod;
  161. yourself ].
  162. SystemAnnouncer current
  163. announce: announcement
  164. !
  165. compile: aString
  166. self compile: aString category: ''
  167. !
  168. compile: aString category: anotherString
  169. Compiler new
  170. install: aString
  171. forClass: self
  172. category: anotherString
  173. !
  174. removeCompiledMethod: aMethod
  175. self basicRemoveCompiledMethod: aMethod.
  176. self methods
  177. detect: [ :each | each protocol = aMethod protocol ]
  178. ifNone: [ self organization removeElement: aMethod protocol ].
  179. SystemAnnouncer current
  180. announce: (MethodRemoved new
  181. method: aMethod;
  182. yourself)
  183. ! !
  184. !Behavior methodsFor: 'enumerating'!
  185. allSubclassesDo: aBlock
  186. "Evaluate the argument, aBlock, for each of the receiver's subclasses."
  187. self subclasses do: [ :each |
  188. aBlock value: each.
  189. each allSubclassesDo: aBlock ].
  190. !
  191. protocolsDo: aBlock
  192. "Execute aBlock for each method category with
  193. its collection of methods in the sort order of category name."
  194. | methodsByCategory |
  195. methodsByCategory := HashedCollection new.
  196. self methodDictionary values do: [:m |
  197. (methodsByCategory at: m category ifAbsentPut: [Array new])
  198. add: m].
  199. self protocols do: [:category |
  200. aBlock value: category value: (methodsByCategory at: category)]
  201. ! !
  202. !Behavior methodsFor: 'instance creation'!
  203. basicNew
  204. <return new self.fn()>
  205. !
  206. new
  207. ^self basicNew initialize
  208. ! !
  209. !Behavior methodsFor: 'private'!
  210. basicAddCompiledMethod: aMethod
  211. <smalltalk.addMethod(aMethod, self)>
  212. !
  213. basicRemoveCompiledMethod: aMethod
  214. <
  215. smalltalk.removeMethod(aMethod)
  216. smalltalk.init(self);
  217. >
  218. ! !
  219. !Behavior methodsFor: 'testing'!
  220. canUnderstand: aSelector
  221. ^(self methodDictionary keys includes: aSelector asString) or: [
  222. self superclass notNil and: [self superclass canUnderstand: aSelector]]
  223. !
  224. includesSelector: aString
  225. ^ self methodDictionary includesKey: aString
  226. !
  227. inheritsFrom: aClass
  228. ^aClass allSubclasses includes: self
  229. ! !
  230. Behavior subclass: #Class
  231. instanceVariableNames: ''
  232. package: 'Kernel-Classes'!
  233. !Class commentStamp!
  234. Class is __the__ class object.
  235. Instances are the classes of the system.
  236. Class creation is done throught a `ClassBuilder`!
  237. !Class methodsFor: 'accessing'!
  238. category
  239. ^self package ifNil: ['Unclassified'] ifNotNil: [self package name]
  240. !
  241. definition
  242. ^ String streamContents: [ :stream |
  243. stream
  244. nextPutAll: self superclass asString;
  245. nextPutAll: ' subclass: #';
  246. nextPutAll: self name;
  247. nextPutAll: String lf, String tab;
  248. nextPutAll: 'instanceVariableNames: '''.
  249. self instanceVariableNames
  250. do: [ :each | stream nextPutAll: each ]
  251. separatedBy: [ stream nextPutAll: ' ' ].
  252. stream
  253. nextPutAll: '''', String lf, String tab;
  254. nextPutAll: 'package: ''';
  255. nextPutAll: self category;
  256. nextPutAll: '''' ]
  257. !
  258. package
  259. <return self.pkg>
  260. !
  261. package: aPackage
  262. <self.pkg = aPackage>
  263. !
  264. rename: aString
  265. ClassBuilder new renameClass: self to: aString
  266. ! !
  267. !Class methodsFor: 'class creation'!
  268. subclass: aString instanceVariableNames: anotherString
  269. "Kept for compatibility."
  270. ^self subclass: aString instanceVariableNames: anotherString package: nil
  271. !
  272. subclass: aString instanceVariableNames: aString2 category: aString3
  273. "Kept for compatibility."
  274. self deprecatedAPI.
  275. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  276. !
  277. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  278. "Just ignore class variables and pools. Added for compatibility."
  279. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  280. !
  281. subclass: aString instanceVariableNames: aString2 package: aString3
  282. ^ClassBuilder new
  283. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  284. ! !
  285. !Class methodsFor: 'converting'!
  286. asJavascript
  287. ^ 'smalltalk.', self name
  288. ! !
  289. !Class methodsFor: 'printing'!
  290. printOn: aStream
  291. aStream nextPutAll: self name
  292. ! !
  293. !Class methodsFor: 'testing'!
  294. isClass
  295. ^true
  296. ! !
  297. Behavior subclass: #Metaclass
  298. instanceVariableNames: ''
  299. package: 'Kernel-Classes'!
  300. !Metaclass commentStamp!
  301. Metaclass is the root of the class hierarchy.
  302. Metaclass instances are metaclasses, one for each real class.
  303. Metaclass instances have a single instance, which they hold onto, which is the class that they are the metaclass of.!
  304. !Metaclass methodsFor: 'accessing'!
  305. definition
  306. ^ String streamContents: [ :stream |
  307. stream
  308. nextPutAll: self asString;
  309. nextPutAll: ' instanceVariableNames: '''.
  310. self instanceVariableNames
  311. do: [ :each | stream nextPutAll: each ]
  312. separatedBy: [ stream nextPutAll: ' ' ].
  313. stream nextPutAll: '''' ]
  314. !
  315. instanceClass
  316. <return self.instanceClass>
  317. !
  318. instanceVariableNames: aCollection
  319. ClassBuilder new
  320. class: self instanceVariableNames: aCollection
  321. !
  322. theMetaClass
  323. ^ self
  324. !
  325. theNonMetaClass
  326. ^ self instanceClass
  327. ! !
  328. !Metaclass methodsFor: 'converting'!
  329. asJavascript
  330. ^ 'smalltalk.', self instanceClass name, '.klass'
  331. ! !
  332. !Metaclass methodsFor: 'printing'!
  333. printOn: aStream
  334. aStream
  335. nextPutAll: self instanceClass name;
  336. nextPutAll: ' class'
  337. ! !
  338. !Metaclass methodsFor: 'testing'!
  339. isMetaclass
  340. ^true
  341. ! !
  342. Object subclass: #ClassBuilder
  343. instanceVariableNames: ''
  344. package: 'Kernel-Classes'!
  345. !ClassBuilder commentStamp!
  346. ClassBuilder is responsible for compiling new classes or modifying existing classes in the system.
  347. Rather than using ClassBuilder directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  348. !ClassBuilder methodsFor: 'accessing'!
  349. instanceVariableNamesFor: aString
  350. ^(aString tokenize: ' ') reject: [ :each | each isEmpty ]
  351. ! !
  352. !ClassBuilder methodsFor: 'class definition'!
  353. addSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  354. | theClass |
  355. theClass := Smalltalk current at: aString.
  356. theClass ifNotNil: [
  357. theClass superclass == aClass ifFalse: [
  358. ^ self
  359. migrateClassNamed: aString
  360. superclass: aClass
  361. instanceVariableNames: aCollection
  362. package: packageName ] ].
  363. ^ self
  364. basicAddSubclassOf: aClass
  365. named: aString
  366. instanceVariableNames: aCollection
  367. package: packageName
  368. !
  369. class: aClass instanceVariableNames: aString
  370. self basicClass: aClass instanceVariableNames: aString.
  371. self setupClass: aClass.
  372. SystemAnnouncer current
  373. announce: (ClassDefinitionChanged new
  374. theClass: aClass;
  375. yourself)
  376. !
  377. superclass: aClass subclass: aString
  378. ^self superclass: aClass subclass: aString instanceVariableNames: '' package: nil
  379. !
  380. superclass: aClass subclass: aString instanceVariableNames: aString2 package: aString3
  381. | newClass |
  382. newClass := self addSubclassOf: aClass
  383. named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2)
  384. package: (aString3 ifNil: ['unclassified']).
  385. self setupClass: newClass.
  386. SystemAnnouncer current
  387. announce: (ClassAdded new
  388. theClass: newClass;
  389. yourself).
  390. ^newClass
  391. ! !
  392. !ClassBuilder methodsFor: 'class migration'!
  393. migrateClass: aClass superclass: anotherClass
  394. console log: aClass name.
  395. self
  396. migrateClassNamed: aClass name
  397. superclass: anotherClass
  398. instanceVariableNames: aClass instanceVariableNames
  399. package: aClass package name
  400. !
  401. migrateClassNamed: aString superclass: aClass instanceVariableNames: aCollection package: packageName
  402. | oldClass newClass tmp |
  403. tmp := 'new*', aString.
  404. oldClass := Smalltalk current at: aString.
  405. newClass := self
  406. addSubclassOf: aClass
  407. named: tmp
  408. instanceVariableNames: aCollection
  409. package: packageName.
  410. self basicSwapClassNames: oldClass with: newClass.
  411. [ self copyClass: oldClass to: newClass ]
  412. on: Error
  413. do: [ :exception |
  414. self
  415. basicSwapClassNames: oldClass with: newClass;
  416. basicRemoveClass: newClass.
  417. exception signal ].
  418. self
  419. rawRenameClass: oldClass to: tmp;
  420. rawRenameClass: newClass to: aString.
  421. oldClass subclasses do: [ :each |
  422. self migrateClass: each superclass: newClass ].
  423. self basicRemoveClass: oldClass.
  424. ^newClass
  425. !
  426. renameClass: aClass to: aString
  427. self basicRenameClass: aClass to: aString.
  428. SystemAnnouncer current
  429. announce: (ClassRenamed new
  430. theClass: aClass;
  431. yourself)
  432. ! !
  433. !ClassBuilder methodsFor: 'copying'!
  434. copyClass: aClass named: aString
  435. | newClass |
  436. newClass := self
  437. addSubclassOf: aClass superclass
  438. named: aString
  439. instanceVariableNames: aClass instanceVariableNames
  440. package: aClass package name.
  441. self copyClass: aClass to: newClass.
  442. ^newClass
  443. !
  444. copyClass: aClass to: anotherClass
  445. anotherClass comment: aClass comment.
  446. aClass methodDictionary values do: [ :each |
  447. Compiler new install: each source forClass: anotherClass category: each category ].
  448. self basicClass: anotherClass class instanceVariables: aClass class instanceVariableNames.
  449. aClass class methodDictionary values do: [ :each |
  450. Compiler new install: each source forClass: anotherClass class category: each category ].
  451. self setupClass: anotherClass
  452. ! !
  453. !ClassBuilder methodsFor: 'method definition'!
  454. installMethod: aCompiledMethod forClass: aBehavior category: aString
  455. aCompiledMethod category: aString.
  456. aBehavior addCompiledMethod: aCompiledMethod.
  457. self setupClass: aBehavior.
  458. ^aCompiledMethod
  459. ! !
  460. !ClassBuilder methodsFor: 'private'!
  461. basicAddSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  462. <
  463. smalltalk.addClass(aString, aClass, aCollection, packageName);
  464. return smalltalk[aString]
  465. >
  466. !
  467. basicClass: aClass instanceVariableNames: aString
  468. self basicClass: aClass instanceVariables: (self instanceVariableNamesFor: aString)
  469. !
  470. basicClass: aClass instanceVariables: aCollection
  471. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  472. aClass basicAt: 'iVarNames' put: aCollection
  473. !
  474. basicRemoveClass: aClass
  475. <smalltalk.removeClass(aClass)>
  476. !
  477. basicRenameClass: aClass to: aString
  478. <
  479. smalltalk[aString] = aClass;
  480. delete smalltalk[aClass.className];
  481. aClass.className = aString;
  482. >
  483. !
  484. basicSwapClassNames: aClass with: anotherClass
  485. <
  486. var tmp = aClass.className;
  487. aClass.className = anotherClass.className;
  488. anotherClass.className = tmp;
  489. >
  490. !
  491. rawRenameClass: aClass to: aString
  492. <
  493. smalltalk[aString] = aClass;
  494. >
  495. ! !
  496. !ClassBuilder methodsFor: 'public'!
  497. setupClass: aClass
  498. <smalltalk.init(aClass);>
  499. ! !
  500. Object subclass: #ClassCategoryReader
  501. instanceVariableNames: 'class category'
  502. package: 'Kernel-Classes'!
  503. !ClassCategoryReader commentStamp!
  504. ClassCategoryReader represents a mechanism for retrieving class descriptions stored on a file.!
  505. !ClassCategoryReader methodsFor: 'accessing'!
  506. class: aClass category: aString
  507. class := aClass.
  508. category := aString
  509. ! !
  510. !ClassCategoryReader methodsFor: 'fileIn'!
  511. scanFrom: aChunkParser
  512. | chunk |
  513. [chunk := aChunkParser nextChunk.
  514. chunk isEmpty] whileFalse: [
  515. self compileMethod: chunk].
  516. ClassBuilder new setupClass: class
  517. ! !
  518. !ClassCategoryReader methodsFor: 'initialization'!
  519. initialize
  520. super initialize.
  521. ! !
  522. !ClassCategoryReader methodsFor: 'private'!
  523. compileMethod: aString
  524. Compiler new install: aString forClass: class category: category
  525. ! !
  526. Object subclass: #ClassCommentReader
  527. instanceVariableNames: 'class'
  528. package: 'Kernel-Classes'!
  529. !ClassCommentReader commentStamp!
  530. ClassCommentReader represents a mechanism for retrieving class comments stored on a file.
  531. See `ClassCategoryReader` too.!
  532. !ClassCommentReader methodsFor: 'accessing'!
  533. class: aClass
  534. class := aClass
  535. ! !
  536. !ClassCommentReader methodsFor: 'fileIn'!
  537. scanFrom: aChunkParser
  538. | chunk |
  539. chunk := aChunkParser nextChunk.
  540. chunk isEmpty ifFalse: [
  541. self setComment: chunk].
  542. ! !
  543. !ClassCommentReader methodsFor: 'initialization'!
  544. initialize
  545. super initialize.
  546. ! !
  547. !ClassCommentReader methodsFor: 'private'!
  548. setComment: aString
  549. class comment: aString
  550. ! !
  551. Object subclass: #ClassSorterNode
  552. instanceVariableNames: 'theClass level nodes'
  553. package: 'Kernel-Classes'!
  554. !ClassSorterNode methodsFor: 'accessing'!
  555. getNodesFrom: aCollection
  556. | children others |
  557. children := #().
  558. others := #().
  559. aCollection do: [:each |
  560. (each superclass = self theClass)
  561. ifTrue: [children add: each]
  562. ifFalse: [others add: each]].
  563. nodes:= children collect: [:each |
  564. ClassSorterNode on: each classes: others level: self level + 1]
  565. !
  566. level
  567. ^level
  568. !
  569. level: anInteger
  570. level := anInteger
  571. !
  572. nodes
  573. ^nodes
  574. !
  575. theClass
  576. ^theClass
  577. !
  578. theClass: aClass
  579. theClass := aClass
  580. ! !
  581. !ClassSorterNode methodsFor: 'visiting'!
  582. traverseClassesWith: aCollection
  583. "sort classes alphabetically Issue #143"
  584. aCollection add: self theClass.
  585. (self nodes sorted: [:a :b | a theClass name <= b theClass name ]) do: [:aNode |
  586. aNode traverseClassesWith: aCollection ].
  587. ! !
  588. !ClassSorterNode class methodsFor: 'instance creation'!
  589. on: aClass classes: aCollection level: anInteger
  590. ^self new
  591. theClass: aClass;
  592. level: anInteger;
  593. getNodesFrom: aCollection;
  594. yourself
  595. ! !