コード例 #1
0
        public static object Default(object T)
        {
            if (T == JsContext.undefined)
            {
                return(null);
            }


            Type   type   = Typeof(T);
            JsType jsType = type.GetJsType();

            if (jsType != null && jsType != null && (jsType.Kind == JsTypeKind.Struct || (jsType.baseTypeName != null && jsType.baseTypeName.indexOf("ValueType") != -1)))
            {
                switch (type.Name)
                {
                case "Boolean":
                    return(false);

                case "Byte":
                case "Int16":
                case "Int32":
                case "Int64":
                case "Double":
                case "Decimal":
                    return(null);

                default:
                    return(Activator.CreateInstance(jsType.As <Type>()));
                }
            }

            //TODO:
            return(null);
        }
コード例 #2
0
        private static object NewWithInitializer(JsType type, JsObject json)
        {
            var obj = JsCompiler.NewByFunc(type.ctor);

            if (@typeof(json) == "array")
            {
                throw new Exception("not implemented");
            }
            else
            {
                foreach (var p in json)
                {
                    var setter = obj.As <JsObject>()["set_" + p];
                    if (@typeof(setter) == "function")
                    {
                        setter.As <JsFunction>().call(obj, json[p]);
                    }
                    else
                    {
                        obj.As <JsObject>()[p] = json[p];
                    }
                }
            }
            return(obj);
        }
コード例 #3
0
        //checkes if the [objType] is of a certain [type]
        static bool TypeIs(JsType objType, JsType type)
        {
            if (objType == type)
            {
                return(true);
            }
            if (type.Kind == JsTypeKind.Interface)
            {
                var testedInterfaces = new JsObject();
                while (objType != null)
                {
                    if (objType == type)
                    {
                        return(true);
                    }
                    if (_TestTypeInterfacesIs(objType, type, testedInterfaces))
                    {
                        return(true);
                    }
                    objType = objType.baseType;
                }
                return(false);
            }
            if (type.Kind == JsTypeKind.Delegate && objType.fullname == "System.Delegate")
            {
                //for now, casting between any delegate type is permitted
                return(true);
            }
            if (objType.fullname == "System.Int32")
            {
                if (type.fullname == "System.Decimal")
                {
                    return(true);
                }
                if (type.fullname == "System.Double")
                {
                    return(true);
                }
                if (type.fullname == "System.Single")
                {
                    return(true);
                }
                if (type.fullname == "System.Nullable$1")
                {
                    return(true);
                }
            }
            var t = objType.baseType;

            while (t != null)
            {
                if (t == type)
                {
                    return(true);
                }
                t = t.baseType;
            }
            return(false);
        }
コード例 #4
0
 public static Type _TypeOf(JsType jsType)
 {
     if (jsType == null)
     {
         throw new Exception("Cannot resovle type");
     }
     if (jsType._ClrType == null)
     {
         jsType._ClrType = CreateClrType(jsType);
     }
     return(jsType._ClrType);
 }
コード例 #5
0
 public static string GetAssemblyQualifiedName(this JsType type)
 {
     if (type._AssemblyQualifiedName == null)
     {
         var name = type.fullname;
         if (type.assemblyName != null)
         {
             name += ", " + type.assemblyName;
         }
         type._AssemblyQualifiedName = name;
     }
     return(type._AssemblyQualifiedName);
 }
コード例 #6
0
        private static void ResolveBaseType(JsType type, JsType currentType)
        {
            var baseType = JsTypeHelper.GetType(type.baseTypeName);

            if (baseType == null)
            {
                baseType = JsTypeHelper.GetTypeIgnoreNamespace(type.baseTypeName, true);
            }
            if (!baseType.isCompiled)
            {
                CompileType(baseType);
            }
            currentType.baseType = baseType;
            baseType.derivedTypes.push(currentType);
        }
