Kernel-Classes.st 16 KB

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