Skip to content

Commit

Permalink
Enable emjs error handling (pyodide#1080)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hood Chatham authored Jan 12, 2021
1 parent 96fcf0e commit f6a67d9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ PYODIDE_CXX=$(PYODIDE_ROOT)/ccache/em++
CC=emcc
CXX=em++
OPTFLAGS=-O2
CFLAGS=$(OPTFLAGS) -g -I$(PYTHONINCLUDE) -Wno-warn-absolute-paths -Werror=int-conversion -Werror=incompatible-pointer-types -fPIC
CFLAGS=$(OPTFLAGS) -g -I$(PYTHONINCLUDE) -fPIC \
-Wno-warn-absolute-paths -Werror=int-conversion -Werror=incompatible-pointer-types

LDFLAGS=\
-O2 \
Expand Down
2 changes: 1 addition & 1 deletion src/core/error_handling.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ error_handling_init()
Module.handle_js_error = function(e)
{
let err = Module.hiwire.new_value(e);
PyodideErr_SetJsError(err);
_PyodideErr_SetJsError(err);
Module.hiwire.decref(err);
};
});
Expand Down
33 changes: 25 additions & 8 deletions src/core/error_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,45 @@ log_error(char* msg);
*/

// clang-format off
#ifdef DEBUG_F
// Yes, the "do {} while(0)" trick solves the same problem in the same way in
// javascript!
#define LOG_EM_JS_ERROR(__funcname__, err) \
do { \
console.error( \
`EM_JS raised exception on line __LINE__ in func __funcname__`); \
console.error("Error was:", err); \
} while (0)
#else
#define LOG_EM_JS_ERROR(__funcname__, err)
#endif

// Need an extra layer to expand LOG_EM_JS_ERROR.
#define EM_JS_DEFER(ret, func_name, args, body...) \
EM_JS(ret, func_name, args, body)

#define EM_JS_REF(ret, func_name, args, body...) \
EM_JS(ret, func_name, args, { \
EM_JS_DEFER(ret, func_name, args, { \
"use strict"; \
try /* intentionally no braces, body already has them */ \
body /* <== body of func */ \
catch (e) { \
/* Dummied out until calling code is ready to catch these errors */ \
throw e; \
LOG_EM_JS_ERROR(func_name, err); \
Module.handle_js_error(e); \
return 0; \
} \
throw new Error("Assertion error: control reached end of function without return");\
throw new Error( \
"Assertion error: control reached end of function without return" \
); \
})

#define EM_JS_NUM(ret, func_name, args, body...) \
EM_JS(ret, func_name, args, { \
EM_JS_DEFER(ret, func_name, args, { \
"use strict"; \
try /* intentionally no braces, body already has them */ \
body /* <== body of func */ \
catch (e) { \
/* Dummied out until calling code is ready to catch these errors */ \
throw e; \
LOG_EM_JS_ERROR(func_name, err); \
Module.handle_js_error(e); \
return -1; \
} \
Expand Down Expand Up @@ -98,7 +115,7 @@ log_error(char* msg);
__FILE__); \
log_error(msg); \
free(msg); \
goto finally \
goto finally; \
} while (0)

#else
Expand Down
2 changes: 1 addition & 1 deletion src/core/hiwire.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ EM_JS_REF(JsRef, hiwire_float64array, (f64 * ptr, int len), {
return Module.hiwire.new_value(array);
})

EM_JS_NUM(errcode, hiwire_throw_error, (JsRef idmsg), {
EM_JS(void, hiwire_throw_error, (JsRef idmsg), {
let jsmsg = Module.hiwire.get_value(idmsg);
Module.hiwire.decref(idmsg);
throw new Error(jsmsg);
Expand Down
2 changes: 1 addition & 1 deletion src/core/hiwire.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ hiwire_push_object_pair(JsRef idobj, JsRef idkey, JsRef idval);
* The message is conventionally a Javascript string, but that is not required.
* TODO: should be hiwire_set_error.
*/
errcode
void
hiwire_throw_error(JsRef idmsg);

/**
Expand Down

0 comments on commit f6a67d9

Please sign in to comment.