Skip to content

Commit

Permalink
Align validate API with V8
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed Oct 15, 2023
1 parent 22fd62f commit 05fefd1
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 21 deletions.
15 changes: 8 additions & 7 deletions R/JSContext.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ JSContext <- R6::R6Class(
#' evaluation context
#'
#' @param stack_size An optional fixed value for the stack size (in bytes)
#' @param disable_stack_size_check Disable fixed/automatic stack size allocation.
#' @return No return value, used internally to initialise the JSContext object
initialize = function(stack_size = NULL) {
initialize = function(stack_size = NULL, disable_stack_size_check = TRUE) {
stack_size_int = ifelse(is.null(stack_size), -1, stack_size)
stack_size_int = ifelse(disable_stack_size_check, 0, stack_size_int)
rt_and_ctx = qjs_context(stack_size_int)
private$runtime_ = rt_and_ctx$runtime_ptr
private$context_ = rt_and_ctx$context_ptr
},
#' @description
#' Checks whether a specified function or object is present in the initialised
#' context.
#' Checks whether JS code string is valid code in the current context
#'
#' @param function_name The name of the function to check
#' @return A boolean indicating whether the function is present
validate = function(function_name) {
qjs_validate(private$context_, function_name)
#' @param code_string The JS code to check
#' @return A boolean indicating whether code is valid
validate = function(code_string) {
qjs_validate(private$context_, code_string)
},
#' @description
#' Evaluate a provided JavaScript file or string within the initialised context.
Expand Down
2 changes: 1 addition & 1 deletion R/qjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ qjs_call <- function(ctx_ptr, function_name, args_json) {
}

qjs_validate <- function(ctx_ptr, function_name) {
.Call(`qjs_source_`, ctx_ptr, function_name)
.Call(`qjs_validate_`, ctx_ptr, function_name)
}
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/Makevars
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ ifeq ($(shell uname -p), powerpc)
endif
endif

QUICKJS_OBJECTS = quickjs/cutils.o quickjs/libbf.o quickjs/libregexp.o quickjs/libunicode.o \
quickjs/quickjs-libc.o quickjs/quickjs.o quickjs/unicode_gen.o
QUICKJS_SOURCES = $(wildcard quickjs/*.c)
QUICKJS_OBJECTS = $(QUICKJS_SOURCES:.c=.o)

$(SHLIB): ../inst/lib/$(R_ARCH)/libquickjs.a

Expand Down
6 changes: 3 additions & 3 deletions src/qjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ RcppExport SEXP qjs_source_(SEXP ctx_ptr_, SEXP code_string_) {
return Rcpp::wrap(succeeded);
}

RcppExport SEXP qjs_validate_(SEXP ctx_ptr_, SEXP function_name_) {
RcppExport SEXP qjs_validate_(SEXP ctx_ptr_, SEXP code_string_) {
JSContext* ctx = ContextXPtr(ctx_ptr_);
const char* function_name = Rcpp::as<const char*>(function_name_);
bool succeeded = qjs_validate_impl(ctx, function_name);
const char* code_string = Rcpp::as<const char*>(code_string_);
bool succeeded = qjs_validate_impl(ctx, code_string);

return Rcpp::wrap(succeeded);
}
Expand Down
10 changes: 2 additions & 8 deletions src/qjs_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,10 @@ bool qjs_source_impl(JSContext* ctx, const char* code_string) {
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 qjs_validate_impl(JSContext* ctx, const char* code_string) {
JSValue val = JS_Eval(ctx, code_string, strlen(code_string), "", JS_EVAL_FLAG_COMPILE_ONLY);
bool failed = JS_IsException(val);
if (failed) {
js_std_dump_error(ctx);
}
JS_FreeValue(ctx, val);
JS_FreeValue(ctx, global);

return !failed;
}
Expand Down

0 comments on commit 05fefd1

Please sign in to comment.