1
0

Kernel-Methods.st 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. Smalltalk current createPackage: 'Kernel-Methods'!
  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: [ self defaultCategory ]
  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. !CompiledMethod methodsFor: 'defaults'!
  187. defaultCategory
  188. ^ 'as yet unclassified'
  189. ! !
  190. Object subclass: #ForkPool
  191. instanceVariableNames: 'poolSize maxPoolSize queue worker'
  192. package: 'Kernel-Methods'!
  193. !ForkPool commentStamp!
  194. A ForkPool is responsible for handling forked blocks.
  195. The pool size sets the maximum concurrent forked blocks.
  196. The default instance is accessed with `ForkPool default`!
  197. !ForkPool methodsFor: 'accessing'!
  198. maxPoolSize
  199. ^ maxPoolSize ifNil: [ self defaultMaxPoolSize ]
  200. !
  201. maxPoolSize: anInteger
  202. maxPoolSize := anInteger
  203. ! !
  204. !ForkPool methodsFor: 'actions'!
  205. fork: aBlock
  206. poolSize < self maxPoolSize ifTrue: [ self addWorker ].
  207. queue back: aBlock
  208. ! !
  209. !ForkPool methodsFor: 'defaults'!
  210. defaultMaxPoolSize
  211. ^ self class defaultMaxPoolSize
  212. ! !
  213. !ForkPool methodsFor: 'initialization'!
  214. initialize
  215. super initialize.
  216. poolSize := 0.
  217. queue := Queue new.
  218. worker := self makeWorker
  219. !
  220. makeWorker
  221. | sentinel |
  222. sentinel := Object new.
  223. ^[ | block |
  224. poolSize := poolSize - 1.
  225. block := queue frontIfAbsent: [ sentinel ].
  226. block == sentinel ifFalse: [
  227. [ block value ] ensure: [ self addWorker ]]]
  228. ! !
  229. !ForkPool methodsFor: 'private'!
  230. addWorker
  231. worker valueWithTimeout: 0.
  232. poolSize := poolSize + 1
  233. ! !
  234. ForkPool class instanceVariableNames: 'default'!
  235. !ForkPool class methodsFor: 'accessing'!
  236. default
  237. ^default ifNil: [ default := self new ]
  238. !
  239. defaultMaxPoolSize
  240. ^100
  241. !
  242. resetDefault
  243. default := nil
  244. ! !
  245. Object subclass: #Message
  246. instanceVariableNames: 'selector arguments'
  247. package: 'Kernel-Methods'!
  248. !Message commentStamp!
  249. Generally, the system does not use instances of Message for efficiency reasons.
  250. 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.
  251. This instance is sent it as an argument with the message `doesNotUnderstand:` to the receiver.
  252. See boot.js, `messageNotUnderstood` and its counterpart `Object>>doesNotUnderstand:`!
  253. !Message methodsFor: 'accessing'!
  254. arguments
  255. ^arguments
  256. !
  257. arguments: anArray
  258. arguments := anArray
  259. !
  260. selector
  261. ^selector
  262. !
  263. selector: aString
  264. selector := aString
  265. ! !
  266. !Message methodsFor: 'actions'!
  267. sendTo: anObject
  268. ^ anObject perform: self selector withArguments: self arguments
  269. ! !
  270. !Message methodsFor: 'printing'!
  271. printOn: aStream
  272. super printOn: aStream.
  273. aStream
  274. nextPutAll: '(';
  275. nextPutAll: self selector;
  276. nextPutAll: ')'
  277. ! !
  278. !Message class methodsFor: 'instance creation'!
  279. selector: aString arguments: anArray
  280. ^self new
  281. selector: aString;
  282. arguments: anArray;
  283. yourself
  284. ! !
  285. Object subclass: #MethodContext
  286. instanceVariableNames: ''
  287. package: 'Kernel-Methods'!
  288. !MethodContext commentStamp!
  289. 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.
  290. MethodContext instances are JavaScript `SmalltalkMethodContext` objects defined in boot.js!
  291. !MethodContext methodsFor: 'accessing'!
  292. home
  293. <return self.methodContext || self.homeContext>
  294. !
  295. locals
  296. <return self.locals>
  297. !
  298. method
  299. ^self methodContext receiver class lookupSelector: self methodContext selector
  300. !
  301. methodContext
  302. self isBlockContext ifFalse: [ ^ self ].
  303. ^ self home
  304. !
  305. outerContext
  306. <return self.homeContext>
  307. !
  308. pc
  309. <return self.pc>
  310. !
  311. receiver
  312. <return self.receiver>
  313. !
  314. selector
  315. <
  316. if(self.selector) {
  317. return smalltalk.convertSelector(self.selector);
  318. } else {
  319. return nil;
  320. }
  321. >
  322. !
  323. temps
  324. self deprecatedAPI.
  325. ^ self locals
  326. ! !
  327. !MethodContext methodsFor: 'converting'!
  328. asString
  329. ^self isBlockContext
  330. ifTrue: [ 'a block (in ', self methodContext receiver class printString, ')' ]
  331. ifFalse: [ self receiver class printString, ' >> ', self selector ]
  332. ! !
  333. !MethodContext methodsFor: 'printing'!
  334. printOn: aStream
  335. super printOn: aStream.
  336. aStream
  337. nextPutAll: '(';
  338. nextPutAll: self asString;
  339. nextPutAll: ')'
  340. ! !
  341. !MethodContext methodsFor: 'testing'!
  342. isBlockContext
  343. "Block context do not have selectors."
  344. ^ self selector isNil
  345. ! !
  346. Object subclass: #NativeFunction
  347. instanceVariableNames: ''
  348. package: 'Kernel-Methods'!
  349. !NativeFunction commentStamp!
  350. NativeFunction is a wrapper around native functions, such as `WebSocket`.
  351. For 'normal' functions (whose constructor is the JavaScript `Function` object), use `BlockClosure`.
  352. See the class-side `instance creation` methods.
  353. Created instances will most probably be instance of `JSObjectProxy`.
  354. Usage example:
  355. | ws |
  356. ws := NativeFunction constructor: 'WebSocket' value: 'ws://localhost'.
  357. ws at: 'onopen' put: [ ws send: 'hey there from Amber' ]!
  358. !NativeFunction class methodsFor: 'instance creation'!
  359. constructor: aString
  360. <
  361. var native=eval(aString);
  362. return new native();
  363. >
  364. !
  365. constructor: aString value:anObject
  366. <
  367. var native=eval(aString);
  368. return new native(anObject);
  369. >
  370. !
  371. constructor: aString value:anObject value: anObject2
  372. <
  373. var native=eval(aString);
  374. return new native(anObject,anObject2);
  375. >
  376. !
  377. constructor: aString value:anObject value: anObject2 value:anObject3
  378. <
  379. var native=eval(aString);
  380. return new native(anObject,anObject2, anObject3);
  381. >
  382. ! !
  383. !NativeFunction class methodsFor: 'testing'!
  384. exists: aString
  385. <
  386. if(aString in window) {
  387. return true
  388. } else {
  389. return false
  390. }
  391. >
  392. ! !