Kernel-Methods.st 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. | local |
  96. <local = setInterval(self, aNumber)>.
  97. ^ Timeout on: local
  98. !
  99. valueWithTimeout: aNumber
  100. | local |
  101. <local = setTimeout(self, aNumber)>.
  102. ^ Timeout on: local
  103. ! !
  104. Object subclass: #CompiledMethod
  105. instanceVariableNames: ''
  106. package: 'Kernel-Methods'!
  107. !CompiledMethod commentStamp!
  108. CompiledMethod hold the source and compiled code of a class method.
  109. You can get a CompiledMethod using `Behavior>>methodAt:`
  110. String methodAt: 'lines'
  111. and read the source code
  112. (String methodAt: 'lines') source
  113. See referenced classes:
  114. (String methodAt: 'lines') referencedClasses
  115. or messages sent from this method:
  116. (String methodAt: 'lines') messageSends!
  117. !CompiledMethod methodsFor: 'accessing'!
  118. arguments
  119. <return self.args || []>
  120. !
  121. category
  122. ^(self basicAt: 'category') ifNil: ['']
  123. !
  124. category: aString
  125. | oldCategory |
  126. oldCategory := self category.
  127. self basicAt: 'category' put: aString.
  128. self methodClass ifNotNil: [
  129. self methodClass organization addElement: aString.
  130. (self methodClass methods
  131. select: [ :each | each category = oldCategory ])
  132. ifEmpty: [ self methodClass organization removeElement: oldCategory ] ]
  133. !
  134. fn
  135. ^self basicAt: 'fn'
  136. !
  137. fn: aBlock
  138. self basicAt: 'fn' put: aBlock
  139. !
  140. messageSends
  141. ^self basicAt: 'messageSends'
  142. !
  143. methodClass
  144. ^self basicAt: 'methodClass'
  145. !
  146. protocol
  147. ^ self category
  148. !
  149. referencedClasses
  150. ^self basicAt: 'referencedClasses'
  151. !
  152. selector
  153. ^self basicAt: 'selector'
  154. !
  155. selector: aString
  156. self basicAt: 'selector' put: aString
  157. !
  158. source
  159. ^(self basicAt: 'source') ifNil: ['']
  160. !
  161. source: aString
  162. self basicAt: 'source' put: aString
  163. ! !
  164. Object subclass: #ForkPool
  165. instanceVariableNames: 'poolSize maxPoolSize queue worker'
  166. package: 'Kernel-Methods'!
  167. !ForkPool methodsFor: 'action'!
  168. addWorker
  169. worker valueWithTimeout: 0.
  170. poolSize := poolSize + 1
  171. !
  172. fork: aBlock
  173. poolSize < maxPoolSize ifTrue: [ self addWorker ].
  174. queue back: aBlock
  175. ! !
  176. !ForkPool methodsFor: 'initialization'!
  177. initialize
  178. super initialize.
  179. poolSize := 0.
  180. maxPoolSize := self class defaultMaxPoolSize.
  181. queue := Queue new.
  182. worker := self makeWorker
  183. !
  184. makeWorker
  185. | sentinel |
  186. sentinel := Object new.
  187. ^[ | block |
  188. poolSize := poolSize - 1.
  189. block := queue frontIfAbsent: [ sentinel ].
  190. block == sentinel ifFalse: [
  191. [ block value ] ensure: [ self addWorker ]]]
  192. ! !
  193. ForkPool class instanceVariableNames: 'default'!
  194. !ForkPool class methodsFor: 'accessing'!
  195. default
  196. ^default ifNil: [ default := self new ]
  197. !
  198. defaultMaxPoolSize
  199. ^100
  200. !
  201. resetDefault
  202. default := nil
  203. ! !
  204. Object subclass: #Message
  205. instanceVariableNames: 'selector arguments'
  206. package: 'Kernel-Methods'!
  207. !Message commentStamp!
  208. Generally, the system does not use instances of Message for efficiency reasons.
  209. 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.
  210. This instance is sent it as an argument with the message `doesNotUnderstand:` to the receiver.
  211. See boot.js, `messageNotUnderstood` and its counterpart `Object>>doesNotUnderstand:`!
  212. !Message methodsFor: 'accessing'!
  213. arguments
  214. ^arguments
  215. !
  216. arguments: anArray
  217. arguments := anArray
  218. !
  219. selector
  220. ^selector
  221. !
  222. selector: aString
  223. selector := aString
  224. ! !
  225. !Message methodsFor: 'printing'!
  226. printString
  227. ^ String streamContents: [:aStream|
  228. aStream
  229. nextPutAll: super printString;
  230. nextPutAll: '(';
  231. nextPutAll: selector;
  232. nextPutAll: ')' ]
  233. !
  234. sendTo: anObject
  235. ^ Smalltalk current send: self selector to: anObject arguments: self arguments
  236. ! !
  237. !Message class methodsFor: 'instance creation'!
  238. selector: aString arguments: anArray
  239. ^self new
  240. selector: aString;
  241. arguments: anArray;
  242. yourself
  243. ! !
  244. Object subclass: #MethodContext
  245. instanceVariableNames: ''
  246. package: 'Kernel-Methods'!
  247. !MethodContext commentStamp!
  248. 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.
  249. MethodContext instances are JavaScript `SmalltalkMethodContext` objects defined in boot.js
  250. Current limitation: MethodContext instances are not created on Block evaluation. That means it's actually impossible to debug inside a Block.!
  251. !MethodContext methodsFor: 'accessing'!
  252. asString
  253. ^self receiver class printString, ' >> ', self selector
  254. !
  255. home
  256. <return self.homeContext>
  257. !
  258. pc
  259. <return self.pc>
  260. !
  261. printString
  262. ^super printString, '(', self asString, ')'
  263. !
  264. receiver
  265. <return self.receiver>
  266. !
  267. selector
  268. <return smalltalk.convertSelector(self.selector)>
  269. !
  270. temps
  271. <return self.temps>
  272. ! !