My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
FunctionExample  
Simple example that shows how to register a D function in Lua
Phase-Implementation, Featured
Updated Feb 4, 2010 by ande...@gmail.com

Introduction

This is going to show you how you can use DLua to register a simple function in Lua. More advanced examples will come later.

Code

test.d

import dlua.all;
import std.stdio;

extern (C) int testfunction(lua_State* L)
{
    writefln("Hello from D!");
    return 0;
}

void main()
{
    lua_State* L = luaL_newstate();
    luaL_openlibs(L);
    lua_register(L, "testfunction", &testfunction);
    luaL_dostring(L, `testfunction()`);
    lua_close(L);
}
Comment by duronthe...@gmail.com, Oct 7, 2007

Thank you for this cool bindings. But there is one mistake in this "FunctionExample".

Line "lua_register(L, "testfunction", &testfunction);"

has to be changed to

lua_register(L, "testfunction", cast(int(*)(void** L))&testfunction);

otherwise I get the following error (in Linux; Distri : Ubuntu Gutsy Gibbon Beta:

test.d:14: function test.testfunction (void) does not match parameter types () test.d:14: Error: expected 1 arguments, not 0 test.d:14: function dlua.lua.lua_register (void,char,int()(void L)) does not match parameter types (void,char12u?,int) test.d:14: Error: cannot implicitly convert expression (testfunction()) of type int to int()(void L)


Sign in to add a comment
Powered by Google Project Hosting