Published 2007-05-20 11:43:00
import dtest; <<< Import the file/module that has your init code..
void compile() {
program = new Program();
Dtest_init(program.callcontext); <<<< Added.
program.compile(srcfile, buffer, null);
}
// register all the classes in this file..
static void Dtest_init(CallContext* cc) {
ThreadContext *tc = ThreadContext.getThreadContext();
// add constructor and prototype to our global list
// used so we can quickly access prototypes by looking the up.
// NOTE: you must make the constructor first, as the prototype
// this() method looks up on the ctorTable..
tc.ctorTable["test"] = new Dtest_Constructor(tc);
tc.protoTable["test"] = new Dtest_prototype(tc);
// Now actually register the WORD 'test' in the global namespace
cc.global.Put("test", tc.ctorTable["test"], DontEnum);
// add "test.prototype"
tc.ctorTable["test"].Put("prototype", tc.protoTable["test"],
DontEnum | DontDelete | ReadOnly);
// THIS IS FOR "new test.test()"
// first just store the constructor and prototype in the lookup table.
// AFAIK It's only put here so it can be quickly retrieved later..
// so the index name is not really that important.
tc.ctorTable["test.test"] = new Dtest_test_Constructor(tc);
tc.protoTable["test.test"] = new Dtest_test_prototype(tc);
// add the prototype "test.test.prototype.*"
tc.ctorTable["test.test"].Put("prototype", tc.protoTable["test.test"],
DontEnum | DontDelete | ReadOnly);
// now add the constructor to "test.test" , basically it adds the
// "test" property to the "test" constructor object.
tc.ctorTable["test"].Put("test", tc.ctorTable["test.test"] , 0);
}
class Dtest_Constructor : Dfunction
{
// register static methods..
this(ThreadContext *tc)
{
super(7, tc.Dfunction_prototype);
this.name = "test";
this.Put("testworld2",
new DnativeFunction(
&Dtest.testworld2, "testworld2", 0, Dfunction.getPrototype()
), 0);
}
// our constructor - called when new test() is called, must return an
// object. of type Dobject in the ret Value.;
void *Construct(CallContext *cc, Value *ret, Value[] arglist)
{
Dobject o;
o = new Dtest(); // can this be automated??
ret.putVobject(o);
return null;
}
}
test.prototype.constructor()and all the prototype properties.
test.prototype.testworld()Which can also be used as
var a = new test; a.testworld();or
test.prototype.testworld.call(new test());and on and on....
class Dtest_prototype : Dtest
{
// we register all our methods in here..
this(ThreadContext *tc)
{
super(tc.Dobject_prototype); // which assigns classname
this.Put(TEXT_constructor, tc.ctorTable[this.classname], DontEnum);
this.Put("testworld",
new DnativeFunction(
&Dtest.testworld, "testworld", 0, Dfunction.getPrototype()
), 0);
this.Put("query",
new DnativeFunction(
&Dtest.query, "query", 0, Dfunction.getPrototype()
), 0);
}
}
class Dtest: DobjectNext Define all the static and instance methods (they are pretty much the same, and you can use the same real method to implement both for javascript)
{
Next the general constructor used by the prototype extension call.
// our instance methods..
static void* testworld( Dobject pthis, CallContext* cc, Dobject othis, Value* ret, Value[] arglist)
{
ret.putVstring("Hello World");
return null;
}
static void* query( Dobject pthis, CallContext* cc, Dobject othis, Value* ret, Value[] arglist)
{
int ret = mysql_query((cast(Dtest)othis).con, "select * from test");
return null;
}
// our static methods..
static void* testworld2( Dobject pthis, CallContext* cc, Dobject othis, Value* ret, Value[] arglist)
{
ret.putVstring("Hello World2");
return null;
}
this(Dobject prototype)
{
super(prototype);
this.classname = "test";
}
Mysqlconnection con;
this() // called when new Object is created...
{
ThreadContext *tc = ThreadContext.getThreadContext();
super( tc.protoTable["test"]);
this.classname = "test";
mysql_connect(con);
}
}
import dmdscript.dobject;
import dmdscript.value;
import dmdscript.script;
import dmdscript.dnative;
import dmdscript.property;
import dmdscript.threadcontext;
import dmdscript.dfunction;
import dmdscript.text;