aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2012-10-02 11:59:32 +0200
committerMike Pall <mike>2012-10-02 11:59:32 +0200
commitfaf05c3a13e3b9b37a128a589a0b780a5fb7720e (patch)
tree1d1e040198151c28d4588c03489ddc756cf4a7fc /src
parent0d7094f36e62854d882c528efb634210b6c9455d (diff)
downloadluajit-faf05c3a13e3b9b37a128a589a0b780a5fb7720e.tar.gz
luajit-faf05c3a13e3b9b37a128a589a0b780a5fb7720e.tar.bz2
luajit-faf05c3a13e3b9b37a128a589a0b780a5fb7720e.zip
From Lua 5.2: '%s' option to string.format() behaves like tostring().
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.dep4
-rw-r--r--src/lib_string.c38
2 files changed, 39 insertions, 3 deletions
diff --git a/src/Makefile.dep b/src/Makefile.dep
index b2d6b6a7..cab23b79 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -32,8 +32,8 @@ lib_package.o: lib_package.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
32 lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h 32 lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h
33lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ 33lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
34 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h \ 34 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h \
35 lj_state.h lj_ff.h lj_ffdef.h lj_bcdump.h lj_lex.h lj_char.h lj_lib.h \ 35 lj_meta.h lj_state.h lj_ff.h lj_ffdef.h lj_bcdump.h lj_lex.h lj_char.h \
36 lj_libdef.h 36 lj_lib.h lj_libdef.h
37lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ 37lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
38 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_lib.h \ 38 lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_lib.h \
39 lj_libdef.h 39 lj_libdef.h
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
776static 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
775LJLIB_CF(string_format) 811LJLIB_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 */