2021-12-29 13:11:56 -08:00
|
|
|
#include "jupyter.h"
|
2021-11-09 03:47:41 -08:00
|
|
|
|
|
|
|
#ifdef CODON_JUPYTER
|
2021-12-29 13:11:56 -08:00
|
|
|
#include <codecvt>
|
2021-11-05 15:26:32 -07:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
2021-11-01 03:31:24 -07:00
|
|
|
#include <iostream>
|
2021-12-29 13:11:56 -08:00
|
|
|
#include <locale>
|
2021-11-01 03:31:24 -07:00
|
|
|
#include <nlohmann/json.hpp>
|
2021-11-05 15:26:32 -07:00
|
|
|
#include <unistd.h>
|
2021-11-01 03:31:24 -07:00
|
|
|
#include <xeus/xhelper.hpp>
|
2021-11-09 03:47:41 -08:00
|
|
|
#include <xeus/xkernel.hpp>
|
|
|
|
#include <xeus/xkernel_configuration.hpp>
|
|
|
|
#include <xeus/xserver_zmq.hpp>
|
|
|
|
|
|
|
|
#include "codon/compiler/compiler.h"
|
|
|
|
#include "codon/compiler/error.h"
|
|
|
|
#include "codon/compiler/jit.h"
|
2021-11-21 05:21:05 -08:00
|
|
|
#include "codon/parser/common.h"
|
2021-11-09 03:47:41 -08:00
|
|
|
#include "codon/util/common.h"
|
2021-11-01 03:31:24 -07:00
|
|
|
|
|
|
|
using std::move;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
namespace nl = nlohmann;
|
|
|
|
namespace codon {
|
|
|
|
|
2021-12-06 01:59:32 -08:00
|
|
|
CodonJupyter::CodonJupyter(const std::string &argv0,
|
|
|
|
const std::vector<std::string> &plugins)
|
|
|
|
: argv0(argv0), plugins(plugins) {}
|
2021-11-09 03:47:41 -08:00
|
|
|
|
2021-11-01 03:31:24 -07:00
|
|
|
nl::json CodonJupyter::execute_request_impl(int execution_counter, const string &code,
|
|
|
|
bool silent, bool store_history,
|
|
|
|
nl::json user_expressions,
|
|
|
|
bool allow_stdin) {
|
2021-11-09 03:47:41 -08:00
|
|
|
auto result = jit->exec(code);
|
2021-11-21 05:21:05 -08:00
|
|
|
string failed;
|
2021-11-09 03:47:41 -08:00
|
|
|
llvm::handleAllErrors(
|
|
|
|
result.takeError(),
|
|
|
|
[&](const codon::error::ParserErrorInfo &e) {
|
|
|
|
std::vector<string> backtrace;
|
|
|
|
for (auto &msg : e)
|
|
|
|
backtrace.push_back(msg.getMessage());
|
|
|
|
string err = backtrace[0];
|
|
|
|
backtrace.erase(backtrace.begin());
|
2021-11-21 06:28:10 -08:00
|
|
|
failed = fmt::format("Compile error: {}\nBacktrace:\n{}", err,
|
|
|
|
ast::join(backtrace, " \n"));
|
2021-11-09 03:47:41 -08:00
|
|
|
},
|
|
|
|
[&](const codon::error::RuntimeErrorInfo &e) {
|
2021-11-21 06:28:10 -08:00
|
|
|
auto backtrace = e.getBacktrace();
|
|
|
|
failed = fmt::format("Runtime error: {}\nBacktrace:\n{}", e.getMessage(),
|
|
|
|
ast::join(backtrace, " \n"));
|
2021-11-09 03:47:41 -08:00
|
|
|
});
|
2021-11-21 05:21:05 -08:00
|
|
|
if (failed.empty()) {
|
2021-12-29 13:11:56 -08:00
|
|
|
std::string msg = *result;
|
2021-11-09 03:47:41 -08:00
|
|
|
nl::json pub_data;
|
2021-12-29 13:11:56 -08:00
|
|
|
if (ast::startswith(msg, "\x00\x00__codon/mime__\x00")) {
|
|
|
|
std::string mime = "";
|
|
|
|
int i = 17;
|
|
|
|
for (; i < msg.size() && msg[i]; i++)
|
|
|
|
mime += msg[i];
|
|
|
|
if (i < msg.size() && !msg[i]) {
|
|
|
|
i += 1;
|
|
|
|
} else {
|
|
|
|
mime = "text/plain";
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string out;
|
|
|
|
out.reserve(msg.size() * 1.5);
|
|
|
|
for (; i < msg.size(); i++) {
|
|
|
|
uint8_t c = msg[i];
|
|
|
|
if (c <= 127) {
|
|
|
|
out.push_back(c);
|
|
|
|
} else {
|
|
|
|
out.push_back((c >> 6) | 0xC0);
|
|
|
|
out.push_back((c & 0x3F) | 0x80);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub_data[mime] = out;
|
|
|
|
} else {
|
|
|
|
pub_data["text/plain"] = msg;
|
|
|
|
}
|
2021-11-09 03:47:41 -08:00
|
|
|
publish_execution_result(execution_counter, move(pub_data), nl::json::object());
|
2021-11-21 05:21:05 -08:00
|
|
|
return nl::json{{"status", "ok"},
|
|
|
|
{"payload", nl::json::array()},
|
|
|
|
{"user_expressions", nl::json::object()}};
|
|
|
|
} else {
|
|
|
|
publish_stream("stderr", failed);
|
|
|
|
return nl::json{{"status", "error"}};
|
2021-11-09 03:47:41 -08:00
|
|
|
}
|
2021-11-01 03:31:24 -07:00
|
|
|
}
|
|
|
|
|
2021-11-05 15:26:32 -07:00
|
|
|
void CodonJupyter::configure_impl() {
|
2021-11-22 05:32:49 -08:00
|
|
|
jit = std::make_unique<codon::jit::JIT>(argv0, "jupyter");
|
2021-12-06 01:59:32 -08:00
|
|
|
|
|
|
|
for (const auto &plugin : plugins) {
|
|
|
|
// TODO: error handling on plugin init
|
|
|
|
bool failed = false;
|
|
|
|
llvm::handleAllErrors(jit->getCompiler()->load(plugin),
|
|
|
|
[&failed](const codon::error::PluginErrorInfo &e) {
|
|
|
|
codon::compilationError(e.getMessage(), /*file=*/"",
|
|
|
|
/*line=*/0, /*col=*/0,
|
|
|
|
/*terminate=*/false);
|
|
|
|
failed = true;
|
|
|
|
});
|
|
|
|
}
|
2021-11-05 15:26:32 -07:00
|
|
|
llvm::cantFail(jit->init());
|
|
|
|
}
|
2021-11-01 03:31:24 -07:00
|
|
|
|
|
|
|
nl::json CodonJupyter::complete_request_impl(const string &code, int cursor_pos) {
|
2021-11-21 05:21:05 -08:00
|
|
|
return nl::json{{"status", "ok"}};
|
2021-11-01 03:31:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nl::json CodonJupyter::inspect_request_impl(const string &code, int cursor_pos,
|
|
|
|
int detail_level) {
|
2021-11-21 05:21:05 -08:00
|
|
|
return nl::json{{"status", "ok"}};
|
2021-11-01 03:31:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nl::json CodonJupyter::is_complete_request_impl(const string &code) {
|
2021-11-21 05:21:05 -08:00
|
|
|
return nl::json{{"status", "complete"}};
|
2021-11-01 03:31:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nl::json CodonJupyter::kernel_info_request_impl() {
|
2021-11-21 06:28:10 -08:00
|
|
|
return xeus::create_info_reply("1.0", "codon_kernel", "0.1.0", "python", "3.7",
|
|
|
|
"text/x-python", ".seq", "python", "", "",
|
|
|
|
"Codon Kernel");
|
2021-11-01 03:31:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void CodonJupyter::shutdown_request_impl() {}
|
|
|
|
|
2021-12-06 01:59:32 -08:00
|
|
|
int startJupyterKernel(const std::string &argv0,
|
|
|
|
const std::vector<std::string> &plugins,
|
|
|
|
const std::string &configPath) {
|
2021-11-09 03:47:41 -08:00
|
|
|
xeus::xconfiguration config = xeus::load_configuration(configPath);
|
|
|
|
|
|
|
|
auto context = xeus::make_context<zmq::context_t>();
|
2021-12-06 01:59:32 -08:00
|
|
|
auto interpreter = std::make_unique<CodonJupyter>(argv0, plugins);
|
2021-11-09 03:47:41 -08:00
|
|
|
xeus::xkernel kernel(config, xeus::get_user_name(), move(context), move(interpreter),
|
|
|
|
xeus::make_xserver_zmq);
|
|
|
|
kernel.start();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace codon
|
|
|
|
#endif
|