Kernel-Methods.st 10 KB

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