mirror of
https://github.com/matrix-construct/construct
synced 2024-09-30 04:38:52 +02:00
ircd::js: Disambiguate and improve value/object conversions.
This commit is contained in:
parent
04c6b67649
commit
7f12f1cce5
2 changed files with 17 additions and 21 deletions
|
@ -42,7 +42,6 @@ struct object
|
||||||
object(const object::handle_mutable &h): object{h.get()} {}
|
object(const object::handle_mutable &h): object{h.get()} {}
|
||||||
object(const object::handle &h): object{h.get()} {}
|
object(const object::handle &h): object{h.get()} {}
|
||||||
explicit object(const value &);
|
explicit object(const value &);
|
||||||
object(JSFunction *const &);
|
|
||||||
object(JSObject *const &);
|
object(JSObject *const &);
|
||||||
object(JSObject &);
|
object(JSObject &);
|
||||||
object();
|
object();
|
||||||
|
@ -100,18 +99,6 @@ object::object(const value &val)
|
||||||
throw type_error("Value is not an Object");
|
throw type_error("Value is not an Object");
|
||||||
}
|
}
|
||||||
|
|
||||||
inline
|
|
||||||
object::object(JSFunction *const &val)
|
|
||||||
:JS::Rooted<JSObject *>
|
|
||||||
{
|
|
||||||
*cx,
|
|
||||||
val? JS_GetFunctionObject(val) : nullptr
|
|
||||||
}
|
|
||||||
{
|
|
||||||
if(unlikely(!get()))
|
|
||||||
throw type_error("Function cannot convert to Object");
|
|
||||||
}
|
|
||||||
|
|
||||||
inline
|
inline
|
||||||
object::object(const JSClass *const &clasp)
|
object::object(const JSClass *const &clasp)
|
||||||
:JS::Rooted<JSObject *>
|
:JS::Rooted<JSObject *>
|
||||||
|
|
|
@ -37,8 +37,6 @@ struct value
|
||||||
using handle = JS::HandleValue;
|
using handle = JS::HandleValue;
|
||||||
using handle_mutable = JS::MutableHandleValue;
|
using handle_mutable = JS::MutableHandleValue;
|
||||||
|
|
||||||
explicit operator JSType() const;
|
|
||||||
|
|
||||||
explicit operator std::string() const;
|
explicit operator std::string() const;
|
||||||
explicit operator double() const;
|
explicit operator double() const;
|
||||||
explicit operator uint64_t() const;
|
explicit operator uint64_t() const;
|
||||||
|
@ -48,8 +46,8 @@ struct value
|
||||||
explicit operator uint16_t() const;
|
explicit operator uint16_t() const;
|
||||||
explicit operator bool() const;
|
explicit operator bool() const;
|
||||||
|
|
||||||
value(const char *const &);
|
|
||||||
explicit value(const std::string &);
|
explicit value(const std::string &);
|
||||||
|
value(const char *const &);
|
||||||
value(const nullptr_t &);
|
value(const nullptr_t &);
|
||||||
value(const double &);
|
value(const double &);
|
||||||
value(const float &);
|
value(const float &);
|
||||||
|
@ -58,6 +56,7 @@ struct value
|
||||||
|
|
||||||
value(const jsid &);
|
value(const jsid &);
|
||||||
value(JSObject &);
|
value(JSObject &);
|
||||||
|
value(JSObject *const &);
|
||||||
value(JSString *const &);
|
value(JSString *const &);
|
||||||
value(JSFunction *const &);
|
value(JSFunction *const &);
|
||||||
value(JS::Symbol *const &);
|
value(JS::Symbol *const &);
|
||||||
|
@ -73,6 +72,7 @@ struct value
|
||||||
value(const value &) = delete;
|
value(const value &) = delete;
|
||||||
value &operator=(value &&) noexcept;
|
value &operator=(value &&) noexcept;
|
||||||
|
|
||||||
|
friend JSType type(const value &);
|
||||||
friend bool undefined(const value &);
|
friend bool undefined(const value &);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -135,6 +135,16 @@ value::value(JS::Symbol *const &val)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline
|
||||||
|
value::value(JSObject *const &val)
|
||||||
|
:JS::Rooted<JS::Value>
|
||||||
|
{
|
||||||
|
*cx,
|
||||||
|
val? JS::ObjectValue(*val) : throw internal_error("NULL JSObject")
|
||||||
|
}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
value::value(JSObject &val)
|
value::value(JSObject &val)
|
||||||
:JS::Rooted<JS::Value>{*cx, JS::ObjectValue(val)}
|
:JS::Rooted<JS::Value>{*cx, JS::ObjectValue(val)}
|
||||||
|
@ -297,17 +307,16 @@ const
|
||||||
return s? native(s) : throw type_error("Failed to cast to string");
|
return s? native(s) : throw type_error("Failed to cast to string");
|
||||||
}
|
}
|
||||||
|
|
||||||
inline
|
inline JSType
|
||||||
value::operator JSType()
|
type(const value &val)
|
||||||
const
|
|
||||||
{
|
{
|
||||||
return JS_TypeOfValue(*cx, *this);
|
return JS_TypeOfValue(*cx, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool
|
inline bool
|
||||||
undefined(const value &val)
|
undefined(const value &val)
|
||||||
{
|
{
|
||||||
return JSType(val) == JSTYPE_VOID;
|
return type(val) == JSTYPE_VOID;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace js
|
} // namespace js
|
||||||
|
|
Loading…
Reference in a new issue