On Wednesday, 29 October 2014 at 22:22:39 UTC, ketmar via
Digitalmars-d-learn wrote:
it's not lightning fast, though, but the code is understandable
and it's fairly easy to extend the language if necessary.
Curious, what have you tried with it?
I wanted to keep it simple but actually complicated it more than
I wanted to, it is cool to know it isn't hard to use.
What I really like though is that the var type works in D too,
making interoperation so easy. My only disappointment is
@property still doesn't work, making foo.bar()() need the double
parens!
you can take it in ARSD repository:
https://github.com/adamdruppe/arsd
what you need is jsvar.d and script.d, just two files and no
external libs required.
Here's an example usage:
import arsd.script;
void main() {
// this var holds the global variables of the script
engine
var globals = var.emptyObject;
// you can set up values or functions with plain
assignment in D
globals.myFunction = (int a, int b) { return a + b; };
import std.file;
// run the interpret function passing script code and the
variables
interpret(readText("scriptcode.js"), globals);
// you can then access script values or functions from D
too
import std.stdio;
writeln(globals.foo()("adr"));
// and also interpret strings here. The interpret function
// returns the value of the last expression
writeln(interpret("myFunction(12, 24);", globals));
}
Here's what my scriptcode.js looks like:
// suppose the code there is:
// the syntax is kinda like javascript and kinda like D
// the concat operator is D style, but function decls
are JS style
function foo(name) { return "hello, " ~ name ~ " you are
" ~ myFunction(12, 53) ~ " years old"; }
// set a global variable too
var myname = "adam";
kinda like a hybrid of D and JavaScript.