-
Notifications
You must be signed in to change notification settings - Fork 0
/
jni-def-macros.scm
152 lines (139 loc) · 7.69 KB
/
jni-def-macros.scm
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
;; macros for automatic generation of type variants procedures:
;; - define-type-procs: iterate over types an generate call-procs and jvalue-procs
;; - define-call-procs: generate call procedures by type, ie: call-void-method
;; - define-jvalue-procs: generate set jvalue procedures by type (ie: set-char-jvalue!)
;; - define-get-field-procs: (ie, for double:
;; get-double-field
;; set-double-field
;; double-field/accessor
;; get-static-double-field
;; set-static-double-field)
;; - define-jni-modifier-procs: generate test and accesor procedures for modifiers (ie: public? / public-modifier)
(define-syntax define-call-procs
(er-macro-transformer
(lambda (x r c)
(let* ((%begin (r 'begin))
(%export (r 'export))
(%define (r 'define))
(%env-lambda (r 'jni-env-lambda))
(type (cadr x))
(s-type (caddr x))
(proc-name (string->symbol (format "call-~A-method" (string-downcase (symbol->string type)))))
(static-proc-name (string->symbol (format "call-static-~A-method" (string-downcase (symbol->string type)))))
(jni-proc-name (string->symbol (format "Call~AMethodA" type)))
(static-jni-proc-name (string->symbol (format "CallStatic~AMethodA" type))))
`(,%begin
(,%export ,proc-name)
(,%export ,static-proc-name)
(,%define ,proc-name
(,%env-lambda ,s-type ,jni-proc-name jobject jmethod-id jvalue))
(,%define ,static-proc-name
(,%env-lambda ,s-type ,static-jni-proc-name jobject jmethod-id jvalue)))))))
(define-syntax define-jvalue-procs
(er-macro-transformer
(lambda (x r c)
(let* ((%begin (r 'begin))
(%export (r 'export))
(%define (r 'define))
(%foreign-lambda (r 'foreign-lambda))
(type (string->symbol (string-downcase (symbol->string (cadr x)))))
(s-type (caddr x))
(type-string (symbol->string type))
(set-proc-name (string->symbol (format "set-~A-jvalue!" type-string)))
(c-set-proc-name (string->symbol (format "set_~A_jvalue" type-string))))
`(,%begin
(,%define ,set-proc-name
(,%foreign-lambda jvalue ,c-set-proc-name jvalue int ,s-type)))))))
(define-syntax define-type-procs
(er-macro-transformer
(lambda (x r c)
(let ((%begin (r 'begin))
(%define-call-procs (r 'define-call-procs))
(%define-jvalue-procs (r 'define-jvalue-procs)))
(cons %begin
(map (lambda (type s-type)
`(,%begin
(,%define-call-procs ,type ,s-type)
(,%define-jvalue-procs ,type ,s-type)))
(cdr types)
(cdr s-types)))))))
(define-syntax define-get-field-procs
(er-macro-transformer
(lambda (x r c)
(let ((%begin (r 'begin))
(%export (r 'export))
(%define (r 'define))
(%apply (r 'apply))
(%lambda (r 'lambda))
(%car (r 'car))
(%jvoid (r 'void))
(%jni-env-lambda (r 'jni-env-lambda)))
(cons %begin
(map (lambda (return-type type type-sig)
(let ((proc-get-name (string->symbol (string-append "get-" (string-downcase type) "-field")))
(proc-set-name (string->symbol (string-append "set-" (string-downcase type) "-field")))
(accessor-name (string->symbol (string-append (string-downcase type) "-field/accessor")))
(static-proc-get-name (string->symbol (string-append "get-static-" (string-downcase type) "-field")))
(static-proc-set-name (string->symbol (string-append "set-static-" (string-downcase type) "-field")))
(jni-get-name (string->symbol (string-append "Get" type "Field")))
(jni-set-name (string->symbol (string-append "Set" type "Field")))
(static-jni-get-name (string->symbol (string-append "GetStatic" type "Field")))
(static-jni-set-name (string->symbol (string-append "SetStatic" type "Field"))))
`(,%begin
(,%export ,static-proc-get-name)
(,%define ,static-proc-get-name
(,%jni-env-lambda ,return-type
,static-jni-get-name
jobject
jfield-id))
(,%export ,static-proc-set-name)
(,%define ,static-proc-set-name
(,%jni-env-lambda ,%jvoid
,static-jni-set-name
jobject
jfield-id
,return-type))
(,%export ,proc-get-name)
(,%define ,proc-get-name
(,%jni-env-lambda ,return-type
,jni-get-name
jobject
jfield-id))
(,%export ,proc-set-name)
(,%define ,proc-set-name
(,%jni-env-lambda ,%jvoid
,jni-set-name
jobject
jfield-id
,return-type))
(,%export ,accessor-name)
(,%define (,accessor-name object field-name)
(let* ((object-class object)
(field-id (get-field object-class field-name ,type-sig)))
(,%lambda value
(if (null? value)
(,proc-get-name object field-id)
(,proc-set-name object field-id (,%car value))))))
)))
(cdr jni-jtypes)
(map symbol->string (cdr jni-types))
(map symbol->string jni-type-sigs)))))))
(define-syntax define-jni-modifier-procs
(er-macro-transformer
(lambda (x i c)
(cons 'begin
(let loop ((accessor-defs '())
(test-defs '())
(exports '())
(modifiers modifiers))
(if (null? modifiers)
(append accessor-defs test-defs exports)
(let* ((modifier (car modifiers))
(accessor-name (string->symbol (format "~A-modifier" (car modifier))))
(test-name (string->symbol (format "~A?" (car modifier)))))
(loop (append accessor-defs (list `(define ,accessor-name ,(cdr modifier))))
(append test-defs (list `(define (,test-name modifier)
(> (bitwise-and modifier ,(cdr modifier)) 0))))
(append exports (list `(export ,accessor-name)
`(export ,test-name)))
(cdr modifiers)))))))))