JavaScript "siglib"

         
/**
* 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, make_check, exec_build, exec_unify, check_goal,
get_ctx, VOID_ARGS, launch, check_nonvar,
copy_term, get_engine, setDelay, Compound,
make_error, check_number, narrow_float
} from "../../nova/core.mjs";
/******************************************************************/
/* os_sleep_promise/2 */
/******************************************************************/
/**
* os_sleep_promise(D, P):
* The predicate succeeds in P with a promise for a delay D seconds.
*/
function test_os_sleep_promise(args) {
let obj = exec_build(args[0]);
check_number(obj);
let delay = narrow_float(obj);
if (delay < 0)
throw make_error(new Compound("domain_error",
["not_less_than_zero", obj]));
let delayms = Math.round(delay*1000);
return exec_unify(args[1],
sleep_promise(get_engine(), delayms));
}
function sleep_promise(en, delay) {
return new Promise(resolve => {
function handler() {
en.abort = () => {};
resolve();
}
let timer = setDelay(handler, delay);
en.abort = () => {
clearTimeout(timer);
handler();
};
});
}
/*********************************************************************/
/* os_call_later/3 and timer_cancel/1 */
/*********************************************************************/
/**
* os_call_later(N, D, T): internal only
* The predicate succeeds in T with a new timer. As a side effect
* it schedules the compiled goal N to be executed after D seconds.
*/
function test_os_call_later(args) {
let goal = exec_build(args[0]);
check_goal(goal);
let obj = exec_build(args[1]);
check_number(obj);
let delay = narrow_float(obj);
if (delay < 0)
throw make_error(new Compound("domain_error",
["not_less_than_zero", obj]));
let delayms = Math.round(delay*1000);
let buf = get_ctx();
let id = setDelay(() => callback(goal, buf, VOID_ARGS), delayms);
return exec_unify(args[2], new Timer(id));
}
export function callback(form, buf, paras) {
try {
return launch(form, buf, paras);
} catch (err) {
return false;
}
}
/**
* timer_cancel(T):
* The predicate succeeds. As a side effect it cancels the timer T.
*/
function test_timer_cancel(args) {
let tid = exec_build(args[0]);
check_timer(tid);
clearTimeout(tid.id);
return true;
}
export function check_timer(beta) {
if (!(beta instanceof Timer)) {
check_nonvar(beta);
beta = copy_term(beta);
throw make_error(new Compound("type_error", ["timer", beta]));
}
}
/**
* Create a timer object.
*/
export function Timer(id) {
this.id = id;
}
/*********************************************************************/
/* Sig Lib Init */
/*********************************************************************/
export function main() {
set("os_sleep_promise", 2, make_check(test_os_sleep_promise));
set("os_call_later", 3, make_check(test_os_call_later));
set("timer_cancel", 1, make_check(test_timer_cancel));
}