Giter Club home page Giter Club logo

Comments (3)

viniciusjarina avatar viniciusjarina commented on July 3, 2024 2
using System;
using System.Runtime.InteropServices;
using KeraLua;

namespace ConsoleApp1
{
    class Program
    {
        private  static readonly uint LimitMemory = 4000;

        private static uint _memory = 0;
        private static uint MemoryInUse
        {
            get => _memory;
            set
            {
                if (value > LimitMemory)
                    _lua.Error("Memory overflow");
                _memory = value;
            }
        }
        private static Lua _lua;
        static void Main(string[] args)
        {
            LuaAlloc myAlloc = MyAlloc;
            _lua = new Lua(myAlloc, IntPtr.Zero);
            bool res = _lua.DoString("x = 10* 10");
            res = _lua.DoString("x = 10* 10");
            res = _lua.DoString("x = 10* 10");
            res = _lua.DoString("x = 10* 10");
            _lua.GetGlobal("x");
            long x = _lua.ToInteger(-1);
        }

        private static IntPtr MyAlloc(IntPtr ud, IntPtr ptr, UIntPtr osize, UIntPtr nsize)
        {
            if (osize == nsize)
                return ptr;

            uint newSize = nsize.ToUInt32();

            if (nsize == UIntPtr.Zero)
            {
                Marshal.FreeHGlobal(ptr);

                MemoryInUse -= newSize;

                return IntPtr.Zero;
            }

            if (ptr == IntPtr.Zero)
            {
                MemoryInUse += newSize;
                return Marshal.AllocHGlobal((IntPtr) newSize);
            }

            return Marshal.ReAllocHGlobal(ptr, (IntPtr) newSize);
        }
    }
}

from keralua.

nicodrive avatar nicodrive commented on July 3, 2024

Fails when executed multiple times. "Process is terminating due to StackOverflowException."

using System;
using System.Runtime.InteropServices;
using KeraLua;
namespace ConsoleApp1
{
    class Program
    {
        private  static readonly uint LimitMemory = 400000;
        private static uint _memory = 0;
        private static uint MemoryInUse
        {
            get => _memory;
            set
            {
                if (value > LimitMemory)
                    _lua.Error("Memory overflow");
                _memory = value;
            }
        }
        private static Lua _lua;
        static void Main(string[] args)
        {
            LuaAlloc myAlloc = MyAlloc;
            _lua = new Lua(myAlloc, IntPtr.Zero);
            bool res = _lua.DoString("x = 10* 10");
            for (int i = 0; i < 1000000; i++)
            {
                res = _lua.DoString("x = 10* 10");    
            }
            _lua.GetGlobal("x");
            long x = _lua.ToInteger(-1);
        }

        private static IntPtr MyAlloc(IntPtr ud, IntPtr ptr, UIntPtr osize, UIntPtr nsize)
        {
            if (osize == nsize)
                return ptr;

            uint newSize = nsize.ToUInt32();

            if (nsize == UIntPtr.Zero)
            {
                Marshal.FreeHGlobal(ptr);

                MemoryInUse -= newSize;

                return IntPtr.Zero;
            }

            if (ptr == IntPtr.Zero)
            {
                MemoryInUse += newSize;
                return Marshal.AllocHGlobal((IntPtr) newSize);
            }

            return Marshal.ReAllocHGlobal(ptr, (IntPtr) newSize);
        }
    }
}

from keralua.

viniciusjarina avatar viniciusjarina commented on July 3, 2024

That's a bummer 😕
Have no idea why. If you figure how to do it let us know 😉

from keralua.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.