当前位置: 首页>>代码示例>>C#>>正文


C# Context.DefineVariable方法代码示例

本文整理汇总了C#中Context.DefineVariable方法的典型用法代码示例。如果您正苦于以下问题:C# Context.DefineVariable方法的具体用法?C# Context.DefineVariable怎么用?C# Context.DefineVariable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Context的用法示例。


在下文中一共展示了Context.DefineVariable方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Invoke

        public object Invoke(IContext context, object @this, object[] arguments)
        {
            // TODO review: any case for use context parameter?
            IContext newctx = new Context(this.context);

            // Set this and arguments
            newctx.DefineVariable("this");
            newctx.SetValue("this", @this);
            newctx.DefineVariable("arguments");
            newctx.SetValue("arguments", arguments);

            for (int k = 0; arguments != null && k < arguments.Length && k < this.Arity; k++)
            {
                newctx.DefineVariable(parameterNames[k]);
                newctx.SetValue(parameterNames[k], arguments[k]);
            }

            // TODO Review: it should be null?
            if (this.Body != null)
                this.Body.Execute(newctx);

            // TODO Review: return undefined it not this?
            if (newctx.ReturnValue == null)
                if (@this != null)
                    return @this;
                else
                    return Undefined.Instance;

            return newctx.ReturnValue.Value;
        }
开发者ID:tario,项目名称:AjScript-migrated,代码行数:30,代码来源:Function.cs

示例2: GetNames

        public void GetNames()
        {
            Context context = new Context();
            context.DefineVariable("a");
            context.DefineVariable("b");
            context.DefineVariable("c");

            var names = context.GetNames();

            Assert.IsNotNull(names);
            Assert.AreEqual(3, names.Count);
            Assert.IsTrue(names.Contains("a"));
            Assert.IsTrue(names.Contains("b"));
            Assert.IsTrue(names.Contains("c"));
        }
开发者ID:ajlopez,项目名称:AjScript,代码行数:15,代码来源:ContextTests.cs

示例3: Main

        static void Main(string[] args)
        {
            IContext context = new Context();
            Parser parser = new Parser(System.Console.In);
            context.DefineVariable("write");
            context.SetValue("write", new WriteFunction());
            context.DefineVariable("writeln");
            context.SetValue("writeln", new WriteLineFunction());
            context.DefineVariable("Object");
            context.SetValue("Object", new ObjectFunction(context));

            Console.WriteLine("AjScript 0.1 (JavaScript-like Interpreter in C#)");

            for (ICommand cmd = parser.ParseCommand(); cmd != null; cmd = parser.ParseCommand())
                cmd.Execute(context);
        }
开发者ID:tario,项目名称:AjScript-migrated,代码行数:16,代码来源:Program.cs

示例4: DefineVariable

        public void DefineVariable()
        {
            Context context = new Context();

            context.DefineVariable("x");

            Assert.AreEqual(Undefined.Instance, context.GetValue("x"));
        }
开发者ID:tario,项目名称:AjScript-migrated,代码行数:8,代码来源:ContextTests.cs

示例5: DefineAndHasVariable

 public void DefineAndHasVariable()
 {
     Context context = new Context();
     Assert.IsFalse(context.HasVariable("a"));
     context.DefineVariable("a");
     Assert.IsTrue(context.HasVariable("a"));
     Assert.IsNull(context.GetValue("a"));
 }
开发者ID:ajlopez,项目名称:DartSharp,代码行数:8,代码来源:ContextTests.cs

示例6: DefineSetAndGetVariable

        public void DefineSetAndGetVariable()
        {
            Context context = new Context();

            context.DefineVariable("x");

            context.SetValue("x", 123);
            Assert.AreEqual(123, context.GetValue("x"));
        }
开发者ID:tario,项目名称:AjScript-migrated,代码行数:9,代码来源:ContextTests.cs

示例7: DefineSetAndGetAndRemoveVariable

        public void DefineSetAndGetAndRemoveVariable()
        {
            Context context = new Context();

            context.DefineVariable("x");

            context.SetValue("x", 123);
            Assert.AreEqual(123, context.GetValue("x"));

            context.RemoveValue("x");
            Assert.AreSame(Undefined.Instance, context.GetValue("x"));
        }
开发者ID:ajlopez,项目名称:AjScript,代码行数:12,代码来源:ContextTests.cs

示例8: DefineAndHasVariableInParent

        public void DefineAndHasVariableInParent()
        {
            Context parent = new Context();
            Context context = new Context(parent);

            Assert.IsFalse(parent.HasVariable("a"));
            Assert.IsFalse(context.HasVariable("a"));
            parent.DefineVariable("a");
            Assert.IsTrue(parent.HasVariable("a"));
            Assert.IsTrue(context.HasVariable("a"));
            Assert.IsNull(context.GetValue("a"));
        }
