2
0

Kernel-Classes.st 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 method |
  108. compiler := Compiler new.
  109. method := compiler install: aString forClass: self category: anotherString.
  110. compiler setupClass: self.
  111. ^ method selector
  112. !
  113. removeCompiledMethod: aMethod
  114. <delete self.fn.prototype[aMethod.selector._asSelector()];
  115. delete self.fn.prototype.methods[aMethod.selector];
  116. smalltalk.init(self);>
  117. !
  118. removeSelector: aString
  119. self removeCompiledMethod: (self methodAt: aString)
  120. ! !
  121. !Behavior methodsFor: 'instance creation'!
  122. basicNew
  123. <return new self.fn()>
  124. !
  125. new
  126. ^self basicNew initialize
  127. ! !
  128. !Behavior methodsFor: 'testing'!
  129. canUnderstand: aSelector
  130. ^(self methodDictionary keys includes: aSelector asString) or: [
  131. self superclass notNil and: [self superclass canUnderstand: aSelector]]
  132. !
  133. inheritsFrom: aClass
  134. ^aClass allSubclasses includes: self
  135. ! !
  136. Behavior subclass: #Class
  137. instanceVariableNames: ''
  138. package: 'Kernel-Classes'!
  139. !Class commentStamp!
  140. Class is __the__ class object.
  141. Instances are the classes of the system.
  142. Class creation is done throught a `ClassBuilder`!
  143. !Class methodsFor: 'accessing'!
  144. category
  145. ^self package ifNil: ['Unclassified'] ifNotNil: [self package name]
  146. !
  147. definition
  148. | stream |
  149. stream := '' writeStream.
  150. stream
  151. nextPutAll: self superclass asString;
  152. nextPutAll: ' subclass: #';
  153. nextPutAll: self name;
  154. nextPutAll: String lf, String tab;
  155. nextPutAll: 'instanceVariableNames: '''.
  156. self instanceVariableNames
  157. do: [:each | stream nextPutAll: each]
  158. separatedBy: [stream nextPutAll: ' '].
  159. stream
  160. nextPutAll: '''', String lf, String tab;
  161. nextPutAll: 'package: ''';
  162. nextPutAll: self category;
  163. nextPutAll: ''''.
  164. ^stream contents
  165. !
  166. package
  167. <return self.pkg>
  168. !
  169. package: aPackage
  170. <self.pkg = aPackage>
  171. !
  172. rename: aString
  173. <
  174. smalltalk[aString] = self;
  175. delete smalltalk[self.className];
  176. self.className = aString;
  177. >
  178. ! !
  179. !Class methodsFor: 'class creation'!
  180. subclass: aString instanceVariableNames: anotherString
  181. "Kept for compatibility."
  182. ^self subclass: aString instanceVariableNames: anotherString package: nil
  183. !
  184. subclass: aString instanceVariableNames: aString2 category: aString3
  185. "Kept for compatibility."
  186. self deprecatedAPI.
  187. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  188. !
  189. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  190. "Just ignore class variables and pools. Added for compatibility."
  191. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  192. !
  193. subclass: aString instanceVariableNames: aString2 package: aString3
  194. ^ClassBuilder new
  195. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  196. ! !
  197. !Class methodsFor: 'printing'!
  198. printString
  199. ^self name
  200. ! !
  201. !Class methodsFor: 'testing'!
  202. isClass
  203. ^true
  204. ! !
  205. Behavior subclass: #Metaclass
  206. instanceVariableNames: ''
  207. package: 'Kernel-Classes'!
  208. !Metaclass commentStamp!
  209. Metaclass is the root of the class hierarchy.
  210. Metaclass instances are metaclasses, one for each real class.
  211. Metaclass instances have a single instance, which they hold onto, which is the class that they are the metaclass of.!
  212. !Metaclass methodsFor: 'accessing'!
  213. definition
  214. | stream |
  215. stream := '' writeStream.
  216. stream
  217. nextPutAll: self asString;
  218. nextPutAll: ' instanceVariableNames: '''.
  219. self instanceVariableNames
  220. do: [:each | stream nextPutAll: each]
  221. separatedBy: [stream nextPutAll: ' '].
  222. stream nextPutAll: ''''.
  223. ^stream contents
  224. !
  225. instanceClass
  226. <return self.instanceClass>
  227. !
  228. instanceVariableNames: aCollection
  229. ClassBuilder new
  230. class: self instanceVariableNames: aCollection
  231. ! !
  232. !Metaclass methodsFor: 'printing'!
  233. printString
  234. ^self instanceClass name, ' class'
  235. ! !
  236. !Metaclass methodsFor: 'testing'!
  237. isMetaclass
  238. ^true
  239. ! !
  240. Object subclass: #ClassBuilder
  241. instanceVariableNames: ''
  242. package: 'Kernel-Classes'!
  243. !ClassBuilder commentStamp!
  244. ClassBuilder is responsible for compiling new classes or modifying existing classes in the system.
  245. Rather than using ClassBuilder directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  246. !ClassBuilder methodsFor: 'class creation'!
  247. class: aClass instanceVariableNames: aString
  248. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  249. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  250. self setupClass: aClass
  251. !
  252. superclass: aClass subclass: aString
  253. ^self superclass: aClass subclass: aString instanceVariableNames: '' package: nil
  254. !
  255. superclass: aClass subclass: aString instanceVariableNames: aString2 package: aString3
  256. | newClass |
  257. newClass := self addSubclassOf: aClass
  258. named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2)
  259. package: (aString3 ifNil: ['unclassified']).
  260. self setupClass: newClass.
  261. ^newClass
  262. ! !
  263. !ClassBuilder methodsFor: 'private'!
  264. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  265. <smalltalk.addClass(aString, aClass, aCollection);
  266. return smalltalk[aString]>
  267. !
  268. addSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  269. <smalltalk.addClass(aString, aClass, aCollection, packageName);
  270. return smalltalk[aString]>
  271. !
  272. copyClass: aClass named: aString
  273. | newClass |
  274. newClass := self
  275. addSubclassOf: aClass superclass
  276. named: aString
  277. instanceVariableNames: aClass instanceVariableNames
  278. package: aClass package name.
  279. self setupClass: newClass.
  280. aClass methodDictionary values do: [:each |
  281. Compiler new install: each source forClass: newClass category: each category].
  282. aClass class methodDictionary values do: [:each |
  283. Compiler new install: each source forClass: newClass class category: each category].
  284. self setupClass: newClass.
  285. ^newClass
  286. !
  287. instanceVariableNamesFor: aString
  288. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  289. !
  290. setupClass: aClass
  291. <smalltalk.init(aClass);>
  292. ! !
  293. Object subclass: #ClassCategoryReader
  294. instanceVariableNames: 'class category chunkParser'
  295. package: 'Kernel-Classes'!
  296. !ClassCategoryReader commentStamp!
  297. ClassCategoryReader represents a mechanism for retrieving class descriptions stored on a file.!
  298. !ClassCategoryReader methodsFor: 'accessing'!
  299. class: aClass category: aString
  300. class := aClass.
  301. category := aString
  302. ! !
  303. !ClassCategoryReader methodsFor: 'fileIn'!
  304. scanFrom: aChunkParser
  305. | chunk |
  306. [chunk := aChunkParser nextChunk.
  307. chunk isEmpty] whileFalse: [
  308. self compileMethod: chunk].
  309. Compiler new setupClass: class
  310. ! !
  311. !ClassCategoryReader methodsFor: 'initialization'!
  312. initialize
  313. super initialize.
  314. chunkParser := ChunkParser new.
  315. ! !
  316. !ClassCategoryReader methodsFor: 'private'!
  317. compileMethod: aString
  318. Compiler new install: aString forClass: class category: category
  319. ! !
  320. Object subclass: #ClassCommentReader
  321. instanceVariableNames: 'class chunkParser'
  322. package: 'Kernel-Classes'!
  323. !ClassCommentReader commentStamp!
  324. ClassCommentReader represents a mechanism for retrieving class descriptions stored on a file.
  325. See `ClassCategoryReader` too.!
  326. !ClassCommentReader methodsFor: 'accessing'!
  327. class: aClass
  328. class := aClass
  329. ! !
  330. !ClassCommentReader methodsFor: 'fileIn'!
  331. scanFrom: aChunkParser
  332. | chunk |
  333. chunk := aChunkParser nextChunk.
  334. chunk isEmpty ifFalse: [
  335. self setComment: chunk].
  336. ! !
  337. !ClassCommentReader methodsFor: 'initialization'!
  338. initialize
  339. super initialize.
  340. chunkParser := ChunkParser new.
  341. ! !
  342. !ClassCommentReader methodsFor: 'private'!
  343. setComment: aString
  344. class comment: aString
  345. ! !
  346. Object subclass: #ClassSorterNode
  347. instanceVariableNames: 'theClass level nodes'
  348. package: 'Kernel-Classes'!
  349. !ClassSorterNode methodsFor: 'accessing'!
  350. getNodesFrom: aCollection
  351. | children others |
  352. children := #().
  353. others := #().
  354. aCollection do: [:each |
  355. (each superclass = self theClass)
  356. ifTrue: [children add: each]
  357. ifFalse: [others add: each]].
  358. nodes:= children collect: [:each |
  359. ClassSorterNode on: each classes: others level: self level + 1]
  360. !
  361. level
  362. ^level
  363. !
  364. level: anInteger
  365. level := anInteger
  366. !
  367. nodes
  368. ^nodes
  369. !
  370. theClass
  371. ^theClass
  372. !
  373. theClass: aClass
  374. theClass := aClass
  375. ! !
  376. !ClassSorterNode methodsFor: 'visiting'!
  377. traverseClassesWith: aCollection
  378. "sort classes alphabetically Issue #143"
  379. aCollection add: self theClass.
  380. (self nodes sorted: [:a :b | a theClass name <= b theClass name ]) do: [:aNode |
  381. aNode traverseClassesWith: aCollection ].
  382. ! !
  383. !ClassSorterNode class methodsFor: 'instance creation'!
  384. on: aClass classes: aCollection level: anInteger
  385. ^self new
  386. theClass: aClass;
  387. level: anInteger;
  388. getNodesFrom: aCollection;
  389. yourself
  390. ! !