Exemple #1
0
        /// <summary>
        /// returns null if no args exist in name.
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="throwIfNotFound"></param>
        /// <returns></returns>
        public static JsArray _GetTypeWithArguments(string typeName, bool throwIfNotFound)
        {
            var name = typeName;
            var gti  = name.As <JsString>().indexOf("`");

            if (gti != -1 && name.As <JsString>().indexOf("[") > -1)
            {
                var args = _ParseTypeNameArgs(name);
                if (args == null)
                {
                    return(null);
                }
                var type = JsTypeHelper.GetType(args[0].As <string>(), throwIfNotFound);
                if (type == null)
                {
                    return(null);
                }
                var res = new object[0].As <JsArray>();
                res.push(type);
                var typeArgs = new object[0].As <JsArray>();
                for (var i = 0; i < args[1].As <JsArray>().length; i++)
                {
                    var typeArg = JsTypeHelper.GetType(args[1].As <JsArray>()[i].As <JsArray>()[0].As <string>(), throwIfNotFound);
                    if (typeArg == null)
                    {
                        return(null);
                    }
                    typeArgs.push(typeArg);
                }
                res.push(typeArgs);
                return(res);
            }
            return(null);
        }
Exemple #2
0
        private static object Cast(object obj, string typeOrName)
        {
            if (obj == null)
            {
                return(obj);
            }
            var type = JsTypeHelper.GetType(typeOrName, true);

            if (Is(obj, type))
            {
                return(obj);
            }
            var converted = TryImplicitConvert(obj, type);

            if (converted != null)
            {
                return(converted);
            }
            var objTypeName = JsContext.@typeof(obj);

            if (JsContext.@typeof(obj.As <JsCompilerObject>().getTypeName) == "function")
            {
                objTypeName = obj.As <JsCompilerObject2>().getTypeName();
            }
            var msg = new JsArray("InvalidCastException: Cannot cast ", objTypeName, " to ", type.fullname, "Exception generated by JsRuntime").join("");

            throw new JsError(msg).As <Exception>();
        }
Exemple #3
0
        static bool Is(object obj, object typeOrName)
        {
            //Profiler.Data.push([1, new Date().getTime(), false, 1]);
            if (obj == null)
            {
                //Profiler.Data.push([1, new Date().getTime(), true, 1]);
                return(false);
            }
            var type = JsTypeHelper.GetType(typeOrName.As <string>(), true);

            //var type = typeCtor._type;
            if (type == null)
            {
                if (type == null && JsTypeOf(typeOrName) == SharpKit.JavaScript.JsTypes.function)
                {
                    var ctor = typeOrName.As <JsFunction>();
                    var i    = 0;
                    while (ctor != null && i < 20) //avoid circular base types (infinite loop)
                    {
                        if (obj.instanceof(ctor))
                        {
                            return(true);
                        }
                        ctor = ctor.As <JsObject>()["$baseCtor"].As <JsFunction>();
                        i++;
                    }
                    return(false);
                }
                throw new JsError("type expected").As <Exception>();
            }

            if (type.Kind == JsTypeKind.Enum)
            {
                JsObject valueCollection = type.staticDefinition ?? type;
                foreach (var key in valueCollection)
                {
                    if (valueCollection[key] == obj)
                    {
                        return(true);
                    }
                }
            }

            var objType = GetObjectType(obj);

            if (objType == null)
            {
                return(false);
            }
            var isIt = TypeIs(objType, type);

            //Profiler.Data.push([1, new Date().getTime(), true, 1]);
            return(isIt);
        }
Exemple #4
0
        private static object As(object obj, string typeOrName)
        {
            if (obj == null)
            {
                return(obj);
            }
            var type = JsTypeHelper.GetType(typeOrName, true);

            if (Is(obj, type))
            {
                return(obj);
            }
            return(null);
        }
        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);
        }
Exemple #6
0
        private static object New(string typeName, JsArray args)
        {
            //TODO:
            var type = JsTypeHelper.GetType(typeName, true);

            if (args == null || args.length == 0)
            {
                var obj = JsCompiler.NewByFunc(type.ctor);
                return(obj);
            }
            else
            {
                var obj = JsCompiler.NewByFuncArgs(type.ctor, args);
                return(obj);
            }
        }
Exemple #7
0
 private static Type Typeof(object jsTypeOrName)
 {
     if (jsTypeOrName == null)
     {
         throw new JsError("Unknown type.").As <Exception>();
     }
     if (JsTypeOf(jsTypeOrName) == SharpKit.JavaScript.JsTypes.function)
     {
         jsTypeOrName = JsTypeHelper.GetType(jsTypeOrName);
     }
     if (JsTypeOf(jsTypeOrName) == SharpKit.JavaScript.JsTypes.@string)
     {
         return(Type.GetType(jsTypeOrName.As <string>(), true));
     }
     return(_TypeOf(jsTypeOrName.As <JsType>()));
 }
 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);
     }
 }
Exemple #9
0
 private static JsType JsTypeof(string typeName)
 {
     return(JsTypeHelper.GetType(typeName, false));
 }
Exemple #10
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()
 }