本文整理汇总了C#中ScriptEngine.GetGlobalValue方法的典型用法代码示例。如果您正苦于以下问题:C# ScriptEngine.GetGlobalValue方法的具体用法?C# ScriptEngine.GetGlobalValue怎么用?C# ScriptEngine.GetGlobalValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ScriptEngine
的用法示例。
在下文中一共展示了ScriptEngine.GetGlobalValue方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Scriptable
public Scriptable()
{
engine = new ScriptEngine();
engine.Evaluate("funcccc = function() { return 3 * 3; }");
object funccc = engine.GetGlobalValue("funcccc");
engine.Evaluate("funcccc = function() { return 9 * 9; }");
loopFuncs = new List<MGFunction>();
timeAtLastUpdate = System.DateTime.Now;
}
示例2: SerializeFunction
public void SerializeFunction()
{
// Set up a script engine.
var scriptEngine = new ScriptEngine();
scriptEngine.Execute(@"function outer(a, b) { function inner() { return a + b; } return inner(); }");
// Attempt to serialize and then deserialize the function.
ScriptEngine.DeserializationEnvironment = scriptEngine;
var function = (FunctionInstance)Clone(scriptEngine.GetGlobalValue("outer"));
// Verify it was deserialized correctly.
Assert.AreEqual(11.0, function.Call(null, 5, 6));
}
示例3: 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");
}
示例4: GetFunctionIL
private static string GetFunctionIL(string code, string functionName)
{
var scriptEngine = new ScriptEngine();
scriptEngine.EnableILAnalysis = true;
scriptEngine.Execute(code);
var function = (Jurassic.Library.UserDefinedFunction)scriptEngine.GetGlobalValue(functionName);
if (function == null)
throw new ArgumentException(string.Format("The function {0} was not found.", functionName));
return TestUtils.NormalizeText(function.DisassembledIL);
}
示例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: SerializeObject
public void SerializeObject()
{
var scriptEngine = new ScriptEngine();
ScriptEngine.DeserializationEnvironment = scriptEngine;
// Create a test object.
scriptEngine.Execute(@"
_obj = { };
Object.defineProperty(_obj, 'a', { configurable: true, writable: true, enumerable: true, value: 5 });
Object.defineProperty(_obj, 'b', { configurable: false, writable: false, enumerable: false, value: 10 });
Object.defineProperty(_obj, 'c', { configurable: true, enumerable: true, get: function() { return 3; } });
Object.defineProperty(_obj, 'd', { configurable: true, enumerable: true, get: function() { return this._value; }, set: function(value) { this._value = value + 1 } });
_obj.nested = { a: 1 };");
// Clone the object using serialization.
scriptEngine.SetGlobalValue("_obj2", Clone(scriptEngine.GetGlobalValue("_obj")));
// Check the cloned object is not simply a pointer to the old object.
Assert.AreEqual(true, scriptEngine.Evaluate("delete _obj.e; _obj2.e = 11; _obj.e === undefined"));
// Check the properties have been cloned successfully.
Assert.AreEqual(5, scriptEngine.Evaluate("_obj2.a"));
scriptEngine.Execute("var descriptor = Object.getOwnPropertyDescriptor(_obj2, 'a')");
Assert.AreEqual(true, scriptEngine.Evaluate("descriptor.configurable"));
Assert.AreEqual(true, scriptEngine.Evaluate("descriptor.writable"));
Assert.AreEqual(true, scriptEngine.Evaluate("descriptor.enumerable"));
Assert.AreEqual(5, scriptEngine.Evaluate("descriptor.value"));
// Check the properties have been cloned successfully.
Assert.AreEqual(10, scriptEngine.Evaluate("_obj2.b"));
scriptEngine.Execute("var descriptor = Object.getOwnPropertyDescriptor(_obj2, 'b')");
Assert.AreEqual(false, scriptEngine.Evaluate("descriptor.configurable"));
Assert.AreEqual(false, scriptEngine.Evaluate("descriptor.writable"));
Assert.AreEqual(false, scriptEngine.Evaluate("descriptor.enumerable"));
Assert.AreEqual(10, scriptEngine.Evaluate("descriptor.value"));
// Check the properties have been cloned successfully.
Assert.AreEqual(3, scriptEngine.Evaluate("_obj2.c"));
scriptEngine.Execute("var descriptor = Object.getOwnPropertyDescriptor(_obj2, 'c')");
Assert.AreEqual(true, scriptEngine.Evaluate("descriptor.configurable"));
Assert.AreEqual(true, scriptEngine.Evaluate("descriptor.enumerable"));
Assert.AreEqual("function", scriptEngine.Evaluate("typeof descriptor.get"));
// Check the properties have been cloned successfully.
Assert.AreEqual(11, scriptEngine.Evaluate("_obj2.d = 10; _obj2.d"));
scriptEngine.Execute("var descriptor = Object.getOwnPropertyDescriptor(_obj2, 'd')");
Assert.AreEqual(true, scriptEngine.Evaluate("descriptor.configurable"));
Assert.AreEqual(true, scriptEngine.Evaluate("descriptor.enumerable"));
Assert.AreEqual("function", scriptEngine.Evaluate("typeof descriptor.get"));
Assert.AreEqual("function", scriptEngine.Evaluate("typeof descriptor.set"));
// Check the properties have been cloned successfully.
Assert.AreEqual(1, scriptEngine.Evaluate("_obj2.nested.a"));
// Make sure the extensible flag works.
scriptEngine.Execute(@"
_obj3 = { };
Object.preventExtensions(_obj3);");
// Clone the object using serialization.
scriptEngine.SetGlobalValue("_obj4", Clone(scriptEngine.GetGlobalValue("_obj3")));
// Check the flag was cloned successfully.
Assert.AreEqual(true, scriptEngine.Evaluate("Object.isExtensible(_obj)"));
Assert.AreEqual(true, scriptEngine.Evaluate("Object.isExtensible(_obj2)"));
Assert.AreEqual(false, scriptEngine.Evaluate("Object.isExtensible(_obj3)"));
Assert.AreEqual(false, scriptEngine.Evaluate("Object.isExtensible(_obj4)"));
}
示例7: RunScript
private ArrayInstance RunScript(GroupChatMessageArrived eventObject, string js)
{
var jsEngine = new ScriptEngine();
jsEngine.ExecuteFile(Path.Combine(BaseScriptFolder, "hubotScriptInvoker.js"));
jsEngine.Execute(js);
jsEngine.SetGlobalValue("messageBody", eventObject.Message.Body);
jsEngine.Execute(GetJSInvoker(eventObject));
jsEngine.Execute("module.exports(bot);");
var result = jsEngine.GetGlobalValue("messages") as ArrayInstance;
return result;
}