D scripting (c++ integration)

Georg Wrede georg.wrede at nospam.org
Tue Oct 3 10:44:14 PDT 2006


mike wrote:
> Maybe for DMD 3.0 ...
> 
> ' import std.compiler;
> '
> ' char[] source = "writefln(\"Hello World!\");";
> ' void delegate() foo = compile(source);
> ' foo();
> 
> That would be just awesome :-)
> 
> - mike

In addition to all the obvious, I'd say this is yet another thing that's 
not doable in C++. Compiling anything takes simply too long. But D, it's 
totally different.

And especially where the same program keeps compiling different stuff, 
then speed would become quite fast since the compiler wouldn't be 
discarded from memory.

"Blurring" this way the distinction and chasm between runtime and 
compile time should give us quite interesting new possibilities and avenues.

>> 1) Launch the D compiler from within your app directly, to compile 
>> the  "script" you want to run - std.process works well.
>> 2) Use DDL to bind the .obj file from the compiler output.  The 
>> linker  will return a DynamicLibrary object to use.
>> 3) Use the DynamicLibrary to bind any functions, classes, or static  
>> fields that you know to exist in the compiled script.
>>
>> That last point is kind of tricky.  While you can simply force script  
>> coders to adhere to a particular interface, I found it easier to wrap  
>> the whole script in a function (call that step #0) before 
>> compilation.  This ensures that the starting point of the script was 
>> always known, at  the cost of disallowing some D constructs 
>> (bang-line, imports, module  static init, etc).  It also allows the 
>> script author to start from line  1, much like one can in PHP or 
>> JavaScript.
>>
> 
> 
> 



More information about the Digitalmars-d mailing list