Add Jupyter plugin support

pull/7/head
Ibrahim Numanagić 2021-12-06 01:59:32 -08:00
parent fa6c46ab7e
commit 887be68011
4 changed files with 35 additions and 9 deletions

2
.gitignore vendored
View File

@ -53,4 +53,4 @@ Thumbs.db
.vscode
extra/jupyter/share/jupyter/kernels/codon/kernel.json
scratch.seq
scratch.*

View File

@ -310,13 +310,20 @@ int buildMode(const std::vector<const char *> &args) {
#ifdef CODON_JUPYTER
namespace codon {
int startJupyterKernel(const std::string &argv0, const std::string &configPath);
int startJupyterKernel(const std::string &argv0,
const std::vector<std::string> &plugins,
const std::string &configPath);
}
#endif
int jupyterMode(const std::vector<const char *> &args) {
#ifdef CODON_JUPYTER
int code = codon::startJupyterKernel(args[0], args.size() > 1 ? std::string(args[1])
: "connection.json");
llvm::cl::list<std::string> plugins("plugin",
llvm::cl::desc("Load specified plugin"));
llvm::cl::opt<std::string> input(llvm::cl::Positional,
llvm::cl::desc("<connection file>"),
llvm::cl::init("connection.json"));
llvm::cl::ParseCommandLineOptions(args.size(), args.data());
int code = codon::startJupyterKernel(args[0], plugins, input);
return code;
#else
fmt::print("Jupyter support not included. Please recompile with "

View File

@ -23,7 +23,9 @@ using std::string;
namespace nl = nlohmann;
namespace codon {
CodonJupyter::CodonJupyter(const std::string &argv0) : argv0(argv0) {}
CodonJupyter::CodonJupyter(const std::string &argv0,
const std::vector<std::string> &plugins)
: argv0(argv0), plugins(plugins) {}
nl::json CodonJupyter::execute_request_impl(int execution_counter, const string &code,
bool silent, bool store_history,
@ -62,6 +64,18 @@ nl::json CodonJupyter::execute_request_impl(int execution_counter, const string
void CodonJupyter::configure_impl() {
jit = std::make_unique<codon::jit::JIT>(argv0, "jupyter");
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;
});
}
llvm::cantFail(jit->init());
}
@ -86,11 +100,13 @@ nl::json CodonJupyter::kernel_info_request_impl() {
void CodonJupyter::shutdown_request_impl() {}
int startJupyterKernel(const std::string &argv0, const std::string &configPath) {
int startJupyterKernel(const std::string &argv0,
const std::vector<std::string> &plugins,
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);
auto interpreter = std::make_unique<CodonJupyter>(argv0, plugins);
xeus::xkernel kernel(config, xeus::get_user_name(), move(context), move(interpreter),
xeus::make_xserver_zmq);
kernel.start();

View File

@ -11,9 +11,10 @@ namespace codon {
class CodonJupyter : public xinterpreter {
std::unique_ptr<codon::jit::JIT> jit;
std::string argv0;
std::vector<std::string> plugins;
public:
CodonJupyter(const std::string &argv0);
CodonJupyter(const std::string &argv0, const std::vector<std::string> &plugins);
private:
void configure_impl() override;
@ -34,7 +35,9 @@ private:
void shutdown_request_impl() override;
};
int startJupyterKernel(const std::string &argv0, const std::string &configPath);
int startJupyterKernel(const std::string &argv0,
const std::vector<std::string> &plugins,
const std::string &configPath);
} // namespace codon
#endif