Compiler-Tests.st 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  1. Smalltalk createPackage: 'Compiler-Tests'!
  2. TestCase subclass: #ASTMethodRunningTest
  3. slots: {#receiver. #arguments}
  4. package: 'Compiler-Tests'!
  5. !ASTMethodRunningTest methodsFor: 'accessing'!
  6. arguments
  7. ^ arguments
  8. !
  9. receiver
  10. ^ receiver
  11. ! !
  12. !ASTMethodRunningTest methodsFor: 'initialization'!
  13. setUp
  14. arguments := #().
  15. receiver := DoIt new
  16. ! !
  17. !ASTMethodRunningTest methodsFor: 'testing'!
  18. should: aString class: aClass receiver: anObject return: aResult
  19. receiver := anObject.
  20. self while: aString inClass: aClass should: [ :runBlock |
  21. self assert: runBlock value equals: aResult ]
  22. !
  23. should: aString receiver: anObject raise: anErrorClass
  24. receiver := anObject.
  25. self while: aString should: [ :runBlock |
  26. self should: runBlock raise: anErrorClass ]
  27. !
  28. should: aString receiver: anObject return: aResult
  29. receiver := anObject.
  30. self should: aString return: aResult
  31. !
  32. should: aString return: anObject
  33. self while: aString should: [ :runBlock |
  34. self assert: runBlock value equals: anObject ]
  35. ! !
  36. ASTMethodRunningTest subclass: #AbstractCompilerTest
  37. slots: {}
  38. package: 'Compiler-Tests'!
  39. !AbstractCompilerTest methodsFor: 'tests'!
  40. testAfterInliningNonLocalBlockReturnIndexSend
  41. self should: 'foo [ ^ true ifTrue: [ self class ] ] value. self class' return: DoIt.
  42. !
  43. testAfterInliningNonLocalBlockReturnSuperSend
  44. self should: 'foo [ ^ true ifTrue: [ super class ] ] value' return: DoIt.
  45. !
  46. testAssignment
  47. self should: 'foo | a | a := true ifTrue: [ 1 ]. ^ a' return: 1.
  48. self should: 'foo | a | a := false ifTrue: [ 1 ]. ^ a' return: nil.
  49. self should: 'foo | a | ^ a := true ifTrue: [ 1 ]' return: 1
  50. !
  51. testBackslashSelectors
  52. self should: '\ arg ^ 4' return: 4.
  53. self should: '\\ arg ^ 42' return: 42
  54. !
  55. testBlockReturn
  56. self should: 'foo ^ #(1 2 3) collect: [ :each | true ifTrue: [ each + 1 ] ]' return: #(2 3 4).
  57. self should: 'foo ^ #(1 2 3) collect: [ :each | false ifFalse: [ each + 1 ] ]' return: #(2 3 4).
  58. self should: 'foo ^ #(1 2 3) collect: [ :each | each odd ifTrue: [ each + 1 ] ifFalse: [ each - 1 ] ]' return: #(2 1 4).
  59. !
  60. testCascades
  61. self should: 'foo ^ Array new add: 3; add: 4; yourself' return: #(3 4)
  62. !
  63. testCascadesInDynamicArray
  64. self should: 'foo | x | x := 1. ^ {x. [x:=2] value; in: [x]}' return: #(1 2)
  65. !
  66. testCascadesInDynamicDictioary
  67. self should: 'foo | x | x := 1. ^ #{''one'' -> x. ''two'' -> ([x:=2] value; in: [x])}' return: #{'one' -> 1. 'two' -> 2}
  68. !
  69. testCascadesInSend
  70. self should: 'foo | x | x := 1. ^ Array with: x with: ([x:=2] value; in: [x])' return: #(1 2)
  71. !
  72. testCascadesWithInlining
  73. self should: 'foo ^ true class; ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 1.
  74. self should: 'foo ^ false class; ifTrue: [ 1 ] ifFalse: [ 2 ]' return: 2
  75. !
  76. testDynamicArrayElementsOrdered
  77. self should: 'foo
  78. | x |
  79. x := 1.
  80. ^ { x. x := 2 }
  81. ' return: #(1 2).
  82. self should: 'foo
  83. | x |
  84. x := 1.
  85. ^ { x. true ifTrue: [ x := 2 ] }
  86. ' return: #(1 2).
  87. !
  88. testDynamicDictionaryElementsOrdered
  89. self should: 'foo
  90. | x |
  91. x := ''foo''.
  92. ^ #{ x->1. ''bar''->(true ifTrue: [ 2 ]) }
  93. ' return: #{'foo'->1. 'bar'->2}.
  94. !
  95. testDynamicDictionaryWithMoreArrows
  96. self should: 'foo ^ #{1->2->3}' return: (HashedCollection with: 1->2->3)
  97. !
  98. testGlobalVar
  99. self should: 'foo ^ eval class' return: BlockClosure.
  100. self should: 'foo ^ Math cos: 0' return: 1.
  101. self should: 'foo ^ NonExistingVar' return: nil
  102. !
  103. testInnerTemporalDependentElementsOrdered
  104. self should: 'foo
  105. | x |
  106. x := Array.
  107. ^ x with: ''foo''->x with: ''bar''->(x := 2)
  108. ' return: {'foo'->Array. 'bar'->2}.
  109. self should: 'foo
  110. | x |
  111. x := Array.
  112. ^ x with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  113. ' return: {'foo'->Array. 'bar'->2}.
  114. self should: 'foo
  115. | x |
  116. x := 1.
  117. ^ Array with: ''foo''->x with: ''bar''->(true ifTrue: [ x := 2 ])
  118. ' return: {'foo'->1. 'bar'->2}.
  119. self should: 'foo
  120. | x |
  121. x := 1.
  122. ^ { ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  123. ' return: {'foo'->1. 'bar'->2}.
  124. self should: 'foo
  125. | x |
  126. x := 1.
  127. ^ #{ ''foo''->x. ''bar''->(true ifTrue: [ x := 2 ]) }
  128. ' return: #{'foo'->1. 'bar'->2}.
  129. !
  130. testLexicalScope
  131. self should: 'foo | a | a := 1. [ a := 2 ] value. ^ a' return: 2
  132. !
  133. testLiterals
  134. self should: 'foo ^ 1' return: 1.
  135. self should: 'foo ^ ''hello''' return: 'hello'.
  136. self should: 'foo ^ #(1 2 3 4)' return: #(1 2 3 4).
  137. self should: 'foo ^ {1. [:x | x ] value: 2. 3. [4] value}' return: #(1 2 3 4).
  138. self should: 'foo ^ true' return: true.
  139. self should: 'foo ^ false' return: false.
  140. self should: 'foo ^ #{1->2. 3->4}' return: #{1->2. 3->4}.
  141. self should: 'foo ^ #hello' return: #hello.
  142. self should: 'foo ^ $h' return: 'h'.
  143. self should: 'foo ^ -123.456' return: -123.456.
  144. self should: 'foo ^ -2.5e4' return: -25000.
  145. !
  146. testLocalReturn
  147. self should: 'foo ^ 1' return: 1.
  148. self should: 'foo ^ 1 + 1' return: 2.
  149. self should: 'foo ' return: receiver.
  150. self should: 'foo self asString' return: receiver.
  151. self should: 'foo | a b | a := 1. b := 2. ^ a + b' return: 3
  152. !
  153. testMessageSends
  154. self should: 'foo ^ 1 asString' return: '1'.
  155. self should: 'foo ^ 1 + 1' return: 2.
  156. self should: 'foo ^ 1 + 2 * 3' return: 9.
  157. self should: 'foo ^ 1 to: 3' return: #(1 2 3).
  158. self should: 'foo ^ 1 to: 5 by: 2' return: #(1 3 5)
  159. !
  160. testMultipleSequences
  161. self should: 'foo | a b c | a := 2. b := 3. c := a + b. ^ c * 6' return: 30
  162. !
  163. testMutableLiterals
  164. "Mutable literals must be aliased in cascades.
  165. See https://lolg.it/amber/amber/issues/428"
  166. self
  167. should: 'foo ^ #( 1 2 ) at: 1 put: 3; yourself'
  168. return: #(3 2)
  169. !
  170. testNestedIfTrue
  171. self should: 'foo ^ true ifTrue: [ false ifFalse: [ 1 ] ]' return: 1.
  172. self should: 'foo ^ true ifTrue: [ false ifTrue: [ 1 ] ]' return: nil.
  173. self should: 'foo true ifTrue: [ false ifFalse: [ ^ 1 ] ]' return: 1.
  174. self should: 'foo true ifTrue: [ false ifTrue: [ ^ 1 ] ]' return: receiver.
  175. !
  176. testNestedSends
  177. self should: 'foo ^ (Point x: (Point x: 2 y: 3) y: 4) asString' return: (Point x: (2@3) y: 4) asString
  178. !
  179. testNilPerform
  180. self should: 'foo ^ nil perform: #yourself' return: nil
  181. !
  182. testNonLocalReturn
  183. self should: 'foo [ ^ 1 ] value' return: 1.
  184. self should: 'foo [ ^ 1 + 1 ] value' return: 2.
  185. self should: 'foo | a b | a := 1. b := 2. [ ^ a + b ] value. self halt' return: 3.
  186. self should: 'foo [ :x | ^ x + x ] value: 4. ^ 2' return: 8
  187. !
  188. testPascalCaseGlobal
  189. self should: 'foo ^Object' return: (Smalltalk globals at: 'Object').
  190. self should: 'foo ^NonExistent' return: nil
  191. !
  192. testPragmaJSStatement
  193. self should: 'foo < inlineJS: ''return 2+3'' >' return: 5
  194. !
  195. testPromiseWithAsyncExecutorAndLocalReturn
  196. self
  197. should: 'foo ^ Promise new: [ :m | [ 3 + 4 ] fork. ^ 5 ]'
  198. return: 5
  199. !
  200. testReceiverEvaluatedOnceInSpecials
  201. self should: 'foo |x| x := 1. ^ {[ x := x+1 ] value ifNil: []. x}' return: {2. 2}.
  202. self should: 'foo |xs| xs := {nil. nil}. ^ {[ xs removeLast ] value ifNotNil: []. xs}' return: {nil. {nil}}.
  203. !
  204. testRegression1242
  205. self should: '
  206. foo
  207. |x|
  208. x := 2.
  209. x := nil ifNil: [].
  210. ^ x
  211. ' return: nil.
  212. self should: '
  213. foo
  214. |x|
  215. x := 2.
  216. x := 1 ifNotNil: [].
  217. ^ x
  218. ' return: nil.
  219. self should: '
  220. foo
  221. |x|
  222. x := 2.
  223. x := false ifFalse: [].
  224. ^ x
  225. ' return: nil.
  226. self should: '
  227. foo
  228. |x|
  229. x := 2.
  230. x := true ifTrue: [].
  231. ^ x
  232. ' return: nil.
  233. !
  234. testRegression1242ForReturn
  235. self should: 'foo [ ^ nil ifNil: [] ] value' return: nil.
  236. self should: 'foo [ ^ 1 ifNotNil: [] ] value' return: nil.
  237. self should: 'foo [ ^ false ifFalse: [] ] value' return: nil.
  238. self should: 'foo [ ^ true ifTrue: [] ] value' return: nil.
  239. !
  240. testRegression1244
  241. self should: 'foo [ ^ true ifTrue: [1] ifFalse: [2] ] value' return: 1
  242. !
  243. testRootSuperSend
  244. self
  245. should: 'foo ^ super class'
  246. receiver: ProtoObject new
  247. raise: MessageNotUnderstood
  248. !
  249. testSendReceiverAndArgumentsOrdered
  250. self should: 'foo
  251. | x |
  252. x := 1.
  253. ^ Array with: x with: (true ifTrue: [ x := 2 ])
  254. ' return: #(1 2).
  255. self should: 'foo
  256. | x |
  257. x := Array.
  258. ^ x with: x with: (true ifTrue: [ x := 2 ])
  259. ' return: {Array. 2}.
  260. !
  261. testSuperSend
  262. self
  263. should: 'foo ^ super isBoolean'
  264. receiver: true
  265. return: false
  266. !
  267. testSuperSend2
  268. self
  269. should: 'foo ^ super isNil'
  270. receiver: nil
  271. return: false
  272. !
  273. testSuperSend3
  274. self
  275. should: 'doo ^ super isNil'
  276. class: Object
  277. receiver: nil
  278. return: false
  279. !
  280. testSuperSend4
  281. self
  282. should: 'foo ^ super asJavaScriptObject'
  283. receiver: 'me'
  284. return: #('m' 'e')
  285. !
  286. testSuperSend5
  287. self
  288. should: 'foo [super addLast: 4] on: Error do: [ self add: 5 ]. ^ self'
  289. class: SequenceableCollection
  290. receiver: #(1 2 3)
  291. return: #(1 2 3 5)
  292. !
  293. testSuperSend6
  294. self
  295. should: 'foo ^ super ifTrue: [ true ] ifFalse: [ false ]'
  296. receiver: true
  297. raise: Error
  298. !
  299. testTempVariables
  300. self should: 'foo | a | ^ a' return: nil.
  301. self should: 'foo | AVariable | ^ AVariable' return: nil.
  302. self should: 'foo | a b c | ^ c' return: nil.
  303. self should: 'foo | a | [ | d | ^ d ] value' return: nil.
  304. self should: 'foo | a | a:= 1. ^ a' return: 1.
  305. self should: 'foo | AVariable | AVariable := 1. ^ AVariable' return: 1.
  306. !
  307. testThisContext
  308. self should: 'foo ^ [ thisContext ] value outerContext == thisContext' return: true
  309. !
  310. testUnknownPragma
  311. self should: 'foo < fooBar: ''return 2+3'' > | x | ^ x := 6' return: 6.
  312. self should: 'foo | x | < fooBar: ''return 2+3'' > ^ x := 6' return: 6
  313. !
  314. testifFalse
  315. self should: 'foo true ifFalse: [ ^ 1 ]' return: receiver.
  316. self should: 'foo false ifFalse: [ ^ 2 ]' return: 2.
  317. self should: 'foo ^ true ifFalse: [ 1 ]' return: nil.
  318. self should: 'foo ^ false ifFalse: [ 2 ]' return: 2.
  319. !
  320. testifFalseIfTrue
  321. self should: 'foo true ifFalse: [ ^ 1 ] ifTrue: [ ^ 2 ]' return: 2.
  322. self should: 'foo false ifFalse: [ ^ 2 ] ifTrue: [ ^1 ]' return: 2.
  323. self should: 'foo ^ true ifFalse: [ 1 ] ifTrue: [ 2 ]' return: 2.
  324. self should: 'foo ^ false ifFalse: [ 2 ] ifTrue: [ 1 ]' return: 2.
  325. !
  326. testifNil
  327. self should: 'foo ^ 1 ifNil: [ 2 ]' return: 1.
  328. self should: 'foo ^ nil ifNil: [ 2 ]' return: 2.
  329. self should: 'foo 1 ifNil: [ ^ 2 ]' return: receiver.
  330. self should: 'foo nil ifNil: [ ^ 2 ]' return: 2.
  331. !
  332. testifNilIfNotNil
  333. self should: 'foo ^ 1 ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 3.
  334. self should: 'foo ^ nil ifNil: [ 2 ] ifNotNil: [ 3 ]' return: 2.
  335. self should: 'foo 1 ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 3.
  336. self should: 'foo nil ifNil: [ ^ 2 ] ifNotNil: [ ^3 ]' return: 2.
  337. !
  338. testifNotNil
  339. self should: 'foo ^ 1 ifNotNil: [ 2 ]' return: 2.
  340. self should: 'foo ^ nil ifNotNil: [ 2 ]' return: nil.
  341. self should: 'foo 1 ifNotNil: [ ^ 2 ]' return: 2.
  342. self should: 'foo nil ifNotNil: [ ^ 2 ]' return: receiver.
  343. !
  344. testifNotNilWithArgument
  345. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ]' return: 3.
  346. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ]' return: nil.
  347. self should: 'foo ^ 1 ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 3.
  348. self should: 'foo ^ nil ifNil: [ 5 ] ifNotNil: [ :val | val + 2 ]' return: 5.
  349. self should: 'foo ^ 1 ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 3.
  350. self should: 'foo ^ nil ifNotNil: [ :val | val + 2 ] ifNil: [ 5 ]' return: 5
  351. !
  352. testifTrue
  353. self should: 'foo false ifTrue: [ ^ 1 ]' return: receiver.
  354. self should: 'foo true ifTrue: [ ^ 2 ]' return: 2.
  355. self should: 'foo ^ false ifTrue: [ 1 ]' return: nil.
  356. self should: 'foo ^ true ifTrue: [ 2 ]' return: 2.
  357. !
  358. testifTrueIfFalse
  359. self should: 'foo false ifTrue: [ ^ 1 ] ifFalse: [ ^2 ]' return: 2.
  360. self should: 'foo true ifTrue: [ ^ 1 ] ifFalse: [ ^ 2 ]' return: 1.
  361. self should: 'foo ^ false ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 1.
  362. self should: 'foo ^ true ifTrue: [ 2 ] ifFalse: [ 1 ]' return: 2.
  363. ! !
  364. !AbstractCompilerTest class methodsFor: 'testing'!
  365. isAbstract
  366. ^ self name = AbstractCompilerTest name
  367. ! !
  368. AbstractCompilerTest subclass: #ASTDebuggerTest
  369. slots: {}
  370. package: 'Compiler-Tests'!
  371. AbstractCompilerTest subclass: #ASTInterpreterTest
  372. slots: {}
  373. package: 'Compiler-Tests'!
  374. AbstractCompilerTest subclass: #CodeGeneratorTest
  375. slots: {}
  376. package: 'Compiler-Tests'!
  377. AbstractCompilerTest subclass: #InliningCodeGeneratorTest
  378. slots: {}
  379. package: 'Compiler-Tests'!
  380. ASTMethodRunningTest subclass: #AbstractJavaScriptGatewayTest
  381. slots: {#theClass}
  382. package: 'Compiler-Tests'!
  383. !AbstractJavaScriptGatewayTest methodsFor: 'accessing'!
  384. theClass
  385. ^ theClass
  386. ! !
  387. !AbstractJavaScriptGatewayTest methodsFor: 'running'!
  388. jsConstructor
  389. <inlineJS: '
  390. var ctr = function () {};
  391. ctr.prototype.foo = function (a,b) {return a+","+b};
  392. return ctr;
  393. '>
  394. ! !
  395. !AbstractJavaScriptGatewayTest methodsFor: 'tests'!
  396. testDyadicSuperDifferentNames
  397. theClass := ObjectMock subclass: #ObjectMock2 slots: #() package: 'Compiler-Tests'.
  398. theClass beJavaScriptSubclassOf: self jsConstructor.
  399. receiver := ObjectMock2 new foo: 'should be shadowed'; yourself.
  400. arguments := #(4 true).
  401. self
  402. should: 'bar: anObject baz: anotherObject
  403. <jsOverride: #foo args: #(anObject anotherObject)>
  404. ^ super bar: anObject baz: anotherObject'
  405. return: '4,true'
  406. !
  407. testDyadicSuperDifferentNamesNested
  408. theClass := ObjectMock subclass: #ObjectMock2 slots: #() package: 'Compiler-Tests'.
  409. theClass beJavaScriptSubclassOf: self jsConstructor.
  410. receiver := ObjectMock2 new foo: 'should be shadowed'; yourself.
  411. arguments := #(4 true).
  412. self
  413. should: 'bar: anObject baz: anotherObject
  414. <jsOverride: #foo args: #(anObject anotherObject)>
  415. ^ [ super bar: anObject baz: anotherObject ] value'
  416. return: '4,true'
  417. !
  418. testDyadicSuperDifferentNamesPermutated
  419. theClass := ObjectMock subclass: #ObjectMock2 slots: #() package: 'Compiler-Tests'.
  420. theClass beJavaScriptSubclassOf: self jsConstructor.
  421. receiver := ObjectMock2 new foo: 'should be shadowed'; yourself.
  422. arguments := #(4 true).
  423. self
  424. should: 'bar: anObject baz: anotherObject
  425. <jsOverride: #foo args: #(anotherObject anObject)>
  426. ^ super bar: anObject baz: anotherObject'
  427. return: 'true,4'
  428. !
  429. testMonadicSuperDifferentNames
  430. theClass := ObjectMock subclass: #ObjectMock2 slots: #() package: 'Compiler-Tests'.
  431. theClass beJavaScriptSubclassOf: self jsConstructor.
  432. receiver := ObjectMock2 new foo: 'should be shadowed'; yourself.
  433. arguments := #(4).
  434. self
  435. should: 'bar: anObject <jsOverride: #foo args: #(anObject)> ^ super bar: anObject'
  436. return: '4,undefined'
  437. !
  438. testNiladicSuper
  439. theClass := ObjectMock subclass: #ObjectMock2 slots: #() package: 'Compiler-Tests'.
  440. theClass beJavaScriptSubclassOf: self jsConstructor.
  441. self
  442. should: 'foo <jsOverride: #foo> ^ super foo'
  443. receiver: (ObjectMock2 new foo: 'should be shadowed'; yourself)
  444. return: 'undefined,undefined'
  445. !
  446. testNiladicSuperDifferentNames
  447. theClass := ObjectMock subclass: #ObjectMock2 slots: #() package: 'Compiler-Tests'.
  448. theClass beJavaScriptSubclassOf: self jsConstructor.
  449. self
  450. should: 'bar <jsOverride: #foo> ^ super bar'
  451. receiver: (ObjectMock2 new foo: 'should be shadowed'; yourself)
  452. return: 'undefined,undefined'
  453. !
  454. testNiladicSuperNested
  455. theClass := ObjectMock subclass: #ObjectMock2 slots: #() package: 'Compiler-Tests'.
  456. theClass beJavaScriptSubclassOf: self jsConstructor.
  457. self
  458. should: 'foo <jsOverride: #foo> ^ [ super foo ] value'
  459. receiver: (ObjectMock2 new foo: 'should be shadowed'; yourself)
  460. return: 'undefined,undefined'
  461. !
  462. testTriadicSuperDifferentNamesPermutated
  463. theClass := ObjectMock subclass: #ObjectMock2 slots: #() package: 'Compiler-Tests'.
  464. theClass beJavaScriptSubclassOf: self jsConstructor.
  465. receiver := ObjectMock2 new foo: 'should be shadowed'; yourself.
  466. arguments := #(4 true 'hello').
  467. self
  468. should: 'bar: anObject baz: anotherObject moo: yao
  469. <jsOverride: #foo args: #(yao anObject anotherObject)>
  470. ^ super bar: anObject baz: anotherObject moo: yao'
  471. return: 'hello,4'
  472. ! !
  473. !AbstractJavaScriptGatewayTest class methodsFor: 'testing'!
  474. isAbstract
  475. ^ self name = AbstractJavaScriptGatewayTest name
  476. ! !
  477. AbstractJavaScriptGatewayTest subclass: #DebuggedJSGTest
  478. slots: {}
  479. package: 'Compiler-Tests'!
  480. AbstractJavaScriptGatewayTest subclass: #InlinedJSGTest
  481. slots: {}
  482. package: 'Compiler-Tests'!
  483. AbstractJavaScriptGatewayTest subclass: #InterpretedJSGTest
  484. slots: {}
  485. package: 'Compiler-Tests'!
  486. AbstractJavaScriptGatewayTest subclass: #PlainJSGTest
  487. slots: {}
  488. package: 'Compiler-Tests'!
  489. TestCase subclass: #ASTPCNodeVisitorTest
  490. slots: {}
  491. package: 'Compiler-Tests'!
  492. !ASTPCNodeVisitorTest methodsFor: 'factory'!
  493. astPCNodeVisitor
  494. ^ ASTPCNodeVisitor new
  495. index: 0;
  496. yourself
  497. !
  498. astPCNodeVisitorForSelector: aString
  499. ^ ASTPCNodeVisitor new
  500. selector: aString;
  501. index: 0;
  502. yourself
  503. !
  504. newTeachableVisitor
  505. | result |
  506. result := Teachable new
  507. whenSend: #visit: evaluate: [ :one | one acceptDagVisitor: result ];
  508. acceptSend: #visitDagNode:.
  509. ^ result
  510. ! !
  511. !ASTPCNodeVisitorTest methodsFor: 'tests'!
  512. testJSStatementNode
  513. | ast result |
  514. ast := self parse: 'foo <inlineJS: ''consolee.log(1)''>' forClass: Object.
  515. result := self astPCNodeVisitor visit: ast; currentNode.
  516. self
  517. assert: ((self newTeachableVisitor whenSend: #visitJSStatementNode: return: 'JS'; yourself) visit: result)
  518. equals: 'JS'
  519. !
  520. testMessageSend
  521. | ast |
  522. ast := self parse: 'foo self asString yourself. ^ self asBoolean' forClass: Object.
  523. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  524. visit: ast;
  525. currentNode) selector equals: 'yourself'
  526. !
  527. testMessageSendWithBlocks
  528. | ast |
  529. ast := self parse: 'foo true ifTrue: [ [ self asString yourself ] value. ]. ^ self asBoolean' forClass: Object.
  530. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  531. visit: ast;
  532. currentNode) selector equals: 'yourself'
  533. !
  534. testMessageSendWithInlining
  535. | ast |
  536. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  537. self assert: ((self astPCNodeVisitorForSelector: 'yourself')
  538. visit: ast;
  539. currentNode) selector equals: 'yourself'.
  540. ast := self parse: 'foo true ifTrue: [ self asString yourself ]. ^ self asBoolean' forClass: Object.
  541. self assert: ((self astPCNodeVisitorForSelector: 'asBoolean')
  542. visit: ast;
  543. currentNode) selector equals: 'asBoolean'
  544. !
  545. testNoMessageSend
  546. | ast |
  547. ast := self parse: 'foo ^ self' forClass: Object.
  548. self assert: (self astPCNodeVisitor
  549. visit: ast;
  550. currentNode) isNil
  551. ! !
  552. TestCase subclass: #ASTPositionTest
  553. slots: {}
  554. package: 'Compiler-Tests'!
  555. !ASTPositionTest methodsFor: 'tests'!
  556. testNodeAtPosition
  557. | node |
  558. node := self parse: 'yourself
  559. ^ self' forClass: Object.
  560. self assert: (node navigationNodeAt: 2@4 ifAbsent: [ nil ]) source equals: 'self'.
  561. node := self parse: 'foo
  562. true ifTrue: [ 1 ]' forClass: Object.
  563. self assert: (node navigationNodeAt: 2@7 ifAbsent: [ nil ]) selector equals: 'ifTrue:'.
  564. node := self parse: 'foo
  565. self foo; bar; baz' forClass: Object.
  566. self assert: (node navigationNodeAt: 2@8 ifAbsent: [ nil ]) selector equals: 'foo'
  567. ! !
  568. TestCase subclass: #AbstractCodeGeneratorInstallTest
  569. slots: {#receiver}
  570. package: 'Compiler-Tests'!
  571. !AbstractCodeGeneratorInstallTest methodsFor: 'accessing'!
  572. receiver
  573. ^ receiver
  574. ! !
  575. !AbstractCodeGeneratorInstallTest methodsFor: 'testing'!
  576. shouldntInstall: aString andRaise: anErrorClass
  577. | method |
  578. [ self
  579. should: [ method := self install: aString forClass: receiver class ]
  580. raise: anErrorClass ]
  581. ensure: [ method ifNotNil: [ receiver class removeCompiledMethod: method ] ]
  582. ! !
  583. !AbstractCodeGeneratorInstallTest methodsFor: 'tests'!
  584. testDyadicJSOverrideArgMismatch
  585. receiver := ObjectMock new.
  586. self
  587. shouldntInstall: 'quux: aNumber foo: anotherNumber
  588. <jsOverride: #mux args: #(anInteger anotherNumber)>
  589. ^ (foo := foo * aNumber + anotherNumber)'
  590. andRaise: CompilerError.
  591. self
  592. shouldntInstall: 'quux: aNumber foo: anotherNumber
  593. <jsOverride: #mux args: #(aNumber anotherInteger)>
  594. ^ (foo := foo * aNumber + anotherNumber)'
  595. andRaise: CompilerError.
  596. self
  597. shouldntInstall: 'quux: aNumber foo: anotherNumber
  598. <jsOverride: #mux args: #(anotherNumber anInteger)>
  599. ^ (foo := foo * aNumber + anotherNumber)'
  600. andRaise: CompilerError
  601. !
  602. testDyadicJSOverrideDifferentNames
  603. receiver := ObjectMock new.
  604. receiver foo: 4.
  605. self while: 'quux: anInteger foo: anotherInteger
  606. <jsOverride: #mux args: #(anInteger anotherInteger)>
  607. ^ (foo := foo * anInteger + anotherInteger)' should: [
  608. self should: [ receiver mux ] raise: MessageNotUnderstood.
  609. self should: [ receiver mux: 2 and: -1 ] raise: MessageNotUnderstood.
  610. self assert: (receiver basicPerform: #mux withArguments: #(2 -2)) equals: 6.
  611. self assert: (receiver quux: 1 foo: 4) equals: 10.
  612. self should: [ receiver basicPerform: #quux ] raise: Error.
  613. self assert: receiver foo equals: 10 ]
  614. !
  615. testDyadicJSOverrideDifferentNamesPermutated
  616. receiver := ObjectMock new.
  617. receiver foo: 4.
  618. self while: 'quux: anInteger foo: anotherInteger
  619. <jsOverride: #mux args: #(anotherInteger anInteger)>
  620. ^ (foo := foo * anInteger + anotherInteger)' should: [
  621. self should: [ receiver mux ] raise: MessageNotUnderstood.
  622. self should: [ receiver mux: 2 and: -1 ] raise: MessageNotUnderstood.
  623. self assert: (receiver basicPerform: #mux withArguments: #(-2 2)) equals: 6.
  624. self assert: (receiver quux: 1 foo: 4) equals: 10.
  625. self should: [ receiver basicPerform: #quux ] raise: Error.
  626. self assert: receiver foo equals: 10 ]
  627. !
  628. testDyadicJSOverrideInOneArg
  629. receiver := ObjectMock new.
  630. self
  631. shouldntInstall: 'quux: anInteger
  632. <jsOverride: #mux args: #(anInteger anotherInteger)>
  633. ^ (foo := foo + anInteger)'
  634. andRaise: CompilerError.
  635. self
  636. shouldntInstall: 'quux: anInteger
  637. <jsOverride: #mux args: #(anotherInteger anInteger)>
  638. ^ (foo := foo + anInteger)'
  639. andRaise: CompilerError
  640. !
  641. testDyadicJSOverrideInUnary
  642. receiver := ObjectMock new.
  643. self
  644. shouldntInstall: 'quux <jsOverride: #mux args: #(anInteger anotherInteger)> ^ (foo := foo + 3)'
  645. andRaise: CompilerError
  646. !
  647. testDyadicJSOverrideRepeatedArgs
  648. receiver := ObjectMock new.
  649. self
  650. shouldntInstall: 'quux: anInteger
  651. <jsOverride: #mux args: #(anInteger anInteger)>
  652. ^ (foo := foo + anInteger)'
  653. andRaise: CompilerError.
  654. self
  655. shouldntInstall: 'quux: anInteger foo: anotherInteger
  656. <jsOverride: #mux args: #(anInteger anInteger)>
  657. ^ (foo := foo * anInteger + anotherInteger)'
  658. andRaise: CompilerError
  659. !
  660. testInvalidAssignment
  661. self shouldntInstall: 'foo:a a:=1' andRaise: InvalidAssignmentError.
  662. self shouldntInstall: 'foo false:=1' andRaise: InvalidAssignmentError.
  663. self shouldntInstall: 'foo console:=1' andRaise: InvalidAssignmentError.
  664. self shouldntInstall: 'foo Number:=1' andRaise: InvalidAssignmentError
  665. !
  666. testMistypedPragmaJSStatement
  667. self shouldntInstall: 'foo < inlineJS: ''return ''foo'''' >' andRaise: ParseError
  668. !
  669. testMonadicJSOverrideArgMismatch
  670. receiver := ObjectMock new.
  671. self
  672. shouldntInstall: 'quux: aNumber <jsOverride: #mux args: #(anInteger)> ^ (foo := foo + aNumber)'
  673. andRaise: CompilerError
  674. !
  675. testMonadicJSOverrideDifferentNames
  676. receiver := ObjectMock new.
  677. receiver foo: 4.
  678. self while: 'quux: anInteger <jsOverride: #mux args: #(anInteger)> ^ (foo := foo + anInteger)' should: [
  679. self should: [ receiver mux ] raise: MessageNotUnderstood.
  680. self should: [ receiver mux: 2 ] raise: MessageNotUnderstood.
  681. self assert: (receiver basicPerform: #mux withArguments: #(2)) equals: 6.
  682. self assert: (receiver quux: 4) equals: 10.
  683. self should: [ receiver basicPerform: #quux ] raise: Error.
  684. self assert: receiver foo equals: 10 ]
  685. !
  686. testMonadicJSOverrideInUnary
  687. receiver := ObjectMock new.
  688. self
  689. shouldntInstall: 'quux <jsOverride: #mux args: #(anInteger)> ^ (foo := foo + 3)'
  690. andRaise: CompilerError
  691. !
  692. testNiladicJSOverride
  693. receiver := ObjectMock new.
  694. receiver foo: 4.
  695. self while: 'baz <jsOverride: #baz> ^ (foo := foo + 3)' should: [
  696. self assert: receiver baz equals: 7.
  697. self assert: (receiver basicPerform: #baz) equals: 10.
  698. self assert: receiver baz equals: 13.
  699. self assert: receiver foo equals: 13 ]
  700. !
  701. testNiladicJSOverrideDifferentNames
  702. receiver := ObjectMock new.
  703. receiver foo: 4.
  704. self while: 'quux <jsOverride: #mux> ^ (foo := foo + 3)' should: [
  705. self should: [ receiver mux ] raise: MessageNotUnderstood.
  706. self assert: (receiver basicPerform: #mux) equals: 7.
  707. self assert: receiver quux equals: 10.
  708. self should: [ receiver basicPerform: #quux ] raise: Error.
  709. self assert: receiver foo equals: 10 ]
  710. !
  711. testNiladicJSOverrideInOneArg
  712. receiver := ObjectMock new.
  713. self
  714. shouldntInstall: 'quux: anInteger <jsOverride: #mux> ^ (foo := foo + anInteger)'
  715. andRaise: CompilerError
  716. !
  717. testPragmaInBlock
  718. self shouldntInstall: 'foo ^ [ < fooBar > 4 ] value' andRaise: ParseError
  719. !
  720. testTriadicJSOverrideDifferentNamesPermutated
  721. receiver := ObjectMock new.
  722. receiver foo: 4.
  723. self while: 'quux: anInteger foo: anotherInteger bar: yaInt
  724. <jsOverride: #mux args: #(yaInt anInteger anotherInteger)>
  725. ^ (foo := foo * anInteger + anotherInteger - yaInt)' should: [
  726. self should: [ receiver mux ] raise: MessageNotUnderstood.
  727. self should: [ receiver mux: 2 and: -1 and: 0 ] raise: MessageNotUnderstood.
  728. self assert: (receiver basicPerform: #mux withArguments: #(5 2 3)) equals: 6.
  729. self assert: (receiver quux: 1 foo: 4 bar: 20) equals: -10.
  730. self should: [ receiver basicPerform: #quux ] raise: Error.
  731. self assert: receiver foo equals: -10 ]
  732. ! !
  733. !AbstractCodeGeneratorInstallTest class methodsFor: 'testing'!
  734. isAbstract
  735. ^ self name = AbstractCodeGeneratorInstallTest name
  736. ! !
  737. AbstractCodeGeneratorInstallTest subclass: #CodeGeneratorInstallTest
  738. slots: {}
  739. package: 'Compiler-Tests'!
  740. AbstractCodeGeneratorInstallTest subclass: #InliningCodeGeneratorInstallTest
  741. slots: {}
  742. package: 'Compiler-Tests'!
  743. TestCase subclass: #ScopeVarTest
  744. slots: {}
  745. package: 'Compiler-Tests'!
  746. !ScopeVarTest methodsFor: 'tests'!
  747. testClassRefVar
  748. | node binding |
  749. node := VariableNode new
  750. identifier: 'Object';
  751. yourself.
  752. SemanticAnalyzer new
  753. pushScope: MethodLexicalScope new;
  754. visit: node.
  755. binding := node binding.
  756. self deny: binding isAssignable.
  757. self deny: binding isIdempotent.
  758. self assert: (binding alias includesSubString: 'Object').
  759. self assert: (binding alias ~= 'Object')
  760. !
  761. testExternallyKnownVar
  762. | node binding |
  763. node := VariableNode new
  764. identifier: 'console';
  765. yourself.
  766. SemanticAnalyzer new
  767. pushScope: MethodLexicalScope new;
  768. visit: node.
  769. binding := node binding.
  770. self deny: binding isAssignable.
  771. self deny: binding isIdempotent.
  772. self assert: binding alias equals: 'console'
  773. !
  774. testExternallyUnknownVar
  775. | node |
  776. node := VariableNode new
  777. identifier: 'bzzz';
  778. yourself.
  779. self
  780. should: [
  781. SemanticAnalyzer new
  782. pushScope: MethodLexicalScope new;
  783. visit: node ]
  784. raise: UnknownVariableError
  785. !
  786. testPseudoVar
  787. #('self' 'super' 'true' 'false' 'nil' 'thisContext') do: [ :each |
  788. | binding |
  789. binding := MethodLexicalScope new bindingFor: each.
  790. self deny: binding isAssignable.
  791. self assert: binding isIdempotent ]
  792. !
  793. testSlotVar
  794. | binding |
  795. binding := MethodLexicalScope new
  796. addSlotVar: 'bzzz';
  797. bindingFor: 'bzzz'.
  798. self assert: binding isAssignable.
  799. self deny: binding isIdempotent.
  800. self assert: (binding alias includesSubString: 'bzzz').
  801. self assert: (binding alias ~= 'bzzz')
  802. !
  803. testTempVar
  804. | binding |
  805. binding := MethodLexicalScope new
  806. addTemp: 'bzzz';
  807. bindingFor: 'bzzz'.
  808. self assert: binding isAssignable.
  809. self deny: binding isIdempotent.
  810. self assert: binding alias equals: 'bzzz'
  811. !
  812. testUnknownVar
  813. self assert: (MethodLexicalScope new bindingFor: 'bzzz') isNil
  814. ! !
  815. TestCase subclass: #SemanticAnalyzerTest
  816. slots: {#analyzer}
  817. package: 'Compiler-Tests'!
  818. !SemanticAnalyzerTest methodsFor: 'running'!
  819. setUp
  820. analyzer := SemanticAnalyzer on: Object
  821. ! !
  822. !SemanticAnalyzerTest methodsFor: 'tests'!
  823. testAssignment
  824. | src ast |
  825. src := 'foo self := 1'.
  826. ast := Smalltalk parse: src.
  827. self should: [analyzer visit: ast] raise: InvalidAssignmentError
  828. !
  829. testNonLocalReturn
  830. | src ast |
  831. src := 'foo | a | a + 1. ^ a'.
  832. ast := Smalltalk parse: src.
  833. analyzer visit: ast.
  834. self deny: ast scope hasNonLocalReturn
  835. !
  836. testNonLocalReturn2
  837. | src ast |
  838. src := 'foo | a | a + 1. [ [ ^ a] ]'.
  839. ast := Smalltalk parse: src.
  840. analyzer visit: ast.
  841. self assert: ast scope hasNonLocalReturn
  842. !
  843. testScope
  844. | src ast |
  845. src := 'foo | a | a + 1. [ | b | b := a ]'.
  846. ast := Smalltalk parse: src.
  847. analyzer visit: ast.
  848. self deny: ast sequenceNode dagChildren last scope == ast scope.
  849. !
  850. testScope2
  851. | src ast |
  852. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  853. ast := Smalltalk parse: src.
  854. analyzer visit: ast.
  855. self deny: ast sequenceNode dagChildren last sequenceNode dagChildren first scope == ast scope.
  856. !
  857. testScopeLevel
  858. | src ast |
  859. src := 'foo | a | a + 1. [ [ | b | b := a ] ]'.
  860. ast := Smalltalk parse: src.
  861. analyzer visit: ast.
  862. self assert: ast scope scopeLevel equals: 1.
  863. self assert: ast sequenceNode dagChildren last sequenceNode dagChildren first scope scopeLevel equals: 3
  864. !
  865. testUnknownVariables
  866. | src ast |
  867. src := 'foo | a | b + a'.
  868. ast := Smalltalk parse: src.
  869. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  870. !
  871. testUnknownVariablesWithScope
  872. | src ast |
  873. src := 'foo | a b | [ c + 1. [ a + 1. d + 1 ]]'.
  874. ast := Smalltalk parse: src.
  875. self should: [ analyzer visit: ast ] raise: UnknownVariableError
  876. !
  877. testVariableShadowing
  878. | src ast |
  879. src := 'foo | a | a + 1'.
  880. ast := Smalltalk parse: src.
  881. analyzer visit: ast
  882. !
  883. testVariableShadowing2
  884. | src ast |
  885. src := 'foo | a | a + 1. [ | a | a := 2 ]'.
  886. ast := Smalltalk parse: src.
  887. self should: [analyzer visit: ast] raise: ShadowingVariableError
  888. !
  889. testVariableShadowing3
  890. | src ast |
  891. src := 'foo | a | a + 1. [ | b | b := 2 ]'.
  892. ast := Smalltalk parse: src.
  893. analyzer visit: ast
  894. !
  895. testVariableShadowing4
  896. | src ast |
  897. src := 'foo | a | a + 1. [ [ [ | b | b := 2 ] ] ]'.
  898. ast := Smalltalk parse: src.
  899. analyzer visit: ast
  900. !
  901. testVariableShadowing5
  902. | src ast |
  903. src := 'foo | a | a + 1. [ [ [ | a | a := 2 ] ] ]'.
  904. ast := Smalltalk parse: src.
  905. self should: [analyzer visit: ast] raise: ShadowingVariableError
  906. !
  907. testVariablesLookup
  908. | src ast |
  909. src := 'foo | a | a + 1. [ | b | b := a ]'.
  910. ast := Smalltalk parse: src.
  911. analyzer visit: ast.
  912. "Binding for `a` in the message send"
  913. self assert: ast sequenceNode dagChildren first receiver binding isAssignable.
  914. self assert: ast sequenceNode dagChildren first receiver binding alias equals: 'a'.
  915. self assert: ast sequenceNode dagChildren first receiver binding scope == ast scope.
  916. "Binding for `b`"
  917. self assert: ast sequenceNode dagChildren last sequenceNode dagChildren first left binding isAssignable.
  918. self assert: ast sequenceNode dagChildren last sequenceNode dagChildren first left binding alias equals: 'b'.
  919. self assert: ast sequenceNode dagChildren last sequenceNode dagChildren first left binding scope == ast sequenceNode dagChildren last scope.
  920. ! !
  921. SemanticAnalyzerTest subclass: #AISemanticAnalyzerTest
  922. slots: {}
  923. package: 'Compiler-Tests'!
  924. !AISemanticAnalyzerTest methodsFor: 'running'!
  925. setUp
  926. analyzer := (AISemanticAnalyzer on: Object)
  927. context: (AIContext new
  928. defineLocal: 'local';
  929. localAt: 'local' put: 3;
  930. yourself);
  931. yourself
  932. ! !
  933. !AISemanticAnalyzerTest methodsFor: 'tests'!
  934. testContextVariables
  935. | src ast |
  936. src := 'foo | a | local + a'.
  937. ast := Smalltalk parse: src.
  938. self shouldnt: [ analyzer visit: ast ] raise: UnknownVariableError
  939. ! !
  940. Trait named: #TASTCompilingTest
  941. package: 'Compiler-Tests'!
  942. !TASTCompilingTest methodsFor: 'accessing'!
  943. codeGeneratorClass
  944. self subclassResponsibility
  945. ! !
  946. !TASTCompilingTest methodsFor: 'compiling'!
  947. install: aString forClass: aClass
  948. ^ self compiler
  949. install: aString
  950. forClass: aClass
  951. protocol: 'tests'
  952. ! !
  953. !TASTCompilingTest methodsFor: 'factory'!
  954. compiler
  955. ^ Compiler new
  956. codeGeneratorClass: self codeGeneratorClass;
  957. yourself
  958. ! !
  959. !TASTCompilingTest methodsFor: 'testing'!
  960. while: aString inClass: aClass should: aBlock
  961. | method |
  962. [
  963. method := self install: aString forClass: aClass.
  964. aBlock value: method ]
  965. ensure: [ method ifNotNil: [ aClass removeCompiledMethod: method ] ]
  966. !
  967. while: aString should: aBlock
  968. self while: aString inClass: self receiver class should: aBlock
  969. ! !
  970. Trait named: #TASTParsingTest
  971. package: 'Compiler-Tests'!
  972. !TASTParsingTest methodsFor: 'parsing'!
  973. parse: aString forClass: aClass
  974. ^ Compiler new
  975. ast: aString
  976. forClass: aClass
  977. protocol: 'test'
  978. ! !
  979. Trait named: #TCTDebugged
  980. package: 'Compiler-Tests'!
  981. !TCTDebugged methodsFor: 'private'!
  982. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  983. "The food is a methodNode. Interpret the sequenceNode only"
  984. | ctx |
  985. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  986. ^ (ASTDebugger context: ctx) proceed; result
  987. ! !
  988. Trait named: #TCTExecuted
  989. package: 'Compiler-Tests'!
  990. !TCTExecuted methodsFor: 'testing'!
  991. while: aString inClass: aClass should: aBlock
  992. super
  993. while: aString
  994. inClass: aClass
  995. should: [ :method | aBlock value: [
  996. self receiver perform: method selector withArguments: self arguments ] ]
  997. ! !
  998. Trait named: #TCTInlined
  999. package: 'Compiler-Tests'!
  1000. !TCTInlined methodsFor: 'accessing'!
  1001. codeGeneratorClass
  1002. ^ InliningCodeGenerator
  1003. ! !
  1004. Trait named: #TCTInterpreted
  1005. package: 'Compiler-Tests'!
  1006. !TCTInterpreted methodsFor: 'private'!
  1007. interpret: aString forClass: aClass receiver: anObject withArguments: aDictionary
  1008. "The food is a methodNode. Interpret the sequenceNode only"
  1009. | ctx |
  1010. ctx := self prepareContextFor: aString class: aClass receiver: anObject withArguments: aDictionary.
  1011. ^ ctx interpreter proceed; result
  1012. !
  1013. prepareContextFor: aString class: aClass receiver: anObject withArguments: anArray
  1014. "The food is a methodNode. Interpret the sequenceNode only"
  1015. | ctx ast |
  1016. ast := self parse: aString forClass: aClass.
  1017. ctx := AIContext new
  1018. receiver: anObject;
  1019. selector: ast selector;
  1020. interpreter: ASTInterpreter new;
  1021. yourself.
  1022. "Define locals for the context"
  1023. ast sequenceNode ifNotNil: [ :sequence |
  1024. sequence temps do: [ :each |
  1025. ctx defineLocal: each ] ].
  1026. ast arguments with: anArray do: [ :key :value |
  1027. ctx defineLocal: key; localAt: key put: value ].
  1028. ctx interpreter
  1029. context: ctx;
  1030. node: ast;
  1031. enterNode.
  1032. ^ctx
  1033. ! !
  1034. !TCTInterpreted methodsFor: 'testing'!
  1035. while: aString inClass: aClass should: aBlock
  1036. super
  1037. while: aString
  1038. inClass: aClass
  1039. should: [ aBlock value: [
  1040. self
  1041. interpret: aString
  1042. forClass: aClass
  1043. receiver: self receiver
  1044. withArguments: self arguments ] ]
  1045. ! !
  1046. Trait named: #TCTNonInlined
  1047. package: 'Compiler-Tests'!
  1048. !TCTNonInlined methodsFor: 'accessing'!
  1049. codeGeneratorClass
  1050. ^ CodeGenerator
  1051. ! !
  1052. TASTCompilingTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  1053. TCTDebugged setTraitComposition: {TCTInterpreted} asTraitComposition!
  1054. ASTMethodRunningTest setTraitComposition: {TASTCompilingTest} asTraitComposition!
  1055. ASTDebuggerTest setTraitComposition: {TCTNonInlined. TCTDebugged} asTraitComposition!
  1056. ASTInterpreterTest setTraitComposition: {TCTNonInlined. TCTInterpreted} asTraitComposition!
  1057. CodeGeneratorTest setTraitComposition: {TCTNonInlined. TCTExecuted} asTraitComposition!
  1058. InliningCodeGeneratorTest setTraitComposition: {TCTInlined. TCTExecuted} asTraitComposition!
  1059. AbstractJavaScriptGatewayTest setTraitComposition: {TClassBuildingTest} asTraitComposition!
  1060. DebuggedJSGTest setTraitComposition: {TCTNonInlined. TCTDebugged} asTraitComposition!
  1061. InlinedJSGTest setTraitComposition: {TCTInlined. TCTExecuted} asTraitComposition!
  1062. InterpretedJSGTest setTraitComposition: {TCTNonInlined. TCTInterpreted} asTraitComposition!
  1063. PlainJSGTest setTraitComposition: {TCTNonInlined. TCTExecuted} asTraitComposition!
  1064. ASTPCNodeVisitorTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  1065. ASTPositionTest setTraitComposition: {TASTParsingTest} asTraitComposition!
  1066. AbstractCodeGeneratorInstallTest setTraitComposition: {TASTCompilingTest} asTraitComposition!
  1067. CodeGeneratorInstallTest setTraitComposition: {TCTNonInlined} asTraitComposition!
  1068. InliningCodeGeneratorInstallTest setTraitComposition: {TCTInlined} asTraitComposition!
  1069. ! !