Kernel-Classes.st 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. methodAt: aSymbol
  57. <return smalltalk.methods(self)[aSymbol._asString()]>
  58. !
  59. methodDictionary
  60. <var dict = smalltalk.HashedCollection._new();
  61. var methods = self.fn.prototype.methods;
  62. for(var i in methods) {
  63. if(methods[i].selector) {
  64. dict._at_put_(methods[i].selector, methods[i]);
  65. }
  66. };
  67. return dict>
  68. !
  69. methods
  70. ^ self methodDictionary values
  71. !
  72. methodsFor: aString
  73. ^ClassCategoryReader new
  74. class: self category: aString;
  75. yourself
  76. !
  77. methodsFor: aString stamp: aStamp
  78. "Added for compatibility, right now ignores stamp."
  79. ^self methodsFor: aString
  80. !
  81. methodsInProtocol: aString
  82. ^ self methodDictionary values select: [ :each | each protocol = aString ]
  83. !
  84. name
  85. <return self.className || nil>
  86. !
  87. protocols
  88. | protocols |
  89. protocols := Array new.
  90. self methodDictionary do: [:each |
  91. (protocols includes: each category) ifFalse: [
  92. protocols add: each category]].
  93. ^protocols sort
  94. !
  95. protocolsDo: aBlock
  96. "Execute aBlock for each method category with
  97. its collection of methods in the sort order of category name."
  98. | methodsByCategory |
  99. methodsByCategory := HashedCollection new.
  100. self methodDictionary values do: [:m |
  101. (methodsByCategory at: m category ifAbsentPut: [Array new])
  102. add: m].
  103. self protocols do: [:category |
  104. aBlock value: category value: (methodsByCategory at: category)]
  105. !
  106. prototype
  107. <return self.fn.prototype>
  108. !
  109. selectors
  110. ^ self methodDictionary keys
  111. !
  112. subclasses
  113. <return smalltalk.subclasses(self)>
  114. !
  115. superclass
  116. <return self.superclass || nil>
  117. !
  118. theMetaClass
  119. ^ self class
  120. !
  121. theNonMetaClass
  122. ^ self
  123. !
  124. withAllSubclasses
  125. ^(Array with: self) addAll: self allSubclasses; yourself
  126. ! !
  127. !Behavior methodsFor: 'compiling'!
  128. addCompiledMethod: aMethod
  129. <smalltalk.addMethod(aMethod.selector._asSelector(), aMethod, self)>.
  130. SystemAnnouncer current
  131. announce: (MethodAdded new
  132. theClass: self;
  133. method: aMethod;
  134. yourself)
  135. !
  136. compile: aString
  137. self compile: aString category: ''
  138. !
  139. compile: aString category: anotherString
  140. Compiler new
  141. install: aString
  142. forClass: self
  143. category: anotherString
  144. !
  145. removeCompiledMethod: aMethod
  146. <delete self.fn.prototype[aMethod.selector._asSelector()];
  147. delete self.fn.prototype.methods[aMethod.selector];
  148. smalltalk.init(self);>.
  149. SystemAnnouncer current
  150. announce: (MethodRemoved new
  151. theClass: self;
  152. method: aMethod;
  153. yourself)
  154. ! !
  155. !Behavior methodsFor: 'instance creation'!
  156. basicNew
  157. <return new self.fn()>
  158. !
  159. new
  160. ^self basicNew initialize
  161. ! !
  162. !Behavior methodsFor: 'testing'!
  163. canUnderstand: aSelector
  164. ^(self methodDictionary keys includes: aSelector asString) or: [
  165. self superclass notNil and: [self superclass canUnderstand: aSelector]]
  166. !
  167. inheritsFrom: aClass
  168. ^aClass allSubclasses includes: self
  169. ! !
  170. Behavior subclass: #Class
  171. instanceVariableNames: ''
  172. package: 'Kernel-Classes'!
  173. !Class commentStamp!
  174. Class is __the__ class object.
  175. Instances are the classes of the system.
  176. Class creation is done throught a `ClassBuilder`!
  177. !Class methodsFor: 'accessing'!
  178. category
  179. ^self package ifNil: ['Unclassified'] ifNotNil: [self package name]
  180. !
  181. definition
  182. ^ String streamContents: [ :stream |
  183. stream
  184. nextPutAll: self superclass asString;
  185. nextPutAll: ' subclass: #';
  186. nextPutAll: self name;
  187. nextPutAll: String lf, String tab;
  188. nextPutAll: 'instanceVariableNames: '''.
  189. self instanceVariableNames
  190. do: [ :each | stream nextPutAll: each ]
  191. separatedBy: [ stream nextPutAll: ' ' ].
  192. stream
  193. nextPutAll: '''', String lf, String tab;
  194. nextPutAll: 'package: ''';
  195. nextPutAll: self category;
  196. nextPutAll: '''' ]
  197. !
  198. package
  199. <return self.pkg>
  200. !
  201. package: aPackage
  202. <self.pkg = aPackage>
  203. !
  204. rename: aString
  205. ClassBuilder new renameClass: self to: aString
  206. ! !
  207. !Class methodsFor: 'class creation'!
  208. subclass: aString instanceVariableNames: anotherString
  209. "Kept for compatibility."
  210. ^self subclass: aString instanceVariableNames: anotherString package: nil
  211. !
  212. subclass: aString instanceVariableNames: aString2 category: aString3
  213. "Kept for compatibility."
  214. self deprecatedAPI.
  215. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  216. !
  217. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  218. "Just ignore class variables and pools. Added for compatibility."
  219. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  220. !
  221. subclass: aString instanceVariableNames: aString2 package: aString3
  222. ^ClassBuilder new
  223. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  224. ! !
  225. !Class methodsFor: 'converting'!
  226. asJavascript
  227. ^ 'smalltalk.', self name
  228. ! !
  229. !Class methodsFor: 'printing'!
  230. printString
  231. ^self name
  232. ! !
  233. !Class methodsFor: 'testing'!
  234. isClass
  235. ^true
  236. ! !
  237. Behavior subclass: #Metaclass
  238. instanceVariableNames: ''
  239. package: 'Kernel-Classes'!
  240. !Metaclass commentStamp!
  241. Metaclass is the root of the class hierarchy.
  242. Metaclass instances are metaclasses, one for each real class.
  243. Metaclass instances have a single instance, which they hold onto, which is the class that they are the metaclass of.!
  244. !Metaclass methodsFor: 'accessing'!
  245. definition
  246. ^ String streamContents: [ :stream |
  247. stream
  248. nextPutAll: self asString;
  249. nextPutAll: ' class ';
  250. nextPutAll: 'instanceVariableNames: '''.
  251. self instanceVariableNames
  252. do: [ :each | stream nextPutAll: each ]
  253. separatedBy: [ stream nextPutAll: ' ' ].
  254. stream nextPutAll: '''' ]
  255. !
  256. instanceClass
  257. <return self.instanceClass>
  258. !
  259. instanceVariableNames: aCollection
  260. ClassBuilder new
  261. class: self instanceVariableNames: aCollection
  262. !
  263. theMetaClass
  264. ^ self
  265. !
  266. theNonMetaClass
  267. ^ self instanceClass
  268. ! !
  269. !Metaclass methodsFor: 'converting'!
  270. asJavascript
  271. ^ 'smalltalk.', self instanceClass name, '.klass'
  272. ! !
  273. !Metaclass methodsFor: 'printing'!
  274. printString
  275. ^self instanceClass name, ' class'
  276. ! !
  277. !Metaclass methodsFor: 'testing'!
  278. isMetaclass
  279. ^true
  280. ! !
  281. Object subclass: #ClassBuilder
  282. instanceVariableNames: ''
  283. package: 'Kernel-Classes'!
  284. !ClassBuilder commentStamp!
  285. ClassBuilder is responsible for compiling new classes or modifying existing classes in the system.
  286. Rather than using ClassBuilder directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  287. !ClassBuilder methodsFor: 'class creation'!
  288. class: aClass instanceVariableNames: aString
  289. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  290. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  291. SystemAnnouncer current
  292. announce: (ClassDefinitionChanged new
  293. theClass: aClass;
  294. yourself).
  295. self setupClass: aClass
  296. !
  297. renameClass: aClass to: aString
  298. <
  299. smalltalk[aString] = aClass;
  300. delete smalltalk[aClass.className];
  301. aClass.className = aString;
  302. >.
  303. SystemAnnouncer current
  304. announce: (ClassRenamed new
  305. theClass: aClass;
  306. yourself)
  307. !
  308. superclass: aClass subclass: aString
  309. ^self superclass: aClass subclass: aString instanceVariableNames: '' package: nil
  310. !
  311. superclass: aClass subclass: aString instanceVariableNames: aString2 package: aString3
  312. | newClass |
  313. newClass := self addSubclassOf: aClass
  314. named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2)
  315. package: (aString3 ifNil: ['unclassified']).
  316. self setupClass: newClass.
  317. SystemAnnouncer current
  318. announce: (ClassAdded new
  319. theClass: newClass;
  320. yourself).
  321. ^newClass
  322. ! !
  323. !ClassBuilder methodsFor: 'private'!
  324. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  325. <smalltalk.addClass(aString, aClass, aCollection);
  326. return smalltalk[aString]>
  327. !
  328. addSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  329. <smalltalk.addClass(aString, aClass, aCollection, packageName);
  330. return smalltalk[aString]>
  331. !
  332. copyClass: aClass named: aString
  333. | newClass |
  334. newClass := self
  335. addSubclassOf: aClass superclass
  336. named: aString
  337. instanceVariableNames: aClass instanceVariableNames
  338. package: aClass package name.
  339. self setupClass: newClass.
  340. aClass methodDictionary values do: [:each |
  341. Compiler new install: each source forClass: newClass category: each category].
  342. aClass class methodDictionary values do: [:each |
  343. Compiler new install: each source forClass: newClass class category: each category].
  344. self setupClass: newClass.
  345. ^newClass
  346. !
  347. instanceVariableNamesFor: aString
  348. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  349. !
  350. setupClass: aClass
  351. <smalltalk.init(aClass);>
  352. ! !
  353. Object subclass: #ClassCategoryReader
  354. instanceVariableNames: 'class category chunkParser'
  355. package: 'Kernel-Classes'!
  356. !ClassCategoryReader commentStamp!
  357. ClassCategoryReader represents a mechanism for retrieving class descriptions stored on a file.!
  358. !ClassCategoryReader methodsFor: 'accessing'!
  359. class: aClass category: aString
  360. class := aClass.
  361. category := aString
  362. ! !
  363. !ClassCategoryReader methodsFor: 'fileIn'!
  364. scanFrom: aChunkParser
  365. | chunk |
  366. [chunk := aChunkParser nextChunk.
  367. chunk isEmpty] whileFalse: [
  368. self compileMethod: chunk].
  369. Compiler new setupClass: class
  370. ! !
  371. !ClassCategoryReader methodsFor: 'initialization'!
  372. initialize
  373. super initialize.
  374. chunkParser := ChunkParser new.
  375. ! !
  376. !ClassCategoryReader methodsFor: 'private'!
  377. compileMethod: aString
  378. Compiler new install: aString forClass: class category: category
  379. ! !
  380. Object subclass: #ClassCommentReader
  381. instanceVariableNames: 'class chunkParser'
  382. package: 'Kernel-Classes'!
  383. !ClassCommentReader commentStamp!
  384. ClassCommentReader represents a mechanism for retrieving class descriptions stored on a file.
  385. See `ClassCategoryReader` too.!
  386. !ClassCommentReader methodsFor: 'accessing'!
  387. class: aClass
  388. class := aClass
  389. ! !
  390. !ClassCommentReader methodsFor: 'fileIn'!
  391. scanFrom: aChunkParser
  392. | chunk |
  393. chunk := aChunkParser nextChunk.
  394. chunk isEmpty ifFalse: [
  395. self setComment: chunk].
  396. ! !
  397. !ClassCommentReader methodsFor: 'initialization'!
  398. initialize
  399. super initialize.
  400. chunkParser := ChunkParser new.
  401. ! !
  402. !ClassCommentReader methodsFor: 'private'!
  403. setComment: aString
  404. class comment: aString
  405. ! !
  406. Object subclass: #ClassSorterNode
  407. instanceVariableNames: 'theClass level nodes'
  408. package: 'Kernel-Classes'!
  409. !ClassSorterNode methodsFor: 'accessing'!
  410. getNodesFrom: aCollection
  411. | children others |
  412. children := #().
  413. others := #().
  414. aCollection do: [:each |
  415. (each superclass = self theClass)
  416. ifTrue: [children add: each]
  417. ifFalse: [others add: each]].
  418. nodes:= children collect: [:each |
  419. ClassSorterNode on: each classes: others level: self level + 1]
  420. !
  421. level
  422. ^level
  423. !
  424. level: anInteger
  425. level := anInteger
  426. !
  427. nodes
  428. ^nodes
  429. !
  430. theClass
  431. ^theClass
  432. !
  433. theClass: aClass
  434. theClass := aClass
  435. ! !
  436. !ClassSorterNode methodsFor: 'visiting'!
  437. traverseClassesWith: aCollection
  438. "sort classes alphabetically Issue #143"
  439. aCollection add: self theClass.
  440. (self nodes sorted: [:a :b | a theClass name <= b theClass name ]) do: [:aNode |
  441. aNode traverseClassesWith: aCollection ].
  442. ! !
  443. !ClassSorterNode class methodsFor: 'instance creation'!
  444. on: aClass classes: aCollection level: anInteger
  445. ^self new
  446. theClass: aClass;
  447. level: anInteger;
  448. getNodesFrom: aCollection;
  449. yourself
  450. ! !