-
Notifications
You must be signed in to change notification settings - Fork 2
/
wasm-global.lisp
48 lines (35 loc) · 1.42 KB
/
wasm-global.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
(in-package #:cl-wasm-runtime)
(define-wasm-ref global)
(cffi:defcfun "wasm_global_new" %wasm-global-type ; own
(store %wasm-store-type)
(globaltype %wasm-globaltype-type)
(val %wasm-val-type))
(cffi:defcfun "wasm_global_type" %wasm-globaltype-type ; own
(global %wasm-global-type))
(cffi:defcfun "wasm_global_get" :void
(global %wasm-global-type)
(out %wasm-val-type))
(cffi:defcfun "wasm_global_set" :void
(global %wasm-global-type)
(value %wasm-val-type))
(define-wasm-object-class global)
(defun make-wasm-global (store globaltype val)
(enable-gc (make-instance 'wasm-global
:pointer (%wasm-global-new store globaltype val))))
(defun wrap-wasm-global (pointer &key owner)
(enable-gc (make-instance 'wasm-global
:pointer pointer
:owner owner)))
(defun global-type (global)
(wrap-wasm-globaltype (%wasm-global-type global) :owner (owner global)))
(defmethod value ((global wasm-global))
(cffi:with-foreign-object (val-pointer '(:struct %wasm-val-struct))
(%wasm-global-get global val-pointer)
(wasm-val-value val-pointer)))
(defmethod (setf value) :before (val (global wasm-global))
(unless (mutable? (global-type global))
(error "Global value is not mutable, cannot set new value")))
(defmethod (setf value) ((val wasm-val) (global wasm-global))
(%wasm-global-set global val))
(defmethod (setf value) (val (global wasm-global))
(%wasm-global-set global (lisp-to-wasm-val val)))