0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-03-17 06:50:23 +01:00

ircd::js: Reflect JSType + value type tests.

This commit is contained in:
Jason Volk 2016-10-23 22:40:45 -07:00
parent 1ae9f4ffa4
commit b69444138f
4 changed files with 46 additions and 2 deletions

View file

@ -25,6 +25,7 @@
namespace ircd {
namespace js {
const char *reflect(const JSType &);
const char *reflect(const JSExnType &);
const char *reflect(const JSGCStatus &);
const char *reflect(const JSGCParamKey &);

View file

@ -59,6 +59,7 @@ inline JSVersion version(const char *const &v) { return JS_StringToVersion(v);
#include "timer.h"
#include "context.h"
#include "compartment.h"
#include "debug.h"
#include "error.h"
#include "value.h"
#include "string.h"
@ -72,5 +73,4 @@ inline JSVersion version(const char *const &v) { return JS_StringToVersion(v);
#include "get.h"
#include "call.h"
#include "for_each.h"
#include "debug.h"
#include "trap.h"

View file

@ -34,6 +34,8 @@ std::string native(const JSString *const &);
struct value
:JS::Rooted<JS::Value>
{
explicit operator JSType() const;
explicit operator std::string() const;
explicit operator double() const;
explicit operator uint64_t() const;
@ -41,7 +43,7 @@ struct value
explicit operator uint32_t() const;
explicit operator int32_t() const;
explicit operator uint16_t() const;
explicit operator bool() const { return JS::ToBoolean(*this); }
explicit operator bool() const;
value(const char *const &);
explicit value(const std::string &);
@ -67,6 +69,8 @@ struct value
value(value &&) noexcept;
value(const value &) = delete;
value &operator=(value &&) noexcept;
friend bool undefined(const value &);
};
inline
@ -209,6 +213,13 @@ value::value(const char *const &s)
{
}
inline
value::operator bool()
const
{
return JS::ToBoolean(*this);
}
inline
value::operator uint16_t()
const
@ -283,5 +294,18 @@ const
return s? native(s) : throw type_error("Failed to cast to string");
}
inline
value::operator JSType()
const
{
return JS_TypeOfValue(*cx, *this);
}
inline bool
undefined(const value &val)
{
return JSType(val) == JSTYPE_VOID;
}
} // namespace js
} // namespace ircd

View file

@ -1302,6 +1302,25 @@ ircd::js::reflect(const JSExnType &e)
return "";
}
const char *
ircd::js::reflect(const JSType &t)
{
switch(t)
{
case JSTYPE_VOID: return "VOID";
case JSTYPE_OBJECT: return "OBJECT";
case JSTYPE_FUNCTION: return "FUNCTION";
case JSTYPE_STRING: return "STRING";
case JSTYPE_NUMBER: return "NUMBER";
case JSTYPE_BOOLEAN: return "BOOLEAN";
case JSTYPE_NULL: return "NULL";
case JSTYPE_SYMBOL: return "SYMBOL";
case JSTYPE_LIMIT: return "LIMIT";
}
return "";
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/compartment.h