Thrown on script syntax errors and the sort.
This represents an exception thrown by throw x; inside the script as it is interpreted.
Thrown on things like interpretation failures.
This is likely your main entry point to the interpreter. It will interpret the script code given, with the given global variable object (which will be modified by the script, meaning you can pass it to subsequent calls to interpret to store context), and return the result of the last expression given.
This script interpreter is contained entirely in two files: jsvar.d and script.d. Download both of them and add them to your project. Then, import arsd.script;, declare and populate a var globals = var.emptyObject;, and interpret("some code", globals); in D.
There's nothing else to it, no complicated build, no external dependencies.
$ wget https://raw.githubusercontent.com/adamdruppe/arsd/master/script.d $ wget https://raw.githubusercontent.com/adamdruppe/arsd/master/jsvar.d$ dmd yourfile.d script.d jsvar.d
OVERVIEW
SPECIFICS
var a = try throw "exception";; // the double ; is because one closes the try, the second closes the var // a is now the thrown exception
So you can do some type coercion like this:
a = a|0; // forces to int a = "" ~ a; // forces to string a = a+0.0; // coerces to float
Though casting is probably better.
Supported types for casting to: int/long (both actually an alias for long, because of how var works), float/double/real, string, char/dchar (these return *integral* types), and arrays, int[], string[], and float[].
This forwards directly to the D function var.opCast.
obj.__prop("name", value); // bypasses operator overloading, useful for use inside the opIndexAssign especially
Note: if opIndex is not overloaded, getting a non-existent member will actually add it to the member. This might be a bug but is needed right now in the D impl for nice chaining. Or is it? FIXME
// static vars go on the auto created prototype static var b = 10;
// instance vars go on this instance itself var instancevar = 20;
// "virtual" functions can be overridden kinda like you expect in D, though there is no override keyword function virt() { b = 30; // lexical scoping is supported for static variables and functions
// but be sure to use this. as a prefix for any class defined instance variables in here this.instancevar = 10; } }
var foo = new Foo(12);
foo.newFunc = function() { this.derived = 0; }; // this is ok too, and scoping, including 'this', works like in Javascript
You can also use 'new' on another object to get a copy of it.
You can encase things in {} anywhere instead of a comma operator, and it works kinda similarly.
{} creates a new scope inside it and returns the last value evaluated.
Special function local variables: _arguments = var[] of the arguments passed _thisfunc = reference to the function itself this = reference to the object on which it is being called - note this is like Javascript, not D.
args can say var if you want, but don't have to default arguments supported in any position when calling, you can use the default keyword to use the default value in any position
FIXME: * make sure superclass ctors are called
FIXME: prettier stack trace when sent to D
FIXME: interpolated string: "$foo" or "#{expr}" or something. FIXME: support more escape things in strings like \n, \t etc.
FIXME: add easy to use premade packages for the global object.
FIXME: maybe simplify the json!q{ } thing a bit.
FIXME: the debugger statement from javascript might be cool to throw in too.
FIXME: add continuations or something too
FIXME: Also ability to get source code for function something so you can mixin. FIXME: add COM support on Windows
Might be nice: varargs lambdas - maybe without function keyword and the x => foo syntax from D.
This example shows the basics of how to interact with the script. The string enclosed in q{ .. } is the script language source.
The var type comes from os1.lang.jsvar and provides a dynamic type to D. It is the same type used in the script language and is weakly typed, providing operator overloads to work with many D types seamlessly.
However, if you do need to convert it to a static type, such as if passing to a function, you can use get!T to get a static type out of it.
var globals = var.emptyObject; globals.x = 25; // we can set variables on the global object globals.name = "script.d"; // of various types // and we can make native functions available to the script globals.sum = (int a, int b) { return a + b; }; // This is the source code of the script. It is similar // to javascript with pieces borrowed from D, so should // be pretty familiar. string scriptSource = q{ function foo() { return 13; } var a = foo() + 12; assert(a == 25); // you can also access the D globals from the script assert(x == 25); assert(name == "script.d"); // as well as call D functions set via globals: assert(sum(5, 6) == 11); // I will also set a function to call from D function bar(str) { // unlike Javascript though, we use the D style // concatenation operator. return str ~ " concatenation"; } }; // once you have the globals set up, you call the interpreter // with one simple function. interpret(scriptSource, globals); // finally, globals defined from the script are accessible here too: // however, notice the two sets of parenthesis: the first is because // @property is broken in D. The second set calls the function and you // can pass values to it. assert(globals.foo()() == 13); assert(globals.bar()("test") == "test concatenation"); // this shows how to convert the var back to a D static type. int x = globals.x.get!int;
Macros are like functions, but instead of evaluating their arguments at the call site and passing value, the AST nodes are passed right in. Calling the node evaluates the argument and yields the result (this is similar to to lazy parameters in D), and they also have methods like toSourceCode, type, and interpolate, which forwards to the given string.
The language also supports macros and custom interpolation functions. This example shows an interpolation string being passed to a macro and used with a custom interpolation string.
You might use this to encode interpolated things or something like that.
var globals = var.emptyObject; interpret(q{ macro test(x) { return x.interpolate(function(str) { return str ~ "test"; }); } var a = "cool"; assert(test("hey #{a}") == "hey cooltest"); }, globals;)
A small script interpreter that builds on os1.lang.jsvar to be easily embedded inside and to have has easy two-way interop with the host D program. The script language it implements is based on a hybrid of D and Javascript. The type the language uses is based directly on var from os1.lang.jsvar.
The interpreter is slightly buggy and poorly documented, but the basic functionality works well and much of your existing knowledge from Javascript will carry over, making it hopefully easy to use right out of the box. See the examples to quickly get the feel of the script language as well as the interop.