call.scm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ;;; Test script to compile Scheme expressions to wasm, then apply 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-call form)
  50. (let lp ((form form) (files '()) (first? #t))
  51. (match form
  52. (()
  53. (apply run-d8 (scope-file "test/test-call.js") "--" srcdir (reverse files)))
  54. ((x . form)
  55. (call-with-compiled-wasm-file
  56. (compile x #:import-abi? (not first?) #:export-abi? first?)
  57. (lambda (file)
  58. (lp form (cons file files) #f)))))))
  59. (define (read1 str)
  60. (call-with-input-string
  61. str
  62. (lambda (port)
  63. (let ((expr (read port)))
  64. (when (eof-object? expr)
  65. (error "No expression to evaluate"))
  66. (let ((tail (read port)))
  67. (unless (eof-object? tail)
  68. (error "Unexpected trailing expression" tail)))
  69. expr))))
  70. (when (batch-mode?)
  71. (match (program-arguments)
  72. ((arg0 f . args)
  73. (compile-call (cons (read1 f) (map read1 args))))
  74. ((arg0 . _)
  75. (format (current-error-port) "usage: ~a FUNC ARG...\n" arg0)
  76. (exit 1))))