Exemple #1
0
        public static T[] FastArrayCopy <T>(T[] array)
        {
            JsFunction function = Jsni.procedure(() => {});

            function.prototype = array.As <JsObject>();
            return(Jsni.@new(function).As <T[]>());
        }
Exemple #2
0
        public static JsTypeFunction Define(string name, JsTypeFunction prototype)
        {
            JsTypeFunction typeFunction = null;

            // Create constructor function, which is a superconstructor that takes in the actual
            // constructor as the first argument, and the rest of the arguments are passed directly
            // to that constructor.  These subconstructors are not Javascript constructors -- they
            // are not called via new, they exist for initialization only.
            typeFunction = Jsni.function(constructor =>
            {
                if (constructor != null || !(Jsni.instanceof(Jsni.@this(), typeFunction)))
                {
                    Jsni.invoke(Jsni.member(typeFunction, SpecialNames.StaticInitializer));
                }
                if (constructor != null)
                {
                    Jsni.apply(constructor, Jsni.@this(), Jsni.call(Jsni.reference("Array.prototype.slice"), Jsni.arguments(), 1.As < JsObject > ()).As <JsArray>());
                }
                if (!Jsni.instanceof(Jsni.@this(), typeFunction))
                {
                    return(typeFunction);
                }
                else
                {
                    return(Jsni.@this());
                }
            }).As <JsTypeFunction>();
            Jsni.memberset(typeFunction, "toString", Jsni.function(() => name.As <JsObject>()));
            Jsni.memberset(typeFunction, SpecialNames.TypeName, name.As <JsString>());
            Jsni.memberset(typeFunction, "prototype", Jsni.@new(prototype));
            return(typeFunction);
        }
Exemple #3
0
        public static JsFunction CreateConstructor(JsTypeFunction enclosingType, JsFunction implementation)
        {
            implementation.memberset(SpecialNames.TypeField, enclosingType);
            implementation.memberset(SpecialNames.New, Jsni.function(() =>
            {
/*
 *              if (!enclosingType.IsPrototypeInitialized)
 *              {
 *                  enclosingType.IsPrototypeInitialized = true;
 *                  enclosingType.prototype = Jsni.@new(enclosingType.PrototypeFactory.invoke());
 *              }
 */
                return(Jsni.@new(enclosingType, implementation, Jsni.arguments()));
            }));

            return(implementation);
        }
Exemple #4
0
        public static JsFunction CreateDelegate(JsObject thisExpression, JsFunction lambda, JsTypeFunction delegateType = null, string delegateKey = null)
        {
            delegateType = delegateType ?? Jsni.reference("System.Delegate").As <JsTypeFunction>();

            JsFunction delegateFunc = null;

            delegateFunc = Jsni.function(() =>
            {
                return(lambda.apply(delegateFunc.As <Delegate>().Target.As <JsObject>(), Jsni.arguments().As <JsArray>()));
            });
            delegateFunc.prototype = Jsni.@new(delegateType);
            delegateFunc.memberset("get_Target", Jsni.function(() => thisExpression));
            delegateFunc.memberset("GetType", Jsni.function(() => delegateType.GetTypeFromType.invoke()));
            Jsni.memberset(delegateFunc, SpecialNames.TypeField, delegateType);
            Jsni.memberset(delegateFunc, "Invoke", delegateFunc);
            Jsni.memberset(delegateFunc, "DynamicInvoke", Jsni.function(args => delegateFunc.apply(delegateFunc, args.As <JsArray>())));
            Jsni.memberset(delegateFunc, "GetHashCode", Jsni.function(() => lambda.toString().GetHashCode().As <JsObject>()));
            Jsni.memberset(delegateFunc, "lambda", lambda);
            Jsni.memberset(delegateFunc, "Equals", Jsni.function(x => x != null && lambda == x.member("lambda")));
            return(delegateFunc);
        }
Exemple #5
0
        public static JsFunction CreateDelegate(JsObject thisExpression, JsTypeFunction delegateType, JsFunction lambda, string delegateKey = null)
        {
            if (delegateKey != null)
            {
                if (thisExpression[delegateKey])
                {
                    return(thisExpression[delegateKey].As <JsFunction>());
                }
            }
            else
            {
                if (lambda.member("$delegate") != null)
                {
                    return(lambda.member("$delegate").As <JsFunction>());
                }
            }

            JsFunction delegateFunc = null;

            delegateFunc = Jsni.function(() =>
            {
                return(lambda.apply(delegateFunc.As <Delegate>().Target.As <JsObject>(), Jsni.arguments().As <JsArray>()));
            });
            delegateFunc.prototype = Jsni.@new(delegateType);
            Jsni.type <object>().TypeInitializer.invoke(delegateFunc, delegateFunc);
            Jsni.type <Delegate>().TypeInitializer.invoke(delegateFunc, delegateFunc);
            Jsni.type <MulticastDelegate>().TypeInitializer.invoke(delegateFunc, delegateFunc);
            delegateType.TypeInitializer.invoke(delegateFunc, delegateFunc);
            Jsni.invoke(Jsni.member(Jsni.member(Jsni.type <MulticastDelegate>().prototype, "$ctor"), "call"), delegateFunc, thisExpression, new[] { delegateFunc }.As <JsArray>());
            Jsni.memberset(delegateFunc, SpecialNames.TypeField, delegateType);
            if (delegateKey != null)
            {
                thisExpression[delegateKey] = delegateFunc;
            }
            else
            {
                lambda.memberset("$delegate", delegateFunc);
            }
            return(delegateFunc);
        }
Exemple #6
0
 internal static JsTypeFunction MakeArrayType(JsTypeFunction elementType)
 {
     if (elementType.ArrayType == null)
     {
         var baseType  = MakeGenericTypeFactory(Jsni.type(typeof(GenericArray <>)), Jsni.array(elementType));
         var arrayType = Jsni.procedure(() => {}).As <JsTypeFunction>();
         arrayType.prototype = Jsni.@new(baseType);
         Jsni.apply(
             Jsni.type <Object>().TypeInitializer,
             Jsni.@this(),
             Jsni.array(arrayType, arrayType.prototype));
         Jsni.apply(
             Jsni.type <Array>().TypeInitializer,
             Jsni.@this(),
             Jsni.invoke(
                 Jsni.member(Jsni.array(arrayType, arrayType.prototype), "concat"),
                 elementType
                 ).As <JsArray>());
         arrayType.TypeInitializer = Jsni.procedure((t, p) =>
         {
             p.___type = arrayType;
             t.As <JsTypeFunction>().TypeName        = elementType.TypeName + "[]";
             t.As <JsTypeFunction>().BaseType        = baseType;
             t.As <JsTypeFunction>().ElementType     = elementType;
             t.As <JsTypeFunction>().GetTypeFromType = Jsni.function(() => Type._GetTypeFromTypeFunc(Jsni.@this().As <JsTypeFunction>()).As <JsObject>());
             t.As <JsTypeFunction>().CreateTypeField = Jsni.function(() =>
             {
                 var lastIndex = elementType.TypeName.LastIndexOf('.');
                 if (lastIndex == -1)
                 {
                     lastIndex = 0;
                 }
                 else
                 {
                     lastIndex++;
                 }
                 var type       = new Type(elementType.TypeName.Substring(lastIndex) + "[]", new Attribute[0]);
                 arrayType.Type = type;
                 type.Init(
                     elementType.TypeName + "[]",
                     TypeAttributes.Public,
                     elementType,
                     Jsni.type <Array>(),
                     typeof(Array).interfaces.Concat(new[] { SpecialFunctions.MakeGenericTypeFactory(Jsni.type(typeof(IEnumerable <>)), Jsni.array(elementType)) }).ToArray(),
                     new JsTypeFunction[0],
                     new FieldInfo[0],
                     new MethodInfo[0],
                     new ConstructorInfo[0],
                     new PropertyInfo[0],
                     new EventInfo[0],
                     false,
                     false,
                     false,
                     false,
                     elementType,
                     null);
                 return(type.As <JsObject>());
             });
         }, SpecialNames.TypeInitializerTypeFunction, SpecialNames.TypeInitializerPrototype);
         Jsni.call(arrayType.TypeInitializer, Jsni.@this(), arrayType, arrayType.prototype);
         var result = arrayType;
         elementType.ArrayType = result;
     }
     return(elementType.ArrayType);
 }
Exemple #7
0
        public static JsTypeFunction Define(JsFunction assembly, JsTypeFunction enclosingType, string name, bool isGenericType, JsArray typeParameters, JsObject prototype, JsFunction typeInitializer)
        {
            JsTypeFunction typeFunction      = null;
            var            isTypeInitialized = false;

            // Create constructor function, which is a superconstructor that takes in the actual
            // constructor as the first argument, and the rest of the arguments are passed directly
            // to that constructor.  These subconstructors are not Javascript constructors -- they
            // are not called via new, they exist for initialization only.
            typeFunction = Jsni.function((constructor, args) =>
            {
                if (constructor != null || !(Jsni.instanceof(Jsni.@this(), typeFunction)))
                {
                    if (!isGenericType || typeFunction.UnconstructedType != null)
                    {
                        typeFunction.member(SpecialNames.StaticInitializer).invoke();
                    }
                }
                if (constructor != null)
                {
                    constructor.apply(Jsni.@this(), args.As <JsArray>());
                }
                if (!Jsni.instanceof(Jsni.@this(), typeFunction))
                {
                    return(typeFunction);
                }
                else
                {
                    return(Jsni.@this());
                }
            }).As <JsTypeFunction>();
            typeFunction.GetAssembly = assembly;
            assembly.member(SpecialNames.AssemblyTypesArray).member("push").invoke(typeFunction);
            typeFunction.memberset("toString", Jsni.function(() => name.As <JsObject>()));
            typeFunction.EnclosingType          = enclosingType;
            typeFunction.TypeName               = name;
            typeFunction.prototype              = Jsni.@new(prototype);
            typeFunction.IsPrototypeInitialized = false;
            typeFunction.TypeInitializer        = Jsni.procedure((_t, p) =>
            {
                var t = _t.As <JsTypeFunction>();
                if (isGenericType)
                {
                    var unconstructedType = t.UnconstructedType ?? t;
                    t.GenericTypeFunction = Jsni.function(() =>
                    {
                        return(Jsni.reference(SpecialNames.MakeGenericTypeConstructor).As <JsFunction>().call(unconstructedType, unconstructedType, Jsni.arguments()).As <JsFunction>().invoke());
                    });
                }
                t.GetTypeFromType = Jsni.function(() =>
                {
                    return(Type._GetTypeFromTypeFunc(Jsni.@this().As <JsTypeFunction>()).As <JsObject>());
                });
                p.memberset(SpecialNames.TypeName, t.member(SpecialNames.TypeName));
                p.___type  = t;
                t.BaseType = prototype.As <JsTypeFunction>();

                typeInitializer.apply(Jsni.@this(), Jsni.arguments().As <JsArray>());
            });
            typeFunction.CallTypeInitializer = Jsni.procedure(() =>
            {
                typeFunction.TypeInitializer.apply(enclosingType, Jsni.array(typeFunction, typeFunction.prototype).concat(typeParameters));
            });
            return(typeFunction);
        }