public T Add(object key, JsNode value) { var name = key as string; if (name == null) { var prefix = GetPrefix(key); int count; if (_names.TryGetValue(prefix, out count)) { count++; } else { count = 1; } _names[prefix] = count; name = prefix + count; } var item = new T { Name = name, Value = value }; _cache.Add(key, item); _list.Add(item); return(item); }
public JsNode Add(object key, JsNode value) { var pair = new KeyValuePair <int, JsNode>(_list.Count, value); _cache.Add(key, pair); _list.Add(value); return(ItemRef(pair.Key)); }
public JsCall(JsNode value, IEnumerable <object> args) { if (value == null) { throw new ArgumentNullException("value"); } _value = value; _args = args; }
public static JsNode And(this JsNode left, object right) { var a = left as JsAnd; if (a != null) { a.Args.Add(right); return(a); } return(new JsAnd(left, right)); }
public static JsNode Or(this JsNode left, object right) { var e = left as JsOr; if (e != null) { e.Args.Add(right); return(e); } return(new JsOr(left, right)); }
internal void Add(JsNode node) { var klass = node as JsClass; if (klass != null) { _classes.Add(klass); } _nodes.Add(node); }
private JsFunction CreateCallFunc(MethodContext context, IMethod method, CallInfo info) { var obj = "o".Id(); var args = "a".Id(); var func = new JsFunction(null, obj.Value, args.Value); InitClass(context, func, method); JsNode call = null; //TODO: inplace inline calls if (method.DeclaringType.Is(SystemTypeCode.Boolean) && method.IsToString()) { call = obj.Ternary("True", "False"); } //to detect stackoverflows //func.Body.Add("console.log".Id().Call(method.JsFullName()).AsStatement()); if (call == null && info != null) { if (info.ReceiverType != null && (info.Flags & CallFlags.Basecall) != 0) { call = method.JsFullName(info.ReceiverType).Id().Apply(obj, args); } else if (IsSuperCall(context, method, info.Flags)) { var baseType = context.Method.DeclaringType.BaseType; call = method.JsFullName(baseType).Id().Apply(obj, args); //TODO: remove $base if it is not needed //call = obj.Get("$base").Get(method.JsName()).Apply(obj, args); } } if (call == null) { if (!method.IsStatic && !method.IsConstructor && method.DeclaringType.IsBoxableType()) { new BoxingImpl(this).BoxUnboxed(context, method.DeclaringType, func, obj); } call = method.Apply(obj, args); } func.Body.Add(method.IsVoid() ? call.AsStatement() : call.Return()); return(func); }
private static void CompileEquals(JsCompiler compiler, JsClass klass) { var other = "o".Id(); var func = new JsFunction(null, other.Value); func.Body.Add(new JsText("if (o === null || o === undefined) return false;")); //TODO: check object type JsNode result = null; foreach (var field in GetInstanceFields(klass)) { var name = field.JsName(); var left = "this".Id().Get(name); var right = other.Get(name); JsNode e; // primitive and ref types if (field.Type.TypeKind != TypeKind.Struct && !field.Type.IsInt64Based()) { e = left.Op(right, BinaryOperator.Equality); } else // value types, int64 based { var objectType = compiler.SystemTypes.Object; var eq = objectType.Methods.Find("Equals", objectType, objectType); compiler.CompileMethod(eq); e = eq.JsFullName().Id().Call(left, right); } result = result == null ? e : result.And(e); } func.Body.Add(result == null ? "false".Id().Return() : result.Return()); var methodName = ObjectMethods.Find(compiler.SystemTypes.Object, ObjectMethodId.Equals).JsName(); klass.ExtendPrototype(func, methodName); }
public static JsNode Apply(this JsNode f, object obj, object args) { return(f.Get("apply").Call(obj, args)); }
public static JsVar Var(this JsNode value, string name) { return(new JsVar(name, value)); }
public static JsNode Ternary(this JsNode condition, object left, object right) { return(new JsConditionalExpression(condition, left, right)); }
public static JsNode Call <T>(this JsNode value, params T[] args) { return(new JsCall(value, args.Cast <object>())); }
public static JsNode Call(this JsNode value, params object[] args) { return(new JsCall(value, args)); }
public JsBinaryOperator(JsNode left, object value, string op) { _left = left; _value = value; _op = op; }
public JsPropertyRef(JsNode obj, object name) { _obj = obj; _name = name; }
public void BoxUnboxed(MethodContext context, IType type, JsFunction func, JsNode obj) { var f = Box(context, type); func.Body.Add(obj.Set("$cbox".Id().Call(obj, f))); }
public static JsNode Call <T>(this IMethod method, JsNode obj, params T[] args) { return(method.IsStatic() ? method.JsFullName().Id().Apply(obj, new JsArray(args.Cast <object>())) : obj.Get(method.JsName()).Call(obj, args)); }
public static JsNode Apply(this IMethod method, JsNode obj, object args) { return(method.IsStatic() ? method.JsFullName().Id().Apply(obj, args) : obj.Get(method.JsName()).Apply(obj, args)); }
public static JsNode AsStatement(this JsNode value) { return(new JsStatement(value)); }
public static JsNode Op(this JsNode left, object right, string op) { return(new JsBinaryOperator(left, right, op)); }
public JsVar(string name, JsNode value) { Name = name; Value = value; }
public static JsNode Op(this JsNode value, string op) { return(new JsUnaryOperator(value, op)); }
public JsUnaryOperator(JsNode value, string op) { _value = value; _op = op; }
public static JsNode Set(this JsNode dest, object value) { return(dest.Op(value, BinaryOperator.Assign).AsStatement()); }
public JsBinaryOperator(JsNode left, object value, BinaryOperator op) : this(left, value, op.EnumString("c#")) { }
public static JsNode Get(this JsNode obj, object name) { return(new JsPropertyRef(obj, name)); }
public JsStatement(JsNode expression) { _expression = expression; }
public static JsNode Set(this JsNode obj, JsNode name, object value) { return(Get(obj, name).Set(value)); }