7 static void jsB_globalf(js_State *J, const char *name, js_CFunction cfun, int n)
9 js_newcfunction(J, cfun, name, n);
10 js_defglobal(J, name, JS_DONTENUM);
13 void jsB_propf(js_State *J, const char *name, js_CFunction cfun, int n)
15 js_newcfunction(J, cfun, name, n);
16 js_defproperty(J, -2, name, JS_DONTENUM);
19 void jsB_propn(js_State *J, const char *name, double number)
21 js_pushnumber(J, number);
22 js_defproperty(J, -2, name, JS_READONLY | JS_DONTENUM | JS_DONTCONF);
25 void jsB_props(js_State *J, const char *name, const char *string)
27 js_pushliteral(J, string);
28 js_defproperty(J, -2, name, JS_DONTENUM);
31 static void jsB_parseInt(js_State *J)
33 const char *s = js_tostring(J, 1);
34 double radix = js_isdefined(J, 2) ? js_tonumber(J, 2) : 10;
38 while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
41 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
45 n = strtol(s, &e, radix);
47 js_pushnumber(J, NAN);
52 static void jsB_parseFloat(js_State *J)
54 const char *s = js_tostring(J, 1);
58 while (jsY_iswhite(*s) || jsY_isnewline(*s)) ++s;
59 if (!strncmp(s, "Infinity", 8))
60 js_pushnumber(J, INFINITY);
61 else if (!strncmp(s, "+Infinity", 9))
62 js_pushnumber(J, INFINITY);
63 else if (!strncmp(s, "-Infinity", 9))
64 js_pushnumber(J, -INFINITY);
66 n = js_stringtofloat(s, &e);
68 js_pushnumber(J, NAN);
74 static void jsB_isNaN(js_State *J)
76 double n = js_tonumber(J, 1);
77 js_pushboolean(J, isnan(n));
80 static void jsB_isFinite(js_State *J)
82 double n = js_tonumber(J, 1);
83 js_pushboolean(J, isfinite(n));
86 static void Encode(js_State *J, const char *str, const char *unescaped)
90 static const char *HEX = "0123456789ABCDEF";
93 int c = (unsigned char) *str++;
94 if (strchr(unescaped, c))
98 js_putc(J, &sb, HEX[(c >> 4) & 0xf]);
99 js_putc(J, &sb, HEX[c & 0xf]);
108 js_pushstring(J, sb ? sb->s : "");
113 static void Decode(js_State *J, const char *str, const char *reserved)
115 js_Buffer *sb = NULL;
119 int c = (unsigned char) *str++;
123 if (!str[0] || !str[1])
124 js_urierror(J, "truncated escape sequence");
127 if (!jsY_ishex(a) || !jsY_ishex(b))
128 js_urierror(J, "invalid escape sequence");
129 c = jsY_tohex(a) << 4 | jsY_tohex(b);
130 if (!strchr(reserved, c))
133 js_putc(J, &sb, '%');
145 js_pushstring(J, sb ? sb->s : "");
150 #define URIRESERVED ";/?:@&=+$,"
151 #define URIALPHA "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
152 #define URIDIGIT "0123456789"
153 #define URIMARK "-_.!~*`()"
154 #define URIUNESCAPED URIALPHA URIDIGIT URIMARK
156 static void jsB_decodeURI(js_State *J)
158 Decode(J, js_tostring(J, 1), URIRESERVED "#");
161 static void jsB_decodeURIComponent(js_State *J)
163 Decode(J, js_tostring(J, 1), "");
166 static void jsB_encodeURI(js_State *J)
168 Encode(J, js_tostring(J, 1), URIUNESCAPED URIRESERVED "#");
171 static void jsB_encodeURIComponent(js_State *J)
173 Encode(J, js_tostring(J, 1), URIUNESCAPED);
176 void jsB_init(js_State *J)
178 /* Create the prototype objects here, before the constructors */
179 J->Object_prototype = jsV_newobject(J, JS_COBJECT, NULL);
180 J->Array_prototype = jsV_newobject(J, JS_CARRAY, J->Object_prototype);
181 J->Function_prototype = jsV_newobject(J, JS_CCFUNCTION, J->Object_prototype);
182 J->Boolean_prototype = jsV_newobject(J, JS_CBOOLEAN, J->Object_prototype);
183 J->Number_prototype = jsV_newobject(J, JS_CNUMBER, J->Object_prototype);
184 J->String_prototype = jsV_newobject(J, JS_CSTRING, J->Object_prototype);
185 J->RegExp_prototype = jsV_newobject(J, JS_COBJECT, J->Object_prototype);
186 J->Date_prototype = jsV_newobject(J, JS_CDATE, J->Object_prototype);
188 /* All the native error types */
189 J->Error_prototype = jsV_newobject(J, JS_CERROR, J->Object_prototype);
190 J->EvalError_prototype = jsV_newobject(J, JS_CERROR, J->Error_prototype);
191 J->RangeError_prototype = jsV_newobject(J, JS_CERROR, J->Error_prototype);
192 J->ReferenceError_prototype = jsV_newobject(J, JS_CERROR, J->Error_prototype);
193 J->SyntaxError_prototype = jsV_newobject(J, JS_CERROR, J->Error_prototype);
194 J->TypeError_prototype = jsV_newobject(J, JS_CERROR, J->Error_prototype);
195 J->URIError_prototype = jsV_newobject(J, JS_CERROR, J->Error_prototype);
197 /* Create the constructors and fill out the prototype objects */
210 /* Initialize the global object */
211 js_pushnumber(J, NAN);
212 js_defglobal(J, "NaN", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
214 js_pushnumber(J, INFINITY);
215 js_defglobal(J, "Infinity", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
218 js_defglobal(J, "undefined", JS_READONLY | JS_DONTENUM | JS_DONTCONF);
220 jsB_globalf(J, "parseInt", jsB_parseInt, 1);
221 jsB_globalf(J, "parseFloat", jsB_parseFloat, 1);
222 jsB_globalf(J, "isNaN", jsB_isNaN, 1);
223 jsB_globalf(J, "isFinite", jsB_isFinite, 1);
225 jsB_globalf(J, "decodeURI", jsB_decodeURI, 1);
226 jsB_globalf(J, "decodeURIComponent", jsB_decodeURIComponent, 1);
227 jsB_globalf(J, "encodeURI", jsB_encodeURI, 1);
228 jsB_globalf(J, "encodeURIComponent", jsB_encodeURIComponent, 1);