1
0

Kernel-Classes.st 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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. 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. includesSelector: aString
  214. ^ self methodDictionary includesKey: aString
  215. !
  216. inheritsFrom: aClass
  217. ^aClass allSubclasses includes: self
  218. !
  219. isBehavior
  220. ^ true
  221. ! !
  222. Behavior subclass: #Class
  223. instanceVariableNames: ''
  224. package: 'Kernel-Classes'!
  225. !Class commentStamp!
  226. Class is __the__ class object.
  227. Instances are the classes of the system.
  228. Class creation is done throught a `ClassBuilder`!
  229. !Class methodsFor: 'accessing'!
  230. category
  231. ^self package ifNil: ['Unclassified'] ifNotNil: [self package name]
  232. !
  233. definition
  234. ^ String streamContents: [ :stream |
  235. stream
  236. nextPutAll: self superclass asString;
  237. nextPutAll: ' subclass: #';
  238. nextPutAll: self name;
  239. nextPutAll: String lf, String tab;
  240. nextPutAll: 'instanceVariableNames: '''.
  241. self instanceVariableNames
  242. do: [ :each | stream nextPutAll: each ]
  243. separatedBy: [ stream nextPutAll: ' ' ].
  244. stream
  245. nextPutAll: '''', String lf, String tab;
  246. nextPutAll: 'package: ''';
  247. nextPutAll: self category;
  248. nextPutAll: '''' ]
  249. !
  250. package
  251. ^ self basicAt: 'pkg'
  252. !
  253. package: aPackage
  254. self basicAt: 'pkg' put: aPackage.
  255. SystemAnnouncer current announce: (ClassMoved new
  256. theClass: self;
  257. yourself)
  258. !
  259. rename: aString
  260. ClassBuilder new renameClass: self to: aString
  261. ! !
  262. !Class methodsFor: 'class creation'!
  263. subclass: aString instanceVariableNames: anotherString
  264. "Kept for compatibility."
  265. ^self subclass: aString instanceVariableNames: anotherString package: nil
  266. !
  267. subclass: aString instanceVariableNames: aString2 category: aString3
  268. "Kept for compatibility."
  269. self deprecatedAPI.
  270. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  271. !
  272. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  273. "Just ignore class variables and pools. Added for compatibility."
  274. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  275. !
  276. subclass: aString instanceVariableNames: aString2 package: aString3
  277. ^ClassBuilder new
  278. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  279. ! !
  280. !Class methodsFor: 'converting'!
  281. asJavascript
  282. ^ 'smalltalk.', self name
  283. ! !
  284. !Class methodsFor: 'printing'!
  285. printOn: aStream
  286. aStream nextPutAll: self name
  287. ! !
  288. !Class methodsFor: 'testing'!
  289. isClass
  290. ^true
  291. ! !
  292. Behavior subclass: #Metaclass
  293. instanceVariableNames: ''
  294. package: 'Kernel-Classes'!
  295. !Metaclass commentStamp!
  296. Metaclass is the root of the class hierarchy.
  297. Metaclass instances are metaclasses, one for each real class.
  298. Metaclass instances have a single instance, which they hold onto, which is the class that they are the metaclass of.!
  299. !Metaclass methodsFor: 'accessing'!
  300. definition
  301. ^ String streamContents: [ :stream |
  302. stream
  303. nextPutAll: self asString;
  304. nextPutAll: ' instanceVariableNames: '''.
  305. self instanceVariableNames
  306. do: [ :each | stream nextPutAll: each ]
  307. separatedBy: [ stream nextPutAll: ' ' ].
  308. stream nextPutAll: '''' ]
  309. !
  310. instanceClass
  311. <return self.instanceClass>
  312. !
  313. instanceVariableNames: aCollection
  314. ClassBuilder new
  315. class: self instanceVariableNames: aCollection
  316. !
  317. theMetaClass
  318. ^ self
  319. !
  320. theNonMetaClass
  321. ^ self instanceClass
  322. ! !
  323. !Metaclass methodsFor: 'converting'!
  324. asJavascript
  325. ^ 'smalltalk.', self instanceClass name, '.klass'
  326. ! !
  327. !Metaclass methodsFor: 'printing'!
  328. printOn: aStream
  329. aStream
  330. nextPutAll: self instanceClass name;
  331. nextPutAll: ' class'
  332. ! !
  333. !Metaclass methodsFor: 'testing'!
  334. isMetaclass
  335. ^true
  336. ! !
  337. Object subclass: #ClassBuilder
  338. instanceVariableNames: ''
  339. package: 'Kernel-Classes'!
  340. !ClassBuilder commentStamp!
  341. ClassBuilder is responsible for compiling new classes or modifying existing classes in the system.
  342. Rather than using ClassBuilder directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  343. !ClassBuilder methodsFor: 'accessing'!
  344. instanceVariableNamesFor: aString
  345. ^(aString tokenize: ' ') reject: [ :each | each isEmpty ]
  346. ! !
  347. !ClassBuilder methodsFor: 'class definition'!
  348. addSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  349. | theClass |
  350. theClass := Smalltalk current at: aString.
  351. theClass ifNotNil: [
  352. theClass superclass == aClass ifFalse: [
  353. ^ self
  354. migrateClassNamed: aString
  355. superclass: aClass
  356. instanceVariableNames: aCollection
  357. package: packageName ] ].
  358. ^ self
  359. basicAddSubclassOf: aClass
  360. named: aString
  361. instanceVariableNames: aCollection
  362. package: packageName
  363. !
  364. class: aClass instanceVariableNames: aString
  365. self basicClass: aClass instanceVariableNames: aString.
  366. self setupClass: aClass.
  367. SystemAnnouncer current
  368. announce: (ClassDefinitionChanged new
  369. theClass: aClass;
  370. yourself)
  371. !
  372. superclass: aClass subclass: aString
  373. ^self superclass: aClass subclass: aString instanceVariableNames: '' package: nil
  374. !
  375. superclass: aClass subclass: aString instanceVariableNames: aString2 package: aString3
  376. | newClass |
  377. newClass := self addSubclassOf: aClass
  378. named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2)
  379. package: (aString3 ifNil: ['unclassified']).
  380. self setupClass: newClass.
  381. SystemAnnouncer current
  382. announce: (ClassAdded new
  383. theClass: newClass;
  384. yourself).
  385. ^newClass
  386. ! !
  387. !ClassBuilder methodsFor: 'class migration'!
  388. migrateClass: aClass superclass: anotherClass
  389. console log: aClass name.
  390. self
  391. migrateClassNamed: aClass name
  392. superclass: anotherClass
  393. instanceVariableNames: aClass instanceVariableNames
  394. package: aClass package name
  395. !
  396. migrateClassNamed: aString superclass: aClass instanceVariableNames: aCollection package: packageName
  397. | oldClass newClass tmp |
  398. tmp := 'new*', aString.
  399. oldClass := Smalltalk current at: aString.
  400. newClass := self
  401. addSubclassOf: aClass
  402. named: tmp
  403. instanceVariableNames: aCollection
  404. package: packageName.
  405. self basicSwapClassNames: oldClass with: newClass.
  406. [ self copyClass: oldClass to: newClass ]
  407. on: Error
  408. do: [ :exception |
  409. self
  410. basicSwapClassNames: oldClass with: newClass;
  411. basicRemoveClass: newClass.
  412. exception signal ].
  413. self
  414. rawRenameClass: oldClass to: tmp;
  415. rawRenameClass: newClass to: aString.
  416. oldClass subclasses do: [ :each |
  417. self migrateClass: each superclass: newClass ].
  418. self basicRemoveClass: oldClass.
  419. ^newClass
  420. !
  421. renameClass: aClass to: aString
  422. self basicRenameClass: aClass to: aString.
  423. SystemAnnouncer current
  424. announce: (ClassRenamed new
  425. theClass: aClass;
  426. yourself)
  427. ! !
  428. !ClassBuilder methodsFor: 'copying'!
  429. copyClass: aClass named: aString
  430. | newClass |
  431. newClass := self
  432. addSubclassOf: aClass superclass
  433. named: aString
  434. instanceVariableNames: aClass instanceVariableNames
  435. package: aClass package name.
  436. self copyClass: aClass to: newClass.
  437. ^newClass
  438. !
  439. copyClass: aClass to: anotherClass
  440. anotherClass comment: aClass comment.
  441. aClass methodDictionary values do: [ :each |
  442. Compiler new install: each source forClass: anotherClass category: each category ].
  443. self basicClass: anotherClass class instanceVariables: aClass class instanceVariableNames.
  444. aClass class methodDictionary values do: [ :each |
  445. Compiler new install: each source forClass: anotherClass class category: each category ].
  446. self setupClass: anotherClass
  447. ! !
  448. !ClassBuilder methodsFor: 'method definition'!
  449. installMethod: aCompiledMethod forClass: aBehavior category: aString
  450. aCompiledMethod category: aString.
  451. aBehavior addCompiledMethod: aCompiledMethod.
  452. self setupClass: aBehavior.
  453. ^aCompiledMethod
  454. ! !
  455. !ClassBuilder methodsFor: 'private'!
  456. basicAddSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  457. <
  458. smalltalk.addClass(aString, aClass, aCollection, packageName);
  459. return smalltalk[aString]
  460. >
  461. !
  462. basicClass: aClass instanceVariableNames: aString
  463. self basicClass: aClass instanceVariables: (self instanceVariableNamesFor: aString)
  464. !
  465. basicClass: aClass instanceVariables: aCollection
  466. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  467. aClass basicAt: 'iVarNames' put: aCollection
  468. !
  469. basicRemoveClass: aClass
  470. <smalltalk.removeClass(aClass)>
  471. !
  472. basicRenameClass: aClass to: aString
  473. <
  474. smalltalk[aString] = aClass;
  475. delete smalltalk[aClass.className];
  476. aClass.className = aString;
  477. >
  478. !
  479. basicSwapClassNames: aClass with: anotherClass
  480. <
  481. var tmp = aClass.className;
  482. aClass.className = anotherClass.className;
  483. anotherClass.className = tmp;
  484. >
  485. !
  486. rawRenameClass: aClass to: aString
  487. <
  488. smalltalk[aString] = aClass;
  489. >
  490. ! !
  491. !ClassBuilder methodsFor: 'public'!
  492. setupClass: aClass
  493. <smalltalk.init(aClass);>
  494. ! !
  495. Object subclass: #ClassCategoryReader
  496. instanceVariableNames: 'class category'
  497. package: 'Kernel-Classes'!
  498. !ClassCategoryReader commentStamp!
  499. ClassCategoryReader represents a mechanism for retrieving class descriptions stored on a file.!
  500. !ClassCategoryReader methodsFor: 'accessing'!
  501. class: aClass category: aString
  502. class := aClass.
  503. category := aString
  504. ! !
  505. !ClassCategoryReader methodsFor: 'fileIn'!
  506. scanFrom: aChunkParser
  507. | chunk |
  508. [chunk := aChunkParser nextChunk.
  509. chunk isEmpty] whileFalse: [
  510. self compileMethod: chunk].
  511. ClassBuilder new setupClass: class
  512. ! !
  513. !ClassCategoryReader methodsFor: 'initialization'!
  514. initialize
  515. super initialize.
  516. ! !
  517. !ClassCategoryReader methodsFor: 'private'!
  518. compileMethod: aString
  519. Compiler new install: aString forClass: class category: category
  520. ! !
  521. Object subclass: #ClassCommentReader
  522. instanceVariableNames: 'class'
  523. package: 'Kernel-Classes'!
  524. !ClassCommentReader commentStamp!
  525. ClassCommentReader represents a mechanism for retrieving class comments stored on a file.
  526. See `ClassCategoryReader` too.!
  527. !ClassCommentReader methodsFor: 'accessing'!
  528. class: aClass
  529. class := aClass
  530. ! !
  531. !ClassCommentReader methodsFor: 'fileIn'!
  532. scanFrom: aChunkParser
  533. | chunk |
  534. chunk := aChunkParser nextChunk.
  535. chunk isEmpty ifFalse: [
  536. self setComment: chunk].
  537. ! !
  538. !ClassCommentReader methodsFor: 'initialization'!
  539. initialize
  540. super initialize.
  541. ! !
  542. !ClassCommentReader methodsFor: 'private'!
  543. setComment: aString
  544. class comment: aString
  545. ! !
  546. Object subclass: #ClassSorterNode
  547. instanceVariableNames: 'theClass level nodes'
  548. package: 'Kernel-Classes'!
  549. !ClassSorterNode methodsFor: 'accessing'!
  550. getNodesFrom: aCollection
  551. | children others |
  552. children := #().
  553. others := #().
  554. aCollection do: [:each |
  555. (each superclass = self theClass)
  556. ifTrue: [children add: each]
  557. ifFalse: [others add: each]].
  558. nodes:= children collect: [:each |
  559. ClassSorterNode on: each classes: others level: self level + 1]
  560. !
  561. level
  562. ^level
  563. !
  564. level: anInteger
  565. level := anInteger
  566. !
  567. nodes
  568. ^nodes
  569. !
  570. theClass
  571. ^theClass
  572. !
  573. theClass: aClass
  574. theClass := aClass
  575. ! !
  576. !ClassSorterNode methodsFor: 'visiting'!
  577. traverseClassesWith: aCollection
  578. "sort classes alphabetically Issue #143"
  579. aCollection add: self theClass.
  580. (self nodes sorted: [:a :b | a theClass name <= b theClass name ]) do: [:aNode |
  581. aNode traverseClassesWith: aCollection ].
  582. ! !
  583. !ClassSorterNode class methodsFor: 'instance creation'!
  584. on: aClass classes: aCollection level: anInteger
  585. ^self new
  586. theClass: aClass;
  587. level: anInteger;
  588. getNodesFrom: aCollection;
  589. yourself
  590. ! !