forked from petrihakkinen/lua-array
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlarray.c
More file actions
105 lines (87 loc) · 2.55 KB
/
larray.c
File metadata and controls
105 lines (87 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
** Lua arrays
** See Copyright Notice in lua.h
*/
#define larray_c
#define LUA_CORE
#include "lprefix.h"
#include <math.h>
#include <limits.h>
#include "lua.h"
#include "ldebug.h"
#include "ldo.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "larray.h"
#include "lvm.h"
Array *luaA_new (lua_State *L) {
GCObject *o = luaC_newobj(L, LUA_TARRAY, sizeof(Array));
Array *a = gco2a(o);
a->array = NULL;
a->sizearray = 0;
return a;
}
void luaA_resize (lua_State *L, Array *a, unsigned int newsize) {
unsigned int i;
unsigned int oldsize = a->sizearray;
TValue *newarray;
/* allocate new array */
newarray = luaM_reallocvector(L, a->array, oldsize, newsize, TValue);
if (newarray == NULL && newsize > 0) { /* allocation failed? */
luaM_error(L); /* raise error (with array unchanged) */
}
/* allocation ok; initialize new part of the array */
a->array = newarray; /* set new array part */
a->sizearray = newsize;
for (i = oldsize; i < newsize; i++) /* clear new slice of the array */
setempty(&a->array[i]);
}
void luaA_free (lua_State *L, Array *a) {
luaM_freearray(L, a->array, a->sizearray);
luaM_free(L, a);
}
const TValue *luaA_getint (Array *a, lua_Integer key) {
/* (1 <= key && key <= t->sizearray) */
if (l_castS2U(key) - 1u < a->sizearray)
return &a->array[key - 1];
else
return luaO_nilobject;
}
const TValue *luaA_get (lua_State *L, Array *a, const TValue *key) {
if (ttypetag(key) == LUA_TNUMINT) {
lua_Integer ikey = ivalue(key);
if (l_castS2U(ikey) - 1u < a->sizearray)
return &a->array[ikey - 1];
else
return luaO_nilobject;
} else {
/* TODO: the error message could be improved */
/*luaG_runerror(L, "attempt to index array with a non-integer value");*/
return luaO_nilobject;
}
}
void luaA_setint (lua_State *L, Array *a, lua_Integer key, TValue *value) {
if (l_castS2U(key) - 1u < a->sizearray) {
/* set value! */
TValue* val = &a->array[key - 1];
val->value_ = value->value_;
val->tt_ = value->tt_;
checkliveness(L,val);
} else {
/* TODO: this error message could be improved! */
luaG_runerror(L, "array index out of bounds");
}
}
void luaA_set (lua_State *L, Array *a, const TValue* key, TValue *value) {
if (ttypetag(key) == LUA_TNUMINT) {
lua_Integer ikey = ivalue(key);
luaA_setint(L, a, ikey, value);
} else {
/* TODO: the error message could be improved */
luaG_runerror(L, "attempt to index array with a non-integer value");
}
}