예제 #1
0
        public ClrEvent(global::Jint.Engine engine, DomConverter converter, EventInfo eventInfo)
        {
            Get = new ClrFunctionInstance(engine, (clrThis, args) =>
            {
                var attachedEvents = converter.GetAttachedEventsFor(clrThis);
                return(attachedEvents.TryGetValue(eventInfo, out var func) ? func : JsValue.Null);
            });

            Set = new ClrFunctionInstance(engine, (jsThis, args) =>
            {
                var objectInstance = jsThis.AsObject();
                var clrThis        = jsThis.ToObject();

                var attachedEvents = converter.GetAttachedEventsFor(objectInstance);

                if (attachedEvents.TryGetValue(eventInfo, out var existHandler))
                {
                    var clrHandler = converter.ConvertToClr(existHandler, eventInfo.EventHandlerType, jsThis);
                    eventInfo.RemoveMethod.Invoke(clrThis, new object[] { clrHandler });
                    attachedEvents.Remove(eventInfo);
                }

                if (args.Length != 0 && !args[0].IsNull() && !args[0].IsUndefined() && args[0].AsObject() is FunctionInstance functionInstance)
                {
                    var clrHandler = converter.ConvertToClr(functionInstance, eventInfo.EventHandlerType, jsThis);
                    eventInfo.AddMethod.Invoke(clrThis, new object[] { clrHandler });
                    attachedEvents[eventInfo] = functionInstance;
                }

                return(null);
            });
        }
예제 #2
0
        public ClrProperty(global::Jint.Engine engine, DomConverter converter, PropertyInfo property)
        {
            Get =
                property.GetMethod != null && property.GetMethod.IsPublic ?  new ClrFunctionInstance(engine, (jsThis, values) =>
            {
                var clrThis = converter.ConvertFromJs(jsThis);
                return(JsValue.FromObject(engine, property.GetValue(clrThis)));
            }) : null;

            var setter = new Lazy <Action <object, JsValue> >(() => CreateSetter(converter, property));

            Set =
                property.SetMethod != null && property.SetMethod.IsPublic ? new ClrFunctionInstance(engine, (jsThis, values) =>
            {
                try
                {
                    var clrThis = converter.ConvertFromJs(jsThis);
                    if (values.Length == 0)
                    {
                        return(JsValue.Undefined);
                    }

                    setter.Value(clrThis, values[0]);

                    return(JsValue.Undefined);
                }
                catch (Exception e)
                {
                    throw new JavaScriptException(e.Message);
                }
            }) : new ClrFunctionInstance(engine, (value, values) => JsValue.Undefined);
        }
예제 #3
0
        public ClrPrototype(global::Jint.Engine engine, DomConverter converter, Type type, ObjectInstance prototype) :
            base(engine)
        {
            Prototype  = prototype;
            _name      = (type.GetCustomAttribute <JsNameAttribute>()?.Name ?? type.Name) + "Prototype";
            Extensible = true;

            converter.DefineProperties(this, type);
        }
예제 #4
0
        public ClrCtor(Engine engine, DomConverter converter, Type type) :
            base(engine, null, null, false)
        {
            _type      = type;
            _converter = converter;
            Prototype  = engine.Function.PrototypeObject;
            FastAddProperty("prototype", _converter.GetPrototype(type), false, false, false);

            DomConverter.DefineStatic(this, type);
        }
예제 #5
0
        // TODO Should check retaining nodes - probably without parsing

        internal static DomElement Parse(string text)
        {
            var doc = new DomConverter().Convert(
                HtmlDocument.ParseXml(text, null),
                (HxlDocumentFragment) new HxlDocument().CreateDocumentFragment(),
                (Type t) => {}
                );

            MarkRetainedNodes.Instance.Preprocess(doc, null);
            return(doc.FirstChild);
        }
예제 #6
0
        public JintJsEngine(object global)
        {
            _typeConverter = new DomConverter(() => _engine);

            _engine = new Engine(o => o.AddObjectConverter(_typeConverter));

            if (global != null)
            {
                _typeConverter.SetGlobal(global);
                _typeConverter.DefineProperties(_engine.Global, global.GetType());
            }
        }
		ICompilationUnit Parse (ICSharpCode.NRefactory.IParser parser, string fileName)
		{
			parser.Parse();
			
			DomConverter visitor = new DomConverter (fileName);
			ICompilationUnit result = (ICompilationUnit)visitor.VisitCompilationUnit(parser.CompilationUnit, null);
/*			visitor.Cu.ErrorsDuringCompile = p.Errors.Count > 0;
			visitor.Cu.Tag = p.CompilationUnit;
			RetrieveRegions(visitor.Cu, p.Lexer.SpecialTracker);
			foreach (IClass c in visitor.Cu.Classes)
				c.Region.FileName = fileName;
			AddCommentTags(visitor.Cu, p.Lexer.TagComments);*/
			return result;
		}
