Permalink
Browse files

Adding $Class() and $Array(). $Class will create a new classical-styl…

…e class and also allow fields to be initialized to default values. $Array is a helper for construction a new array where each element needs to be initialized to default values
  • Loading branch information...
1 parent c2f579f commit ce1fae0683980c9f3ac4ebe088d062e9c8013dd9 Frank Laub committed Mar 15, 2010
@@ -288,6 +288,10 @@ public class JsCodeGenerator: ICodeStatementVisitor, ICodeMemberVisitor
WriteLine("throw {0};", Print(stmt.Expression));
}
+ public void Visit(CodeGotoStatement stmt) {
+ throw new NotSupportedException();
+ }
+
#endregion
public void WriteTypeConstructor(TypeDefinition type) {
@@ -307,7 +311,21 @@ public class JsCodeGenerator: ICodeStatementVisitor, ICodeMemberVisitor
string ns = JsPrinter.EncodeName(JsPrinter.GetNamespace(type));
string name = JsPrinter.EncodeName(this.printer.GetTypeName(type));
- WriteLine("$Class({0}, '{1}', '{2}');", parent, ns, name);
+ var items = new List<string>();
+ foreach (FieldDefinition field in type.Fields) {
+ if (!field.IsStatic) {
+ string value = JsPrinter.GetDefaultValue(field.FieldType);
+ items.Add(string.Format("{0}: {1}", this.printer.GetMemberName(field), value));
+ }
+ }
+
+ string dict = "";
+ if (items.Any()) {
+ var dictInner = string.Join(", ", items.ToArray());
+ dict = string.Format(", {{ {0} }}", dictInner);
+ }
+
+ WriteLine("$Class({0}, '{1}', '{2}'{3});", parent, ns, name, dict);
WriteLine();
}
@@ -264,17 +264,14 @@ class JsPrinter : ICodeExpressionVisitor<string>
}
}
- //private int GetFixedArgs(MethodDefinition method) {
- // for (int i = 0; i < method.Parameters.Count; i++) {
- // var def = method.Parameters[i];
- // foreach (CustomAttribute attr in def.CustomAttributes) {
- // if (attr.Constructor.DeclaringType.FullName == "System.ParamArrayAttribute") {
- // return i;
- // }
- // }
- // }
- // return method.Parameters.Count;
- //}
+ public static string GetDefaultValue(TypeReference type) {
+ if (type.IsValueType) {
+ return "0";
+ }
+ else {
+ return "null";
+ }
+ }
private string PrintMacro(string macro, MethodDefinition method, string target, List<CodeExpression> parameters) {
var args = new List<string>();
@@ -497,7 +494,7 @@ class JsPrinter : ICodeExpressionVisitor<string>
public string VisitReturn(CodeArrayCreateExpression exp) {
string size = Print(exp.SizeExpression);
//return string.Format("new /*{0}*/Array({1})", Print(exp.Type), size);
- return string.Format("new Array({0})", size);
+ return string.Format("$Array({0}, {1})", size, GetDefaultValue(exp.Type));
}
public string VisitReturn(CodeArrayInitializeExpression obj) {
@@ -23,11 +23,15 @@ $Namespace = function(name) {
return parent;
}
-$Class = function(parent, ns, name, ctor) {
+$Class = function(parent, ns, name, dict) {
var cls = function() {
// if (ns) console.log(ns + '.' + name); else console.log(name);
+ if (dict) {
+ for (var key in dict) {
+ this[key] = dict[key];
+ }
+ }
if (parent) parent();
- if (ctor) ctor.call(this);
};
if (parent) {
var tmp = function() { };
@@ -53,6 +57,14 @@ $Delegate = function(scope, target) {
};
};
+$Array = function(len, init) {
+ var array = new Array(len);
+ for (var i = 0; i < len; i++) {
+ array[i] = init;
+ }
+ return array;
+}
+
Error.prototype.get_Message = function() {
return this.message;
}
@@ -23,12 +23,17 @@ $Namespace = function(name) {
return parent;
}
-$Class = function(parent, ns, name, ctor) {
+$Class = function(parent, ns, name, dict) {
var cls = function() {
if (ns) console.log(ns + '.' + name); else console.log(name);
+ if (dict) {
+ for (var key in dict) {
+ this[key] = dict[key];
+ }
+ }
if (parent) parent();
- if (ctor) ctor.call(this);
};
+
if (parent) {
var tmp = function() { };
tmp.prototype = parent.prototype;
@@ -77,7 +82,7 @@ Class.StaticMethod = function() {
console.log(this.$typename + '.StaticMethod');
};
-$Class(Class, '', 'Child');
+$Class(Class, '', 'Child', { x: 4 });
Child.prototype.$ctor = function(id) {
console.log('Child.$ctor');
@@ -115,3 +120,6 @@ console.log('> instance.$typename == "Class"');
console.log(instance.$typename);
console.log('> child.$typename == "Child"');
console.log(child.$typename);
+
+console.log(instance.x);
+console.log(child.x);

0 comments on commit ce1fae0

Please sign in to comment.