98 lines
2.9 KiB
C
98 lines
2.9 KiB
C
/*
|
|
* cryo_host.c — the WebAssembly host functions of cryo_host.h, as EM_JS
|
|
* bodies that forward to Module.cryoHost (see wasm-host/ for the JS side).
|
|
*/
|
|
#ifdef HAVE_CONFIG_H
|
|
# include "config.h"
|
|
#endif
|
|
/* built inside php-src (static, as in JumPHP), CRYO_BACKEND_WASM comes from php_config.h: without this
|
|
* include every function below was left out and the engine aborted on the first call ("missing function") */
|
|
#include "php.h"
|
|
|
|
#ifdef CRYO_BACKEND_WASM
|
|
#include <emscripten.h>
|
|
#include "cryo_host.h"
|
|
|
|
int cryo_host_link_anchor(void)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
EM_JS(int32_t, cryo_host_present, (void), {
|
|
return (typeof Module !== 'undefined' && Module.cryoHost) ? 1 : 0;
|
|
});
|
|
|
|
EM_JS(void, cryo_host_restart, (const char *path, uint32_t len), {
|
|
var h = Module.cryoHost;
|
|
var p = UTF8ToString(path, len);
|
|
if (h && h.restart) { h.restart(p); } else { console.warn('[cryo] cryo_restart(' + p + '): no JS host (attachCryoHost)'); }
|
|
});
|
|
|
|
EM_JS(int32_t, cryo_host_clone, (uint32_t mode), {
|
|
var h = Module.cryoHost;
|
|
return h && h.clone ? (h.clone(mode) | 0) : -1;
|
|
});
|
|
|
|
EM_JS(int32_t, cryo_host_spawn, (const uint8_t *opts, uint32_t len), {
|
|
var h = Module.cryoHost;
|
|
return h && h.spawn ? (h.spawn(HEAPU8.slice(opts, opts + len)) | 0) : -1;
|
|
});
|
|
|
|
EM_JS(int32_t, cryo_host_send, (int32_t id, const uint8_t *buf, uint32_t len), {
|
|
var h = Module.cryoHost;
|
|
return h && h.send ? (h.send(id, HEAPU8.slice(buf, buf + len)) | 0) : -1;
|
|
});
|
|
|
|
EM_JS(int32_t, cryo_host_recv, (int32_t id, uint8_t *buf, uint32_t cap, int32_t timeout_ms), {
|
|
var h = Module.cryoHost;
|
|
if (!h || !h.recv) return -1;
|
|
var m = h.recv(id, cap, timeout_ms);
|
|
if (m === null || m === undefined) return -1;
|
|
if (typeof m === 'number') return m | 0;
|
|
var n = Math.min(m.length, cap);
|
|
HEAPU8.set(m.subarray(0, n), buf);
|
|
return n;
|
|
});
|
|
|
|
EM_JS(void, cryo_host_kill, (int32_t id), {
|
|
var h = Module.cryoHost;
|
|
if (h && h.kill) h.kill(id);
|
|
});
|
|
|
|
EM_JS(int32_t, cryo_host_status, (int32_t id), {
|
|
var h = Module.cryoHost;
|
|
if (!h || !h.status) return -1;
|
|
var s = h.status(id);
|
|
return (s === null || s === undefined) ? -1 : (s | 0);
|
|
});
|
|
|
|
EM_JS(int32_t, cryo_host_error, (int32_t id, uint8_t *buf, uint32_t cap), {
|
|
var h = Module.cryoHost;
|
|
if (!h || !h.error) return 0;
|
|
var t = h.error(id);
|
|
if (!t) return 0;
|
|
var b = new TextEncoder().encode(String(t));
|
|
var n = Math.min(b.length, cap);
|
|
HEAPU8.set(b.subarray(0, n), buf);
|
|
return n;
|
|
});
|
|
|
|
EM_JS(void, cryo_host_arm, (uint32_t ms, const uint8_t *frame, uint32_t len), {
|
|
var h = Module.cryoHost;
|
|
if (h && h.arm) h.arm(ms, len ? HEAPU8.slice(frame, frame + len) : null);
|
|
});
|
|
|
|
EM_JS(void, cryo_host_emit, (uint32_t kind, const uint8_t *buf, uint32_t len), {
|
|
var h = Module.cryoHost;
|
|
if (h && h.emit) h.emit(kind, HEAPU8.slice(buf, buf + len));
|
|
});
|
|
|
|
EM_JS(double, cryo_host_now, (void), {
|
|
return (typeof performance !== 'undefined') ? performance.now() : Date.now();
|
|
});
|
|
|
|
EM_JS(void, cryo_host_timer, (uint32_t ms), {
|
|
var h = Module.cryoHost;
|
|
if (h && h.timer) h.timer(ms);
|
|
});
|
|
#endif
|