Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Record tag error message includes record name instead of only "userdata" #595

Merged
merged 4 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/pallene/coder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,23 @@ function Coder:get_stack_slot(typ, dst, slot, loc, description_fmt, ...)
else
assert(not typ.is_upvalue_box)
local extra_args = table.pack(...)
local expected_type = pallene_type_tag(typ)
if expected_type == "LUA_TUSERDATA" then
expected_type = C.string(typ.name)
else
expected_type = "pallene_tag_name(" .. expected_type .. ")"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need pallene_tag_name, now that we have pallene_type_name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would require both pallene_tag_name and pallene_type_name. They are different in two ways.

  1. Difference in the function signature const char *pallene_tag_name(int raw_tag) and const char *pallene_type_name(lua_State *L, const TValue *v)
  2. expected_type is known at the compile time (and uses pallene_tag_name) i.e. the argument's expected type. Whereas the argument const TValue *v passed to pallene_type_name cannot be known at compile time. v is the type of the actual argument passed at runtime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. IIRC, there aren't any other places that call pallene_tag_name. The tag name function was there only for the error messages and pallene_type_name now does that job. Notice how both functions are currently quite similar.

  2. If your goal was that the expected_type be known at compile time, then calling pallene_tag_name is not the way to do that. That C function call will only happen at run-time.

  3. One last thing: In the coder.lua, please use different variable names for the type tag returned by pallene_type_tag and for the type name that you compute inside the if statement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a look at the recent changes. Hope the latest commit resolves the issues/suggestions you mentioned.

end
check_tag = util.render([[
if (l_unlikely(!$test)) {
pallene_runtime_tag_check_error(L,
$file, $line, $expected_tag, rawtt($slot),
$file, $line, $expected_type, pallene_type_name(L, $slot),
hugomg marked this conversation as resolved.
Show resolved Hide resolved
${description_fmt}${opt_comma}${extra_args});
}
]], {
test = self:test_tag(typ, slot),
file = C.string(loc and loc.file_name or "<anonymous>"),
line = C.integer(loc and loc.line or 0),
expected_tag = pallene_type_tag(typ),
expected_type = expected_type,
slot = slot,
description_fmt = C.string(description_fmt),
opt_comma = (#extra_args == 0 and "" or ", "),
Expand Down Expand Up @@ -1771,11 +1777,13 @@ function Coder:generate_luaopen_function()
if tag == "coder.Constant.Metatable" then
is_upvalue_box = upv.typ.is_upvalue_box
if not is_upvalue_box then
table.insert(init_constants, [[
lua_newtable(L);
table.insert(init_constants, util.render([[
luaL_newmetatable(L, $type_name);
lua_pushstring(L, "__metatable");
lua_pushboolean(L, 0);
lua_settable(L, -3); ]])
lua_settable(L, -3); ]], {
type_name = C.string(upv.typ.name)
}))
end
elseif tag == "coder.Constant.String" then
table.insert(init_constants, util.render([[
Expand Down
22 changes: 16 additions & 6 deletions src/pallene/pallenelib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ return [==[

/* Type tags */
static const char *pallene_tag_name(int raw_tag);
static const char *pallene_type_name(lua_State *L, const TValue *raw_type);
hugomg marked this conversation as resolved.
Show resolved Hide resolved
static int pallene_is_truthy(const TValue *v);
static int pallene_is_record(const TValue *v, const TValue *meta_table);
static int pallene_bvalue(TValue *obj);
Expand All @@ -57,7 +58,8 @@ static void pallene_setbvalue(TValue *obj, int b);
static void pallene_barrierback_unboxed(lua_State *L, GCObject *p, GCObject *v);

/* Runtime errors */
static l_noret pallene_runtime_tag_check_error(lua_State *L, const char* file, int line, int expected_tag, int received_tag,const char *description_fmt, ...);
static l_noret pallene_runtime_tag_check_error(lua_State *L, const char* file, int line,
const char *expected_type, const char *received_type, const char *description_fmt, ...);
static l_noret pallene_runtime_arity_error(lua_State *L, const char *name, int expected, int received);
static l_noret pallene_runtime_divide_by_zero_error(lua_State *L, const char* file, int line);
static l_noret pallene_runtime_mod_by_zero_error(lua_State *L, const char* file, int line);
Expand Down Expand Up @@ -107,6 +109,17 @@ static const char *pallene_tag_name(int raw_tag)
}
}

static const char *pallene_type_name(lua_State *L, const TValue *raw_type)
{
if (rawtt(raw_type) == LUA_VNUMINT) {
return "integer";
} else if (rawtt(raw_type) == LUA_VNUMFLT) {
return "float";
} else {
return luaT_objtypename(L, raw_type);
}
}

static int pallene_is_truthy(const TValue *v)
{
return !l_isfalse(v);
Expand Down Expand Up @@ -147,14 +160,11 @@ static void pallene_runtime_tag_check_error(
lua_State *L,
const char* file,
int line,
int expected_tag,
int received_tag,
const char *expected_type,
const char *received_type,
const char *description_fmt,
...
){
const char *expected_type = pallene_tag_name(expected_tag);
const char *received_type = pallene_tag_name(received_tag);

/* This code is inspired by luaL_error */
luaL_where(L, 1);
if (line > 0) {
Expand Down