1
0

Kernel-Methods.st 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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: 'converting'!
  34. asCompiledMethod: aString
  35. <return smalltalk.method({selector:aString, fn:self});>
  36. !
  37. currySelf
  38. "Transforms [ :selfarg :x :y | stcode ] block
  39. which represents JS function (selfarg, x, y, ...) {jscode}
  40. into function (x, y, ...) {jscode} that takes selfarg from 'this'.
  41. IOW, it is usable as JS method and first arg takes the receiver."
  42. <
  43. return function () {
  44. var args = [ this ];
  45. args.push.apply(args, arguments);
  46. return self.apply(null, args);
  47. }
  48. >
  49. ! !
  50. !BlockClosure methodsFor: 'error handling'!
  51. on: anErrorClass do: aBlock
  52. "All exceptions thrown in the Smalltalk stack are cought.
  53. Convert all JS exceptions to JavaScriptException instances."
  54. ^self try: self catch: [ :error | | smalltalkError |
  55. smalltalkError := Smalltalk current asSmalltalkException: error.
  56. (smalltalkError isKindOf: anErrorClass)
  57. ifTrue: [ aBlock value: smalltalkError ]
  58. ifFalse: [ smalltalkError signal ] ]
  59. ! !
  60. !BlockClosure methodsFor: 'evaluating'!
  61. applyTo: anObject arguments: aCollection
  62. <return self.apply(anObject, aCollection)>
  63. !
  64. ensure: aBlock
  65. <try{return self()}finally{aBlock._value()}>
  66. !
  67. new
  68. "Use the receiver as a JS constructor.
  69. *Do not* use this method to instanciate Smalltalk objects!!"
  70. <return new self()>
  71. !
  72. newValue: anObject
  73. "Use the receiver as a JS constructor.
  74. *Do not* use this method to instanciate Smalltalk objects!!"
  75. <return new self(anObject)>
  76. !
  77. newValue: anObject value: anObject2
  78. "Use the receiver as a JS constructor.
  79. *Do not* use this method to instanciate Smalltalk objects!!"
  80. <return new self(anObject, anObject2)>
  81. !
  82. newValue: anObject value: anObject2 value: anObject3
  83. "Use the receiver as a JS constructor.
  84. *Do not* use this method to instanciate Smalltalk objects!!"
  85. <return new self(anObject, anObject2,anObject3)>
  86. !
  87. timeToRun
  88. "Answer the number of milliseconds taken to execute this block."
  89. ^ Date millisecondsToRun: self
  90. !
  91. value
  92. "inlined in the Compiler"
  93. <return self();>
  94. !
  95. value: anArg
  96. "inlined in the Compiler"
  97. <return self(anArg);>
  98. !
  99. value: firstArg value: secondArg
  100. "inlined in the Compiler"
  101. <return self(firstArg, secondArg);>
  102. !
  103. value: firstArg value: secondArg value: thirdArg
  104. "inlined in the Compiler"
  105. <return self(firstArg, secondArg, thirdArg);>
  106. !
  107. valueWithPossibleArguments: aCollection
  108. <return self.apply(null, aCollection);>
  109. ! !
  110. !BlockClosure methodsFor: 'timeout/interval'!
  111. fork
  112. ForkPool default fork: self
  113. !
  114. valueWithInterval: aNumber
  115. <
  116. var interval = setInterval(self, aNumber);
  117. return smalltalk.Timeout._on_(interval);
  118. >
  119. !
  120. valueWithTimeout: aNumber
  121. <
  122. var timeout = setTimeout(self, aNumber);
  123. return smalltalk.Timeout._on_(timeout);
  124. >
  125. ! !
  126. Object subclass: #CompiledMethod
  127. instanceVariableNames: ''
  128. package: 'Kernel-Methods'!
  129. !CompiledMethod commentStamp!
  130. CompiledMethod hold the source and compiled code of a class method.
  131. You can get a CompiledMethod using `Behavior>>methodAt:`
  132. String methodAt: 'lines'
  133. and read the source code
  134. (String methodAt: 'lines') source
  135. See referenced classes:
  136. (String methodAt: 'lines') referencedClasses
  137. or messages sent from this method:
  138. (String methodAt: 'lines') messageSends!
  139. !CompiledMethod methodsFor: 'accessing'!
  140. arguments
  141. <return self.args || []>
  142. !
  143. category
  144. ^(self basicAt: 'category') ifNil: ['']
  145. !
  146. category: aString
  147. | oldCategory |
  148. oldCategory := self category.
  149. self basicAt: 'category' put: aString.
  150. self methodClass ifNotNil: [
  151. self methodClass organization addElement: aString.
  152. (self methodClass methods
  153. select: [ :each | each category = oldCategory ])
  154. ifEmpty: [ self methodClass organization removeElement: oldCategory ] ]
  155. !
  156. fn
  157. ^self basicAt: 'fn'
  158. !
  159. fn: aBlock
  160. self basicAt: 'fn' put: aBlock
  161. !
  162. messageSends
  163. ^self basicAt: 'messageSends'
  164. !
  165. methodClass
  166. ^self basicAt: 'methodClass'
  167. !
  168. protocol
  169. ^ self category
  170. !
  171. referencedClasses
  172. ^self basicAt: 'referencedClasses'
  173. !
  174. selector
  175. ^self basicAt: 'selector'
  176. !
  177. selector: aString
  178. self basicAt: 'selector' put: aString
  179. !
  180. source
  181. ^(self basicAt: 'source') ifNil: ['']
  182. !
  183. source: aString
  184. self basicAt: 'source' put: aString
  185. ! !
  186. Object subclass: #ForkPool
  187. instanceVariableNames: 'poolSize maxPoolSize queue worker'
  188. package: 'Kernel-Methods'!
  189. !ForkPool commentStamp!
  190. A ForkPool is responsible for handling forked blocks.
  191. The pool size sets the maximum concurrent forked blocks.
  192. The default instance is accessed with `ForkPool default`!
  193. !ForkPool methodsFor: 'accessing'!
  194. maxPoolSize
  195. ^ maxPoolSize ifNil: [ self defaultMaxPoolSize ]
  196. !
  197. maxPoolSize: anInteger
  198. maxPoolSize := anInteger
  199. ! !
  200. !ForkPool methodsFor: 'actions'!
  201. fork: aBlock
  202. poolSize < self maxPoolSize ifTrue: [ self addWorker ].
  203. queue back: aBlock
  204. ! !
  205. !ForkPool methodsFor: 'defaults'!
  206. defaultMaxPoolSize
  207. ^ self class defaultMaxPoolSize
  208. ! !
  209. !ForkPool methodsFor: 'initialization'!
  210. initialize
  211. super initialize.
  212. poolSize := 0.
  213. queue := Queue new.
  214. worker := self makeWorker
  215. !
  216. makeWorker
  217. | sentinel |
  218. sentinel := Object new.
  219. ^[ | block |
  220. poolSize := poolSize - 1.
  221. block := queue frontIfAbsent: [ sentinel ].
  222. block == sentinel ifFalse: [
  223. [ block value ] ensure: [ self addWorker ]]]
  224. ! !
  225. !ForkPool methodsFor: 'private'!
  226. addWorker
  227. worker valueWithTimeout: 0.
  228. poolSize := poolSize + 1
  229. ! !
  230. ForkPool class instanceVariableNames: 'default'!
  231. !ForkPool class methodsFor: 'accessing'!
  232. default
  233. ^default ifNil: [ default := self new ]
  234. !
  235. defaultMaxPoolSize
  236. ^100
  237. !
  238. resetDefault
  239. default := nil
  240. ! !
  241. Object subclass: #Message
  242. instanceVariableNames: 'selector arguments'
  243. package: 'Kernel-Methods'!
  244. !Message commentStamp!
  245. Generally, the system does not use instances of Message for efficiency reasons.
  246. 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.
  247. This instance is sent it as an argument with the message `doesNotUnderstand:` to the receiver.
  248. See boot.js, `messageNotUnderstood` and its counterpart `Object>>doesNotUnderstand:`!
  249. !Message methodsFor: 'accessing'!
  250. arguments
  251. ^arguments
  252. !
  253. arguments: anArray
  254. arguments := anArray
  255. !
  256. selector
  257. ^selector
  258. !
  259. selector: aString
  260. selector := aString
  261. ! !
  262. !Message methodsFor: 'printing'!
  263. printString
  264. ^ String streamContents: [:aStream|
  265. aStream
  266. nextPutAll: super printString;
  267. nextPutAll: '(';
  268. nextPutAll: selector;
  269. nextPutAll: ')' ]
  270. !
  271. sendTo: anObject
  272. ^ anObject perform: self selector withArguments: self arguments
  273. ! !
  274. !Message class methodsFor: 'instance creation'!
  275. selector: aString arguments: anArray
  276. ^self new
  277. selector: aString;
  278. arguments: anArray;
  279. yourself
  280. ! !
  281. Object subclass: #MethodContext
  282. instanceVariableNames: ''
  283. package: 'Kernel-Methods'!
  284. !MethodContext commentStamp!
  285. 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.
  286. MethodContext instances are JavaScript `SmalltalkMethodContext` objects defined in boot.js!
  287. !MethodContext methodsFor: 'accessing'!
  288. home
  289. <return self.methodContext || self.homeContext>
  290. !
  291. locals
  292. <return self.locals>
  293. !
  294. method
  295. ^self methodContext receiver class lookupSelector: self methodContext selector
  296. !
  297. methodContext
  298. self isBlockContext ifFalse: [ ^ self ].
  299. ^ self home
  300. !
  301. outerContext
  302. <return self.homeContext>
  303. !
  304. pc
  305. <return self.pc>
  306. !
  307. printString
  308. ^super printString, '(', self asString, ')'
  309. !
  310. receiver
  311. <return self.receiver>
  312. !
  313. selector
  314. <
  315. if(self.selector) {
  316. return smalltalk.convertSelector(self.selector);
  317. } else {
  318. return nil;
  319. }
  320. >
  321. !
  322. temps
  323. self deprecatedAPI.
  324. ^ self locals
  325. ! !
  326. !MethodContext methodsFor: 'converting'!
  327. asString
  328. ^self isBlockContext
  329. ifTrue: [ 'a block (in ', self methodContext receiver class printString, ')' ]
  330. ifFalse: [ self receiver class printString, ' >> ', self selector ]
  331. ! !
  332. !MethodContext methodsFor: 'testing'!
  333. isBlockContext
  334. "Block context do not have selectors."
  335. ^ self selector isNil
  336. ! !
  337. Object subclass: #NativeFunction
  338. instanceVariableNames: ''
  339. package: 'Kernel-Methods'!
  340. !NativeFunction commentStamp!
  341. NativeFunction is a wrapper around native functions, such as `WebSocket`.
  342. For 'normal' functions (whose constructor is the JavaScript `Function` object), use `BlockClosure`.
  343. See the class-side `instance creation` methods.
  344. Created instances will most probably be instance of `JSObjectProxy`.
  345. Usage example:
  346. | ws |
  347. ws := NativeFunction constructor: 'WebSocket' value: 'ws://localhost'.
  348. ws at: 'onopen' put: [ ws send: 'hey there from Amber' ]!
  349. !NativeFunction class methodsFor: 'instance creation'!
  350. constructor: aString
  351. <
  352. var native=eval(aString);
  353. return new native();
  354. >
  355. !
  356. constructor: aString value:anObject
  357. <
  358. var native=eval(aString);
  359. return new native(anObject);
  360. >
  361. !
  362. constructor: aString value:anObject value: anObject2
  363. <
  364. var native=eval(aString);
  365. return new native(anObject,anObject2);
  366. >
  367. !
  368. constructor: aString value:anObject value: anObject2 value:anObject3
  369. <
  370. var native=eval(aString);
  371. return new native(anObject,anObject2, anObject3);
  372. >
  373. ! !
  374. !NativeFunction class methodsFor: 'testing'!
  375. exists: aString
  376. <
  377. if(aString in window) {
  378. return true
  379. } else {
  380. return false
  381. }
  382. >
  383. ! !