Skip to content

Commit 64b67ea

Browse files
authored
Upgrade xeus (#35)
* missing await * no more async * delete * update dependencies
1 parent b1488a0 commit 64b67ea

File tree

8 files changed

+105
-113
lines changed

8 files changed

+105
-113
lines changed

CMakeLists.txt

+8-6
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,9 @@ endif()
6666
# Dependencies
6767
# ============
6868

69-
set(xtl_REQUIRED_VERSION 0.7.0)
70-
set(xeus_REQUIRED_VERSION 3.0.5)
69+
set(xeus_REQUIRED_VERSION 5.0.0)
7170

7271
find_package(xeus-lite REQUIRED)
73-
find_package(xtl ${xtl_REQUIRED_VERSION} REQUIRED)
7472
find_package(xeus ${xeus_REQUIRED_VERSION} REQUIRED)
7573

7674

@@ -84,6 +82,7 @@ if (MSVC)
8482
endif ()
8583

8684
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Intel")
85+
message(STATUS "COMPILER ID: ${CMAKE_CXX_COMPILER_ID}")
8786
if(NOT EMSCRIPTEN)
8887
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wunused-parameter -Wextra -Wreorder")
8988
endif()
@@ -92,6 +91,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU"
9291
CHECK_CXX_COMPILER_FLAG("-std=c++17" HAS_CPP_17_FLAG)
9392
if (HAS_CPP_17_FLAG)
9493
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
94+
9595
else ()
9696
message(FATAL_ERROR "Unsupported compiler -- xeus requires C++17 support!")
9797
endif ()
@@ -203,9 +203,12 @@ macro(xeus_javascript_create_target target_name linkage output_name)
203203
SOVERSION ${XEUS_JAVASCRIPT_VERSION_MAJOR}
204204
OUTPUT_NAME "lib${output_name}")
205205

206+
target_compile_features(${target_name} PUBLIC cxx_std_17)
207+
208+
209+
206210
target_compile_definitions(${target_name} PUBLIC "XEUS_JAVASCRIPT_EXPORTS")
207211

208-
target_compile_features(${target_name} PRIVATE cxx_std_17)
209212

