示例#1
0
/* Register module */
LUA_API int luaopen_python(lua_State *L) {
    lua_Object python = lua_createtable(L);

    set_table_string(L, python, PY_UNICODE_ENCODING, "utf8");
    set_table_string(L, python, PY_UNICODE_ENCODING_ERRORHANDLER, "strict");
    set_table_number(L, python, PY_OBJECT_BY_REFERENCE, 0);
    set_table_number(L, python, PY_API_IS_EMBEDDED, 0);  // If Python is inside Lua
    set_table_number(L, python, PY_LUA_TABLE_CONVERT, 0); // table convert ?

    lua_pushcfunction(L, py_args);
    lua_setglobal(L, PY_ARGS_FUNC);

    lua_pushcfunction(L, py_kwargs);
    lua_setglobal(L, PY_KWARGS_FUNC);

    lua_pushcfunction(L, py_args_array);
    lua_setglobal(L, PY_ARGS_ARRAY_FUNC);

    lua_pushobject(L, python);
    lua_setglobal(L, PY_API_NAME);  // api python

    int index = 0;
    while (py_lib[index].name) {
        set_table_fn(L, python, py_lib[index].name, py_lib[index].func);
        index++;
    }

    // register all tag methods
    int ntag = lua_newtag(L);
    index = 0;
    while (lua_tag_methods[index].name) {
        lua_pushcfunction(L, lua_tag_methods[index].func);
        lua_settagmethod(L, ntag, lua_tag_methods[index].name);
        index++;
    }

    // tag event
    set_table_number(L, python, PY_API_TAG, ntag);

    PyObject *pyObject = Py_True;
    Py_INCREF(pyObject);
    set_table_usertag(L, python, PY_TRUE, py_object_container(L, pyObject, 0), ntag);

    pyObject = Py_False;
    Py_INCREF(pyObject);
    set_table_usertag(L, python, PY_FALSE, py_object_container(L, pyObject, 0), ntag);

    pyObject = Py_None;
    Py_INCREF(pyObject);
    set_table_usertag(L, python, PY_NONE, py_object_container(L, pyObject, 0), ntag);
    return 0;
}
示例#2
0
/* Register module */
LUA_API int luaopen_python(lua_State *L) {
    lua_Object python = lua_createtable(L);

    lua_pushobject(L, python);
    lua_setglobal(L, "python");  // api python

    int index = 0;
    while (py_lib[index].name) {
        set_table_fn(L, python, py_lib[index].name, py_lib[index].func);
        index++;
    }

    set_table_userdata(L, python, "True", Py_True);
    set_table_userdata(L, python, "False", Py_False);

    // base python object
    lua_Object ltable = lua_createtable(L);
    set_table_object(L, python, POBJECT, ltable);

    // register all tag methods
    int ntag = lua_newtag(L);
    index = 0;
    while (lua_tag_methods[index].name) {
        lua_pushcfunction(L, lua_tag_methods[index].func);
        lua_settagmethod(L, ntag, lua_tag_methods[index].name);
        index++;
    }

    // set tag
    lua_pushobject(L, ltable);
    lua_settag(L, ntag);

    // try startup system
    // python_system_init(L);
    return 0;
}