NLua Example

https://github.com/NLua/NLua


Example:You can use/instantiate any .NET class without any previous registration or annotation.

    public class SomeClass
    {
        public string MyProperty {get; private set;}

        public SomeClass (string param1 = "defaulValue")
        {
            MyProperty = param1;
        }

        public int Func1 ()
        {
            return 32;
        }

        public string AnotherFunc (int val1, string val2)
        {
            return "Some String";
        }

        public static string StaticMethod (int param)
        {
            return "Return of Static Method";
        }

Creating Lua state:

    using NLua;

    Lua state = new Lua ()

Evaluating simple expressions:

    var res = state.DoString ("return 10 + 3*(5 + 2)")[0] as double;
    // Lua can return multiple values, for this reason DoString return a array of objects

Passing raw values to the state:

    double val = 12.0;
    state ["x"] = val; // Create a global value 'x' 
    var res = state.DoString ("return 10 + x*(5 + 2)")[0] as double;

Retrieving global values:

    state.DoString ("y = 10 + x*(5 + 2)");
    var y = state ["y"] as double; // Retrieve the value of y

Retrieving Lua functions:

    state.DoString (@"
    function ScriptFunc (val1, val2)
        if val1 > val2 then
            return val1 + 1
        else
            return val2 - 1
        end
    end
    ");
    var scriptFunc = state ["ScriptFunc"] as LuaFunction;
    var res = (int)scriptFunc.Call (3, 5).First ();
    // LuaFunction.Call will also return a array of objects, since a Lua function
    // can return multiple values

Using the .NET objects.

Passing .NET objects to the state:

    SomeClass obj = new SomeClass ("Param");
    state ["obj"] = obj; // Create a global value 'obj' of .NET type SomeClass 
    // This could be any .NET object, from BCL or from your assemblies

Using .NET assemblies inside Lua:

To access any .NET assembly to create objects, events etc inside Lua you need to ask NLua to use CLR as a Lua package.To do this just use the method LoadCLRPackage and use the import function inside your Lua script to load the Assembly.

    state.LoadCLRPackage ();
    state.DoString (@" import ('MyAssembly', 'MyNamespace') 
               import ('System.Web') ");
    // import will load any .NET assembly and they will be available inside the Lua context.

Creating .NET objects:To create object you only need to use the class name with the ().

state.DoString (@"
     obj2 = SomeClass() -- you can suppress default values.
     client = WebClient()
    ");

Calling instance methods:To call instance methods you need to use the : notation, you can call methods from objects passed to Lua or to objects created inside the Lua context.

    state.DoString (@"
    local res1 = obj:Func1()
    local res2 = obj2:AnotherFunc (10, 'hello')
    local res3 = client:DownloadString('http://nlua.org')
    ");

Calling static methods:You can call static methods using only the class name and the . notation from Lua.

    state.DoString (@"
    local res4 = SomeClass.StaticMethod(4)
    ");

Calling properties:You can get (or set) any property using . notation from Lua.

    state.DoString (@"
    local res5 = obj.MyProperty
    ");

All methods, events or property need to be public available, NLua will fail to call non-public members.

If you are using Xamarin.iOS you need to Preserve the class you want to use inside NLua, otherwise the Linker will remove the class from final binary if the class is not in use.

Sandboxing

There is many ways to sandbox scripts inside your application. I strongly recomend you to use plain Lua to do your sandbox.You can re-write the import function before load the user script and if the user try to import a .NET assembly nothing will happen.

    state.DoString (@"
        import = function () end
    ");

Lua-Sandbox user-list

Copyright (c) 2014 Vinicius Jarina (viniciusjarina@gmail.com)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值