2021-11-01 03:31:24 -07:00
|
|
|
#include "codon.h"
|
2021-11-09 03:47:41 -08:00
|
|
|
|
|
|
|
#ifdef CODON_JUPYTER
|
2021-11-05 15:26:32 -07:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
2021-11-01 03:31:24 -07:00
|
|
|
#include <iostream>
|
|
|
|
#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-11-09 03:47:41 -08:00
|
|
|
CodonJupyter::CodonJupyter(const std::string &argv0) : argv0(argv0) {}
|
|
|
|
|
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 05:21:05 -08:00
|
|
|
failed =
|
|
|
|
fmt::format("Error: {}\nBacktrace:\n{}", err, ast::join(backtrace, " \n"));
|
|
|
|
// publish_execution_error("ParserError", err, backtrace);
|
|
|
|
// failed = true;
|
2021-11-09 03:47:41 -08:00
|
|
|
},
|
|
|
|
[&](const codon::error::RuntimeErrorInfo &e) {
|
2021-11-21 05:21:05 -08:00
|
|
|
failed = fmt::format("Error: {}", e.getMessage());
|
|
|
|
// publish_execution_error(e.getType(), e.getMessage(), {});
|
|
|
|
// failed = true;
|
2021-11-09 03:47:41 -08:00
|
|
|
});
|
2021-11-21 05:21:05 -08:00
|
|
|
if (failed.empty()) {
|
2021-11-09 03:47:41 -08:00
|
|
|
nl::json pub_data;
|
2021-11-21 05:21:05 -08:00
|
|
|
pub_data["text/plain"] = fmt::format(">> {}", *result);
|
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-09 03:47:41 -08:00
|
|
|
jit = std::make_unique<codon::jit::JIT>(argv0);
|
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() {
|
|
|
|
return xeus::create_info_reply("", "codon_kernel", "0.1.0", "python", "3.7",
|
|
|
|
"text/x-python", ".seq");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodonJupyter::shutdown_request_impl() {}
|
|
|
|
|
2021-11-09 03:47:41 -08:00
|
|
|
int startJupyterKernel(const std::string &argv0, const std::string &configPath) {
|
|
|
|
xeus::xconfiguration config = xeus::load_configuration(configPath);
|
|
|
|
|
|
|
|
auto context = xeus::make_context<zmq::context_t>();
|
|
|
|
auto interpreter = std::make_unique<CodonJupyter>(argv0);
|
|
|
|
xeus::xkernel kernel(config, xeus::get_user_name(), move(context), move(interpreter),
|
|
|
|
xeus::make_xserver_zmq);
|
|
|
|
kernel.start();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace codon
|
|
|
|
#endif
|