Kernel-Classes.st 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. Smalltalk current createPackage: 'Kernel-Classes' properties: #{}!
  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. <smalltalk.addMethod(aMethod.selector._asSelector(), aMethod, self)>.
  141. SystemAnnouncer current
  142. announce: (MethodAdded new
  143. theClass: self;
  144. method: aMethod;
  145. yourself)
  146. !
  147. compile: aString
  148. self compile: aString category: ''
  149. !
  150. compile: aString category: anotherString
  151. Compiler new
  152. install: aString
  153. forClass: self
  154. category: anotherString
  155. !
  156. removeCompiledMethod: aMethod
  157. <
  158. smalltalk.removeMethod(aMethod)
  159. smalltalk.init(self);
  160. >.
  161. SystemAnnouncer current
  162. announce: (MethodRemoved new
  163. theClass: self;
  164. method: aMethod;
  165. yourself)
  166. ! !
  167. !Behavior methodsFor: 'instance creation'!
  168. basicNew
  169. <return new self.fn()>
  170. !
  171. new
  172. ^self basicNew initialize
  173. ! !
  174. !Behavior methodsFor: 'testing'!
  175. canUnderstand: aSelector
  176. ^(self methodDictionary keys includes: aSelector asString) or: [
  177. self superclass notNil and: [self superclass canUnderstand: aSelector]]
  178. !
  179. includesSelector: aSymbol
  180. ^ self methodDictionary includesKey: aSymbol asString
  181. !
  182. inheritsFrom: aClass
  183. ^aClass allSubclasses includes: self
  184. ! !
  185. Behavior subclass: #Class
  186. instanceVariableNames: ''
  187. package: 'Kernel-Classes'!
  188. !Class commentStamp!
  189. Class is __the__ class object.
  190. Instances are the classes of the system.
  191. Class creation is done throught a `ClassBuilder`!
  192. !Class methodsFor: 'accessing'!
  193. category
  194. ^self package ifNil: ['Unclassified'] ifNotNil: [self package name]
  195. !
  196. definition
  197. ^ String streamContents: [ :stream |
  198. stream
  199. nextPutAll: self superclass asString;
  200. nextPutAll: ' subclass: #';
  201. nextPutAll: self name;
  202. nextPutAll: String lf, String tab;
  203. nextPutAll: 'instanceVariableNames: '''.
  204. self instanceVariableNames
  205. do: [ :each | stream nextPutAll: each ]
  206. separatedBy: [ stream nextPutAll: ' ' ].
  207. stream
  208. nextPutAll: '''', String lf, String tab;
  209. nextPutAll: 'package: ''';
  210. nextPutAll: self category;
  211. nextPutAll: '''' ]
  212. !
  213. package
  214. <return self.pkg>
  215. !
  216. package: aPackage
  217. <self.pkg = aPackage>
  218. !
  219. rename: aString
  220. ClassBuilder new renameClass: self to: aString
  221. ! !
  222. !Class methodsFor: 'class creation'!
  223. subclass: aString instanceVariableNames: anotherString
  224. "Kept for compatibility."
  225. ^self subclass: aString instanceVariableNames: anotherString package: nil
  226. !
  227. subclass: aString instanceVariableNames: aString2 category: aString3
  228. "Kept for compatibility."
  229. self deprecatedAPI.
  230. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  231. !
  232. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  233. "Just ignore class variables and pools. Added for compatibility."
  234. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  235. !
  236. subclass: aString instanceVariableNames: aString2 package: aString3
  237. ^ClassBuilder new
  238. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  239. ! !
  240. !Class methodsFor: 'converting'!
  241. asJavascript
  242. ^ 'smalltalk.', self name
  243. ! !
  244. !Class methodsFor: 'printing'!
  245. printString
  246. ^self name
  247. ! !
  248. !Class methodsFor: 'testing'!
  249. isClass
  250. ^true
  251. ! !
  252. Behavior subclass: #Metaclass
  253. instanceVariableNames: ''
  254. package: 'Kernel-Classes'!
  255. !Metaclass commentStamp!
  256. Metaclass is the root of the class hierarchy.
  257. Metaclass instances are metaclasses, one for each real class.
  258. Metaclass instances have a single instance, which they hold onto, which is the class that they are the metaclass of.!
  259. !Metaclass methodsFor: 'accessing'!
  260. definition
  261. ^ String streamContents: [ :stream |
  262. stream
  263. nextPutAll: self asString;
  264. nextPutAll: ' class ';
  265. nextPutAll: 'instanceVariableNames: '''.
  266. self instanceVariableNames
  267. do: [ :each | stream nextPutAll: each ]
  268. separatedBy: [ stream nextPutAll: ' ' ].
  269. stream nextPutAll: '''' ]
  270. !
  271. instanceClass
  272. <return self.instanceClass>
  273. !
  274. instanceVariableNames: aCollection
  275. ClassBuilder new
  276. class: self instanceVariableNames: aCollection
  277. !
  278. theMetaClass
  279. ^ self
  280. !
  281. theNonMetaClass
  282. ^ self instanceClass
  283. ! !
  284. !Metaclass methodsFor: 'converting'!
  285. asJavascript
  286. ^ 'smalltalk.', self instanceClass name, '.klass'
  287. ! !
  288. !Metaclass methodsFor: 'printing'!
  289. printString
  290. ^self instanceClass name, ' class'
  291. ! !
  292. !Metaclass methodsFor: 'testing'!
  293. isMetaclass
  294. ^true
  295. ! !
  296. Object subclass: #ClassBuilder
  297. instanceVariableNames: ''
  298. package: 'Kernel-Classes'!
  299. !ClassBuilder commentStamp!
  300. ClassBuilder is responsible for compiling new classes or modifying existing classes in the system.
  301. Rather than using ClassBuilder directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  302. !ClassBuilder methodsFor: 'class creation'!
  303. class: aClass instanceVariableNames: aString
  304. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  305. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  306. SystemAnnouncer current
  307. announce: (ClassDefinitionChanged new
  308. theClass: aClass;
  309. yourself).
  310. self setupClass: aClass
  311. !
  312. renameClass: aClass to: aString
  313. <
  314. smalltalk[aString] = aClass;
  315. delete smalltalk[aClass.className];
  316. aClass.className = aString;
  317. >.
  318. SystemAnnouncer current
  319. announce: (ClassRenamed new
  320. theClass: aClass;
  321. yourself)
  322. !
  323. superclass: aClass subclass: aString
  324. ^self superclass: aClass subclass: aString instanceVariableNames: '' package: nil
  325. !
  326. superclass: aClass subclass: aString instanceVariableNames: aString2 package: aString3
  327. | newClass |
  328. newClass := self addSubclassOf: aClass
  329. named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2)
  330. package: (aString3 ifNil: ['unclassified']).
  331. self setupClass: newClass.
  332. SystemAnnouncer current
  333. announce: (ClassAdded new
  334. theClass: newClass;
  335. yourself).
  336. ^newClass
  337. ! !
  338. !ClassBuilder methodsFor: 'private'!
  339. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  340. <smalltalk.addClass(aString, aClass, aCollection);
  341. return smalltalk[aString]>
  342. !
  343. addSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  344. <smalltalk.addClass(aString, aClass, aCollection, packageName);
  345. return smalltalk[aString]>
  346. !
  347. copyClass: aClass named: aString
  348. | newClass |
  349. newClass := self
  350. addSubclassOf: aClass superclass
  351. named: aString
  352. instanceVariableNames: aClass instanceVariableNames
  353. package: aClass package name.
  354. self setupClass: newClass.
  355. aClass methodDictionary values do: [:each |
  356. Compiler new install: each source forClass: newClass category: each category].
  357. aClass class methodDictionary values do: [:each |
  358. Compiler new install: each source forClass: newClass class category: each category].
  359. self setupClass: newClass.
  360. ^newClass
  361. !
  362. instanceVariableNamesFor: aString
  363. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  364. !
  365. setupClass: aClass
  366. <smalltalk.init(aClass);>
  367. ! !
  368. Object subclass: #ClassCategoryReader
  369. instanceVariableNames: 'class category chunkParser'
  370. package: 'Kernel-Classes'!
  371. !ClassCategoryReader commentStamp!
  372. ClassCategoryReader represents a mechanism for retrieving class descriptions stored on a file.!
  373. !ClassCategoryReader methodsFor: 'accessing'!
  374. class: aClass category: aString
  375. class := aClass.
  376. category := aString
  377. ! !
  378. !ClassCategoryReader methodsFor: 'fileIn'!
  379. scanFrom: aChunkParser
  380. | chunk |
  381. [chunk := aChunkParser nextChunk.
  382. chunk isEmpty] whileFalse: [
  383. self compileMethod: chunk].
  384. Compiler new setupClass: class
  385. ! !
  386. !ClassCategoryReader methodsFor: 'initialization'!
  387. initialize
  388. super initialize.
  389. chunkParser := ChunkParser new.
  390. ! !
  391. !ClassCategoryReader methodsFor: 'private'!
  392. compileMethod: aString
  393. Compiler new install: aString forClass: class category: category
  394. ! !
  395. Object subclass: #ClassCommentReader
  396. instanceVariableNames: 'class chunkParser'
  397. package: 'Kernel-Classes'!
  398. !ClassCommentReader commentStamp!
  399. ClassCommentReader represents a mechanism for retrieving class descriptions stored on a file.
  400. See `ClassCategoryReader` too.!
  401. !ClassCommentReader methodsFor: 'accessing'!
  402. class: aClass
  403. class := aClass
  404. ! !
  405. !ClassCommentReader methodsFor: 'fileIn'!
  406. scanFrom: aChunkParser
  407. | chunk |
  408. chunk := aChunkParser nextChunk.
  409. chunk isEmpty ifFalse: [
  410. self setComment: chunk].
  411. ! !
  412. !ClassCommentReader methodsFor: 'initialization'!
  413. initialize
  414. super initialize.
  415. chunkParser := ChunkParser new.
  416. ! !
  417. !ClassCommentReader methodsFor: 'private'!
  418. setComment: aString
  419. class comment: aString
  420. ! !
  421. Object subclass: #ClassSorterNode
  422. instanceVariableNames: 'theClass level nodes'
  423. package: 'Kernel-Classes'!
  424. !ClassSorterNode methodsFor: 'accessing'!
  425. getNodesFrom: aCollection
  426. | children others |
  427. children := #().
  428. others := #().
  429. aCollection do: [:each |
  430. (each superclass = self theClass)
  431. ifTrue: [children add: each]
  432. ifFalse: [others add: each]].
  433. nodes:= children collect: [:each |
  434. ClassSorterNode on: each classes: others level: self level + 1]
  435. !
  436. level
  437. ^level
  438. !
  439. level: anInteger
  440. level := anInteger
  441. !
  442. nodes
  443. ^nodes
  444. !
  445. theClass
  446. ^theClass
  447. !
  448. theClass: aClass
  449. theClass := aClass
  450. ! !
  451. !ClassSorterNode methodsFor: 'visiting'!
  452. traverseClassesWith: aCollection
  453. "sort classes alphabetically Issue #143"
  454. aCollection add: self theClass.
  455. (self nodes sorted: [:a :b | a theClass name <= b theClass name ]) do: [:aNode |
  456. aNode traverseClassesWith: aCollection ].
  457. ! !
  458. !ClassSorterNode class methodsFor: 'instance creation'!
  459. on: aClass classes: aCollection level: anInteger
  460. ^self new
  461. theClass: aClass;
  462. level: anInteger;
  463. getNodesFrom: aCollection;
  464. yourself
  465. ! !