signals and component calls

This commit is contained in:
2026-02-05 10:47:57 +01:00
parent 077b2eb4e2
commit e5095f4624
3 changed files with 166 additions and 6 deletions

View File

@@ -6,12 +6,19 @@
#include <stdio.h>
static nn_Exit sandbox_handler(nn_ComponentRequest *req) {
nn_Computer *c = req->computer;
switch(req->action) {
case NN_COMP_INIT:
return NN_OK;
case NN_COMP_DEINIT:
return NN_OK;
case NN_COMP_CALL:
if(nn_getstacksize(c) != 1) {
nn_setError(c, "bad argument count");
return NN_EBADCALL;
}
const char *s = nn_tostring(c, 0);
puts(s);
return NN_OK;
case NN_COMP_ENABLED:
req->methodEnabled = true; // all methods always enabled
@@ -29,19 +36,21 @@ int main() {
nn_ComponentMethod sandboxMethods[] = {
{"log", "log(msg: string) - Log to stdout", true},
NULL,
{NULL},
};
nn_ComponentType *ctype = nn_createComponentType(u, "sandbox", NULL, sandboxMethods, sandbox_handler);
nn_Computer *c = nn_createComputer(u, NULL, "computer0", 8 * NN_MiB, 256, 256);
nn_addComponent(c, ctype, "sandbox", -1, NULL);
nn_pushstring(c, "Hello from component call");
nn_call(c, "sandbox", "log");
printf("Component added: %s\n", nn_hasComponent(c, "sandbox") ? "TRUE" : "FALSE");
printf("Method active: %s\n", nn_hasMethod(c, "sandbox", "log") ? "TRUE" : "FALSE");
printf("Incorrect method active: %s\n", nn_hasMethod(c, "sandbox", "notlog") ? "TRUE" : "FALSE");
cleanup:;
nn_destroyComputer(c);
nn_destroyComponentType(ctype);
// rip the universe