;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.

;; Lower first to generate the string.consts custom section, then lift it back.

;; RUN: wasm-opt %s -all --string-lowering --string-lifting -S -o - | filecheck %s

(module
  ;; CHECK:      (type $0 (array (mut i16)))

  ;; CHECK:      (type $1 (func))

  ;; CHECK:      (type $2 (func (param externref externref) (result i32)))

  ;; CHECK:      (type $3 (func (param externref) (result i32)))

  ;; CHECK:      (type $4 (func (param (ref null $0) i32 i32) (result (ref extern))))

  ;; CHECK:      (type $5 (func (param i32) (result (ref extern))))

  ;; CHECK:      (type $6 (func (param externref externref) (result (ref extern))))

  ;; CHECK:      (type $7 (func (param externref (ref null $0) i32) (result i32)))

  ;; CHECK:      (type $8 (func (param externref i32) (result i32)))

  ;; CHECK:      (type $9 (func (param externref i32 i32) (result (ref extern))))

  ;; CHECK:      (import "string.const" "0" (global $"string.const_\"bar\"" (ref extern)))

  ;; CHECK:      (import "string.const" "1" (global $"string.const_\"foo\"" (ref extern)))

  ;; CHECK:      (import "string.const" "2" (global $"string.const_\"needs\\tescaping\\00.\\\'#%\\\"- .\\r\\n\\\\08\\0c\\n\\r\\t.\\ea\\99\\ae\"" (ref extern)))

  ;; CHECK:      (import "string.const" "3" (global $"string.const_\"surrogate pair \\f0\\90\\8d\\88 \"" (ref extern)))

  ;; CHECK:      (import "string.const" "4" (global $"string.const_\"unpaired high surrogate \\ed\\a0\\80 \"" (ref extern)))

  ;; CHECK:      (import "string.const" "5" (global $"string.const_\"unpaired low surrogate \\ed\\bd\\88 \"" (ref extern)))

  ;; CHECK:      (import "string.const" "6" (global $"string.const_\"z\\\\\"" (ref extern)))

  ;; CHECK:      (import "wasm:js-string" "fromCharCodeArray" (func $fromCharCodeArray (type $4) (param (ref null $0) i32 i32) (result (ref extern))))

  ;; CHECK:      (import "wasm:js-string" "fromCodePoint" (func $fromCodePoint (type $5) (param i32) (result (ref extern))))

  ;; CHECK:      (import "wasm:js-string" "concat" (func $concat (type $6) (param externref externref) (result (ref extern))))

  ;; CHECK:      (import "wasm:js-string" "intoCharCodeArray" (func $intoCharCodeArray (type $7) (param externref (ref null $0) i32) (result i32)))

  ;; CHECK:      (import "wasm:js-string" "equals" (func $equals (type $2) (param externref externref) (result i32)))

  ;; CHECK:      (import "wasm:js-string" "test" (func $test (type $3) (param externref) (result i32)))

  ;; CHECK:      (import "wasm:js-string" "compare" (func $compare (type $2) (param externref externref) (result i32)))

  ;; CHECK:      (import "wasm:js-string" "length" (func $length (type $3) (param externref) (result i32)))

  ;; CHECK:      (import "wasm:js-string" "charCodeAt" (func $charCodeAt (type $8) (param externref i32) (result i32)))

  ;; CHECK:      (import "wasm:js-string" "substring" (func $substring (type $9) (param externref i32 i32) (result (ref extern))))

  ;; CHECK:      (func $consts (type $1)
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (string.const "foo")
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (string.const "bar")
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (string.const "foo")
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $consts
    ;; These strings get lowered into the custom section, then lifted back up,
    ;; so they do not change. We will see above the imported globals that were
    ;; created for them (and not cleaned up), two imports from "string.const".
    (drop
      (string.const "foo")
    )
    (drop
      (string.const "bar")
    )
    (drop
      (string.const "foo")
    )
  )

  ;; CHECK:      (func $tricky-consts (type $1)
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (string.const "needs\tescaping\00.\'#%\"- .\r\n\\08\0c\n\r\t.\ea\99\ae")
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (string.const "surrogate pair \f0\90\8d\88 ")
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (string.const "unpaired high surrogate \ed\a0\80 ")
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (string.const "unpaired low surrogate \ed\bd\88 ")
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT:  (drop
  ;; CHECK-NEXT:   (string.const "z\\")
  ;; CHECK-NEXT:  )
  ;; CHECK-NEXT: )
  (func $tricky-consts
    ;; These tricky strings should remain exactly the same after lowering and
    ;; lifting.
    (drop
      (string.const "needs\tescaping\00.'#%\"- .\r\n\\08\0C\0A\0D\09.ꙮ")
    )
    (drop
      (string.const "surrogate pair \F0\90\8D\88 ")
    )
    (drop
      (string.const "unpaired high surrogate \ED\A0\80 ")
    )
    (drop
      (string.const "unpaired low surrogate \ED\BD\88 ")
    )
    ;; A string with \", but the " is not escaped, as the \ is part of \\.
    (drop
      (string.const "z\\")
    )
  )
)