コード例 #7
0
        public static JsType GetTypeIgnoreNamespace(string name, bool throwIfNotFound)
        {
            JsType type  = null;
            var    cache = GetTypeIgnoreNamespaceCache;//arguments.callee.cache;

            if (cache != null)
            {
                type = cache[name].As <JsType>();
                if (JsContext.@typeof(type) != "undefined")
                {
                    if (throwIfNotFound && type == null)
                    {
                        throw new JsError("type " + name + " was not found with (with IgnoreNamespace).").As <Exception>();
                    }
                    return(type);
                }
            }
            if (name.As <JsString>().search(".") > -1)
            {
                var tokens = name.As <JsString>().split(".").As <JsArray>();
                name = tokens[tokens.length - 1].As <string>();
            }
            type = JsCompiler.Types[name].As <JsType>();
            var nameAfterNs = "." + name;

            if (type == null)
            {
                foreach (var p in JsCompiler.Types)
                {
                    if (p == name || p.As <JsString>().endsWith(nameAfterNs))
                    {
                        type = JsCompiler.Types[p].As <JsType>();
                        break;
                    }
                }
            }
            if (throwIfNotFound && type == null)
            {
                throw new JsError("type " + name + " was not found with (with IgnoreNamespace).").As <Exception>();
            }
            if (cache != null)
            {
                cache[name] = type ?? null;
            }
            return(type);
        }
コード例 #8
0
 private static void CompileEnum(JsType currentType)
 {
     if (currentType.Kind == JsTypeKind.Enum)
     {
         currentType.tryParse = _EnumTryParse;
         foreach (var p in currentType.staticDefinition)
         {
             if (@typeof(currentType.staticDefinition[p]) == "string")
             {
                 var x = NewByFunc(currentType.ctor);
                 x.As <JsObject>()["_Name"]      = p;
                 currentType.staticDefinition[p] = x;
                 currentType.As <JsObject>()[p]  = x;
             }
         }
     }
 }
