2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 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. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2016-10-19 02:17:29 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_JS_OBJECT_H
|
|
|
|
|
2016-10-27 10:55:26 +02:00
|
|
|
namespace ircd {
|
|
|
|
namespace js {
|
2016-11-01 15:21:30 +01:00
|
|
|
|
|
|
|
using object_handle = JS::Handle<JSObject *>;
|
|
|
|
using object_handle_mutable = JS::MutableHandle<JSObject *>;
|
|
|
|
|
2016-11-14 00:08:03 +01:00
|
|
|
// get()/set() et al overload on reserved{n} to manipulate reserved slot n
|
|
|
|
IRCD_STRONG_TYPEDEF(uint, reserved)
|
|
|
|
|
2016-11-01 15:21:30 +01:00
|
|
|
// Get the JSClass from which the trap can also be derived.
|
2016-11-14 00:08:03 +01:00
|
|
|
bool has_jsclass(const JSObject *const &);
|
|
|
|
const JSClass &jsclass(JSObject *const &);
|
|
|
|
|
|
|
|
// Get the flags from the object's JSClass or 0.
|
|
|
|
uint flags(const JSObject *const &);
|
2016-11-01 15:21:30 +01:00
|
|
|
|
2016-11-13 02:29:34 +01:00
|
|
|
// Get the `this` global from any object
|
|
|
|
JSObject *current_global(JSObject *const &);
|
|
|
|
|
2016-11-14 00:08:03 +01:00
|
|
|
// Misc utils
|
2016-11-01 15:21:30 +01:00
|
|
|
bool is_array(const object_handle &);
|
2016-11-25 09:15:27 +01:00
|
|
|
bool is_extensible(const object_handle &);
|
2016-11-01 15:21:30 +01:00
|
|
|
uint32_t size(const object_handle &);
|
|
|
|
bool deep_freeze(const object_handle &);
|
|
|
|
bool freeze(const object_handle &);
|
2016-11-25 04:04:26 +01:00
|
|
|
size_t bytecodes(const object_handle &, uint8_t *const &buf, const size_t &size);
|
2016-11-01 15:21:30 +01:00
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
struct object
|
2016-11-25 09:15:27 +01:00
|
|
|
:root<JSObject *>
|
2016-10-19 02:17:29 +02:00
|
|
|
{
|
2016-11-25 03:20:34 +01:00
|
|
|
IRCD_OVERLOAD(json)
|
2016-10-31 14:31:58 +01:00
|
|
|
IRCD_OVERLOAD(array)
|
2016-11-02 14:18:29 +01:00
|
|
|
IRCD_OVERLOAD(uninitialized)
|
2016-11-25 09:15:27 +01:00
|
|
|
|
2016-11-01 15:21:30 +01:00
|
|
|
using handle = object_handle;
|
|
|
|
using handle_mutable = object_handle_mutable;
|
2016-10-25 07:14:39 +02:00
|
|
|
|
2016-11-25 03:21:46 +01:00
|
|
|
explicit operator JSString *() const;
|
2016-11-25 09:15:27 +01:00
|
|
|
explicit operator JS::Value() const;
|
|
|
|
operator value() const;
|
2016-10-19 02:17:29 +02:00
|
|
|
|
2016-11-28 06:46:08 +01:00
|
|
|
object constructor() const; // Get the constructor
|
|
|
|
object prototype() const; // Get the prototype
|
|
|
|
void prototype(object::handle); // Set the prototype
|
2016-10-31 14:31:58 +01:00
|
|
|
|
2016-11-28 06:46:08 +01:00
|
|
|
bool is_array() const;
|
|
|
|
uint32_t size() const; // Number elements in array object
|
|
|
|
void resize(const uint32_t &); // Set the number of elements in array object
|
2016-11-04 00:46:43 +01:00
|
|
|
|
2016-11-28 06:46:08 +01:00
|
|
|
object clone() const; // Copy the object and prototype
|
2016-11-13 02:29:34 +01:00
|
|
|
|
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-11-25 04:04:26 +01:00
|
|
|
object(const uint8_t *const &bytecode, const size_t &size);
|
2016-11-25 09:15:27 +01:00
|
|
|
using root<JSObject *>::root;
|
2016-11-25 03:20:34 +01:00
|
|
|
template<class... args> object(json_t, args&&...);
|
2016-10-31 14:31:58 +01:00
|
|
|
object(array_t, const size_t &length);
|
|
|
|
object(const JS::HandleValueArray &);
|
2016-11-25 09:15:27 +01:00
|
|
|
object(std::initializer_list<std::pair<const char *, value>>);
|
|
|
|
object(const value &);
|
2016-10-19 02:17:29 +02:00
|
|
|
object(JSObject *const &);
|
2016-10-23 06:08:04 +02:00
|
|
|
object(JSObject &);
|
2017-08-23 23:25:22 +02:00
|
|
|
object(nullptr_t);
|
2016-11-02 14:18:29 +01:00
|
|
|
object(uninitialized_t);
|
2016-10-19 02:17:29 +02:00
|
|
|
object();
|
2016-11-13 02:29:34 +01:00
|
|
|
|
|
|
|
static object global(); // current_global(cx)
|
2016-10-19 02:17:29 +02:00
|
|
|
};
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object()
|
|
|
|
:object::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-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(uninitialized_t)
|
|
|
|
:object::root::type{}
|
2016-11-13 02:29:34 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-23 23:25:22 +02:00
|
|
|
inline
|
|
|
|
object::object(nullptr_t)
|
|
|
|
:object{value{nullptr}}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(JSObject &obj)
|
|
|
|
:object::root::type{&obj}
|
2016-10-23 06:08:04 +02:00
|
|
|
{
|
2016-11-01 15:21:30 +01:00
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (ref)");
|
2016-10-23 06:08:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(JSObject *const &obj)
|
|
|
|
:object::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-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(const value &val)
|
|
|
|
:object::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-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(std::initializer_list<std::pair<const char *, value>> list)
|
|
|
|
:object{}
|
2016-11-25 03:39:36 +01:00
|
|
|
{
|
|
|
|
for(const auto &pair : list)
|
|
|
|
{
|
|
|
|
const auto &key(pair.first);
|
|
|
|
const auto &val(pair.second);
|
|
|
|
if(!JS_SetProperty(*cx, *this, key, val))
|
|
|
|
throw jserror(jserror::pending);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(const JS::HandleValueArray &values)
|
|
|
|
:object::root::type
|
2016-10-31 14:31:58 +01:00
|
|
|
{
|
|
|
|
JS_NewArrayObject(*cx, values)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (array)");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(array_t,
|
|
|
|
const size_t &length)
|
|
|
|
:object::root::type
|
2016-10-31 14:31:58 +01:00
|
|
|
{
|
|
|
|
JS_NewArrayObject(*cx, length)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw internal_error("NULL object (array)");
|
|
|
|
}
|
|
|
|
|
2016-11-25 03:20:34 +01:00
|
|
|
template<class... args>
|
2016-11-25 09:15:27 +01:00
|
|
|
object::object(json_t,
|
|
|
|
args&&... a)
|
|
|
|
:object(js::json::parse(std::forward<args>(a)...))
|
2016-11-25 03:20:34 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(const JSClass *const &clasp)
|
|
|
|
:object::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-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(const JSClass *const &clasp,
|
|
|
|
const object &proto)
|
|
|
|
:object::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-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(const JSClass *const &clasp,
|
|
|
|
const JS::CallArgs &args)
|
|
|
|
:object::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-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(const JSClass *const &clasp,
|
|
|
|
const object::handle &ctor,
|
|
|
|
const JS::HandleValueArray &args)
|
|
|
|
:object::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)");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
|
|
|
object::object(const uint8_t *const &bytecode,
|
|
|
|
const size_t &size)
|
|
|
|
:object::root::type
|
2016-11-25 04:04:26 +01:00
|
|
|
{
|
2017-08-23 23:25:22 +02:00
|
|
|
//JS_DecodeInterpretedFunction(*cx, bytecode, size)
|
2016-11-25 04:04:26 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
if(unlikely(!this->get()))
|
|
|
|
throw jserror(jserror::pending);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline object
|
|
|
|
object::global()
|
2016-11-13 02:29:34 +01:00
|
|
|
{
|
|
|
|
return current_global();
|
|
|
|
}
|
|
|
|
|
2016-11-28 06:46:08 +01:00
|
|
|
inline object
|
|
|
|
object::clone()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return JS_CloneObject(*cx, *this, prototype());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
object::resize(const uint32_t &length)
|
|
|
|
{
|
|
|
|
if(!JS_SetArrayLength(*cx, *this, length))
|
|
|
|
throw internal_error("Failed to set array object length");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t
|
|
|
|
object::size()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return js::size(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
object::is_array()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return js::is_array(handle(*this));
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline object
|
|
|
|
object::constructor()
|
2016-11-13 02:29:34 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
return JS_GetConstructor(*cx, *this);
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline void
|
|
|
|
object::prototype(object::handle obj)
|
2016-11-04 00:46:43 +01:00
|
|
|
{
|
|
|
|
if(!JS_SetPrototype(*cx, *this, obj))
|
|
|
|
throw internal_error("Failed to set prototype for object");
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline object
|
|
|
|
object::prototype()
|
2016-11-04 00:46:43 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
object ret;
|
|
|
|
if(!JS_GetPrototype(*cx, *this, &ret))
|
|
|
|
throw internal_error("Failed to get prototype for object");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
2017-08-23 23:25:22 +02:00
|
|
|
object::operator value()
|
2016-11-25 03:21:46 +01:00
|
|
|
const
|
|
|
|
{
|
2017-08-23 23:25:22 +02:00
|
|
|
return static_cast<JS::Value>(*this);
|
2016-11-25 03:21:46 +01:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
2017-08-23 23:25:22 +02:00
|
|
|
object::operator JS::Value()
|
2016-10-19 02:17:29 +02:00
|
|
|
const
|
|
|
|
{
|
2016-11-25 03:21:46 +01:00
|
|
|
assert(this->get());
|
|
|
|
return JS::ObjectValue(*this->get());
|
2016-10-19 02:17:29 +02:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline
|
2017-08-23 23:25:22 +02:00
|
|
|
object::operator JSString *()
|
2016-11-25 09:15:27 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(this->get());
|
2017-08-23 23:25:22 +02:00
|
|
|
return JS_BasicObjectToString(*cx, *this);
|
2016-11-25 09:15:27 +01:00
|
|
|
}
|
2016-11-01 15:21:30 +01:00
|
|
|
|
2016-11-25 04:04:26 +01:00
|
|
|
inline size_t
|
|
|
|
bytecodes(const object_handle &obj,
|
|
|
|
uint8_t *const &buf,
|
|
|
|
const size_t &size)
|
|
|
|
{
|
|
|
|
uint32_t ret;
|
|
|
|
const custom_ptr<void> ptr
|
|
|
|
{
|
2017-08-23 23:25:22 +02:00
|
|
|
//JS_EncodeInterpretedFunction(*cx, obj, &ret), js_free
|
2016-11-25 04:04:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto cpsz(std::min(size_t(ret), size));
|
|
|
|
memcpy(buf, ptr.get(), cpsz);
|
|
|
|
return cpsz;
|
|
|
|
}
|
|
|
|
|
2016-11-01 15:21:30 +01:00
|
|
|
inline bool
|
|
|
|
freeze(const object_handle &obj)
|
2016-10-31 14:31:58 +01:00
|
|
|
{
|
|
|
|
return JS_FreezeObject(*cx, obj);
|
|
|
|
}
|
|
|
|
|
2016-11-01 15:21:30 +01:00
|
|
|
inline bool
|
|
|
|
deep_freeze(const object_handle &obj)
|
2016-10-31 14:31:58 +01:00
|
|
|
{
|
|
|
|
return JS_DeepFreezeObject(*cx, obj);
|
|
|
|
}
|
|
|
|
|
2016-11-01 15:21:30 +01:00
|
|
|
inline uint32_t
|
|
|
|
size(const object_handle &obj)
|
|
|
|
{
|
|
|
|
uint32_t ret;
|
|
|
|
if(!JS_GetArrayLength(*cx, obj, &ret))
|
|
|
|
throw internal_error("Failed to get array object length");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-25 09:15:27 +01:00
|
|
|
inline bool
|
|
|
|
is_array(const object &obj)
|
|
|
|
{
|
|
|
|
return is_array(object::handle(obj));
|
|
|
|
}
|
|
|
|
|
2016-11-01 15:21:30 +01:00
|
|
|
inline bool
|
|
|
|
is_array(const object_handle &obj)
|
2016-10-31 14:31:58 +01:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
if(!JS_IsArrayObject(*cx, obj, &ret))
|
|
|
|
throw internal_error("Failed to query if object is array");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-01 15:21:30 +01:00
|
|
|
inline bool
|
|
|
|
is_extensible(const object_handle &obj)
|
2016-10-31 14:31:58 +01:00
|
|
|
{
|
|
|
|
bool ret;
|
|
|
|
if(!JS_IsExtensible(*cx, obj, &ret))
|
|
|
|
throw internal_error("Failed to query object extensibility");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-13 02:29:34 +01:00
|
|
|
inline JSObject *
|
|
|
|
current_global(JSObject *const &obj)
|
|
|
|
{
|
|
|
|
return JS_GetGlobalForObject(*cx, obj);
|
|
|
|
}
|
|
|
|
|
2016-11-14 00:08:03 +01:00
|
|
|
inline uint
|
|
|
|
flags(const JSObject *const &obj)
|
|
|
|
{
|
|
|
|
return has_jsclass(obj)? jsclass(const_cast<JSObject *>(obj)).flags : 0;
|
|
|
|
}
|
|
|
|
|
2016-11-01 15:21:30 +01:00
|
|
|
inline const JSClass &
|
2016-11-14 00:08:03 +01:00
|
|
|
jsclass(JSObject *const &obj)
|
2016-10-29 14:15:36 +02:00
|
|
|
{
|
2016-11-14 00:08:03 +01:00
|
|
|
assert(has_jsclass(obj));
|
2016-10-29 14:15:36 +02:00
|
|
|
const auto jsc(JS_GetClass(obj));
|
|
|
|
return *const_cast<JSClass *>(jsc);
|
|
|
|
}
|
|
|
|
|
2016-11-14 00:08:03 +01:00
|
|
|
inline bool
|
|
|
|
has_jsclass(const JSObject *const &obj)
|
2016-11-13 02:36:03 +01:00
|
|
|
{
|
2016-11-25 03:21:46 +01:00
|
|
|
assert(obj);
|
2016-11-14 00:08:03 +01:00
|
|
|
return JS_GetClass(const_cast<JSObject *>(obj)) != nullptr;
|
2016-11-13 02:36:03 +01:00
|
|
|
}
|
|
|
|
|
2016-10-19 02:17:29 +02:00
|
|
|
} // namespace js
|
|
|
|
} // namespace ircd
|