Skip to content

Commit d0a3b0a

Browse files
committed
Updated pre-generated bindings.
Object's default constructor can now be called like: local rep = git2.Repository("./repo/.git/") local raw = git2.RawObject('blob', 'blob contents') or with the old interface: local rep = git2.Repository.open("./repo/.git/") local raw = git2.RawObject.new('blob', 'blob contents')
1 parent 1b046b3 commit d0a3b0a

File tree

3 files changed

+74
-29
lines changed

3 files changed

+74
-29
lines changed

Diff for: pre_generated-git2.nobj.c

+70-24
Original file line numberDiff line numberDiff line change
@@ -574,35 +574,45 @@ static FUNC_UNUSED int obj_simple_udata_default_tostring(lua_State *L) {
574574
return 1;
575575
}
576576

577+
static int obj_constructor_call_wrapper(lua_State *L) {
578+
/* replace '__call' table with constructor function. */
579+
lua_pushvalue(L, lua_upvalueindex(1));
580+
lua_replace(L, 1);
581+
582+
/* call constructor function with all parameters after the '__call' table. */
583+
lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
584+
/* return all results from constructor. */
585+
return lua_gettop(L);
586+
}
587+
577588
static void obj_type_register(lua_State *L, const obj_type_reg *type_reg) {
578589
const luaL_reg *reg_list;
579590
obj_type *type = type_reg->type;
580591
const obj_base *base = type_reg->bases;
581592
const obj_const *constants = type_reg->constants;
582593

583-
/* create methods table. */
584-
lua_newtable(L);
585-
luaL_register(L, NULL, type_reg->methods); /* fill methods table. */
586-
587594
/* create public functions table. */
588595
reg_list = type_reg->pub_funcs;
589596
if(reg_list != NULL && reg_list[0].name != NULL) {
590-
if(reg_list[1].name != NULL) {
591-
lua_newtable(L);
592-
luaL_register(L, NULL, reg_list); /* fill public functions table. */
593-
} else {
594-
lua_pushcfunction(L, reg_list[0].func); /* push single 'new' function. */
595-
}
596-
/* register "public functions table" or "constructor" as '<object_name> */
597+
/* register "constructors" as to object's public API */
598+
luaL_register(L, NULL, reg_list); /* fill public API table. */
599+
600+
/* make public API table callable as the default constructor. */
601+
lua_newtable(L); /* create metatable */
602+
lua_pushliteral(L, "__call");
603+
lua_pushcfunction(L, reg_list[0].func); /* push first constructor function. */
604+
lua_pushcclosure(L, obj_constructor_call_wrapper, 1); /* make __call wrapper. */
605+
lua_rawset(L, -3); /* metatable.__call = <default constructor> */
606+
lua_setmetatable(L, -2);
607+
608+
lua_pop(L, 1); /* pop public API table, don't need it any more. */
609+
/* create methods table. */
610+
lua_newtable(L);
597611
} else {
598-
/* register "methods table" as '<object_name> */
599-
lua_pushvalue(L, -1);
612+
/* register all methods as public functions. */
600613
}
601-
#if REG_OBJECTS_AS_GLOBALS
602-
lua_pushvalue(L, -1); /* dup value. */
603-
lua_setglobal(L, type->name); /* global: <object_name> = value */
604-
#endif
605-
lua_setfield(L, -3, type->name); /* module["<object_name>"] = value */
614+
615+
luaL_register(L, NULL, type_reg->methods); /* fill methods table. */
606616

607617
luaL_newmetatable(L, type->name); /* create metatable */
608618
lua_pushliteral(L, ".name");
@@ -3282,11 +3292,24 @@ static const obj_type_reg obj_type_regs[] = {
32823292

32833293

32843294

3285-
int luaopen_git2(lua_State *L) {
3286-
const obj_type_reg *reg = obj_type_regs;
3287-
/* module table. */
3288-
luaL_register(L, "git2", git2_function);
32893295

3296+
3297+
3298+
3299+
static const luaL_Reg submodule_libs[] = {
3300+
{NULL, NULL}
3301+
};
3302+
3303+
3304+
3305+
static void create_object_instance_cache(lua_State *L) {
3306+
lua_pushlightuserdata(L, obj_udata_weak_ref_key); /* key for weak table. */
3307+
lua_rawget(L, LUA_REGISTRYINDEX); /* check if weak table exists already. */
3308+
if(!lua_isnil(L, -1)) {
3309+
lua_pop(L, 1); /* pop weak table. */
3310+
return;
3311+
}
3312+
lua_pop(L, 1); /* pop nil. */
32903313
/* create weak table for object instance references. */
32913314
lua_pushlightuserdata(L, obj_udata_weak_ref_key); /* key for weak table. */
32923315
lua_newtable(L); /* weak table. */
@@ -3296,12 +3319,35 @@ int luaopen_git2(lua_State *L) {
32963319
lua_rawset(L, -3); /* metatable.__mode = 'v' weak values. */
32973320
lua_setmetatable(L, -2); /* add metatable to weak table. */
32983321
lua_rawset(L, LUA_REGISTRYINDEX); /* create reference to weak table. */
3322+
}
3323+
3324+
int luaopen_git2(lua_State *L) {
3325+
const obj_type_reg *reg = obj_type_regs;
3326+
const luaL_Reg *submodules = submodule_libs;
3327+
/* module table. */
3328+
luaL_register(L, "git2", git2_function);
3329+
3330+
/* create object cache. */
3331+
create_object_instance_cache(L);
3332+
3333+
for(; submodules->func != NULL ; submodules++) {
3334+
lua_pushcfunction(L, submodules->func);
3335+
lua_pushstring(L, submodules->name);
3336+
lua_call(L, 1, 0);
3337+
}
32993338

33003339
/* register objects */
3301-
while(reg->type != NULL) {
3340+
for(; reg->type != NULL ; reg++) {
3341+
lua_newtable(L); /* create public API table for object. */
3342+
lua_pushvalue(L, -1); /* dup. object's public API table. */
3343+
lua_setfield(L, -3, reg->type->name); /* module["<object_name>"] = <object public API> */
3344+
#if REG_OBJECTS_AS_GLOBALS
3345+
lua_pushvalue(L, -1); /* dup value. */
3346+
lua_setglobal(L, reg->type->name); /* global: <object_name> = <object public API> */
3347+
#endif
33023348
obj_type_register(L, reg);
3303-
reg++;
33043349
}
3350+
33053351
return 1;
33063352
}
33073353

Diff for: test_rep.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require"utils"
1313
print("dump git2 interface")
1414
print(dbg_dump(git2))
1515

16-
local rep = assert(git2.Repository.open(git_path))
16+
local rep = assert(git2.Repository(git_path))
1717

1818
print("dump Repository interface")
1919
print(dbg_dump(rep))
@@ -24,7 +24,7 @@ print(dbg_dump(oid))
2424
print('convert OID value to string = <' .. tostring(oid) .. '>')
2525

2626
print('test writing to the object database:')
27-
local raw_obj = git2.RawObject.new('blob',"any ol content will do")
27+
local raw_obj = git2.RawObject('blob',"any ol content will do")
2828
print()
2929
print("dump RawObject interface")
3030
print(dbg_dump(raw_obj))

Diff for: utils.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function dump(obj)
9898
return dump_recur(seen, obj, 0, 0)
9999
end
100100

101-
local function sizeof2(size, ...)
101+
local function sizeof2(size, test, ...)
102102
collectgarbage"collect"
103103
size= collectgarbage"count"*1024 - size
104104
print("used size: " .. size)
@@ -107,12 +107,11 @@ end
107107

108108
function sizeof(test, ...)
109109
local size=0
110-
local ret
111110
if type(test) == "string" then
112111
test=assert(loadstring(test))
113112
end
114113
collectgarbage"collect"
115114
size=collectgarbage"count"*1024
116-
return sizeof2(size, maker(...))
115+
return sizeof2(size, test, test(...))
117116
end
118117

0 commit comments

Comments
 (0)