Kernel-Classes.st 16 KB

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