diff --git a/include/ircd/js/ctor.h b/include/ircd/js/ctor.h new file mode 100644 index 000000000..b6a0f90c9 --- /dev/null +++ b/include/ircd/js/ctor.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2016 Charybdis Development Team + * Copyright (C) 2016 Jason Volk + * + * 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_CTOR_H + +namespace ircd { +namespace js { + +inline object +ctor(const object::handle &proto, + const vector::handle &args = {}) +{ + return JS_New(*cx, proto, args); +} + +inline object +ctor(trap &trap, + const vector::handle &args = {}) +{ + object proto(trap()); + return JS_New(*cx, proto, args); +} + +} // namespace js +} // namespace ircd diff --git a/include/ircd/js/js.h b/include/ircd/js/js.h index d42da125e..a69c95f63 100644 --- a/include/ircd/js/js.h +++ b/include/ircd/js/js.h @@ -88,6 +88,7 @@ inline JSVersion version(const char *const &v) { return JS_StringToVersion(v); #include "args.h" #include "trap.h" #include "trap_function.h" +#include "ctor.h" #include "generator.h" #include "global.h" #include "contract.h" diff --git a/include/ircd/js/trap.h b/include/ircd/js/trap.h index ffaa3da48..49f58c0bc 100644 --- a/include/ircd/js/trap.h +++ b/include/ircd/js/trap.h @@ -35,6 +35,11 @@ class trap std::unique_ptr _class; std::map children; + static trap &from(const JSObject &); + static trap &from(const JS::HandleObject &); + + void debug(const char *fmt, ...) const AFP(2, 3); + // Override these to define JS objects in C virtual value on_call(object::handle, value::handle, const args &); virtual value on_set(object::handle, id::handle, value::handle); @@ -48,13 +53,9 @@ class trap virtual void on_gc(JSObject &); private: - void host_exception(const char *fmt, ...) const AFP(2, 3); - void debug(const char *fmt, ...) const AFP(2, 3); void add_this(); void del_this(); - - static trap &from(const JSObject &); - static trap &from(const JS::HandleObject &); + void host_exception(const char *fmt, ...) const AFP(2, 3); // Internal callback interface static void handle_trace(JSTracer *, JSObject *) noexcept; @@ -85,8 +86,8 @@ class trap operator const JSClass *() const { return &jsclass(); } IRCD_OVERLOAD(prototyped) - template object operator()(prototyped_t, const object &parent, const object &proto, args&&...); - template object operator()(const object &parent, args&&...); + object operator()(prototyped_t, const object &parent, const object &proto); + object operator()(const object &parent = {}); trap(const std::string &path, const uint &flags = 0, const uint &prop_flags = 0); trap(trap &&) = delete; @@ -96,20 +97,16 @@ class trap extern __thread trap *tree; -template -object -trap::operator()(const object &parent, - args&&... a) +inline object +trap::operator()(const object &parent) { - return operator()(prototyped, parent, object{}, std::forward(a)...); + return operator()(prototyped, parent, object{}); } -template -object +inline object trap::operator()(prototyped_t, const object &parent, - const object &parent_proto, - args&&... a) + const object &parent_proto) { object proto(JS_InitClass(*cx, parent, @@ -121,8 +118,7 @@ trap::operator()(prototyped_t, fs, nullptr, nullptr)); - - return JS_New(*cx, proto, vector{std::forward(a)...}); + return proto; } } // namespace js