Skip to content

Commit

Permalink
Testing quickjs-ng as base (#57)
Browse files Browse the repository at this point in the history
* Migrate to QJS-ng base

* Pedantic branch

* More compat

* More compat

* Disable R36, fix R API NOTE
  • Loading branch information
andrjohns committed Jun 22, 2024
1 parent a7e1b1b commit 75b3f89
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel'}
- {os: ubuntu-latest, r: '3.6'}
# - {os: ubuntu-latest, r: '3.6'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "src/quickjs"]
path = src/quickjs
url = https://github.com/andrjohns/quickjs
branch = wpedantic-compat
url = https://github.com/andrjohns/quickjs-ng
branch = v0.5.0-pedantic
8 changes: 8 additions & 0 deletions R/qjs.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,11 @@ to_json <- function(arg, auto_unbox = FALSE) {
from_json <- function(json) {
.Call(`from_json_`, json)
}

#' Get the version of the bundled QuickJS library
#'
#' @return Character string of the version of the bundled QuickJS library
#' @export
quickjs_version <- function() {
.Call(`qjs_version_`)
}
9 changes: 0 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
#' Get the version of the bundled QuickJS library
#'
#' @return Character string of the version of the bundled QuickJS library
#' @export
quickjs_version <- function() {
version_file <- system.file("VERSION", package = "QuickJSR", mustWork = TRUE)
readLines(version_file)
}

get_tz_offset_seconds <- function() {
as.POSIXlt(Sys.time())$gmtoff
}
5 changes: 2 additions & 3 deletions inst/include/cpp11/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class environment {
safe[Rf_defineVar](name_, as_sexp(value), parent_);
return *this;
}
operator SEXP() const { return safe[Rf_findVarInFrame3](parent_, name_, TRUE); };
operator SEXP() const { return safe[Rf_findVarInFrame](parent_, name_); };
operator sexp() const { return SEXP(); };
};

Expand All @@ -48,8 +48,7 @@ class environment {
proxy operator[](const std::string& name) const { return operator[](name.c_str()); }

bool exists(SEXP name) const {
SEXP res = safe[Rf_findVarInFrame3](env_, name, FALSE);
return res != R_UnboundValue;
return safe[R_existsVarInFrame](env_, name);
}
bool exists(const char* name) const { return exists(safe[Rf_install](name)); }

Expand Down
5 changes: 0 additions & 5 deletions inst/include/quickjs_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ namespace quickjsr {
return NULL;
}

JS_AddIntrinsicBigFloat(ctx);
JS_AddIntrinsicBigDecimal(ctx);
JS_AddIntrinsicOperators(ctx);
JS_EnableBignumExt(ctx, TRUE);

/* system modules */
js_init_module_std(ctx, "std");
js_init_module_os(ctx, "os");
Expand Down
20 changes: 0 additions & 20 deletions inst/include/quickjsr/JSValue_Date.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,6 @@ inline bool JS_IsDate(JSContext* ctx, const JSValue& val) {
return isDate;
}

inline JSValue JS_NewDate(JSContext* ctx, double timestamp, bool posixct = false) {
static constexpr double milliseconds_day = 86400000;
static constexpr double milliseconds_second = 1000;
double tz_offset_seconds = get_tz_offset_seconds();
JSValue global_obj = JS_GetGlobalObject(ctx);
JSValue date_ctor = JS_GetPropertyStr(ctx, global_obj, "Date");
JSValue timestamp_val;
if (posixct) {
timestamp_val = JS_NewFloat64(ctx, (timestamp + tz_offset_seconds) * milliseconds_second);
} else {
timestamp_val = JS_NewFloat64(ctx, timestamp * milliseconds_day);
}
JSValue date = JS_CallConstructor(ctx, date_ctor, 1, &timestamp_val);

JS_FreeValue(ctx, global_obj);
JS_FreeValue(ctx, date_ctor);
JS_FreeValue(ctx, timestamp_val);
return date;
}

inline JSValue JS_GetTime(JSContext* ctx, const JSValue& val) {
JSAtom timeAtom = JS_NewAtom(ctx, "getTime");
JSValue timeVal = JS_Invoke(ctx, val, timeAtom, 0, NULL);
Expand Down
7 changes: 5 additions & 2 deletions inst/include/quickjsr/SEXP_to_JSValue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,12 @@ namespace quickjsr {
}
case REALSXP: {
if (Rf_inherits(x, "POSIXct")) {
return JS_NewDate(ctx, REAL_ELT(x, index), true);
static constexpr double milliseconds_second = 1000;
double tz_offset_seconds = get_tz_offset_seconds();
return JS_NewDate(ctx, (REAL_ELT(x, index) + tz_offset_seconds) * milliseconds_second);
} else if (Rf_inherits(x, "Date")) {
return JS_NewDate(ctx, REAL_ELT(x, index));
static constexpr double milliseconds_day = 86400000;
return JS_NewDate(ctx, REAL_ELT(x, index) * milliseconds_day);
} else {
return JS_NewFloat64(ctx, REAL_ELT(x, index));
}
Expand Down
4 changes: 1 addition & 3 deletions src/Makevars
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
PKG_CPPFLAGS = -I"../inst/include/" -I"../inst/include/quickjs"
PKG_CPPFLAGS += -D_GNU_SOURCE -DCONFIG_BIGNUM
PKG_CPPFLAGS += -DCONFIG_VERSION=\"$(shell cat quickjs/VERSION)\"
PKG_LIBS = ../inst/lib/$(R_ARCH)/libquickjs.a

ifeq ($(OS),Windows_NT)
Expand Down Expand Up @@ -48,7 +47,7 @@ endif
QUICKJS_C_FILES = cutils.c libbf.c libregexp.c libunicode.c quickjs.c quickjs-libc.c
QUICKJS_C_HEADERS = $(QUICKJS_C_FILES:.c=.h) \
libregexp-opcode.h libunicode-table.h list.h \
quickjs-atom.h quickjs-opcode.h
quickjs-atom.h quickjs-opcode.h quickjs-c-atomics.h

QUICKJS_SOURCES = $(QUICKJS_C_FILES:%=quickjs/%)
QUICKJS_OBJECTS = $(QUICKJS_SOURCES:.c=.o)
Expand All @@ -65,7 +64,6 @@ $(SHLIB): build-static
package-quickjs:
@mkdir -p ../inst/include/quickjs
@cp $(QUICKJS_C_HEADERS:%=quickjs/%) ../inst/include/quickjs
@cp quickjs/VERSION ../inst/

build-static: $(QUICKJS_OBJECTS)
@mkdir -p ../inst/lib/$(R_ARCH)
Expand Down
2 changes: 2 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern "C" {
SEXP qjs_eval_(SEXP eval_string_);
SEXP to_json_(SEXP arg_, SEXP auto_unbox_);
SEXP from_json_(SEXP json_);
SEXP qjs_version_();


static const R_CallMethodDef CallEntries[] = {
Expand All @@ -26,6 +27,7 @@ extern "C" {
{"qjs_assign_", (DL_FUNC) &qjs_assign_, 3},
{"to_json_", (DL_FUNC) &to_json_, 2},
{"from_json_", (DL_FUNC) &from_json_, 1},
{"qjs_version_", (DL_FUNC) &qjs_version_, 0},
{NULL, NULL, 0}
};

Expand Down
2 changes: 1 addition & 1 deletion src/quickjs
6 changes: 6 additions & 0 deletions src/quickjsr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,9 @@ extern "C" SEXP from_json_(SEXP json_) {
return rtn;
END_CPP11
}

extern "C" SEXP qjs_version_() {
BEGIN_CPP11
return cpp11::as_sexp(JS_GetVersion());
END_CPP11
}

0 comments on commit 75b3f89

Please sign in to comment.