开发者ID:ajlopez,项目名称:DartSharp,代码行数:12,代码来源:ContextTests.cs

示例9: Execute

        public object Execute(Context context)
        {
            context.DefineVariable(this.name);

            if (this.expression != null)
            {
                var value = this.expression.Evaluate(context);
                context.SetValue(this.name, value);
                return value;
            }

            return null;
        }
开发者ID:ajlopez,项目名称:DartSharp,代码行数:13,代码来源:DefineVariableCommand.cs

示例10: Script

 /// <summary>
 /// Инициализирует объект типа Script и преобрзует код сценария во внутреннее представление.
 /// </summary>
 /// <param name="code">Код скрипта на языке JavaScript.</param>
 /// <param name="parentContext">Родительский контекст для контекста выполнения сценария.</param>
 /// <param name="messageCallback">Делегат обратного вызова, используемый для вывода сообщений компилятора</param>
 public Script(string code, Context parentContext, CompilerMessageCallback messageCallback, Options options)
 {
     if (code == null)
         throw new ArgumentNullException();
     Code = code;
     int i = 0;
     root = CodeBlock.Parse(new ParsingState(Tools.RemoveComments(code, 0), Code, messageCallback), ref i).Statement;
     if (i < code.Length)
         throw new System.ArgumentException("Invalid char");
     CompilerMessageCallback icallback = messageCallback != null ? (level, cord, message) =>
         {
             messageCallback(level, CodeCoordinates.FromTextPosition(code, cord.Column, cord.Length), message);
         } : null as CompilerMessageCallback;
     var stat = new FunctionStatistics();
     Parser.Build(ref root, 0, new System.Collections.Generic.Dictionary<string, VariableDescriptor>(), _BuildState.None, icallback, stat, options);
     var body = root as CodeBlock;
     Context = new Context(parentContext ?? NiL.JS.Core.Context.globalContext, true, pseudoCaller);
     Context.thisBind = new GlobalObject(Context);
     Context.variables = (root as CodeBlock).variables;
     Context.strict = (root as CodeBlock).strict;
     for (i = body.localVariables.Length; i-- > 0; )
     {
         var f = Context.DefineVariable(body.localVariables[i].name);
         body.localVariables[i].cacheRes = f;
         body.localVariables[i].cacheContext = Context;
         if (body.localVariables[i].Inititalizator != null)
             f.Assign(body.localVariables[i].Inititalizator.Evaluate(Context));
         if (body.localVariables[i].isReadOnly)
             body.localVariables[i].cacheRes.attributes |= JSObjectAttributesInternal.ReadOnly;
         body.localVariables[i].captured |= stat.ContainsEval;
     }
     var bd = body as CodeNode;
     body.Optimize(ref bd, null, icallback, options, stat);
 }
开发者ID:modulexcite,项目名称:NiL.JS,代码行数:40,代码来源:Script.cs

示例11: Initialize

        public Context Initialize(string binDirectory = null, string modulesDirectoryName = "modules")
        {
            if (string.IsNullOrWhiteSpace(binDirectory))
                binDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

            var modulesPath = Path.Combine(binDirectory, modulesDirectoryName);

            //TODO: Allow the Kernel to be injected.
            if (Kernel == null)
                Kernel = new StandardKernel();

            //Set the Kernel on the GlobalFunction object for use by globals.
            Library.GlobalFunctions.Kernel = Kernel;

            Kernel.Bind(x => x
                .FromAssembliesInPath(modulesPath)
                .SelectAllClasses()
                .InheritedFrom<IModule>()
                .BindAllInterfaces()
                .Configure((c, moduleType) =>
                {
                    c.WithMetadata("ModuleType", moduleType);
                    var moduleAttribute = moduleType.GetCustomAttributes(typeof(ModuleAttribute), false).FirstOrDefault() as ModuleAttribute;

                    if (moduleAttribute == null)
                        return;

                    c.WithMetadata("ModuleName", moduleAttribute.Name);
                })
                );

            //Execute ModuleInitialize methods on all modules.
            var bindings = Kernel.GetBindings(typeof(IModule));
            foreach(var binding in bindings)
            {
                var initializeMethods = binding.Metadata.Get<Type>("ModuleType").GetMethods(BindingFlags.Public | BindingFlags.Static).Where(mi => mi.CustomAttributes.Any(ca => ca.AttributeType == typeof(ModuleInitializeAttribute)));
                foreach(var initializeMethod in initializeMethods)
                {
                    initializeMethod.Invoke(null, BindingFlags.Default, null, null, null);
                }
            }

            var ctx = new Context();

            ctx.AttachModule(typeof(Library.Promise));
            ctx.DefineVariable("require").Assign(new ExternalFunction(Library.GlobalFunctions.require));

            return ctx;
        }
开发者ID:Oceanswave,项目名称:SkraprSharp,代码行数:49,代码来源:SkraprContext.cs


注:本文中的Context.DefineVariable方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。