0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-06-02 18:18:56 +02:00

ircd::js: Add global/task struct.

This commit is contained in:
Jason Volk 2016-10-29 05:25:59 -07:00
parent d0ec6497c7
commit cff506e0be
4 changed files with 174 additions and 0 deletions

35
include/ircd/js/global.h Normal file
View file

@ -0,0 +1,35 @@
/*
* 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_GLOBAL_H
namespace ircd {
namespace js {
struct global
:heap_object
{
global(trap &, JSPrincipals *const & = nullptr);
};
} // namespace js
} // namespace ircd

View file

@ -86,3 +86,5 @@ inline JSVersion version(const char *const &v) { return JS_StringToVersion(v);
#include "trap.h"
#include "trap_function.h"
#include "generator.h"
#include "global.h"
#include "task.h"

64
include/ircd/js/task.h Normal file
View file

@ -0,0 +1,64 @@
/*
* 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_TASK_H
namespace ircd {
namespace js {
struct task
{
struct global global; // global / this / root scope object
heap_function main; // main generator wrapper function
struct generator generator; // generator state
// Invokes next() on the generator
template<class... args> value operator()(args&&...);
task(const std::string &source);
task() = default;
static task &get(const object &global);
static task &get();
};
template<class... args>
value
task::operator()(args&&... a)
{
return generator.next(std::forward<args>(a)...);
}
inline task &
task::get()
{
return get(current_global());
}
inline task &
task::get(const object &global)
{
return priv<task>(global);
}
} // namespace js
} // namespace ircd

View file

@ -137,6 +137,79 @@ js::ReportOutOfMemory(ExclusiveContext *const c)
// ircd/js/js.h - With 3rd party (JSAPI) symbols
//
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/task.h
//
ircd::js::task::task(const std::string &source)
:global{[this]
{
// Global object is constructed using the root trap (JSClass) at *tree;
// This is a thread_local registered by the kernel.so module.
struct global global(*tree);
// The root trap is configured with HAS_PRIVATE and that slot is set so we can find this
// `struct task` from the global object using task::get(object). The global object
// can first be found from a context or active compartment. As a convenience `struct task`
// can be found contextually with task::get(void).
priv(global, this);
return global;
}()}
,main{[this, &source]
{
// A compartment for the global must be entered to compile in this scope
const compartment c(this->global);
// TODO: options
JS::CompileOptions opts(*cx);
JS::AutoObjectVector stack(*cx);
// The function must be compiled in this scope and returned as a heap_function
// before the compartment destructs.
return heap_function { stack, opts, "main", {}, source };
}()}
,generator{[this]
{
// A compartment for the global must be entered to run the generator wrapper.
const compartment c(this->global);
// Run the generator wrapper (main function) returning the generator object.
// The run() closure provides safety for entering the JS engine.
value state(run([this]
{
return this->main(this->global);
}));
// Construct the generator object here on the stack while in a compartment. The
// instance then contains returnable heap objects.
struct generator ret(state);
return ret;
}()}
{
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/global.h
//
ircd::js::global::global(trap &trap,
JSPrincipals *const &principals)
:heap_object
{
JS_NewGlobalObject(*cx, &trap.jsclass(), principals, JS::DontFireOnNewGlobalHook)
}
{
const compartment c(*this);
if(!JS_InitStandardClasses(*cx, *this))
throw error("Failed to init standard classes for global object");
JS_FireOnNewGlobalObject(*cx, *this);
}
///////////////////////////////////////////////////////////////////////////////
//
// ircd/js/generator.h