Tuesday
19
Jun 2007

Why do we blame the tools before fully checking our own code?

(12:57 pm) Tags: [Software, Rants]

Do we think that our code is just cleaner somehow? That we possibly couldn’t make an error that stupid? Guess what, if you think that, I can almost guarantee that you did :)

Popularity: 22%

Comments: (0)

Disable HTTP TRACE method in Tomcat

(10:19 am) Tags: [Software, Sysadmin]

This one is also quite easy, simply edit $TOMCAT/conf/server.xml, and for the <connector> element, add an attribute: allowTrace="false". Restart Tomcat and enjoy.

Popularity: 23%

Comments: (0)

Disable directory listings in Tomcat

(8:20 am) Tags: [Software, Sysadmin]

Found this from Tomcat 5 on Linux Step by Step. Simply edit the server’s conf/web.xml, and change the DefaultFileServlets listing init parameter from true to false:

<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>

Popularity: 23%

Comments: (0)
Monday
18
Jun 2007

dlua GC API done

(5:29 pm) Tags: [Software, Projects, D Programming Language]

Just finished the GC API for the OO wrapper of dlua. I think the OO paradigm is really starting to show well.

An example:

import lua.wrap.lua;

import tango.io.Console;
import tango.io.Stdout;

void main()
{
Lua lua = new Lua();
Stdout.formatln (”init:\t{} bytes of memory”, lua.gc.memoryBytesUsed);
lua.execute(”print(’Hello World!’)”);
Stdout.formatln (”pre-gc:\t{} bytes of memory”, lua.gc.memoryBytesUsed);
lua.gc.stop();
lua.gc.restart();
Stdout.formatln (”start:\t{} bytes of memory”, lua.gc.memoryBytesUsed);
lua.gc.full();
Stdout.formatln (”post:\t{} bytes of memory”, lua.gc.memoryBytesUsed);
}

Output from that program on my Windows machine:

D:\d\dlua\examples\wrap>GCExample
init: 19254 bytes of memory
Hello World!
pre-gc: 19577 bytes of memory
start: 19577 bytes of memory
post: 19093 bytes of memory

As you can see, Lua is fairly memory efficient, and also code efficient, weighing in at about 30K of overhead compared to a basic “Hello World” D application, 132K vs 162K. To be fair, including Lua also means you are distributing a 132K DLL for the real Lua functionality.

Lua is a good fit for the D Programming Language, as shown here.

Popularity: 22%

Comments: (0)
Saturday
16
Jun 2007

Lua D bindings have a home

(7:51 pm) Tags: [Software, Projects, D Programming Language]

… I was just uninformed. Anders has created a fantastic set of the C bindings in D over at Google code: dlua.

The module lua.c is the original C bindings ported to the D Programming Language, changing as little as possible. The module lua.wrap contains an Object-Oriented D wrapper to the C bindings, and allows simple interaction without knowing the C Lua API. Both lua.c and lua.wrap are tango and phobos compatible, and should work on any platform for which D and Lua are available. A simple example:

private import lua.wrap.lua;

version (Tango)
import tango.io.Console;
else
import std.stdio : Cout = writef;

extern (C) int testfunction(State L)
{
Cout(”Hello from D!\n”);
return 0;
}

void main()
{
Lua lua = new Lua();
lua.register(&testfunction, “testfunction”);
lua.execute(”testfunction()”);
}

The main() function is the bit to look at. The intention is to have a simple, powerful wrapper for the Lua engine so that D developers can expose scripting functionality in their own code.

I am currently testing out how to add runtime reflection into the wrapper, so you can just register an object and have it operable in the Lua engine, without having to register each member and function.

Popularity: 23%

Comments: (0)
Thursday
14
Jun 2007

Lua embedded in D - Simple First Example

(8:54 am) Tags: [Software, Projects, D Programming Language]

I have been looking for a scripting engine to embed in my D code, and since DMDScript was GPL, I needed something different.

Enter Lua.

After trying to find the right binary library, I finally got the Simple Lua API Example working in the D Programming Language.

So far, here is the header conversion that you need to make the example work:

module lua;

extern(C):

version (Windows) {
pragma (lib, “lua.lib”);
//extern (Windows):
} else {
pragma (msg, “You will need to manually link in the Lua library.”);
}

int LUA_MULTRET = -1;
int LUA_GLOBALSINDEX = -10002;

alias void lua_State;

extern (C) void lua_close (lua_State *L);
extern (C) lua_State *luaL_newstate();//lua_open()
extern (C) void luaL_openlibs(lua_State *L);
extern (C) int luaL_loadfile(lua_State *, char *);
extern (C) void lua_createtable(lua_State *, int=0, int=0);
extern (C) void lua_pushnumber(lua_State *, double=0.0);
extern (C) void lua_rawset(lua_State *, int);
extern (C) void lua_setfield(lua_State *, int, char *);
extern (C) int lua_pcall(lua_State *, int, int, int);
extern (C) double lua_tonumber(lua_State *, int);
extern (C) int lua_gettop(lua_State *);
extern (C) void lua_settop(lua_State *, int);

