-
Notifications
You must be signed in to change notification settings - Fork 8
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
Error handling #13
Error handling #13
Changes from all commits
97bb961
3a8cced
acbf9fc
adf832f
271ab5e
362f4d4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,12 @@ static int luaL_typerror (lua_State *L, int narg, const char *tname) { | |
#define lua_rawlen(a, b) do_not_use_lua_rawlen_use_lua_objlen_instead; | ||
#endif | ||
|
||
#if LUA_VERSION_NUM >= 503 | ||
#define oocairo_lua_isinteger(L, idx) lua_isinteger(L, idx) | ||
#else | ||
#define oocairo_lua_isinteger(L, idx) lua_isnumber(L, idx) | ||
#endif | ||
|
||
static const int ENDIANNESS_TEST_VAL = 1; | ||
#define IS_BIG_ENDIAN (!(*(const char *) &ENDIANNESS_TEST_VAL)) | ||
|
||
|
@@ -363,12 +369,37 @@ from_lua_rectangle (lua_State *L, cairo_rectangle_int_t *rect, int pos) { | |
#define DO(t) \ | ||
lua_pushstring(L, #t); \ | ||
lua_gettable(L, pos); \ | ||
rect->t = luaL_checkinteger(L, -1); \ | ||
if (!oocairo_lua_isinteger(L, -1)) { \ | ||
luaL_argerror(L, pos, "invalid rectangle: coordinates must be integer values"); \ | ||
} \ | ||
rect->t = lua_tointeger(L, -1); \ | ||
lua_pop(L, 1) | ||
DO(x); | ||
DO(y); | ||
DO(width); | ||
DO(height); | ||
#undef DO | ||
} | ||
|
||
static void | ||
from_lua_rectangle_list_element (lua_State *L, cairo_rectangle_int_t *rect, int list_pos, int element_index) { | ||
lua_rawgeti(L, list_pos, element_index); /* push rectangle from list */ | ||
if (lua_type(L, -1) != LUA_TTABLE) { | ||
luaL_argerror(L, list_pos, "list contains invalid rectangle"); | ||
} | ||
#define DO(t) \ | ||
lua_pushstring(L, #t); \ | ||
lua_gettable(L, -2); \ | ||
if (!oocairo_lua_isinteger(L, -1)) { \ | ||
luaL_argerror(L, list_pos, "list contains invalid rectangle: coordinates must be integer values"); \ | ||
} \ | ||
rect->t = lua_tointeger(L, -1); \ | ||
lua_pop(L, 1) | ||
DO(x); | ||
DO(y); | ||
DO(width); | ||
DO(height); | ||
lua_pop(L, 1); /* pop rectangle */ | ||
#undef DO | ||
} | ||
#endif | ||
|
@@ -494,7 +525,9 @@ from_lua_glyph_array (lua_State *L, cairo_glyph_t **glyphs, int *num_glyphs, | |
return; | ||
} | ||
*glyphs = GLYPHS_ALLOC(*num_glyphs); | ||
assert(*glyphs); | ||
if (!*glyphs) { | ||
return luaL_error(L, "out of memory"); | ||
} | ||
|
||
for (i = 0; i < *num_glyphs; ++i) { | ||
lua_rawgeti(L, pos, i + 1); | ||
|
@@ -578,7 +611,9 @@ from_lua_clusters_table (lua_State *L, cairo_text_cluster_t **clusters, | |
return; | ||
} | ||
*clusters = cairo_text_cluster_allocate(*num); | ||
assert(*clusters); | ||
if (!*clusters) { | ||
return luaL_error(L, "out of memory"); | ||
} | ||
|
||
for (i = 0; i < *num; ++i) { | ||
lua_rawgeti(L, pos, i + 1); | ||
|
@@ -778,8 +813,9 @@ free_surface_userdata (SurfaceUserdata *ud) { | |
static char * | ||
my_strdup (const char *s) { | ||
char *copy = malloc(strlen(s) + 1); | ||
assert(copy); | ||
strcpy(copy, s); | ||
if (copy) { | ||
strcpy(copy, s); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Memo to myself: Get rid of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let us keep it as it is, because |
||
return copy; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -433,7 +433,9 @@ if Cairo.check_version(1, 10, 0) then | |
test("test/data", "test data") | ||
|
||
local surface = Cairo.image_surface_create("rgb24", 0, 0) | ||
assert_error(function () surface_set_mime_data(nil, "foo") end) | ||
local ok, errmsg = pcall(function () surface:set_mime_data(nil, "foo") end) | ||
assert_false(ok) | ||
assert_match("bad argument %#1 to %'set_mime_data%' %(string expected%, got nil%)", errmsg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this a typo? The commit message confuses me. Ah, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How could this be made clearer? The commit message explains what the commit is for. One could double the code diff in the commit message, for example "fixed typo in testcase: call surface:set_mime_data instead of undefined surface_set_mime_data". Is this better? However there are onle three lines in the commit so it should not be too hard to understand what this commit is doing. |
||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... With Lua 5.3,
luaL_checkinteger
does more thanlua_isnumber
, because 5.5 is a number, but not an integer. Butlua_isinteger
only exists in Lua 5.3 and not in older Lua versions.How about something like this:
One could also check if the number is an integer in older Lua versions instead of silently truncating, but I am not sure if that is worth it...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Lua 5.1 documentation for
luaL_checkinteger
differs from the Lua 5.3 documentation:Lua 5.1
Lua 5.3
So if the documentation is true there was already a silent truncation of numbers to integers before my changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I incorporated
oocairo_lua_isinteger
as you suggested, see my latest commit.