Esempio n. 1
0
        /// <summary>
        /// Creates new instance of the type.
        /// </summary>
        /// <returns></returns>
        public object VisitNew(NewExpr expr)
        {
            object[] constructorArgs = null;
            var      paramListExprs  = expr.ParamListExpressions;

            if (paramListExprs != null && paramListExprs.Count > 0)
            {
                expr.ParamList = new List <object>();
                ParamHelper.ResolveNonNamedParameters(paramListExprs, expr.ParamList, this);
                constructorArgs = expr.ParamList.ToArray();
            }

            // CASE 1: Built in basic system types ( string, date, time, etc )
            if (LTypesLookup.IsBasicTypeShortName(expr.TypeName))
            {
                // TODO: Move this check to Semacts later
                var langType  = LTypesLookup.GetLType(expr.TypeName);
                var methods   = this.Ctx.Methods.Get(langType);
                var canCreate = methods.CanCreateFromArgs(constructorArgs);
                if (!canCreate)
                {
                    throw ExceptionHelper.BuildRunTimeException(expr, "Can not create " + expr.TypeName + " from parameters");
                }

                // Allow built in type methods to create it.
                var result = methods.CreateFromArgs(constructorArgs);
                return(result);
            }
            // CASE 2: Custom types e.g. custom classes.
            var hostLangArgs = LangTypeHelper.ConvertToArrayOfHostLangValues(constructorArgs);
            var instance     = this.Ctx.Types.Create(expr.TypeName, hostLangArgs);
            var obj          = LangTypeHelper.ConvertToLangClass(instance);

            return(obj);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates new instance of the type.
        /// </summary>
        /// <returns></returns>
        public override object DoEvaluate()
        {
            object[] constructorArgs = null;
            if (ParamListExpressions != null && ParamListExpressions.Count > 0)
            {
                ParamList = new List <object>();
                ParamHelper.ResolveNonNamedParameters(ParamListExpressions, ParamList);
                constructorArgs = ParamList.ToArray();
            }

            // CASE 1: Built in basic system types ( string, date, time, etc )
            if (LTypesLookup.IsBasicTypeShortName(this.TypeName))
            {
                // TODO: Move this check to Semacts later
                var langType  = LTypesLookup.GetLType(this.TypeName);
                var methods   = this.Ctx.Methods.Get(langType);
                var canCreate = methods.CanCreateFromArgs(constructorArgs);
                if (!canCreate)
                {
                    throw BuildRunTimeException("Can not create " + this.TypeName + " from parameters");
                }

                // Allow built in type methods to create it.
                var result = methods.CreateFromArgs(constructorArgs);
                return(result);
            }
            // CASE 2: Custom types e.g. custom classes.
            var hostLangArgs = LangTypeHelper.ConvertToArrayOfHostLangValues(constructorArgs);
            var instance     = Ctx.Types.Create(this.TypeName, hostLangArgs);

            return(new LClass(instance));
        }
        public void InitPlugins()
        {
            var totalPlugins = _context.Plugins.Total();
            var tokenIt      = _parser.TokenIt;
            var lexer        = _parser.Lexer;

            if (!_pluginsInitialized)
            {
                // 2. Register default methods if not present.
                Tokens.Default();
                ErrorCodes.Init();
                LTypesLookup.Init();
                _context.Methods.RegisterIfNotPresent(LTypes.Array, new LJSArrayMethods());
                _context.Methods.RegisterIfNotPresent(LTypes.Date, new LJSDateMethods());
                _context.Methods.RegisterIfNotPresent(LTypes.String, new LJSStringMethods());
                _context.Methods.RegisterIfNotPresent(LTypes.Time, new LJSTimeMethods());
                _context.Methods.RegisterIfNotPresent(LTypes.Map, new LJSMapMethods());
                _context.Methods.RegisterIfNotPresent(LTypes.Table, new LJSTableMethods());
            }

            if (!_pluginsInitialized || totalPlugins > _lastInitializationPluginCount)
            {
                // 3. Initialize the plugins.
                var expParser = new ExprParser();
                expParser._parser           = _parser;
                _context.PluginsMeta.Parser = expParser;
                _context.Plugins.RegisterAllSystem();
                _context.Plugins.ForEach <IExprPlugin>(plugin =>
                {
                    plugin.Init(_parser, tokenIt);
                    plugin.Ctx       = _context;
                    plugin.ExpParser = expParser;
                });

                _context.Plugins.ForEach <ITokenPlugin>(plugin => { plugin.Init(_parser, tokenIt); });
                _context.Plugins.ForEach <ILexPlugin>(plugin => { plugin.Init(lexer); plugin.Ctx = _context; });
                _context.Plugins.ExecuteSetupPlugins(_context);

                PreprocessHelper.Ctx           = _context;
                _lastInitializationPluginCount = _context.Plugins.Total();
            }
            _pluginsInitialized = true;
        }
Esempio n. 4
0
 /// <summary>
 /// Callback for when these methods are registered with the system.
 /// </summary>
 public override void OnRegistered()
 {
     // Associated the javascript Date type name "Date" to LDate type name : "sys.datetime".
     LTypesLookup.RegisterAlias(this.DataType, "Time", "Time");
 }