History

Implement some Array functions.

Tor Andersson committed 12 years ago

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

Showing the changes to jsarray.c (1 of 3 files). Show all

Unified · Split

jsarray.c+188−3
@@ -2,8 +2,186 @@ · 1 unmodified line
22 #include "jsvalue.h"
33 #include "jsbuiltin.h"
44
5−static int jsB_Array(js_State *J, int argc) { return 0; }
6−static int jsB_new_Array(js_State *J, int argc) { return 0; }
5+static int jsB_new_Array(js_State *J, int argc)
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+
30+static 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+
68+static 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+
126+static 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+
149+static 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+
180+static int Ap_toString(js_State *J, int argc)
181+{
182+ js_pop(J, argc);
183+ return Ap_join(J, 0);
184+}
7185
8186 static int A_isArray(js_State *J, int argc)
9187 {
@@ -19,7 +197,14 @@ static int A_isArray(js_State *J, int argc) · 9 unmodified lines
19197 void jsB_initarray(js_State *J)
20198 {
21199 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);
23208 {
24209 /* ECMA-262-5 */
25210 jsB_propf(J, "isArray", A_isArray, 1);

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