eval.scm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;;; Test script to compile a Scheme expression to wasm, then run via V8
  2. ;;; Copyright (C) 2023 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 (scope-file file-name)
  25. (in-vicinity srcdir file-name))
  26. (define (unwind-protect body unwind)
  27. (call-with-values
  28. (lambda ()
  29. (with-exception-handler
  30. (lambda (exn)
  31. (unwind)
  32. (raise-exception exn))
  33. body))
  34. (lambda vals
  35. (unwind)
  36. (apply values vals))))
  37. (define (call-with-compiled-wasm-file wasm f)
  38. (let* ((wasm-port (mkstemp "/tmp/tmp-wasm-XXXXXX"))
  39. (wasm-file-name (port-filename wasm-port)))
  40. (put-bytevector wasm-port (assemble-wasm wasm))
  41. (close-port wasm-port)
  42. (unwind-protect
  43. (lambda () (f wasm-file-name))
  44. (lambda () (delete-file wasm-file-name)))))
  45. (define (run-d8 . args)
  46. (let* ((args (cons* d8 "--experimental-wasm-stringref" args))
  47. (pid (spawn d8 args)))
  48. (exit (status:exit-val (cdr (waitpid pid))))))
  49. (define (compile-expr expr)
  50. (call-with-compiled-wasm-file
  51. (compile expr)
  52. (lambda (wasm-file-name)
  53. (run-d8 (scope-file "test/load-wasm-and-print.js") "--" srcdir wasm-file-name))))
  54. (define (read1 str)
  55. (call-with-input-string
  56. str
  57. (lambda (port)
  58. (let ((expr (read port)))
  59. (when (eof-object? expr)
  60. (error "No expression to evaluate"))
  61. (let ((tail (read port)))
  62. (unless (eof-object? tail)
  63. (error "Unexpected trailing expression" tail)))
  64. expr))))
  65. (when (batch-mode?)
  66. (match (program-arguments)
  67. ((arg0 str)
  68. (compile-expr (read1 str)))
  69. ((arg0 . _)
  70. (format (current-error-port) "usage: ~a EXPR\n" arg0)
  71. (exit 1))))