Skip to content

Commit ebeff5d

Browse files
authored
xeus 4.0 updates (#31)
* xeus 4.0 updates
1 parent cc1bc89 commit ebeff5d

File tree

7 files changed

+113
-90
lines changed

7 files changed

+113
-90
lines changed

CMakeLists.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,6 @@ if (XEUS_JAVASCRIPT_BUILD_STATIC)
258258
list(APPEND XEUS_JAVASCRIPT_TARGETS xeus-javascript-static)
259259
endif ()
260260

261-
# xjavascript
262-
# =======
263-
if (XEUS_JAVASCRIPT_BUILD_EXECUTABLE)
264-
find_package(xeus-zmq 1.0.2 REQUIRED)
265-
add_executable(xjavascript ${XEUS_JAVASCRIPT_MAIN_SRC})
266-
target_compile_features(xjavascript PRIVATE cxx_std_17)
267-
xeus_javascript_set_common_options(xjavascript)
268-
xeus_javascript_set_kernel_options(xjavascript)
269-
target_link_libraries(xjavascript PRIVATE xeus-zmq)
270-
endif()
271-
272261

273262
include(WasmBuildOptions)
274263

environment-wasm-host.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ channels:
44
- https://repo.mamba.pm/conda-forge
55
dependencies:
66
- nlohmann_json
7-
- xeus-lite
8-
- xeus >=3.0.5,<4.0
7+
- xeus-lite >=2.0.0,<3.0
8+
- xeus >=4.0.1,<5.0
99
- xtl >=0.7,<0.8

include/xeus-javascript/xinterpreter.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ namespace xeus_javascript
5454

5555
void configure_impl() override;
5656

57-
nl::json execute_request_impl(int execution_counter,
58-
const std::string& code,
59-
bool silent,
60-
bool store_history,
61-
nl::json user_expressions,
62-
bool allow_stdin) override;
57+
void execute_request_impl(xeus::xrequest_context request_context,
58+
send_reply_callback cb,
59+
int execution_counter,
60+
const std::string& code,
61+
xeus::execute_request_config config,
62+
nl::json user_expressions) override;
6363

6464
nl::json complete_request_impl(const std::string& code, int cursor_pos) override;
6565

src/post.js

Lines changed: 55 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -83,42 +83,56 @@ function handle_last_statement(
8383
code_user,
8484
ast
8585
) {
86-
// is the very last character a semicolon?
87-
const last_char_is_semicolon = code_user[code_user.length-1] == ";";
8886

8987
// get the last node
9088
const last_node = ast.body[ast.body.length-1];
9189

90+
9291
// if the last node is an expression statement
9392
// then we need to add a return statement
9493
// so that the expression gets returned
95-
if(!last_char_is_semicolon && last_node.type == "ExpressionStatement" && last_node.expression.type != "AssignmentExpression"){
94+
if(last_node.type == "ExpressionStatement" && last_node.expression.type != "AssignmentExpression"){
9695

97-
const last_node_start = last_node.start;
98-
const last_node_end = last_node.end;
96+
const last_node_expr_start = last_node.expression.start;
97+
const last_node_expr_end = last_node.expression.end;
9998

100-
// remove the last node from the code
101-
const modified_user_code = code_user.substring(0, last_node_start) + code_user.substring(last_node_end);
102-
const code_of_last_node = code_user.substring(last_node_start, last_node_end);
99+
// "rest"
100+
const last_node_rest_end = last_node.end
101+
// search between last_node_expr_end and last_node_rest_end for a semicolon
102+
// if there is a semicolon then we dont need to add a return
103103

104-
const extra_return_code = `return [${code_of_last_node}];`;
104+
let semicolon_found = false;
105+
for(let i=last_node_expr_end; i<last_node_rest_end; i++){
106+
if(code_user[i] == ";"){
107+
semicolon_found = true;
108+
break;
109+
}
110+
}
105111

112+
if(!semicolon_found){
113+
// remove the last node from the code
114+
const modified_user_code = code_user.substring(0, last_node_expr_start) + code_user.substring(last_node_expr_end);
115+
const code_of_last_node = code_user.substring(last_node_expr_start, last_node_expr_end);
106116

107-
return {
108-
with_return: true,
109-
modified_user_code: modified_user_code,
110-
extra_return_code: extra_return_code
117+
118+
const extra_return_code = `return [${code_of_last_node}];`;
119+
120+
121+
return {
122+
with_return: true,
123+
modified_user_code: modified_user_code,
124+
extra_return_code: extra_return_code
125+
}
111126
}
112127
}
113-
else
114-
{
115-
return {
116-
with_return: false,
117-
modified_user_code: code_user,
118-
extra_return_code: ""
119-
}
128+
129+
return {
130+
with_return: false,
131+
modified_user_code: code_user,
132+
extra_return_code: ""
120133
}
121134

135+
122136
}
123137

124138
function transform_import_source (source) {
@@ -317,7 +331,7 @@ function _configure() {
317331
msg += " ";
318332
}
319333
}
320-
Module.interpreter.publish_stream("stdout", `${msg}\n`);
334+
Module.interpreter.publish_stream(Module.get_request_context(), "stdout", `${msg}\n`);
321335
}
322336
// alias
323337
globalThis.pp = globalThis.pprint;
@@ -330,7 +344,7 @@ function _configure() {
330344
msg += " ";
331345
}
332346
}
333-
Module.interpreter.publish_stream("stdout", `${msg}\n`);
347+
Module.interpreter.publish_stream(Module.get_request_context(), "stdout", `${msg}\n`);
334348
}
335349
console.error = function (... args) {
336350
let msg = ""
@@ -340,29 +354,42 @@ function _configure() {
340354
msg += " ";
341355
}
342356
}
343-
Module.interpreter.publish_stream("stderr", `${msg}\n`);
357+
Module.interpreter.publish_stream(Module.get_request_context(), "stderr", `${msg}\n`);
344358
}
345359

