예제 #1
0
        private static void CompileImplementedMethods(JsClass klass, IMethod method)
        {
            if (method.IsExplicitImplementation)
            {
                return;
            }

            var impls = method.Implements;

            if (impls == null)
            {
                return;
            }
            if (impls.Count <= 0)
            {
                return;
            }

            var type = method.DeclaringType.JsFullName(method);
            var name = method.JsName();

            foreach (var i in impls)
            {
                var func = new JsFunction(null, i.JsParams());

                var call = "this".Id().Get(name).Call(i.JsArgs());
                func.Body.Add(method.IsVoid() ? call.AsStatement() : call.Return());

                klass.Add(new JsGeneratedMethod(string.Format("{0}.prototype.{1}", type, i.JsName()), func));
            }
        }
예제 #2
0
        private void CompileField(JsClass klass, IField field)
        {
            if (field.Data != null)
            {
                return;
            }

            //TODO: do not compile primitive types
            klass.Add(JsField.Make(field));

            if (field.Type.TypeKind == TypeKind.Struct)
            {
                CompileType(field.Type);
            }
        }
예제 #3
0
        private string CompileBox(IType type, JsClass klass)
        {
            var name = string.Format("{0}.$box", type.JsFullName());

            if (klass.BoxCompiled)
            {
                return(name);
            }

            klass.BoxCompiled = true;

            var val  = (JsNode)"v".Id();
            var func = new JsFunction(null, "v");

            if (type.IsNullableInstance())
            {
                func.Body.Add(new JsText(string.Format("if (!v.has_value) return null;")));
                func.Body.Add("$copy".Id().Call(val).Return());

                type = type.GetTypeArgument(0);
                CompileClass(type);
            }
            else
            {
                if (type.Is(SystemTypeCode.Boolean))
                {
                    val = val.Op("!!");
                }

                func.Body.Add(type.New(val).Return());
            }

            klass.Add(new JsGeneratedMethod(name, func));

            return(name);
        }
예제 #4
0
        private string CompileUnbox(MethodContext context, IType type, JsClass klass)
        {
            var name = string.Format("{0}.$unbox", type.JsFullName());

            if (klass.UnboxCompiled)
            {
                return(name);
            }

            klass.UnboxCompiled = true;

            var valueType = type.GetTypeArgument(0);
            var unbox     = Unbox(context, valueType);

            var val  = (JsNode)"v".Id();
            var func = new JsFunction(null, "v");

            func.Body.Add(val.Set(unbox.Call(val)));
            func.Body.Add(type.New(val).Return());

            klass.Add(new JsGeneratedMethod(name, func));

            return(name);
        }
예제 #5
0
        private static void CompileValues(JsCompiler compiler, JsClass klass)
        {
            var type = klass.Type;

            var fields = type.GetEnumFields()
                         .Select(x =>
            {
                var value = GetValue(compiler, x, type.ValueType);
                return(new { Name = x.Name, Value = value });
            });

            var    typeName    = type.JsFullName();
            var    valuesField = string.Format("{0}.$$values", typeName);
            object values;

            if (type.ValueType.IsInt64())
            {
                values = new JsArray(fields.Select(x => (object)new JsObject
                {
                    { "name", x.Name },
                    { "value", x.Value },
                }));
            }
            else
            {
                values = new JsObject(fields.Select(x => new KeyValuePair <object, object>(x.Value, x.Name)));
            }

            klass.Add(new JsGeneratedField(valuesField, values));

            var func = new JsFunction(null);

            func.Body.Add(valuesField.Id().Return());

            klass.ExtendPrototype(func, "$values");
        }