test-ffi.scm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ;;; Copyright (C) 2023 David Thompson <dave@spritely.institute>
  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. ;;; FFI tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (ice-9 binary-ports)
  20. (ice-9 exceptions)
  21. (ice-9 popen)
  22. (ice-9 textual-ports)
  23. (srfi srfi-64)
  24. (test utils)
  25. (hoot reflect)
  26. (wasm parse))
  27. (test-begin "test-ffi")
  28. (define-syntax-rule (test-ffi name expected source imports)
  29. (test-equal name
  30. expected
  31. (compile-value 'source imports)))
  32. (test-ffi
  33. "i32 param and result"
  34. 16
  35. (let ()
  36. (define-foreign fsquare
  37. "math" "square"
  38. i32 -> i32)
  39. (square 4))
  40. `(("math" . (("square" . ,(lambda (x) (* x x)))))))
  41. (test-ffi
  42. "i64 param and result"
  43. 16
  44. (let ()
  45. (define-foreign fsquare
  46. "math" "square"
  47. i64 -> i64)
  48. (square 4))
  49. `(("math" . (("square" . ,(lambda (x) (* x x)))))))
  50. (test-ffi
  51. "f32 param and result"
  52. 16.0
  53. (let ()
  54. (define-foreign fsquare
  55. "math" "fsquare"
  56. f32 -> f32)
  57. (fsquare 4.0))
  58. `(("math" . (("fsquare" . ,(lambda (x) (* x x)))))))
  59. (test-ffi
  60. "f64 param and result"
  61. 16.0
  62. (let ()
  63. (define-foreign fsquare
  64. "math" "fsquare"
  65. f64 -> f64)
  66. (fsquare 4.0))
  67. `(("math" . (("fsquare" . ,(lambda (x) (* x x)))))))
  68. (test-ffi
  69. "string param and result"
  70. "Hello, owl!"
  71. (let ()
  72. (define-foreign hello
  73. "host" "hello"
  74. (ref string) -> (ref string))
  75. (hello "owl"))
  76. `(("host" .
  77. (("hello" . ,(lambda (name) (string-append "Hello, " name "!")))))))
  78. (test-ffi
  79. "external?"
  80. #t
  81. (let ()
  82. (define-foreign get-extern
  83. "host" "getExtern"
  84. -> (ref null extern))
  85. (external? (get-extern)))
  86. `(("host" . (("getExtern" . ,(lambda () '(external value)))))))
  87. (test-ffi
  88. "external-null?"
  89. #t
  90. (let ()
  91. (define-foreign get-null
  92. "host" "getNull"
  93. -> (ref null extern))
  94. (external-null? (get-null)))
  95. `(("host" . (("getNull" . ,(lambda () #f))))))
  96. (test-ffi
  97. "procedure->extern"
  98. 1
  99. (let ((counter 0))
  100. (define-foreign callback
  101. "host" "callback"
  102. (ref null extern) -> none)
  103. (callback
  104. (procedure->external
  105. (lambda () (set! counter (+ counter 1)))))
  106. counter)
  107. `(("host" .
  108. (("callback" . ,(lambda (f) (f) *unspecified*))))))
  109. (test-end* "test-ffi")