diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib_string.c | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/src/lib_string.c b/src/lib_string.c index 9c4cee8b..b36fcd6d 100644 --- a/src/lib_string.c +++ b/src/lib_string.c | |||
@@ -20,6 +20,7 @@ | |||
20 | #include "lj_err.h" | 20 | #include "lj_err.h" |
21 | #include "lj_str.h" | 21 | #include "lj_str.h" |
22 | #include "lj_tab.h" | 22 | #include "lj_tab.h" |
23 | #include "lj_meta.h" | ||
23 | #include "lj_state.h" | 24 | #include "lj_state.h" |
24 | #include "lj_ff.h" | 25 | #include "lj_ff.h" |
25 | #include "lj_bcdump.h" | 26 | #include "lj_bcdump.h" |
@@ -772,6 +773,41 @@ static unsigned LUA_INTFRM_T num2uintfrm(lua_State *L, int arg) | |||
772 | } | 773 | } |
773 | } | 774 | } |
774 | 775 | ||
776 | static GCstr *meta_tostring(lua_State *L, int arg) | ||
777 | { | ||
778 | TValue *o = L->base+arg-1; | ||
779 | cTValue *mo; | ||
780 | lua_assert(o < L->top); /* Caller already checks for existence. */ | ||
781 | if (LJ_LIKELY(tvisstr(o))) | ||
782 | return strV(o); | ||
783 | if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) { | ||
784 | copyTV(L, L->top++, mo); | ||
785 | copyTV(L, L->top++, o); | ||
786 | lua_call(L, 1, 1); | ||
787 | L->top--; | ||
788 | if (tvisstr(L->top)) | ||
789 | return strV(L->top); | ||
790 | o = L->base+arg-1; | ||
791 | copyTV(L, o, L->top); | ||
792 | } | ||
793 | if (tvisnumber(o)) { | ||
794 | return lj_str_fromnumber(L, o); | ||
795 | } else if (tvisnil(o)) { | ||
796 | return lj_str_newlit(L, "nil"); | ||
797 | } else if (tvisfalse(o)) { | ||
798 | return lj_str_newlit(L, "false"); | ||
799 | } else if (tvistrue(o)) { | ||
800 | return lj_str_newlit(L, "true"); | ||
801 | } else { | ||
802 | if (tvisfunc(o) && isffunc(funcV(o))) | ||
803 | lj_str_pushf(L, "function: builtin#%d", funcV(o)->c.ffid); | ||
804 | else | ||
805 | lj_str_pushf(L, "%s: %p", lj_typename(o), lua_topointer(L, arg)); | ||
806 | L->top--; | ||
807 | return strV(L->top); | ||
808 | } | ||
809 | } | ||
810 | |||
775 | LJLIB_CF(string_format) | 811 | LJLIB_CF(string_format) |
776 | { | 812 | { |
777 | int arg = 1, top = (int)(L->top - L->base); | 813 | int arg = 1, top = (int)(L->top - L->base); |
@@ -832,7 +868,7 @@ LJLIB_CF(string_format) | |||
832 | luaL_addvalue(&b); | 868 | luaL_addvalue(&b); |
833 | continue; | 869 | continue; |
834 | case 's': { | 870 | case 's': { |
835 | GCstr *str = lj_lib_checkstr(L, arg); | 871 | GCstr *str = meta_tostring(L, arg); |
836 | if (!strchr(form, '.') && str->len >= 100) { | 872 | if (!strchr(form, '.') && str->len >= 100) { |
837 | /* no precision and string is too long to be formatted; | 873 | /* no precision and string is too long to be formatted; |
838 | keep original string */ | 874 | keep original string */ |