int release(lua_State *L) { TraySettings &s = g_tray.traySettings(); xml::node sv(L"tray"); sv.create(L"params"); sv.set(L"timeout", s.timeout); sv.set(L"interval", s.interval); sv.set(L"showactive", s.showactive ? 1 : 0); wchar_t buffer[16]; swprintf(buffer, L"%d,%d,%d", GetRValue(s.text), GetGValue(s.text), GetBValue(s.text)); sv.set(L"textcolor", buffer); swprintf(buffer, L"%d,%d,%d", GetRValue(s.background), GetGValue(s.background), GetBValue(s.background)); sv.set(L"bkgndcolor", buffer); sv.move(L"/"); std::wstring path; base::getPath(L, L"config.xml", &path); if (!sv.save(path.c_str())) { sv.deletenode(); std::wstring error(L"Ошибка записи файла с настройками плагина : "); error.append(path); return luaT_error(L, error.c_str()); } sv.deletenode(); if (g_tray.IsWindow()) g_tray.DestroyWindow(); return 0; }
void luamp_convert_key(struct lua_State *L, struct luaL_serializer *cfg, struct mpstream *stream, int index) { /* Performs keyfy() logic */ struct tuple *tuple = luaT_istuple(L, index); if (tuple != NULL) return tuple_to_mpstream(tuple, stream); struct luaL_field field; if (luaL_tofield(L, cfg, index, &field) < 0) luaT_error(L); if (field.type == MP_ARRAY) { lua_pushvalue(L, index); luamp_encode_r(L, cfg, stream, &field, 0); lua_pop(L, 1); } else if (field.type == MP_NIL) { mpstream_encode_array(stream, 0); } else { mpstream_encode_array(stream, 1); lua_pushvalue(L, index); luamp_encode_r(L, cfg, stream, &field, 0); lua_pop(L, 1); } }
int netserver_check(lua_State *L) { if (!luaT_check(L, 1, LUA_TNUMBER)) return luaT_error(L, "netserver: check [incorrect parameter]"); int mseconds = lua_tointeger(L, -1); selector.Check(mseconds); return 0; }
//------------------------------------------------------------------------------ int netserver_open(lua_State *L) { if (!luaT_check(L, 2, LUA_TNUMBER, LUA_TTABLE)) return luaT_error(L, "netserver: listen [incorrect parameters]"); if (!_network.init()) return luaT_error(L, "netserver: listen [network not initialized]"); int port = lua_tointeger(L, 1); if (port < 0 || port > 65535) return luaT_errorf(L, "netserver: listen [incorrect (%d) port number]", port); for (int i=0,e=listen_sockets.size(); i<e; ++i) { ListenSocket *s = listen_sockets[i]; if (s->port == port) return luaT_errorf(L, "netserver: listen [port (%d) busy]", port); } ListenSocket *new_socket = NULL; Socket s; if (s.create()) { if (s.setreuseopt() && s.bind(port) && s.setnonblockopt() && s.listen(10)) { new_socket = new (std::nothrow) ListenSocket(); if (new_socket) { new_socket->socket = s; new_socket->port = port; listen_sockets.push_back(new_socket); selector.AddSocket(s); } } if (!new_socket) s.close(); } if (!new_socket) return luaT_errorf(L, "netserver: listen [port (%d) does't opened]", port); lua_pushboolean(L, 1); return 1; }
void luamp_encode_tuple(struct lua_State *L, struct luaL_serializer *cfg, struct mpstream *stream, int index) { struct tuple *tuple = luaT_istuple(L, index); if (tuple != NULL) { return tuple_to_mpstream(tuple, stream); } else if (luamp_encode(L, cfg, stream, index) != MP_ARRAY) { diag_set(ClientError, ER_TUPLE_NOT_ARRAY); luaT_error(L); } }
static int lbox_tuple_slice(struct lua_State *L) { struct tuple *tuple = lua_checktuple(L, 1); int argc = lua_gettop(L) - 1; uint32_t start, end; int32_t offset; /* * Prepare the range. The second argument is optional. * If the end is beyond tuple size, adjust it. * If no arguments, or start > end, return an error. */ if (argc == 0 || argc > 2) luaL_error(L, "tuple.slice(): bad arguments"); int32_t field_count = box_tuple_field_count(tuple); offset = lua_tonumber(L, 2); if (offset >= 0 && offset < field_count) { start = offset; } else if (offset < 0 && -offset <= field_count) { start = offset + field_count; } else { return luaL_error(L, "tuple.slice(): start >= field count"); } if (argc == 2) { offset = lua_tonumber(L, 3); if (offset > 0 && offset <= field_count) { end = offset; } else if (offset < 0 && -offset < field_count) { end = offset + field_count; } else { return luaL_error(L, "tuple.slice(): end > field count"); } } else { end = field_count; } if (end <= start) return luaL_error(L, "tuple.slice(): start must be less than end"); box_tuple_iterator_t *it = box_tuple_iterator(tuple); lua_pushcfunction(L, lbox_tuple_slice_wrapper); lua_pushlightuserdata(L, it); lua_pushinteger(L, start); lua_pushinteger(L, end); int rc = luaT_call(L, 3, end - start); box_tuple_iterator_free(it); if (rc != 0) return luaT_error(L); return end - start; }
static int lbox_tuple_to_string(struct lua_State *L) { struct tuple *tuple = lua_checktuple(L, 1); size_t used = region_used(&fiber()->gc); char *res = tuple_to_yaml(tuple); if (res == NULL) { region_truncate(&fiber()->gc, used); return luaT_error(L); } lua_pushstring(L, res); region_truncate(&fiber()->gc, used); return 1; }
int netserver_close(lua_State *L) { if (!luaT_check(L, 1, LUA_TNUMBER)) return luaT_error(L, "netserver: close [incorrect parameter]"); int index = -1; int port = lua_tointeger(L, -1); for (int i=0,e=listen_sockets.size(); i<e; ++i) { ListenSocket *s = listen_sockets[i]; if (s->port == port && !s->closing) { index = i; break; } } if (index == -1) return luaT_errorf(L, "netserver: close [incorrect (%d) port number]", port); ListenSocket *s = listen_sockets[index]; s->socket.shutdown(HOWTO_RECEIVE); s->closing = true; return 0; }
static int lbox_tuple_new(lua_State *L) { int argc = lua_gettop(L); if (argc < 1) { lua_newtable(L); /* create an empty tuple */ ++argc; } /* * Use backward-compatible parameters format: * box.tuple.new(1, 2, 3) (idx == 0), or the new one: * box.tuple.new({1, 2, 3}) (idx == 1). */ int idx = argc == 1 && (lua_istable(L, 1) || luaT_istuple(L, 1)); box_tuple_format_t *fmt = box_tuple_format_default(); struct tuple *tuple = luaT_tuple_new(L, idx, fmt); if (tuple == NULL) return luaT_error(L); /* box_tuple_new() doesn't leak on exception, see public API doc */ luaT_pushtuple(L, tuple); return 1; }
/** * Tuple transforming function. * * Remove the fields designated by 'offset' and 'len' from an tuple, * and replace them with the elements of supplied data fields, * if any. * * Function returns newly allocated tuple. * It does not change any parent tuple data. */ static int lbox_tuple_transform(struct lua_State *L) { struct tuple *tuple = lua_checktuple(L, 1); int argc = lua_gettop(L); if (argc < 3) luaL_error(L, "tuple.transform(): bad arguments"); lua_Integer offset = lua_tointeger(L, 2); /* Can be negative and can be > INT_MAX */ lua_Integer len = lua_tointeger(L, 3); lua_Integer field_count = box_tuple_field_count(tuple); /* validate offset and len */ if (offset == 0) { luaL_error(L, "tuple.transform(): offset is out of bound"); } else if (offset < 0) { if (-offset > field_count) luaL_error(L, "tuple.transform(): offset is out of bound"); offset += field_count + 1; } else if (offset > field_count) { offset = field_count + 1; } if (len < 0) luaL_error(L, "tuple.transform(): len is negative"); if (len > field_count + 1 - offset) len = field_count + 1 - offset; assert(offset + len <= field_count + 1); /* * Calculate the number of operations and length of UPDATE expression */ uint32_t op_cnt = 0; if (offset < field_count + 1 && len > 0) op_cnt++; if (argc > 3) op_cnt += argc - 3; if (op_cnt == 0) { /* tuple_update() does not accept an empty operation list. */ luaT_pushtuple(L, tuple); return 1; } struct ibuf *buf = tarantool_lua_ibuf; ibuf_reset(buf); struct mpstream stream; mpstream_init(&stream, buf, ibuf_reserve_cb, ibuf_alloc_cb, luamp_error, L); /* * Prepare UPDATE expression */ mpstream_encode_array(&stream, op_cnt); if (len > 0) { mpstream_encode_array(&stream, 3); mpstream_encode_str(&stream, "#"); mpstream_encode_uint(&stream, offset); mpstream_encode_uint(&stream, len); } for (int i = argc ; i > 3; i--) { mpstream_encode_array(&stream, 3); mpstream_encode_str(&stream, "!"); mpstream_encode_uint(&stream, offset); luamp_encode(L, luaL_msgpack_default, &stream, i); } mpstream_flush(&stream); uint32_t new_size = 0, bsize; const char *old_data = tuple_data_range(tuple, &bsize); struct region *region = &fiber()->gc; size_t used = region_used(region); struct tuple *new_tuple = NULL; /* * Can't use box_tuple_update() since transform must reset * the tuple format to default. The new tuple most likely * won't coerce into the original space format, so we have * to use the default one with no restrictions on field * count or types. */ const char *new_data = tuple_update_execute(region_aligned_alloc_cb, region, buf->buf, buf->buf + ibuf_used(buf), old_data, old_data + bsize, &new_size, 1, NULL); if (new_data != NULL) new_tuple = tuple_new(box_tuple_format_default(), new_data, new_data + new_size); region_truncate(region, used); if (new_tuple == NULL) luaT_error(L); luaT_pushtuple(L, new_tuple); ibuf_reset(buf); return 1; }