2016-10-12 09:04:26 +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_RUNTIME_H
|
|
|
|
|
|
|
|
namespace ircd {
|
|
|
|
namespace js {
|
|
|
|
|
2016-10-13 04:58:48 +02:00
|
|
|
class runtime
|
2016-10-12 09:04:26 +02:00
|
|
|
{
|
2016-10-18 03:43:23 +02:00
|
|
|
static void handle_error(JSContext *, const char *msg, JSErrorReport *) noexcept;
|
|
|
|
static void handle_out_of_memory(JSContext *, void *) noexcept;
|
|
|
|
static void handle_large_allocation_failure(void *) noexcept;
|
2016-10-24 03:17:33 +02:00
|
|
|
static void handle_telemetry(int id, uint32_t sample, const char *key) noexcept;
|
2016-11-13 02:36:03 +01:00
|
|
|
static void handle_finalize(JSFreeOp *, JSFinalizeStatus, bool is_compartment, void *) noexcept;
|
|
|
|
static void handle_trace_gray(JSTracer *, void *) noexcept;
|
|
|
|
static void handle_trace_extra(JSTracer *, void *) noexcept;
|
2016-11-26 04:17:26 +01:00
|
|
|
static void handle_weak_pointer_zone(JSRuntime *, void *) noexcept;
|
|
|
|
static void handle_weak_pointer_compartment(JSRuntime *, JSCompartment *, void *) noexcept;
|
2016-11-13 06:00:36 +01:00
|
|
|
static void handle_slice(JSRuntime *, JS::GCProgress, const JS::GCDescription &) noexcept;
|
2016-10-30 13:51:22 +01:00
|
|
|
static void handle_zone_sweep(JS::Zone *) noexcept;
|
|
|
|
static void handle_zone_destroy(JS::Zone *) noexcept;
|
2016-10-18 03:43:23 +02:00
|
|
|
static void handle_compartment_name(JSRuntime *, JSCompartment *, char *buf, size_t) noexcept;
|
|
|
|
static void handle_compartment_destroy(JSFreeOp *, JSCompartment *) noexcept;
|
2016-11-13 02:36:03 +01:00
|
|
|
static void handle_gc(JSRuntime *, JSGCStatus, void *) noexcept;
|
2016-11-25 03:43:58 +01:00
|
|
|
static bool handle_preserve_wrapper(JSContext *, JSObject *) noexcept;
|
2016-10-18 03:43:23 +02:00
|
|
|
static bool handle_context(JSContext *, uint op, void *) noexcept;
|
2016-10-24 03:17:33 +02:00
|
|
|
static void handle_activity(void *priv, bool active) noexcept;
|
2016-10-18 03:43:23 +02:00
|
|
|
static bool handle_interrupt(JSContext *) noexcept;
|
|
|
|
|
2016-10-13 04:58:48 +02:00
|
|
|
public:
|
2016-10-14 02:24:50 +02:00
|
|
|
struct opts
|
|
|
|
{
|
2016-10-29 05:44:20 +02:00
|
|
|
size_t max_bytes = 64_MiB;
|
|
|
|
size_t max_nursery_bytes = 16_MiB;
|
2016-10-14 02:24:50 +02:00
|
|
|
size_t code_stack_max = 0;
|
|
|
|
size_t trusted_stack_max = 0;
|
|
|
|
size_t untrusted_stack_max = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct opts opts; // We keep a copy of the given opts here
|
2016-10-29 05:44:20 +02:00
|
|
|
std::thread::id tid; // This is recorded for assertions/logging.
|
2016-11-13 02:36:03 +01:00
|
|
|
struct tracing tracing; // State for garbage collection / tracing.
|
|
|
|
custom_ptr<JSRuntime> _ptr;
|
2016-10-14 02:24:50 +02:00
|
|
|
|
2016-11-13 02:36:03 +01:00
|
|
|
operator JSRuntime *() const { return _ptr.get(); }
|
|
|
|
operator JSRuntime &() const { return *_ptr; }
|
|
|
|
bool operator!() const { return !_ptr; }
|
|
|
|
auto get() const { return _ptr.get(); }
|
|
|
|
auto get() { return _ptr.get(); }
|
2016-10-13 04:58:48 +02:00
|
|
|
|
2016-10-29 05:44:20 +02:00
|
|
|
runtime(const struct opts &, runtime *const &parent = nullptr);
|
2016-10-12 09:04:26 +02:00
|
|
|
runtime() = default;
|
2016-10-14 02:24:50 +02:00
|
|
|
runtime(runtime &&) noexcept;
|
|
|
|
runtime(const runtime &) = delete;
|
|
|
|
runtime &operator=(runtime &&) noexcept;
|
|
|
|
runtime &operator=(const runtime &) = delete;
|
2016-10-16 01:53:44 +02:00
|
|
|
~runtime() noexcept;
|
2016-10-14 02:24:50 +02:00
|
|
|
|
|
|
|
friend void interrupt(runtime &);
|
2016-10-12 09:04:26 +02:00
|
|
|
};
|
|
|
|
|
2016-11-13 02:29:34 +01:00
|
|
|
// Current thread_local runtime. This value affects contextual data for almost every function
|
|
|
|
// in this entire subsystem (ircd::js). Located in ircd/js.cc
|
2016-10-16 01:53:44 +02:00
|
|
|
extern __thread runtime *rt;
|
|
|
|
|
2016-10-14 02:24:50 +02:00
|
|
|
// Get to our `struct runtime` from any upstream JSRuntime
|
|
|
|
const runtime &our(const JSRuntime *const &);
|
|
|
|
runtime &our(JSRuntime *const &);
|
|
|
|
|
2016-10-15 07:44:42 +02:00
|
|
|
// Get to our runtime from any JSObject
|
2016-10-22 09:34:12 +02:00
|
|
|
const runtime &our_runtime(const JSObject &);
|
|
|
|
runtime &our_runtime(JSObject &);
|
|
|
|
|
|
|
|
// Get to our runtime from any JSFreeOp
|
|
|
|
JSFreeOp *default_freeop(runtime &);
|
|
|
|
const runtime &our_runtime(const JSFreeOp &);
|
|
|
|
runtime &our_runtime(JSFreeOp &);
|
2016-10-15 07:44:42 +02:00
|
|
|
|
2016-10-20 12:25:41 +02:00
|
|
|
// Do not call interrupt() unless you know what you're doing; see context.h.
|
2016-10-14 02:24:50 +02:00
|
|
|
void interrupt(runtime &r);
|
2016-11-07 08:12:38 +01:00
|
|
|
bool run_gc(runtime &r) noexcept;
|
2016-10-14 02:24:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
inline void
|
|
|
|
interrupt(runtime &r)
|
|
|
|
{
|
|
|
|
JS_RequestInterruptCallback(r);
|
|
|
|
}
|
|
|
|
|
2016-10-15 07:44:42 +02:00
|
|
|
inline runtime &
|
2016-10-22 09:34:12 +02:00
|
|
|
our_runtime(JSFreeOp &o)
|
|
|
|
{
|
|
|
|
return our(o.runtime());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const runtime &
|
|
|
|
our_runtime(const JSFreeOp &o)
|
|
|
|
{
|
|
|
|
return our(o.runtime());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JSFreeOp *
|
|
|
|
default_freeop(runtime &r)
|
|
|
|
{
|
|
|
|
return JS_GetDefaultFreeOp(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline runtime &
|
|
|
|
our_runtime(JSObject &o)
|
2016-10-15 07:44:42 +02:00
|
|
|
{
|
|
|
|
return our(JS_GetObjectRuntime(&o));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const runtime &
|
2016-10-22 09:34:12 +02:00
|
|
|
our_runtime(const JSObject &o)
|
2016-10-15 07:44:42 +02:00
|
|
|
{
|
|
|
|
return our(JS_GetObjectRuntime(const_cast<JSObject *>(&o)));
|
|
|
|
}
|
|
|
|
|
2016-10-14 02:24:50 +02:00
|
|
|
inline runtime &
|
|
|
|
our(JSRuntime *const &c)
|
2016-10-12 09:04:26 +02:00
|
|
|
{
|
2016-10-14 02:24:50 +02:00
|
|
|
return *static_cast<runtime *>(JS_GetRuntimePrivate(c));
|
2016-10-12 09:04:26 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 02:24:50 +02:00
|
|
|
inline const runtime &
|
|
|
|
our(const JSRuntime *const &c)
|
|
|
|
{
|
|
|
|
return *static_cast<const runtime *>(JS_GetRuntimePrivate(const_cast<JSRuntime *>(c)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-12 09:04:26 +02:00
|
|
|
} // namespace js
|
|
|
|
} // namespace ircd
|