-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathclass-gen.py
104 lines (86 loc) · 3.7 KB
/
class-gen.py
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
#!/usr/bin/env python3
MAX_ARGS = 15
def types(i):
return ", ".join(["A{0}".format(x) for x in range(i)])
def params(i):
return ", ".join(["A{0} arg{0}".format(x) for x in range(i)])
def valtypes(i):
return ", ".join(["WasmValType<A{0}> a{0}".format(x) for x in range(i)])
def type_calls(i):
return ", ".join(["a{0}.type()".format(x) for x in range(i)])
def from_wasms(i):
return ", ".join(["a{0}.fromWasmVal(params[{0}])".format(x) for x in range(i)])
def to_wasms(i):
return ", ".join(["a{0}.toWasmVal(arg{0})".format(x) for x in range(i)])
print("""// Automatically generated. DO NOT EDIT.
package io.github.kawamuray.wasmtime;
import java.util.Arrays;
@SuppressWarnings("ALL")
public final class WasmFunctions {
private static final Val.Type[] EMPTY_TYPES = new Val.Type[0];
private WasmFunctions() {}
""");
# Function
for i in range(MAX_ARGS + 1):
print(""" public interface Function{i}<{types}R0> {{
R0 call({params});
}}
""".format(i=i, types=types(i)+', ' if i > 0 else '', params=params(i)))
# Consumer
for i in range(MAX_ARGS + 1):
print(""" public interface Consumer{i}{types} {{
void accept({params});
}}
""".format(i=i, types='<'+types(i)+'>' if i > 0 else '', params=params(i)))
# wrap for functions
for i in range(MAX_ARGS + 1):
print(""" public static <D, {typedecls}R0> Func wrap(Store<D> store, {valtypes}WasmValType<R0> r0, Function{i}<{types}R0> func) {{
return new Func(store, new FuncType(new Val.Type[] {{ {type_calls} }}, new Val.Type[] {{ r0.type() }}),
(caller, params, results) -> {{
R0 ret = func.call({from_wasms});
results[0] = r0.toWasmVal(ret);
}});
}}
""".format(i=i,
typedecls=types(i)+', ' if i > 0 else '',
types=types(i)+', ' if i > 0 else '',
valtypes=valtypes(i)+', ' if i > 0 else '',
type_calls=type_calls(i),
from_wasms=from_wasms(i)))
# wrap for consumers
for i in range(MAX_ARGS + 1):
print(""" public static <D{typedecls}> Func wrap(Store<D> store, {valtypes}Consumer{i}{types} func) {{
return new Func(store, new FuncType(new Val.Type[] {{ {type_calls} }}, EMPTY_TYPES),
(caller, params, results) -> {{
func.accept({from_wasms});
}});
}}
""".format(i=i,
typedecls=','+types(i) if i > 0 else '',
types='<'+types(i)+'>' if i > 0 else '',
valtypes=valtypes(i)+', ' if i > 0 else '',
type_calls=type_calls(i),
from_wasms=from_wasms(i)))
# wrap for functions
for i in range(MAX_ARGS + 1):
print(""" public static <D, {typedecls}R0> Function{i}<{types}R0> func(Store<D> store, Func fn, {valtypes}WasmValType<R0> r0) {{
return ({args}) -> r0.fromWasmVal(fn.call(store, Arrays.asList({to_wasms})).get(0));
}}
""".format(i=i,
typedecls=types(i)+', ' if i > 0 else '',
types=types(i)+', ' if i > 0 else '',
valtypes=valtypes(i)+', ' if i > 0 else '',
args=', '.join(["arg{0}".format(x) for x in range(i)]),
to_wasms=to_wasms(i)))
# wrap for consumers
for i in range(MAX_ARGS + 1):
print(""" public static <D{typedecls}> Consumer{i}{types} consumer(Store<D> store, Func fn{valtypes}) {{
return ({args}) -> fn.call(store, Arrays.asList({to_wasms}));
}}
""".format(i=i,
typedecls=', '+types(i) if i > 0 else '',
types='<'+types(i)+'>' if i > 0 else '',
valtypes=', '+valtypes(i) if i > 0 else '',
args=', '.join(["arg{0}".format(x) for x in range(i)]),
to_wasms=to_wasms(i)))
print("}");