test-vm.scm 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245
  1. ;;; WebAssembly virtual machine
  2. ;;; Copyright (C) 2023, 2024 David Thompson <dave@spritely.institute>
  3. ;;;
  4. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  5. ;;; you may not use this file except in compliance with the License.
  6. ;;; You may obtain a copy of the License at
  7. ;;;
  8. ;;; http://www.apache.org/licenses/LICENSE-2.0
  9. ;;;
  10. ;;; Unless required by applicable law or agreed to in writing, software
  11. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  12. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. ;;; See the License for the specific language governing permissions and
  14. ;;; limitations under the License.
  15. ;;; Commentary:
  16. ;;;
  17. ;;; Virtual machine tests.
  18. ;;;
  19. ;;; Code:
  20. (use-modules (ice-9 binary-ports)
  21. (ice-9 exceptions)
  22. (ice-9 popen)
  23. (ice-9 textual-ports)
  24. (srfi srfi-64)
  25. (test utils)
  26. (hoot config)
  27. (wasm wat)
  28. (wasm lower)
  29. (wasm assemble)
  30. (wasm vm))
  31. (define s32-overflow (@@ (wasm vm) s32-overflow))
  32. (define s64-overflow (@@ (wasm vm) s64-overflow))
  33. (define (run-v8 read args)
  34. (let* ((port (apply open-pipe* OPEN_READ
  35. (v8) "--experimental-wasm-stringref" args))
  36. (result (string-trim-both (get-string-all port))))
  37. (if (zero? (close-pipe port))
  38. (call-with-input-string result read)
  39. (throw 'v8-error result))))
  40. (define (run-wasm-in-v8 wasm func read args)
  41. (call-with-compiled-wasm-file
  42. wasm
  43. (lambda (wasm-file-name)
  44. (run-v8 read
  45. (cons* (in-vicinity %js-runner-dir "load-primitive.js")
  46. "--" wasm-file-name func
  47. (map (lambda (arg) (format #f "~a" arg)) args))))))
  48. (define (run-wasm-in-vm wasm func args imports)
  49. (let ((instance (instantiate-wasm (validate-wasm wasm) #:imports imports)))
  50. (apply (wasm-instance-export-ref instance func) args)))
  51. (define (wat->wasm* wat)
  52. (lower-wasm (wat->wasm wat) #:stringref-lowering 'stringref))
  53. (define (eval-wat wat func args imports v8? v8-read)
  54. (let* ((wasm (wat->wasm* wat))
  55. (our-result (run-wasm-in-vm wasm func args imports)))
  56. (when v8?
  57. (let ((v8-result (run-wasm-in-v8 wasm func v8-read args)))
  58. (unless (if (and (number? our-result) (number? v8-result))
  59. (= our-result v8-result)
  60. (equal? our-result v8-result))
  61. (error "our result differs from v8" our-result v8-result))))
  62. our-result))
  63. (define* (test-vm name expected wat #:key
  64. (func "main") (args '()) (imports '())
  65. (v8? (or (use-node?) (use-d8?))) (v8-read read))
  66. (test-equal name
  67. expected
  68. (eval-wat wat func args imports v8? v8-read)))
  69. (define (eval-wat/error wat func args imports v8? v8-read)
  70. (let ((wasm (wat->wasm* wat)))
  71. (define (handle-error e)
  72. (if v8?
  73. (with-exception-handler (lambda (e) #t)
  74. (lambda ()
  75. (let ((result (run-wasm-in-v8 wasm func v8-read args)))
  76. (error "v8 did not throw an error" result))
  77. #f)
  78. #:unwind? #t
  79. #:unwind-for-type 'v8-error)
  80. #t))
  81. (with-exception-handler handle-error
  82. (lambda ()
  83. (run-wasm-in-vm wasm func args imports)
  84. #f)
  85. #:unwind? #t
  86. #:unwind-for-type &wasm-error)))
  87. (define* (test-vm/error name wat #:key
  88. (func "main") (args '()) (imports '())
  89. (v8? (or (use-node?) (use-d8?))) (v8-read read))
  90. (test-assert name
  91. (eval-wat/error wat func args imports v8? v8-read)))
  92. (test-begin "test-vm")
  93. (test-vm "i32.eqz true"
  94. 1
  95. '(module
  96. (func (export "main") (result i32)
  97. (i32.eqz (i32.const 0)))))
  98. (test-vm "i32.eqz false"
  99. 0
  100. '(module
  101. (func (export "main") (result i32)
  102. (i32.eqz (i32.const 42)))))
  103. (test-vm "i32.eq true"
  104. 1
  105. '(module
  106. (func (export "main") (result i32)
  107. (i32.eq (i32.const 42) (i32.const 42)))))
  108. (test-vm "i32.eq false"
  109. 0
  110. '(module
  111. (func (export "main") (result i32)
  112. (i32.eq (i32.const 42) (i32.const 7)))))
  113. (test-vm "i32.ne true"
  114. 1
  115. '(module
  116. (func (export "main") (result i32)
  117. (i32.ne (i32.const 42) (i32.const 7)))))
  118. (test-vm "i32.ne false"
  119. 0
  120. '(module
  121. (func (export "main") (result i32)
  122. (i32.ne (i32.const 42) (i32.const 42)))))
  123. (test-vm "i32.lt_s true"
  124. 1
  125. '(module
  126. (func (export "main") (result i32)
  127. (i32.lt_s (i32.const -42) (i32.const 42)))))
  128. (test-vm "i32.lt_s false"
  129. 0
  130. '(module
  131. (func (export "main") (result i32)
  132. (i32.lt_s (i32.const 42) (i32.const -42)))))
  133. (test-vm "i32.lt_u true"
  134. 1
  135. '(module
  136. (func (export "main") (result i32)
  137. (i32.lt_s (i32.const 7) (i32.const 42)))))
  138. (test-vm "i32.lt_u false"
  139. 0
  140. '(module
  141. (func (export "main") (result i32)
  142. (i32.lt_s (i32.const 42) (i32.const 7)))))
  143. (test-vm "i32.le_s true"
  144. 1
  145. '(module
  146. (func (export "main") (result i32)
  147. (i32.le_s (i32.const 42) (i32.const 42)))))
  148. (test-vm "i32.le_s false"
  149. 0
  150. '(module
  151. (func (export "main") (result i32)
  152. (i32.le_s (i32.const 42) (i32.const -42)))))
  153. (test-vm "i32.le_u true"
  154. 1
  155. '(module
  156. (func (export "main") (result i32)
  157. (i32.le_s (i32.const 42) (i32.const 42)))))
  158. (test-vm "i32.le_u false"
  159. 0
  160. '(module
  161. (func (export "main") (result i32)
  162. (i32.le_s (i32.const 42) (i32.const 7)))))
  163. (test-vm "i32.gt_s true"
  164. 1
  165. '(module
  166. (func (export "main") (result i32)
  167. (i32.gt_s (i32.const 42) (i32.const -42)))))
  168. (test-vm "i32.gt_s false"
  169. 0
  170. '(module
  171. (func (export "main") (result i32)
  172. (i32.gt_s (i32.const -42) (i32.const 42)))))
  173. (test-vm "i32.gt_u true"
  174. 1
  175. '(module
  176. (func (export "main") (result i32)
  177. (i32.gt_s (i32.const 42) (i32.const 7)))))
  178. (test-vm "i32.gt_u false"
  179. 0
  180. '(module
  181. (func (export "main") (result i32)
  182. (i32.gt_s (i32.const 7) (i32.const 42)))))
  183. (test-vm "i32.ge_s true"
  184. 1
  185. '(module
  186. (func (export "main") (result i32)
  187. (i32.ge_s (i32.const 42) (i32.const 42)))))
  188. (test-vm "i32.ge_s false"
  189. 0
  190. '(module
  191. (func (export "main") (result i32)
  192. (i32.ge_s (i32.const -42) (i32.const 42)))))
  193. (test-vm "i32.ge_u true"
  194. 1
  195. '(module
  196. (func (export "main") (result i32)
  197. (i32.ge_s (i32.const 42) (i32.const 42)))))
  198. (test-vm "i32.ge_u false"
  199. 0
  200. '(module
  201. (func (export "main") (result i32)
  202. (i32.ge_s (i32.const 7) (i32.const 42)))))
  203. (test-vm "i32.add"
  204. 80
  205. '(module
  206. (func (export "main") (result i32)
  207. (i32.add (i32.const 42) (i32.const 38)))))
  208. (test-vm "i32.sub"
  209. 4
  210. '(module
  211. (func (export "main") (result i32)
  212. (i32.sub (i32.const 42) (i32.const 38)))))
  213. (test-vm "i32.mul"
  214. 42
  215. '(module
  216. (func (export "main") (result i32)
  217. (i32.mul (i32.const 7) (i32.const 6)))))
  218. (test-vm "i32.div_s"
  219. -6
  220. '(module
  221. (func (export "main") (result i32)
  222. (i32.div_s (i32.const -42) (i32.const 7)))))
  223. (test-vm "i32.div_u"
  224. 6
  225. '(module
  226. (func (export "main") (result i32)
  227. (i32.div_u (i32.const 42) (i32.const 7)))))
  228. (test-vm "i32.rem_s"
  229. -2
  230. '(module
  231. (func (export "main") (result i32)
  232. (i32.rem_s (i32.const -42) (i32.const 5)))))
  233. (test-vm "i32.rem_u"
  234. 2
  235. '(module
  236. (func (export "main") (result i32)
  237. (i32.rem_u (i32.const 42) (i32.const 5)))))
  238. (test-vm "i32.and"
  239. #b1010
  240. '(module
  241. (func (export "main") (result i32)
  242. (i32.and (i32.const #b1111) (i32.const #b1010)))))
  243. (test-vm "i32.or"
  244. #b1111
  245. '(module
  246. (func (export "main") (result i32)
  247. (i32.or (i32.const #b0101) (i32.const #b1010)))))
  248. (test-vm "i32.xor"
  249. #b1110
  250. '(module
  251. (func (export "main") (result i32)
  252. (i32.or (i32.const #b1010) (i32.const #b0100)))))
  253. (test-vm "i32.shl"
  254. #b1000
  255. '(module
  256. (func (export "main") (result i32)
  257. (i32.shl (i32.const #b0001) (i32.const 3)))))
  258. (test-vm "i32.shl - out of 32 bit range"
  259. 0
  260. `(module
  261. (func (export "main") (result i32)
  262. (i32.shl (i32.const ,(ash -1 31))
  263. (i32.const 2)))))
  264. (test-vm "i32.shl - more than 32 bits"
  265. #b1000
  266. '(module
  267. (func (export "main") (result i32)
  268. (i32.shl (i32.const #b0001) (i32.const 35)))))
  269. (test-vm "i32.shr_s"
  270. -2
  271. '(module
  272. (func (export "main") (result i32)
  273. (i32.shr_s (i32.const -16) (i32.const 3)))))
  274. (test-vm "i32.shr_u"
  275. #b0001
  276. '(module
  277. (func (export "main") (result i32)
  278. (i32.shr_u (i32.const #b1000) (i32.const 3)))))
  279. (test-vm "i32.shr_u - more than 32 bits"
  280. #b0001
  281. '(module
  282. (func (export "main") (result i32)
  283. (i32.shr_u (i32.const #b1000) (i32.const 35)))))
  284. (test-vm "i32.rotl"
  285. (s32-overflow #b11000011110000111100001111000011)
  286. `(module
  287. (func (export "main") (result i32)
  288. (i32.rotl (i32.const
  289. ,(s32-overflow #b11110000111100001111000011110000))
  290. (i32.const 2)))))
  291. (test-vm "i32.rotr"
  292. (s32-overflow #b00111100001111000011110000111100)
  293. `(module
  294. (func (export "main") (result i32)
  295. (i32.rotr (i32.const
  296. ,(s32-overflow #b11110000111100001111000011110000))
  297. (i32.const 2)))))
  298. (test-vm "i32.clz"
  299. 7
  300. `(module
  301. (func (export "main") (result i32)
  302. (i32.clz (i32.const ,(ash 1 24))))))
  303. (test-vm "i32.ctz"
  304. 24
  305. `(module
  306. (func (export "main") (result i32)
  307. (i32.ctz (i32.const ,(ash 1 24))))))
  308. (test-vm "i32.popcnt"
  309. 16
  310. `(module
  311. (func (export "main") (result i32)
  312. (i32.popcnt (i32.const ,(s32-overflow #xaaaaAAAA))))))
  313. (test-vm "i32.wrap_i64 positive"
  314. 3
  315. `(module
  316. (func (export "main") (result i32)
  317. (i32.wrap_i64 (i64.const ,(+ (ash 1 32) 3))))))
  318. (test-vm "i32.wrap_i64 negative"
  319. -3
  320. `(module
  321. (func (export "main") (result i32)
  322. (i32.wrap_i64 (i64.const ,(- (ash -1 32) 3))))))
  323. (test-vm "i32.trunc_f32_s"
  324. -1
  325. `(module
  326. (func (export "main") (result i32)
  327. (i32.trunc_f32_s (f32.const -1.2)))))
  328. (test-vm "i32.trunc_f32_u"
  329. 1
  330. `(module
  331. (func (export "main") (result i32)
  332. (i32.trunc_f32_u (f32.const 1.2)))))
  333. (test-vm "i32.trunc_f64_s"
  334. -1
  335. `(module
  336. (func (export "main") (result i32)
  337. (i32.trunc_f64_s (f64.const -1.2)))))
  338. (test-vm "i32.trunc_f64_u"
  339. 1
  340. `(module
  341. (func (export "main") (result i32)
  342. (i32.trunc_f64_u (f64.const 1.2)))))
  343. (test-vm "i32.reinterpret_f32"
  344. 1067030938
  345. `(module
  346. (func (export "main") (result i32)
  347. (i32.reinterpret_f32 (f32.const 1.2)))))
  348. (test-vm "i64.eqz true"
  349. 1
  350. '(module
  351. (func (export "main") (result i32)
  352. (i64.eqz (i64.const 0)))))
  353. (test-vm "i64.eqz false"
  354. 0
  355. '(module
  356. (func (export "main") (result i32)
  357. (i64.eqz (i64.const 42)))))
  358. (test-vm "i64.eq true"
  359. 1
  360. '(module
  361. (func (export "main") (result i32)
  362. (i64.eq (i64.const 42) (i64.const 42)))))
  363. (test-vm "i64.eq false"
  364. 0
  365. '(module
  366. (func (export "main") (result i32)
  367. (i64.eq (i64.const 42) (i64.const 7)))))
  368. (test-vm "i64.ne true"
  369. 1
  370. '(module
  371. (func (export "main") (result i32)
  372. (i64.ne (i64.const 42) (i64.const 7)))))
  373. (test-vm "i64.ne false"
  374. 0
  375. '(module
  376. (func (export "main") (result i32)
  377. (i64.ne (i64.const 42) (i64.const 42)))))
  378. (test-vm "i64.lt_s true"
  379. 1
  380. '(module
  381. (func (export "main") (result i32)
  382. (i64.lt_s (i64.const -42) (i64.const 42)))))
  383. (test-vm "i64.lt_s false"
  384. 0
  385. '(module
  386. (func (export "main") (result i32)
  387. (i64.lt_s (i64.const 42) (i64.const -42)))))
  388. (test-vm "i64.lt_u true"
  389. 1
  390. '(module
  391. (func (export "main") (result i32)
  392. (i64.lt_s (i64.const 7) (i64.const 42)))))
  393. (test-vm "i64.lt_u false"
  394. 0
  395. '(module
  396. (func (export "main") (result i32)
  397. (i64.lt_s (i64.const 42) (i64.const 7)))))
  398. (test-vm "i64.le_s true"
  399. 1
  400. '(module
  401. (func (export "main") (result i32)
  402. (i64.le_s (i64.const 42) (i64.const 42)))))
  403. (test-vm "i64.le_s false"
  404. 0
  405. '(module
  406. (func (export "main") (result i32)
  407. (i64.le_s (i64.const 42) (i64.const -42)))))
  408. (test-vm "i64.le_u true"
  409. 1
  410. '(module
  411. (func (export "main") (result i32)
  412. (i64.le_s (i64.const 42) (i64.const 42)))))
  413. (test-vm "i64.le_u false"
  414. 0
  415. '(module
  416. (func (export "main") (result i32)
  417. (i64.le_s (i64.const 42) (i64.const 7)))))
  418. (test-vm "i64.gt_s true"
  419. 1
  420. '(module
  421. (func (export "main") (result i32)
  422. (i64.gt_s (i64.const 42) (i64.const -42)))))
  423. (test-vm "i64.gt_s false"
  424. 0
  425. '(module
  426. (func (export "main") (result i32)
  427. (i64.gt_s (i64.const -42) (i64.const 42)))))
  428. (test-vm "i64.gt_u true"
  429. 1
  430. '(module
  431. (func (export "main") (result i32)
  432. (i64.gt_s (i64.const 42) (i64.const 7)))))
  433. (test-vm "i64.gt_u false"
  434. 0
  435. '(module
  436. (func (export "main") (result i32)
  437. (i64.gt_s (i64.const 7) (i64.const 42)))))
  438. (test-vm "i64.ge_s true"
  439. 1
  440. '(module
  441. (func (export "main") (result i32)
  442. (i64.ge_s (i64.const 42) (i64.const 42)))))
  443. (test-vm "i64.ge_s false"
  444. 0
  445. '(module
  446. (func (export "main") (result i32)
  447. (i64.ge_s (i64.const -42) (i64.const 42)))))
  448. (test-vm "i64.ge_u true"
  449. 1
  450. '(module
  451. (func (export "main") (result i32)
  452. (i64.ge_s (i64.const 42) (i64.const 42)))))
  453. (test-vm "i64.ge_u false"
  454. 0
  455. '(module
  456. (func (export "main") (result i32)
  457. (i64.ge_s (i64.const 7) (i64.const 42)))))
  458. (test-vm "i64.add"
  459. 80
  460. '(module
  461. (func (export "main") (result i64)
  462. (i64.add (i64.const 42) (i64.const 38)))))
  463. (test-vm "i64.sub"
  464. 4
  465. '(module
  466. (func (export "main") (result i64)
  467. (i64.sub (i64.const 42) (i64.const 38)))))
  468. (test-vm "i64.mul"
  469. 42
  470. '(module
  471. (func (export "main") (result i64)
  472. (i64.mul (i64.const 7) (i64.const 6)))))
  473. (test-vm "i64.div_s"
  474. -6
  475. '(module
  476. (func (export "main") (result i64)
  477. (i64.div_s (i64.const -42) (i64.const 7)))))
  478. (test-vm "i64.div_u"
  479. 6
  480. '(module
  481. (func (export "main") (result i64)
  482. (i64.div_u (i64.const 42) (i64.const 7)))))
  483. (test-vm "i64.rem_s"
  484. -2
  485. '(module
  486. (func (export "main") (result i64)
  487. (i64.rem_s (i64.const -42) (i64.const 5)))))
  488. (test-vm "i64.rem_u"
  489. 2
  490. '(module
  491. (func (export "main") (result i64)
  492. (i64.rem_u (i64.const 42) (i64.const 5)))))
  493. (test-vm "i64.and"
  494. #b1010
  495. '(module
  496. (func (export "main") (result i64)
  497. (i64.and (i64.const #b1111) (i64.const #b1010)))))
  498. (test-vm "i64.or"
  499. #b1111
  500. '(module
  501. (func (export "main") (result i64)
  502. (i64.or (i64.const #b0101) (i64.const #b1010)))))
  503. (test-vm "i64.xor"
  504. #b1110
  505. '(module
  506. (func (export "main") (result i64)
  507. (i64.or (i64.const #b1010) (i64.const #b0100)))))
  508. (test-vm "i64.shl"
  509. #b1000
  510. '(module
  511. (func (export "main") (result i64)
  512. (i64.shl (i64.const #b0001) (i64.const 3)))))
  513. (test-vm "i64.shr_s"
  514. -2
  515. '(module
  516. (func (export "main") (result i64)
  517. (i64.shr_s (i64.const -16) (i64.const 3)))))
  518. (test-vm "i64.shr_u"
  519. #b0001
  520. '(module
  521. (func (export "main") (result i64)
  522. (i64.shr_u (i64.const #b1000) (i64.const 3)))))
  523. (test-vm "i64.rotl"
  524. (s64-overflow #xfff0000ffff0000f)
  525. `(module
  526. (func (export "main") (result i64)
  527. (i64.rotl (i64.const
  528. ,(s64-overflow #xffff0000ffff0000))
  529. (i64.const 4)))))
  530. (test-vm "i64.rotr"
  531. (s64-overflow #xffff0000ffff0000)
  532. `(module
  533. (func (export "main") (result i64)
  534. (i64.rotr (i64.const
  535. ,(s64-overflow #xfff0000ffff0000f))
  536. (i64.const 4)))))
  537. (test-vm "i64.clz"
  538. 15
  539. `(module
  540. (func (export "main") (result i64)
  541. (i64.clz (i64.const ,(ash 1 48))))))
  542. (test-vm "i64.ctz"
  543. 48
  544. `(module
  545. (func (export "main") (result i64)
  546. (i64.ctz (i64.const ,(ash 1 48))))))
  547. (test-vm "i64.popcnt"
  548. 32
  549. `(module
  550. (func (export "main") (result i64)
  551. (i64.popcnt (i64.const ,(s64-overflow #xaaaaAAAAaaaaAAAA))))))
  552. (test-vm "i64.extend_i32_s"
  553. -42
  554. `(module
  555. (func (export "main") (result i64)
  556. (i64.extend_i32_s (i32.const -42)))))
  557. (test-vm "i64.extend_i32_u"
  558. 42
  559. `(module
  560. (func (export "main") (result i64)
  561. (i64.extend_i32_u (i32.const 42)))))
  562. (test-vm "i64.trunc_f32_s"
  563. -1
  564. `(module
  565. (func (export "main") (result i64)
  566. (i64.trunc_f32_s (f32.const -1.2)))))
  567. (test-vm "i64.trunc_f32_u"
  568. 1
  569. `(module
  570. (func (export "main") (result i64)
  571. (i64.trunc_f32_u (f32.const 1.2)))))
  572. (test-vm "i64.trunc_f64_s"
  573. -1
  574. `(module
  575. (func (export "main") (result i64)
  576. (i64.trunc_f64_s (f64.const -1.2)))))
  577. (test-vm "i64.trunc_f64_u"
  578. 1
  579. `(module
  580. (func (export "main") (result i64)
  581. (i64.trunc_f64_u (f64.const 1.2)))))
  582. (test-vm "i64.reinterpret_f64"
  583. 4608083138725491507
  584. `(module
  585. (func (export "main") (result i64)
  586. (i64.reinterpret_f64 (f64.const 1.2)))))
  587. (test-vm "f32.eq true"
  588. 1
  589. '(module
  590. (func (export "main") (result i32)
  591. (f32.eq (f32.const 42.0) (f32.const 42.0)))))
  592. (test-vm "f32.eq false"
  593. 0
  594. '(module
  595. (func (export "main") (result i32)
  596. (f32.eq (f32.const 42.0) (f32.const 7.0)))))
  597. (test-vm "f32.ne true"
  598. 1
  599. '(module
  600. (func (export "main") (result i32)
  601. (f32.ne (f32.const 42.0) (f32.const 7.0)))))
  602. (test-vm "f32.ne false"
  603. 0
  604. '(module
  605. (func (export "main") (result i32)
  606. (f32.ne (f32.const 42.0) (f32.const 42.0)))))
  607. (test-vm "f32.lt true"
  608. 1
  609. '(module
  610. (func (export "main") (result i32)
  611. (f32.lt (f32.const -42.0) (f32.const 42.0)))))
  612. (test-vm "f32.lt false"
  613. 0
  614. '(module
  615. (func (export "main") (result i32)
  616. (f32.lt (f32.const 42.0) (f32.const -42.0)))))
  617. (test-vm "f32.le true"
  618. 1
  619. '(module
  620. (func (export "main") (result i32)
  621. (f32.le (f32.const 42.0) (f32.const 42.0)))))
  622. (test-vm "f32.le false"
  623. 0
  624. '(module
  625. (func (export "main") (result i32)
  626. (f32.le (f32.const 42.0) (f32.const -42.0)))))
  627. (test-vm "f32.gt true"
  628. 1
  629. '(module
  630. (func (export "main") (result i32)
  631. (f32.gt (f32.const 42.0) (f32.const -42.0)))))
  632. (test-vm "f32.gt false"
  633. 0
  634. '(module
  635. (func (export "main") (result i32)
  636. (f32.gt (f32.const -42.0) (f32.const 42.0)))))
  637. (test-vm "f32.ge true"
  638. 1
  639. '(module
  640. (func (export "main") (result i32)
  641. (f32.ge (f32.const 42.0) (f32.const 42.0)))))
  642. (test-vm "f32.ge false"
  643. 0
  644. '(module
  645. (func (export "main") (result i32)
  646. (f32.ge (f32.const -42.0) (f32.const 42.0)))))
  647. (test-vm "f32.add"
  648. 3.5
  649. '(module
  650. (func (export "main") (result f32)
  651. (f32.add (f32.const 1.5) (f32.const 2.0)))))
  652. (test-vm "f32.sub"
  653. 2.5
  654. '(module
  655. (func (export "main") (result f32)
  656. (f32.sub (f32.const 3.0) (f32.const 0.5)))))
  657. (test-vm "f32.mul"
  658. 4.5
  659. '(module
  660. (func (export "main") (result f32)
  661. (f32.mul (f32.const 1.5) (f32.const 3.0)))))
  662. (test-vm "f32.div"
  663. 0.5
  664. '(module
  665. (func (export "main") (result f32)
  666. (f32.div (f32.const 1.0) (f32.const 2.0)))))
  667. (test-vm "f32.abs"
  668. 1.5
  669. '(module
  670. (func (export "main") (result f32)
  671. (f32.abs (f32.const -1.5)))))
  672. (test-vm "f32.neg"
  673. -1.5
  674. '(module
  675. (func (export "main") (result f32)
  676. (f32.neg (f32.const 1.5)))))
  677. (test-vm "f32.ceil"
  678. 2.0
  679. '(module
  680. (func (export "main") (result f32)
  681. (f32.ceil (f32.const 1.5)))))
  682. (test-vm "f32.floor"
  683. 1.0
  684. '(module
  685. (func (export "main") (result f32)
  686. (f32.floor (f32.const 1.5)))))
  687. (test-vm "f32.trunc"
  688. 1.0
  689. '(module
  690. (func (export "main") (result f32)
  691. (f32.trunc (f32.const 1.5)))))
  692. (test-vm "f32.nearest"
  693. 1.0
  694. '(module
  695. (func (export "main") (result f32)
  696. (f32.nearest (f32.const 1.4)))))
  697. (test-vm "f32.sqrt"
  698. 2.0
  699. '(module
  700. (func (export "main") (result f32)
  701. (f32.sqrt (f32.const 4.0)))))
  702. (test-vm "f32.min"
  703. 0.5
  704. '(module
  705. (func (export "main") (result f32)
  706. (f32.min (f32.const 0.5) (f32.const 1.5)))))
  707. (test-vm "f32.max"
  708. 1.5
  709. '(module
  710. (func (export "main") (result f32)
  711. (f32.max (f32.const 0.5) (f32.const 1.5)))))
  712. (test-vm "f32.copysign +42.0, +inf.0"
  713. +42.0
  714. '(module
  715. (func (export "main") (result f32)
  716. (f32.copysign (f32.const +42.0) (f32.const +inf.0)))))
  717. (test-vm "f32.copysign -42.0, +inf.0"
  718. +42.0
  719. '(module
  720. (func (export "main") (result f32)
  721. (f32.copysign (f32.const -42.0) (f32.const +inf.0)))))
  722. (test-vm "f32.copysign +42.0, -inf.0"
  723. -42.0
  724. '(module
  725. (func (export "main") (result f32)
  726. (f32.copysign (f32.const +42.0) (f32.const -inf.0)))))
  727. (test-vm "f32.copysign -42.0, -inf.0"
  728. -42.0
  729. '(module
  730. (func (export "main") (result f32)
  731. (f32.copysign (f32.const -42.0) (f32.const -inf.0)))))
  732. (test-vm "f32.copysign +42.0, +nan.0"
  733. +42.0
  734. '(module
  735. (func (export "main") (result f32)
  736. (f32.copysign (f32.const +42.0) (f32.const +nan.0)))))
  737. (test-vm "f32.copysign -42.0, +nan.0"
  738. 42.0
  739. '(module
  740. (func (export "main") (result f32)
  741. (f32.copysign (f32.const -42.0) (f32.const +nan.0)))))
  742. (test-vm "f32.copysign +42.0, +0.0"
  743. 42.0
  744. '(module
  745. (func (export "main") (result f32)
  746. (f32.copysign (f32.const +42.0) (f32.const +0.0)))))
  747. (test-vm "f32.copysign -42.0, +0.0"
  748. +42.0
  749. '(module
  750. (func (export "main") (result f32)
  751. (f32.copysign (f32.const -42.0) (f32.const +0.0)))))
  752. (test-vm "f32.copysign +42.0, -0.0"
  753. -42.0
  754. '(module
  755. (func (export "main") (result f32)
  756. (f32.copysign (f32.const +42.0) (f32.const -0.0)))))
  757. (test-vm "f32.copysign -42.0, -0.0"
  758. -42.0
  759. '(module
  760. (func (export "main") (result f32)
  761. (f32.copysign (f32.const -42.0) (f32.const -0.0)))))
  762. (test-vm "f32.copysign +42.0, +3.14"
  763. +42.0
  764. '(module
  765. (func (export "main") (result f32)
  766. (f32.copysign (f32.const +42.0) (f32.const +3.14)))))
  767. (test-vm "f32.copysign -42.0, +3.14"
  768. +42.0
  769. '(module
  770. (func (export "main") (result f32)
  771. (f32.copysign (f32.const -42.0) (f32.const +3.14)))))
  772. (test-vm "f32.copysign +42.0, -3.14"
  773. -42.0
  774. '(module
  775. (func (export "main") (result f32)
  776. (f32.copysign (f32.const +42.0) (f32.const -3.14)))))
  777. (test-vm "f32.copysign -42.0, -3.14"
  778. -42.0
  779. '(module
  780. (func (export "main") (result f32)
  781. (f32.copysign (f32.const -42.0) (f32.const -3.14)))))
  782. (test-vm "f32.convert_i32_s"
  783. -42.0
  784. '(module
  785. (func (export "main") (result f32)
  786. (f32.convert_i32_s (i32.const -42)))))
  787. (test-vm "f32.convert_i32_u"
  788. 42.0
  789. '(module
  790. (func (export "main") (result f32)
  791. (f32.convert_i32_u (i32.const 42)))))
  792. (test-vm "f32.convert_i64_s"
  793. -42.0
  794. '(module
  795. (func (export "main") (result f32)
  796. (f32.convert_i64_s (i64.const -42)))))
  797. (test-vm "f32.convert_i64_u"
  798. 42.0
  799. '(module
  800. (func (export "main") (result f32)
  801. (f32.convert_i64_u (i64.const 42)))))
  802. (test-vm "f32.demote_f64"
  803. 42.0
  804. '(module
  805. (func (export "main") (result f32)
  806. (f32.demote_f64 (f64.const 42.0)))))
  807. (test-vm "f32.reinterpret_i32"
  808. 1.5
  809. '(module
  810. (func (export "main") (result f32)
  811. (f32.reinterpret_i32 (i32.const 1069547520)))))
  812. (test-vm "f64.eq true"
  813. 1
  814. '(module
  815. (func (export "main") (result i32)
  816. (f64.eq (f64.const 42.0) (f64.const 42.0)))))
  817. (test-vm "f64.eq false"
  818. 0
  819. '(module
  820. (func (export "main") (result i32)
  821. (f64.eq (f64.const 42.0) (f64.const 7.0)))))
  822. (test-vm "f64.ne true"
  823. 1
  824. '(module
  825. (func (export "main") (result i32)
  826. (f64.ne (f64.const 42.0) (f64.const 7.0)))))
  827. (test-vm "f64.ne false"
  828. 0
  829. '(module
  830. (func (export "main") (result i32)
  831. (f64.ne (f64.const 42.0) (f64.const 42.0)))))
  832. (test-vm "f64.lt true"
  833. 1
  834. '(module
  835. (func (export "main") (result i32)
  836. (f64.lt (f64.const -42.0) (f64.const 42.0)))))
  837. (test-vm "f64.lt false"
  838. 0
  839. '(module
  840. (func (export "main") (result i32)
  841. (f64.lt (f64.const 42.0) (f64.const -42.0)))))
  842. (test-vm "f64.le true"
  843. 1
  844. '(module
  845. (func (export "main") (result i32)
  846. (f64.le (f64.const 42.0) (f64.const 42.0)))))
  847. (test-vm "f64.le false"
  848. 0
  849. '(module
  850. (func (export "main") (result i32)
  851. (f64.le (f64.const 42.0) (f64.const -42.0)))))
  852. (test-vm "f64.gt true"
  853. 1
  854. '(module
  855. (func (export "main") (result i32)
  856. (f64.gt (f64.const 42.0) (f64.const -42.0)))))
  857. (test-vm "f64.gt false"
  858. 0
  859. '(module
  860. (func (export "main") (result i32)
  861. (f64.gt (f64.const -42.0) (f64.const 42.0)))))
  862. (test-vm "f64.ge true"
  863. 1
  864. '(module
  865. (func (export "main") (result i32)
  866. (f64.ge (f64.const 42.0) (f64.const 42.0)))))
  867. (test-vm "f64.ge false"
  868. 0
  869. '(module
  870. (func (export "main") (result i32)
  871. (f64.ge (f64.const -42.0) (f64.const 42.0)))))
  872. (test-vm "f64.add"
  873. 3.5
  874. '(module
  875. (func (export "main") (result f64)
  876. (f64.add (f64.const 1.5) (f64.const 2.0)))))
  877. (test-vm "f64.sub"
  878. 2.5
  879. '(module
  880. (func (export "main") (result f64)
  881. (f64.sub (f64.const 3.0) (f64.const 0.5)))))
  882. (test-vm "f64.mul"
  883. 4.5
  884. '(module
  885. (func (export "main") (result f64)
  886. (f64.mul (f64.const 1.5) (f64.const 3.0)))))
  887. (test-vm "f64.div"
  888. 0.5
  889. '(module
  890. (func (export "main") (result f64)
  891. (f64.div (f64.const 1.0) (f64.const 2.0)))))
  892. (test-vm "f64.abs"
  893. 1.5
  894. '(module
  895. (func (export "main") (result f64)
  896. (f64.abs (f64.const -1.5)))))
  897. (test-vm "f64.neg"
  898. -1.5
  899. '(module
  900. (func (export "main") (result f64)
  901. (f64.neg (f64.const 1.5)))))
  902. (test-vm "f64.ceil"
  903. 2.0
  904. '(module
  905. (func (export "main") (result f64)
  906. (f64.ceil (f64.const 1.5)))))
  907. (test-vm "f64.floor"
  908. 1.0
  909. '(module
  910. (func (export "main") (result f64)
  911. (f64.floor (f64.const 1.5)))))
  912. (test-vm "f64.trunc"
  913. 1.0
  914. '(module
  915. (func (export "main") (result f64)
  916. (f64.trunc (f64.const 1.5)))))
  917. (test-vm "f64.nearest"
  918. 1.0
  919. '(module
  920. (func (export "main") (result f64)
  921. (f64.nearest (f64.const 1.4)))))
  922. (test-vm "f64.sqrt"
  923. 2.0
  924. '(module
  925. (func (export "main") (result f64)
  926. (f64.sqrt (f64.const 4.0)))))
  927. (test-vm "f64.min"
  928. 0.5
  929. '(module
  930. (func (export "main") (result f64)
  931. (f64.min (f64.const 0.5) (f64.const 1.5)))))
  932. (test-vm "f64.max"
  933. 1.5
  934. '(module
  935. (func (export "main") (result f64)
  936. (f64.max (f64.const 0.5) (f64.const 1.5)))))
  937. (test-vm "f64.copysign +42.0, +inf.0"
  938. +42.0
  939. '(module
  940. (func (export "main") (result f64)
  941. (f64.copysign (f64.const +42.0) (f64.const +inf.0)))))
  942. (test-vm "f64.copysign -42.0, +inf.0"
  943. +42.0
  944. '(module
  945. (func (export "main") (result f64)
  946. (f64.copysign (f64.const -42.0) (f64.const +inf.0)))))
  947. (test-vm "f64.copysign +42.0, -inf.0"
  948. -42.0
  949. '(module
  950. (func (export "main") (result f64)
  951. (f64.copysign (f64.const +42.0) (f64.const -inf.0)))))
  952. (test-vm "f64.copysign -42.0, -inf.0"
  953. -42.0
  954. '(module
  955. (func (export "main") (result f64)
  956. (f64.copysign (f64.const -42.0) (f64.const -inf.0)))))
  957. (test-vm "f64.copysign +42.0, +nan.0"
  958. +42.0
  959. '(module
  960. (func (export "main") (result f64)
  961. (f64.copysign (f64.const +42.0) (f64.const +nan.0)))))
  962. (test-vm "f64.copysign -42.0, +nan.0"
  963. 42.0
  964. '(module
  965. (func (export "main") (result f64)
  966. (f64.copysign (f64.const -42.0) (f64.const +nan.0)))))
  967. (test-vm "f64.copysign +42.0, +0.0"
  968. 42.0
  969. '(module
  970. (func (export "main") (result f64)
  971. (f64.copysign (f64.const +42.0) (f64.const +0.0)))))
  972. (test-vm "f64.copysign -42.0, +0.0"
  973. +42.0
  974. '(module
  975. (func (export "main") (result f64)
  976. (f64.copysign (f64.const -42.0) (f64.const +0.0)))))
  977. (test-vm "f64.copysign +42.0, -0.0"
  978. -42.0
  979. '(module
  980. (func (export "main") (result f64)
  981. (f64.copysign (f64.const +42.0) (f64.const -0.0)))))
  982. (test-vm "f64.copysign -42.0, -0.0"
  983. -42.0
  984. '(module
  985. (func (export "main") (result f64)
  986. (f64.copysign (f64.const -42.0) (f64.const -0.0)))))
  987. (test-vm "f64.copysign +42.0, +3.14"
  988. +42.0
  989. '(module
  990. (func (export "main") (result f64)
  991. (f64.copysign (f64.const +42.0) (f64.const +3.14)))))
  992. (test-vm "f64.copysign -42.0, +3.14"
  993. +42.0
  994. '(module
  995. (func (export "main") (result f64)
  996. (f64.copysign (f64.const -42.0) (f64.const +3.14)))))
  997. (test-vm "f64.copysign +42.0, -3.14"
  998. -42.0
  999. '(module
  1000. (func (export "main") (result f64)
  1001. (f64.copysign (f64.const +42.0) (f64.const -3.14)))))
  1002. (test-vm "f64.copysign -42.0, -3.14"
  1003. -42.0
  1004. '(module
  1005. (func (export "main") (result f64)
  1006. (f64.copysign (f64.const -42.0) (f64.const -3.14)))))
  1007. (test-vm "f64.convert_i32_s"
  1008. -42.0
  1009. '(module
  1010. (func (export "main") (result f64)
  1011. (f64.convert_i32_s (i32.const -42)))))
  1012. (test-vm "f64.convert_i32_u"
  1013. 42.0
  1014. '(module
  1015. (func (export "main") (result f64)
  1016. (f64.convert_i32_u (i32.const 42)))))
  1017. (test-vm "f64.convert_i64_s"
  1018. -42.0
  1019. '(module
  1020. (func (export "main") (result f64)
  1021. (f64.convert_i64_s (i64.const -42)))))
  1022. (test-vm "f64.convert_i64_u"
  1023. 42.0
  1024. '(module
  1025. (func (export "main") (result f64)
  1026. (f64.convert_i64_u (i64.const 42)))))
  1027. (test-vm "f64.promote_f32"
  1028. 42.0
  1029. '(module
  1030. (func (export "main") (result f64)
  1031. (f64.promote_f32 (f32.const 42.0)))))
  1032. (test-vm "f64.reinterpret_i64"
  1033. 1.5
  1034. '(module
  1035. (func (export "main") (result f64)
  1036. (f64.reinterpret_i64 (i64.const 4609434218613702656)))))
  1037. (test-vm "i32.store + i32.load"
  1038. 42
  1039. '(module
  1040. (memory 1)
  1041. (func (export "main") (result i32)
  1042. (i32.store (i32.const 0) (i32.const 42))
  1043. (i32.load (i32.const 0)))))
  1044. (test-vm "i32.store16 + i32.load16_s"
  1045. -42
  1046. '(module
  1047. (memory $memory 1)
  1048. (func (export "main") (result i32)
  1049. (i32.store16 (i32.const 0) (i32.const -42))
  1050. (i32.load16_s (i32.const 0)))))
  1051. (test-vm "i32.store16 + i32.load16_s wrap"
  1052. -1
  1053. '(module
  1054. (memory $memory 1)
  1055. (func (export "main") (result i32)
  1056. (i32.store16 (i32.const 0) (i32.const -65537))
  1057. (i32.load16_s (i32.const 0)))))
  1058. (test-vm "i32.store16 + i32.load16_u"
  1059. 65535
  1060. '(module
  1061. (memory $memory 1)
  1062. (func (export "main") (result i32)
  1063. (i32.store16 (i32.const 0) (i32.const 65535))
  1064. (i32.load16_u (i32.const 0)))))
  1065. (test-vm "i32.store16 + i32.load16_u wrap"
  1066. 65535
  1067. '(module
  1068. (memory $memory 1)
  1069. (func (export "main") (result i32)
  1070. (i32.store16 (i32.const 0) (i32.const -65537))
  1071. (i32.load16_u (i32.const 0)))))
  1072. (test-vm "i32.store8 + i32.load8_s"
  1073. -42
  1074. '(module
  1075. (memory $memory 1)
  1076. (func (export "main") (result i32)
  1077. (i32.store8 (i32.const 0) (i32.const -42))
  1078. (i32.load8_s (i32.const 0)))))
  1079. (test-vm "i32.store8 + i32.load8_s wrap"
  1080. -1
  1081. '(module
  1082. (memory $memory 1)
  1083. (func (export "main") (result i32)
  1084. (i32.store8 (i32.const 0) (i32.const -257))
  1085. (i32.load8_s (i32.const 0)))))
  1086. (test-vm "i32.store8 + i32.load8_u"
  1087. 255
  1088. '(module
  1089. (memory $memory 1)
  1090. (func (export "main") (result i32)
  1091. (i32.store8 (i32.const 0) (i32.const 255))
  1092. (i32.load8_u (i32.const 0)))))
  1093. (test-vm "i32.store8 + i32.load8_u wrap"
  1094. 255
  1095. '(module
  1096. (memory $memory 1)
  1097. (func (export "main") (result i32)
  1098. (i32.store8 (i32.const 0) (i32.const -257))
  1099. (i32.load8_u (i32.const 0)))))
  1100. (test-vm "i64.store + i64.load"
  1101. 42
  1102. '(module
  1103. (memory 1)
  1104. (func (export "main") (result i64)
  1105. (i64.store (i32.const 0) (i64.const 42))
  1106. (i64.load (i32.const 0)))))
  1107. (test-vm "i64.store32 + i64.load32_s"
  1108. -42
  1109. '(module
  1110. (memory $memory 1)
  1111. (func (export "main") (result i64)
  1112. (i64.store32 (i32.const 0) (i64.const -42))
  1113. (i64.load32_s (i32.const 0)))))
  1114. (test-vm "i64.store32 + i64.load32_s wrap"
  1115. -1
  1116. `(module
  1117. (memory $memory 1)
  1118. (func (export "main") (result i64)
  1119. (i64.store32 (i32.const 0) (i64.const ,(- (ash 1 32) 1)))
  1120. (i64.load32_s (i32.const 0)))))
  1121. (test-vm "i64.store32 + i64.load32_u"
  1122. (- (ash 1 32) 1)
  1123. `(module
  1124. (memory $memory 1)
  1125. (func (export "main") (result i64)
  1126. (i64.store32 (i32.const 0) (i64.const ,(- (ash 1 32) 1)))
  1127. (i64.load32_u (i32.const 0)))))
  1128. (test-vm "i64.store32 + i64.load32_u wrap"
  1129. (- (ash 1 32) 1)
  1130. `(module
  1131. (memory $memory 1)
  1132. (func (export "main") (result i64)
  1133. (i64.store32 (i32.const 0) (i64.const ,(- (ash -1 32) 1)))
  1134. (i64.load32_u (i32.const 0)))))
  1135. (test-vm "i64.store16 + i64.load16_s"
  1136. -42
  1137. '(module
  1138. (memory $memory 1)
  1139. (func (export "main") (result i64)
  1140. (i64.store16 (i32.const 0) (i64.const -42))
  1141. (i64.load16_s (i32.const 0)))))
  1142. (test-vm "i64.store16 + i64.load16_s wrap"
  1143. -1
  1144. '(module
  1145. (memory $memory 1)
  1146. (func (export "main") (result i64)
  1147. (i64.store16 (i32.const 0) (i64.const -65537))
  1148. (i64.load16_s (i32.const 0)))))
  1149. (test-vm "i64.store16 + i64.load16_u"
  1150. 65535
  1151. '(module
  1152. (memory $memory 1)
  1153. (func (export "main") (result i64)
  1154. (i64.store16 (i32.const 0) (i64.const 65535))
  1155. (i64.load16_u (i32.const 0)))))
  1156. (test-vm "i64.store16 + i64.load16_u wrap"
  1157. 65535
  1158. '(module
  1159. (memory $memory 1)
  1160. (func (export "main") (result i64)
  1161. (i64.store16 (i32.const 0) (i64.const -65537))
  1162. (i64.load16_u (i32.const 0)))))
  1163. (test-vm "i64.store8 + i64.load8_s"
  1164. -42
  1165. '(module
  1166. (memory $memory 1)
  1167. (func (export "main") (result i64)
  1168. (i64.store8 (i32.const 0) (i64.const -42))
  1169. (i64.load8_s (i32.const 0)))))
  1170. (test-vm "i64.store8 + i64.load8_s wrap"
  1171. -1
  1172. '(module
  1173. (memory $memory 1)
  1174. (func (export "main") (result i64)
  1175. (i64.store8 (i32.const 0) (i64.const -257))
  1176. (i64.load8_s (i32.const 0)))))
  1177. (test-vm "i64.store8 + i64.load8_u"
  1178. 255
  1179. '(module
  1180. (memory $memory 1)
  1181. (func (export "main") (result i64)
  1182. (i64.store8 (i32.const 0) (i64.const 255))
  1183. (i64.load8_u (i32.const 0)))))
  1184. (test-vm "i64.store8 + i64.load8_u wrap"
  1185. 255
  1186. '(module
  1187. (memory $memory 1)
  1188. (func (export "main") (result i64)
  1189. (i64.store8 (i32.const 0) (i64.const -257))
  1190. (i64.load8_u (i32.const 0)))))
  1191. (test-vm "f32.store + f32.load"
  1192. 1.5
  1193. '(module
  1194. (memory 1)
  1195. (func (export "main") (result f32)
  1196. (f32.store (i32.const 0) (f32.const 1.5))
  1197. (f32.load (i32.const 0)))))
  1198. (test-vm "f64.store + f64.load"
  1199. 1.5
  1200. '(module
  1201. (memory 1)
  1202. (func (export "main") (result f64)
  1203. (f64.store (i32.const 0) (f64.const 1.5))
  1204. (f64.load (i32.const 0)))))
  1205. (test-vm "memory.size"
  1206. 1
  1207. '(module
  1208. (memory 1)
  1209. (func (export "main") (result i32)
  1210. (memory.size))))
  1211. (test-vm "memory.grow within limits"
  1212. 2
  1213. '(module
  1214. (memory 2 3)
  1215. (func (export "main") (result i32)
  1216. (i32.const 1)
  1217. (memory.grow))))
  1218. (test-vm "memory.grow outside limits"
  1219. -1
  1220. '(module
  1221. (memory 1 1)
  1222. (func (export "main") (result i32)
  1223. (i32.const 1)
  1224. (memory.grow))))
  1225. (test-vm "memory.grow no-op"
  1226. 1
  1227. '(module
  1228. (memory 1 1)
  1229. (func (export "main") (result i32)
  1230. (i32.const 0)
  1231. (memory.grow))))
  1232. (test-vm "if"
  1233. 37
  1234. '(module
  1235. (func (export "main") (result i32)
  1236. (if (result i32)
  1237. (i32.eq (i32.const 42) (i32.const 47))
  1238. (then (i32.const 13))
  1239. (else (i32.const 37))))))
  1240. (test-vm "branching in a block"
  1241. 42
  1242. '(module
  1243. (func (export "main") (result i32)
  1244. (block $foo
  1245. (br $foo)
  1246. (unreachable))
  1247. (i32.const 42))))
  1248. (test-vm "return"
  1249. 42
  1250. '(module
  1251. (func (export "main") (result i32)
  1252. (block $foo (result i32)
  1253. (i32.const 42)
  1254. (return)
  1255. (unreachable)))))
  1256. (test-vm "return via br"
  1257. 42
  1258. '(module
  1259. (func (export "main") (result i32)
  1260. (i32.const 42)
  1261. (br 0)
  1262. (unreachable))))
  1263. (test-vm "return_call"
  1264. 42
  1265. '(module
  1266. (func $count (param $i i32) (param $k i32) (result i32)
  1267. (if (result i32)
  1268. (i32.eq (local.get $i) (local.get $k))
  1269. (then (local.get $k))
  1270. (else (return_call $count
  1271. (i32.add (local.get $i) (i32.const 1))
  1272. (local.get $k)))))
  1273. (func $main (export "main") (param $k i32) (result i32)
  1274. (call $count (i32.const 0) (local.get $k))))
  1275. #:args '(42))
  1276. (test-vm "return with extra values on stack"
  1277. 42
  1278. '(module
  1279. (func (export "main") (result i32)
  1280. (block $foo (result i32)
  1281. (i32.const 13)
  1282. (i32.const 42)
  1283. (return)))))
  1284. (test-vm/error "fallthrough with too many values on stack"
  1285. '(module
  1286. (func (export "main") (result i32)
  1287. (i32.const 1)
  1288. (i32.const 2))))
  1289. (test-vm/error "fallthrough with too many values on invalid stack"
  1290. '(module
  1291. (func (export "main") (result i32)
  1292. (i32.const 1)
  1293. (return) ; stack is invalid after this
  1294. (i32.const 2)
  1295. (i32.const 3))))
  1296. (test-vm "branching in a loop"
  1297. 24
  1298. '(module
  1299. ;; Iterative factorial.
  1300. (func (export "main") (param $n i32) (result i32)
  1301. (local $result i32)
  1302. (local.set $result (i32.const 1))
  1303. (loop $loop (result i32)
  1304. (if (result i32)
  1305. (i32.eq (local.get $n) (i32.const 1))
  1306. (then (local.get $result))
  1307. (else (local.set $result (i32.mul (local.get $n)
  1308. (local.get $result)))
  1309. (local.set $n (i32.sub (local.get $n) (i32.const 1)))
  1310. (br $loop))))))
  1311. #:args '(4))
  1312. (test-vm "blocks with params and results"
  1313. 24
  1314. '(module
  1315. (func (export "main") (param $n i32) (result i32)
  1316. (local.get $n)
  1317. (block $block (param i32) (result i32)
  1318. (loop $loop (param i32) (result i32)
  1319. (i32.eq (local.get $n) (i32.const 1))
  1320. (br_if $block)
  1321. (local.tee $n (i32.sub (local.get $n) (i32.const 1)))
  1322. (i32.mul)
  1323. (br $loop)))))
  1324. #:args '(4))
  1325. (test-vm "br_table"
  1326. 42
  1327. '(module
  1328. (func (export "main") (result i32)
  1329. (block $foo (result i32)
  1330. (block $bar (result i32)
  1331. (i32.const 21)
  1332. (i32.const 0)
  1333. (br_table $foo $bar))
  1334. (unreachable))
  1335. (i32.const 21)
  1336. (i32.add))))
  1337. (test-vm "br_table with negative i"
  1338. 42
  1339. '(module
  1340. (func (export "main") (result i32)
  1341. (block $foo (result i32)
  1342. (block $bar (result i32)
  1343. (i32.const 21)
  1344. (i32.const -1)
  1345. (br_table $bar $foo))
  1346. (unreachable))
  1347. (i32.const 21)
  1348. (i32.add))))
  1349. (test-vm "inner function call"
  1350. 11
  1351. '(module
  1352. (func $y (param $m i32) (param $x i32) (param $b i32) (result i32)
  1353. (i32.add (i32.mul (local.get $m) (local.get $x)) (local.get $b)))
  1354. (func (export "main") (param $x i32) (result i32)
  1355. (call $y (i32.const 2) (local.get $x) (i32.const 3))))
  1356. #:args '(4))
  1357. (test-vm "imported function call"
  1358. 80
  1359. '(module
  1360. (func $add42 (import "lib" "add42")
  1361. (param i32) (result i32))
  1362. (func (export "main") (param $x i32) (result i32)
  1363. (call $add42 (local.get $x))))
  1364. #:imports `(("lib" . (("add42" . ,(lambda (x) (+ x 42))))))
  1365. #:args '(38)
  1366. #:v8? #f)
  1367. (test-vm "mutable globals"
  1368. 42
  1369. '(module
  1370. (global $foo (mut i32) (i32.const 30))
  1371. (func (export "main") (param $n i32) (result i32)
  1372. (global.set $foo (i32.add (local.get $n) (global.get $foo)))
  1373. (global.get $foo)))
  1374. #:args '(12))
  1375. (test-vm "imported globals"
  1376. 80
  1377. '(module
  1378. (global $foo i32 (i32.const 42))
  1379. (global $bar (import "globals" "bar") i32)
  1380. (func (export "main") (result i32)
  1381. (i32.add (global.get $foo) (global.get $bar))))
  1382. #:imports `(("globals" . (("bar" . ,(make-wasm-global 38 #f)))))
  1383. #:v8? #f)
  1384. (test-vm "immutable global reference in constant expression"
  1385. 42
  1386. '(module
  1387. (global $foo i32 (i32.const 42))
  1388. (global $bar i32 (global.get $foo))
  1389. (func (export "main") (result i32)
  1390. (global.get $bar))))
  1391. (test-vm/error "reference to mutable global in constant expression"
  1392. '(module
  1393. (global $foo (mut i32) (i32.const 42))
  1394. (global $bar i32 (global.get $foo))
  1395. (func (export "main") (result i32)
  1396. (global.get $bar))))
  1397. (test-vm/error "reference to subsequent global in constant expression"
  1398. '(module
  1399. (global $bar i32 (global.get $foo))
  1400. (global $foo i32 (i32.const 42))
  1401. (func (export "main") (result i32)
  1402. (global.get $bar))))
  1403. (test-vm "drop"
  1404. 42
  1405. '(module
  1406. (func (export "main") (result i32)
  1407. (i32.const 42)
  1408. (i32.const 13)
  1409. (drop))))
  1410. (test-vm "drop in unreachable code"
  1411. 42
  1412. '(module
  1413. (func (export "main") (result i32)
  1414. (i32.const 42)
  1415. (return)
  1416. (drop))))
  1417. (test-vm "select first"
  1418. 42
  1419. '(module
  1420. (func (export "main") (result i32)
  1421. (i32.const 42)
  1422. (i32.const 13)
  1423. (i32.const 1)
  1424. (select))))
  1425. (test-vm "select second"
  1426. 13
  1427. '(module
  1428. (func (export "main") (result i32)
  1429. (i32.const 42)
  1430. (i32.const 13)
  1431. (i32.const 0)
  1432. (select))))
  1433. (test-vm "select in unreachable code"
  1434. 42
  1435. '(module
  1436. (func (export "main") (result i32)
  1437. (i32.const 42)
  1438. (return)
  1439. (select))))
  1440. (test-vm "select in unreachable code with i32 on top"
  1441. 42
  1442. '(module
  1443. (func (export "main") (result i32)
  1444. (i32.const 42)
  1445. (return)
  1446. (i32.const 0)
  1447. (select))))
  1448. (test-vm "start function"
  1449. 42
  1450. '(module
  1451. (global $foo (mut i32) (i32.const 13))
  1452. (func $init
  1453. (global.set $foo (i32.const 42)))
  1454. (start $init)
  1455. (func (export "main") (result i32)
  1456. (global.get $foo))))
  1457. (test-vm "table.size"
  1458. 2
  1459. '(module
  1460. (table $funcs 2 funcref)
  1461. (func (export "main") (result i32)
  1462. (table.size $funcs))))
  1463. (test-vm "table.grow within limits"
  1464. 2
  1465. '(module
  1466. (table $funcs 2 3 funcref)
  1467. (func $main (export "main") (result i32)
  1468. (ref.func $main)
  1469. (i32.const 1)
  1470. (table.grow $funcs))))
  1471. (test-vm "table.grow outside limits"
  1472. -1
  1473. '(module
  1474. (table $funcs 1 1 funcref)
  1475. (func $main (export "main") (result i32)
  1476. (ref.func $main)
  1477. (i32.const 1)
  1478. (table.grow $funcs))))
  1479. (test-vm "table.grow no-op"
  1480. 1
  1481. '(module
  1482. (table $funcs 1 1 funcref)
  1483. (func $main (export "main") (result i32)
  1484. (ref.func $main)
  1485. (i32.const 0)
  1486. (table.grow $funcs))))
  1487. (test-vm "table.init with passive element segment"
  1488. 1
  1489. '(module
  1490. (table $a 1 funcref)
  1491. (elem $funcs funcref (ref.null func))
  1492. (func $main (export "main") (result i32)
  1493. (table.init $a $funcs
  1494. (i32.const 0)
  1495. (i32.const 0)
  1496. (i32.const 1))
  1497. (ref.is_null (table.get $a (i32.const 0))))))
  1498. (test-vm "table.fill"
  1499. 1
  1500. '(module
  1501. (table $funcs 3 funcref)
  1502. (elem $funcs (i32.const 0) $main)
  1503. (func $main (export "main") (result i32)
  1504. (i32.const 1)
  1505. (ref.func $main)
  1506. (i32.const 2)
  1507. (table.fill $funcs)
  1508. (i32.const 1))))
  1509. (test-vm "table.copy"
  1510. 1
  1511. '(module
  1512. (table $a 3 funcref)
  1513. (table $b 3 funcref)
  1514. (elem $funcs (i32.const 0) $main $main $main)
  1515. (func $main (export "main") (result i32)
  1516. (i32.const 3)
  1517. (i32.const 0)
  1518. (i32.const 0)
  1519. (table.copy $b $a)
  1520. (i32.const 1))))
  1521. (test-vm "elem.drop"
  1522. 1
  1523. '(module
  1524. (table $funcs 1 funcref)
  1525. (elem $foo (i32.const 0) $main)
  1526. (func $main (export "main") (result i32)
  1527. (elem.drop $foo)
  1528. (i32.const 1))))
  1529. (test-vm "call_indirect"
  1530. 42
  1531. '(module
  1532. (table $funcs 1 funcref)
  1533. (type $i32->i32 (func (param i32) (result i32)))
  1534. (func $inc (param $a i32) (result i32)
  1535. (i32.add (local.get $a) (i32.const 1)))
  1536. (elem $funcs (i32.const 0) $inc)
  1537. (func (export "main") (result i32)
  1538. (call_indirect $funcs (type $i32->i32)
  1539. (i32.const 41) (i32.const 0)))))
  1540. (test-vm "call_ref"
  1541. 42
  1542. '(module
  1543. (type $i32->i32 (func (param i32) (result i32)))
  1544. (func $sum-41 (param $x i32) (result i32)
  1545. (i32.add (local.get $x) (i32.const 41)))
  1546. (func (export "main") (result i32)
  1547. (call_ref $i32->i32 (i32.const 1) (ref.func $sum-41)))))
  1548. (test-vm "ref.null + ref.is_null"
  1549. 1
  1550. '(module
  1551. (func (export "main") (result i32)
  1552. (ref.null func)
  1553. (ref.is_null))))
  1554. (test-vm "ref.test true"
  1555. 1
  1556. '(module
  1557. (type $foo (struct (field $bar (ref eq))))
  1558. (func (export "main") (result i32)
  1559. (ref.test $foo (struct.new $foo (ref.i31 (i32.const 42)))))))
  1560. (test-vm "ref.test false"
  1561. 0
  1562. '(module
  1563. (type $foo (struct (field $bar (ref eq))))
  1564. (func (export "main") (result i32)
  1565. (ref.test $foo (ref.null $foo)))))
  1566. (test-vm "ref.as_non_null"
  1567. 42
  1568. '(module
  1569. (type $foo (struct (field $bar i32)))
  1570. (func (export "main") (result i32)
  1571. (struct.get $foo $bar
  1572. (ref.as_non_null
  1573. (struct.new $foo (i32.const 42)))))))
  1574. (test-vm "ref.cast identity non-null"
  1575. 42
  1576. '(module
  1577. (func (export "main") (result i32)
  1578. (ref.i31 (i32.const 42))
  1579. (ref.cast i31)
  1580. (i31.get_s))))
  1581. (test-vm "ref.cast identity null"
  1582. 42
  1583. '(module
  1584. (func (export "main") (result i32)
  1585. (ref.null i31)
  1586. (ref.cast null i31)
  1587. (drop)
  1588. (i32.const 42))))
  1589. (test-vm/error "ref.cast null"
  1590. '(module
  1591. (func (export "main") (result i32)
  1592. (ref.null i31)
  1593. (ref.cast i31)
  1594. (i31.get_s))))
  1595. (test-vm "external reference passthrough"
  1596. '(opaque to wasm)
  1597. '(module
  1598. (func (export "main") (param $foo externref) (result externref)
  1599. (local.get $foo)))
  1600. #:args '((opaque to wasm))
  1601. #:v8? #f)
  1602. (test-vm "i31.get_s"
  1603. -42
  1604. '(module
  1605. (func (export "main") (result i32)
  1606. (i31.get_s (ref.i31 (i32.const -42))))))
  1607. (test-vm "i31.get_u"
  1608. 2147483606
  1609. '(module
  1610. (func (export "main") (result i32)
  1611. (i31.get_u (ref.i31 (i32.const -42))))))
  1612. (test-vm "struct.get"
  1613. 5.0
  1614. '(module
  1615. (type $vec2 (struct (field $x f32) (field $y f32)))
  1616. (func (export "main") (result f32) (local $v (ref $vec2))
  1617. (local.set $v (struct.new $vec2 (f32.const 3.0) (f32.const 4.0)))
  1618. ;; Calculate vector magnitude, for fun.
  1619. (f32.sqrt
  1620. (f32.add (f32.mul (struct.get $vec2 $x (local.get $v))
  1621. (struct.get $vec2 $x (local.get $v)))
  1622. (f32.mul (struct.get $vec2 $y (local.get $v))
  1623. (struct.get $vec2 $y (local.get $v))))))))
  1624. (test-vm "struct.get_s"
  1625. -42
  1626. '(module
  1627. (type $foo (struct (field $bar i8)))
  1628. (func (export "main") (result i32)
  1629. (struct.get_s $foo $bar (struct.new $foo (i32.const -42))))))
  1630. (test-vm "struct.get_u"
  1631. 214
  1632. '(module
  1633. (type $foo (struct (field $bar i8)))
  1634. (func (export "main") (result i32)
  1635. (struct.get_u $foo $bar (struct.new $foo (i32.const -42))))))
  1636. (test-vm "struct.set"
  1637. 42
  1638. '(module
  1639. (type $foo (struct (field $bar (mut i32))))
  1640. (func (export "main") (result i32) (local $a (ref $foo))
  1641. (local.set $a (struct.new $foo (i32.const 0)))
  1642. (struct.set $foo $bar (local.get $a) (i32.const 42))
  1643. (struct.get $foo $bar (local.get $a)))))
  1644. (test-vm "struct subtyping"
  1645. 42
  1646. '(module
  1647. (type $heap-object
  1648. (sub
  1649. (struct
  1650. (field $hash (mut i32)))))
  1651. (type $pair
  1652. (sub $heap-object
  1653. (struct
  1654. (field $hash (mut i32))
  1655. (field $car (mut (ref eq)))
  1656. (field $cdr (mut (ref eq))))))
  1657. (func (export "main") (result i32) (local $a (ref $pair))
  1658. (local.set $a (struct.new $pair (i32.const 1)
  1659. (ref.i31 (i32.const 21))
  1660. (ref.i31 (i32.const 21))))
  1661. (i32.add (i31.get_s (ref.cast i31 (struct.get $pair $car (local.get $a))))
  1662. (i31.get_s (ref.cast i31 (struct.get $pair $cdr (local.get $a))))))))
  1663. (test-vm "struct.new_default"
  1664. 0
  1665. '(module
  1666. (type $foo (struct (field $bar i32)))
  1667. (func (export "main") (result i32)
  1668. (struct.get $foo $bar (struct.new_default $foo)))))
  1669. (test-vm "array.len"
  1670. 3
  1671. '(module
  1672. (type $foo (array i32))
  1673. (func (export "main") (result i32)
  1674. (array.len (array.new $foo (i32.const 0) (i32.const 3))))))
  1675. (test-vm "array.get"
  1676. 42
  1677. '(module
  1678. (type $foo (array i32))
  1679. (func (export "main") (result i32)
  1680. (array.get $foo (array.new $foo (i32.const 42) (i32.const 1)) (i32.const 0)))))
  1681. (test-vm "array.get_s"
  1682. -42
  1683. '(module
  1684. (type $foo (array i8))
  1685. (func (export "main") (result i32)
  1686. (array.get_s $foo (array.new_fixed $foo 1 (i32.const -42)) (i32.const 0)))))
  1687. (test-vm "array.get_u"
  1688. 214
  1689. '(module
  1690. (type $foo (array i8))
  1691. (func (export "main") (result i32)
  1692. (array.get_u $foo (array.new_fixed $foo 1 (i32.const -42)) (i32.const 0)))))
  1693. (test-vm "array.set"
  1694. 42
  1695. '(module
  1696. (type $foo (array (mut i32)))
  1697. (func (export "main") (result i32) (local $a (ref $foo))
  1698. (local.set $a (array.new $foo (i32.const 0) (i32.const 1)))
  1699. (array.set $foo (local.get $a) (i32.const 0) (i32.const 42))
  1700. (array.get $foo (local.get $a) (i32.const 0)))))
  1701. (test-vm "array.new_fixed"
  1702. 42
  1703. '(module
  1704. (type $foo (array i32))
  1705. (func (export "main") (result i32)
  1706. (array.get $foo (array.new_fixed $foo 1 (i32.const 42)) (i32.const 0)))))
  1707. (test-vm "array.new_default"
  1708. 0
  1709. '(module
  1710. (type $foo (array i32))
  1711. (func (export "main") (result i32)
  1712. (array.get $foo (array.new_default $foo (i32.const 1)) (i32.const 0)))))
  1713. (test-vm "array.new_data"
  1714. 4
  1715. '(module
  1716. (type $foo (array i32))
  1717. (data $init #s32(1 2 3 4))
  1718. (func (export "main") (result i32)
  1719. (array.get $foo (array.new_data $foo $init
  1720. (i32.const 0)
  1721. (i32.const 4))
  1722. (i32.const 3)))))
  1723. (test-vm "array.new_elem"
  1724. 4
  1725. '(module
  1726. (type $foo (array (ref i31)))
  1727. (elem $init (ref i31)
  1728. (item (ref.i31 (i32.const 1)))
  1729. (item (ref.i31 (i32.const 2)))
  1730. (item (ref.i31 (i32.const 3)))
  1731. (item (ref.i31 (i32.const 4))))
  1732. (func (export "main") (result i32)
  1733. (i31.get_s
  1734. (array.get $foo (array.new_elem $foo $init
  1735. (i32.const 0)
  1736. (i32.const 4))
  1737. (i32.const 3))))))
  1738. (test-vm "array.init_data"
  1739. 4
  1740. '(module
  1741. (type $foo (array (mut i32)))
  1742. (data $init #s32(1 2 3 4))
  1743. (func (export "main") (result i32)
  1744. (local $a (ref $foo))
  1745. (local.set $a (array.new $foo
  1746. (i32.const 0)
  1747. (i32.const 4)))
  1748. (array.init_data $foo $init
  1749. (local.get $a)
  1750. (i32.const 0)
  1751. (i32.const 0)
  1752. (i32.const 4))
  1753. (array.get $foo (local.get $a) (i32.const 3)))))
  1754. (test-vm "array.init_elem"
  1755. 4
  1756. '(module
  1757. (type $foo (array (mut (ref i31))))
  1758. (elem $init (ref i31)
  1759. (item (ref.i31 (i32.const 1)))
  1760. (item (ref.i31 (i32.const 2)))
  1761. (item (ref.i31 (i32.const 3)))
  1762. (item (ref.i31 (i32.const 4))))
  1763. (func (export "main") (result i32)
  1764. (local $a (ref $foo))
  1765. (local.set $a (array.new $foo
  1766. (ref.i31 (i32.const 0))
  1767. (i32.const 4)))
  1768. (array.init_elem $foo $init
  1769. (local.get $a)
  1770. (i32.const 0)
  1771. (i32.const 0)
  1772. (i32.const 4))
  1773. (i31.get_s
  1774. (array.get $foo (local.get $a) (i32.const 3))))))
  1775. (test-vm "array.fill"
  1776. 42
  1777. '(module
  1778. (type $foo (array (mut i32)))
  1779. (func (export "main") (result i32) (local $a (ref $foo))
  1780. (local.set $a (array.new_default $foo (i32.const 1)))
  1781. (array.fill $foo
  1782. (local.get $a)
  1783. (i32.const 0)
  1784. (i32.const 42)
  1785. (i32.const 1))
  1786. (array.get $foo (local.get $a) (i32.const 0)))))
  1787. (test-vm "array.copy"
  1788. 42
  1789. '(module
  1790. (type $foo (array (mut i32)))
  1791. (func (export "main") (result i32) (local $a (ref $foo))
  1792. (local.set $a (array.new_default $foo (i32.const 1)))
  1793. (array.copy $foo $foo
  1794. (local.get $a)
  1795. (i32.const 0)
  1796. (array.new_fixed $foo 1 (i32.const 42))
  1797. (i32.const 0)
  1798. (i32.const 1))
  1799. (array.get $foo (local.get $a) (i32.const 0)))))
  1800. (test-vm "reference type constants"
  1801. 42
  1802. '(module
  1803. (type $foo (array (ref i31)))
  1804. (global $bar (ref $foo)
  1805. (array.new $foo
  1806. (ref.i31 (i32.const 42))
  1807. (i32.const 1)))
  1808. (func (export "main") (result i32)
  1809. (i31.get_s
  1810. (array.get $foo
  1811. (global.get $bar)
  1812. (i32.const 0))))))
  1813. (test-vm "string.const"
  1814. "Hello, world!"
  1815. '(module
  1816. (func (export "main") (result (ref string))
  1817. (string.const "Hello, world!")))
  1818. #:v8-read get-line)
  1819. (test-vm "string.new_lossy_utf8_array"
  1820. "HELLO"
  1821. '(module
  1822. (type $utf8 (array (mut i8)))
  1823. (func (export "main") (result (ref string))
  1824. (string.new_lossy_utf8_array (array.new_fixed $utf8 5
  1825. (i32.const 72)
  1826. (i32.const 69)
  1827. (i32.const 76)
  1828. (i32.const 76)
  1829. (i32.const 79))
  1830. (i32.const 0)
  1831. (i32.const 5))))
  1832. #:v8-read get-line)
  1833. (test-vm "string.encode_wtf8_array"
  1834. 5
  1835. '(module
  1836. (type $utf8 (array (mut i8)))
  1837. (func (export "main") (result i32)
  1838. (string.encode_wtf8_array (string.const "HELLO")
  1839. (array.new $utf8
  1840. (i32.const 0)
  1841. (i32.const 5))
  1842. (i32.const 0)))))
  1843. (test-vm "string.measure_utf8"
  1844. 5
  1845. '(module
  1846. (func (export "main") (result i32)
  1847. (string.measure_utf8 (string.const "HELLO")))))
  1848. (test-vm "string.measure_wtf8"
  1849. 5
  1850. '(module
  1851. (func (export "main") (result i32)
  1852. (string.measure_wtf8 (string.const "HELLO")))))
  1853. (test-equal "inter-instance function calls"
  1854. 17
  1855. (let* ((wat-a '(module
  1856. (func (export "square") (param $x i32) (result i32)
  1857. (i32.mul (local.get $x) (local.get $x)))))
  1858. (wat-b '(module
  1859. (func $square (import "wasm" "square") (param i32) (result i32))
  1860. (func (export "main") (param $x i32) (result i32)
  1861. (i32.add (call $square (local.get $x)) (i32.const 1)))))
  1862. (wasm-a (wat->wasm* wat-a))
  1863. (wasm-b (wat->wasm* wat-b))
  1864. (inst-a (instantiate-wasm (validate-wasm wasm-a)))
  1865. (square (wasm-instance-export-ref inst-a "square"))
  1866. (inst-b (instantiate-wasm (validate-wasm wasm-b)
  1867. #:imports `(("wasm" .
  1868. (("square" . ,square)))))))
  1869. ((wasm-instance-export-ref inst-b "main") 4)))
  1870. (test-vm "try"
  1871. 111
  1872. '(module
  1873. (tag $foo (param i32))
  1874. (func (export "main") (result i32)
  1875. (try (result i32)
  1876. (do (i32.const 42)
  1877. (throw $foo))
  1878. (catch $foo
  1879. (i32.const 69)
  1880. (i32.add))
  1881. (catch_all
  1882. (unreachable))))))
  1883. (test-vm "try_delegate"
  1884. 111
  1885. '(module
  1886. (tag $foo (param i32))
  1887. (func (export "main") (result i32)
  1888. (try $bloop (result i32)
  1889. (do (try
  1890. (do (i32.const 69)
  1891. (throw $foo))
  1892. (delegate $bloop))
  1893. (unreachable))
  1894. (catch $foo
  1895. (i32.const 42)
  1896. (i32.add))
  1897. (catch_all (i32.const 77))))))
  1898. (test-vm "rethrow"
  1899. 42
  1900. '(module
  1901. (tag $foo)
  1902. (func (export "main") (result i32)
  1903. (try $l1 (result i32)
  1904. (do (try $l2 (result i32)
  1905. (do (throw $foo))
  1906. (catch $foo
  1907. (rethrow $l2))
  1908. (catch_all (unreachable))))
  1909. (catch $foo
  1910. (i32.const 42))
  1911. (catch_all (unreachable))))))
  1912. (test-equal "throw across instances"
  1913. 111
  1914. (let* ((wat-a '(module
  1915. (tag $err (export "err") (param i32))
  1916. (func (export "foo") (param $x i32)
  1917. (throw $err (local.get $x)))))
  1918. (wat-b '(module
  1919. (tag $err (import "other" "err") (param i32))
  1920. (func $foo (import "other" "foo") (param i32))
  1921. (func (export "main") (param $x i32) (result i32)
  1922. (try (result i32)
  1923. (do (call $foo (local.get $x))
  1924. (unreachable))
  1925. (catch $err
  1926. (i32.const 69)
  1927. (i32.add))
  1928. (catch_all (unreachable))))))
  1929. (wasm-a (wat->wasm* wat-a))
  1930. (wasm-b (wat->wasm* wat-b))
  1931. (inst-a (instantiate-wasm (validate-wasm wasm-a)))
  1932. (err (wasm-instance-export-ref inst-a "err"))
  1933. (foo (wasm-instance-export-ref inst-a "foo"))
  1934. (inst-b (instantiate-wasm (validate-wasm wasm-b)
  1935. #:imports `(("other" .
  1936. (("err" . ,err)
  1937. ("foo" . ,foo)))))))
  1938. ((wasm-instance-export-ref inst-b "main") 42)))
  1939. (test-end* "test-vm")