forked from compiler-research/xeus-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_emscripten_kernel.cpp
More file actions
44 lines (35 loc) · 1.28 KB
/
main_emscripten_kernel.cpp
File metadata and controls
44 lines (35 loc) · 1.28 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
/***************************************************************************
* Copyright (c) 2023, xeus-cpp contributors
*
* Distributed under the terms of the BSD 3-Clause License.
*
* The full license is in the file LICENSE, distributed with this software.
****************************************************************************/
#include <iostream>
#include <memory>
#include <emscripten/bind.h>
#include <xeus/xembind.hpp>
#include "xeus-cpp/xinterpreter.hpp"
template <class interpreter_type>
static interpreter_type* builder_with_args(emscripten::val js_args)
{
// TODO: Refactor interpreter constructor to avoid static args-to-argv conversion.
static std::vector<std::string> args = emscripten::vecFromJSArray<std::string>(js_args);
static std::vector<const char*> argv_vec;
for (const auto& s : args)
{
argv_vec.push_back(s.c_str());
}
int argc = static_cast<int>(argv_vec.size());
char** argv = const_cast<char**>(argv_vec.data());
auto* res = new interpreter_type(argc, argv);
argv_vec.clear();
args.clear();
return res;
}
EMSCRIPTEN_BINDINGS(my_module)
{
xeus::export_core();
using interpreter_type = xcpp::interpreter;
xeus::export_kernel<interpreter_type, &builder_with_args<interpreter_type>>("xkernel");
}