aboutsummaryrefslogtreecommitdiff
path: root/src/op/if.c
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-11-10 23:47:10 +0100
committerXavier Del Campo Romero <xavi92@disroot.org>2025-11-10 23:47:10 +0100
commitba623c7a3a5b450b9b492de50efceea93df96039 (patch)
tree92b0d70db2fb304bac604338215401e25e8c4aac /src/op/if.c
parent806824b23c28d2d81ffd588c555be82fa5a14004 (diff)
downloadnanowasm-ba623c7a3a5b450b9b492de50efceea93df96039.tar.gz
Add if and else operators
Diffstat (limited to 'src/op/if.c')
-rw-r--r--src/op/if.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/op/if.c b/src/op/if.c
new file mode 100644
index 0000000..2ef9565
--- /dev/null
+++ b/src/op/if.c
@@ -0,0 +1,43 @@
+/*
+ * nanowasm, a tiny WebAssembly/Wasm interpreter
+ * Copyright (C) 2023-2025 Xavier Del Campo Romero
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#include <nanowasm/nw.h>
+#include <nw/interp.h>
+#include <nw/io.h>
+#include <nw/ops.h>
+#include <nw/routines.h>
+#include <nw/stack.h>
+#include <nw/types.h>
+
+static enum nw_state pop(struct nw_interp *const i)
+{
+ struct nw_i_sm_if *const pif = &i->sm.sm_if;
+ const enum nw_state n = nwp_stack_pop(i, &pif->io);
+
+ if (n)
+ return n;
+ else if (pif->condition)
+ nwp_interp_resume(i);
+ else
+ nwp_break(i, 0);
+
+ return NW_AGAIN;
+}
+
+void nwp_op_if(struct nw_interp *const i)
+{
+ static const struct nw_i_sm_if sm_if;
+ struct nw_i_sm_if *const pif = &i->sm.sm_if;
+ struct nw_sm_io *const io = &pif->io;
+
+ *pif = sm_if;
+ io->buf = &pif->condition;
+ io->n = sizeof pif->condition;
+ nwp_start_block(i, &pif->sb, pop);
+}