Exemple #1
0
        /// <summary>
        /// Executes the given string as a Lua chunk.
        /// </summary>
        /// <returns>The results.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="s"/> is <c>null</c>.</exception>
        /// <exception cref="LuaException">A Lua error occurs.</exception>
        /// <exception cref="ObjectDisposedException">The <see cref="Lua"/> environment is disposed.</exception>
        public object[] DoString(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }
            ThrowIfDisposed();

            if (LuaApi.LoadString(MainState, s) != LuaStatus.Ok)
            {
                var errorMessage = LuaApi.ToString(MainState, -1);
                LuaApi.Pop(MainState, 1);
                throw new LuaException(errorMessage);
            }

            return(Call(EmptyObjectArray));
        }
Exemple #2
0
        /// <summary>
        /// Creates a <see cref="LuaFunction"/> from the given string as a Lua chunk.
        /// </summary>
        /// <returns>The resulting <see cref="LuaFunction"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="s"/> is <c>null</c>.</exception>
        /// <exception cref="LuaException">A Lua error occurs.</exception>
        /// <exception cref="ObjectDisposedException">The <see cref="Lua"/> environment is disposed.</exception>
        public LuaFunction CreateFunction(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException(nameof(s));
            }
            ThrowIfDisposed();

            if (LuaApi.LoadString(MainState, s) != LuaStatus.Ok)
            {
                var errorMessage = LuaApi.ToString(MainState, -1);
                LuaApi.Pop(MainState, 1);
                throw new LuaException(errorMessage);
            }

            var result = (LuaFunction)ToObject(-1, LuaType.Function);

            LuaApi.Pop(MainState, 1);
            return(result);
        }