Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Samples

Using Lua with Ziron

A community discussion post from the original code:zIRON forum.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Using Lua with Ziron

The latest release of Ziron 2.0.0.11 contains a lua include (experimental)

You will need to download the Lua DLL

Here is a quick example of loading a lua script:

program PE32_CUI 'test';

#include 'win_def.zir';
#include 'console.zir';

#include 'lua/lua.zir';

lua_state* L;

lua_loadlib('lua52-32.dll');
L = luaL_newstate();
luaL_openlibs(L); 

eax = luaL_loadfile(L, 'script.lua', nil);
if (eax != 0) {
	ecx = lua_tostring(L, -1);
	print('Couldn\'t load file: ',[char*]ecx,'\n');
} else {
	// Ask Lua to run our little script
	eax = lua_pcall(L, 0, LUA_MULTRET, 0);
	if (eax != 0) {
		ecx = lua_tostring(L, -1);
		print('Failed to run script: ',[char*]ecx,'\n');
	} else {
		lua_pop(L, 1);  // Take script return value out of the stack
	}
}

lua_close(L); 
lua_freelib();

wait_key();
ExitProcess(0);

A very super simple script:

--
-- Sample lua script for Ziron
--

print("Hello World!");

n = 1
while n <= 20000 do
	print("Hello, World "..n.."!");
	n = n + 1
end;

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

registering a function:

program PE32_CUI 'test';

#include 'win_def.zir';
#include 'console.zir';

#include 'lua/lua.zir';


function LUA_API printSomething(lua_State* L) {
	print('Hello World from Ziron Function!\r\n');
	
	// return the number of results
	return 0;
}

lua_state* L;

lua_loadlib('lua52-32.dll');
L = luaL_newstate();
luaL_openlibs(L);

/* register our functions */
lua_register(L, 'printSomething', @printSomething); 

eax = luaL_loadfile(L, 'script.lua', nil);
if (eax != 0) {
	ecx = lua_tostring(L, -1);
	print('Couldn\'t load file: ',[char*]ecx,'\n');
} else {
	// Ask Lua to run our little script
	eax = lua_pcall(L, 0, LUA_MULTRET, 0);
	if (eax != 0) {
		ecx = lua_tostring(L, -1);
		print('Failed to run script: ',[char*]ecx,'\n');
	} else {
		lua_pop(L, 1);  // Take script return value out of the stack
	}
}

lua_close(L); 
lua_freelib();

wait_key();
ExitProcess(0);

then your lua script can append:

-- call Ziron Function
printSomething();