/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
import {
set, determine_engine, make_check, make_special,
launch_async, exec_build, exec_unify, melt_quote,
VOID_ARGS, get_engine, freeze_quote, get_cont,
setDelay, Context, check_goal, objects_list, copy_term,
Compound, deref, unify,
check_task, cont, make_error, check_nonvar
} from "../../nova/core.mjs";
/*********************************************************************/
/* ir_quote_new/2 and ir_quote_current/2 */
/*********************************************************************/
/**
* test_ir_quote_new(D, H): internal only
* The predicate succeeds in H with the quote from the term D.
*/
function test_ir_quote_new(args) {
let alpha = exec_build(args[0]);
alpha = freeze_quote(alpha);
return exec_unify(args[1], alpha);
}
/**
* special_ir_quote_current(H, D): internal only
* The predicate succeeds in D with the term from the quote H.
*/
function special_ir_quote_current(args) {
let alpha = deref(args[0]);
alpha = melt_quote(alpha);
if (!unify(args[1], alpha))
return false;
cont(get_cont().args[1]);
return true;
}
/*********************************************************************/
/* Waitable */
/*********************************************************************/
function Waiter() {
this.callback = null;
this.owner = null;
}
export function Waitable() {
this.waiters = [];
}
function waitable_notify(obj, count) {
let list = obj.waiters;
if (count === 0)
count = list.length;
while (list.length > 0 && count > 0) {
let waiter = list[0];
list.splice(0, 1);
waiter.callback(true);
count--;
}
}
async function waitable_wait(obj, owner) {
return new Promise(resolve => {
let waiter = new Waiter();
waiter.callback = resolve;
waiter.owner = owner;
obj.waiters.push(waiter);
});
}
function waitable_cancel(obj, owner) {
let list = obj.waiters;
let i = 0;
for (; i < list.length; i++)
if (list[i].owner === owner)
break;
if (i < list.length) {
let waiter = list[i];
list.splice(i, 1);
waiter.callback(false);
}
}
/*********************************************************************/
/* Semaphore */
/*********************************************************************/
class Semaphore extends Waitable {
constructor(permits) {
super();
this.permits = permits;
}
}
function sem_release(sem) {
sem.permits++;
waitable_notify(sem, 1);
}
async function sem_acquire(sem, owner) {
while (sem.permits === 0)
if (!await waitable_wait(sem, owner))
return false;
sem.permits--;
return true;
}
/*********************************************************************/
/* Channel */
/*********************************************************************/
function Channel() {
this.head = new Semaphore(0);
this.tail = new Semaphore(1);
this.data = null;
}
async function chan_put(chan, data, owner) {
if (!await sem_acquire(chan.tail, owner))
return false;
chan.data = data;
sem_release(chan.head);
return true;
}
function chan_unput(chan, owner) {
waitable_cancel(chan.tail, owner);
}
async function chan_take(chan, owner) {
if (!await sem_acquire(chan.head, owner))
return undefined;
let res = chan.data;
chan.data = null;
sem_release(chan.tail);
return res;
}
function chan_untake(chan, owner) {
waitable_cancel(chan.head, owner);
}
/******************************************************************/
/* os_chan_new/1 and ir_is_chan/1 */
/******************************************************************/
/**
* test_os_chan_new(A): internal only
* The predicate succeeds in A with a new channel.
*/
function test_os_chan_new(args) {
let res = new Channel();
return exec_unify(args[0], res);
}
/**
* ir_is_chan(O): internal only
* The predicate succeeds if O is a channel.
*/
function test_ir_is_chan(args) {
let obj = exec_build(args[0]);
return (obj instanceof Channel);
}
/******************************************************************/
/* os_chan_put/3 and os_chan_take/3 */
/******************************************************************/
/**
* test_os_chan_put(C, Q, P): internal only
* The predicate succeeds in P with a promise that
* sends the quote Q to the channel C.
*/
function test_os_chan_put(args) {
let chan = exec_build(args[0]);
check_channel(chan);
let data = exec_build(args[1]);
return exec_unify(args[2],
os_put_promise(get_engine(), chan, data));
}
async function os_put_promise(en, chan, data) {
en.abort = () => chan_unput(chan, en);
await chan_put(chan, data, en);
en.abort = () => {};
}
/**
* test_os_chan_take(C, M, P): internal only
* The predicate succeeds in P with a promise that
* receives a quote into the map M from the channel C.
*/
function test_os_chan_take(args) {
let chan = exec_build(args[0]);
check_channel(chan);
let map = exec_build(args[1]);
return exec_unify(args[2],
os_take_promise(get_engine(), chan, map));
}
async function os_take_promise(en, chan, map) {
en.abort = () => chan_untake(chan, en);
let data = await chan_take(chan, en);
if (data !== undefined)
map.set("data", data);
en.abort = () => {};
}
/**
* check_channel(beta): internal only
* The predicate checks if beta is a channel.
*/
function check_channel(beta) {
if (!(beta instanceof Channel)) {
check_nonvar(beta);
beta = copy_term(beta);
throw make_error(new Compound("type_error",
["channel", beta]));
}
}
/*********************************************************************/
/* Event */
/*********************************************************************/
export class Event extends Waitable {
constructor() {
super();
this.set = false;
}
}
function event_is(evnt) {
return evnt.set;
}
function event_set(evnt) {
evnt.set = true;
waitable_notify(evnt, 0);
}
async function event_wait(evnt, owner) {
while (!evnt.set)
if (!await waitable_wait(evnt, owner))
return false;
return true;
}
/*********************************************************************/
/* os_task_create/2, os_task_list/1 and os_task_alive/1 */
/*********************************************************************/
/**
* os_task_create(N, E): internal only
* The predicate succeeds in E with a new task for the compiled
* goal N. The task is scheduled to execute immediately.
*/
function test_os_task_create(args) {
let goal = exec_build(args[0]);
check_goal(goal);
let tsk = new Context();
setDelay(async () => await task_async(goal, tsk, VOID_ARGS), 0);
return exec_unify(args[1], tsk);
}
let tasks = new Array(0);
export async function task_async(form, tsk, paras) {
tsk.ref.done = new Event();
tasks.push(tsk);
try {
await launch_async(form, tsk, paras);
} catch (err) {
/* */
} finally {
event_set(tsk.ref.done);
let k = tasks.lastIndexOf(tsk);
tasks.splice(k, 1);
}
}
/**
* os_task_list(L): internal only
* The predicate succeeds in L with a snapshot of the alive tasks.
*/
function test_os_task_list(args) {
let res = objects_list(tasks, 0, tasks.length);
return exec_unify(args[0], res);
}
/**
* os_task_alive(T): internal only
* The predicate succeeds if the task T is alive.
*/
function test_os_task_alive(args) {
let tsk = exec_build(args[0]);
check_task(tsk);
let en = determine_engine(tsk);
return !event_is(en.done);
}
/*********************************************************************/
/* os_task_join/2 */
/*********************************************************************/
/**
* os_task_join(T, P): internal only
* The predicate succeeds in P with a promise for the task T to die.
*/
function test_os_task_join(args) {
let tsk = exec_build(args[0]);
check_task(tsk);
let en2 = determine_engine(tsk);
return exec_unify(args[1],
task_join_promise(get_engine(), en2));
}
async function task_join_promise(en, en2) {
en.abort = () => waitable_cancel(en2.done, en);
await event_wait(en2.done, en);
en.abort = () => {};
}
/*********************************************************************/
/* Evnt Lib Init */
/*********************************************************************/
export function main() {
set("ir_quote_new", 2, make_check(test_ir_quote_new));
set("ir_quote_current", 2, make_special(special_ir_quote_current));
set("os_chan_new", 1, make_check(test_os_chan_new));
set("ir_is_chan", 1, make_check(test_ir_is_chan));
set("os_chan_put", 3, make_check(test_os_chan_put));
set("os_chan_take", 3, make_check(test_os_chan_take));
set("os_task_create", 2, make_check(test_os_task_create));
set("os_task_list", 1, make_check(test_os_task_list));
set("os_task_alive", 1, make_check(test_os_task_alive));
set("os_task_join", 2, make_check(test_os_task_join));
}