Compiler-Tests.st 35 KB

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