From 21e96eaedfa1261b3c61a228ee660fc0d98f2f2c Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 26 Oct 2016 10:10:00 -0700 Subject: [PATCH] ircd::js: id creation constructors and comparison. --- include/ircd/js/id.h | 89 +++++++++++++++++++++++++++++++++----------- 1 file changed, 67 insertions(+), 22 deletions(-) diff --git a/include/ircd/js/id.h b/include/ircd/js/id.h index e0a97e4c3..ce1023766 100644 --- a/include/ircd/js/id.h +++ b/include/ircd/js/id.h @@ -31,16 +31,23 @@ struct id using handle = JS::HandleId; using handle_mutable = JS::MutableHandleId; + explicit id(const char *const &); // creates new id (permanent) + explicit id(const std::string &); // creates new id (permanent) + id(const string::handle &); + id(const value::handle &); id(const JSProtoKey &); id(const uint32_t &); + id(const handle_mutable &); + id(const handle &); id(const jsid &); - id(const JS::HandleString &); - id(const JS::HandleValue &); - id(const JS::MutableHandleId &); - id(const JS::HandleId &); id(); id(id &&) noexcept; id(const id &) = delete; + + friend bool operator==(const id &, const char *const &); + friend bool operator==(const id &, const std::string &); + friend bool operator==(const char *const &, const id &); + friend bool operator==(const std::string &, const id &); }; inline @@ -63,33 +70,17 @@ id::id(const jsid &i) } inline -id::id(const JS::HandleId &h) +id::id(const handle &h) :JS::Rooted{*cx, h} { } inline -id::id(const JS::MutableHandleId &h) +id::id(const handle_mutable &h) :JS::Rooted{*cx, h} { } -inline -id::id(const JS::HandleValue &h) -:JS::Rooted{*cx} -{ - if(!JS_ValueToId(*cx, h, &(*this))) - throw type_error("Failed to construct id from Value"); -} - -inline -id::id(const JS::HandleString &h) -:JS::Rooted{*cx} -{ - if(!JS_StringToId(*cx, h, &(*this))) - throw type_error("Failed to construct id from String"); -} - inline id::id(const uint32_t &index) :JS::Rooted{*cx} @@ -105,5 +96,59 @@ id::id(const JSProtoKey &key) JS::ProtoKeyToId(*cx, key, &(*this)); } +inline +id::id(const value::handle &h) +:JS::Rooted{*cx} +{ + if(!JS_ValueToId(*cx, h, &(*this))) + throw type_error("Failed to construct id from Value"); +} + +inline +id::id(const string::handle &h) +:JS::Rooted{*cx} +{ + if(!JS_StringToId(*cx, h, &(*this))) + throw type_error("Failed to construct id from String"); +} + +inline +id::id(const std::string &str) +:id(str.c_str()) +{ +} + +inline +id::id(const char *const &str) +:JS::Rooted{*cx, jsid()} +{ + if(!JS::PropertySpecNameToPermanentId(*cx, str, address())) + throw type_error("Failed to create id from native string"); +} + +inline bool +operator==(const std::string &a, const id &b) +{ + return operator==(a.c_str(), b); +} + +inline bool +operator==(const char *const &a, const id &b) +{ + return JS::PropertySpecNameEqualsId(a, b); +} + +inline bool +operator==(const id &a, const std::string &b) +{ + return operator==(a, b.c_str()); +} + +inline bool +operator==(const id &a, const char *const &b) +{ + return JS::PropertySpecNameEqualsId(b, a); +} + } // namespace js } // namespace ircd