-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from andrjohns/js-to-r-init
Initial implementations for passing JS variables to R
- Loading branch information
Showing
4 changed files
with
119 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#ifndef QUICKJSR_JSVALUE_TO_CPP_HPP | ||
#define QUICKJSR_JSVALUE_TO_CPP_HPP | ||
|
||
#include <quickjsr/type_traits.hpp> | ||
#include <type_traits> | ||
#include <string> | ||
#include <vector> | ||
#include <quickjs-libc.h> | ||
|
||
namespace quickjsr { | ||
template <typename T, std::enable_if_t<std::is_same<T, double>::value>* = nullptr> | ||
T JSValue_to_Cpp(JSContext* ctx, JSValue val) { | ||
double res; | ||
JS_ToFloat64(ctx, &res, val); | ||
return res; | ||
} | ||
|
||
template <typename T, std::enable_if_t<std::is_same<T, int32_t>::value>* = nullptr> | ||
T JSValue_to_Cpp(JSContext* ctx, JSValue val) { | ||
int32_t res; | ||
JS_ToInt32(ctx, &res, val); | ||
return res; | ||
} | ||
|
||
template <typename T, std::enable_if_t<std::is_same<T, bool>::value>* = nullptr> | ||
T JSValue_to_Cpp(JSContext* ctx, JSValue val) { | ||
return static_cast<bool>(JS_ToBool(ctx, val)); | ||
} | ||
|
||
template <typename T, std::enable_if_t<std::is_same<T, std::string>::value>* = nullptr> | ||
T JSValue_to_Cpp(JSContext* ctx, JSValue val) { | ||
return JS_ToCString(ctx, val); | ||
} | ||
|
||
template <typename T, std::enable_if_t<is_std_vector<T>::value>* = nullptr> | ||
T JSValue_to_Cpp(JSContext* ctx, JSValue val) { | ||
T res; | ||
uint32_t len; | ||
JSValue arr_len = JS_GetPropertyStr(ctx, val, "length"); | ||
JS_ToUint32(ctx, &len, arr_len); | ||
JS_FreeValue(ctx, arr_len); | ||
for (uint32_t i = 0; i < len; i++) { | ||
JSValue elem = JS_GetPropertyUint32(ctx, val, i); | ||
res.push_back(JSValue_to_Cpp<value_type_t<T>>(ctx, elem)); | ||
JS_FreeValue(ctx, elem); | ||
} | ||
return res; | ||
} | ||
|
||
} // namespace quickjsr | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef QUICKJSR_TYPE_TRAITS_HPP | ||
#define QUICKJSR_TYPE_TRAITS_HPP | ||
|
||
#include <type_traits> | ||
#include <vector> | ||
|
||
namespace quickjsr { | ||
template <typename T> | ||
struct is_std_vector : std::false_type {}; | ||
template <typename T> | ||
struct is_std_vector<std::vector<T>> : std::true_type {}; | ||
|
||
template <typename T, typename = void> | ||
struct value_type { | ||
using type = typename std::decay_t<T>; | ||
}; | ||
|
||
template <typename T> | ||
struct value_type<T, std::enable_if_t<is_std_vector<T>::value>> { | ||
using type = typename std::decay_t<T>::value_type; | ||
}; | ||
|
||
template <typename T> | ||
using value_type_t = typename value_type<T>::type; | ||
} // namespace quickjsr | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters