Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
cdata_tostring should chain user provided __tostring
Browse files Browse the repository at this point in the history
  • Loading branch information
jmckaskill committed Jun 4, 2012
1 parent 3d258f0 commit 82ef487
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ffi.c
Expand Up @@ -2205,7 +2205,16 @@ static int cdata_tostring(lua_State* L)
{
struct ctype ct;
char buf[64];
void* p = check_cdata(L, 1, &ct);
void* p;
int ret;

lua_settop(L, 1);
p = to_cdata(L, 1, &ct);

ret = call_user_op(L, "__tostring", 1, 2, &ct);
if (ret >= 0) {
return ret;
}

if (ct.pointers == 0) {
switch (ct.type) {
Expand Down
2 changes: 2 additions & 0 deletions test.lua
Expand Up @@ -685,6 +685,7 @@ mt.__call = function(vls, a, b) return '__call', vls.d.a .. a .. (b or 'nil') e
mt.__unm = function(vls) return -vls.d.a end
mt.__concat = function(vls, a) return vls.d.a .. a end
mt.__len = function(vls) return vls.d.a end
mt.__tostring = function(vls) return 'string ' .. vls.d.a end
vls.d.a = 5
check(vls + 5, 10)
Expand All @@ -706,6 +707,7 @@ check(-vls, -5)
local a,b = vls('6')
check(a, '__call')
check(b, '56nil')
check(tostring(vls), 'string 5')
if _VERSION ~= 'Lua 5.1' then
check(vls .. 'str', '5str')
Expand Down

0 comments on commit 82ef487

Please sign in to comment.