-
Notifications
You must be signed in to change notification settings - Fork 2
/
wasm-extern.lisp
79 lines (62 loc) · 2.7 KB
/
wasm-extern.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(in-package #:cl-wasm-runtime)
(define-wasm-ref extern)
(define-wasm-vec extern)
(cffi:defcfun "wasm_extern_kind" %wasm-extern-kind-type
(extern %wasm-extern-type))
(cffi:defcfun "wasm_extern_type" %wasm-externtype-type ; own
(extern %wasm-extern-type))
(defmacro define-wasm-extern-conversion (type)
(let ((type-name (make-object-type-parser-sym type))
(as-extern-name (format-wasm-cfun-name type "as_extern"))
(as-type-name (list (format nil "wasm_extern_as_~a" (string-downcase type))
(alexandria:symbolicate '%wasm-extern-as- type))))
`(progn
(cffi:defcfun ,as-extern-name %wasm-extern-type
(,type ,type-name))
(cffi:defcfun ,as-type-name ,type-name
(extern %wasm-extern-type)))))
(define-wasm-extern-conversion func)
(define-wasm-extern-conversion global)
(define-wasm-extern-conversion table)
(define-wasm-extern-conversion memory)
(define-wasm-object-class extern ()
((kind :reader wasm-extern-kind)))
(defun wrap-wasm-extern (pointer &key owner)
(let ((extern (enable-gc (make-instance 'wasm-extern :pointer pointer :owner owner))))
(setf (slot-value extern 'kind)
(wasm-externkind-to-key (%wasm-extern-kind pointer)))
extern))
(defmethod extern-type ((extern wasm-extern))
(%wasm-extern-type extern))
(defmacro define-to-wasm-extern-method (type)
(let* ((class-name (alexandria:symbolicate 'wasm- type))
(as-type-name (translate-wasm-name (format-wasm-cfun-name type "as_extern"))))
`(defmethod to-wasm-extern ((,type ,class-name))
(let ((pointer (,as-type-name ,type)))
(unless (null? pointer)
(wrap-wasm-extern pointer :owner (owner ,type)))))))
(defgeneric to-wasm-extern (object))
(define-to-wasm-extern-method func)
(define-to-wasm-extern-method global)
(define-to-wasm-extern-method table)
(define-to-wasm-extern-method memory)
(defmethod to-wasm-extern ((object wasm-extern))
object)
(defmacro define-from-wasm-extern-method (type)
(let* ((class-name (alexandria:symbolicate 'wasm- type))
(as-extern-name (alexandria:symbolicate '%wasm-extern-as- type))
(wrapper-name (alexandria:symbolicate 'wrap- class-name)))
`(defmethod from-wasm-extern ((extern wasm-extern) (wasm-type (eql ',class-name)))
(let ((pointer (,as-extern-name extern)))
(unless (null? pointer)
(,wrapper-name pointer :owner (owner extern)))))))
(defgeneric from-wasm-extern (extern type))
(define-from-wasm-extern-method func)
(define-from-wasm-extern-method global)
(define-from-wasm-extern-method table)
(define-from-wasm-extern-method memory)
(defmethod from-wasm-extern ((extern wasm-extern) (type (eql 'wasm-extern)))
extern)
(define-wasm-vec-class extern ()
((wrap-data-function :allocation :class
:initform #'wrap-wasm-extern)))