1
0

Kernel-Classes.st 12 KB

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