Is this D or is it Javascript?
Adam D. Ruppe
destructionator at gmail.com
Fri Jul 5 17:21:53 PDT 2013
On Friday, 5 July 2013 at 08:26:23 UTC, Rory McGuire wrote:
> Just need a decent wrapper on phobos to fix the naming of some
> stuff.
That's fairly easy too:
import arsd.jsvar;
import arsd.script;
void main() {
var globals = var.emptyObject;
{
import std.algorithm;
var Math = globals.Math = var.emptyObject;
Math.max = &std.algorithm.max!(var, var);
// testing it in D
import std.stdio;
// the second () is just because @property is
broken
writeln(Math.max()("12", 24)); // prints 24
}
{
import std.file;
var File = globals.File = var.emptyObject;
File.readText = &readText!string;
}
import std.stdio;
writeln(interpret("Math.max(14, 33);", globals)); //
prints 33
writeln(interpret("File.readText(\"test29.d\");",
globals)); // prints this file's source code
}
The opAssign function can wrap native functions pretty much
automatically. The one thing, as you can see here, is they do
need to be functions, not templates, so you might have to
instantiate them all with some type. But that's no big deal.
I don't remember if I mentioned this or not too, but the opAssign
and .get!T functions also can go to and from structs. Only the
data members, the methods won't work since the this pointer for
them would point to a var instead of the real type it expects,
but the data members and pulled/filled in by name, coercing types
as needed.
Anyway, if you did all these assignments, then you can make it
all available with dynamic calling from D or from the script
interpreter, without manually wrapping them.
It shouldn't be too hard to do a __traits(allMembers) over a
module too if you wanted to pull it all in at once, perhaps even
automatically instantiating the templates. But it would only work
especially well for free functions, again, due to the delegate
this problem. (Which I have an idea on how to solve - keep a copy
of the real type around, and update its pieces by way of a proxy
function that takes the name and searches for it, so the delegate
this/context pointer is still sane, but it was kinda buggy so I
dropped it. Another option would be to have other type tags
available, not just generic Type.Object, but something like
Type.NativeObject too that is added to the union. But meh, this
is pretty cool as it is imo.)
More information about the Digitalmars-d-announce
mailing list