D run-time interpretation

Vladimir Panteleev via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 6 15:16:46 PDT 2015


On Tuesday, 6 October 2015 at 19:42:08 UTC, Jacob wrote:
> There are many programs out there that use some sort of 
> scripting capabilities.
>
> I've been wanting to write an app that exposes a scripting like 
> environment D, lua, or some other fast language to the user.
>
> But there are two requirements:
>
> 1. The user has access to all objects created in app. e.g., if 
> I create a class X in the app then the user can instantiate X 
> in the script without having to recreate it. (the script then, 
> is sort of an extension of the app)
>
> I don't mind actually having to expose X to the script, but it 
> should be easy.
>
> 2. It should be fast. When it is "recompiled" it shouldn't take 
> more than a few seconds. It also shouldn't require a 
> re-initialization of everything.
>
> Can D do this easily?

I can see two ways about this:

1. Use D

Can you include your application's source code with the 
application?

If so, what I think could work is simply import modules belonging 
to your app from your plugin's source code, but don't link with 
the app object files.

The main complication with this approach is that you would need 
to either include a D compiler with your app, or expect the user 
to have one installed on their system. Note that DMD is not 
redistributable (you can't include DMD with your program), but 
this doesn't apply to LDC/GDC.

This might also get tricky on Windows, as you'll probably need to 
create or generate a .DEF file (and accompanying import library) 
which lists all exports/imports shared between your program and 
plugins. This will include all class methods as mangled names.

D compile times are quite low already, so I don't think this 
would be an issue.

2. Use a scripting language, e.g. Lua

D's compile-time reflection and code-generation abilities 
certainly make it possible to expose functions and classes to 
scripts. You could add a mixin to an exposed class, which would 
generate all the necessary run-time type information to expose 
the class to the scripting language. I don't know if any existing 
library already provides this in an easy-to-use manner, though.

Perhaps a good starting point would be LuaD:
https://github.com/JakobOvrum/LuaD



More information about the Digitalmars-d mailing list