本文整理汇总了C#中ScriptEngine.SetGlobalValue方法的典型用法代码示例。如果您正苦于以下问题:C# ScriptEngine.SetGlobalValue方法的具体用法?C# ScriptEngine.SetGlobalValue怎么用?C# ScriptEngine.SetGlobalValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ScriptEngine
的用法示例。
在下文中一共展示了ScriptEngine.SetGlobalValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RequireJsCompiler
public RequireJsCompiler(TextWriter consoleOut, string currentDirectory)
{
_jsEngine = new ScriptEngine();
_jsEngine.CompatibilityMode = CompatibilityMode.ECMAScript3;
_jsEngine.Evaluate(LoadResource("require.js"));
_jsEngine.Evaluate(LoadResource("json2.js"));
_jsEngine.Evaluate(LoadResource(@"adapt\rhino.js"));
_jsEngine.Evaluate("require(" + new JavaScriptSerializer().Serialize(new {
baseUrl = ResourceBaseUrl
}) + ");");
_fs = new NativeFS(_jsEngine, currentDirectory);
_jsEngine.SetGlobalValue("YUNoFS", _fs);
_path = new NativePath(_jsEngine, currentDirectory);
_jsEngine.SetGlobalValue("YUNoPath", _path);
_ioAdapter = new IOAdapter(_jsEngine, consoleOut);
_jsEngine.SetGlobalFunction("load", (Action<string>)_ioAdapter.load);
_jsEngine.SetGlobalValue("ioe", _ioAdapter);
_jsEngine.SetGlobalValue("IsRunningYUNoAMD", true);
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "env.js", @"build\jslib\env.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "yunoamd/args.js", @"build\jslib\yunoamd\args.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "build.js", @"build\build.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "print.js", @"build\jslib\yunoamd\print.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "fs.js", @"build\jslib\yunoamd\fs.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "path.js", @"build\jslib\yunoamd\path.js");
SetupModuleFromResource(RequireJsCompiler.ResourceBaseUrl + "file.js", @"build\jslib\node\file.js");
}
示例2: GetMissingPropertyValue
public void GetMissingPropertyValue()
{
var engine = new ScriptEngine();
// Create a new TestClass1 instance.
engine.SetGlobalValue("test", new TestClass1(engine));
// Missing properties should return the name of the property.
Assert.AreEqual("unknown", engine.Evaluate("test.unknown"));
Assert.AreEqual("unknown", engine.Evaluate("test['unknown']"));
Assert.AreEqual("1", engine.Evaluate("test[1]"));
// Existing properties should still work.
Assert.AreEqual(1, engine.Evaluate("test.exists"));
// Missing properties return null for Object.getOwnPropertyDescriptor().
Assert.AreEqual(Undefined.Value, engine.Evaluate("Object.getOwnPropertyDescriptor(test, 'unknown')"));
// Missing properties return false from hasOwnProperty().
Assert.AreEqual(false, engine.Evaluate("test.hasOwnProperty('unknown')"));
// Missing properties return false from propertyIsEnumerable().
Assert.AreEqual(false, engine.Evaluate("test.propertyIsEnumerable('unknown')"));
// Create a new TestClass2 instance.
engine.SetGlobalValue("test", new TestClass2(engine));
// Missing properties should return a function which can be called.
Assert.AreEqual("unknown0", engine.Evaluate("test.unknown()"));
Assert.AreEqual("unknown1", engine.Evaluate("test.unknown(1)"));
}
示例3: Construct
internal static ScriptEngine Construct(HttpListenerContext context)
{
ScriptEngine engine = new ScriptEngine();
engine.SetGlobalValue("Request", new RequestInstance(engine, context.Request));
engine.SetGlobalValue("Response", new ResponseInstance(engine, context.Response));
engine.SetGlobalValue("Console", new ConsoleInstance(engine));
return engine;
}
示例4: SetGlobalValue
public void SetGlobalValue()
{
var engine = new ScriptEngine();
// Try setting a value using the standard types.
engine.SetGlobalValue("test", true);
Assert.AreEqual(true, engine.Evaluate("test === true"));
engine.SetGlobalValue("test", 5.1);
Assert.AreEqual(true, engine.Evaluate("test === 5.1"));
engine.SetGlobalValue("test", uint.MaxValue);
Assert.AreEqual(true, engine.Evaluate("test === 4294967295"));
engine.SetGlobalValue("test", 1);
Assert.AreEqual(true, engine.Evaluate("test === 1.0"));
engine.SetGlobalValue("test", "test");
Assert.AreEqual(true, engine.Evaluate("test === 'test'"));
engine.SetGlobalValue("test", engine.String.Construct("test"));
Assert.AreEqual(true, engine.Evaluate("test == 'test'"));
engine.SetGlobalValue("test", new CustomObjectInstance(engine));
Assert.AreEqual(true, engine.Evaluate("test.a() === 5"));
Assert.AreEqual(true, engine.Evaluate("test.Test1 === undefined"));
engine.SetGlobalValue("test", new InheritedObjectInstance1(engine));
Assert.AreEqual(true, engine.Evaluate("test.a() === 5"));
Assert.AreEqual(true, engine.Evaluate("test.b() === 6"));
engine.SetGlobalValue("test", new InheritedObjectInstance2(engine));
Assert.AreEqual(true, engine.Evaluate("test.a() === 7"));
}
示例5: SetGlobalValueClassInstance
public void SetGlobalValueClassInstance()
{
var engine = new ScriptEngine();
engine.EnableExposedClrTypes = false;
TestUtils.ExpectException<JavaScriptException>(() => engine.SetGlobalValue("TestInstance", typeof(TestInstance)));
TestUtils.ExpectException<JavaScriptException>(() => engine.SetGlobalValue("TestInstance2", typeof(TestInstance2)));
engine.EnableExposedClrTypes = true;
// Try setting up some types.
engine.SetGlobalValue("TestInstance", typeof(TestInstance));
engine.SetGlobalValue("TestInstance2", typeof(TestInstance2));
// Constructor.
engine.Execute("var instance = new TestInstance(5)");
// Keys.
Assert.AreEqual("Add,Equals,ReferenceEquals,length,length,name,name", engine.Evaluate("var keys = []; x = TestInstance; while (x != null) { keys = keys.concat(Object.getOwnPropertyNames(x)); x = Object.getPrototypeOf(x); } keys.sort().toString()"));
Assert.AreEqual("Add,Equals,GetHashCode,GetType,ToString,ToString,Value,value", engine.Evaluate("var keys = []; x = instance; while (x != null) { keys = keys.concat(Object.getOwnPropertyNames(x)); x = Object.getPrototypeOf(x); } keys.sort().toString()"));
// Property
Assert.AreEqual(5, engine.Evaluate("instance.Value"));
engine.Execute("instance.Value = 6");
Assert.AreEqual(6, engine.Evaluate("instance.Value"));
// Field
Assert.AreEqual(6, engine.Evaluate("instance.value"));
engine.Execute("instance.value = 7");
Assert.AreEqual(7, engine.Evaluate("instance.value"));
// Instance method.
engine.Execute("instance.Add(10)");
Assert.AreEqual(17, engine.Evaluate("instance.Value"));
Assert.AreEqual("17", engine.Evaluate("instance.ToString()"));
// Static method.
engine.Execute("var instance2 = TestInstance.Add(instance, instance)");
Assert.AreEqual(17, engine.Evaluate("instance.Value"));
Assert.AreEqual(34, engine.Evaluate("instance2.Value"));
// Constructor overloading.
engine.Execute("instance2 = new TestInstance(instance)");
Assert.AreEqual(17, engine.Evaluate("instance2.Value"));
// Types get unwrapped when they are returned from ScriptEngine APIs.
Assert.AreEqual(typeof(TestInstance), engine.Evaluate("TestInstance"));
Assert.AreEqual(typeof(TestInstance), engine.GetGlobalValue("TestInstance"));
// Class with no constructors.
engine.Execute("var instance2 = new TestInstance2()");
Assert.AreEqual(0, engine.Evaluate("instance2.value"));
}
示例6: Construct
/// <summary>
/// Creates and configures a Jurassic ScriptEngine
/// </summary>
/// <param name="context">The request/response context to process</param>
/// <returns>A RaptorJS-configured Jurassic ScriptEngine instance</returns>
public static ScriptEngine Construct(HttpListenerContext context)
{
ScriptEngine engine = new ScriptEngine();
engine.SetGlobalValue("Request", new RequestInstance(engine, context.Request));
engine.SetGlobalValue("Response", new ResponseInstance(engine, context.Response));
engine.SetGlobalValue("console", new ConsoleInstance(engine));
engine.SetGlobalValue("file", new FileSystemInstance(engine));
engine.SetGlobalFunction("require", new Action<string>((path) => engine.ExecuteFile(path)));
engine.SetGlobalFunction("require", new Func<string, object>((path) => engine.Evaluate(new FileScriptSource(path))));
return engine;
}
示例7: Awake
void Awake()
{
scriptEngine = new ScriptEngine();
scriptEngine.SetGlobalValue("console", new JSConsole(scriptEngine));
scriptEngine.SetGlobalValue("me", new JSPlayer(scriptEngine, Player));
foreach (var script in Scripts) {
Debug.Log("Running " + script.name);
var scriptFunc = scriptEngine.Evaluate<ObjectInstance>("({run: function(game){" + script.text + "}})");
var game = new JSGame(scriptEngine, Context);
(scriptFunc["run"] as FunctionInstance).Call(null, game); //Will eventually be a script instance, game context
}
}
示例8: BigNumber
public void BigNumber()
{
var engine = new ScriptEngine();
engine.SetGlobalValue("console", new Jurassic.Library.FirebugConsole(engine));
engine.ExecuteFile(@"..\..\..\Unit Tests\Real-world\Files\bignumber.js");
engine.EnableDebugging = true;
Assert.AreEqual("129963294281830400", engine.Evaluate(@"new BigNumber('129963294281830401').subtract(1).toString()"));
}
示例9: Setup
public void Setup()
{
engine = new ScriptEngine { EnableDebugging = true };
engine.SetGlobalValue("nativeModule", new NativeModuleInstance(engine));
engine.SetGlobalValue("console", new FirebugConsole(engine));
engine.SetGlobalFunction("native_require", new Func<string, ObjectInstance>(identifier =>
{
switch (identifier)
{
case "nativeModule":
return new NativeModuleInstance(engine);
default:
throw new ArgumentOutOfRangeException(string.Format("Unsupported module name: {0}", identifier));
}
}));
engine.ExecuteFile("./Builtin/require.js");
}
示例10: Awake
// Use this for initialization
void Awake()
{
ScriptEngine = new ScriptEngine();
ScriptEngine.SetGlobalValue("console", new JSConsole(ScriptEngine));
BlockTypes = new ObservableDictionary<string, JSBlockType>();
BlockTypes.OnAdded += HandleBlockTypesOnAdded;
Map = new JSMap(ScriptEngine);
}
示例11: Colors
public Colors(ScriptEngine engine)
: base(engine)
{
engine.SetGlobalValue("Color", new ColorConstructor(engine));
foreach (string key in _colorSet.Keys)
{
ColorInstance instance = new ColorInstance(engine, key, _colorSet[key]);
this.DefineProperty(key, new PropertyDescriptor(instance, PropertyAttributes.Enumerable | PropertyAttributes.Sealed), false);
}
}
示例12: Uglify
public string Uglify(string script)
{
string uglifyCode = new StringBuilder()
.AppendLine("var exports = {}; function require() { return exports; };")
.AppendLine(ReadResource("parse-js.js"))
.AppendLine(ReadResource("process.js"))
.AppendLine("value = gen_code(ast_squeeze(ast_mangle(parse(value))));")
.ToString();
var engine = new ScriptEngine();
engine.SetGlobalValue("value", script);
engine.Execute(uglifyCode);
return engine.GetGlobalValue<string>("value");
}
示例13: SerializeEngine
public void SerializeEngine()
{
// Set up a script engine.
var scriptEngine = new ScriptEngine();
scriptEngine.SetGlobalValue("test", "one");
// Attempt to serialize and then deserialize the entire script engine.
var scriptEngine2 = (ScriptEngine)Clone(scriptEngine);
// Verify it was deserialized correctly.
Assert.AreEqual("one", scriptEngine2.GetGlobalValue<string>("test"));
Assert.AreEqual(scriptEngine2, scriptEngine2.Global.Engine);
}
示例14: JsEngine
public JsEngine(Main main)
{
_scriptEngine = new ScriptEngine();
_scriptEngine.SetGlobalFunction("Pause", new Action<int>(Utils.Pause));
_scriptEngine.SetGlobalValue("Bebop", new Api(main,_scriptEngine));
_scriptEngine.SetGlobalValue("console", new Jurassic.Library.FirebugConsole(_scriptEngine));
//_scriptEngine.EnableDebugging = true;
//_scriptEngine.ExecuteFile("Test.js");
string windows = "";
foreach (Windows w in Enum.GetValues(typeof(Windows)))
{
if (windows != "") windows += ",";
windows += Enum.GetName(typeof(Windows), w) + " : " + (int)w;
}
_scriptEngine.Execute("var Windows={ " + windows + " };");
}
示例15: JsHost
public JsHost(ScriptEngine engine, IJinxBotClient client)
{
Debug.Assert(engine != null);
Debug.Assert(client != null);
_engine = engine;
_client = client;
_jsClient = new JsJinxBotClientObjectInstance(client, this, engine);
_engine.SetGlobalValue("Colors", new Colors(engine));
_engine.SetGlobalValue("CssClasses", new CssClasses(engine));
_engine.SetGlobalFunction("send", (Action<string>)send);
_engine.SetGlobalFunction("addChat", (Action<ArrayInstance>)addChat);
_engine.SetGlobalFunction("format", (Func<string, ArrayInstance, string>)format);
_engine.SetGlobalFunction("setTimeout", (Func<double, ObjectInstance, int>)setTimeout);
_engine.SetGlobalFunction("clearTimeout", (Action<int>)clearTimeout);
_engine.SetGlobalFunction("setInterval", (Func<double, ObjectInstance, int>)setInterval);
_engine.SetGlobalFunction("clearInterval", (Action<int>)clearInterval);
FunctionInstance instance = engine.Evaluate(DUMP_FUNC) as FunctionInstance;
engine.SetGlobalValue("dump", instance);
engine.SetGlobalValue("client", _jsClient);
}