Kernel-Classes.st 16 KB

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