210213
target_include_directories(${target_name}
211214
PUBLIC
@@ -218,7 +221,7 @@ macro(xeus_javascript_create_target target_name linkage output_name)
218221
set(XEUS_JAVASCRIPT_XEUS_TARGET xeus-static)
219222
endif ()
220223

221-
target_link_libraries(${target_name} PUBLIC ${XEUS_JAVASCRIPT_XEUS_TARGET} xtl)
224+
target_link_libraries(${target_name} PUBLIC ${XEUS_JAVASCRIPT_XEUS_TARGET})
222225
if (WIN32 OR CYGWIN)
223226
#
224227
elseif (APPLE)
@@ -270,7 +273,6 @@ xeus_wasm_link_options(xjavascript "web,worker")
270273
#
271274
target_link_options(xjavascript
272275
PUBLIC "SHELL: -s NO_EXIT_RUNTIME=1"
273-
PUBLIC "SHELL: -s ASYNCIFY"
274276
PUBLIC "SHELL: -s FORCE_FILESYSTEM=1"
275277
PUBLIC "SHELL: --pre-js ${CMAKE_CURRENT_SOURCE_DIR}/src/pre.js"
276278
PUBLIC "SHELL: --post-js ${CMAKE_CURRENT_SOURCE_DIR}/src/post.js"

environment-wasm-host.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ channels:
44
- https://repo.mamba.pm/conda-forge
55
dependencies:
66
- nlohmann_json
7-
- xeus-lite >=2.0.0,<3.0
8-
- xeus >=4.0.1,<5.0
9-
- xtl >=0.7,<0.8
7+
- xeus-lite >=3.0.0,<4.0
8+
- xeus >=5.0.0,<6.0

include/xeus-javascript/convert_json.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <emscripten/bind.h>
55
#include <emscripten/emscripten.h>
66

7+
// decay_t
8+
#include <type_traits>
9+
710
namespace emscripten {
811
namespace internal {
912

include/xeus-javascript/xinterpreter.hpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,15 @@ namespace nl = nlohmann;
2929
namespace xeus_javascript
3030
{
3131

32-
void publish_stdout_stream(const std::string& message);
33-
void publish_stderr_stream(const std::string& message);
34-
void display_data(const std::string& json_str);
35-
void update_display_data(const std::string& json_str);
3632

37-
void publish_execution_result(const std::string jstring);
38-
void publish_execution_error(const std::string jstring);
33+
struct XEUS_JAVASCRIPT_API send_reply_callback_wrapper
34+
{
35+
xeus::xinterpreter::send_reply_callback callback;
36+
void call(const nl::json& reply) const;
3937

38+
void reply_success() const;
39+
void reply_error(const std::string& error_type, const std::string& error_message, const std::string & error_stack) const;
40+
};
4041

4142
class XEUS_JAVASCRIPT_API interpreter : public xeus::xinterpreter
4243
{
@@ -49,13 +50,13 @@ namespace xeus_javascript
4950
{
5051
return "xjavascript";
5152
}
53+
void js_publish_execution_error(const std::string& error_type, const std::string& error_message, const std::string & error_stack);
5254

5355
protected:
5456

5557
void configure_impl() override;
5658

57-
void execute_request_impl(xeus::xrequest_context request_context,
58-
send_reply_callback cb,
59+
void execute_request_impl(send_reply_callback cb,
5960
int execution_counter,
6061
const std::string& code,
6162
xeus::execute_request_config config,
@@ -73,6 +74,7 @@ namespace xeus_javascript
7374

7475
void shutdown_request_impl() override;
7576

77+
7678
};
7779

7880
void export_xinterpreter();

src/post.js

+30-49
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ function _configure() {
331331
msg += " ";
332332
}
333333
}
334-
Module.interpreter.publish_stream(Module.get_request_context(), "stdout", `${msg}\n`);
334+
Module.interpreter.publish_stream("stdout", `${msg}\n`);
335335
}
336336
// alias
337337
globalThis.pp = globalThis.pprint;
@@ -344,7 +344,7 @@ function _configure() {
344344
msg += " ";
345345
}
346346
}
347-
Module.interpreter.publish_stream(Module.get_request_context(), "stdout", `${msg}\n`);
347+
Module.interpreter.publish_stream("stdout", `${msg}\n`);
348348
}
349349
console.error = function (... args) {
350350
let msg = ""
@@ -354,20 +354,16 @@ function _configure() {
354354
msg += " ";
355355
}
356356
}
357-
Module.interpreter.publish_stream(Module.get_request_context(), "stderr", `${msg}\n`);
357+
Module.interpreter.publish_stream("stderr", `${msg}\n`);
358358
}
359359

360360
// add ijs to global scope
361361
globalThis["ijs"] = Module["ijs"];
362362

363363
Module.interpreter = Module._get_interpreter();
364364

365-
366-
// the execute request context
367-
Module._xrequest_context = null;
368365
}
369366

370-
371367
Module.get_request_context = function () {
372368
return Module._xrequest_context;
373369
}
@@ -377,13 +373,8 @@ Module.get_interpreter = function () {
377373
}
378374

379375

380-
async function _call_user_code(context, code) {
376+
async function _call_user_code(execution_counter, config, reply_callback, code) {
381377

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;
387378

388379
try{
389380

@@ -398,17 +389,14 @@ async function _call_user_code(context, code) {
398389
let result_holder = await result_promise;
399390
let result = result_holder[0];
400391
data = Module["ijs"]["get_mime_bundle"](result);
392+
if(!config.silent){
393+
Module.get_interpreter().publish_execution_result(execution_counter, data, {});
394+
}
401395
}
402396
else{
403397
await result_promise;
404398
}
405-
406-
return {
407-
has_error: false,
408-
with_result: ret.with_return,
409-
pub_data: data,
410-
metadata: {},
411-
};
399+
reply_callback.reply_success();
412400
}
413401
catch(err){
414402

@@ -423,35 +411,28 @@ async function _call_user_code(context, code) {
423411
if(msg instanceof Promise){
424412
msg = await msg;
425413
}
426-
427-
return{
428-
error_type: "C++ Exception",
429-
error_message: `${msg}`,
430-
error_stack: "",
431-
has_error: true
432-
}
414+
Module.get_interpreter().publish_execution_error("C++ Exception", `${msg}`, "");
415+
reply_callback.reply_error("C++ Exception", `${msg}`, "");
433416
}
434-
435-
436-
// remove a bunch of noise from the stack
437-
let err_stack_str = `${err.stack || ""}`;
438-
let err_stack_lines = err_stack_str.split("\n");
439-
let used_lines = [];
440-
for(const line of err_stack_lines){
441-
if(line.includes("make_async_from_code ")){
442-
break;
417+
else{
418+
// remove a bunch of noise from the stack
419+
let err_stack_str = `${err.stack || ""}`;
420+
let err_stack_lines = err_stack_str.split("\n");
421+
let used_lines = [];
422+
for(const line of err_stack_lines){
423+
if(line.includes("make_async_from_code ")){
424+
break;
425+
}
426+
used_lines.push(line);
443427
}
444-
used_lines.push(line);
428+
err_stack_str = used_lines.join("\n");
429+
Module.get_interpreter().publish_execution_error(`${err.name || "UnkwownError"}`, `${err.message || ""}`, `${err_stack_str}`);
430+
reply_callback.reply_error(`${err.name || "UnkwownError"}`, `${err.message || ""}`, `${err_stack_str}`);
445431
}
446-
err_stack_str = used_lines.join("\n");
447-
448-
449-
return {
450-
error_type: `${err.name || "UnkwownError"}`,
451-
error_message: `${err.message || ""}`,
452-
error_stack: `${err_stack_str}`,
453-
has_error: true
454-
};
432+
}
433+
finally{
434+
config.delete();
435+
reply_callback.delete();
455436
}
456437

457438
}
@@ -576,10 +557,10 @@ let ijs = {
576557
magic_imports: magic_imports,
577558
display : {
578559
display: function (data, metadata={}, transient={}) {
579-
Module.get_interpreter().display_data(Module.get_request_context(), data, metadata, transient);
560+
Module.get_interpreter().display_data(data, metadata, transient);
580561
},
581562
update_display_data: function (data, metadata={}, transient={}) {
582-
Module.get_interpreter().update_display_data(Module.get_request_context(), data, metadata, transient);
563+
Module.get_interpreter().update_display_data(data, metadata, transient);
583564
},
584565
mime_type: function (mime_type, data) {
585566
this.display({ mime_type: data });
@@ -661,7 +642,7 @@ let ijs = {
661642
}
662643
}
663644
catch(err){
664-
Module.interpreter.publish_stream(Module.get_request_context(), "stderr", `display error: ${err}\n`);
645+
Module.interpreter.publish_stream("stderr", `display error: ${err}\n`);
665646
}
666647
}
667648

src/xcomm.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,21 @@ namespace xeus_javascript
4343
{
4444
}
4545
void xcomm::open(
46-
nl::json parent_header,
4746
const nl::json & data,
4847
const nl::json& metadata,
4948
const em::val & buffers)
5049
{
51-
m_comm.open(parent_header, metadata, data, jslist_to_cpp_buffers(buffers));
50+
m_comm.open(metadata, data, jslist_to_cpp_buffers(buffers));
5251
}
5352

54-
void xcomm::close(nl::json parent_header, const nl::json & data, const nl::json& metadata, const em::val & buffers)
53+
void xcomm::close( const nl::json & data, const nl::json& metadata, const em::val & buffers)
5554
{
56-
m_comm.close(parent_header, metadata, data, jslist_to_cpp_buffers(buffers));
55+
m_comm.close(metadata, data, jslist_to_cpp_buffers(buffers));
5756
}
5857

59-
void xcomm::send( nl::json parent_header, const nl::json & data, const nl::json& metadata, const em::val & buffers)
58+
void xcomm::send(const nl::json & data, const nl::json& metadata, const em::val & buffers)
6059
{
61-
m_comm.send(parent_header, metadata, data, jslist_to_cpp_buffers(buffers));
60+
m_comm.send(metadata, data, jslist_to_cpp_buffers(buffers));
6261
}
6362

6463

src/xcomm.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ namespace xeus_javascript
4949
std::string comm_id() const;
5050
bool kernel() const;
5151

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);
52+
void open(const nl::json& data, const nl::json& metadata, const em::val& buffers);
53+
void close(const nl::json& data, const nl::json& metadata, const em::val& buffers);
54+
void send(const nl::json& data, const nl::json& metadata, const em::val& buffers);
5555
void on_msg(const js_callback_type& callback);
5656
void on_close(const js_callback_type& callback);
5757

0 commit comments

Comments
 (0)