-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_bindings.py
221 lines (175 loc) · 5.36 KB
/
generate_bindings.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from jinja2 import Template
import schema
def convert_ts_type(t):
if t == 'int':
return 'number'
if t == 'float':
return 'number'
if t == 'void*':
return 'any'
if t == 'bool':
return 'boolean'
return t
def cpp_to_json_converter(t):
if t == 'void*':
return 'MemoryHelpers::cppToJsonPointer'
# return '(unsigned long)'
return ''
def json_to_cpp_converter(t):
if t == 'void*':
return 'MemoryHelpers::jsonToCppPointer'
# return '(void*)(unsigned long)'
return ''
ts_template = Template("""
{% for (enum_name, enum_prefix, values) in enums %}
export enum {{ enum_name }} {
{% for value in values %}
{{ value }},
{% endfor %}
UNKNOWN
};
{% endfor %}
{% for (name, values) in messages %}
export interface {{ name }} {
type: '{{ name }}'
{% for (property_name, property_type) in values %}
{{ property_name }}: {{ convert_ts_type(property_type) }};
{% endfor %}
}
{% endfor %}
export type Message = { type: 'Unknown' } {% for (name, values) in messages %} | {{ name }} {% endfor %}
export function parseMessage(json: string): Message {
const val = JSON.parse(json)
switch (val.type) {
{% for (name, values) in messages %}
case '{{ name }}': return val as {{ name }}
{% endfor %}
}
return { type: 'Unknown' }
}
""")
h_template = Template("""
#ifndef BINDINGS_H
#define BINDINGS_H
#include "support.h"
#include "json.hpp"
using namespace nlohmann;
{% for (enum_name, enum_prefix, values) in enums %}
enum {{ enum_name }} {
{% for value in values %}
{{ value }},
{% endfor %}
UNKNOWN
};
{% endfor %}
{% for (name, values) in messages %}
struct {{ name }} {
{% for (property_name, property_type) in values %}
{{ property_type }} {{ property_name }};
{% endfor %}
string toJson() const;
static {{ name }} fromJson(const json& j);
};
{% endfor %}
class IMessageHandler {
public:
virtual ~IMessageHandler() {}
{% for (name, values) in messages %}
virtual void handleMessageFromWeb(const {{ name }}& message) {}
{% endfor %}
};
namespace MessageBindings {
{% for (name, values) in messages %}
void sendMessageToWeb(const {{ name }}& message);
{% endfor %}
};
struct MessageLogger : IMessageHandler {
public:
{% for (name, values) in messages %}
void handleMessageFromWeb(const {{ name }}& message) override {
cout << "TS => CPP w/ " << message.toJson() << "\\n";
}
{% endfor %}
};
#endif
""")
cpp_template = Template("""
#include "bindings.h"
#include "binding_helpers.h"
#ifdef __APPLE__
////////////////////////////////////////////////
// Native bindings
#include "osx_helpers.h"
{% for (name, values) in messages %}
void MessageBindings::sendMessageToWeb(const {{ name }}& message) {
auto json = message.toJson();
std::replace(json.begin(), json.end(), '"', '\\\'');
std::string evalString = "window.MessageHandler.handleMessageFromCPP(JSON.stringify(" + json + "));";
OSXWebView::getInstance()->eval(evalString);
}
{% endfor %}
#else
////////////////////////////////////////////////
// Emscripten bindings
#include <emscripten/val.h>
{% for (name, values) in messages %}
void MessageBindings::sendMessageToWeb(const {{ name }}& message) {
emscripten::val MessageHandler = emscripten::val::global("MessageHandler");
if (!MessageHandler.as<bool>()) {
cerr << "No global MessageHandler\\n";
return;
}
MessageHandler.call<void>("handleMessageFromCPP", emscripten::val(message.toJson()));
}
{% endfor %}
#endif
{% for (name, values) in messages %}
string {{ name }}::toJson() const {
json j;
j["type"] = "{{ name }}";
{% for (property_name, property_type) in values %}
j["{{ property_name }}"] = {{ cpp_to_json_converter(property_type) }}({{ property_name }});
{% endfor %}
return j.dump();
}
{{ name }} {{ name }}::fromJson(const json& j) {
return {{ name }} {
{% for (property_name, property_type) in values %}
{{ json_to_cpp_converter(property_type) }}(j["{{ property_name }}"]),
{% endfor %}
};
}
{% endfor %}
void MessagesFromWeb::sendMessage(const json& j) {
{% for (name, values) in messages %}
if (j["type"] == "{{ name }}") {
auto message = {{ name }}::fromJson(j);
for (const auto& handler : _handlers) {
handler->handleMessageFromWeb(message);
}
}
{% endfor %}
}
""")
def generate_h_file(messages, enums):
h_result = h_template.render(messages=messages, enums=enums, cpp_to_json_converter=cpp_to_json_converter, json_to_cpp_converter=json_to_cpp_converter)
h_result = '\n'.join([line for line in h_result.split('\n') if line.strip() != ''])
h_file = open('src/cpp/bindings.h', 'w')
h_file.truncate(0)
h_file.write(h_result)
h_file.close()
cpp_result = cpp_template.render(messages=messages, enums=enums, cpp_to_json_converter=cpp_to_json_converter, json_to_cpp_converter=json_to_cpp_converter)
cpp_result = '\n'.join([line for line in cpp_result.split('\n') if line.strip() != ''])
cpp_file = open('src/cpp/bindings.cpp', 'w')
cpp_file.truncate(0)
cpp_file.write(cpp_result)
cpp_file.close()
def generate_ts_file(messages, enums):
ts_result = ts_template.render(messages=messages, enums=enums, convert_ts_type=convert_ts_type)
ts_result = '\n'.join([line for line in ts_result.split('\n') if line.strip() != ''])
ts_file = open('src/ts/bindings.ts', 'w')
ts_file.truncate(0)
ts_file.write(ts_result)
ts_file.close()
generate_h_file(schema.messages, schema.enums)
generate_ts_file(schema.messages, schema.enums)