예제 #8
0
        ICompilationUnit Parse(ICSharpCode.NRefactory.IParser parser, string fileName)
        {
            parser.Parse();

            DomConverter     visitor = new DomConverter(fileName);
            ICompilationUnit result  = (ICompilationUnit)visitor.VisitCompilationUnit(parser.CompilationUnit, null);

/*			visitor.Cu.ErrorsDuringCompile = p.Errors.Count > 0;
 *                      visitor.Cu.Tag = p.CompilationUnit;
 *                      RetrieveRegions(visitor.Cu, p.Lexer.SpecialTracker);
 *                      foreach (IClass c in visitor.Cu.Classes)
 *                              c.Region.FileName = fileName;
 *                      AddCommentTags(visitor.Cu, p.Lexer.TagComments);*/
            return(result);
        }
예제 #9
0
        private static Action <object, JsValue> CreateSetter(DomConverter converter, PropertyInfo property)
        {
            var expThisArg  = Expression.Parameter(typeof(object), "clrThis");
            var expValueArg = Expression.Parameter(typeof(JsValue), "jsValue");

            var setterExpression =
                Expression.Assign(
                    Expression.Property(Expression.Convert(expThisArg, property.DeclaringType), property),
                    converter.CreateConverterExpr(property.PropertyType, expValueArg, expThisArg));

            var setterLambda = Expression.Lambda <Action <object, JsValue> >(setterExpression, expThisArg, expValueArg)
                               .Compile();

            return(setterLambda);
        }
예제 #10
0
        private HxlDocumentFragment PrepareDocument()
        {
            _metrics.StartPreprocessor();
            ParsedTemplate parsed = this;

            // TODO Consider which services belong here
            var sp = new ServiceContainer(ServiceProvider.Current);

            var all       = CreateCompilerProcessors();
            var converter = new DomConverter();
            var myDoc     = converter.Convert(parsed.SourceDocument,
                                              NewDocument(),
                                              t => _implicitAssemblyReferences.Add(t.GetTypeInfo().Assembly));

            foreach (var c in all)
            {
                c.Preprocess(myDoc, sp);
            }

            // TODO Combine adjacent text uses (performance)
            // TODO ToArray() is wasteful (performance)

            // Convert document
            HxlDocumentFragment result = NewDocument();

            foreach (var m in myDoc.ChildNodes.ToArray())
            {
                var conv = HxlCompilerConverter.ChooseConverter(m);
                conv.ConvertAndAppend(result, m, CSharpScriptGenerator.Instance);
            }

            _metrics.EndPreprocessor();

            // TODO This is a compiler settings property
            bool skipOptimizations = false;

            _metrics.TemplateOptimizerStarting("cs", !skipOptimizations);

            if (!skipOptimizations)
            {
                _metrics.StartOptimizer();
                OptimizeRenderIslands.Rewrite(result);
                _metrics.EndOptimizer();
            }

            return(result);
        }
