Kernel-Methods.st 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. | success |
  46. success := false.
  47. ^[self value. success := true. aBlock value]
  48. on: Error
  49. do: [:ex |
  50. success ifFalse: [aBlock value].
  51. ex signal]
  52. !
  53. new
  54. "Use the receiver as a JS constructor.
  55. *Do not* use this method to instanciate Smalltalk objects!!"
  56. <return new self()>
  57. !
  58. newValue: anObject
  59. "Use the receiver as a JS constructor.
  60. *Do not* use this method to instanciate Smalltalk objects!!"
  61. <return new self(anObject)>
  62. !
  63. newValue: anObject value: anObject2
  64. "Use the receiver as a JS constructor.
  65. *Do not* use this method to instanciate Smalltalk objects!!"
  66. <return new self(anObject, anObject2)>
  67. !
  68. newValue: anObject value: anObject2 value: anObject3
  69. "Use the receiver as a JS constructor.
  70. *Do not* use this method to instanciate Smalltalk objects!!"
  71. <return new self(anObject, anObject2)>
  72. !
  73. timeToRun
  74. "Answer the number of milliseconds taken to execute this block."
  75. ^ Date millisecondsToRun: self
  76. !
  77. value
  78. "inlined in the Compiler"
  79. <return self();>
  80. !
  81. value: anArg
  82. "inlined in the Compiler"
  83. <return self(anArg);>
  84. !
  85. value: firstArg value: secondArg
  86. "inlined in the Compiler"
  87. <return self(firstArg, secondArg);>
  88. !
  89. value: firstArg value: secondArg value: thirdArg
  90. "inlined in the Compiler"
  91. <return self(firstArg, secondArg, thirdArg);>
  92. !
  93. valueWithPossibleArguments: aCollection
  94. <return self.apply(null, aCollection);>
  95. ! !
  96. !BlockClosure methodsFor: 'timeout/interval'!
  97. fork
  98. ForkPool default fork: self
  99. !
  100. valueWithInterval: aNumber
  101. <return setInterval(self, aNumber)>
  102. !
  103. valueWithTimeout: aNumber
  104. <return setTimeout(self, aNumber)>
  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 methodsFor: 'action'!
  170. addWorker
  171. worker valueWithTimeout: 0.
  172. poolSize := poolSize + 1
  173. !
  174. fork: aBlock
  175. poolSize < maxPoolSize ifTrue: [ self addWorker ].
  176. queue back: aBlock
  177. ! !
  178. !ForkPool methodsFor: 'initialization'!
  179. initialize
  180. poolSize := 0.
  181. maxPoolSize := self class defaultMaxPoolSize.
  182. queue := Queue new.
  183. worker := [
  184. | block |
  185. poolSize := poolSize - 1.
  186. block := queue frontIfAbsent: [ ^nil ].
  187. [ block value ] ensure: [ self addWorker ]
  188. ].
  189. ! !
  190. ForkPool class instanceVariableNames: 'default'!
  191. !ForkPool class methodsFor: 'accessing'!
  192. default
  193. ^default ifNil: [ default := self new ]
  194. !
  195. defaultMaxPoolSize
  196. ^100
  197. ! !
  198. Object subclass: #Message
  199. instanceVariableNames: 'selector arguments'
  200. package: 'Kernel-Methods'!
  201. !Message commentStamp!
  202. Generally, the system does not use instances of Message for efficiency reasons.
  203. 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.
  204. This instance is sent it as an argument with the message `doesNotUnderstand:` to the receiver.
  205. See boot.js, `messageNotUnderstood` and its counterpart `Object>>doesNotUnderstand:`!
  206. !Message methodsFor: 'accessing'!
  207. arguments
  208. ^arguments
  209. !
  210. arguments: anArray
  211. arguments := anArray
  212. !
  213. selector
  214. ^selector
  215. !
  216. selector: aString
  217. selector := aString
  218. ! !
  219. !Message methodsFor: 'printing'!
  220. printString
  221. ^ String streamContents: [:aStream|
  222. aStream
  223. nextPutAll: super printString;
  224. nextPutAll: '(';
  225. nextPutAll: selector;
  226. nextPutAll: ')' ]
  227. !
  228. sendTo: anObject
  229. ^ Smalltalk current send: self selector to: anObject arguments: self arguments
  230. ! !
  231. !Message class methodsFor: 'instance creation'!
  232. selector: aString arguments: anArray
  233. ^self new
  234. selector: aString;
  235. arguments: anArray;
  236. yourself
  237. ! !
  238. Object subclass: #MethodContext
  239. instanceVariableNames: ''
  240. package: 'Kernel-Methods'!
  241. !MethodContext commentStamp!
  242. 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.
  243. MethodContext instances are JavaScript `SmalltalkMethodContext` objects defined in boot.js
  244. Current limitation: MethodContext instances are not created on Block evaluation. That means it's actually impossible to debug inside a Block.!
  245. !MethodContext methodsFor: 'accessing'!
  246. asString
  247. ^self receiver class printString, ' >> ', self selector
  248. !
  249. home
  250. <return self.homeContext>
  251. !
  252. pc
  253. <return self.pc>
  254. !
  255. printString
  256. ^super printString, '(', self asString, ')'
  257. !
  258. receiver
  259. <return self.receiver>
  260. !
  261. selector
  262. <return smalltalk.convertSelector(self.selector)>
  263. !
  264. temps
  265. <return self.temps>
  266. ! !