本文整理汇总了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;
}
示例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"));
}
示例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);
}
示例4: DefineVariable
public void DefineVariable()
{
Context context = new Context();
context.DefineVariable("x");
Assert.AreEqual(Undefined.Instance, context.GetValue("x"));
}
示例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"));
}
示例6: DefineSetAndGetVariable
public void DefineSetAndGetVariable()
{
Context context = new Context();
context.DefineVariable("x");
context.SetValue("x", 123);
Assert.AreEqual(123, context.GetValue("x"));
}
示例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"));
}
示例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"));
}
示例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;
}
示例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);
}
示例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;
}