Beispiel #1
0
        void System.Collections.IEnumerator.Reset()
        {
            int err_func = load_error_func(L, errorFuncRef);

            lua_getref(L, _ref);
            lua_pushstring(L, "reset");

            if (LuaStatus.OK != LunaNative.luna_pgettable(L, -2))
            {
                ThrowExceptionFromError(L, err_func - 1);
            }

            if (!lua_isfunction(L, -1))
            {
                lua_pushstring(L, "no such function Reset");
                ThrowExceptionFromError(L, err_func - 1);
            }

            lua_pushvalue(L, -2);
            lua_remove(L, -3);

            if (lua_pcall(L, 1, 0, err_func) != LuaStatus.OK)
            {
                ThrowExceptionFromError(L, err_func - 1);
            }

            lua_settop(L, err_func - 1);
        }
Beispiel #2
0
        public void Start()
        {
            if (ReadBytes == null)
            {
                ReadBytes = System.IO.File.ReadAllBytes;
            }

            L         = Lua.newstate();
            mainState = L;

            luaL_openlibs(L);
            lua_atpanic(L, PanicCallback);

            Register("print", DoPrint);
            Register("dofile", DoFile);
            Register("loadfile", LoadFile);

            errorFuncRef = get_error_func_ref(L);

            AddAssembly(Assembly.GetExecutingAssembly());

            _global = LuaRef.Globals(L);
            _global.Set("luna", LuaRef.CreateTable(L));

            Register("luna.typeof", GetClassType);
            Register("luna.findType", FindClassType);

#if LUNA_SCRIPT
            DoString(coroutineSource);
            DoString(classSource);
            DoString(listSource);
#endif

            _binder = new SharpModule(this);

            AddSearcher(LuaLoader);

            LunaNative.Init(L);
            SharpObject.Init(L);

            RegisterWraps(this.GetType());

            foreach (var moduleInfo in this._config)
            {
                this.RegisterModel(moduleInfo);
            }

            PostInit?.Invoke();

            var it = _classWrapers.GetEnumerator();
            while (it.MoveNext())
            {
                if (!SharpModule.IsRegistered(it.Current.Key))
                {
                    RegisterClass(it.Current.Key);
                }
            }

            //_classWrapers.Clear();
        }
Beispiel #3
0
        public unsafe static void PushObject(lua_State L, object obj)
        {
            if (obj2id.TryGetValue(obj, out var key))
            {
                if (LunaNative.TryGetUserData(L, key, weakTableRef) == 1)
                {
                    return;
                }
            }

            int classId = TypeID(obj);

            IntPtr mem = lua_newuserdata(L, (UIntPtr)sizeof(long));
            int    id  = freeList.Alloc(obj);

            *(int *)mem       = 0;
            *(int *)(mem + 4) = id;

            obj2id[obj] = (int)id;

            LunaNative.CacheUserData(L, id, weakTableRef);

            lua_rawgeti(L, LUA_REGISTRYINDEX, classId);

            if (!lua_istable(L, -1))
            {
                Debug.LogWarning($"class not registered : {obj.GetType() }, id: {classId} obj: {obj}");
                lua_pop(L, 1);
                return;
            }

            lua_setmetatable(L, -2);
        }
Beispiel #4
0
        bool System.Collections.IEnumerator.MoveNext()
        {
            int err_func = load_error_func(L, errorFuncRef);

            lua_getref(L, _ref);
            lua_pushstring(L, "moveNext");

            if (LuaStatus.OK != LunaNative.luna_pgettable(L, -2))
            {
                ThrowExceptionFromError(L, err_func - 1);
            }

            if (!lua_isfunction(L, -1))
            {
                lua_pushstring(L, "no such function MoveNext");
                ThrowExceptionFromError(L, err_func - 1);
            }

            lua_pushvalue(L, -2);
            lua_remove(L, -3);

            if (lua_pcall(L, 1, 1, err_func) != LuaStatus.OK)
            {
                ThrowExceptionFromError(L, err_func - 1);
            }

            int ret = lua_toboolean(L, err_func + 1);

            lua_settop(L, err_func - 1);
            return(ret != 0);
        }
Beispiel #5
0
        public void SetReadOnly(string name)
        {
            LuaRef meta_class = meta;
            string full_name  = LunaNative.GetMemberName(meta_class, name);
            LuaRef err        = LuaRef.CreateFunctionWith(State, LunaNative.ErrorReadOnly, full_name);

            meta_class.RawGet(LunaNative.___setters).RawSet(name, err);
        }