346360
// add ijs to global scope
347361
globalThis["ijs"] = Module["ijs"];
348362

349363
Module.interpreter = Module._get_interpreter();
350364

351-
Module.interpreter.publish_stream("stdout", "Configured\n");
365+
366+
// the execute request context
367+
Module._xrequest_context = null;
352368
}
353369

354370

371+
Module.get_request_context = function () {
372+
return Module._xrequest_context;
373+
}
374+
355375
Module.get_interpreter = function () {
356376
return Module.interpreter;
357377
}
358378

359379

360-
async function _call_user_code(code) {
380+
async function _call_user_code(context, code) {
361381

382+
// call _xrequest_context.delete() if it exists
383+
if(Module._xrequest_context && Module._xrequest_context.delete){
384+
Module._xrequest_context.delete();
385+
}
386+
Module._xrequest_context = context;
362387

363388
try{
389+
364390
const ret = make_async_from_code(code);
365391
const async_function = ret.async_function;
392+
366393
let result_promise = async_function();
367394

368395
let data = {};
@@ -441,8 +468,6 @@ function complete_line(code_line){
441468
}
442469
}
443470
let pseudo_expression = code_line.substring(code_begin);
444-
445-
446471
// pseudo_expression is "fubar.b" "fubar", "fubar." or "fubar['aaa']"
447472

448473
// find part right of dot / bracket
@@ -469,16 +494,13 @@ function complete_line(code_line){
469494
curser_start += split_pos+1;
470495
}
471496

472-
473-
474497
// find root object
475498
let root_object = globalThis;
476499
if(root_object_str != ""){
477500
try{
478501
root_object = eval(root_object_str);
479502
}
480503
catch(err){
481-
Module["_publish_stderr_stream"](`${err}\n`);
482504
return {
483505
matches : [],
484506
cursor_start : curser_start,
@@ -551,10 +573,10 @@ let ijs = {
551573
magic_imports: magic_imports,
552574
display : {
553575
display: function (data, metadata={}, transient={}) {
554-
Module.get_interpreter().display_data(data, metadata, transient);
576+
Module.get_interpreter().display_data(Module.get_request_context(), data, metadata, transient);
555577
},
556578
update_display_data: function (data, metadata={}, transient={}) {
557-
Module.get_interpreter().update_display_data(data, metadata, transient);
579+
Module.get_interpreter().update_display_data(Module.get_request_context(), data, metadata, transient);
558580
},
559581
mime_type: function (mime_type, data) {
560582
this.display({ mime_type: data });
@@ -636,7 +658,7 @@ let ijs = {
636658
}
637659
}
638660
catch(err){
639-
Module.interpreter.publish_stream("stderr", `display error: ${err}\n`);
661+
Module.interpreter.publish_stream(Module.get_request_context(), "stderr", `display error: ${err}\n`);
640662
}
641663
}
642664

src/xcomm.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,50 @@ namespace xeus_javascript
2929

3030
xcomm::xcomm(
3131
std::string target_name,
32-
nl::json data,
33-
nl::json metadata,
34-
em::val buffers,
3532
nl::json kwargs
3633
)
3734
: m_comm(target(target_name), id(kwargs))
3835
{
39-
m_comm.open(metadata, data, jslist_to_cpp_buffers(buffers));
4036
}
4137

4238
xcomm::xcomm(xeus::xcomm&& comm)
4339
: m_comm(std::move(comm))
4440
{
4541
}
46-
4742
xcomm::~xcomm()
4843
{
4944
}
45+
void xcomm::open(
46+
nl::json parent_header,
47+
const nl::json & data,
48+
const nl::json& metadata,
49+
const em::val & buffers)
50+
{
51+
m_comm.open(parent_header, metadata, data, jslist_to_cpp_buffers(buffers));
52+
}
5053

51-
std::string xcomm::comm_id() const
54+
void xcomm::close(nl::json parent_header, const nl::json & data, const nl::json& metadata, const em::val & buffers)
5255
{
53-
return m_comm.id();
56+
m_comm.close(parent_header, metadata, data, jslist_to_cpp_buffers(buffers));
5457
}
5558

56-
bool xcomm::kernel() const
59+
void xcomm::send( nl::json parent_header, const nl::json & data, const nl::json& metadata, const em::val & buffers)
5760
{
58-
return true;
61+
m_comm.send(parent_header, metadata, data, jslist_to_cpp_buffers(buffers));
5962
}
6063

61-
void xcomm::close(const nl::json & data, const nl::json& metadata, const em::val & buffers)
64+
65+
std::string xcomm::comm_id() const
6266
{
63-
m_comm.close(metadata, data, jslist_to_cpp_buffers(buffers));
67+
return m_comm.id();
6468
}
6569

66-
void xcomm::send(const nl::json & data, const nl::json& metadata, const em::val & buffers)
70+
bool xcomm::kernel() const
6771
{
68-
m_comm.send(metadata, data, jslist_to_cpp_buffers(buffers));
72+
return true;
6973
}
7074

75+
7176
void xcomm::on_msg(const js_callback_type& callback)
7277
{
7378
m_comm.on_message(cpp_callback(callback));
@@ -120,9 +125,10 @@ namespace xeus_javascript
120125

121126
void export_xcomm(){
122127
em::class_<xcomm>("Comm")
123-
.constructor<std::string, nl::json, nl::json, em::val, nl::json>()
128+
.constructor<std::string, nl::json>()
124129
.function("comm_id", &xcomm::comm_id)
125130
.function("kernel", &xcomm::kernel)
131+
.function("open", &xcomm::open)
126132
.function("close", &xcomm::close)
127133
.function("send", &xcomm::send)
128134
.function("on_msg", &xcomm::on_msg)

src/xcomm.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ namespace xeus_javascript
4040

4141
xcomm(
4242
std::string target_name,
43-
nl::json data,
44-
nl::json metadata,
45-
em::val buffers,
4643
nl::json extra_kwargs
4744
);
4845
xcomm(xeus::xcomm&& comm);
@@ -52,8 +49,9 @@ namespace xeus_javascript
5249
std::string comm_id() const;
5350
bool kernel() const;
5451

55-
void close(const nl::json& data, const nl::json& metadata, const em::val& buffers);
56-
void send(const nl::json& data, const nl::json& metadata, const em::val& buffers);
52+
void open(nl::json parent_header, const nl::json& data, const nl::json& metadata, const em::val& buffers);
53+
void close(nl::json parent_header, const nl::json& data, const nl::json& metadata, const em::val& buffers);
54+
void send(nl::json parent_header, const nl::json& data, const nl::json& metadata, const em::val& buffers);
5755
void on_msg(const js_callback_type& callback);
5856
void on_close(const js_callback_type& callback);
5957

0 commit comments

Comments
 (0)