コード例 #1
0
        TypeWrapper(Type type)
        {
            TypeWrapper baseTypeWrapper = null;

            if (type.BaseType != null)
            {
                baseTypeWrapper = TypeWrapper.Wrap(type.BaseType);
            }

            this.type = type;
            var ctors = type.GetConstructors();

            thisPtr = GCHandle.Alloc(this);
            if (ctors.Length == 0)
            {
                constructorValue = JavaScriptValue.CreateFunction(NoConstructorDg, GCHandle.ToIntPtr(thisPtr));
            }
            else if (ctors.Length == 1)
            {
                var ctorw = ConstructorWrapper.Wrap(ctors[0]);
                constructorValue = ctorw.GetJavaScriptValue();
                ctorWrapper      = ctorw;
            }
            else
            {
                var os = new OverloadSelector();
                foreach (var m in ctors)
                {
                    os.AppendMethod(m);
                }
                constructorValue = os.GetJavaScriptValue();
                ctorWrapper      = os;
            }
            constructorValue.AddRef();
            prototypeValue = JavaScriptValue.CreateObject();
            prototypeValue.AddRef();
            typePtr = GCHandle.Alloc(type);
            constructorValue.SetIndexedProperty(JavaScriptValue.FromString("_clrtypevalue"),
                                                JavaScriptValue.CreateExternalObject(GCHandle.ToIntPtr(typePtr), FreeDg));
            // statics
            ctorStr = type.FullName;
            constructorValue.SetIndexedProperty(JavaScriptValue.FromString("toString"),
                                                JavaScriptValue.CreateFunction(InternalUtil.GetSavedStringDg, GCHandle.ToIntPtr(GCHandle.Alloc(ctorStr))));
            AssignMethodProc(constructorValue,
                             type.GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static));
            AssignFieldProc(constructorValue,
                            type.GetFields(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static));
            AssignPropertyProc(constructorValue,
                               type.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Static));
            // instances
            prototypeStr = type.FullName + " Instance";
            prototypeValue.SetIndexedProperty(JavaScriptValue.FromString("toString"),
                                              JavaScriptValue.CreateFunction(InternalUtil.GetSavedStringDg, GCHandle.ToIntPtr(GCHandle.Alloc(prototypeStr))));
            AssignMethodProc(prototypeValue,
                             type.GetMethods(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance));
            AssignFieldProc(prototypeValue,
                            type.GetFields(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance));
            AssignPropertyProc(prototypeValue,
                               type.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance));

            constructorValue.SetIndexedProperty(JavaScriptValue.FromString("prototype"), prototypeValue);

            if (baseTypeWrapper != null)
            {
                constructorValue.Prototype = baseTypeWrapper.constructorValue;
                prototypeValue.Prototype   = baseTypeWrapper.prototypeValue;
            }
        }