Exemple #1
0
int luaL_ref(LuaThread* L) {

  LuaTable* registry = L->l_G->getRegistry();

  LuaValue val = L->stack_.top_[-1];
  L->stack_.pop();

  if(val.isNil()) return LUA_REFNIL;

  LuaValue ref1 = registry->get(LuaValue(freelist));
  int ref = ref1.isInteger() ? ref1.getInteger() : 0;

  if (ref != 0) {  // any free element?
    LuaValue temp = registry->get( LuaValue(ref) );
    registry->set( LuaValue(freelist), temp );
    registry->set( LuaValue(ref), val );
    return ref;
  }
  else  {
    // no free elements, get a new reference.
    ref = (int)registry->getLength() + 1;
    registry->set( LuaValue(ref), val );
    return ref;
  }
}
Exemple #2
0
void luaL_unref(LuaThread* L, int ref) {
  THREAD_CHECK(L);

  if(ref >= 0) {
    LuaTable* registry = L->l_G->getRegistry();
    LuaValue a = registry->get( LuaValue(freelist) );
    registry->set( LuaValue(ref), a );
    registry->set( LuaValue(freelist), LuaValue(ref) );
  }
}
Exemple #3
0
void luaL_openlibs (LuaThread *L) {
  THREAD_CHECK(L);

  LuaTable* loadedModules = L->l_G->getRegistryTable("_LOADED");
  LuaTable* globals = L->l_G->getGlobals();

  /* call open functions from 'loadedlibs' and set results to global table */
  for (const luaL_Reg* lib = loadedlibs; lib->func; lib++) {
      
    lib->func(L);
    loadedModules->set(lib->name, L->stack_.at(-1) );
    globals->set(lib->name, L->stack_.at(-1) );

    L->stack_.pop();  /* remove lib */
  }
}
Exemple #4
0
int luaopen_os (LuaThread *L) {
  THREAD_CHECK(L);

  LuaTable* lib = new LuaTable();
  for(const luaL_Reg* cursor = syslib; cursor->name; cursor++) {
    lib->set( cursor->name, LuaValue(cursor->func) );
  }
  L->stack_.push(lib);

  return 1;
}
Exemple #5
0
static void collectvalidlines (LuaThread *L, LuaClosure *f) {
  THREAD_CHECK(L);
  if (f == NULL || f->isC) {
    LuaResult result = L->stack_.push_reserve2(LuaValue::Nil());
    handleResult(result);
  }
  else {
    LuaTable* t = new LuaTable();  /* new table to store active lines */
    LuaResult result = L->stack_.push_reserve2(LuaValue(t));
    handleResult(result);

    LuaValue v = LuaValue(true);
    for (int i = 0; i < (int)f->proto_->lineinfo.size(); i++) {
      /* for all lines with code */
      LuaValue key(f->proto_->lineinfo[i]);
      t->set(key, v);  /* table[line] = true */
    }
  }
}