History

Implement some Array functions.

Tor Andersson committed 12 years ago

Commit cbdebfd1 parent 6fd45b83 files · +195 −3Browse files

Unified · Split

3 files
  1. js.h +1 −0
  2. jsarray.c +188 −3
  3. jsrun.c +6 −0
js.h+1−0
@@ -121,6 +121,7 @@ int js_isnumber(js_State *J, int idx); · 120 unmodified lines
121int js_isstring(js_State *J, int idx);121int js_isstring(js_State *J, int idx);
122int js_isprimitive(js_State *J, int idx);122int js_isprimitive(js_State *J, int idx);
123int js_isobject(js_State *J, int idx);123int js_isobject(js_State *J, int idx);
124int js_isarray(js_State *J, int idx);
124int js_iscallable(js_State *J, int idx);125int js_iscallable(js_State *J, int idx);
125int js_isuserdata(js_State *J, const char *tag, int idx);126int js_isuserdata(js_State *J, const char *tag, int idx);
126 127
jsarray.c+188−3
@@ -2,8 +2,186 @@ · 1 unmodified line
2#include "jsvalue.h"2#include "jsvalue.h"
3#include "jsbuiltin.h"3#include "jsbuiltin.h"
4 4
5static int jsB_Array(js_State *J, int argc) { return 0; }5static int jsB_new_Array(js_State *J, int argc)
6static int jsB_new_Array(js_State *J, int argc) { return 0; }6{
7 int i;
8
9 js_newarray(J);
10
11 if (argc == 1) {
12 if (js_isnumber(J, 1)) {
13 js_copy(J, 1);
14 js_setproperty(J, -2, "length");
15 } else {
16 js_copy(J, 1);
17 js_setproperty(J, -2, "0");
18 }
19 } else {
20 char buf[32];
21 for (i = 1; i <= argc; ++i) {
22 sprintf(buf, "%u", i - 1);
23 js_copy(J, i);
24 js_setproperty(J, -2, buf);
25 }
26 }
27 return 1;
28}
29
30static int Ap_concat(js_State *J, int argc)
31{
32 unsigned int n, len, i;
33 char buf[32];
34
35 js_newarray(J);
36 n = 0;
37
38 for (i = 0; i <= argc; i++) {
39 js_copy(J, i);
40 if (js_isarray(J, -1)) {
41 unsigned int k = 0;
42
43 js_getproperty(J, -1, "length");
44 len = js_touint32(J, -1);
45 js_pop(J, 1);
46
47 while (k < len) {
48 sprintf(buf, "%u", k);
49 if (js_hasproperty(J, -1, buf)) {
50 js_getproperty(J, -1, buf);
51 sprintf(buf, "%u", n);
52 js_setproperty(J, -3, buf);
53 }
54 ++k;
55 ++n;
56 }
57 js_pop(J, 1);
58 } else {
59 sprintf(buf, "%u", n);
60 js_setproperty(J, -2, buf);
61 ++n;
62 }
63 }
64
65 return 1;
66}
67
68static int Ap_join(js_State *J, int argc)
69{
70 char * volatile out = NULL;
71 char buf[32];
72 const char *r, *sep = ",";
73 unsigned int seplen, n;
74 unsigned int k, len;
75
76 js_getproperty(J, 0, "length");
77 len = js_touint32(J, -1);
78 js_pop(J, 1);
79
80 if (argc == 1 && !js_isundefined(J, 1)) {
81 sep = js_tostring(J, 1);
82 seplen = strlen(sep);
83 }
84
85 if (len == 0) {
86 js_pushliteral(J, "");
87 return 1;
88 }
89
90 if (js_try(J)) {
91 free(out);
92 js_throw(J);
93 }
94
95 n = 1;
96 for (k = 0; k < len; ++k) {
97 sprintf(buf, "%u", k);
98 js_getproperty(J, 0, buf);
99 if (js_isundefined(J, -1) || js_isnull(J, -1))
100 r = "";
101 else
102 r = js_tostring(J, -1);
103 n += strlen(r);
104
105 if (k == 0) {
106 out = malloc(n);
107 strcpy(out, r);
108 } else {
109 n += seplen;
110 out = realloc(out, n);
111 strcat(out, sep);
112 strcat(out, r);
113 }
114
115 js_pop(J, 1);
116 }
117
118 js_pushstring(J, out);
119
120 js_endtry(J);
121 free(out);
122 return 1;
123}
124
125
126static int Ap_pop(js_State *J, int argc)
127{
128 unsigned int n;
129 char buf[32];
130
131 js_getproperty(J, 0, "length");
132 n = js_touint32(J, -1);
133 js_pop(J, 1);
134
135 if (n > 0) {
136 sprintf(buf, "%u", n - 1);
137 js_getproperty(J, 0, buf);
138 js_delproperty(J, 0, buf);
139 js_pushnumber(J, n - 1);
140 js_setproperty(J, 0, "length");
141 } else {
142 js_pushnumber(J, 0);
143 js_setproperty(J, 0, "length");
144 js_pushundefined(J);
145 }
146 return 1;
147}
148
149static int Ap_push(js_State *J, int argc)
150{
151 unsigned int n;
152 int i;
153 char buf[32];
154
155 js_getproperty(J, 0, "length");
156 n = js_touint32(J, -1);
157 js_pop(J, 1);
158
159 for (i = 1; i <= argc; ++i) {
160 sprintf(buf, "%u", n);
161 js_copy(J, i);
162 js_setproperty(J, 0, buf);
163 ++n;
164 }
165
166 js_pushnumber(J, n);
167 js_setproperty(J, 0, "length");
168
169 js_pushnumber(J, n);
170 return 1;
171}
172
173// reverse
174// shift
175// slice
176// sort
177// splice
178// unshift
179
180static int Ap_toString(js_State *J, int argc)
181{
182 js_pop(J, argc);
183 return Ap_join(J, 0);
184}
7 185
8static int A_isArray(js_State *J, int argc)186static int A_isArray(js_State *J, int argc)
9{187{
@@ -19,7 +197,14 @@ static int A_isArray(js_State *J, int argc) · 9 unmodified lines
19void jsB_initarray(js_State *J)197void jsB_initarray(js_State *J)
20{198{
21 js_pushobject(J, J->Array_prototype);199 js_pushobject(J, J->Array_prototype);
22 js_newcconstructor(J, jsB_Array, jsB_new_Array, 1);200 {
201 jsB_propf(J, "toString", Ap_toString, 0);
202 jsB_propf(J, "concat", Ap_concat, 1);
203 jsB_propf(J, "join", Ap_join, 1);
204 jsB_propf(J, "pop", Ap_pop, 0);
205 jsB_propf(J, "push", Ap_push, 1);
206 }
207 js_newcconstructor(J, jsB_new_Array, jsB_new_Array, 1);
23 {208 {
24 /* ECMA-262-5 */209 /* ECMA-262-5 */
25 jsB_propf(J, "isArray", A_isArray, 1);210 jsB_propf(J, "isArray", A_isArray, 1);
jsrun.c+6−0
@@ -98,6 +98,12 @@ int js_iscallable(js_State *J, int idx) · 97 unmodified lines
98 return 0;98 return 0;
99}99}
100 100
101int js_isarray(js_State *J, int idx)
102{
103 const js_Value *v = stackidx(J, idx);
104 return v->type == JS_TOBJECT && v->u.object->type == JS_CARRAY;
105}
106
101int js_isiterator(js_State *J, int idx)107int js_isiterator(js_State *J, int idx)
102{108{
103 const js_Value *v = stackidx(J, idx);109 const js_Value *v = stackidx(J, idx);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t