Kernel-Classes.st 11 KB

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