1
0

Kernel-Classes.st 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. Smalltalk current createPackage: 'Kernel-Classes' properties: #{}!
  2. Object subclass: #ClassCommentReader
  3. instanceVariableNames: 'class chunkParser'
  4. category: 'Kernel-Classes'!
  5. !ClassCommentReader commentStamp!
  6. ClassCommentReader represents a mechanism for retrieving class descriptions stored on a file.
  7. See `ClassCategoryReader` too.!
  8. !ClassCommentReader methodsFor: 'accessing'!
  9. class: aClass
  10. class := aClass
  11. ! !
  12. !ClassCommentReader methodsFor: 'fileIn'!
  13. scanFrom: aChunkParser
  14. | chunk |
  15. chunk := aChunkParser nextChunk.
  16. chunk isEmpty ifFalse: [
  17. self setComment: chunk].
  18. ! !
  19. !ClassCommentReader methodsFor: 'initialization'!
  20. initialize
  21. super initialize.
  22. chunkParser := ChunkParser new.
  23. ! !
  24. !ClassCommentReader methodsFor: 'private'!
  25. setComment: aString
  26. class comment: aString
  27. ! !
  28. Object subclass: #ClassCategoryReader
  29. instanceVariableNames: 'class category chunkParser'
  30. category: 'Kernel-Classes'!
  31. !ClassCategoryReader commentStamp!
  32. ClassCategoryReader represents a mechanism for retrieving class descriptions stored on a file.!
  33. !ClassCategoryReader methodsFor: 'accessing'!
  34. class: aClass category: aString
  35. class := aClass.
  36. category := aString
  37. ! !
  38. !ClassCategoryReader methodsFor: 'fileIn'!
  39. scanFrom: aChunkParser
  40. | chunk |
  41. [chunk := aChunkParser nextChunk.
  42. chunk isEmpty] whileFalse: [
  43. self compileMethod: chunk]
  44. ! !
  45. !ClassCategoryReader methodsFor: 'initialization'!
  46. initialize
  47. super initialize.
  48. chunkParser := ChunkParser new.
  49. ! !
  50. !ClassCategoryReader methodsFor: 'private'!
  51. compileMethod: aString
  52. | method compiler |
  53. method := (compiler := Compiler new) load: aString forClass: class.
  54. method category: category.
  55. class addCompiledMethod: method.
  56. compiler setupClass: class.
  57. ! !
  58. Object subclass: #ClassBuilder
  59. instanceVariableNames: ''
  60. category: 'Kernel-Classes'!
  61. !ClassBuilder commentStamp!
  62. ClassBuilder is responsible for compiling new classes or modifying existing classes in the system.
  63. Rather than using ClassBuilder directly to compile a class, use `Class >> subclass:instanceVariableNames:package:`.!
  64. !ClassBuilder methodsFor: 'class creation'!
  65. superclass: aClass subclass: aString
  66. ^self superclass: aClass subclass: aString instanceVariableNames: '' package: nil
  67. !
  68. class: aClass instanceVariableNames: aString
  69. aClass isMetaclass ifFalse: [self error: aClass name, ' is not a metaclass'].
  70. aClass basicAt: 'iVarNames' put: (self instanceVariableNamesFor: aString).
  71. self setupClass: aClass
  72. !
  73. superclass: aClass subclass: aString instanceVariableNames: aString2 package: aString3
  74. | newClass |
  75. newClass := self addSubclassOf: aClass
  76. named: aString instanceVariableNames: (self instanceVariableNamesFor: aString2)
  77. package: (aString3 ifNil: ['unclassified']).
  78. self setupClass: newClass.
  79. ^newClass
  80. ! !
  81. !ClassBuilder methodsFor: 'private'!
  82. instanceVariableNamesFor: aString
  83. ^(aString tokenize: ' ') reject: [:each | each isEmpty]
  84. !
  85. addSubclassOf: aClass named: aString instanceVariableNames: aCollection
  86. <smalltalk.addClass(aString, aClass, aCollection);
  87. return smalltalk[aString]>
  88. !
  89. setupClass: aClass
  90. <smalltalk.init(aClass);>
  91. !
  92. addSubclassOf: aClass named: aString instanceVariableNames: aCollection package: packageName
  93. <smalltalk.addClass(aString, aClass, aCollection, packageName);
  94. return smalltalk[aString]>
  95. !
  96. copyClass: aClass named: aString
  97. | newClass |
  98. newClass := self
  99. addSubclassOf: aClass superclass
  100. named: aString
  101. instanceVariableNames: aClass instanceVariableNames
  102. package: aClass package name.
  103. self setupClass: newClass.
  104. aClass methodDictionary values do: [:each |
  105. newClass addCompiledMethod: (Compiler new load: each source forClass: newClass).
  106. (newClass methodDictionary at: each selector) category: each category].
  107. aClass class methodDictionary values do: [:each |
  108. newClass class addCompiledMethod: (Compiler new load: each source forClass: newClass class).
  109. (newClass class methodDictionary at: each selector) category: each category].
  110. self setupClass: newClass.
  111. ^newClass
  112. ! !
  113. Object subclass: #Behavior
  114. instanceVariableNames: ''
  115. category: 'Kernel-Classes'!
  116. !Behavior commentStamp!
  117. Behavior is the superclass of all class objects.
  118. It defines the protocol for creating instances of a class with `#basicNew` and `#new` (see `boot.js` for class constructors details).
  119. Instances know about the subclass/superclass relationships between classes, contain the description that instances are created from,
  120. and hold the method dictionary that's associated with each class.
  121. Behavior also provides methods for compiling methods, examining the method dictionary, and iterating over the class hierarchy.!
  122. !Behavior methodsFor: 'accessing'!
  123. name
  124. <return self.className || nil>
  125. !
  126. superclass
  127. <return self.superclass || nil>
  128. !
  129. subclasses
  130. <return smalltalk.subclasses(self)>
  131. !
  132. allSubclasses
  133. | result |
  134. result := self subclasses.
  135. self subclasses do: [:each |
  136. result addAll: each allSubclasses].
  137. ^result
  138. !
  139. withAllSubclasses
  140. ^(Array with: self) addAll: self allSubclasses; yourself
  141. !
  142. prototype
  143. <return self.fn.prototype>
  144. !
  145. methodDictionary
  146. <var dict = smalltalk.HashedCollection._new();
  147. var methods = self.fn.prototype.methods;
  148. for(var i in methods) {
  149. if(methods[i].selector) {
  150. dict._at_put_(methods[i].selector, methods[i]);
  151. }
  152. };
  153. return dict>
  154. !
  155. methodsFor: aString
  156. ^ClassCategoryReader new
  157. class: self category: aString;
  158. yourself
  159. !
  160. instanceVariableNames
  161. <return self.iVarNames>
  162. !
  163. comment
  164. ^(self basicAt: 'comment') ifNil: ['']
  165. !
  166. comment: aString
  167. self basicAt: 'comment' put: aString
  168. !
  169. commentStamp
  170. ^ClassCommentReader new
  171. class: self;
  172. yourself
  173. !
  174. protocols
  175. | protocols |
  176. protocols := Array new.
  177. self methodDictionary do: [:each |
  178. (protocols includes: each category) ifFalse: [
  179. protocols add: each category]].
  180. ^protocols sort
  181. !
  182. protocolsDo: aBlock
  183. "Execute aBlock for each method category with
  184. its collection of methods in the sort order of category name."
  185. | methodsByCategory |
  186. methodsByCategory := HashedCollection new.
  187. self methodDictionary values do: [:m |
  188. (methodsByCategory at: m category ifAbsentPut: [Array new])
  189. add: m].
  190. self protocols do: [:category |
  191. aBlock value: category value: (methodsByCategory at: category)]
  192. !
  193. allInstanceVariableNames
  194. | result |
  195. result := self instanceVariableNames copy.
  196. self superclass ifNotNil: [
  197. result addAll: self superclass allInstanceVariableNames].
  198. ^result
  199. !
  200. methodAt: aString
  201. <return smalltalk.methods(self)[aString]>
  202. !
  203. methodsFor: aString stamp: aStamp
  204. "Added for compatibility, right now ignores stamp."
  205. ^self methodsFor: aString
  206. !
  207. commentStamp: aStamp prior: prior
  208. ^self commentStamp
  209. ! !
  210. !Behavior methodsFor: 'compiling'!
  211. addCompiledMethod: aMethod
  212. <smalltalk.addMethod(aMethod.selector._asSelector(), aMethod, self)>
  213. !
  214. removeCompiledMethod: aMethod
  215. <delete self.fn.prototype[aMethod.selector._asSelector()];
  216. delete self.fn.prototype.methods[aMethod.selector];
  217. smalltalk.init(self);>
  218. !
  219. compile: aString
  220. self compile: aString category: ''
  221. !
  222. compile: aString category: anotherString
  223. | method |
  224. method := Compiler new load: aString forClass: self.
  225. method category: anotherString.
  226. self addCompiledMethod: method
  227. ! !
  228. !Behavior methodsFor: 'instance creation'!
  229. new
  230. ^self basicNew initialize
  231. !
  232. basicNew
  233. <return new self.fn()>
  234. ! !
  235. !Behavior methodsFor: 'testing'!
  236. inheritsFrom: aClass
  237. ^aClass allSubclasses includes: self
  238. ! !
  239. Behavior subclass: #Class
  240. instanceVariableNames: ''
  241. category: 'Kernel-Classes'!
  242. !Class commentStamp!
  243. Class is __the__ class object.
  244. Instances are the classes of the system.
  245. Class creation is done throught a `ClassBuilder`!
  246. !Class methodsFor: 'accessing'!
  247. category
  248. ^self package ifNil: ['Unclassified'] ifNotNil: [self package name]
  249. !
  250. rename: aString
  251. <
  252. smalltalk[aString] = self;
  253. delete smalltalk[self.className];
  254. self.className = aString;
  255. >
  256. !
  257. package
  258. <return self.pkg>
  259. !
  260. package: aPackage
  261. <self.pkg = aPackage>
  262. ! !
  263. !Class methodsFor: 'class creation'!
  264. subclass: aString instanceVariableNames: anotherString
  265. "Kept for compatibility."
  266. ^self subclass: aString instanceVariableNames: anotherString package: nil
  267. !
  268. subclass: aString instanceVariableNames: aString2 category: aString3
  269. "Kept for compatibility."
  270. self deprecatedAPI.
  271. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  272. !
  273. subclass: aString instanceVariableNames: aString2 classVariableNames: classVars poolDictionaries: pools category: aString3
  274. "Just ignore class variables and pools. Added for compatibility."
  275. ^self subclass: aString instanceVariableNames: aString2 package: aString3
  276. !
  277. subclass: aString instanceVariableNames: aString2 package: aString3
  278. ^ClassBuilder new
  279. superclass: self subclass: aString asString instanceVariableNames: aString2 package: aString3
  280. ! !
  281. !Class methodsFor: 'printing'!
  282. printString
  283. ^self name
  284. ! !
  285. !Class methodsFor: 'testing'!
  286. isClass
  287. ^true
  288. ! !
  289. Behavior subclass: #Metaclass
  290. instanceVariableNames: ''
  291. category: 'Kernel-Classes'!
  292. !Metaclass commentStamp!
  293. Metaclass is the root of the class hierarchy.
  294. Metaclass instances are metaclasses, one for each real class.
  295. Metaclass instances have a single instance, which they hold onto, which is the class that they are the metaclass of.!
  296. !Metaclass methodsFor: 'accessing'!
  297. instanceClass
  298. <return self.instanceClass>
  299. !
  300. instanceVariableNames: aCollection
  301. ClassBuilder new
  302. class: self instanceVariableNames: aCollection
  303. ! !
  304. !Metaclass methodsFor: 'printing'!
  305. printString
  306. ^self instanceClass name, ' class'
  307. ! !
  308. !Metaclass methodsFor: 'testing'!
  309. isMetaclass
  310. ^true
  311. ! !