Example #1
0
        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);
        }
Example #2
0
        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));
        }
Example #3
0
        public JsCall(JsNode value, IEnumerable <object> args)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            _value = value;
            _args  = args;
        }
Example #4
0
        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));
        }
Example #5
0
        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));
        }
Example #6
0
        internal void Add(JsNode node)
        {
            var klass = node as JsClass;

            if (klass != null)
            {
                _classes.Add(klass);
            }

            _nodes.Add(node);
        }
Example #7
0
        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);
        }
Example #8
0
        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);
        }
Example #9
0
 public static JsNode Apply(this JsNode f, object obj, object args)
 {
     return(f.Get("apply").Call(obj, args));
 }
Example #10
0
 public static JsVar Var(this JsNode value, string name)
 {
     return(new JsVar(name, value));
 }
Example #11
0
 public static JsNode Ternary(this JsNode condition, object left, object right)
 {
     return(new JsConditionalExpression(condition, left, right));
 }
Example #12
0
 public static JsNode Call <T>(this JsNode value, params T[] args)
 {
     return(new JsCall(value, args.Cast <object>()));
 }
Example #13
0
 public static JsNode Call(this JsNode value, params object[] args)
 {
     return(new JsCall(value, args));
 }
Example #14
0
 public JsBinaryOperator(JsNode left, object value, string op)
 {
     _left  = left;
     _value = value;
     _op    = op;
 }
Example #15
0
 public JsPropertyRef(JsNode obj, object name)
 {
     _obj  = obj;
     _name = name;
 }
Example #16
0
        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)));
        }
Example #17
0
 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));
 }
Example #18
0
 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));
 }
Example #19
0
 public static JsNode AsStatement(this JsNode value)
 {
     return(new JsStatement(value));
 }
Example #20
0
 public static JsNode Op(this JsNode left, object right, string op)
 {
     return(new JsBinaryOperator(left, right, op));
 }
Example #21
0
 public JsVar(string name, JsNode value)
 {
     Name  = name;
     Value = value;
 }
Example #22
0
 public static JsNode Op(this JsNode value, string op)
 {
     return(new JsUnaryOperator(value, op));
 }
Example #23
0
 public JsUnaryOperator(JsNode value, string op)
 {
     _value = value;
     _op    = op;
 }
Example #24
0
 public static JsNode Set(this JsNode dest, object value)
 {
     return(dest.Op(value, BinaryOperator.Assign).AsStatement());
 }
Example #25
0
 public JsBinaryOperator(JsNode left, object value, BinaryOperator op)
     : this(left, value, op.EnumString("c#"))
 {
 }
Example #26
0
 public static JsNode Get(this JsNode obj, object name)
 {
     return(new JsPropertyRef(obj, name));
 }
Example #27
0
 public JsStatement(JsNode expression)
 {
     _expression = expression;
 }
Example #28
0
 public static JsNode Set(this JsNode obj, JsNode name, object value)
 {
     return(Get(obj, name).Set(value));
 }