Kernel-Classes.st 16 KB

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