diff --git a/lib/tyche.h b/lib/tyche.h index e8e25a5..e9cf121 100644 --- a/lib/tyche.h +++ b/lib/tyche.h @@ -35,6 +35,7 @@ TYC_RESULT tyc_load_bytecode(TycheVM* T, uint8_t const* bytecode, size_t bytecod TYC_RESULT tyc_call(TycheVM* t, uint16_t n_pars); // stack manipulation and query +size_t tyc_stack_size(TycheVM* T); void tyc_pushinteger(TycheVM* T, int32_t value); TYC_RESULT tyc_type(TycheVM* T, int idx, TYC_TYPE* type); TYC_RESULT tyc_tointeger(TycheVM* T, int idx, int32_t* value); diff --git a/lib/vm.c b/lib/vm.c index 13168f4..4b0dd76 100644 --- a/lib/vm.c +++ b/lib/vm.c @@ -35,6 +35,11 @@ TYC_RESULT tyc_call(TycheVM* T, uint16_t n_pars) abort(); // TODO } +size_t tyc_stack_size(TycheVM* T) +{ + return stack_len(T->stack); +} + void tyc_pushinteger(TycheVM* T, int32_t value) { stack_push(T->stack, create_value_integer(value)); diff --git a/test/tests.c b/test/tests.c index 8651942..cc0c001 100644 --- a/test/tests.c +++ b/test/tests.c @@ -331,9 +331,9 @@ static void run_assembly_tests(void) lua_call(L, 0, 1); assert(lua_istable(L, -1)); - int len = luaL_len(L, -1); - for (int i = 0; i < len; ++i) { - lua_geti(L, -1, i + 1); + size_t len = (size_t) luaL_len(L, -1); + for (size_t i = 0; i < len; ++i) { + lua_geti(L, -1, (int)i + 1); run_assembly_test(L); lua_pop(L, 1); } @@ -343,7 +343,41 @@ static void run_assembly_tests(void) static void run_assembly_test(lua_State* L) { + TycheVM* T = tyc_new(); + + // print test name lua_getfield(L, -1, "name"); printf(" - %s\n", lua_tostring(L, -1)); - lua_pop(L, -1); + lua_pop(L, 1); + + // load code + uint8_t* bytecode; size_t bytecode_sz; + lua_getfield(L, -1, "code"); + assert(code_assemble(lua_tostring(L, -1), &bytecode, &bytecode_sz) == T_OK); + lua_pop(L, 1); + + // run code + assert(tyc_load_bytecode(T, bytecode, bytecode_sz) == T_OK); + assert(tyc_call(T, 0) == T_OK); + + // check stack size + lua_getfield(L, -1, "expected_stack_size"); + if (!lua_isnil(L, -1)) + assert(tyc_stack_size(T) == (size_t) lua_tointeger(L, -1)); + lua_pop(L, 1); + + // check stack top + lua_getfield(L, -1, "expected_stack_size"); + if (lua_isinteger(L, -1)) { + int32_t v; + assert(tyc_tointeger(T, -1, &v) == T_OK); + assert(v == lua_tointeger(L, -1)); + } + else if (!lua_isnil(L, -1)) + abort(); + lua_pop(L, 1); + + // cleanup + free(bytecode); + tyc_destroy(T); } \ No newline at end of file