Skip to content

Commit

Permalink
Merge pull request #2 from andrjohns/error-messages
Browse files Browse the repository at this point in the history
Output JS runtime error messages
  • Loading branch information
andrjohns authored Sep 12, 2023
2 parents d45d0c9 + 814d431 commit 2784c3f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: QuickJSR
Title: Interface for the 'QuickJS' Lightweight 'JavaScript' Engine
Version: 1.0.5
Version: 1.0.6
Authors@R: c(
person(given = c("Andrew", "R."), family = "Johnson", role = c("aut", "cre"),
email = "[email protected]",
Expand Down
2 changes: 1 addition & 1 deletion R/JSContext.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ JSContext <- R6::R6Class(

eval_success <- qjs_source(private$context_, code_string)
if (!eval_success) {
stop("Evaluating JS code failed!", call. = FALSE)
stop("Evaluating JS code failed, see message above!", call. = FALSE)
}
invisible(NULL)
},
Expand Down
11 changes: 2 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@ args_to_json <- function(...) {
}

parse_return <- function(qjs_return) {
error_strings = c(
"Error in JSON.stringify()!",
"Error initialising function!",
"Error calling function!",
"Error in evaluation!"
)

if (qjs_return %in% error_strings) {
stop(qjs_return, call. = FALSE)
if (qjs_return == "Error!") {
stop("Error in JS runtime, see error message above for more information!", call. = FALSE)
}
jsonlite::fromJSON(qjs_return)
}
38 changes: 23 additions & 15 deletions src/qjs_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@

bool qjs_source_impl(JSContext* ctx, const char* code_string) {
JSValue val = JS_Eval(ctx, code_string, strlen(code_string), "", 0);
bool succeeded = !(JS_IsException(val) || JS_IsError(ctx, val));
bool failed = JS_IsException(val);
if (failed) {
js_std_dump_error(ctx);
}
JS_FreeValue(ctx, val);

return succeeded;
return !failed;
}

bool qjs_validate_impl(JSContext* ctx, const char* function_name) {
JSValue global = JS_GetGlobalObject(ctx);
JSValue val = JS_GetPropertyStr(ctx, global, function_name);

bool succeeded = !(JS_IsException(val) || JS_IsError(ctx, val));
bool failed = JS_IsException(val);
if (failed) {
js_std_dump_error(ctx);
}
JS_FreeValue(ctx, val);
JS_FreeValue(ctx, global);

return succeeded;
return !failed;
}

const char* JS_ValToJSON(JSContext* ctx, JSValue* val) {
Expand All @@ -28,8 +34,9 @@ const char* JS_ValToJSON(JSContext* ctx, JSValue* val) {

JSValue result_js = JS_Call(ctx, stringify, global, 1, val);
const char* result;
if (JS_IsException(result_js) || JS_IsError(ctx, result_js)) {
result = "Error in JSON.stringify()!";
if (JS_IsException(result_js)) {
js_std_dump_error(ctx);
result = "Error!";
} else {
result = JS_ToCString(ctx, result_js);
}
Expand All @@ -45,10 +52,11 @@ const char* JS_ValToJSON(JSContext* ctx, JSValue* val) {
const char* qjs_call_impl(JSContext* ctx, const char* wrapped_name,
const char* call_wrapper, const char* args_json) {
JSValue tmp = JS_Eval(ctx, call_wrapper, strlen(call_wrapper), "", 0);
bool failed = (JS_IsException(tmp) || JS_IsError(ctx, tmp));
bool failed = JS_IsException(tmp);
JS_FreeValue(ctx, tmp);
if (failed) {
return "Error initialising function!";
js_std_dump_error(ctx);
return "Error!";
}

JSValue global = JS_GetGlobalObject(ctx);
Expand All @@ -57,11 +65,11 @@ const char* qjs_call_impl(JSContext* ctx, const char* wrapped_name,
JS_NewString(ctx, args_json)
};

const char* result;
JSValue result_js = JS_Call(ctx, function_wrapper, global, 1, args);
failed = (JS_IsException(result_js) || JS_IsError(ctx, result_js));
if (failed) {
result = "Error calling function!";
const char* result;
if (JS_IsException(result_js)) {
js_std_dump_error(ctx);
result = "Error!";
} else {
result = JS_ValToJSON(ctx, &result_js);
}
Expand All @@ -79,10 +87,10 @@ const char* qjs_eval_impl(const char* eval_string) {
JSContext* ctx = JS_NewContext(rt);

JSValue val = JS_Eval(ctx, eval_string, strlen(eval_string), "", 0);
bool failed = (JS_IsException(val) || JS_IsError(ctx, val));
const char* result;
if (failed) {
result = "Error in evaluation!";
if (JS_IsException(val)) {
js_std_dump_error(ctx);
result = "Error!";
} else {
result = JS_ValToJSON(ctx, &val);
}
Expand Down

0 comments on commit 2784c3f

Please sign in to comment.