1
0

Kernel-Classes.st 16 KB

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