# String.format bug in HTML5 application

**URL:** <https://forum.defold.com/t/string-format-bug-in-html5-application/78063>\
**Category:** Bugs\
**Created:** [September 11, 2024, 10:20am UTC](https://forum.defold.com/t/string-format-bug-in-html5-application/78063 "2024-09-11T10:20:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew-flick](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andrew-flick/32/27062_2.png) [@andrew-flick](https://forum.defold.com/u/andrew-flick)\
**Post date:** [September 11, 2024, 10:20am UTC](https://forum.defold.com/t/string-format-bug-in-html5-application/78063/1 "2024-09-11T10:20:32Z")

</div>

I get an error in the HTML5 bundled application when I call string.format with a table argument after the format string. The table has a `__tostring` metatmethod so it should convert to a string and it works as expected when running from the editor. But in the bundled application in causes an error as string.format complains about being given a table when expecting a string.

 ![Screenshot 2024-09-11 at 10.26.51](https://forum-defold-uploads.b-cdn.net/original/3X/6/d/6d72a7f6a064a8c4df11395c97af3961a833ebe5.jpeg)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [September 11, 2024, 11:39am UTC](https://forum.defold.com/t/string-format-bug-in-html5-application/78063/2 "2024-09-11T11:39:19Z")

</div>

> [@andrew-flick](#):
>
> I get an error in the HTML5 bundled application when I call string.format with a table argument after the format string. The table has a `__tostring` metatmethod so it should convert to a string and it works as expected when running from the editor

HTML5 use Lua 5.1.4 while all other platforms use LuaJIT. I bet there is some subtle difference where the \_\_tostring metamethod isn’t invoked in this case…

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [September 11, 2024, 11:45am UTC](https://forum.defold.com/t/string-format-bug-in-html5-application/78063/3 "2024-09-11T11:45:00Z")

</div>

Yep:

```lua
> lua -v
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio

> cat foo.lua
local t = setmetatable({}, {
	__tostring = function(t, ...)
		return "Hello" .. 123
	end
})

print(t)
print(string.format("Testing %s", t))%

> lua foo.lua 
Hello123
lua: foo.lua:8: bad argument #2 to 'format' (string expected, got table)
stack traceback:
	[C]: in function 'format'
	foo.lua:8: in main chunk
	[C]: ?

```

---

<div class="post-metadata">

**Author:** ![andrew-flick](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andrew-flick/32/27062_2.png) [@andrew-flick](https://forum.defold.com/u/andrew-flick)\
**Post date:** [September 11, 2024, 11:56am UTC](https://forum.defold.com/t/string-format-bug-in-html5-application/78063/4 "2024-09-11T11:56:44Z")

</div>

I came back to say that I tested it and you are right. Interesting to know…

---

<div class="post-metadata">

**Author:** ![andrew-flick](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andrew-flick/32/27062_2.png) [@andrew-flick](https://forum.defold.com/u/andrew-flick)\
**Post date:** [September 11, 2024, 12:10pm UTC](https://forum.defold.com/t/string-format-bug-in-html5-application/78063/5 "2024-09-11T12:10:12Z")

</div>

A quick hack to give better behaviour:

```
function log(f, ...)
	local args = { ... }
	for i, v in ipairs(args) do
		if type(v) == "table" then
			args[i] = tostring(v)
		else
			args[i] = v
		end
	end

	print(string.format(f, unpack(args)))
end

```

I do more in my log function but just in case anybody else hits this problem this shows one way to work around it.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [September 11, 2024, 12:34pm UTC](https://forum.defold.com/t/string-format-bug-in-html5-application/78063/6 "2024-09-11T12:34:11Z")

</div>

Also discussed here: [\_\_tostring and string.format](https://lua-l.lua.narkive.com/QFmbwrf7/tostring-and-string-format) and [Incompatibility with Lua 5.1: \_\_tostring/tostring/print · Issue #351 · LuaJIT/LuaJIT · GitHub](https://github.com/LuaJIT/LuaJIT/issues/351)
