Skip to content

Commit

Permalink
Merge branch 'hotfix/1.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
graup committed May 2, 2019
2 parents 15ebc4c + 620ccac commit 72ab486
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 83 deletions.
6 changes: 5 additions & 1 deletion contract/contract_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ static int modulePcall(lua_State *L)
int argc = lua_gettop(L) - 1;
int *service = (int *)getLuaExecContext(L);
struct LuaSetRecoveryPoint_return start_seq;
int ret;

if (service == NULL) {
luaL_error(L, "cannot find execution context");
Expand All @@ -261,7 +262,10 @@ static int modulePcall(lua_State *L)
luaL_throwerror(L);
}

if (lua_pcall(L, argc, LUA_MULTRET, 0) != 0) {
if ((ret = lua_pcall(L, argc, LUA_MULTRET, 0)) != 0) {
if (ret == LUA_ERRMEM) {
luaL_throwerror(L);
}
lua_pushboolean(L, false);
lua_insert(L, 1);
if (start_seq.r0 > 0) {
Expand Down
4 changes: 4 additions & 0 deletions contract/lgmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "lua.h"
#include "lauxlib.h"
#include "lgmp.h"
#include "math.h"

#define lua_boxpointer(L,u) \
(*(void **)(lua_newuserdata(L, sizeof(void *))) = (u))
Expand Down Expand Up @@ -83,6 +84,9 @@ static mp_num Bget(lua_State *L, int i)
x = bn_alloc(BN_Integer);
if (x == NULL)
luaL_error(L, mp_num_memory_error);
if (isnan(d) || isinf(d)) {
luaL_error(L, "can't convert nan or infinity");
}
mpz_init_set_d(x->mpptr, d);
Bnew(L, x);
lua_replace(L, i);
Expand Down
20 changes: 10 additions & 10 deletions contract/state_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ static void state_array_load_len(lua_State *L, state_array_t *arr)

static void state_array_checkarg(lua_State *L, state_array_t *arr)
{
int idx = luaL_checkint(L, -1);
int idx;
if (!luaL_isinteger(L, 2)) {
luaL_typerror(L, 2, "integer");
}
idx = luaL_checkint(L, 2);
luaL_argcheck(L, idx >= 1 && idx <= arr->len, 2, "index out of range");
}

Expand All @@ -192,15 +196,14 @@ static void state_array_push_key(lua_State *L, const char *id)

static int state_array_get(lua_State *L)
{
const char *method;
const char *idx;
state_array_t *arr;
int key_type = LUA_TNONE;

arr = luaL_checkudata(L, 1, STATE_ARRAY_ID);
state_array_load_len(L, arr);

method = lua_tostring(L, 2);
if (method != NULL) { /* methods */
if (lua_type(L, 2) == LUA_TSTRING) { /* methods */
const char *method = lua_tostring(L, 2);
if (strcmp(method, "append") == 0) {
lua_pushcfunction(L, state_array_append);
return 1;
Expand All @@ -211,6 +214,7 @@ static int state_array_get(lua_State *L)
lua_pushcfunction(L, state_array_len);
return 1;
}
luaL_typerror(L, 2, "integer");
}
state_array_checkarg(L, arr); /* a i */
lua_pushcfunction(L, getItemWithPrefix); /* a i f */
Expand All @@ -222,16 +226,12 @@ static int state_array_get(lua_State *L)

static int state_array_set(lua_State *L)
{
const char *idx;
state_array_t *arr;

arr = luaL_checkudata(L, 1, STATE_ARRAY_ID);
state_array_load_len(L, arr);

lua_pushvalue(L, 1); /* a i v a */
lua_pushvalue(L, 2); /* a i v a i */
state_array_checkarg(L, arr); /* a i v a i */
lua_pop(L, 2); /* a i v */
state_array_checkarg(L, arr); /* a i v */
lua_pushcfunction(L, setItemWithPrefix); /* a i v f */
state_array_push_key(L, arr->id); /* a i v f id-i */
lua_pushvalue(L, 3); /* a i v f id-i v */
Expand Down
30 changes: 13 additions & 17 deletions contract/system_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static int systemPrint(lua_State *L)
if (jsonValue == NULL) {
luaL_throwerror(L);
}
LuaPrint(service, jsonValue);
LuaPrint(L, service, jsonValue);
free(jsonValue);
return 0;
}
Expand Down Expand Up @@ -89,6 +89,7 @@ int getItemWithPrefix(lua_State *L)
if (ret.r0 == NULL)
return 0;

minus_inst_count(L, strlen(ret.r0));
if (lua_util_json_to_lua(L, ret.r0, false) != 0) {
strPushAndRelease(L, ret.r0);
luaL_error(L, "getItem error : can't convert %s", lua_tostring(L, -1));
Expand Down Expand Up @@ -378,38 +379,33 @@ static int os_difftime(lua_State *L)
static int lua_random(lua_State *L)
{
int *service = (int *)getLuaExecContext(L);
lua_Number n;
lua_Number min, max;
double d;
int min, max;

if (service == NULL) {
luaL_error(L, "cannot find execution context");
}

switch (lua_gettop(L)) {
case 0:
d = LuaRandomNumber(L, *service);
lua_pushnumber(L, d);
break;
case 1:
n = luaL_checkinteger(L, 1);
if (n < 1) {
max = luaL_checkint(L, 1);
if (max < 1) {
luaL_error(L, "system.random: the maximum value must be greater than zero");
}
n = LuaRandomInt(L, n, 0, *service);
lua_pushinteger(L, n);
lua_pushinteger(L, LuaRandomInt(1, max, *service));
break;
default:
min = luaL_checkinteger(L, 1);
max = luaL_checkinteger(L, 2);
case 2:
min = luaL_checkint(L, 1);
max = luaL_checkint(L, 2);
if (min < 1) {
luaL_error(L, "system.random: the minimum value must be greater than zero");
}
if (min > max) {
luaL_error(L, "system.random: the maximum value must be greater than the minimum value");
}
n = LuaRandomInt(L, min, max, *service);
lua_pushinteger(L, n);
lua_pushinteger(L, LuaRandomInt(min, max, *service));
break;
default:
luaL_error(L, "system.random: 1 or 2 arguments required");
break;
}

Expand Down
38 changes: 32 additions & 6 deletions contract/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,30 @@ static bool lua_util_dump_json (lua_State *L, int idx, sbuff_t *sbuf, bool json_
switch (lua_type(L, idx)) {
case LUA_TNUMBER: {
if (json_form && iskey) {
if (luaL_isinteger(L, idx))
if (luaL_isinteger(L, idx)) {
len = sprintf (tmp, "\"%ld\",", lua_tointeger(L, idx));
else
len = sprintf (tmp, "\"%.14g\",", lua_tonumber(L, idx));
}
else {
double d = lua_tonumber(L, idx);
if (isinf(d) || isnan(d)) {
lua_pushstring(L, "not support nan or infinity");
return false;
}
len = sprintf (tmp, "\"%.14g\",", d);
}
}
else {
if (luaL_isinteger(L, idx))
if (luaL_isinteger(L, idx)) {
len = sprintf (tmp, "%ld,", lua_tointeger(L, idx));
else
len = sprintf (tmp, "%.14g,", lua_tonumber(L, idx));
}
else {
double d = lua_tonumber(L, idx);
if (isinf(d) || isnan(d)) {
lua_pushstring(L, "not support nan or infinity");
return false;
}
len = sprintf (tmp, "%.14g,", lua_tonumber(L, idx));
}
}
src_val = tmp;
break;
Expand Down Expand Up @@ -549,6 +563,15 @@ static int json_to_lua (lua_State *L, char **start, bool check, bool is_bignum)
return 0;
}

void minus_inst_count(lua_State *L, int count) {
int cnt = luaL_instcount(L);

cnt -= count;
if (cnt <= 0)
cnt = 1;
luaL_setinstcount(L, cnt);
}

int lua_util_json_to_lua (lua_State *L, char *json, bool check)
{
if (json_to_lua (L, &json, check, false) != 0)
Expand Down Expand Up @@ -586,6 +609,7 @@ char *lua_util_get_json_from_stack (lua_State *L, int start, int end, bool json_
sbuf.buf[sbuf.idx] = '\0';
}

minus_inst_count(L, strlen(sbuf.buf));
return sbuf.buf;
}

Expand All @@ -605,6 +629,7 @@ char *lua_util_get_json (lua_State *L, int idx, bool json_form)
if (sbuf.idx != 0)
sbuf.buf[sbuf.idx - 1] = '\0';

minus_inst_count(L, strlen(sbuf.buf));
return sbuf.buf;
}

Expand All @@ -623,6 +648,7 @@ static int lua_json_decode (lua_State *L)
char *org = (char *)luaL_checkstring(L, -1);
char *json = strdup(org);

minus_inst_count(L, strlen(json));
if (lua_util_json_to_lua(L, json, true) != 0) {
free (json);
luaL_error(L, "not proper json format");
Expand Down
1 change: 1 addition & 0 deletions contract/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
char *lua_util_get_json (lua_State *L, int idx, bool json_form);
int lua_util_json_to_lua (lua_State *L, char *json, bool check);
char *lua_util_get_json_from_stack (lua_State *L, int start, int end, bool json_form);
void minus_inst_count(lua_State *L, int count);

int luaopen_json(lua_State *L);

Expand Down
1 change: 1 addition & 0 deletions contract/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ const char *vm_copy_result(lua_State *L, lua_State *target, int cnt)
if (json == NULL)
return lua_tostring(L, -1);

minus_inst_count(L, strlen(json));
lua_util_json_to_lua(target, json, false);
free (json);
}
Expand Down
Loading

0 comments on commit 72ab486

Please sign in to comment.