Kernel-Classes.st 16 KB

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