You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm developing a scripting-capable utility. I now use successfully wren-lang with wrenbind17 but I would like to try QuickJS.
However, I was unable to define a generic (variadic) datatype that allows me to call C++ functions from JS with compound objects such as Lists/Arrays or Map. Is it possible to do such a thing?
namespacejs {
structList {
// ??? DATAtemplate <typename T> T get(constsize_t ix) const;
size_tsize() const;
};
structMap {
// ??? DATAtemplate <typename K, typename V> V get(const K key) const;
size_tsize() const;
};
using Variant = std::variant<std::nullptr_t, bool, int, double, std::string, js::List, js::Map>;
structany : publicVariant {
using Variant::variant;
using Variant::operator=;
enumclassType : int {
null_t = 0, bool_t, int_t, double_t, string_t, list_t, map_t
};
any(const Variant& p) : Variant(p) {}
Type type() const { return (Type)this->index(); }
template <classT> boolis() const {
const Type t = type();
ifconstexpr (std::is_same<T, std::nullptr_t>::value)
return t == Type::null_t;
ifconstexpr (std::is_same<T, bool>::value)
return t == Type::bool_t;
ifconstexpr (std::is_same<T, int>::value)
return t == Type::int_t;
ifconstexpr (std::is_same<T, double>::value)
return t == Type::double_t;
ifconstexpr (std::is_same<T, std::string>::value)
return t == Type::string_t;
ifconstexpr (std::is_same<T, js::List>::value)
return t == Type::list_t;
ifconstexpr (std::is_same<T, js::Map>::value)
return t == Type::map_t;
returnfalse;
}
template <classT> T as() const { return std::get<T>(*this); }
};
}
So js::List is just a list/array of js::any and js::Map is a map of <std::string, js::any> .
I'd like to do something like this:
/* C++ function definition*/voidf(std::string s, js::any a){
if(a.is<js::Map>()){
// Code for accessing map elements for example convert to nlohmann_json
} elseif(a.is<js::List>()){
auto v = a.as<js::list>().get(0); //v is also a js::any.
} else { ... }
}
/* JS function call*/module.f("~/example.dat",{"parms" : {"hello":"HI!"},"array" : [false,"str1",true,"str2"],"id" : 38,});
Is it possible to do this in quickjspp?
The text was updated successfully, but these errors were encountered:
Hi, I'm developing a scripting-capable utility. I now use successfully wren-lang with wrenbind17 but I would like to try QuickJS.
However, I was unable to define a generic (variadic) datatype that allows me to call C++ functions from JS with compound objects such as Lists/Arrays or Map. Is it possible to do such a thing?
So
js::List
is just a list/array ofjs::any
andjs::Map
is a map of<std::string, js::any>
.I'd like to do something like this:
Is it possible to do this in quickjspp?
The text was updated successfully, but these errors were encountered: