Ejemplo n.º 1
0
/*
* Get an actionscript class with the given namespace and class name
* in the format: package::ClassName
*/
AS3_Val get_class(const char * as_namespaceclass_path)
{
  /* TODO: Merge with get_class2()? */

  AS3_Val as_namespace;
  AS3_Val as_class;
  AS3_Val ret;
  char * class_ptr = NULL;

  /* TODO might want to store classes in a table to save loading again */

  class_ptr = strstr(as_namespaceclass_path, "::");

  if (class_ptr > as_namespaceclass_path)
  {
    as_namespace = AS3_StringN(
        as_namespaceclass_path, (class_ptr - as_namespaceclass_path) / sizeof(char)
      );
    as_class = AS3_String(class_ptr + 2);
  }
  else
  {
    as_namespace = AS3_Undefined();
    as_class = AS3_String(as_namespaceclass_path);
  }

  ret = AS3_NSGet(as_namespace, as_class);

  /* TODO check for failure getting class */

  AS3_Release(as_namespace);
  AS3_Release(as_class);

  return ret;
}
Ejemplo n.º 2
0
/*
* Take the Lua stack item at index i and convert it into an
* ActionScript value.
*/
AS3_Val get_as3_value_from_lua_stack_type(lua_State * L, int i, int type)
{
  /* WARNING: Panic alert! Use L*_FN checkers here! */

  LCALL(L, stack);

  AS3_Val value;
  switch (type)
  {
    case LUA_TSTRING:  /* strings */
      {
        size_t length = 0;
        const char * str = lua_tolstring(L, i, &length);
        if (str == NULL)  /* NOTE: This is unreachable. Assert instead */
        {
          length = 6;
          str = "(null)";
        }
        /* NOTE: Alchemy .5a truncates embedded zeroes in string
        * regardless to the passed length
        */
        value = AS3_StringN(str, length);
      }
      break;

    case LUA_TBOOLEAN:  /* booleans */
      value = lua_toboolean(L, i) ? AS3_True() : AS3_False();
      break;

    case LUA_TNUMBER:  /* numbers */
      value = AS3_Number(lua_tonumber(L, i));
      break;

    case LUA_TNONE: /* fall through */
    case LUA_TNIL:  /* nil */
      value = AS3_Null();
      break;

    case LUA_TUSERDATA:  /* userdata */
      {
        void * userdata = lua_touserdata(L, i);
        lua_getfield(L, LUA_REGISTRYINDEX, AS3LUA_METATABLE);
        if (userdata == NULL || !lua_getmetatable(L, i))
        {
          lua_pop(L, 1); /* Pop AS3LUA_METATABLE */
          value = as3_value_from_foreign_userdata(L, i);
        }
        else if (!lua_rawequal(L, -2, -1))
        {
          lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */
          value = as3_value_from_foreign_userdata(L, i);
        }
        else
        {
          lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */
          AS3LuaUserData * userdata = (AS3LuaUserData *)lua_touserdata(L, i);
          value = userdata->value;
          /*
          * We just created one more reference to the AS3 value,
          * as it still lives inside Lua.
          * (And will probably be collected by GC.)
          */
          AS3_Acquire(value);
        }
      }
      break;

    case LUA_TFUNCTION: /* function */
      value = setup_callback(L, i);
      break;

    case LUA_TLIGHTUSERDATA: /* TODO: blackbox this type */
    case LUA_TTABLE: /* TODO: deal with this type */
    case LUA_TTHREAD: /* TODO: blackbox this type */
      value = AS3_String(lua_typename(L, type));
      break;

    default:  /* unreachable */
      fatal_error("unknown Lua type");
      break;
  }

#ifdef DO_SPAM
  SPAM(("get_as3_value_from_lua_stack(): end"));
  AS3_Trace(
      AS3_Call(
          getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", value)
        )
    );
#endif /* DO_SPAM */

  LCHECK_FN(L, stack, 0, fatal_error);

  return value;
}