test-lower-stringrefs.scm 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. ;;; Copyright (C) 2023 Igalia, S.L.
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; String tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (wasm wat)
  21. (wasm lower-stringrefs)
  22. (test utils))
  23. (test-begin "test-lower-stringrefs")
  24. (let ((mod (wat->wasm
  25. '((func $string-to-i32 (import "app" "string-to-i32")
  26. (param (ref string)) (result i32))
  27. (func $i32-to-string (import "app" "i32-to-string")
  28. (param i32) (result (ref string)))
  29. (func $main (param $i i32) (result i32)
  30. (local.get $i)
  31. (call $i32-to-string)
  32. (call $string-to-i32))
  33. (global $s0 (ref string) (string.const "hey"))))))
  34. (test-equal '(module
  35. (type $wtf8 (array (mut i8)))
  36. (import "app" "string-to-i32"
  37. (func $string-to-i32-stringref-0
  38. (param (ref extern)) (result i32)))
  39. (import "app" "i32-to-string"
  40. (func $i32-to-string-stringref-1
  41. (param i32) (result (ref extern))))
  42. (import "rt" "wtf8_to_string"
  43. (func $wtf8->extern-string
  44. (param $wtf8 (ref null $wtf8)) (result (ref extern))))
  45. (import "rt" "string_to_wtf8"
  46. (func $extern-string->wtf8
  47. (param $str (ref null extern)) (result (ref $wtf8))))
  48. ;; This is an odd lowering: sure, it's correct, but why
  49. ;; two globals? But having a global which is itself a
  50. ;; string.const is pretty unusual, we think:
  51. ;; string.const is usually used inline. It's not
  52. ;; important to optimize this case; the common logic to
  53. ;; intern a unique global for each unique operand of
  54. ;; string.const is OK.
  55. (global $stringref-2 (ref $wtf8)
  56. (i32.const 0) (i32.const 3) (array.new_data $wtf8 $stringref-2))
  57. (global $s0 (ref $wtf8) (global.get $stringref-2))
  58. (data $stringref-2 #vu8(104 101 121))
  59. (func $main
  60. (param $i i32) (result i32)
  61. (local.get $i)
  62. (call $i32-to-string)
  63. (call $string-to-i32))
  64. (func $string-to-i32
  65. (param (ref $wtf8)) (result i32)
  66. (local #f i32)
  67. (local.get 0)
  68. (call $wtf8->extern-string)
  69. (call $string-to-i32-stringref-0)
  70. (local.set 1)
  71. (local.get 1))
  72. (func $i32-to-string
  73. (param i32)
  74. (result (ref $wtf8))
  75. (local #f (ref extern))
  76. (local.get 0)
  77. (call $i32-to-string-stringref-1)
  78. (local.set 1)
  79. (local.get 1)
  80. (call $extern-string->wtf8)))
  81. (wasm->wat (lower-stringrefs mod #:strategy 'wtf8))))
  82. (test-end* "test-lower-stringrefs")