-
Notifications
You must be signed in to change notification settings - Fork 63
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
Revive boolean
type
#227
Open
snoopcatt
wants to merge
2
commits into
dibyendumajumdar:master
Choose a base branch
from
snoopcatt:boolean
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Revive boolean
type
#227
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,7 +79,6 @@ | |
|
||
#endif | ||
|
||
|
||
/* | ||
** Try to convert a value from string to a number value. | ||
** If the value is not a string or is a string not representing | ||
|
@@ -95,6 +94,33 @@ static int l_strton (const TValue *obj, TValue *result) { | |
return (luaO_str2num(svalue(obj), result) == vslen(obj) + 1); | ||
} | ||
|
||
/* | ||
** Try to convert a value (string or numeric) to boolean. | ||
** On successful conversion returns 1 and stores value in (*b). | ||
** On failure, returns 0. | ||
*/ | ||
int luaV_toboolean (const TValue *obj, int *b) { | ||
// initial value is negative | ||
(*b) = -1; | ||
// try as integer | ||
if (ttisinteger(obj)) { | ||
int i = ivalue(obj); | ||
if (i == 0) | ||
(*b) = 0; | ||
else if (i == 1) | ||
(*b) = 1; | ||
} | ||
// try as string | ||
else if (ttisstring(obj)) { | ||
const char* s = getstr(tsvalue(obj)); | ||
if (strcmp(s, "false") == 0) | ||
(*b) = 0; | ||
else if (strcmp(s, "true") == 0) | ||
(*b) = 1; | ||
} | ||
// if still negative (so unchanged) -- conversion failed | ||
return ((*b) >= 0); | ||
} | ||
|
||
/* | ||
** Try to convert a value to a float. The float case is already handled | ||
|
@@ -1331,6 +1357,7 @@ int luaV_execute (lua_State *L) { | |
&&vmlabel(OP_RAVI_TOFARRAY), | ||
&&vmlabel(OP_RAVI_TOTAB), | ||
&&vmlabel(OP_RAVI_TOSTRING), | ||
&&vmlabel(OP_RAVI_TOBOOLEAN), | ||
&&vmlabel(OP_RAVI_TOCLOSURE), | ||
&&vmlabel(OP_RAVI_TOTYPE), | ||
&&vmlabel(OP_RAVI_MOVEI), | ||
|
@@ -2589,6 +2616,13 @@ int luaV_execute (lua_State *L) { | |
luaG_runerror(L, "string expected"); | ||
vmbreak; | ||
} | ||
vmcase(OP_RAVI_TOBOOLEAN) { | ||
int b; | ||
if (RAVI_LIKELY(luaV_toboolean(ra, &b))) { setbvalue(ra, b); } | ||
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. Not convinced we should conversion. |
||
if (!ttisnil(ra) && RAVI_UNLIKELY(!ttisboolean(ra))) | ||
luaG_runerror(L, "boolean expected"); | ||
vmbreak; | ||
} | ||
vmcase(OP_RAVI_TOCLOSURE) { | ||
if (!ttisnil(ra) && RAVI_UNLIKELY(!ttisclosure(ra))) | ||
luaG_runerror(L, "closure expected"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -600,6 +600,7 @@ static const char Lua_header[] = | |
" (ttisinteger(o) ? (*(i) = ivalue(o), 1) : luaV_tointeger(o,i,LUA_FLOORN2I))\n" | ||
"extern int luaV_tonumber_(const TValue *obj, lua_Number *n);\n" | ||
"extern int luaV_tointeger(const TValue *obj, lua_Integer *p, int mode);\n" | ||
"extern int luaV_toboolean (const TValue *obj, int *b); \n" | ||
#ifdef RAVI_DEFER_STATEMENT | ||
"extern int luaF_close (lua_State *L, StkId level, int status);\n" | ||
#else | ||
|
@@ -1659,6 +1660,21 @@ static void emit_op_tostring(struct function *fn, int A, int pc) { | |
membuff_add_string(&fn->body, "}\n"); | ||
} | ||
|
||
static void emit_op_toboolean(struct function *fn, int A, int pc) { | ||
(void)pc; | ||
emit_reg(fn, "ra", A); | ||
membuff_add_string(&fn->body, "int bool = 0;\n"); | ||
membuff_add_string(&fn->body, "if (luaV_toboolean(ra, &bool)) { setbvalue(ra, bool); }\n"); | ||
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. As above, not convinced conversion is a good idea. |
||
membuff_add_string(&fn->body, "if (!ttisboolean(ra)) {\n"); | ||
#if GOTO_ON_ERROR | ||
membuff_add_fstring(&fn->body, " error_code = %d;\n", Error_boolean_expected); | ||
membuff_add_string(&fn->body, " goto Lraise_error;\n"); | ||
#else | ||
membuff_add_fstring(&fn->body, " raviV_raise_error(L, %d);\n", Error_boolean_expected); | ||
#endif | ||
membuff_add_string(&fn->body, "}\n"); | ||
} | ||
|
||
static void emit_op_totype(struct function *fn, int A, int Bx, int pc) { | ||
(void)pc; | ||
emit_reg(fn, "ra", A); | ||
|
@@ -2267,6 +2283,9 @@ bool raviJ_codegen(struct lua_State *L, struct Proto *p, struct ravi_compile_opt | |
case OP_RAVI_TOSTRING: { | ||
emit_op_tostring(&fn, A, pc); | ||
} break; | ||
case OP_RAVI_TOBOOLEAN: { | ||
emit_op_toboolean(&fn, A, pc); | ||
} break; | ||
case OP_RAVI_TOCLOSURE: { | ||
emit_op_toclosure(&fn, A, pc); | ||
} break; | ||
|
@@ -2391,6 +2410,7 @@ static const char *errortext[] = {"integer expected", | |
"for initial value must be a number", | ||
"array index is out of bounds", | ||
"string expected", | ||
"boolean expected", | ||
"closure expected", | ||
"type mismatch: wrong userdata type", | ||
NULL}; | ||
|
@@ -2404,3 +2424,4 @@ void raviV_raise_error_with_info(lua_State *L, int errorcode, const char *info) | |
assert(errorcode == Error_type_mismatch); | ||
luaG_runerror(L, "type mismatch: expected %s", info); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This doesn't comply with Lua semantics - string and numbers are true.