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:

var a = 10;
a ~= "20";
	assert(a == "1020");

var a = function(int b, int c) { return b+c; };
// note the second set of () is because of broken @property
assert(a()(10,20) == 30);

var a = var.emptyObject;
a.foo = 30;
assert(a["foo"] == 30);

var b = json!q{
	"foo":12,
	"bar":{"hey":[1,2,3,"lol"]}
};

assert(b.bar.hey[1] == 2);
More...

Members

Classes

DynamicTypeException
class DynamicTypeException
Undocumented in source.
OverloadSet
class OverloadSet

Does dynamic dispatch to overloads in a jsvar function set.

PropertyPrototype
class PropertyPrototype
Undocumented in source.
PrototypeObject
class PrototypeObject
Undocumented in source.
WrappedNativeObject
class WrappedNativeObject
Undocumented in source.
WrappedOpaque
class WrappedOpaque(T)
Undocumented in source.
WrappedOpaque
class WrappedOpaque(T)
Undocumented in source.

Enums

JSONType
enum JSONType
Undocumented in source.

Functions

appearsNumeric
bool appearsNumeric(string n)
Undocumented in source. Be warned that the author may not have intended to support it.
isScriptable
bool isScriptable()
Undocumented in source. Be warned that the author may not have intended to support it.
isScriptableOpaque
bool isScriptableOpaque()
Undocumented in source. Be warned that the author may not have intended to support it.
main
void main()
Undocumented in source. Be warned that the author may not have intended to support it.
subclassable
var subclassable()

EXPERIMENTAL

typeCompatibilityScore
int typeCompatibilityScore(var arg, var type)
Undocumented in source. Be warned that the author may not have intended to support it.
varArray
var varArray(T t)
Undocumented in source. Be warned that the author may not have intended to support it.
varObject
var varObject(T t)
Undocumented in source. Be warned that the author may not have intended to support it.
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!

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!

wrapOpaquely
WrappedOpaque!Obj wrapOpaquely(Obj obj)
Undocumented in source. Be warned that the author may not have intended to support it.
wrapUfcs
WrappedNativeObject wrapUfcs(Type obj)

Wraps an opaque struct pointer in a module with ufcs functions

Interfaces

ScriptableSubclass
interface ScriptableSubclass
Undocumented in source.
VarMetadata
interface VarMetadata
Undocumented in source.

Manifest constants

scriptable
enum scriptable;
Undocumented in source.

Structs

Foop
struct Foop
Undocumented in source.
ScriptLocation
struct ScriptLocation
var
struct var

Templates

helper
template helper(alias T)
Undocumented in source.
json
template json(string s)
Undocumented in source.
makeAscii
template makeAscii()
Undocumented in source.

Variables

jsvar_throw
bool jsvar_throw;

Variable to decide if jsvar throws on certain invalid operations or continues on propagating null vars.

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.

Please note that function default arguments are NOT likely to work in the script. You'd have to use a helper thing that I haven't written yet. opAssign can never do it because that information is lost when it becomes a pointer. ParamDefault is thus commented out for now.

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.

Meta