call.scm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ;;; Test script to compile Scheme expressions to wasm, then apply via V8
  2. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  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. (use-modules (wasm assemble)
  16. (hoot compile)
  17. (ice-9 binary-ports)
  18. (ice-9 match)
  19. (ice-9 popen)
  20. (ice-9 textual-ports)
  21. (srfi srfi-64))
  22. (define d8 (or (getenv "D8") "d8"))
  23. (define srcdir (or (getenv "SRCDIR") (getcwd)))
  24. (define builddir (or (getenv "BUILDDIR") (getcwd)))
  25. (define (scope-file file-name)
  26. (in-vicinity srcdir file-name))
  27. (define (unwind-protect body unwind)
  28. (call-with-values
  29. (lambda ()
  30. (with-exception-handler
  31. (lambda (exn)
  32. (unwind)
  33. (raise-exception exn))
  34. body))
  35. (lambda vals
  36. (unwind)
  37. (apply values vals))))
  38. (define (call-with-compiled-wasm-file wasm f)
  39. (let* ((wasm-port (mkstemp "/tmp/tmp-wasm-XXXXXX"))
  40. (wasm-file-name (port-filename wasm-port)))
  41. (put-bytevector wasm-port (assemble-wasm wasm))
  42. (close-port wasm-port)
  43. (unwind-protect
  44. (lambda () (f wasm-file-name))
  45. (lambda () (delete-file wasm-file-name)))))
  46. (define (run-d8 . args)
  47. (let* ((args (cons* d8 "--experimental-wasm-stringref" args))
  48. (pid (spawn d8 args)))
  49. (exit (status:exit-val (cdr (waitpid pid))))))
  50. (define (compile-call form)
  51. (let lp ((form form) (files '()) (first? #t))
  52. (match form
  53. (()
  54. (apply run-d8 (scope-file "test/test-call.js") "--" srcdir builddir (reverse files)))
  55. ((x . form)
  56. (call-with-compiled-wasm-file
  57. (compile x #:import-abi? (not first?) #:export-abi? first?)
  58. (lambda (file)
  59. (lp form (cons file files) #f)))))))
  60. (define (read1 str)
  61. (call-with-input-string
  62. str
  63. (lambda (port)
  64. (let ((expr (read port)))
  65. (when (eof-object? expr)
  66. (error "No expression to evaluate"))
  67. (let ((tail (read port)))
  68. (unless (eof-object? tail)
  69. (error "Unexpected trailing expression" tail)))
  70. expr))))
  71. (when (batch-mode?)
  72. (match (program-arguments)
  73. ((arg0 f . args)
  74. (compile-call (cons (read1 f) (map read1 args))))
  75. ((arg0 . _)
  76. (format (current-error-port) "usage: ~a FUNC ARG...\n" arg0)
  77. (exit 1))))