예제 #11
0
        public DomConverter CompileAllIfNeed(string FileName, bool parse_only_interface = false)
        {
            this.FileName = FileName;
            this.Text     = comp.GetSourceFileText(FileName);
            string                 ext        = Path.GetExtension(FileName);
            List <Error>           ErrorsList = new List <Error>();
            List <CompilerWarning> Warnings   = new List <CompilerWarning>();
            compilation_unit       cu         = null;

            if (Text != null)
            {
                cu = ParsersController.GetCompilationUnit(FileName, Text, ErrorsList, Warnings);
            }
            Parser = ParsersController.selectParser(Path.GetExtension(FileName).ToLower());
            ErrorsList.Clear();
            Warnings.Clear();
            documentation_comment_list dt       = ParsersController.Compile(System.IO.Path.ChangeExtension(FileName, get_doctagsParserExtension(ext)), Text, ErrorsList, Warnings, ParseMode.Normal) as documentation_comment_list;
            DocumentationConstructor   docconst = new DocumentationConstructor();

            if (cu != null)
            {
                docs = docconst.Construct(cu, dt);
            }
            DomConverter dconv = new DomConverter(this);

            dconv.visitor.parse_only_interface = parse_only_interface;
            if (XmlDoc.LookupLocalizedXmlDocForUnitWithSources(FileName, CodeCompletionController.currentLanguageISO) != null)
            {
                dconv.visitor.add_doc_from_text = false;
            }
            if (cu != null)
            {
                dconv.ConvertToDom(cu);
            }
            else
            {
                ErrorsList.Clear();
                Warnings.Clear();
                //cu = ParsersControllerGetComilationUnit(FileName, Text, ErrorsList, true);
                if (comp_modules[FileName] == null)
                {
                    string tmp = ParsersHelper.GetModifiedProgramm(Text);
                    if (tmp != null)
                    {
                        cu = ParsersControllerGetCompilationUnitSpecial(FileName, tmp, ErrorsList, Warnings);
                        ErrorsList.Clear();
                    }
                    if (cu == null)
                    {
                        cu = get_fictive_unit(Text, FileName);
                    }
                }
                ErrorsList.Clear();
                Warnings.Clear();
                dt = ParsersController.Compile(System.IO.Path.ChangeExtension(FileName, get_doctagsParserExtension(ext)), Text, ErrorsList, Warnings, ParseMode.Normal) as documentation_comment_list;
                if (cu != null)
                {
                    docs = docconst.Construct(cu, dt);
                }
                if (XmlDoc.LookupLocalizedXmlDocForUnitWithSources(FileName, CodeCompletionController.currentLanguageISO) != null)
                {
                    dconv.visitor.add_doc_from_text = false;
                }
                if (cu != null)
                {
                    dconv.ConvertToDom(cu);
                }
            }
            if (dconv.is_compiled)
            {
                comp_modules[FileName] = dconv;
            }
            if (docs != null)
            {
                docs.Clear();
            }
            //comp_modules[FileName] = dconv;
            // GC.Collect();
            return(dconv);
        }
예제 #12
0
        public DomConverter Compile(string FileName, string Text)
        {
            this.Text     = Text;
            this.FileName = FileName;
            List <Error>           ErrorsList = new List <Error>();
            List <CompilerWarning> Warnings   = new List <CompilerWarning>();
            compilation_unit       cu         = null;
            string ext = Path.GetExtension(FileName);

            try
            {
                cu = ParsersControllerGetCompilationUnit(FileName, Text, ErrorsList, Warnings);
                ErrorsList.Clear();
                documentation_comment_list dt       = ParsersController.Compile(System.IO.Path.ChangeExtension(FileName, get_doctagsParserExtension(ext)), Text /*+")))));end."*/, ErrorsList, Warnings, ParseMode.Normal) as documentation_comment_list;
                DocumentationConstructor   docconst = new DocumentationConstructor();
                if (cu != null)
                {
                    docs = docconst.Construct(cu, dt);
                }
            }
            catch (Exception e)
            {
#if DEBUG
                File.AppendAllText("log.txt", e.Message + Environment.NewLine + e.StackTrace + Environment.NewLine);
#endif
            }
            DomConverter dconv = new DomConverter(this);
            if (cu != null)
            {
                NetHelper.reset();

                dconv.ConvertToDom(cu);
            }
            else
            {
                ErrorsList.Clear();
                Warnings.Clear();
                try
                {
                    //cu = ParsersController.GetComilationUnit(FileName, Text+")))));end.",comp.CompilerOptions.ParserSearchPatchs,ErrorsList);
                    //cu = ParsersControllerGetComilationUnit(FileName, get_temp_text(Text), ErrorsList, true);
                    string tmp = ParsersHelper.GetModifiedProgramm(Text);
                    if (tmp != null)
                    {
                        cu = ParsersControllerGetCompilationUnitSpecial(FileName, tmp, ErrorsList, Warnings);
                    }
                    if (comp_modules[FileName] == null)
                    {
                        if (cu == null)
                        {
                            cu = get_fictive_unit(Text, FileName);
                        }
                    }
                    ErrorsList.Clear();
                    documentation_comment_list dt       = ParsersController.Compile(System.IO.Path.ChangeExtension(FileName, get_doctagsParserExtension(ext)), Text + ")))));end.", ErrorsList, Warnings, ParseMode.Normal) as documentation_comment_list;
                    DocumentationConstructor   docconst = new DocumentationConstructor();
                    if (cu != null)
                    {
                        docs = docconst.Construct(cu, dt);
                    }
                }
                catch (Exception e)
                {
#if DEBUG
                    File.AppendAllText("log.txt", e.Message + Environment.NewLine + e.StackTrace + Environment.NewLine);
#endif
                }
                if (cu != null)
                {
                    NetHelper.reset();
                    dconv.ConvertToDom(cu);
                }
            }
            if (docs != null)
            {
                docs.Clear();
            }
            //if (dconv.is_compiled) comp_modules[FileName]=dconv;
            return(dconv);
            //ConvertToDom(cu);
        }
