Kernel-Classes.st 11 KB

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