luajit-ffi

so so at so.so
Tue May 1 13:21:21 PDT 2012


On Tuesday, 1 May 2012 at 16:56:48 UTC, Alex Rønne Petersen
wrote:

>>> Yes, creating manual bindings is tedious and annoying to 
>>> maintain, but
>>> it is the most foolproof approach.
>>
>> In another language there may not be other exit but D, i am 
>> not sure.

As it happens, you are safe with D even if you pick that path.
With my limited knowledge, these a few lines of code destroys
all lua binders out there. Make sure they are inlined and it
would be identical to handcrafted bindings.

WARNING: DO NOT TRY THIS WITH C++! (See the "alias F"?
ParamTuple? Yep)

import std.stdio;
import std.traits;

struct handle {
	int i=0;
	int get_arg() {
		return i++;
	}
}

alias handle* hnd;

void assign(uint N, P...)(ref P p, hnd h)
{
	static if(N)
	{
		p[N-1] = h.get_arg();
		assign!(N-1)(p, h);
	}
}

int fn(alias F)(hnd h)
{
	alias ParameterTypeTuple!(typeof(&F)) P;
	P p;

	assign!(P.length)(p, h);
	F(p);
	return 0;
}

// test >>>>
void fun0(int a)
{
	writeln("fun0 a: ", a);
}

void fun1(int a, int b)
{
	writeln("fun1 a: ", a);
	writeln("fun1 b: ", b);
}

void main()
{
// 	reg(&fn!fun0, "fun0");
// 	reg(&fn!fun1, "fun1");
	auto f0 = &fn!fun0;
	auto f1 = &fn!fun1;
	handle h;
	f0(&h);
	f1(&h);
}
// test <<<<


More information about the Digitalmars-d mailing list