]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c15969fd | 2 | * Copyright (C) 2009-2013 Jay Freeman (saurik) |
37954781 JF |
3 | */ |
4 | ||
c15969fd | 5 | /* GNU General Public License, Version 3 {{{ */ |
37954781 | 6 | /* |
c15969fd JF |
7 | * Cycript is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation, either version 3 of the License, | |
10 | * or (at your option) any later version. | |
37954781 | 11 | * |
c15969fd JF |
12 | * Cycript is distributed in the hope that it will be useful, but |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
37954781 | 16 | * |
c15969fd | 17 | * You should have received a copy of the GNU General Public License |
b3378a02 JF |
18 | * along with Cycript. If not, see <http://www.gnu.org/licenses/>. |
19 | **/ | |
37954781 JF |
20 | /* }}} */ |
21 | ||
37954781 JF |
22 | #include <dlfcn.h> |
23 | #include <iconv.h> | |
24 | ||
25 | #include "cycript.hpp" | |
26 | ||
37954781 JF |
27 | #include "Pooling.hpp" |
28 | ||
29 | #include <sys/mman.h> | |
30 | ||
31 | #include <iostream> | |
32 | #include <ext/stdio_filebuf.h> | |
33 | #include <set> | |
34 | #include <map> | |
35 | #include <iomanip> | |
36 | #include <sstream> | |
37 | #include <cmath> | |
38 | ||
39 | #include "Parser.hpp" | |
40 | #include "Cycript.tab.hh" | |
41 | ||
42 | #include "Error.hpp" | |
37954781 | 43 | #include "String.hpp" |
2f51d6ab | 44 | #include "Execute.hpp" |
07db5f4d | 45 | #include "JavaScript.hpp" |
37954781 | 46 | |
59cd74c1 JF |
47 | /* C Strings {{{ */ |
48 | template <typename Type_> | |
49 | _finline size_t iconv_(size_t (*iconv)(iconv_t, Type_, size_t *, char **, size_t *), iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { | |
50 | return iconv(cd, const_cast<Type_>(inbuf), inbytesleft, outbuf, outbytesleft); | |
51 | } | |
52 | ||
53 | #ifdef __GLIBC__ | |
54 | #define UCS_2_INTERNAL "UCS-2" | |
55 | #else | |
56 | #define UCS_2_INTERNAL "UCS-2-INTERNAL" | |
57 | #endif | |
58 | ||
b799113b | 59 | CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16) { |
59cd74c1 JF |
60 | _assert(pool != NULL); |
61 | ||
62 | const char *in(reinterpret_cast<const char *>(utf16.data)); | |
63 | ||
64 | iconv_t conversion(_syscall(iconv_open("UTF-8", UCS_2_INTERNAL))); | |
65 | ||
66 | // XXX: this is wrong | |
67 | size_t size(utf16.size * 5); | |
68 | char *out(new(pool) char[size]); | |
69 | CYUTF8String utf8(out, size); | |
70 | ||
71 | size = utf16.size * 2; | |
72 | _syscall(iconv_(&iconv, conversion, const_cast<char **>(&in), &size, &out, &utf8.size)); | |
73 | ||
74 | *out = '\0'; | |
75 | utf8.size = out - utf8.data; | |
76 | ||
77 | _syscall(iconv_close(conversion)); | |
78 | ||
79 | return utf8; | |
80 | } | |
81 | ||
b799113b | 82 | CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8) { |
59cd74c1 JF |
83 | _assert(pool != NULL); |
84 | ||
85 | const char *in(utf8.data); | |
86 | ||
87 | iconv_t conversion(_syscall(iconv_open(UCS_2_INTERNAL, "UTF-8"))); | |
88 | ||
89 | // XXX: this is wrong | |
90 | size_t size(utf8.size * 5); | |
91 | uint16_t *temp(new (pool) uint16_t[size]); | |
92 | CYUTF16String utf16(temp, size * 2); | |
93 | char *out(reinterpret_cast<char *>(temp)); | |
94 | ||
95 | size = utf8.size; | |
96 | _syscall(iconv_(&iconv, conversion, const_cast<char **>(&in), &size, &out, &utf16.size)); | |
97 | ||
98 | utf16.size = reinterpret_cast<uint16_t *>(out) - utf16.data; | |
99 | temp[utf16.size] = 0; | |
100 | ||
101 | _syscall(iconv_close(conversion)); | |
102 | ||
103 | return utf16; | |
104 | } | |
105 | /* }}} */ | |
37954781 JF |
106 | /* Index Offsets {{{ */ |
107 | size_t CYGetIndex(const CYUTF8String &value) { | |
108 | if (value.data[0] != '0') { | |
ba4fa42f JF |
109 | size_t index(0); |
110 | for (size_t i(0); i != value.size; ++i) { | |
111 | if (!DigitRange_[value.data[i]]) | |
112 | return _not(size_t); | |
113 | index *= 10; | |
114 | index += value.data[i] - '0'; | |
115 | } | |
116 | return index; | |
117 | } else if (value.size == 1) | |
37954781 | 118 | return 0; |
ba4fa42f JF |
119 | else |
120 | return _not(size_t); | |
37954781 JF |
121 | } |
122 | ||
ba4fa42f | 123 | // XXX: this isn't actually right |
37954781 JF |
124 | bool CYGetOffset(const char *value, ssize_t &index) { |
125 | if (value[0] != '0') { | |
126 | char *end; | |
127 | index = strtol(value, &end, 10); | |
128 | if (value + strlen(value) == end) | |
129 | return true; | |
130 | } else if (value[1] == '\0') { | |
131 | index = 0; | |
132 | return true; | |
133 | } | |
134 | ||
135 | return false; | |
136 | } | |
137 | /* }}} */ | |
37954781 JF |
138 | /* JavaScript *ify {{{ */ |
139 | void CYStringify(std::ostringstream &str, const char *data, size_t size) { | |
140 | unsigned quot(0), apos(0); | |
141 | for (const char *value(data), *end(data + size); value != end; ++value) | |
142 | if (*value == '"') | |
143 | ++quot; | |
144 | else if (*value == '\'') | |
145 | ++apos; | |
146 | ||
147 | bool single(quot > apos); | |
148 | ||
149 | str << (single ? '\'' : '"'); | |
150 | ||
151 | for (const char *value(data), *end(data + size); value != end; ++value) | |
152 | switch (*value) { | |
153 | case '\\': str << "\\\\"; break; | |
154 | case '\b': str << "\\b"; break; | |
155 | case '\f': str << "\\f"; break; | |
156 | case '\n': str << "\\n"; break; | |
157 | case '\r': str << "\\r"; break; | |
158 | case '\t': str << "\\t"; break; | |
159 | case '\v': str << "\\v"; break; | |
160 | ||
161 | case '"': | |
162 | if (!single) | |
163 | str << "\\\""; | |
164 | else goto simple; | |
165 | break; | |
166 | ||
167 | case '\'': | |
168 | if (single) | |
169 | str << "\\'"; | |
170 | else goto simple; | |
171 | break; | |
172 | ||
173 | default: | |
b64ab4da | 174 | // this test is designed to be "awesome", generating neither warnings nor incorrect results |
37954781 | 175 | if (*value < 0x20 || *value >= 0x7f) |
51a72a9d | 176 | str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value)); |
37954781 JF |
177 | else simple: |
178 | str << *value; | |
179 | } | |
180 | ||
181 | str << (single ? '\'' : '"'); | |
182 | } | |
183 | ||
184 | void CYNumerify(std::ostringstream &str, double value) { | |
185 | char string[32]; | |
186 | // XXX: I want this to print 1e3 rather than 1000 | |
187 | sprintf(string, "%.17g", value); | |
188 | str << string; | |
189 | } | |
190 | ||
191 | bool CYIsKey(CYUTF8String value) { | |
192 | const char *data(value.data); | |
193 | size_t size(value.size); | |
194 | ||
195 | if (size == 0) | |
196 | return false; | |
197 | ||
198 | if (DigitRange_[data[0]]) { | |
199 | size_t index(CYGetIndex(value)); | |
200 | if (index == _not(size_t)) | |
201 | return false; | |
202 | } else { | |
203 | if (!WordStartRange_[data[0]]) | |
204 | return false; | |
205 | for (size_t i(1); i != size; ++i) | |
206 | if (!WordEndRange_[data[i]]) | |
207 | return false; | |
208 | } | |
209 | ||
210 | return true; | |
211 | } | |
212 | /* }}} */ | |
213 | ||
37954781 JF |
214 | double CYCastDouble(const char *value, size_t size) { |
215 | char *end; | |
216 | double number(strtod(value, &end)); | |
217 | if (end != value + size) | |
218 | return NAN; | |
219 | return number; | |
220 | } | |
221 | ||
222 | double CYCastDouble(const char *value) { | |
223 | return CYCastDouble(value, strlen(value)); | |
224 | } | |
225 | ||
07db5f4d JF |
226 | size_t CYArrayLength(JSContextRef context, JSObjectRef array) { |
227 | return CYCastDouble(context, CYGetProperty(context, array, length_s)); | |
228 | } | |
229 | ||
230 | JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index) { | |
231 | JSValueRef exception(NULL); | |
232 | JSValueRef value(JSObjectGetPropertyAtIndex(context, array, index, &exception)); | |
233 | CYThrow(context, exception); | |
234 | return value; | |
235 | } | |
236 | ||
237 | void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) { | |
238 | JSValueRef exception(NULL); | |
239 | JSValueRef arguments[1]; | |
240 | arguments[0] = value; | |
241 | JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype"))); | |
242 | JSObjectCallAsFunction(context, CYCastJSObject(context, CYGetProperty(context, Array, push_s)), array, 1, arguments, &exception); | |
243 | CYThrow(context, exception); | |
244 | } | |
245 | ||
2eb8215d JF |
246 | extern "C" void CydgetPoolParse(apr_pool_t *remote, const uint16_t **data, size_t *size) { |
247 | CYLocalPool local; | |
248 | ||
2eb8215d | 249 | CYUTF8String utf8(CYPoolUTF8String(local, CYUTF16String(*data, *size))); |
d3b63265 JF |
250 | CYStream stream(utf8.data, utf8.data + utf8.size); |
251 | CYDriver driver(stream); | |
59cd74c1 | 252 | |
d3b63265 | 253 | cy::parser parser(driver); |
59cd74c1 JF |
254 | if (parser.parse() != 0 || !driver.errors_.empty()) |
255 | return; | |
256 | ||
029bc65b | 257 | CYOptions options; |
2eb8215d | 258 | CYContext context(options); |
59cd74c1 JF |
259 | driver.program_->Replace(context); |
260 | std::ostringstream str; | |
029bc65b | 261 | CYOutput out(str, options); |
59cd74c1 JF |
262 | out << *driver.program_; |
263 | std::string code(str.str()); | |
264 | ||
b799113b JF |
265 | CYPool pool(remote); |
266 | CYUTF16String utf16(CYPoolUTF16String(pool, CYUTF8String(code.c_str(), code.size()))); | |
59cd74c1 JF |
267 | |
268 | *data = utf16.data; | |
269 | *size = utf16.size; | |
270 | } | |
271 | ||
37954781 JF |
272 | static bool initialized_; |
273 | ||
9cad30fa | 274 | void CYInitializeStatic() { |
37954781 JF |
275 | if (!initialized_) |
276 | initialized_ = true; | |
277 | else return; | |
278 | ||
279 | _aprcall(apr_initialize()); | |
37954781 JF |
280 | } |
281 | ||
b799113b | 282 | CYPool &CYGetGlobalPool() { |
9cad30fa | 283 | CYInitializeStatic(); |
b799113b JF |
284 | static CYPool pool; |
285 | return pool; | |
37954781 JF |
286 | } |
287 | ||
37954781 JF |
288 | void CYThrow(const char *format, ...) { |
289 | va_list args; | |
b64ab4da | 290 | va_start(args, format); |
37954781 JF |
291 | throw CYPoolError(format, args); |
292 | // XXX: does this matter? :( | |
b64ab4da | 293 | va_end(args); |
37954781 JF |
294 | } |
295 | ||
b799113b JF |
296 | const char *CYPoolError::PoolCString(CYPool &pool) const { |
297 | return pool.strdup(message_); | |
37954781 JF |
298 | } |
299 | ||
300 | CYPoolError::CYPoolError(const char *format, ...) { | |
301 | va_list args; | |
b64ab4da | 302 | va_start(args, format); |
b799113b | 303 | message_ = pool_.vsprintf(format, args); |
b64ab4da | 304 | va_end(args); |
37954781 JF |
305 | } |
306 | ||
307 | CYPoolError::CYPoolError(const char *format, va_list args) { | |
b799113b | 308 | message_ = pool_.vsprintf(format, args); |
37954781 | 309 | } |