Open karlp opened 4 years ago
Can you provide me with a minimal Lua testcase for a userdata object?
Probably easiest is one of the libraries available.
it's the same as for functions really, here's both:
/# lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio (double int32)
> lj = require("luci.jsonc")
> pl = require("pl.import_into")()
> mosq = require("mosquitto")
> client = mosq.new() ----- the client object is a userdata....
> =lj.stringify(client)
null
> pl.pretty.dump(client)
userdata: 0xbfdca8 not a table
> =tostring(client)
userdata: 0xbfdca8
> x = {a = 123, b = function(q) return q * q end} --- table with function embedded
> pl.pretty.dump(x)
{
a = 123,
b = "function: 0xc10980"
}
> =lj.stringify(x)
{"a":123,"b":null}
>
I'd "like" the mosquitto client object to be rendered as "userdata: 0xbfdca8" (or whatever tostring() on the object calls, I have other libraries that provide "nice" __tostrting() implementations, and likewise for the function, I'd rather see "function: 0xc10980" than just "null"
Okay, got it.
luci-lib-json supports serializing userdata and functions by calling tostring on them, see https://github.com/openwrt/luci/commit/09de5a74e13b225c2159bd62bef2563bd2a615c2 (much like penlight.pretty.dump and similar)
luci-lib-jsonc doesn't. It doesn't crash at least, but it just puts in null. I've tried adding a "default" case here:https://github.com/openwrt/luci/blob/master/libs/luci-lib-jsonc/src/jsonc.c#L375 but it just gives me segfaults, so I haven't chased it further.
This is low priority / FYI only really. I have some rich tables I was hoping to just serialize as is for doing some reporting/stats, but none of the readily available libraries can serialize nicely, and luci-lib-jsonc isn't easily building for a non-openwrt/non-luci environment right now anyway.