Exemple #1
0
 protected Context()
 {
     // Context tables
     _functions = new RoutinesTable(RoutinesAppContext.NameToIndex, RoutinesAppContext.AppRoutines, RoutinesAppContext.ContextRoutinesCounter, FunctionRedeclared);
     _types     = new TypesTable(TypesAppContext.NameToIndex, TypesAppContext.AppTypes, TypesAppContext.ContextTypesCounter, TypeRedeclared);
     _statics   = new object[StaticIndexes.StaticsCount];
 }
Exemple #2
0
            static void AddScriptReference(Assembly assembly)
            {
                if (assembly == null)
                {
                    throw new ArgumentNullException(nameof(assembly));
                }

                // remember the assembly for class map:
                s_assClassMap.AddPhpAssemblyNoLock(assembly);

                // reflect the module for imported symbols:

                var module = assembly.ManifestModule;

                // PhpPackageReferenceAttribute
                foreach (var r in module.GetCustomAttributes <PhpPackageReferenceAttribute>())
                {
                    Context.AddScriptReference(r.ScriptType);
                }

                // ImportPhpTypeAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpTypeAttribute>())
                {
                    TypesTable.DeclareAppType(PhpTypeInfoExtension.GetPhpTypeInfo(t.ImportedType));
                }

                // ImportPhpFunctionsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpFunctionsAttribute>())
                {
                    // TODO: remember the container, do not reflect repetitiously

                    foreach (var m in t.ContainerType.GetMethods())
                    {
                        if (m.IsPublic && m.IsStatic && !m.IsPhpHidden())
                        {
                            RoutinesTable.DeclareAppRoutine(m.Name, m);
                        }
                    }
                }

                // ImportPhpConstantsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpConstantsAttribute>())
                {
                    // TODO: remember the container, do not reflect repetitiously

                    foreach (var m in t.ContainerType.GetMembers(BindingFlags.Static | BindingFlags.Public))
                    {
                        if (m is FieldInfo fi && !fi.IsPhpHidden())
                        {
                            Debug.Assert(fi.IsStatic && fi.IsPublic);

                            if (fi.IsInitOnly || fi.IsLiteral)
                            {
                                ConstsMap.DefineAppConstant(fi.Name, PhpValue.FromClr(fi.GetValue(null)));
                            }
                            else
                            {
                                ConstsMap.DefineAppConstant(fi.Name, new Func <PhpValue>(() => PhpValue.FromClr(fi.GetValue(null))));
                            }
                        }
Exemple #3
0
 protected Context()
 {
     // tables
     _functions = new RoutinesTable();
     _types     = new TypesTable();
     _statics   = new object[StaticIndexes.StaticsCount];
     _constants = ConstsMap.Create(this);
     _scripts   = ScriptsMap.Create();
 }
Exemple #4
0
        protected Context()
        {
            // Context tables
            _functions = new RoutinesTable(FunctionRedeclared);
            _types     = new TypesTable(TypeRedeclared);
            _statics   = new object[StaticIndexes.StaticsCount];

            //
            this.DefineConstant("PHP_SAPI", (PhpValue)this.ServerApi, ignorecase: false);
        }
Exemple #5
0
        protected Context()
        {
            _functions = new RoutinesTable(RoutinesAppContext.NameToIndex, RoutinesAppContext.AppRoutines, RoutinesAppContext.ContextRoutinesCounter, FunctionRedeclared);
            _types     = new TypesTable(TypesAppContext.NameToIndex, TypesAppContext.AppTypes, TypesAppContext.ContextTypesCounter, TypeRedeclared);
            _statics   = new object[StaticIndexes.StaticsCount];

            _globals = new PhpArray();
            _server  = new PhpArray();  // TODO: virtual initialization method, reuse server static information with request context
            // TODO: InitGlobalVariables(); //_globals.SetItemAlias(new IntStringKey("GLOBALS"), new PhpAlias(PhpValue.Create(_globals)));
        }
Exemple #6
0
        /// <summary>
        /// Initializes instance of context.
        /// </summary>
        /// <param name="services">Service provider. Can be <c>null</c> reference to use implicit services.</param>
        protected Context(IServiceProvider services)
        {
            _services = services;

            // tables
            _functions = new RoutinesTable();
            _types     = new TypesTable();
            _statics   = Array.Empty <object>();
            _constants = ConstsMap.Create(this);
            _scripts   = ScriptsMap.Create();
        }
Exemple #7
0
            /// <summary>
            /// Reflects given assembly for PeachPie compiler specifics - compiled scripts, references to other assemblies, declared functions and classes.
            /// Scripts and declarations are loaded into application context (static).
            /// </summary>
            /// <param name="assembly">PeachPie compiler generated assembly.</param>
            /// <remarks>Not thread safe.</remarks>
            public static void AddScriptReference(Assembly assembly)
            {
                if (assembly == null)
                {
                    throw new ArgumentNullException(nameof(assembly));
                }

                if (assembly.GetType(ScriptInfo.ScriptTypeName) == null || !TryAddAssembly(assembly))
                {
                    // nothing to reflect
                    return;
                }

                // reflect the module for imported symbols:

                var module = assembly.ManifestModule;

                // PhpPackageReferenceAttribute
                foreach (var r in module.GetCustomAttributes <PhpPackageReferenceAttribute>())
                {
                    AddPhpPackageReference(r.ScriptType);
                }

                // ImportPhpTypeAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpTypeAttribute>())
                {
                    TypesTable.DeclareAppType(PhpTypeInfoExtension.GetPhpTypeInfo(t.ImportedType));
                }

                // ImportPhpFunctionsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpFunctionsAttribute>())
                {
                    if (ExtensionsAppContext.ExtensionsTable.VisitFunctionsContainer(t.ContainerType, out var attr))
                    {
                        foreach (var m in t.ContainerType.GetMethods())
                        {
                            if (m.IsPublic && m.IsStatic && !m.IsSpecialName && !m.IsPhpHidden())
                            {
                                ExtensionsAppContext.ExtensionsTable.AddRoutine(attr, RoutinesTable.DeclareAppRoutine(m.Name, m));
                            }
                        }
                    }
                }

                // ImportPhpConstantsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpConstantsAttribute>())
                {
                    if (!s_processedConstantsContainers.Add(t.ContainerType))
                    {
                        // already visited before
                        continue;
                    }

                    //
                    var extensionName = t.ContainerType.GetCustomAttribute <PhpExtensionAttribute>(false)?.FirstExtensionOrDefault;

                    // reflect constants defined in the container
                    foreach (var m in t.ContainerType.GetMembers(BindingFlags.Static | BindingFlags.Public))
                    {
                        if (m is FieldInfo fi && !fi.IsPhpHidden())
                        {
                            Debug.Assert(fi.IsStatic && fi.IsPublic);

                            if (fi.IsInitOnly || fi.IsLiteral)
                            {
                                // constant
                                ConstsMap.DefineAppConstant(fi.Name, PhpValue.FromClr(fi.GetValue(null)), false, extensionName);
                            }
                            else
                            {
                                // static field
                                ConstsMap.DefineAppConstant(fi.Name, new Func <PhpValue>(() => PhpValue.FromClr(fi.GetValue(null))), false, extensionName);
                            }
                        }
Exemple #8
0
            /// <summary>
            /// Reflects given assembly for PeachPie compiler specifics - compiled scripts, references to other assemblies, declared functions and classes.
            /// Scripts and declarations are loaded into application context (static).
            /// </summary>
            /// <param name="assembly">PeachPie compiler generated assembly.</param>
            /// <remarks>Not thread safe.</remarks>
            public static void AddScriptReference(Assembly assembly)
            {
                if (assembly == null)
                {
                    throw new ArgumentNullException(nameof(assembly));
                }

                if (assembly.GetType(ScriptInfo.ScriptTypeName) == null || !s_processedAssemblies.Add(assembly))
                {
                    // nothing to reflect
                    return;
                }

                s_processedAssembliesArr = ArrayUtils.AppendRange(assembly, s_processedAssembliesArr);    // TODO: ImmutableArray<T>

                // remember the assembly for class map:
                s_assClassMap.AddPhpAssemblyNoLock(assembly);

                // reflect the module for imported symbols:

                var module = assembly.ManifestModule;

                // PhpPackageReferenceAttribute
                foreach (var r in module.GetCustomAttributes <PhpPackageReferenceAttribute>())
                {
                    if (r.ScriptType != null) // always true
                    {
                        AddScriptReference(r.ScriptType.Assembly);
                    }
                }

                // ImportPhpTypeAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpTypeAttribute>())
                {
                    TypesTable.DeclareAppType(PhpTypeInfoExtension.GetPhpTypeInfo(t.ImportedType));
                }

                // ImportPhpFunctionsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpFunctionsAttribute>())
                {
                    if (ExtensionsAppContext.ExtensionsTable.VisitFunctionsContainer(t.ContainerType, out var attr))
                    {
                        foreach (var m in t.ContainerType.GetMethods())
                        {
                            if (m.IsPublic && m.IsStatic && !m.IsPhpHidden())
                            {
                                ExtensionsAppContext.ExtensionsTable.AddRoutine(attr, RoutinesTable.DeclareAppRoutine(m.Name, m));
                            }
                        }
                    }
                }

                // ImportPhpConstantsAttribute
                foreach (var t in module.GetCustomAttributes <ImportPhpConstantsAttribute>())
                {
                    if (!s_processedConstantsContainers.Add(t.ContainerType))
                    {
                        // already visited before
                        continue;
                    }

                    // reflect constants defined in the container
                    foreach (var m in t.ContainerType.GetMembers(BindingFlags.Static | BindingFlags.Public))
                    {
                        if (m is FieldInfo fi && !fi.IsPhpHidden())
                        {
                            Debug.Assert(fi.IsStatic && fi.IsPublic);

                            if (fi.IsInitOnly || fi.IsLiteral)
                            {
                                ConstsMap.DefineAppConstant(fi.Name, PhpValue.FromClr(fi.GetValue(null)));
                            }
                            else
                            {
                                ConstsMap.DefineAppConstant(fi.Name, new Func <PhpValue>(() => PhpValue.FromClr(fi.GetValue(null))));
                            }
                        }