void lua_setglobal(lua_State *L, char* s) {
lua_setfield(L, LUA_GLOBALSINDEX, s);
}

void lua_pop(lua_State *L, int n=0) {
lua_settop(L, -(n)-1);
}

And here is the C example converted to D:

private import tango.util.log.Configurator, tango.util.log.Log;
private import tango.text.convert.Sprint;
private import lua;

void main() {
Configurator();
testSimple;
}

void testSimple() {
auto log = Log.getLogger(”testlua.testsimple”);
auto sprint = new Sprint!(char);

lua_State *L;

//AKA - L = lua_open();
L = luaL_newstate();

luaL_openlibs(L);

int status;

status = luaL_loadfile(L, “script.lua”);
log.info(sprint(”loadfile status: {}”, status));

/*
* Ok, now here we go: We pass data to the lua script on the stack.
* That is, we first have to prepare Lua’s virtual stack the way we
* want the script to receive it, then ask Lua to run it.
*/
log.info(”new table”);
lua_createtable(L);

/*
* To put values into the table, we first push the index, then the
* value, and then call lua_rawset() with the index of the table in the
* stack. Let’s see why it’s -3: In Lua, the value -1 always refers to
* the top of the stack. When you create the table with lua_newtable(),
* the table gets pushed into the top of the stack. When you push the
* index and then the cell value, the stack looks like:
*
* < - [stack bottom] -- table, index, value [top]
*
* So the -1 will refer to the cell value, thus -3 is used to refer to
* the table itself. Note that lua_rawset() pops the two last elements
* of the stack, so that after it has been called, the table is at the
* top of the stack.
*/
log.info("load the table");
for (int i = 1; i <= 5; i++) {
lua_pushnumber(L, i); /* Push the table index */
lua_pushnumber(L, i*2); /* Push the cell value */
lua_rawset(L, -3); /* Stores the pair in the table */
}

/* By which name is the script going to reference our table ? */
lua_setglobal(L, "foo");

/* Ask Lua to run our little script */
int result = lua_pcall(L, 0, LUA_MULTRET, 0);
log.info(sprint("pcall() status: {}", result));

/* Get the returned value at the top of the stack */
double sum = lua_tonumber(L, lua_gettop(L));
log.info(sprint("sum is: {}", sum));

lua_pop(L, 1); /* Take the returned value out of the stack */

log.info("Lua SimpleTest complete.");

/* Remember to destroy the Lua State */
lua_close(L);

}

Put your lua.lib and lua.dll in the appropriate places, and build:

D:\d\sc\lua>dsss clean

D:\d\sc\lua>dsss build testlua.d
testlua.d => testlua
+ D:\d\dsss-0.52-dmd-win\bin\rebuild.exe -Idsss_imports\ -I. -S.\ -ID:\d\dsss-0.52-dmd-win\include\d -SD:\d\dsss-0.52-dmd-win\lib\ -oqdsss_objs testlua.d -oftestlua
d:\d\dm\bin\link.exe dsss_objs\testlua+dsss_objs\tango-util-log-Configurator+dsss_objs\tango-util-log-Log+dsss_objs\tango-util-log-Logger+dsss_objs\tango-util-log-Appender+dsss_objs\tango-util-log-Event+dsss_objs\tango-sys-Common+dsss_objs\tango-util-time-Clock+dsss_objs\tango-util-time-Date+dsss_objs\tango-core-Type+dsss_objs\tango-util-log-model-ILevel+dsss_objs\tango-util-log-model-IHierarchy+dsss_objs\tango-util-log-Layout+dsss_objs\tango-uti
l-log-Hierarchy+dsss_objs\tango-util-log-ConsoleAppender+dsss_objs\tango-io-Console+dsss_objs\tango-io-Buffer+dsss_objs\tango-io-model-IBuffer+dsss_objs\tango-io-model-IConduit+dsss_objs\tango-io-DeviceConduit+dsss_objs\tango-io-Conduit+dsss_objs\tango-text-convert-Sprint+dsss_objs\tango-text-convert-Layout+dsss_objs\tango-text-convert-Utf+dsss_objs\tango-text-convert-Float+dsss_objs\tango-text-convert-Integer+dsss_objs\lua,testlua,,user32+kernel
32/noi+.\\+D:\d\dsss-0.52-dmd-win\lib\\;

And now run the example:

D:\d\sc\lua>testlua
1 INFO testlua.testsimple - loadfile status: 0
1 INFO testlua.testsimple - new table
1 INFO testlua.testsimple - load the table
The table the script received has:
1 2
2 4
3 6
4 8
5 10
Returning data back to C
3 INFO testlua.testsimple - pcall() status: 0
3 INFO testlua.testsimple - sum is: 30.00
4 INFO testlua.testsimple - Lua SimpleTest complete.

Popularity: 23%

Comments: (0)