-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathxinterpreter.cpp
414 lines (361 loc) · 13.5 KB
/
xinterpreter.cpp
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/************************************************************************************
* Copyright (c) 2023, xeus-cpp contributors *
* Copyright (c) 2023, Martin Vassilev *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/
#include <algorithm>
#include <cinttypes> // required before including llvm/ExecutionEngine/Orc/LLJIT.h because missing llvm/Object/SymbolicFile.h
#include <cstdarg>
#include <cstdio>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include "xeus/xsystem.hpp"
#include <xeus/xhelper.hpp>
#include "xeus-cpp/xbuffer.hpp"
#include "xeus-cpp/xeus_cpp_config.hpp"
#include "xeus-cpp/xinterpreter.hpp"
#include "xeus-cpp/xmagics.hpp"
#include "xinput.hpp"
#include "xinspect.hpp"
#include "xmagics/os.hpp"
#ifndef EMSCRIPTEN
#include "xmagics/xassist.hpp"
#endif
#include "xparser.hpp"
#include "xsystem.hpp"
using Args = std::vector<const char*>;
void* createInterpreter(const Args &ExtraArgs = {}) {
Args ClangArgs = {/*"-xc++"*/"-v"}; // ? {"-Xclang", "-emit-llvm-only", "-Xclang", "-diagnostic-log-file", "-Xclang", "-", "-xc++"};
if (std::find_if(ExtraArgs.begin(), ExtraArgs.end(), [](const std::string& s) {
return s == "-resource-dir";}) == ExtraArgs.end()) {
std::string resource_dir = Cpp::DetectResourceDir();
if (resource_dir.empty())
std::cerr << "Failed to detect the resource-dir\n";
ClangArgs.push_back("-resource-dir");
ClangArgs.push_back(resource_dir.c_str());
}
std::vector<std::string> CxxSystemIncludes;
Cpp::DetectSystemCompilerIncludePaths(CxxSystemIncludes);
for (const std::string& CxxInclude : CxxSystemIncludes) {
ClangArgs.push_back("-isystem");
ClangArgs.push_back(CxxInclude.c_str());
}
ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
// FIXME: We should process the kernel input options and conditionally pass
// the gpu args here.
return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/);
}
using namespace std::placeholders;
namespace xcpp
{
struct StreamRedirectRAII {
std::string &err;
StreamRedirectRAII(std::string &e) : err(e) {
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
}
~StreamRedirectRAII() {
std::string out = Cpp::EndStdStreamCapture();
err = Cpp::EndStdStreamCapture();
std::cout << out;
}
};
void interpreter::configure_impl()
{
xeus::register_interpreter(this);
}
static std::string get_stdopt()
{
// We need to find what's the C++ version the interpreter runs with.
const char* code = R"(
int __get_cxx_version () {
#if __cplusplus > 202302L
return 26;
#elif __cplusplus > 202002L
return 23;
#elif __cplusplus > 201703L
return 20;
#elif __cplusplus > 201402L
return 17;
#elif __cplusplus > 201103L || (defined(_WIN32) && _MSC_VER >= 1900)
return 14;
#elif __cplusplus >= 201103L
return 11;
#else
return 0;
#endif
}
__get_cxx_version ()
)";
auto cxx_version = Cpp::Evaluate(code);
return std::to_string(cxx_version);
}
interpreter::interpreter(int argc, const char* const* argv) :
xmagics()
, p_cout_strbuf(nullptr)
, p_cerr_strbuf(nullptr)
, m_cout_buffer(std::bind(&interpreter::publish_stdout, this, _1))
, m_cerr_buffer(std::bind(&interpreter::publish_stderr, this, _1))
{
//NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic)
createInterpreter(Args(argv ? argv + 1 : argv, argv + argc));
m_version = get_stdopt();
redirect_output();
init_includes();
init_preamble();
init_magic();
}
interpreter::~interpreter()
{
restore_output();
}
void interpreter::execute_request_impl(
send_reply_callback cb,
int /*execution_count*/,
const std::string& code,
xeus::execute_request_config config,
nl::json /*user_expressions*/
)
{
nl::json kernel_res;
auto input_guard = input_redirection(config.allow_stdin);
// Check for magics
for (auto& pre : preamble_manager.preamble)
{
if (pre.second.is_match(code))
{
pre.second.apply(code, kernel_res);
cb(kernel_res);
return;
}
}
auto errorlevel = 0;
std::string ename;
std::string evalue;
bool compilation_result = false;
// If silent is set to true, temporarily dismiss all std::cerr and
// std::cout outputs resulting from `process_code`.
auto cout_strbuf = std::cout.rdbuf();
auto cerr_strbuf = std::cerr.rdbuf();
if (config.silent)
{
auto null = xnull();
std::cout.rdbuf(&null);
std::cerr.rdbuf(&null);
}
std::string err;
// Attempt normal evaluation
try
{
StreamRedirectRAII R(err);
compilation_result = Cpp::Process(code.c_str());
}
catch (std::exception& e)
{
errorlevel = 1;
ename = "Standard Exception: ";
evalue = e.what();
}
catch (...)
{
errorlevel = 1;
ename = "Error: ";
}
if (compilation_result)
{
errorlevel = 1;
ename = "Error: ";
evalue = "Compilation error! " + err;
std::cerr << err;
}
// Flush streams
std::cout << std::flush;
std::cerr << std::flush;
// Reset non-silent output buffers
if (config.silent)
{
std::cout.rdbuf(cout_strbuf);
std::cerr.rdbuf(cerr_strbuf);
}
// Depending of error level, publish execution result or execution
// error, and compose execute_reply message.
if (errorlevel)
{
// Classic Notebook does not make use of the "evalue" or "ename"
// fields, and only displays the traceback.
//
// JupyterLab displays the "{ename}: {evalue}" if the traceback is
// empty.
if (evalue.size() < 4) {
ename = " ";
}
std::vector<std::string> traceback({ename + evalue});
if (!config.silent)
{
publish_execution_error(ename, evalue, traceback);
}
// Compose execute_reply message.
kernel_res["status"] = "error";
kernel_res["ename"] = ename;
kernel_res["evalue"] = evalue;
kernel_res["traceback"] = traceback;
}
else
{
/*
// Publish a mime bundle for the last return value if
// the semicolon was omitted.
if (!config.silent && output.hasValue() && trim(code).back() != ';')
{
nl::json pub_data = mime_repr(output);
publish_execution_result(execution_counter, std::move(pub_data), nl::json::object());
}
*/
// Compose execute_reply message.
kernel_res["status"] = "ok";
kernel_res["payload"] = nl::json::array();
kernel_res["user_expressions"] = nl::json::object();
}
cb(kernel_res);
}
nl::json interpreter::complete_request_impl(const std::string& code, int cursor_pos)
{
std::vector<std::string> results;
// split the input to have only the word in the back of the cursor
std::string delims = " \t\n`!@#$^&*()=+[{]}\\|;:\'\",<>?.";
std::size_t _cursor_pos = cursor_pos;
auto text = split_line(code, delims, _cursor_pos);
std::string to_complete = text.back().c_str();
Cpp::CodeComplete(results, code.c_str(), 1, _cursor_pos + 1);
return xeus::create_complete_reply(results /*matches*/,
cursor_pos - to_complete.length() /*cursor_start*/,
cursor_pos /*cursor_end*/
);
}
nl::json interpreter::inspect_request_impl(const std::string& code, int cursor_pos, int /*detail_level*/)
{
nl::json kernel_res;
std::string exp = R"(\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)";
std::regex re(R"((\w*(?:\:{2}|\<.*\>|\(.*\)|\[.*\])?)(\.?)*$)");
auto inspect_request = is_inspect_request(code.substr(0, cursor_pos), re);
if (inspect_request.first)
{
inspect(inspect_request.second[0], kernel_res);
}
return kernel_res;
}
nl::json interpreter::is_complete_request_impl(const std::string& code)
{
if (!code.empty() && code[code.size() - 1] == '\\') {
auto found = code.rfind('\n');
if (found == std::string::npos)
found = -1;
auto found1 = found++;
while (isspace(code[++found1])) ;
return xeus::create_is_complete_reply("incomplete", code.substr(found, found1-found));
}
return xeus::create_is_complete_reply("complete");
}
nl::json interpreter::kernel_info_request_impl()
{
nl::json result;
result["implementation"] = "xeus-cpp";
result["implementation_version"] = XEUS_CPP_VERSION;
/* The jupyter-console banner for xeus-cpp is the following:
__ _____ _ _ ___
\ \/ / _ \ | | / __|
> < __/ |_| \__ \
/_/\_\___|\__,_|___/
xeus-cpp: a C++ Jupyter kernel based on Clang
*/
std::string banner = ""
" __ _____ _ _ ___\n"
" \\ \\/ / _ \\ | | / __|\n"
" > < __/ |_| \\__ \\\n"
" /_/\\_\\___|\\__,_|___/\n"
"\n"
" xeus-cpp: a C++ Jupyter kernel - based on Clang-repl\n";
result["banner"] = banner;
result["language_info"]["name"] = "C++";
result["language_info"]["version"] = m_version;
result["language_info"]["mimetype"] = "text/x-c++src";
result["language_info"]["codemirror_mode"] = "text/x-c++src";
result["language_info"]["file_extension"] = ".cpp";
result["help_links"] = nl::json::array();
result["help_links"][0] = nl::json::object(
{{"text", "Xeus-cpp Reference"}, {"url", "https://xeus-cpp.readthedocs.io"}}
);
result["status"] = "ok";
return result;
}
void interpreter::shutdown_request_impl()
{
restore_output();
}
static std::string c_format(const char* format, std::va_list args)
{
// Call vsnprintf once to determine the required buffer length. The
// return value is the number of characters _excluding_ the null byte.
std::va_list args_bufsz;
va_copy(args_bufsz, args);
std::size_t bufsz = vsnprintf(NULL, 0, format, args_bufsz);
va_end(args_bufsz);
// Create an empty string of that size.
std::string s(bufsz, 0);
// Now format the data into this string and return it.
std::va_list args_format;
va_copy(args_format, args);
// The second parameter is the maximum number of bytes that vsnprintf
// will write _including_ the terminating null byte.
vsnprintf(&s[0], s.size() + 1, format, args_format);
va_end(args_format);
return s;
}
void interpreter::redirect_output()
{
p_cout_strbuf = std::cout.rdbuf();
p_cerr_strbuf = std::cerr.rdbuf();
std::cout.rdbuf(&m_cout_buffer);
std::cerr.rdbuf(&m_cerr_buffer);
}
void interpreter::restore_output()
{
std::cout.rdbuf(p_cout_strbuf);
std::cerr.rdbuf(p_cerr_strbuf);
}
void interpreter::publish_stdout(const std::string& s)
{
publish_stream("stdout", s);
}
void interpreter::publish_stderr(const std::string& s)
{
publish_stream("stderr", s);
}
void interpreter::init_includes()
{
Cpp::AddIncludePath((xeus::prefix_path() + "/include/").c_str());
}
void interpreter::init_preamble()
{
//NOLINTBEGIN(cppcoreguidelines-owning-memory)
preamble_manager.register_preamble("introspection", std::make_unique<xintrospection>());
preamble_manager.register_preamble("magics", std::make_unique<xmagics_manager>());
preamble_manager.register_preamble("shell", std::make_unique<xsystem>());
//NOLINTEND(cppcoreguidelines-owning-memory)
}
void interpreter::init_magic()
{
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("executable", executable(m_interpreter));
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("file", writefile());
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("timeit", timeit(&m_interpreter));
// preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("python", pythonexec());
#ifndef EMSCRIPTEN
preamble_manager["magics"].get_cast<xmagics_manager>().register_magic("xassist", xassist());
#endif
}
}