-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk_extension.cpp
More file actions
355 lines (295 loc) · 7.7 KB
/
sdk_extension.cpp
File metadata and controls
355 lines (295 loc) · 7.7 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <covscript/covscript.hpp>
#include <covscript/cni.hpp>
#include <covscript/dll.hpp>
#include <iostream>
#ifdef COVSCRIPT_PLATFORM_WIN32
#include <windows.h>
bool ctrlhandler(DWORD fdwctrltype)
{
switch (fdwctrltype) {
case CTRL_C_EVENT:
std::cout << "Keyboard Interrupt (Ctrl+C Received)" << std::endl;
cs::current_process->raise_sigint();
return true;
case CTRL_BREAK_EVENT: {
int code = 0;
cs::process_context::on_process_exit_default_handler(&code);
return true;
}
default:
return false;
}
}
void activate_sigint_handler()
{
::SetConsoleCtrlHandler((PHANDLER_ROUTINE)ctrlhandler, true);
}
#else
#include <signal.h>
#include <unistd.h>
void signal_handler(int sig)
{
std::cout << "Keyboard Interrupt (Ctrl+C Received)" << std::endl;
cs::current_process->raise_sigint();
}
void activate_sigint_handler()
{
struct sigaction sa_usr {};
sa_usr.sa_handler = &signal_handler;
sigemptyset(&sa_usr.sa_mask);
// sa_usr.sa_flags = SA_RESTART | SA_NODEFER;
sigaction(SIGINT, &sa_usr, NULL);
}
#endif
static bool signal_handler_installed = false;
class repl_instance final {
cs::context_t context;
bool exit_flag = false;
public:
cs::repl repl_impl;
repl_instance(const cs::array &args)
: context(cs::create_context(args)),
repl_impl(context)
{
if (!signal_handler_installed) {
activate_sigint_handler();
cs::current_process->on_process_exit.add_listener([](void *code) -> bool {
cs::current_process->exit_code = *static_cast<int *>(code);
throw cs::fatal_error("CS_EXIT");
});
cs::current_process->on_process_sigint.add_listener([](void *) -> bool {
throw cs::fatal_error("CS_SIGINT");
});
cs::current_process->on_process_sigint.add_listener([](void *) -> bool {
std::cin.clear();
return false;
});
signal_handler_installed = true;
}
}
~repl_instance() = default;
bool has_exited() const
{
return exit_flag;
}
cs::var readline()
{
try {
std::string line;
#ifdef COVSCRIPT_PLATFORM_WIN32
// Workaround: https://stackoverflow.com/a/26763490
while (true) {
cs::current_process->poll_event();
std::getline(std::cin, line);
if (std::cin) break;
}
#else
if (!std::cin) {
int code = 0;
cs::process_context::on_process_exit_default_handler(&code);
}
std::getline(std::cin, line);
cs::current_process->poll_event();
#endif
return std::move(line);
}
catch (const std::exception &e) {
if (std::strstr(e.what(), "CS_SIGINT") != nullptr)
activate_sigint_handler();
else if (std::strstr(e.what(), "CS_EXIT") != nullptr)
exit_flag = true;
else
throw cs::lang_error(e.what());
}
catch (...) {
throw cs::lang_error("Uncaught exception: Unknown exception");
}
return cs::null_pointer;
}
bool exec(const std::string &code)
{
try {
repl_impl.exec(code);
return true;
}
catch (const std::exception &e) {
if (std::strstr(e.what(), "CS_SIGINT") != nullptr)
activate_sigint_handler();
else if (std::strstr(e.what(), "CS_EXIT") != nullptr)
exit_flag = true;
else
throw cs::lang_error(e.what());
}
catch (...) {
throw cs::lang_error("Uncaught exception: Unknown exception");
}
return false;
}
};
using repl_instance_t = std::shared_ptr<repl_instance>;
CNI_ROOT_NAMESPACE {
using namespace cs;
CNI_NAMESPACE(typeids)
{
CNI_VALUE_CONST(type, type_id(typeid(cs::type_t)))
CNI_VALUE_CONST(range, type_id(typeid(cs::range_type)))
CNI_VALUE_CONST(istream, type_id(typeid(cs::istream)))
CNI_VALUE_CONST(ostream, type_id(typeid(cs::ostream)))
CNI_VALUE_CONST(context, type_id(typeid(cs::context_t)))
CNI_VALUE_CONST(callable, type_id(typeid(cs::callable)))
CNI_VALUE_CONST(memberfn, type_id(typeid(cs::object_method)))
CNI_VALUE_CONST(exception, type_id(typeid(cs::lang_error)))
CNI_VALUE_CONST(namespace, type_id(typeid(cs::namespace_t)))
CNI_VALUE_CONST(structure, type_id(typeid(cs::structure)))
type_id get_real(const var &val) {
return type_id(val.type());
}
CNI(get_real)
}
CNI_NAMESPACE(variable)
{
bool is_rvalue(const var &val) {
return val.is_rvalue();
}
CNI(is_rvalue)
bool is_protect(const var &val) {
return val.is_protect();
}
CNI(is_protect)
bool is_constant(const var &val) {
return val.is_constant();
}
CNI(is_constant)
bool is_single(const var &val) {
return val.is_single();
}
CNI(is_single)
}
CNI_NAMESPACE(function)
{
const var & get_callable(const cs::object_method &om) {
return om.callable;
}
CNI(get_callable)
string get_type(const cs::callable &func) {
switch (func.type()) {
/**
* A Regular function
*/
case callable::types::normal:
return "regular";
/**
* A Foldable(marker for optimizer) regular function
*/
case callable::types::request_fold:
return "constant";
/**
* A Member function
*/
case callable::types::member_fn:
return "member_function";
/**
* Automatically called member visitor
*/
case callable::types::member_visitor:
return "member_visitor";
/**
* A regular function cannot convert into object method
*/
case callable::types::force_regular:
return "force_regular";
}
return "null";
}
CNI(get_type)
}
var catch_stdexcept(const var &func, const cs::array &argument)
{
try {
if (func.type() == typeid(cs::callable)) {
cs::vector args(argument.begin(), argument.end());
return func.const_val<callable>().call(args);
}
else if (func.type() == typeid(cs::object_method)) {
const auto &om = func.const_val<cs::object_method>();
cs::vector args{om.object};
args.insert(args.end(), argument.begin(), argument.end());
return om.callable.const_val<callable>().call(args);
}
else
throw cs::lang_error("Invoke non-callable object.");
}
catch (const std::exception &e) {
throw lang_error(e.what());
}
}
CNI(catch_stdexcept)
template <typename T>
var predict_domain(T &&domain)
{
cs::var ret = cs::var::make<cs::hash_set>();
cs::hash_set &set = ret.val<cs::hash_set>();
for (auto &it : domain) set.insert(cs::var::make<cs::string>(it.first));
return ret;
}
var predict_symbols(const var &a)
{
if (a.type() == typeid(namespace_t))
return predict_domain(a.val<namespace_t>()->get_domain());
else if (a.type() == typeid(type_t))
return predict_domain(a.const_val<type_t>().extensions->get_domain());
else if (a.type() == typeid(structure))
return predict_domain(a.val<structure>().get_domain());
else
return predict_domain(a.get_ext()->get_domain());
}
CNI(predict_symbols)
var predict_current_symbols(const cs::context_t &cxt)
{
return predict_domain(cxt->instance->storage.get_domain());
}
CNI(predict_current_symbols)
var predict_global_symbols(const cs::context_t &cxt)
{
return predict_domain(cxt->instance->storage.get_global());
}
CNI(predict_global_symbols)
void extend_type(cs::type_t &type, const std::string &name, const var &obj)
{
type.extensions->add_var(name.data(), obj);
}
CNI(extend_type)
void set_import_path(const std::string &path)
{
cs::current_process->import_path = path;
}
CNI(set_import_path)
CNI_NAMESPACE(repl)
{
repl_instance_t create(const cs::array &args) {
return std::make_shared<repl_instance>(args);
}
CNI(create)
bool has_exited(const repl_instance_t & repl) {
return repl->has_exited();
}
CNI(has_exited)
cs::var readline(repl_instance_t & repl) {
return repl->readline();
}
CNI(readline)
bool exec(repl_instance_t & repl, const std::string &code) {
return repl->exec(code);
}
CNI(exec)
void reset(repl_instance_t & repl) {
repl->repl_impl.reset_status();
}
CNI(reset)
void echo(repl_instance_t & repl, bool value) {
repl->repl_impl.echo = value;
}
CNI(echo)
}
}
CNI_ENABLE_TYPE_EXT(repl, repl_instance_t)