Пример #1
0
lxs_string* lxs_screate(lua_State* const L,
                        size_t cap /*= 0u*/,
                        bool force /*= true*/)
{
    lxs_assert(L, L);

    lxs_string* s = NULL;
    if (cap == 0u)
    {
        s = static_cast<lxs_string*>(luaM_malloc(L, sizeof(lxs_string)));
    }
    else
    {
        if (!force)
            cap = lxs_mgrow(L, 0u, cap, true, LXS_GROTH_FACTOR);

        lxs_assert(L, cap > 0u);
        lxs_scheck_mem_limits(L, 0u, cap, 0u);

        s = static_cast<lxs_string*>(
            luaM_malloc(L, sizeof(lxs_string) + sizeof(char) * cap)
        );

        s->data = reinterpret_cast<char*>(s + sizeof(lxs_string));
        lxs_assert(L, s->data);
    }
    lxs_assert(L, s);

    s->cap = cap;

    return s;
}
Пример #2
0
void luaL_addlibtolist(luaL_reg *l, int32 n) {
	luaL_libList *list = (luaL_libList *)luaM_malloc(sizeof(luaL_libList));
	list->list = l;
	list->number = n;
	list->next = list_of_libs;
	list_of_libs = list;
}
Пример #3
0
Closure *luaF_newclosure(int32 nelems) {
	Closure *c = (Closure *)luaM_malloc(sizeof(Closure) + nelems * sizeof(TObject));
	luaO_insertlist(&rootcl, (GCnode *)c);
	nblocks += gcsizeclosure(c);
	c->nelems = nelems;
	return c;
}
Пример #4
0
static int ws2812_buffer_fill(lua_State* L) {
  ws2812_buffer * buffer = (ws2812_buffer*)lua_touserdata(L, 1);

  luaL_argcheck(L, buffer && buffer->canary == CANARY_VALUE, 1, "ws2812.buffer expected");

  // Grab colors
  int i, j;
  int * colors = luaM_malloc(L, buffer->colorsPerLed * sizeof(int));

  for (i = 0; i < buffer->colorsPerLed; i++)
  {
    colors[i] = luaL_checkinteger(L, 2+i);
  }

  // Fill buffer
  uint8_t * p = &buffer->values[0];
  for(i = 0; i < buffer->size; i++)
  {
    for (j = 0; j < buffer->colorsPerLed; j++)
    {
      *p++ = colors[j];
    }
  }

  // Free memory
  luaM_free(L, colors);

  return 0;
}
Пример #5
0
static lua_State *mallocstate (lua_State *L) {
  lu_byte *block = (lu_byte *)luaM_malloc(L, sizeof(lua_State) + EXTRASPACE);
  if (block == NULL) return NULL;
  else {
    block += EXTRASPACE;
    return cast(lua_State *, block);
  }
}
Пример #6
0
Closure *luaF_newclosure (lua_State *L, int nelems) {
  int size = sizeclosure(nelems);
  Closure *c = (Closure *)luaM_malloc(L, size);
  c->next = L->rootcl;
  L->rootcl = c;
  c->mark = c;
  c->nupvalues = nelems;
  L->nblocks += size;
  return c;
}
Пример #7
0
static void block (LexState *ls) {
  /* block -> chunk */
  FuncState *fs = ls->fs;
  BlockCnt *pbl = (BlockCnt*)luaM_malloc(ls->L,sizeof(BlockCnt));
  enterblock(fs, pbl, 0);
  chunk(ls);
  lua_assert(pbl->breaklist == NO_JUMP);
  leaveblock(fs);
  luaM_free(ls->L,pbl);
}
Пример #8
0
static void unpersiststring(UnpersistInfo *upi) {
	/* perms reftbl sptbl ref */
	int length;
	char* string;
	verify(luaZ_read(&upi->zio, &length, sizeof(int)) == 0);
	string = (char *)luaM_malloc(upi->L, length);
	verify(luaZ_read(&upi->zio, string, length) == 0);
	lua_pushlstring(upi->L, string, length);
	/* perms reftbl sptbl ref str */
	luaM_free(upi->L, string, length);
}
Пример #9
0
lua_State *luaE_newthread (lua_State *L) {
  lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
  luaC_link(L, obj2gco(L1), LUA_TTHREAD);
  preinit_state(L1, G(L));
  stack_init(L1, L);  /* init stack */
  setobj2n(L, gt(L1), gt(L));  /* share table of globals */
  L1->hookmask = L->hookmask;
  L1->basehookcount = L->basehookcount;
  L1->hook = L->hook;
  resethookcount(L1);
  lua_assert(iswhite(obj2gco(L1)));
  return L1;
}
Пример #10
0
void *rstack_alloc (lua_State *L, size_t size) {
    RStack *rs = rstack(L);
    Region *r = check_exp(rs->cregnum > 0, rs->creg);
    RObject *top = r->top;
    if (top == rs->rbuf.last) {
        RObject *ohead = rs->rbuf.head;
        buf_resize(L, rs, rs->rbuf.size * 2);
        buf_fix(rs, ohead);
        top = r->top;
    }
    top->body = luaM_malloc(L, size);
    ++r->top;
    return top->body;
}
Пример #11
0
static TaggedString *newone_s (char *str, long l, unsigned long h)
{
  TaggedString *ts = (TaggedString *)luaM_malloc(sizeof(TaggedString)+l);
  memcpy(ts->str, str, l);
  ts->str[l] = 0;  /* ending 0 */
  ts->u.s.globalval.ttype = LUA_T_NIL;  /* initialize global value */
  ts->u.s.len = l;
  ts->constindex = 0;
  L->nblocks += gcsizestring(l);
  ts->head.marked = 0;
  ts->head.next = (GCnode *)ts;  /* signal it is in no list */
  ts->hash = h;
  return ts;
}
Пример #12
0
lua_State *luaE_newthread (lua_State *L) {
  lua_State *L1 = tostate(luaM_malloc(L, state_size(lua_State)));
  luaC_link(L, obj2gco(L1), LUA_TTHREAD);
  setthvalue(L, L->top, L1); /* put thread on stack */
  incr_top(L);
  preinit_state(L1, G(L));
  stack_init(L1, L);  /* init stack */
  setobj2n(L, gt(L1), gt(L));  /* share table of globals */
  L1->hookmask = L->hookmask;
  L1->basehookcount = L->basehookcount;
  L1->hook = L->hook;
  resethookcount(L1);
  lua_assert(!isdead(G(L), obj2gco(L1)));
  L->top--; /* remove thread from stack */
  return L1;
}
Пример #13
0
static void forbody (LexState *ls, int base, int line, int nvars, int isnum) {
  /* forbody -> DO block */
  BlockCnt *pbl = (BlockCnt*)luaM_malloc(ls->L,sizeof(BlockCnt));
  FuncState *fs = ls->fs;
  int prep, endfor;
  adjustlocalvars(ls, 3);  /* control variables */
  checknext(ls, TK_DO);
  prep = isnum ? luaK_codeAsBx(fs, OP_FORPREP, base, NO_JUMP) : luaK_jump(fs);
  enterblock(fs, pbl, 0);  /* scope for declared variables */
  adjustlocalvars(ls, nvars);
  luaK_reserveregs(fs, nvars);
  block(ls);
  leaveblock(fs);  /* end of scope for declared variables */
  luaK_patchtohere(fs, prep);
  endfor = (isnum) ? luaK_codeAsBx(fs, OP_FORLOOP, base, NO_JUMP) :
                     luaK_codeABC(fs, OP_TFORLOOP, base, 0, nvars);
  luaK_fixline(fs, line);  /* pretend that `OP_FOR' starts the loop */
  luaK_patchlist(fs, (isnum ? endfor : luaK_jump(fs)), prep + 1);
  luaM_free(ls->L,pbl);
}
Пример #14
0
void lxs_sinit(lua_State* const L, lxs_string* const s, size_t capacity /*= 0*/)
{
    lxs_assert(L, L);
    lxs_assert(L, s);

    if (capacity == 0)
#if LUAXS_STR_READONLY_OPTIONS
        capacity = LUAXS_STR_INITIAL_CAPACITY;
#else
        capacity = lxs_sdefault_capacity();
#endif // LUAXS_STR_READONLY_OPTIONS

    lxs_scheck_mem_limits(L, 0u, capacity, 0u);

    char* buffer = static_cast<char*>(luaM_malloc(L, capacity));
    if (buffer == NULL)
        lxs_error(L, MEMERRMSG);

    s->cap  = capacity;
    s->len  = 0;
    s->data = buffer;
}
Пример #15
0
static int websocket_createClient(lua_State *L) {
  NODE_DBG("websocket_createClient\n");

  // create user data
  ws_data *data = (ws_data *) luaM_malloc(L, sizeof(ws_data));
  data->onConnection = LUA_NOREF;
  data->onReceive = LUA_NOREF;
  data->onClose = LUA_NOREF;
  data->self_ref = LUA_NOREF; // only set when ws:connect is called

  ws_info *ws = (ws_info *) lua_newuserdata(L, sizeof(ws_info));
  ws->connectionState = 0;
  ws->onConnection = &websocketclient_onConnectionCallback;
  ws->onReceive = &websocketclient_onReceiveCallback;
  ws->onFailure = &websocketclient_onCloseCallback;
  ws->reservedData = data;

  // set its metatable
  luaL_getmetatable(L, METATABLE_WSCLIENT);
  lua_setmetatable(L, -2);

  return 1;
}
Пример #16
0
void rec_bytecode(TProtoFunc* func, int* inst) {
	int n_op = num_opcodes(func);
	int i, newsize;
	Opcode *opcode_list = luaM_newvector(n_op, Opcode);

	Byte* p = func->code;

	//Load opcodes in a more descriptive structure
	i = 0;
	do
		p += INFO(func, p, &opcode_list[i]);
	while (opcode_list[i++].op != ENDCODE);
	luaM_free(func->code);
	func->code = NULL;

	//For jump instructions, calculate the number of
	//instructions to skip/rewind, from the number of
	//bytes to skip/rewind. The result is improperly
	//stored in op.arg2, since it's a unused field.
	for (i = 0; opcode_list[i].op != ENDCODE; ++i) {
		Opcode &op = opcode_list[i];
		int bytesToSkip, instToSkip, bytesToRewind, instToRewind, j;

		switch (op.op_class) {
		//Forward jump
		case ONTJMP:
		case ONFJMP:
		case JMP:
		case IFFJMP:
			bytesToSkip = op.arg;
			instToSkip = 0;
			j = i;
			while (bytesToSkip > 0) {
				instToSkip++;
				bytesToSkip -= opcode_list[++j].size;
			}
			assert(bytesToSkip == 0);
			op.arg2 = instToSkip;
			break;

		//Backwards jump
		case IFTUPJMP:
		case IFFUPJMP:
			bytesToRewind = op.arg;
			instToRewind = 0;
			j = i;
			while (bytesToRewind > 0) {
				bytesToRewind -= opcode_list[j--].size;
				instToRewind++;
			}
			assert(bytesToRewind == 0);
			op.arg2 = instToRewind;
			break;
		}
	}

	//Change const index
	for (i = 0; opcode_list[i].op != ENDCODE; ++i) {
		Opcode &op = opcode_list[i];

		//Change const index, if needed
		if (op.op_class == PUSHCONSTANT ||
			op.op_class == GETGLOBAL ||
			op.op_class == SETGLOBAL ||
			op.op_class == GETDOTTED ||
			op.op_class == PUSHSELF)
			if (op.arg != inst[op.arg]) {
				op.arg = inst[op.arg];
				fix_op(&op);
			}
	}

	//Recalculate the number of bytes to jump
	bool expJmp;
	do {
		expJmp = false;

		for (i = 0; opcode_list[i].op != ENDCODE; ++i) {
			Opcode &op = opcode_list[i];
			int bytesToSkip, instToSkip, bytesToRewind, instToRewind, j;


			switch (op.op_class) {
			//Forward jump
			case ONTJMP:
			case ONFJMP:
			case JMP:
			case IFFJMP:
				bytesToSkip = 0;
				instToSkip = op.arg2;
				j = i;
				while (instToSkip > 0) {
					instToSkip--;
					bytesToSkip += opcode_list[++j].size;
				}
				assert(instToSkip == 0);
				op.arg = bytesToSkip;
				break;

			//Backwards jump
			case IFTUPJMP:
			case IFFUPJMP:
				bytesToRewind = 0;
				instToRewind = op.arg2;
				j = i;
				while (instToRewind > 0) {
					bytesToRewind += opcode_list[j--].size;
					instToRewind--;
				}
				assert(instToRewind == 0);
				op.arg = bytesToRewind;
				break;

			default:
				continue;
			}

			//Expand JMPs to JMPWs, if needed.
			//It set also the expJmp flag, in order
			//to make another cycle, since this
			//action changed again the code size
			if (op.size == 2 && op.arg > 255) {
				op.op++;
				op.size++;
				expJmp = true;
			}
		}
	} while (expJmp);

	//Calculate the size of new bytecode and
	//alloc the space for it
	i = 0;
	newsize = 0;
	do
		newsize += opcode_list[i].size;
	while (opcode_list[i++].op != ENDCODE);

	Byte *code = (Byte*)luaM_malloc(newsize);
	func->code = code;

	//Compile bytecode
	Byte out[4];

	//Out stacksize and arguments number
	code[0] = (byte)opcode_list[0].arg;
	if (opcode_list[1].op == VARARGS)
		code[1] = (byte)opcode_list[1].arg + ZEROVARARG;
	else
		code[1] = (byte)opcode_list[1].arg;
	code += 2;

	for (i = 2; i < n_op; ++i) {
		Opcode &op = opcode_list[i];

		//Out opcode
		out[0] = (byte)op.op;

		//Out args
		if (op.op == SETLIST || op.op == CLOSURE || op.op == CALLFUNC) {
			out[1] = (byte)op.arg;
			out[2] = (byte)op.arg2;
		}
		else if (op.size == 2)
			out[1] = (byte)op.arg;
		else if (op.size >= 3)
			WRITE_LE_UINT16(out + 1, op.arg);
		if (op.op == SETLISTW)
			out[3] = (byte)op.arg2;

		memcpy(code, out, op.size);
		code += op.size;
	}

	luaM_free(opcode_list);
}
Пример #17
0
/* this function handles only `%d', `%c', %f, %p, and `%s' formats
   and now it handles %x and %X as well */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp)
{
#if LUAXS_CORE_FORMAT == 2
    size_t alloclen = _vscprintf(fmt, argp) + 1;
    char* buf = luaM_malloc(L, alloclen);
    vsprintf_s(buf, alloclen, fmt, argp);
    pushstr(L, buf);
    luaM_freemem(L, buf, alloclen);
    return svalue(L->top - 1);
#else
    int n = 1;
    pushstr(L, "");
    for (;;) {
        const char *e = strchr(fmt, '%');
        if (e == NULL) break;
        setsvalue2s(L, L-&gt;top, luaS_newlstr(L, fmt, e-fmt));
        incr_top(L);
        switch (*(e+1)) {
        case 's': {
            const char *s = va_arg(argp, char *);
            if (s == NULL) s = "(null)";
            pushstr(L, s);
            break;
            }
        case 'c': {
            char buff[2];
            buff[0] = cast(char, va_arg(argp, int));
            buff[1] = '\0';
            pushstr(L, buff);
            break;
            }
        case 'd': {
            setnvalue(L-&gt;top, cast_num(va_arg(argp, int)));
            incr_top(L);
            break;
            }
        case 'f': {
            setnvalue(L-&gt;top, cast_num(va_arg(argp, l_uacNumber)));
            incr_top(L);
            break;
            }
        case 'p': {
            char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
            sprintf(buff, "%p", va_arg(argp, void *));
            pushstr(L, buff);
            break;
            }
#if LUAXS_CORE_FORMAT == 1
        case 'X': {
            char buff[sizeof(int)*2+3];
            sprintf(buff, "%X", va_arg(argp, int));
            pushstr(L, buff);
            break;
            }
        case 'x': {
            char buff[sizeof(int)*2+3];
            sprintf(buff, "%x", va_arg(argp, int));
            pushstr(L, buff);
            break;
            }
#endif
        case '%': {
            pushstr(L, "%");
            break;
            }
        default: {
            char buff[3];
            buff[0] = '%';
            buff[1] = *(e+1);
            buff[2] = '\0';
            pushstr(L, buff);
            break;
            }
        }
        n += 2;
        fmt = e+2;
    }
    pushstr(L, fmt);
    luaV_concat(L, n+1, cast_int(L-&gt;top - L-&gt;base) - 1);
    L-&gt;top -= n;
    return svalue(L-&gt;top - 1);
#endif
}
Пример #18
0
void rec_bytecode(TProtoFunc* func, int* inst) {
	int n_op = num_opcodes(func);
	int i, newsize;
	Opcode *opcode_list = luaM_newvector(n_op, Opcode);

	Byte* p = func->code;

	//Change const index
	i = 0;
	newsize = 0;
	while (1) {
		p += INFO(func, p, &opcode_list[i]);
		Opcode &op = opcode_list[i];

		//Change const index, if needed
		if (op.op_class == PUSHCONSTANT ||
			op.op_class == GETGLOBAL ||
			op.op_class == SETGLOBAL ||
			op.op_class == GETDOTTED ||
			op.op_class == PUSHSELF)
			if (op.arg != inst[op.arg]) {
				op.arg = inst[op.arg];
				fix_op(&op);
			}

		newsize += op.size;
		++i;
		if (op.op == ENDCODE)
			break;
	}

	luaM_free(func->code);
	Byte *code = (Byte*)luaM_malloc(newsize);
	func->code = code;

	//Compile bytecode
	Byte out[4];

	//Out stacksize and arguments number
	code[0] = (byte)opcode_list[0].arg;
	if (opcode_list[1].op == VARARGS)
		code[1] = (byte)opcode_list[1].arg + ZEROVARARG;
	else
		code[1] = (byte)opcode_list[1].arg;
	code += 2;

	for (i = 2; i < n_op; ++i) {
		Opcode &op = opcode_list[i];

		//Out opcode
		out[0] = (byte)op.op;

		//Out args
		if (op.op == SETLIST || op.op == CLOSURE || op.op == CALLFUNC) {
			out[1] = (byte)op.arg;
			out[2] = (byte)op.arg2;
		}
		else if (op.size == 2)
			out[1] = (byte)op.arg;
		else if (op.size >= 3)
			WRITE_LE_UINT16(out + 1, op.arg);
		if (op.op == SETLISTW)
			out[3] = (byte)op.arg2;

		memcpy(code, out, op.size);
		code += op.size;
	}

	luaM_free(opcode_list);
}