#ifndef LUA2WASM_CODEGEN_H
#define LUA2WASM_CODEGEN_H

#include "parser.h"
#include "wat_builder.h"

/* tree_shake: when nonzero, only emit globals / elem-declare / _G
 * registrations for builtins referenced by the AST. Lets wasm-opt
 * drop the function bodies that no longer have a ref.func keeping
 * them live. Costs: `_G.print` access fails if user code never names
 * `print`. Default off.
 *
 * opt: optimization level. 0 (-O0) is the boxed fallback — every Lua value
 * is a host-GC object and arithmetic goes through generic dispatch. >=1 (the
 * default) enables numeric/call specialization: int/float slot unboxing,
 * typed direct-call entries, and comparison specialization. Behaviour is
 * identical across levels; only the emitted code shape and speed differ.
 *
 * embed_api: when nonzero, emit the host-call ABI — a block of exported thunks
 * (lua_str_new/lua_str_setb, lua_get_global, lua_args_new/set/get/len,
 * lua_call, lua_pcall, lua_table_new/get/set/len) that let an embedder build
 * Lua values and tables, look up globals by name, and invoke Lua functions
 * (raw or protected) from outside the module. Because that ABI can reach any
 * global and call anything, it forces the whole stdlib live and keeps
 * $g_globals populated (i.e. it disables tree-shaking and the skip-runtime-init
 * optimization for this module). Off by default; opt-in via the CLI's
 * --embed-api. See examples/embed/. */
int codegen_module(const ParseResult *pr, const char *src_name,
                   int tree_shake, int opt, int embed_api, WatBuilder *out,
                   char *errbuf, size_t errbuf_len);

#endif
