JavaScript "xbarlib"

         
/**
* 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, deref, is_structure, make_check, MAX_ARITY,
check_nil, exec_build, exec_unify, Compound,
norm_smallint, make_error, get_engine,
check_integer, check_nonvar, copy_term
} from "../../nova/core.mjs";
import {
Crossbar, cpu_xbar_put, cpu_xbar_take,
cpu_xbar_unput, cpu_xbar_untake,
cpu_xbar_offer, MAX_DATA, cpu_xbar_poll
} from "./sync.mjs";
/******************************************************************/
/* CPU Integer Arrays */
/******************************************************************/
/**
* test_cpu_buf_new(A, B): internal only
* The predicate succeeds in H with the buffer from the list D.
*/
function test_cpu_buf_new(args) {
let alpha = exec_build(args[0]);
let res = cpu_list_ints(alpha);
return exec_unify(args[1], res);
}
function cpu_list_ints(obj) {
let peek = obj;
let i = 0;
while (is_structure(peek) &&
"." === peek.functor &&
peek.args.length === 2 &&
i < MAX_ARITY) {
i++;
peek = deref(peek.args[1]);
}
check_nil(peek);
if (typeof SharedArrayBuffer === 'undefined')
throw make_error(new Compound("resource_error", ["shared_missing"]));
let buf = new SharedArrayBuffer(i * 4);
let args = new Int32Array(buf);
peek = obj;
i = 0;
while (is_structure(peek) &&
"." === peek.functor &&
peek.args.length === 2) {
let val = deref(peek.args[0]);
check_integer(val);
args[i++] = int32(val);
peek = deref(peek.args[1]);
}
return args;
}
export function int32(val) {
if (typeof val === 'number') {
return val | 0;
} else {
return Number(BigInt.asIntN(32, val));
}
}
/**
* test_cpu_buf_current(A, B): internal only
* The predicate succeeds in D with the list from the buffer H.
*/
function test_cpu_buf_current(args) {
let alpha = exec_build(args[0]);
let res = cpu_ints_list(alpha, 0, alpha.length);
return exec_unify(args[1], res);
}
function cpu_ints_list(alpha, off, count) {
let res = "[]";
for (let i = off + count - 1; i >= off; i--) {
res = new Compound(".", [norm_smallint(alpha[i]), res]);
}
return res;
}
/******************************************************************/
/* CPU Crossbar Interface */
/******************************************************************/
/**
* test_cpu_xbar_new(A): internal only
* The predicate succeeds in A with a new xbar.
*/
function test_cpu_xbar_new(args) {
if (typeof SharedArrayBuffer === 'undefined')
throw make_error(new Compound("resource_error", ["shared_missing"]));
let buf = new SharedArrayBuffer((3+MAX_DATA)*4);
let arr = new Int32Array(buf);
let res = new Crossbar(arr, 0);
return exec_unify(args[0], res);
}
/**
* test_cpu_xbar_put(C, H, P): internal only
* The predicate succeeds in P with a promise that
* sends the buffer H to the xbar C.
*/
function test_cpu_xbar_put(args) {
let xbar = exec_build(args[0]);
check_xbar(xbar);
let data = exec_build(args[1]);
return exec_unify(args[2],
cpu_put_promise(get_engine(), xbar, data));
}
async function cpu_put_promise(en, xbar, data) {
en.abort = () => cpu_xbar_unput(xbar);
try {
await cpu_xbar_put(xbar, data, 0, data.length, en);
} finally {
en.abort = () => {};
}
}
/**
* test_cpu_xbar_take(C, M, P): internal only
* The predicate succeeds in P with a promise that
* receives a buffer into the map M from the xbar C.
*/
function test_cpu_xbar_take(args) {
let xbar = exec_build(args[0]);
check_xbar(xbar);
let map = exec_build(args[1]);
return exec_unify(args[2],
cpu_take_promise(get_engine(), xbar, map));
}
async function cpu_take_promise(en, xbar, map) {
en.abort = () => cpu_xbar_untake(xbar);
try {
let len = map.get("len");
let data = new Array(len);
let res = await cpu_xbar_take(xbar, data, 0, len, en);
if (res !== len) {
map.set("len", res);
} else {
map.set("data", data);
}
} finally {
en.abort = () => {};
}
}
/**
* test_cpu_xbar_offer(C, H): internal only
* The predicate succeeds when it can send
* the buffer H to the xbar C.
*/
function test_cpu_xbar_offer(args) {
let xbar = exec_build(args[0]);
check_xbar(xbar);
let data = exec_build(args[1]);
return cpu_xbar_offer(xbar, data, 0, data.length);
}
/**
* test_cpu_xbar_poll(C, M): internal only
* The predicate succeeds with it can receives
* a buffer into the map M from the xbar C.
*/
function test_cpu_xbar_poll(args) {
let xbar = exec_build(args[0]);
check_xbar(xbar);
let map = exec_build(args[1]);
let len = map.get("len");
let data = new Array(len);
let res = cpu_xbar_poll(xbar, data, 0, len);
if (res === -1)
return false;
if (res !== len) {
map.set("len", res);
} else {
map.set("data", data);
}
return true;
}
/******************************************************************/
/* CPU Crossbar Arrays */
/******************************************************************/
/**
* test_cpu_comm_new(A, B): internal only
* The predicate succeeds in H with the table from the list D.
*/
function test_cpu_comm_new(args) {
let alpha = exec_build(args[0]);
let res = cpu_list_comm(alpha);
return exec_unify(args[1], res);
}
function cpu_list_comm(obj) {
let peek = obj;
let i = 0;
while (is_structure(peek) &&
"." === peek.functor &&
peek.args.length === 2 &&
i < MAX_ARITY) {
i++;
peek = deref(peek.args[1]);
}
check_nil(peek);
let args = new Array(i);
peek = obj;
i = 0;
while (is_structure(peek) &&
"." === peek.functor &&
peek.args.length === 2) {
let val = deref(peek.args[0]);
check_xbar(val);
args[i++] = val;
peek = deref(peek.args[1]);
}
return args;
}
function check_xbar(beta) {
if (!(beta instanceof Crossbar)) {
check_nonvar(beta);
beta = copy_term(beta);
throw make_error(new Compound("type_error",
["xbar", beta]));
}
}
/******************************************************************/
/* Chan Lib Init */
/******************************************************************/
export function main() {
set("cpu_buf_new", 2, make_check(test_cpu_buf_new));
set("cpu_buf_current", 2, make_check(test_cpu_buf_current));
set("cpu_xbar_new", 1, make_check(test_cpu_xbar_new));
set("cpu_xbar_put", 3, make_check(test_cpu_xbar_put));
set("cpu_xbar_take", 3, make_check(test_cpu_xbar_take));
set("cpu_xbar_offer", 2, make_check(test_cpu_xbar_offer));
set("cpu_xbar_poll", 2, make_check(test_cpu_xbar_poll));
set("cpu_comm_new", 2, make_check(test_cpu_comm_new));
}