コード例 #9
0
 static bool _TestTypeInterfacesIs(JsType testType, JsType iface, JsObject testedInterfaces)
 {
     if (testedInterfaces[iface.name].As <bool>())
     {
         return(false);
     }
     for (var i = 0; i < testType.interfaces.length; i++)
     {
         var testIface = testType.interfaces[i].As <JsType>();
         if (testIface == iface)
         {
             return(true);
         }
         testedInterfaces[testIface.name] = true;
         if (_TestTypeInterfacesIs(testIface, iface, testedInterfaces))
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #10
0
 private static void ResolveInterfaces(JsType type, JsType currentType)
 {
     if (type.interfaceNames == null)
     {
         return;
     }
     for (var i = 0; i < type.interfaceNames.length; i++)
     {
         var iName = type.interfaceNames[i].As <string>();
         var iface = JsTypeHelper.GetType(iName);
         if (iface == null)
         {
             iface = JsTypeHelper.GetTypeIgnoreNamespace(iName, true);
         }
         if (!iface.isCompiled)
         {
             CompileType(iface);
         }
         currentType.interfaces.push(iface);
     }
 }
コード例 #11
0
        private static void Compile_Phase2_TmpType(JsType tmpType)
        {
            var p    = tmpType.fullname;
            var type = CompileType(tmpType);

            if (type != null)
            {
                bool result = CopyMemberIfNotDefined(type, type.fullname, window);
                if (result)
                {
                    _NewJsTypes.push(type);
                }
            }
            if (type.ns != null)
            {
                var ns = ResolveNamespace(type.ns);
                if (type != null)
                {
                    ns[type.name] = type;
                }
            }
        }
コード例 #12
0
 private static Type CreateClrType(JsType jsType)
 {
     return(null);
 }
コード例 #13
0
 internal static JsDelegateFunction CreateClrDelegate(JsType type, JsArray <JsType> genericArgs, object target, JsFunction func)
 {
     return(JsTypeHelper.GetDelegate(target, func).As <JsDelegateFunction>()); //TODO: support delegate.getType()
 }
コード例 #14
0
 static JsFunction CreateBaseCtor(JsType type)
 {
     throw new NotImplementedException();
 }
コード例 #15
0
        private static JsType CompileType(JsType type)
        {
            var currentType = Types[type.fullname].As <JsType>() ?? type;

            if (currentType.ctors == null)
            {
                currentType.ctors = new JsObject();
            }
            if (!type.isCompiled)
            {
                var baseTypeResolved = false;
                if (currentType.baseType == null && currentType.baseTypeName != null)
                {
                    ResolveBaseType(type, currentType);
                    if (currentType.baseType != null)
                    {
                        baseTypeResolved = true;
                    }
                }
                ResolveInterfaces(type, currentType);
                foreach (var p in type.definition)
                {
                    if (p.As <JsString>().search("ctor") == 0) //isCtor
                    {
                        currentType.As <JsObject>()[p] = type.definition[p];
                        delete(type.definition[p]);
                        if (@typeof(currentType.commonPrototype) == "undefined")
                        {
                            currentType.commonPrototype = currentType.As <JsObject>()[p].As <JsFunction>().prototype.As <JsCompilerPrototype>();
                        }
                        else
                        {
                            currentType.As <JsObject>()[p].As <JsFunction>().prototype = currentType.commonPrototype;
                        }
                        currentType.ctors[p] = currentType.As <JsObject>()[p];
                    }
                    if (p == "cctor")
                    {
                        currentType.cctor = p.As <JsFunction>();
                    }
                }
                if (currentType.ctor == null)
                {
                    if (currentType.ns == null || currentType.ns == "")
                    {
                        var jsCtor = window[currentType.name].As <JsFunction>();
                        currentType.ctor = jsCtor;
                    }
                    if (currentType.ctor == null && currentType.ctors != null)
                    {
                        //create default ctor anyway for generic argument passing, etc...
                        //var createCtor = true;
                        //foreach (var p in currentType.ctors)
                        //{
                        //    createCtor = false;
                        //    break;
                        //}
                        //if (createCtor)
                        //{
                        if (currentType.baseType != null)
                        {
                            currentType.ctor = CreateBaseCtor(currentType);
                        }
                        else
                        {
                            currentType.ctor = CreateEmptyCtor();
                        }
                        //}
                    }
                    if (currentType.ctor != null)
                    {
                        currentType.ctors["ctor"] = currentType.ctor;
                        if (@typeof(currentType.commonPrototype) == "undefined")
                        {
                            currentType.commonPrototype = currentType.ctor.prototype.As <JsCompilerPrototype>();
                        }
                        else
                        {
                            currentType.ctor.prototype = currentType.commonPrototype;
                        }
                    }
                }
                foreach (var p in currentType.ctors)
                {
                    var ctor = currentType.ctors[p].As <JsCompilerFunction>();
                    if (ctor._type == null)
                    {
                        ctor._type = currentType;
                    }
                }
                if (baseTypeResolved)
                {
                    _CopyObject(currentType.baseType.commonPrototype, currentType.commonPrototype);
                }
                foreach (var p in type.definition)
                {
                    var member = type.definition[p];
                    currentType.commonPrototype[p] = member;
                    if (JsContext.@typeof(member) == "function")
                    {
                        member.As <JsCompilerFunction>()._name = p;
                        member.As <JsCompilerFunction>()._type = currentType;
                    }
                }
                if (type.definition.As <JsCompilerPrototype>().toString != JsCompilerObject.prototype.toString)
                {
                    currentType.commonPrototype.toString = type.definition.As <JsCompilerPrototype>().toString;
                    //currentType.commonPrototype.toString.name = "toString"; //It's always readonly! (and forbidden in strict mode)
                    currentType.commonPrototype.toString._type = currentType;
                }
                foreach (var p in type.staticDefinition)
                {
                    var member = type.staticDefinition[p];
                    //TODO: if (@typeof(currentType.As<JsObject>()[p]) != "undefined")
                    //TODO:    throw new JsError("Reserved static member name " + p).As<Exception>();
                    currentType.As <JsObject>()[p] = member;
                    if (JsContext.@typeof(member) == "function")
                    {
                        member.As <JsCompilerFunction>()._name = p;
                        member.As <JsCompilerFunction>()._type = currentType;
                    }
                }
                type.isCompiled = true;
            }
            CompileEnum(currentType);
            if (currentType != type && type.customAttributes != null)
            {
                if (currentType.customAttributes != null)
                {
                    for (var i = 0; i < type.customAttributes.length; i++)
                    {
                        currentType.customAttributes.push(type.customAttributes[i]);
                    }
                }
                else
                {
                    currentType.customAttributes = type.customAttributes;
                }
            }

            return(currentType);
        }
コード例 #16
0
 private static object TryImplicitConvert(object obj, JsType type)
 {
     throw new NotImplementedException();
 }
コード例 #17
0
 public static string GetName(this JsType type)
 {
     return(type.name);
 }