test-lower-stringrefs.scm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. (func $string-to-i32-stringref-0 (import "app" "string-to-i32")
  37. (param (ref extern)) (result i32))
  38. (func $i32-to-string-stringref-1 (import "app" "i32-to-string")
  39. (param i32) (result (ref extern)))
  40. (func $wtf8->extern-string (import "rt" "wtf8_to_string")
  41. (param $wtf8 (ref null $wtf8)) (result (ref extern)))
  42. (func $extern-string->wtf8 (import "rt" "string_to_wtf8")
  43. (param $str (ref null extern)) (result (ref $wtf8)))
  44. ;; This is an odd lowering: sure, it's correct, but why
  45. ;; two globals? But having a global which is itself a
  46. ;; string.const is pretty unusual, we think:
  47. ;; string.const is usually used inline. It's not
  48. ;; important to optimize this case; the common logic to
  49. ;; intern a unique global for each unique operand of
  50. ;; string.const is OK.
  51. (global $stringref-2 (ref $wtf8)
  52. (i32.const 0) (i32.const 3) (array.new_data $wtf8 $stringref-2))
  53. (global $s0 (ref $wtf8) (global.get $stringref-2))
  54. (data $stringref-2 #vu8(104 101 121))
  55. (func $main
  56. (param $i i32) (result i32)
  57. (local.get $i)
  58. (call $i32-to-string)
  59. (call $string-to-i32))
  60. (func $string-to-i32
  61. (param (ref $wtf8)) (result i32)
  62. (local #f i32)
  63. (local.get 0)
  64. (call $wtf8->extern-string)
  65. (call $string-to-i32-stringref-0)
  66. (local.set 1)
  67. (local.get 1))
  68. (func $i32-to-string
  69. (param i32)
  70. (result (ref $wtf8))
  71. (local #f (ref extern))
  72. (local.get 0)
  73. (call $i32-to-string-stringref-1)
  74. (local.set 1)
  75. (local.get 1)
  76. (call $extern-string->wtf8)))
  77. (wasm->wat (lower-stringrefs mod #:strategy 'wtf8))))
  78. (test-end* "test-lower-stringrefs")