本文整理汇总了C#中System.Script.DoString方法的典型用法代码示例。如果您正苦于以下问题:C# Script.DoString方法的具体用法?C# Script.DoString怎么用?C# Script.DoString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Script
的用法示例。
在下文中一共展示了Script.DoString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TableAddWithMetatable
public void TableAddWithMetatable()
{
string script = @"
v1 = { 'aaaa' }
v2 = { 'aaaaaa' }
meta = { }
function meta.__add(t1, t2)
local o1 = #t1[1];
local o2 = #t2[1];
return o1 * o2;
end
setmetatable(v1, meta);
return(v1 + v2);";
var S = new Script();
Table globalCtx = S.Globals;
globalCtx.RegisterModuleType<TableIteratorsModule>();
globalCtx.RegisterModuleType<MetaTableModule>();
DynValue res = S.DoString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(24, res.Number);
}
示例2: Test_ConstIntFieldSetter
public void Test_ConstIntFieldSetter(InteropAccessMode opt)
{
try
{
string script = @"
myobj.ConstIntProp = 1;
return myobj.ConstIntProp;";
Script S = new Script();
SomeClass obj = new SomeClass() { IntProp = 321 };
UserData.UnregisterType<SomeClass>();
UserData.RegisterType<SomeClass>(opt);
S.Globals.Set("myobj", UserData.Create(obj));
DynValue res = S.DoString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(115, res.Number);
}
catch (ScriptRuntimeException)
{
return;
}
Assert.Fail();
}
示例3: Do
void Do(string code, Action<DynValue, RegCollMethods> asserts)
{
try
{
UserData.RegisterType<RegCollMethods>();
UserData.RegisterType<RegCollItem>();
UserData.RegisterType(typeof(IList<>));
Script s = new Script();
var obj = new RegCollMethods();
s.Globals["o"] = obj;
s.Globals["ctor"] = UserData.CreateStatic<RegCollItem>();
DynValue res = s.DoString(code);
asserts(res, obj);
}
catch (ScriptRuntimeException ex)
{
Debug.WriteLine(ex.DecoratedMessage);
throw;
}
finally
{
UserData.UnregisterType<RegCollMethods>();
UserData.UnregisterType<RegCollItem>();
UserData.UnregisterType<Array>();
UserData.UnregisterType(typeof(IList<>));
UserData.UnregisterType(typeof(IList<RegCollItem>));
UserData.UnregisterType(typeof(IList<int>));
//UserData.UnregisterType<IEnumerable>();
}
}
示例4: TailCallFromCLR
public void TailCallFromCLR()
{
string script = @"
function getResult(x)
return 156*x;
end
return clrtail(9)";
Script S = new Script();
S.Globals.Set("clrtail", DynValue.NewCallback((xc, a) =>
{
DynValue fn = S.Globals.Get("getResult");
DynValue k3 = DynValue.NewNumber(a[0].Number / 3);
return DynValue.NewTailCallReq(fn, k3);
}));
var res = S.DoString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(468, res.Number);
}
示例5: DoTest
private void DoTest(string code, string expectedResult)
{
Script S = new Script();
S.DoString(@"
function f(a,b)
local debug = 'a: ' .. tostring(a) .. ' b: ' .. tostring(b)
return debug
end
function g(a, b, ...)
local debug = 'a: ' .. tostring(a) .. ' b: ' .. tostring(b)
local arg = {...}
debug = debug .. ' arg: {'
for k, v in pairs(arg) do
debug = debug .. tostring(v) .. ', '
end
debug = debug .. '}'
return debug
end
function r()
return 1, 2, 3
end
function h(...)
return g(...)
end
function i(...)
return g('extra', ...)
end
");
DynValue res = S.DoString("return " + code);
Assert.AreEqual(res.Type, DataType.String);
Assert.AreEqual(expectedResult, res.String);
}
示例6: PCallMultipleReturns
public void PCallMultipleReturns()
{
string script = @"return pcall(function() return 1,2,3 end)";
Script S = new Script();
var res = S.DoString(script);
Assert.AreEqual(DataType.Tuple, res.Type);
Assert.AreEqual(4, res.Tuple.Length);
Assert.AreEqual(true, res.Tuple[0].Boolean);
Assert.AreEqual(1, res.Tuple[1].Number);
Assert.AreEqual(2, res.Tuple[2].Number);
Assert.AreEqual(3, res.Tuple[3].Number);
}
示例7: Script_LoadFunc
private DynValue Script_LoadFunc(string script, string funcname)
{
Script s1 = new Script();
DynValue v1 = s1.DoString(script);
DynValue func = s1.Globals.Get(funcname);
using (MemoryStream ms = new MemoryStream())
{
s1.Dump(func, ms);
ms.Seek(0, SeekOrigin.Begin);
Script s2 = new Script();
return s2.LoadStream(ms);
}
}
示例8: IndexerTest
private void IndexerTest(string code, int expected)
{
Script S = new Script();
IndexerTestClass obj = new IndexerTestClass();
UserData.RegisterType<IndexerTestClass>();
S.Globals.Set("o", UserData.Create(obj));
DynValue v = S.DoString(code);
Assert.AreEqual(DataType.Number, v.Type);
Assert.AreEqual(expected, v.Number);
}
示例9: CSharpStaticFunctionCall4
public void CSharpStaticFunctionCall4()
{
string script = "return callback()();";
var callback2 = DynValue.NewCallback(new CallbackFunction((_x, a) => { return DynValue.NewNumber(1234.0); }));
var callback = DynValue.NewCallback(new CallbackFunction((_x, a) => { return callback2; }));
var S = new Script();
S.Globals.Set("callback", callback);
DynValue res = S.DoString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(1234.0, res.Number);
}
示例10: ProxyTest
public void ProxyTest()
{
UserData.RegisterProxyType<Proxy, Random>(r => new Proxy(r));
Script S = new Script();
S.Globals["R"] = new Random();
S.Globals["func"] = (Action<Random>)(r => { Assert.IsNotNull(r); Assert.IsTrue(r is Random); });
S.DoString(@"
x = R.GetValue();
func(R);
");
Assert.AreEqual(3.0, S.Globals.Get("x").Number);
}
示例11: Test
private MyClass Test(string tableDef)
{
Script s = new Script(CoreModules.None);
DynValue table = s.DoString("return " + tableDef);
Assert.AreEqual(DataType.Table, table.Type);
PropertyTableAssigner<MyClass> pta = new PropertyTableAssigner<MyClass>("class");
MyClass o = new MyClass();
pta.AssignObject(o, table.Table);
return o;
}
示例12: CSharpStaticFunctionCallRedef
public void CSharpStaticFunctionCallRedef()
{
IList<DynValue> args = null;
string script = "local print = print; print(\"hello\", \"world\");";
var S = new Script();
S.Globals.Set("print", DynValue.NewCallback(new CallbackFunction((_x, a) => { args = a.GetArray(); return DynValue.NewNumber(1234.0); })));
DynValue res = S.DoString(script);
Assert.AreEqual(2, args.Count);
Assert.AreEqual(DataType.String, args[0].Type);
Assert.AreEqual("hello", args[0].String);
Assert.AreEqual(DataType.String, args[1].Type);
Assert.AreEqual("world", args[1].String);
Assert.AreEqual(DataType.Void, res.Type);
}
示例13: Test_IntPropertyGetter
public void Test_IntPropertyGetter(InteropAccessMode opt)
{
string script = @"
x = myobj.IntProp;
return x;";
Script S = new Script();
SomeClass obj = new SomeClass() { IntProp = 321 };
UserData.UnregisterType<SomeClass>();
UserData.RegisterType<SomeClass>(opt);
S.Globals.Set("myobj", UserData.Create(obj));
DynValue res = S.DoString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(321, res.Number);
}
示例14: TcoTest_Pre
public void TcoTest_Pre()
{
// this just verifies the algorithm for TcoTest_Big
string script = @"
function recsum(num, partial)
if (num == 0) then
return partial
else
return recsum(num - 1, partial + num)
end
end
return recsum(10, 0)";
Script S = new Script();
var res = S.DoString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(55, res.Number);
}
示例15: TcoTest_Big
public void TcoTest_Big()
{
// calc the sum of the first N numbers in the most stupid way ever to waste stack and trigger TCO..
// (this could be a simple X*(X+1) / 2... )
string script = @"
function recsum(num, partial)
if (num == 0) then
return partial
else
return recsum(num - 1, partial + num)
end
end
return recsum(70000, 0)";
Script S = new Script();
var res = S.DoString(script);
Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(2450035000.0, res.Number);
}