Kernel-Classes.st 16 KB

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