Skip to content

Commit

Permalink
Update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed Oct 15, 2023
1 parent 05fefd1 commit f291e9c
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 45 deletions.
5 changes: 3 additions & 2 deletions R/JSContext.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ JSContext <- R6::R6Class(
warning("Both a filepath and code string cannot be provided,",
" code will be ignored!", call. = FALSE)
}
eval_success <- qjs_source_file(private$context_, file)
code_string <- paste0(readLines(con = file), collapse = "\n")
} else if (!is.null(code)) {
eval_success <- qjs_source(private$context_, code)
code_string <- code
} else {
stop("No JS code provided!", call. = FALSE)
}
eval_success <- qjs_source(private$context_, code_string)
if (!eval_success) {
stop("Evaluating JS code failed, see message above!", call. = FALSE)
}
Expand Down
4 changes: 0 additions & 4 deletions R/qjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ qjs_source <- function(ctx_ptr, code_string) {
.Call(`qjs_source_`, ctx_ptr, code_string)
}

qjs_source_file <- function(ctx_ptr, filename) {
.Call(`qjs_source_file_`, ctx_ptr, filename)
}

qjs_call <- function(ctx_ptr, function_name, args_json) {
parse_return(.Call(`qjs_call_`, ctx_ptr, function_name, args_json))
}
Expand Down
13 changes: 7 additions & 6 deletions man/JSContext.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ extern "C" {

SEXP qjs_context_(SEXP stack_size_);
SEXP qjs_source_(SEXP ctx_ptr_, SEXP code_string_);
SEXP qjs_source_file_(SEXP ctx_ptr_, SEXP filename_);
SEXP qjs_validate_(SEXP ctx_ptr_, SEXP function_name_);
SEXP qjs_call_(SEXP ctx_ptr_, SEXP function_name_, SEXP args_json_);
SEXP qjs_eval_(SEXP eval_string_);
Expand All @@ -28,7 +27,6 @@ SEXP qjs_eval_(SEXP eval_string_);
static const R_CallMethodDef CallEntries[] = {
CALLDEF(qjs_context_, 1),
CALLDEF(qjs_source_, 2),
CALLDEF(qjs_source_file_, 2),
CALLDEF(qjs_validate_, 2),
CALLDEF(qjs_call_, 3),
CALLDEF(qjs_eval_, 1),
Expand Down
9 changes: 0 additions & 9 deletions src/qjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ extern "C" {
void js_std_add_helpers(JSContext *ctx, int argc, char **argv);

bool qjs_source_impl(JSContext* ctx, const char* code_string);
bool qjs_source_file_impl(JSContext* ctx, const char* code_string);
bool qjs_validate_impl(JSContext* ctx, const char* function_name);
const char* qjs_call_impl(JSContext* ctx, const char* wrapped_name,
const char* call_wrapper, const char* args_json);
Expand Down Expand Up @@ -52,14 +51,6 @@ RcppExport SEXP qjs_context_(SEXP stack_size_) {
);
}

RcppExport SEXP qjs_source_file_(SEXP ctx_ptr_, SEXP filename_) {
JSContext* ctx = ContextXPtr(ctx_ptr_);
const char* filename = Rcpp::as<const char*>(filename_);
bool succeeded = qjs_source_file_impl(ctx, filename);

return Rcpp::wrap(succeeded);
}

RcppExport SEXP qjs_source_(SEXP ctx_ptr_, SEXP code_string_) {
JSContext* ctx = ContextXPtr(ctx_ptr_);
const char* code_string = Rcpp::as<const char*>(code_string_);
Expand Down
22 changes: 0 additions & 22 deletions src/qjs_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,6 @@
#include <stdbool.h>
#include <string.h>

bool qjs_source_file_impl(JSContext* ctx, const char* filename) {
uint8_t *buf;
size_t buf_len;
buf = js_load_file(ctx, &buf_len, filename);
if (!buf) {
JS_ThrowReferenceError(ctx, "could not load '%s'", filename);
js_std_dump_error(ctx);
js_free(ctx, buf);
return false;
}
JSValue val = JS_Eval(ctx, (char *)buf, buf_len, filename,
JS_EVAL_TYPE_GLOBAL);
js_free(ctx, buf);
bool failed = JS_IsException(val);
if (failed) {
js_std_dump_error(ctx);
}
JS_FreeValue(ctx, val);

return !failed;
}

bool qjs_source_impl(JSContext* ctx, const char* code_string) {
JSValue val = JS_Eval(ctx, code_string, strlen(code_string), "", 0);
bool failed = JS_IsException(val);
Expand Down

0 comments on commit f291e9c

Please sign in to comment.