Skip to content

LuaState 集合维护有问题,当有多个LuaState,关闭再创建的时候会有问题 #17

Description

@xiaoyaofeng

LuaStateFactory.java update

/*

  • $Id: LuaStateFactory.java,v 1.4 2006/12/22 14:06:40 thiago Exp $
  • Copyright (C) 2003-2007 Kepler Project.
    *
  • Permission is hereby granted, free of charge, to any person obtaining
  • a copy of this software and associated documentation files (the
  • "Software"), to deal in the Software without restriction, including
  • without limitation the rights to use, copy, modify, merge, publish,
  • distribute, sublicense, and/or sell copies of the Software, and to
  • permit persons to whom the Software is furnished to do so, subject to
  • the following conditions:
    *
  • The above copyright notice and this permission notice shall be
  • included in all copies or substantial portions of the Software.
    *
  • THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  • EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  • MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  • IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  • CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  • TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  • SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    */

package org.keplerproject.luajava;

import java.util.ArrayList;
import java.util.List;

/**

  • This class is responsible for instantiating new LuaStates.

  • When a new LuaState is instantiated it is put into a List

  • and an index is returned. This index is registred in Lua

  • and it is used to find the right LuaState when lua calls

  • a Java Function.

  • @author Thiago Ponte
    /
    public final class LuaStateFactory
    {
    /
    *

    • Array with all luaState's instances
      */
      private static final List states = new ArrayList();

    /**

    • Non-public constructor.
      */
      private LuaStateFactory()
      {}

    /**

    • Method that creates a new instance of LuaState

    • @return LuaState
      */
      public synchronized static LuaState newLuaState()
      {
      int idx = getNextStateIndex();
      LuaState L = new LuaState(idx);

      int size = states.size();
      if (idx>=size ) {//当集合为空的时候 i== 0 && size == 0
      states.add(idx, L);
      }else if (idx<size && idx >= 0) {//当集合的
      states.set(idx, L) ;
      } else {
      //异常情况
      }
      return L;
      }

    /**

    • Returns a existing instance of LuaState
    • @PARAM index
    • @return LuaState
      */
      public synchronized static LuaState getExistingState(int index)
      {
      return (LuaState) states.get(index);
      }

    /**

    • Receives a existing LuaState and checks if it exists in the states list.

    • If it doesn't exist adds it to the list.

    • @PARAM L

    • @return int
      */
      public synchronized static int insertLuaState(LuaState L)
      {
      int idx;
      for (idx = 0 ; idx < states.size() ; idx++)
      {
      LuaState state = (LuaState) states.get(idx);

      if (state != null)
      {
          if (state.getCPtrPeer() == L.getCPtrPeer())
              return idx;
      }
      

      }

      idx = getNextStateIndex();

      int size = states.size();
      if (idx>=size ) {//当集合为空的时候 i== 0 && size == 0
      states.add(idx, L);
      }else if (idx<size && idx >= 0) {//
      states.set(idx, L) ;
      } else {
      //异常情况
      }

      return idx;
      }

    /**

    • removes the luaState from the states list
    • @PARAM idx
      */
      public synchronized static void removeLuaState(int idx)
      {
      int size = states.size();
      if (idx<size) {
      states.set(idx, null);
      }else {
      states.add(idx, null);
      }
      }

    /**

    • Get next available index

    • @return int
      */
      private synchronized static int getNextStateIndex()
      {
      int i;
      for ( i=0 ; i < states.size() && states.get(i) != null ; i++ );

      return i;
      }
      }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions