2016-10-19 02:17:29 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Charybdis Development Team
|
|
|
|
* Copyright (C) 2016 Jason Volk <jason@zemos.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_JS_OBJECT_H
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace js {
|
|
|
|
namespace basic {
|
2016-10-19 02:17:29 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
2016-10-19 02:17:29 +02:00
|
|
|
struct object
|
2016-10-27 10:55:26 +02:00
|
|
|
:root<JSObject *, L>
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-31 14:31:58 +01:00
|
|
|
IRCD_OVERLOAD(array)
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
using handle = typename root<JSObject *, L>::handle;
|
|
|
|
using handle_mutable = typename root<JSObject *, L>::handle_mutable;
|
2016-10-25 07:14:39 +02:00
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
operator JS::Value() const;
|
|
|
|
|
2016-10-31 14:31:58 +01:00
|
|
|
// for array objects
|
|
|
|
uint32_t size() const;
|
|
|
|
void resize(const uint32_t &);
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
// new object
|
2016-10-25 07:26:47 +02:00
|
|
|
object(const JSClass *const &, const handle &ctor, const JS::HandleValueArray &args);
|
|
|
|
object(const JSClass *const &, const JS::CallArgs &args);
|
2016-10-19 02:17:29 +02:00
|
|
|
object(const JSClass *const &, const object &proto);
|
|
|
|
object(const JSClass *const &);
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<class T, lifetime LL> object(const root<T, LL> &);
|
|
|
|
using root<JSObject *, L>::root;
|
2016-10-31 14:31:58 +01:00
|
|
|
object(array_t, const size_t &length);
|
|
|
|
object(const JS::HandleValueArray &);
|
2016-10-27 10:55:26 +02:00
|
|
|
object(const value<L> &);
|
2016-10-19 02:17:29 +02:00
|
|
|
object(JSObject *const &);
|
2016-10-23 06:08:04 +02:00
|
|
|
object(JSObject &);
|
2016-10-19 02:17:29 +02:00
|
|
|
object();
|
|
|
|
};
|
|
|
|
|
2016-10-29 14:15:36 +02:00
|
|
|
// Get the JSClass from which the trap can also be derived.
|
|
|
|
template<lifetime L> const JSClass &jsclass(const object<L> &);
|
|
|
|
|
|
|
|
// Private data slot (trap must have flag JSCLASS_HAS_PRIVATE)
|
|
|
|
template<class T, lifetime L> T &priv(const object<L> &);
|
|
|
|
template<lifetime L> void priv(object<L> &, void *const &);
|
|
|
|
template<lifetime L> void priv(object<L> &, const void *const &);
|
|
|
|
|
2016-10-31 14:31:58 +01:00
|
|
|
template<lifetime L> bool is_extensible(const object<L> &);
|
|
|
|
template<lifetime L> bool is_array(const object<L> &);
|
|
|
|
template<lifetime L> uint32_t size(const object<L> &);
|
|
|
|
|
|
|
|
template<lifetime L> bool deep_freeze(const object<L> &);
|
|
|
|
template<lifetime L> bool freeze(const object<L> &);
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
} // namespace basic
|
|
|
|
|
|
|
|
using object = basic::object<lifetime::stack>;
|
|
|
|
using heap_object = basic::object<lifetime::heap>;
|
2016-10-29 14:15:36 +02:00
|
|
|
using basic::priv;
|
|
|
|
|
|
|
|
IRCD_STRONG_TYPEDEF(uint, reserved)
|
2016-10-19 02:17:29 +02:00
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
//
|
|
|
|
// Implementation
|
|
|
|
//
|
|
|
|
namespace basic {
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object()
|
|
|
|
:object<L>::root::type
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
JS_NewPlainObject(*cx)
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
|
|
|
{
|
2016-10-31 14:31:58 +01:00
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (plain)");
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object(JSObject &obj)
|
|
|
|
:object<L>::root::type{&obj}
|
2016-10-23 06:08:04 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object(JSObject *const &obj)
|
|
|
|
:object<L>::root::type{obj}
|
2016-10-23 06:08:04 +02:00
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
if(unlikely(!this->get()))
|
2016-10-23 06:08:04 +02:00
|
|
|
throw internal_error("NULL object");
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object(const value<L> &val)
|
|
|
|
:object<L>::root::type{}
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
if(!JS_ValueToObject(*cx, val, &(*this)))
|
|
|
|
throw type_error("Value is not an Object");
|
|
|
|
}
|
|
|
|
|
2016-10-31 14:31:58 +01:00
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object(const JS::HandleValueArray &values)
|
|
|
|
:object<L>::root::type
|
|
|
|
{
|
|
|
|
JS_NewArrayObject(*cx, values)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (array)");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object(array_t,
|
|
|
|
const size_t &length)
|
|
|
|
:object<L>::root::type
|
|
|
|
{
|
|
|
|
JS_NewArrayObject(*cx, length)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (array)");
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
template<class T,
|
|
|
|
lifetime LL>
|
|
|
|
object<L>::object(const root<T, LL> &o)
|
|
|
|
:object{o.get()}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object(const JSClass *const &clasp)
|
|
|
|
:object<L>::root::type
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
JS_NewObject(*cx, clasp)
|
|
|
|
}
|
|
|
|
{
|
2016-10-31 14:31:58 +01:00
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (clasp)");
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object(const JSClass *const &clasp,
|
|
|
|
const object &proto)
|
|
|
|
:object<L>::root::type
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
|
|
|
JS_NewObjectWithGivenProto(*cx, clasp, proto)
|
|
|
|
}
|
|
|
|
{
|
2016-10-31 14:31:58 +01:00
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (with given proto)");
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object(const JSClass *const &clasp,
|
|
|
|
const JS::CallArgs &args)
|
|
|
|
:object<L>::root::type
|
2016-10-25 07:26:47 +02:00
|
|
|
{
|
|
|
|
JS_NewObjectForConstructor(*cx, clasp, args)
|
|
|
|
}
|
|
|
|
{
|
2016-10-31 14:31:58 +01:00
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (for constructor)");
|
2016-10-25 07:26:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
object<L>::object(const JSClass *const &clasp,
|
|
|
|
const object::handle &ctor,
|
|
|
|
const JS::HandleValueArray &args)
|
|
|
|
:object<L>::root::type
|
2016-10-25 07:26:47 +02:00
|
|
|
{
|
|
|
|
JS_New(*cx, ctor, args)
|
|
|
|
}
|
|
|
|
{
|
2016-10-31 14:31:58 +01:00
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (new)");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
void
|
|
|
|
object<L>::resize(const uint32_t &length)
|
|
|
|
{
|
|
|
|
if(!JS_SetArrayLength(*cx, &(*this), length))
|
|
|
|
throw internal_error("Failed to set array object length");
|
|
|
|
}
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
uint32_t
|
|
|
|
object<L>::size()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
uint32_t ret;
|
|
|
|
if(!JS_GetArrayLength(*cx, handle(*this), &ret))
|
|
|
|
throw internal_error("Failed to get array object length");
|
|
|
|
|
|
|
|
return ret;
|
2016-10-25 07:26:47 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
template<lifetime L>
|
|
|
|
object<L>::operator JS::Value()
|
2016-10-19 02:17:29 +02:00
|
|
|
const
|
|
|
|
{
|
2016-10-27 10:55:26 +02:00
|
|
|
return this->get()? JS::ObjectValue(*this->get()) : JS::NullValue();
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
|
|
|
|
2016-10-31 14:31:58 +01:00
|
|
|
template<lifetime L>
|
|
|
|
bool
|
|
|
|
freeze(const object<L> & obj)
|
|
|
|
{
|
|
|
|
return JS_FreezeObject(*cx, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
bool
|
|
|
|
deep_freeze(const object<L> & obj)
|
|
|
|
{
|
|
|
|
return JS_DeepFreezeObject(*cx, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
bool
|
|
|
|
is_array(const object<L> & obj)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
if(!JS_IsArrayObject(*cx, obj, &ret))
|
|
|
|
throw internal_error("Failed to query if object is array");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
bool
|
|
|
|
is_extensible(const object<L> & obj)
|
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
if(!JS_IsExtensible(*cx, obj, &ret))
|
|
|
|
throw internal_error("Failed to query object extensibility");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-29 14:15:36 +02:00
|
|
|
template<lifetime L>
|
|
|
|
void
|
|
|
|
priv(object<L> &obj,
|
|
|
|
const void *const &ptr)
|
|
|
|
{
|
|
|
|
priv(obj, const_cast<void *>(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
void
|
|
|
|
priv(object<L> &obj,
|
|
|
|
void *const &ptr)
|
|
|
|
{
|
|
|
|
JS_SetPrivate(obj, ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T,
|
|
|
|
lifetime L>
|
|
|
|
T &
|
|
|
|
priv(const object<L> &obj)
|
|
|
|
{
|
|
|
|
const auto &jsc(jsclass(obj));
|
|
|
|
const auto ret(JS_GetInstancePrivate(*cx, obj, &jsc, nullptr));
|
|
|
|
if(!ret)
|
|
|
|
throw error("Object has no private data");
|
|
|
|
|
|
|
|
return *reinterpret_cast<T *>(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<lifetime L>
|
|
|
|
const JSClass &
|
|
|
|
jsclass(const object<L> &obj)
|
|
|
|
{
|
|
|
|
const auto jsc(JS_GetClass(obj));
|
|
|
|
if(unlikely(!jsc))
|
|
|
|
throw error("Object has no JSClass");
|
|
|
|
|
|
|
|
return *const_cast<JSClass *>(jsc);
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
} // namespace basic
|
2016-10-19 02:17:29 +02:00
|
|
|
} // namespace js
|
|
|
|
} // namespace ircd
|