Skip to content

Commit 227329c

Browse files
committed
Attempt to process cell without semicolon
1 parent 4745f72 commit 227329c

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed

src/xinterpreter.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "xinput.hpp"
2929
#include "xinspect.hpp"
3030
#include "xmagics/os.hpp"
31+
#include "xmime_internal.hpp"
3132
#include "xparser.hpp"
3233
#include "xsystem.hpp"
3334

@@ -128,7 +129,7 @@ __get_cxx_version ()
128129

129130
void interpreter::execute_request_impl(
130131
send_reply_callback cb,
131-
int /*execution_count*/,
132+
int execution_counter,
132133
const std::string& code,
133134
xeus::execute_request_config config,
134135
nl::json /*user_expressions*/
@@ -153,7 +154,9 @@ __get_cxx_version ()
153154
auto errorlevel = 0;
154155
std::string ename;
155156
std::string evalue;
156-
bool compilation_result = false;
157+
//bool compilation_result = false;
158+
intptr_t output_value;
159+
bool hadError = false;
157160

158161
// If silent is set to true, temporarily dismiss all std::cerr and
159162
// std::cout outputs resulting from `process_code`.
@@ -174,7 +177,7 @@ __get_cxx_version ()
174177
try
175178
{
176179
StreamRedirectRAII R(err);
177-
compilation_result = Cpp::Process(code.c_str());
180+
output_value = Cpp::Evaluate(code.c_str(), &hadError);
178181
}
179182
catch (std::exception& e)
180183
{
@@ -188,7 +191,7 @@ __get_cxx_version ()
188191
ename = "Error: ";
189192
}
190193

191-
if (compilation_result)
194+
if (hadError)
192195
{
193196
errorlevel = 1;
194197
ename = "Error: ";
@@ -233,15 +236,12 @@ __get_cxx_version ()
233236
}
234237
else
235238
{
236-
/*
237-
// Publish a mime bundle for the last return value if
238-
// the semicolon was omitted.
239-
if (!config.silent && output.hasValue() && trim(code).back() != ';')
240-
{
241-
nl::json pub_data = mime_repr(output);
242-
publish_execution_result(execution_counter, std::move(pub_data), nl::json::object());
243-
}
244-
*/
239+
// Publish a mime bundle for the last return value if
240+
// the semicolon was omitted.
241+
if (!config.silent && !hadError && trim(code).back() != ';') {
242+
nl::json pub_data = mime_repr(output_value);
243+
publish_execution_result(execution_counter, std::move(pub_data), nl::json::object());
244+
}
245245
// Compose execute_reply message.
246246
kernel_res["status"] = "ok";
247247
kernel_res["payload"] = nl::json::array();

src/xmime_internal.hpp

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/************************************************************************************
2+
* Copyright (c) 2016, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
3+
* Copyright (c) 2016, QuantStack *
4+
* *
5+
* Distributed under the terms of the BSD 3-Clause License. *
6+
* *
7+
* The full license is in the file LICENSE, distributed with this software. *
8+
************************************************************************************/
9+
10+
#ifndef XCPP_MIME_INTERNAL_HPP
11+
#define XCPP_MIME_INTERNAL_HPP
12+
13+
#include <cstddef>
14+
#include <locale>
15+
#include <string>
16+
17+
#include "clang/Interpreter/CppInterOp.h" // from CppInterOp package
18+
19+
#include <nlohmann/json.hpp>
20+
21+
namespace nl = nlohmann;
22+
23+
namespace xcpp
24+
{
25+
inline nl::json mime_repr(intptr_t V)
26+
{
27+
// Return a JSON mime bundle representing the specified value.
28+
// Include "xcpp/xmime.hpp" only on the first time a variable is displayed.
29+
static bool xmime_included = false;
30+
31+
if (!xmime_included)
32+
{
33+
Cpp::Declare("#include \"xcpp/xmime.hpp\"");
34+
xmime_included = true;
35+
}
36+
37+
intptr_t mimeReprV;
38+
bool hadError = false;
39+
{
40+
std::ostringstream oss;
41+
oss << reinterpret_cast<void*>(V);
42+
std::string pointerStr = oss.str();
43+
44+
std::string code;
45+
code = "using xcpp::mime_bundle_repr; ";
46+
code += "mime_bundle_repr(*((const void**)";
47+
code += pointerStr;
48+
code += "));";
49+
50+
mimeReprV = Cpp::Evaluate(code.c_str(), &hadError);
51+
}
52+
53+
if (!hadError && mimeReprV != 0) {
54+
return *(nl::json*)mimeReprV;
55+
} else {
56+
return nl::json::object();
57+
}
58+
}
59+
}
60+
61+
#endif

0 commit comments

Comments
 (0)