Beispiel #6
0
 public override void Push(IntPtr L, T data)
 {
     if (newRef == -1)
     {
         LunaNative.luna_pushstruct(L, metaRef, (IntPtr)Unsafe.AsPointer(ref data), buffer.Addr, buffer.Count);
     }
     else
     {
         LunaNative.luna_packstruct(L, newRef, (IntPtr)Unsafe.AsPointer(ref data), buffer.Addr, buffer.Count);
     }
 }
Beispiel #7
0
        public override T Get(IntPtr L, int index)
        {
            T data = default;

            if (unpackRef == -1)
            {
                LunaNative.luna_getstruct(L, index, (IntPtr)Unsafe.AsPointer(ref data), buffer.Addr, buffer.Count);
            }
            else
            {
                LunaNative.luna_unpackstruct(L, index, unpackRef, (IntPtr)Unsafe.AsPointer(ref data), buffer.Addr, buffer.Count);
            }
            return(data);
        }
Beispiel #8
0
        void _Push(IntPtr L, object data)
        {
            byte *ptr = stackalloc byte[buffer.size];

            Marshal.StructureToPtr(data, (IntPtr)ptr, false);
            if (newRef == -1)
            {
                LunaNative.luna_pushstruct(L, metaRef, (IntPtr)ptr, buffer.Addr, buffer.Count);
            }
            else
            {
                LunaNative.luna_packstruct(L, newRef, (IntPtr)ptr, buffer.Addr, buffer.Count);
            }
        }
Beispiel #9
0
        public SharpModule(SharpModule parent, string name) : base(parent)
        {
            LuaRef parentMeta = parent.meta;
            LuaRef luaref     = parentMeta.RawGet(name) as LuaRef;

            if (luaref)
            {
                meta = luaref;
                return;
            }

            this.parent = parent;
            meta        = LunaNative.create_module(parentMeta.State, parentMeta, name);
            Name        = name;
        }
Beispiel #10
0
        object _Get(IntPtr L, int index)
        {
            byte *ptr = stackalloc byte[buffer.size];

            if (unpackRef == -1)
            {
                LunaNative.luna_getstruct(L, index, (IntPtr)ptr, buffer.Addr, buffer.Count);
            }
            else
            {
                LunaNative.luna_unpackstruct(L, index, unpackRef, (IntPtr)ptr, buffer.Addr, buffer.Count);
            }
            object boxed = Marshal.PtrToStructure((IntPtr)ptr, type);

            return(boxed);
        }
Beispiel #11
0
        public override void Push(IntPtr L, T data)
        {
            byte *ptr = stackalloc byte[buffer.size];

            buffer.Init(ptr);
            state = State.Writing;
            BuildStruct(ref data);

            if (newRef == -1)
            {
                LunaNative.luna_pushstruct(L, metaRef, (IntPtr)ptr, buffer.Addr, buffer.Count);
            }
            else
            {
                LunaNative.luna_packstruct(L, newRef, (IntPtr)ptr, buffer.Addr, buffer.Count);
            }
        }
Beispiel #12
0
        public override T Get(IntPtr L, int index)
        {
            byte *ptr = stackalloc byte[buffer.size];

            if (unpackRef == -1)
            {
                LunaNative.luna_getstruct(L, index, (IntPtr)ptr, buffer.Addr, buffer.Count);
            }
            else
            {
                LunaNative.luna_unpackstruct(L, index, unpackRef, (IntPtr)ptr, buffer.Addr, buffer.Count);
            }

            buffer.Init(ptr);
            state = State.Reading;
            T obj = default;

            BuildStruct(ref obj);
            return((T)obj);
        }
Beispiel #13
0
        public static LuaRef CreateClass(LuaRef module, string name, Type classType, Type superClass, LuaNativeFunction dctor)
        {
            LuaRef classref = module.RawGet <LuaRef, string>(name);

            if (classref)
            {
                return(classref);
            }

            var meta = LunaNative.create_class(module.State, module, name, classType, dctor);

            if (superClass != null)
            {
                LuaRef registry     = new LuaRef(module.State, LUA_REGISTRYINDEX);
                int    superClassID = SharpObject.TypeID(superClass);
                LuaRef super        = registry.RawGet <LuaRef>(superClassID);
                meta.RawSet(LunaNative.___super, super);
            }

            return(meta);
        }