예제 #13
0
        public DomConverter CompileAllIfNeed(string FileName, string Text)
        {
            DomConverter dconv = (DomConverter)comp_modules[FileName];

            if (dconv != null)
            {
                return(dconv);
            }
            this.Text     = Text;
            this.FileName = FileName;
            string ext = Path.GetExtension(FileName);
            List <PascalABCCompiler.Errors.Error> ErrorsList = new List <PascalABCCompiler.Errors.Error>();
            List <CompilerWarning> Warnings = new List <CompilerWarning>();

            PascalABCCompiler.SyntaxTree.compilation_unit cu = ParsersController.GetCompilationUnit(FileName, Text, ErrorsList, Warnings);
            Parser = ParsersController.selectParser(Path.GetExtension(FileName).ToLower());
            ErrorsList.Clear();
            PascalABCCompiler.SyntaxTree.documentation_comment_list dt = ParsersController.Compile(System.IO.Path.ChangeExtension(FileName, get_doctagsParserExtension(ext)), Text, ErrorsList, Warnings, PascalABCCompiler.Parsers.ParseMode.Normal) as PascalABCCompiler.SyntaxTree.documentation_comment_list;
            PascalABCCompiler.DocumentationConstructor docconst        = new PascalABCCompiler.DocumentationConstructor();
            if (cu != null)
            {
                docs = docconst.Construct(cu, dt);
            }
            dconv = new DomConverter(this);
            if (CodeCompletionTools.XmlDoc.LookupLocalizedXmlDocForUnitWithSources(FileName, CodeCompletionController.currentLanguageISO) != null)
            {
                dconv.visitor.add_doc_from_text = false;
            }
            if (cu != null)
            {
                dconv.ConvertToDom(cu);
            }
            else
            {
                ErrorsList.Clear();
                Warnings.Clear();
                //cu = ParsersControllerGetComilationUnit(FileName, Text, ErrorsList, true);
                if (comp_modules[FileName] == null)
                {
                    string tmp = ParsersHelper.GetModifiedProgramm(Text);
                    if (tmp != null)
                    {
                        cu = ParsersControllerGetCompilationUnitSpecial(FileName, tmp, ErrorsList, Warnings);
                        ErrorsList.Clear();
                    }
                    if (cu == null)
                    {
                        cu = get_fictive_unit(Text, FileName);
                    }
                }
                ErrorsList.Clear();
                Warnings.Clear();
                dt = ParsersController.Compile(System.IO.Path.ChangeExtension(FileName, get_doctagsParserExtension(ext)), Text + ")))));end.", ErrorsList, Warnings, PascalABCCompiler.Parsers.ParseMode.Normal) as PascalABCCompiler.SyntaxTree.documentation_comment_list;
                //PascalABCCompiler.DocumentationConstructor docconst = new PascalABCCompiler.DocumentationConstructor();
                if (cu != null)
                {
                    docs = docconst.Construct(cu, dt);
                }
                if (CodeCompletionTools.XmlDoc.LookupLocalizedXmlDocForUnitWithSources(FileName, CodeCompletionController.currentLanguageISO) != null)
                {
                    dconv.visitor.add_doc_from_text = false;
                }
                if (cu != null)
                {
                    dconv.ConvertToDom(cu);
                }
            }
            //comp_modules[FileName] = dconv;
            if (dconv.is_compiled)
            {
                comp_modules[FileName] = dconv;
            }

            if (docs != null)
            {
                docs.Clear();
            }
            //GC.Collect();
            return(dconv);
        }
예제 #14
0
        public static string GetDescription(int pos, string content, int line, int col, string FileName, DomConverter dc, PascalABCCompiler.Parsers.Controller controller)
        {
            string expr_without_brackets = null;

            PascalABCCompiler.Parsers.KeywordKind keyw;
            var expr = CodeCompletion.CodeCompletionController.CurrentParser.LanguageInformation.FindExpressionFromAnyPosition(pos, content, line, col, out keyw, out expr_without_brackets);

            if (expr == null)
            {
                expr = expr_without_brackets;
            }
            var errors   = new List <PascalABCCompiler.Errors.Error>();
            var warnings = new List <CompilerWarning>();
            var tree     = controller.GetExpression("test" + Path.GetExtension(FileName), expr, errors, warnings);
            var desc     = dc.GetDescription(tree, FileName, expr_without_brackets, controller, line, col, keyw, false);

            return(desc);
        }