0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2025-01-13 08:23:56 +01:00

ircd::js: Add script bytecode and compile-test utils.

This commit is contained in:
Jason Volk 2016-11-24 18:46:17 -08:00
parent 6c54215336
commit 4c6e182241
2 changed files with 48 additions and 1 deletions

View file

@ -25,8 +25,11 @@
namespace ircd {
namespace js {
string decompile(const JS::Handle<JSScript *> &, const char *const &name, const bool &pretty = false);
ctx::future<void *> compile_async(const JS::ReadOnlyCompileOptions &, const std::u16string &);
string decompile(const JS::Handle<JSScript *> &, const char *const &name, const bool &pretty = false);
size_t bytecodes(const JS::Handle<JSScript *> &, uint8_t *const &buf, const size_t &max);
bool compilable(const char *const &str, const size_t &len, const object &stack = {});
bool compilable(const std::string &str, const object &stack = {});
namespace basic {
@ -43,6 +46,7 @@ struct script
script(yielding_t, const JS::ReadOnlyCompileOptions &opts, const std::u16string &src);
script(const JS::ReadOnlyCompileOptions &opts, const std::u16string &src);
script(const JS::ReadOnlyCompileOptions &opts, const std::string &src);
script(const uint8_t *const &bytecode, const size_t &size);
script(JSScript *const &);
script(JSScript &);
};
@ -71,6 +75,18 @@ script<L>::script(JSScript *const &val)
throw internal_error("NULL script");
}
template<lifetime L>
script<L>::script(const uint8_t *const &bytecode,
const size_t &size)
:script<L>::root::type
{
JS_DecodeScript(*cx, bytecode, size)
}
{
if(unlikely(!this->get()))
throw jserror(jserror::pending);
}
template<lifetime L>
script<L>::script(const JS::ReadOnlyCompileOptions &opts,
const std::string &src)

View file

@ -1230,6 +1230,37 @@ void handle_compile_async(void *, void *) noexcept;
} // namespace js
} // namespace ircd
bool
ircd::js::compilable(const std::string &str,
const object &stack)
{
return compilable(str.c_str(), str.size(), stack);
}
bool
ircd::js::compilable(const char *const &str,
const size_t &len,
const object &stack)
{
return JS_BufferIsCompilableUnit(*cx, stack, str, len);
}
size_t
ircd::js::bytecodes(const JS::Handle<JSScript *> &s,
uint8_t *const &buf,
const size_t &max)
{
uint32_t len;
const custom_ptr<void> ptr
{
JS_EncodeScript(*cx, s, &len), js_free
};
const size_t ret(std::min(size_t(len), max));
memcpy(buf, ptr.get(), ret);
return ret;
}
ircd::js::string
ircd::js::decompile(const JS::Handle<JSScript *> &s,
const char *const &name,