arsd.jsvar

jsvar provides a D type called var that works similarly to the same in Javascript.

It is weakly (even weaker than JS, frequently returning null rather than throwing on an invalid operation) and dynamically typed, but interops pretty easily with D itself:

1 var a = 10;
2 a ~= "20";
3 	assert(a == "1020");
4 
5 var a = function(int b, int c) { return b+c; };
6 // note the second set of () is because of broken @property
7 assert(a()(10,20) == 30);
8 
9 var a = var.emptyObject;
10 a.foo = 30;
11 assert(a["foo"] == 30);
12 
13 var b = json!q{
14 	"foo":12,
15 	"bar":{"hey":[1,2,3,"lol"]}
16 };
17 
18 assert(b.bar.hey[1] == 2);
More...

Members

Functions

wrapNativeObject
WrappedNativeObject wrapNativeObject(Struct* obj)

Wraps a struct by reference. The pointer is stored - be sure the struct doesn't get freed or go out of scope!

wrapNativeObject
WrappedNativeObject wrapNativeObject(Class obj)

Wraps a class. If you are manually managing the memory, remember the jsvar may keep a reference to the object; don't free it!

Structs

var
struct var

Detailed Description

You can also use var.fromJson, a static method, to quickly and easily read json or var.toJson to write it.

Also, if you combine this with my arsd.script module, you get pretty easy interop with a little scripting language that resembles a cross between D and Javascript - just like you can write in D itself using this type.

Properties:

  • note that @property doesn't work right in D, so the opDispatch properties will require double parenthesis to call as functions.
  • Properties inside a var itself are set specially: obj.propName._object = new PropertyPrototype(getter, setter);

D structs can be turned to vars, but it is a copy.

Wrapping D native objects is coming later, the current ways suck. I really needed properties to do them sanely at all, and now I have it. A native wrapped object will also need to be set with _object prolly.

Suggestion Box / Bug Report