1
0

Kernel-Methods.st 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. Smalltalk current createPackage: 'Kernel-Methods' properties: #{}!
  2. Object subclass: #BlockClosure
  3. instanceVariableNames: ''
  4. package: 'Kernel-Methods'!
  5. !BlockClosure commentStamp!
  6. A BlockClosure is a lexical closure.
  7. The JavaScript representation is a function.
  8. A BlockClosure is evaluated with the `#value*` methods in the 'evaluating' protocol.!
  9. !BlockClosure methodsFor: 'accessing'!
  10. compiledSource
  11. <return self.toString()>
  12. !
  13. numArgs
  14. <return self.length>
  15. ! !
  16. !BlockClosure methodsFor: 'controlling'!
  17. whileFalse
  18. "inlined in the Compiler"
  19. self whileFalse: []
  20. !
  21. whileFalse: aBlock
  22. "inlined in the Compiler"
  23. <while(!!self()) {aBlock()}>
  24. !
  25. whileTrue
  26. "inlined in the Compiler"
  27. self whileTrue: []
  28. !
  29. whileTrue: aBlock
  30. "inlined in the Compiler"
  31. <while(self()) {aBlock()}>
  32. ! !
  33. !BlockClosure methodsFor: 'error handling'!
  34. on: anErrorClass do: aBlock
  35. ^self try: self catch: [:error |
  36. (error isKindOf: anErrorClass)
  37. ifTrue: [aBlock value: error]
  38. ifFalse: [error signal]]
  39. ! !
  40. !BlockClosure methodsFor: 'evaluating'!
  41. applyTo: anObject arguments: aCollection
  42. <return self.apply(anObject, aCollection)>
  43. !
  44. ensure: aBlock
  45. <try{return self()}finally{aBlock._value()}>
  46. !
  47. new
  48. "Use the receiver as a JS constructor.
  49. *Do not* use this method to instanciate Smalltalk objects!!"
  50. <return new self()>
  51. !
  52. newValue: anObject
  53. "Use the receiver as a JS constructor.
  54. *Do not* use this method to instanciate Smalltalk objects!!"
  55. <return new self(anObject)>
  56. !
  57. newValue: anObject value: anObject2
  58. "Use the receiver as a JS constructor.
  59. *Do not* use this method to instanciate Smalltalk objects!!"
  60. <return new self(anObject, anObject2)>
  61. !
  62. newValue: anObject value: anObject2 value: anObject3
  63. "Use the receiver as a JS constructor.
  64. *Do not* use this method to instanciate Smalltalk objects!!"
  65. <return new self(anObject, anObject2)>
  66. !
  67. timeToRun
  68. "Answer the number of milliseconds taken to execute this block."
  69. ^ Date millisecondsToRun: self
  70. !
  71. value
  72. "inlined in the Compiler"
  73. <return self();>
  74. !
  75. value: anArg
  76. "inlined in the Compiler"
  77. <return self(anArg);>
  78. !
  79. value: firstArg value: secondArg
  80. "inlined in the Compiler"
  81. <return self(firstArg, secondArg);>
  82. !
  83. value: firstArg value: secondArg value: thirdArg
  84. "inlined in the Compiler"
  85. <return self(firstArg, secondArg, thirdArg);>
  86. !
  87. valueWithPossibleArguments: aCollection
  88. <return self.apply(null, aCollection);>
  89. ! !
  90. !BlockClosure methodsFor: 'timeout/interval'!
  91. fork
  92. ForkPool default fork: self
  93. !
  94. valueWithInterval: aNumber
  95. <
  96. var interval = setInterval(self, aNumber);
  97. return smalltalk.Timeout._on_(interval);
  98. >
  99. !
  100. valueWithTimeout: aNumber
  101. <
  102. var timeout = setTimeout(self, aNumber);
  103. return smalltalk.Timeout._on_(timeout);
  104. >
  105. ! !
  106. Object subclass: #CompiledMethod
  107. instanceVariableNames: ''
  108. package: 'Kernel-Methods'!
  109. !CompiledMethod commentStamp!
  110. CompiledMethod hold the source and compiled code of a class method.
  111. You can get a CompiledMethod using `Behavior>>methodAt:`
  112. String methodAt: 'lines'
  113. and read the source code
  114. (String methodAt: 'lines') source
  115. See referenced classes:
  116. (String methodAt: 'lines') referencedClasses
  117. or messages sent from this method:
  118. (String methodAt: 'lines') messageSends!
  119. !CompiledMethod methodsFor: 'accessing'!
  120. arguments
  121. <return self.args || []>
  122. !
  123. category
  124. ^(self basicAt: 'category') ifNil: ['']
  125. !
  126. category: aString
  127. | oldCategory |
  128. oldCategory := self category.
  129. self basicAt: 'category' put: aString.
  130. self methodClass ifNotNil: [
  131. self methodClass organization addElement: aString.
  132. (self methodClass methods
  133. select: [ :each | each category = oldCategory ])
  134. ifEmpty: [ self methodClass organization removeElement: oldCategory ] ]
  135. !
  136. fn
  137. ^self basicAt: 'fn'
  138. !
  139. fn: aBlock
  140. self basicAt: 'fn' put: aBlock
  141. !
  142. messageSends
  143. ^self basicAt: 'messageSends'
  144. !
  145. methodClass
  146. ^self basicAt: 'methodClass'
  147. !
  148. protocol
  149. ^ self category
  150. !
  151. referencedClasses
  152. ^self basicAt: 'referencedClasses'
  153. !
  154. selector
  155. ^self basicAt: 'selector'
  156. !
  157. selector: aString
  158. self basicAt: 'selector' put: aString
  159. !
  160. source
  161. ^(self basicAt: 'source') ifNil: ['']
  162. !
  163. source: aString
  164. self basicAt: 'source' put: aString
  165. ! !
  166. Object subclass: #ForkPool
  167. instanceVariableNames: 'poolSize maxPoolSize queue worker'
  168. package: 'Kernel-Methods'!
  169. !ForkPool commentStamp!
  170. A ForkPool is responsible for handling forked blocks.
  171. The pool size sets the maximum concurrent forked blocks.
  172. The default instance is accessed with `ForkPool default`!
  173. !ForkPool methodsFor: 'accessing'!
  174. maxPoolSize
  175. ^ maxPoolSize ifNil: [ self defaultMaxPoolSize ]
  176. !
  177. maxPoolSize: anInteger
  178. maxPoolSize := anInteger
  179. ! !
  180. !ForkPool methodsFor: 'action'!
  181. addWorker
  182. worker valueWithTimeout: 0.
  183. poolSize := poolSize + 1
  184. !
  185. fork: aBlock
  186. poolSize < self maxPoolSize ifTrue: [ self addWorker ].
  187. queue back: aBlock
  188. ! !
  189. !ForkPool methodsFor: 'defaults'!
  190. defaultMaxPoolSize
  191. ^ self class defaultMaxPoolSize
  192. ! !
  193. !ForkPool methodsFor: 'initialization'!
  194. initialize
  195. super initialize.
  196. poolSize := 0.
  197. queue := Queue new.
  198. worker := self makeWorker
  199. !
  200. makeWorker
  201. | sentinel |
  202. sentinel := Object new.
  203. ^[ | block |
  204. poolSize := poolSize - 1.
  205. block := queue frontIfAbsent: [ sentinel ].
  206. block == sentinel ifFalse: [
  207. [ block value ] ensure: [ self addWorker ]]]
  208. ! !
  209. ForkPool class instanceVariableNames: 'default'!
  210. !ForkPool class methodsFor: 'accessing'!
  211. default
  212. ^default ifNil: [ default := self new ]
  213. !
  214. defaultMaxPoolSize
  215. ^100
  216. !
  217. resetDefault
  218. default := nil
  219. ! !
  220. Object subclass: #Message
  221. instanceVariableNames: 'selector arguments'
  222. package: 'Kernel-Methods'!
  223. !Message commentStamp!
  224. Generally, the system does not use instances of Message for efficiency reasons.
  225. However, when a message is not understood by its receiver, the interpreter will make up an instance of it in order to capture the information involved in an actual message transmission.
  226. This instance is sent it as an argument with the message `doesNotUnderstand:` to the receiver.
  227. See boot.js, `messageNotUnderstood` and its counterpart `Object>>doesNotUnderstand:`!
  228. !Message methodsFor: 'accessing'!
  229. arguments
  230. ^arguments
  231. !
  232. arguments: anArray
  233. arguments := anArray
  234. !
  235. selector
  236. ^selector
  237. !
  238. selector: aString
  239. selector := aString
  240. ! !
  241. !Message methodsFor: 'printing'!
  242. printString
  243. ^ String streamContents: [:aStream|
  244. aStream
  245. nextPutAll: super printString;
  246. nextPutAll: '(';
  247. nextPutAll: selector;
  248. nextPutAll: ')' ]
  249. !
  250. sendTo: anObject
  251. ^ anObject perform: self selector withArguments: self arguments
  252. ! !
  253. !Message class methodsFor: 'instance creation'!
  254. selector: aString arguments: anArray
  255. ^self new
  256. selector: aString;
  257. arguments: anArray;
  258. yourself
  259. ! !
  260. Object subclass: #MethodContext
  261. instanceVariableNames: ''
  262. package: 'Kernel-Methods'!
  263. !MethodContext commentStamp!
  264. MethodContext holds all the dynamic state associated with the execution of either a method activation resulting from a message send. That is used to build the call stack while debugging.
  265. MethodContext instances are JavaScript `SmalltalkMethodContext` objects defined in boot.js!
  266. !MethodContext methodsFor: 'accessing'!
  267. home
  268. <return self.methodContext || self.homeContext>
  269. !
  270. locals
  271. <return self.locals>
  272. !
  273. method
  274. ^self methodContext receiver class lookupSelector: self methodContext selector
  275. !
  276. methodContext
  277. self isBlockContext ifFalse: [ ^ self ].
  278. ^ self home
  279. !
  280. outerContext
  281. <return self.homeContext>
  282. !
  283. pc
  284. <return self.pc>
  285. !
  286. printString
  287. ^super printString, '(', self asString, ')'
  288. !
  289. receiver
  290. <return self.receiver>
  291. !
  292. selector
  293. <
  294. if(self.selector) {
  295. return smalltalk.convertSelector(self.selector);
  296. } else {
  297. return nil;
  298. }
  299. >
  300. !
  301. temps
  302. self deprecatedAPI.
  303. ^ self locals
  304. ! !
  305. !MethodContext methodsFor: 'converting'!
  306. asString
  307. ^self isBlockContext
  308. ifTrue: [ 'a block (in ', self methodContext receiver class printString, ')' ]
  309. ifFalse: [ self receiver class printString, ' >> ', self selector ]
  310. ! !
  311. !MethodContext methodsFor: 'testing'!
  312. isBlockContext
  313. "Block context do not have selectors."
  314. ^ self selector isNil
  315. ! !