From ae5678269c121ead16c6ae8ce27b28100c8c35e5 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 3 Jul 2018 15:40:36 -0700 Subject: [PATCH] ircd::js: Fixes for js bitrot / maintenance cleanup. --- include/ircd/js/error.h | 13 ++++- include/ircd/js/string.h | 2 +- ircd/js.cc | 100 ++++++++++++++++----------------------- modules/js/listener.cc | 10 ++-- 4 files changed, 59 insertions(+), 66 deletions(-) diff --git a/include/ircd/js/error.h b/include/ircd/js/error.h index 7111e36c0..5f41cc165 100644 --- a/include/ircd/js/error.h +++ b/include/ircd/js/error.h @@ -28,14 +28,14 @@ struct jserror void create(JSErrorReport &); void create(const JSErrorReport &); - void generate(const JSExnType &type, const char *const &fmt, va_list ap); + void generate(const JSExnType &type, const char *const &fmt, const va_rtti &ap); void set_pending() const; void set_uncatchable() const; jserror(pending_t); jserror(generate_skip_t); jserror(const JSErrorReport &); - jserror(const char *fmt = " ", ...) AFP(2, 3); + template jserror(const char *const &fmt = " ", args&&...); jserror(JSObject &); jserror(JSObject *const &); jserror(const JS::Value &); @@ -65,3 +65,12 @@ IRCD_JS_ERROR_DEF( uri_error, JSEXN_URIERR ) } // namespace js } // namespace ircd + +template +ircd::js::jserror::jserror(const char *const &fmt, + args&&... a) +:ircd::js::error{generate_skip} +,val{} +{ + generate(JSEXN_ERR, fmt, va_rtti{std::forward(a)...}); +} diff --git a/include/ircd/js/string.h b/include/ircd/js/string.h index 05e168c5d..84140ce3b 100644 --- a/include/ircd/js/string.h +++ b/include/ircd/js/string.h @@ -531,7 +531,7 @@ inline auto hash(const string &s) { //TODO: optimize - return ircd::util::hash(std::u16string(s)); + return ircd::hash(std::u16string(s)); } templateget("IRCD_JS_MODULE")); const auto iit(imports.emplace(requesting, exporter)); const string name(exporter->trap->name); @@ -752,14 +753,12 @@ ircd::js::trap::prototype(const object::handle &globals) { 0 } }; -/* - std::transform(begin(this->memfunc), end(this->memfunc), begin(memfunc), [] - (const auto &it) - { - const auto &function(*it.second); - return function.spec; - }); -*/ +// std::transform(begin(this->memfunc), end(this->memfunc), begin(memfunc), [] +// (const auto &it) +// { +// const auto &function(*it.second); +// return function.spec; +// }); // Create object const object proto @@ -1303,27 +1302,23 @@ bool ircd::js::trap::on_has(object::handle, id::handle id) { -/* - const string sid(id); - if(children.count(sid)) - return false; - - if(std::any_of(begin(memfun), end(memfun), [&sid] - (const auto &it) - { - const auto &memfun(*it.second); - return sid == memfun.name; - })) - return false; -*/ - -/* - value val; - const auto flags(JSPROP_SHARED | JSPROP_ENUMERATE); - if(!JS_DefinePropertyById(*cx, obj, id, val, flags, handle_getter, handle_setter)) - throw jserror("Failed to define property '%s'", sid.c_str()); - -*/ +// const string sid(id); +// if(children.count(sid)) +// return false; +// +// if(std::any_of(begin(memfun), end(memfun), [&sid] +// (const auto &it) +// { +// const auto &memfun(*it.second); +// return sid == memfun.name; +// })) +// return false; +// +// value val; +// const auto flags(JSPROP_SHARED | JSPROP_ENUMERATE); +// if(!JS_DefinePropertyById(*cx, obj, id, val, flags, handle_getter, handle_setter)) +// throw jserror("Failed to define property '%s'", sid.c_str()); +// // const auto flags(0); // if(!JS_DefineObject(*cx, obj, sid.c_str(), &jsclass(), flags)) // throw jserror("Failed to define property '%s'", sid.c_str()); @@ -1464,13 +1459,11 @@ noexcept { // This frame is entered on a thread owned by SpiderMonkey, not IRCd. Do not call // ircd::log from here, it is not thread-safe. - /* - printf("[thread %s]: context(%p): compile(%p) READY (priv: %p)\n", - ircd::string(std::this_thread::get_id()).c_str(), - (const void *)cx, - token, - priv); - */ +// printf("[thread %s]: context(%p): compile(%p) READY (priv: %p)\n", +// ircd::string(std::this_thread::get_id()).c_str(), +// (const void *)cx, +// token, +// priv); // Setting the value of the promise and then deleting the promise is thread-safe. // Note that JS::FinishOffThreadScript(); will need to be called on the main thread. @@ -1813,7 +1806,7 @@ ircd::js::del(const object::handle &src, object obj(src); const char *fail(nullptr); char buffer[strlen(path) + 1]; - ircd::tokens(path, ".", buffer, sizeof(buffer), [&path, &val, &obj, &fail] + ircd::tokens(path, ".", mutable_buffer{buffer, sizeof(buffer)}, [&path, &val, &obj, &fail] (const string_view &part) { if(fail) @@ -1889,7 +1882,7 @@ ircd::js::set(const object::handle &src, object obj(src); char buffer[strlen(path) + 1]; const char *fail(nullptr), *key(nullptr); - ircd::tokens(path, ".", buffer, sizeof(buffer), [&path, &obj, &fail, &key] + ircd::tokens(path, ".", mutable_buffer{buffer, sizeof(buffer)}, [&path, &obj, &fail, &key] (const string_view &part) { if(fail) @@ -1981,8 +1974,8 @@ ircd::js::get(const object::handle &src, value ret; object obj(src); const char *fail(nullptr); - char buf[strlen(path) + 1]; - ircd::tokens(path, ".", buf, sizeof(buf), [&obj, &path, &ret, &fail] + char buffer[strlen(path) + 1]; + ircd::tokens(path, ".", mutable_buffer{buffer, sizeof(buffer)}, [&obj, &path, &ret, &fail] (const string_view &part) { if(fail) @@ -2046,8 +2039,8 @@ ircd::js::has(const object::handle &src, bool ret(true); object obj(src); const char *fail(nullptr); - char buf[strlen(path) + 1]; - ircd::tokens(path, ".", buf, sizeof(buf), [&obj, &path, &ret, &fail] + char buffer[strlen(path) + 1]; + ircd::tokens(path, ".", mutable_buffer{buffer, sizeof(buffer)}, [&obj, &path, &ret, &fail] (const string_view &part) { if(fail) @@ -2391,17 +2384,6 @@ ircd::js::jserror::jserror(generate_skip_t) { } -ircd::js::jserror::jserror(const char *const fmt, - ...) -:ircd::js::error{generate_skip} -,val{} -{ - va_list ap; - va_start(ap, fmt); - generate(JSEXN_ERR, fmt, ap); - va_end(ap); -} - ircd::js::jserror::jserror(const JSErrorReport &report) :ircd::js::error{generate_skip} ,val{} @@ -2455,7 +2437,7 @@ const void ircd::js::jserror::generate(const JSExnType &type, const char *const &fmt, - va_list ap) + const va_rtti &ap) { ircd::exception::generate(fmt, ap); const auto msg(locale::char16::conv(what())); diff --git a/modules/js/listener.cc b/modules/js/listener.cc index f2943b846..074791e67 100644 --- a/modules/js/listener.cc +++ b/modules/js/listener.cc @@ -8,7 +8,7 @@ // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. -#include +#include #include namespace ircd { @@ -29,11 +29,13 @@ static listener; struct listener::listen :trap::function { - ircd::listener *l; + net::listener *l; value on_call(object::handle obj, value::handle _this, const args &args) override { - l = new ircd::listener(std::string(args[0])); + const std::string a(args[0]); + const ircd::json::object o(a); + l = new net::listener(o.get("name", "js"), o); return {}; } @@ -81,7 +83,7 @@ ircd::js::listener::listener() { } -extern "C" ircd::js::module * +ircd::js::module * IRCD_JS_MODULE { &ircd::js::listener.module