diff --git a/src/lapi.c b/src/lapi.c index f55874a..8bb20e7 100644 --- a/src/lapi.c +++ b/src/lapi.c @@ -1630,7 +1630,7 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { */ StkId input = L->top - 1; - int compatible = ravi_checktype(L, input, type, usertype); + int compatible = raviV_checktype(L, input, type, usertype); if (!compatible) name = NULL; diff --git a/src/lcode.c b/src/lcode.c index 0a7f406..be67d56 100644 --- a/src/lcode.c +++ b/src/lcode.c @@ -920,7 +920,7 @@ static void check_valid_store(FuncState *fs, expdesc *var, expdesc *ex) { fs->ls, luaO_pushfstring( fs->ls->L, - "Invalid assignment: string expected")); + "Invalid assignment: string expected")); } else if (var->ravi_type_map == (RAVI_TM_FUNCTION | RAVI_TM_NIL)) { if ((ex_ravi_type_map & ~(RAVI_TM_FUNCTION | RAVI_TM_NIL)) == 0) @@ -929,7 +929,7 @@ static void check_valid_store(FuncState *fs, expdesc *var, expdesc *ex) { fs->ls, luaO_pushfstring( fs->ls->L, - "Invalid assignment: function expected")); + "Invalid assignment: function expected")); } else if (var->ravi_type_map == (RAVI_TM_USERDATA | RAVI_TM_NIL)) { if ((ex_ravi_type_map & ~(RAVI_TM_USERDATA | RAVI_TM_NIL)) == 0 && @@ -939,8 +939,8 @@ static void check_valid_store(FuncState *fs, expdesc *var, expdesc *ex) { fs->ls, luaO_pushfstring( fs->ls->L, - "Invalid assignment: usertype %s expected", (var->usertype ? getstr(var->usertype) : "UNKNOWN"))); - } + "Invalid assignment: usertype %s expected", (var->usertype ? getstr(var->usertype) : "UNKNOWN"))); + } } static OpCode check_valid_setupval(FuncState *fs, expdesc *var, expdesc *ex, diff --git a/src/lparser.c b/src/lparser.c index 0968490..3b6695c 100644 --- a/src/lparser.c +++ b/src/lparser.c @@ -1223,30 +1223,30 @@ static void constructor (LexState *ls, expdesc *t) { * be anchored somewhere else by the time parsing finishes */ static TString *user_defined_type_name(LexState *ls, TString *typename) { - size_t len = 0; - if (testnext(ls, '.')) { - char buffer[128] = { 0 }; - const char *str = getstr(typename); - len = strlen(str); - if (len >= sizeof buffer) { - luaX_syntaxerror(ls, "User defined type name is too long"); - return typename; - } - snprintf(buffer, sizeof buffer, "%s", str); - do { - typename = str_checkname(ls); - str = getstr(typename); - size_t newlen = len + strlen(str) + 1; - if (newlen >= sizeof buffer) { - luaX_syntaxerror(ls, "User defined type name is too long"); - return typename; - } - snprintf(buffer + len, sizeof buffer - len, ".%s", str); - len = newlen; - } while (testnext(ls, '.')); - typename = luaX_newstring(ls, buffer, strlen(buffer)); - } - return typename; + size_t len = 0; + if (testnext(ls, '.')) { + char buffer[128] = {0}; + const char *str = getstr(typename); + len = strlen(str); + if (len >= sizeof buffer) { + luaX_syntaxerror(ls, "User defined type name is too long"); + return typename; + } + snprintf(buffer, sizeof buffer, "%s", str); + do { + typename = str_checkname(ls); + str = getstr(typename); + size_t newlen = len + strlen(str) + 1; + if (newlen >= sizeof buffer) { + luaX_syntaxerror(ls, "User defined type name is too long"); + return typename; + } + snprintf(buffer + len, sizeof buffer - len, ".%s", str); + len = newlen; + } while (testnext(ls, '.')); + typename = luaX_newstring(ls, buffer, strlen(buffer)); + } + return typename; } /* RAVI Parse @@ -1279,8 +1279,8 @@ static ravi_type_map declare_localvar(LexState *ls, TString **pusertype) { tm = RAVI_TM_TABLE; else if (strcmp(str, "string") == 0) tm = RAVI_TM_STRING | RAVI_TM_NIL; - else if (strcmp(str, "boolean") == 0) - tm = RAVI_TM_BOOLEAN | RAVI_TM_NIL; + //else if (strcmp(str, "boolean") == 0) + // tm = RAVI_TM_BOOLEAN | RAVI_TM_NIL; else if (strcmp(str, "any") == 0) tm = RAVI_TM_ANY; else { diff --git a/src/lvm.c b/src/lvm.c index eb96425..a95ecfc 100644 --- a/src/lvm.c +++ b/src/lvm.c @@ -1204,6 +1204,33 @@ void luaV_finishOp (lua_State *L) { if (!luaV_fastset(L,t,k,slot,luaH_get,v)) \ Protect(luaV_finishset(L,t,k,v,slot)); } +int raviV_checktype(lua_State *L, TValue *input, ravitype_t type, TString *usertype) { + if (type == RAVI_TANY) + return 1; + if (type == RAVI_TNIL && ttisnil(input)) + return 1; + if (type == RAVI_TBOOLEAN && ttisboolean(input)) + return 1; + if (type == RAVI_TNUMINT && ttisinteger(input)) + return 1; + if (type == RAVI_TNUMFLT && ttisfloat(input)) + return 1; + if (type == RAVI_TARRAYINT && ttisiarray(input)) + return 1; + if (type == RAVI_TARRAYFLT && ttisfarray(input)) + return 1; + if (type == RAVI_TTABLE && ttisLtable(input)) + return 1; + if (type == RAVI_TSTRING && ttisstring(input)) + return 1; + if (type == RAVI_TFUNCTION && ttisclosure(input)) + return 1; + if (type == RAVI_TUSERDATA) { + if (raviV_check_usertype(L, usertype, input)) + return 1; + } + return 0; +} int raviV_check_usertype(lua_State *L, TString *name, const TValue *o) { diff --git a/src/lvm.h b/src/lvm.h index bf2bf6c..111211e 100644 --- a/src/lvm.h +++ b/src/lvm.h @@ -172,6 +172,7 @@ LUAI_FUNC void raviV_settable_sskey(lua_State *L, const TValue *t, TValue *key, LUAI_FUNC void raviV_gettable_i(lua_State *L, const TValue *t, TValue *key, StkId val); LUAI_FUNC void raviV_settable_i(lua_State *L, const TValue *t, TValue *key, StkId val); LUAI_FUNC void raviV_op_totype(lua_State *L, TValue *ra, TValue *rb); +LUAI_FUNC int raviV_checktype(lua_State *L, TValue *input, ravitype_t type, TString *usertype); LUAI_FUNC int raviV_check_usertype(lua_State *L, TString *name, const TValue *o); #ifdef RAVI_DEFER_STATEMENT LUAI_FUNC void raviV_op_defer(lua_State *L, TValue *ra); diff --git a/tests/comptests/inputs/25_bits.lua b/tests/comptests/inputs/25_bits.lua index 722a926..bdd7f9a 100644 --- a/tests/comptests/inputs/25_bits.lua +++ b/tests/comptests/inputs/25_bits.lua @@ -1,5 +1,5 @@ f = compiler.load([[ -function doit(a, 25_bits.luawhat) +function doit(a, what) local f: integer = ~0 if what == '&' then return a&f @@ -25,4 +25,4 @@ assert(math.abs(doit(16, '|') - ~0) < 1e-15) assert(math.abs(doit(16, '<<') - 32) < 1e-15) assert(math.abs(doit(16, '>>') - 8) < 1e-15) assert(math.abs(doit(16, '~') - ~16) < 1e-15) -print 'Ok' \ No newline at end of file +print 'Ok' diff --git a/tests/language/ravi_tests1.ravi b/tests/language/ravi_tests1.ravi index d27cc2a..9767d0a 100644 --- a/tests/language/ravi_tests1.ravi +++ b/tests/language/ravi_tests1.ravi @@ -1842,6 +1842,42 @@ do end print 'Test 87 OK' +do + local function doit(a: integer, what) + local f: integer = ~0 + if what == '&' then + return a&f + elseif what == '|' then + return a|f + elseif what == '<<' then + return a<<1 + elseif what == '>>' then + return a>>1 + elseif what == '~' then + return ~a + else + return 0 + end + end + check(doit, 'TOINT', 'LOADK', 'EQ', 'JMP', 'BAND_II', + 'RETURN', 'JMP', 'EQ', 'JMP', 'BOR_II', 'RETURN', + 'JMP', 'EQ', 'JMP', 'SHL_II', 'RETURN', 'JMP', 'EQ', + 'JMP', 'SHR_II', 'RETURN', 'JMP', 'EQ', 'JMP', + 'BNOT_I', 'RETURN', 'JMP', 'LOADK', 'RETURN', 'RETURN') + assert(doit and type(doit) == 'function') + assert(math.abs(doit(16, '&') - 16) < 1e-15) + assert(math.abs(doit(16, '|') - ~0) < 1e-15) + assert(math.abs(doit(16, '<<') - 32) < 1e-15) + assert(math.abs(doit(16, '>>') - 8) < 1e-15) + assert(math.abs(doit(16, '~') - ~16) < 1e-15) + compile(doit) + assert(math.abs(doit(16, '&') - 16) < 1e-15) + assert(math.abs(doit(16, '|') - ~0) < 1e-15) + assert(math.abs(doit(16, '<<') - 32) < 1e-15) + assert(math.abs(doit(16, '>>') - 8) < 1e-15) + assert(math.abs(doit(16, '~') - ~16) < 1e-15) +end +print 'Test 88 OK' for k,v in pairs(opcodes_coverage) do