示例#1
0
static int luaopen_ltextextents (lua_State *L)
{
    int metatable, methods;

    /* create methods table, & add it to the table of globals */
    //hd.. luaL_openlib(L, YOUR_T, your_methods, 0);
	luaL_newlib(L, TextExtents_methods);
	lua_setglobal(L, "TextExtents");
    //luaL_register(L, LUACAIRO ".TextExtents", TextExtents_methods);
    methods = lua_gettop(L);

    /* create metatable for your_t, & add it to the registry */
    luaL_newmetatable(L, LUACAIRO ".TextExtents.mt");
    //hd.. luaL_openlib(L, 0, your_meta_methods, 0);  /* fill metatable */
	luaL_newlib(L, TextExtents_meta_methods);
	lua_setglobal(L, "TextExtents.mt");
    //luaL_register(L, NULL, TextExtents_meta_methods);
    metatable = lua_gettop(L);

    lua_pushliteral(L, "__metatable");
    lua_pushvalue(L, methods);           /* dup methods table*/
    lua_rawset(L, metatable);            /* hide metatable:
                                            metatable.__metatable = methods */
    lua_pushliteral(L, "__index");
    lua_pushvalue(L, metatable);         /* upvalue index 1 */
    Xet_add(L, TextExtents_getters);     /* fill metatable with getters */
    lua_pushvalue(L, methods);           /* upvalue index 2 */
    lua_pushcclosure(L, Xet_index_handler, 2);
    lua_rawset(L, metatable);            /* metatable.__index = index_handler */

    lua_pushliteral(L, "__newindex");
    lua_newtable(L);                     /* table for members you can set */
    Xet_add(L, TextExtents_setters);     /* fill with setters */
    lua_pushcclosure(L, Xet_newindex_handler, 1);
    lua_rawset(L, metatable);            /* metatable.__newindex = newindex_handler */

    lua_newtable(L);
    lua_pushstring(L, "__call");
    lua_pushcfunction(L, new_TextExtents);
    lua_settable(L, -3);
    lua_setmetatable(L, methods);

    lua_pop(L, 1);                       /* drop metatable */
    //hd.. return 1;                     /* return methods on the stack */
    //lua_pop(L, 1);                       //hd.. drop methods
    return 0;
}
int registerStruct(lua_State* luaSt,
                   const char* name,
                   const Xet_reg_pre* getters,
                   const Xet_reg_pre* setters,
                   const luaL_reg* regMetaMethods,
                   const luaL_reg* regMethods)
{
    int metatable, methods;

    /* create methods table, and add it to the table of globals */
    luaL_register(luaSt, name, regMethods);
    methods = lua_gettop(luaSt);

    /* create metatable for type, and add it to the registry */
    luaL_newmetatable(luaSt, name);
    luaL_register(luaSt, NULL, regMetaMethods);  /* fill metatable */
    metatable = lua_gettop(luaSt);

    lua_pushliteral(luaSt, "__metatable");
    lua_pushvalue(luaSt, methods);    /* dup methods table*/
    lua_rawset(luaSt, metatable);     /* hide metatable:
                                         metatable.__metatable = methods */
    lua_pushliteral(luaSt, "__index");
    lua_pushvalue(luaSt, metatable);     /* upvalue index 1 */
    Xet_add(luaSt, getters);             /* fill metatable with getters */
    lua_pushvalue(luaSt, methods);       /* upvalue index 2 */
    lua_pushcclosure(luaSt, indexHandler, 2);
    lua_rawset(luaSt, metatable);        /* metatable.__index = indexHandler */

    lua_pushliteral(luaSt, "__newindex");
    lua_newtable(luaSt);                 /* table for members you can set */
    Xet_add(luaSt, setters);             /* fill with setters */
    lua_pushcclosure(luaSt, newIndexHandler, 1);
    lua_rawset(luaSt, metatable);     /* metatable.__newindex = newIndexHandler */

    lua_pop(luaSt, 2);         /* drop metatable and methods */
    return 0;
}