Ejemplo n.º 1
0
        public ClassDecl Bind(TypeRegister register, bool crossbind = false)
        {
            ClassDecl cls;
            var       db    = register.GetTypeDB();
            var       ctx   = (JSContext)register.GetContext();
            var       flags = DefaultFlags;
            var       proto = db.FindPrototypeOf(_type, out _type_id);

            if (proto.IsNullish())
            {
                _type_id = db.AddType(_type, JSApi.JS_UNDEFINED);
                // UnityEngine.Debug.Log($"add dynamic type {_type}: {_type_id}");

                #region BindConstructors(register, flags, type_id);
                var constructors       = _type.GetConstructors(flags);
                var dynamicConstructor = default(IDynamicMethod);
                if (constructors.Length > 0)
                {
                    var count = constructors.Length;
                    if (count == 1)
                    {
                        dynamicConstructor = new DynamicConstructor(this, constructors[0], crossbind);
                    }
                    else
                    {
                        var overloads = new DynamicMethods("constructor", count);
                        for (var i = 0; i < count; i++)
                        {
                            var overload = new DynamicConstructor(this, constructors[i], crossbind);
                            overloads.Add(overload);
                        }
                        dynamicConstructor = overloads;
                    }
                }
                #endregion

                cls = register.CreateClass(_type.Name, _type, dynamicConstructor);
            }
            else
            {
                cls = register.CreateClass(_type, proto);
            }

            #region BindMethods(register, flags);
            var instMap   = new Dictionary <string, List <MethodInfo> >();
            var staticMap = new Dictionary <string, List <MethodInfo> >();
            CollectMethod(ref cls, _type.GetMethods(flags), instMap, staticMap);
            AddMethods(ref cls, true, staticMap);
            AddMethods(ref cls, false, instMap);
            #endregion

            #region BindFields(register, flags);
            var fieldInfos = _type.GetFields(flags);
            for (int i = 0, count = fieldInfos.Length; i < count; i++)
            {
                var fieldInfo = fieldInfos[i];
                if (!fieldInfo.Name.StartsWith("_JSFIX_")) // skip hotfix slots
                {
                    var dynamicField = new DynamicField(this, fieldInfo);
                    cls.AddField(fieldInfo.IsStatic, fieldInfo.Name, dynamicField);
                }
            }
            #endregion

            #region BindProperties(register, flags);
            var propertyInfos = _type.GetProperties(flags);
            for (int i = 0, count = propertyInfos.Length; i < count; i++)
            {
                var propertyInfo    = propertyInfos[i];
                var anyMethod       = propertyInfo.GetMethod ?? propertyInfo.SetMethod;
                var dynamicProperty = new DynamicProperty(this, propertyInfo);
                cls.AddField(anyMethod.IsStatic, propertyInfo.Name, dynamicProperty);
            }
            #endregion

            // var ns = new NamespaceDecl();
            // typeDB.AddType()
            return(cls);
        }
Ejemplo n.º 2
0
        public void Bind(TypeRegister register)
        {
            // UnityEngine.Debug.LogErrorFormat("dynamic bind {0}", _type);
            var db     = register.GetTypeDB();
            var ctx    = (JSContext)register.GetContext();
            var flags  = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public;
            var pflags = flags | BindingFlags.NonPublic;

            _type_id = register.RegisterType(_type);

            #region BindConstructors(register, flags, type_id);
            var constructors       = _type.GetConstructors(flags);
            var dynamicConstructor = default(IDynamicMethod);
            if (constructors.Length > 0)
            {
                var count = constructors.Length;
                if (count == 1)
                {
                    dynamicConstructor = new DynamicConstructor(this, constructors[0]);
                }
                else
                {
                    var overloads = new DynamicMethods(count);
                    for (var i = 0; i < count; i++)
                    {
                        var overload = new DynamicConstructor(this, constructors[i]);
                        overloads.Add(overload);
                    }
                    dynamicConstructor = overloads;
                }
            }
            #endregion

            var cls = register.CreateClass(_type.Name, _type, dynamicConstructor);

            #region BindMethods(register, pflags);
            var instMap   = new Dictionary <string, List <MethodInfo> >();
            var staticMap = new Dictionary <string, List <MethodInfo> >();
            CollectMethod(_type.GetMethods(pflags), instMap, staticMap);
            AddMethods(cls, true, staticMap);
            AddMethods(cls, false, instMap);
            #endregion

            #region BindFields(register, pflags);
            var fieldInfos = _type.GetFields(pflags);
            for (int i = 0, count = fieldInfos.Length; i < count; i++)
            {
                var fieldInfo = fieldInfos[i];
                if (fieldInfo.Name.StartsWith("_JSFIX_"))
                {
                    //TODO: collect hotfix slots
                }
                else
                {
                    var dynamicField = new DynamicField(this, fieldInfo);
                    cls.AddField(fieldInfo.IsStatic, fieldInfo.Name, dynamicField);
                }
            }
            #endregion

            #region BindProperties(register, pflags);
            var propertyInfos = _type.GetProperties(pflags);
            for (int i = 0, count = propertyInfos.Length; i < count; i++)
            {
                var propertyInfo    = propertyInfos[i];
                var anyMethod       = propertyInfo.GetMethod ?? propertyInfo.SetMethod;
                var dynamicProperty = new DynamicProperty(this, propertyInfo);
                cls.AddField(anyMethod.IsStatic, propertyInfo.Name, dynamicProperty);
            }
            #endregion

            // var ns = new NamespaceDecl();
            // typeDB.AddType()
            cls.Close();
        }