Kernel-Methods.st 11 KB

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