]>
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 | 22 | #include <dlfcn.h> |
37954781 JF |
23 | |
24 | #include "cycript.hpp" | |
25 | ||
37954781 JF |
26 | #include "Pooling.hpp" |
27 | ||
28 | #include <sys/mman.h> | |
29 | ||
30 | #include <iostream> | |
31 | #include <ext/stdio_filebuf.h> | |
32 | #include <set> | |
33 | #include <map> | |
34 | #include <iomanip> | |
35 | #include <sstream> | |
36 | #include <cmath> | |
37 | ||
38 | #include "Parser.hpp" | |
39 | #include "Cycript.tab.hh" | |
40 | ||
41 | #include "Error.hpp" | |
37954781 | 42 | #include "String.hpp" |
2f51d6ab | 43 | #include "Execute.hpp" |
07db5f4d | 44 | #include "JavaScript.hpp" |
37954781 | 45 | |
142cb9e3 | 46 | #include "ConvertUTF.h" |
59cd74c1 | 47 | |
142cb9e3 | 48 | /* C Strings {{{ */ |
b799113b | 49 | CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16) { |
59cd74c1 JF |
50 | // XXX: this is wrong |
51 | size_t size(utf16.size * 5); | |
142cb9e3 | 52 | char *temp(new(pool) char[size]); |
59cd74c1 | 53 | |
142cb9e3 JF |
54 | const uint16_t *lhs(utf16.data); |
55 | uint8_t *rhs(reinterpret_cast<uint8_t *>(temp)); | |
56 | _assert(ConvertUTF16toUTF8(&lhs, lhs + utf16.size, &rhs, rhs + size, lenientConversion) == conversionOK); | |
59cd74c1 | 57 | |
142cb9e3 JF |
58 | *rhs = 0; |
59 | return CYUTF8String(temp, reinterpret_cast<char *>(rhs) - temp); | |
59cd74c1 JF |
60 | } |
61 | ||
b799113b | 62 | CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8) { |
59cd74c1 JF |
63 | // XXX: this is wrong |
64 | size_t size(utf8.size * 5); | |
65 | uint16_t *temp(new (pool) uint16_t[size]); | |
59cd74c1 | 66 | |
142cb9e3 JF |
67 | const uint8_t *lhs(reinterpret_cast<const uint8_t *>(utf8.data)); |
68 | uint16_t *rhs(temp); | |
69 | _assert(ConvertUTF8toUTF16(&lhs, lhs + utf8.size, &rhs, rhs + size, lenientConversion) == conversionOK); | |
59cd74c1 | 70 | |
142cb9e3 JF |
71 | *rhs = 0; |
72 | return CYUTF16String(temp, rhs - temp); | |
59cd74c1 JF |
73 | } |
74 | /* }}} */ | |
37954781 JF |
75 | /* Index Offsets {{{ */ |
76 | size_t CYGetIndex(const CYUTF8String &value) { | |
77 | if (value.data[0] != '0') { | |
ba4fa42f JF |
78 | size_t index(0); |
79 | for (size_t i(0); i != value.size; ++i) { | |
80 | if (!DigitRange_[value.data[i]]) | |
81 | return _not(size_t); | |
82 | index *= 10; | |
83 | index += value.data[i] - '0'; | |
84 | } | |
85 | return index; | |
86 | } else if (value.size == 1) | |
37954781 | 87 | return 0; |
ba4fa42f JF |
88 | else |
89 | return _not(size_t); | |
37954781 JF |
90 | } |
91 | ||
ba4fa42f | 92 | // XXX: this isn't actually right |
37954781 JF |
93 | bool CYGetOffset(const char *value, ssize_t &index) { |
94 | if (value[0] != '0') { | |
95 | char *end; | |
96 | index = strtol(value, &end, 10); | |
97 | if (value + strlen(value) == end) | |
98 | return true; | |
99 | } else if (value[1] == '\0') { | |
100 | index = 0; | |
101 | return true; | |
102 | } | |
103 | ||
104 | return false; | |
105 | } | |
106 | /* }}} */ | |
37954781 JF |
107 | /* JavaScript *ify {{{ */ |
108 | void CYStringify(std::ostringstream &str, const char *data, size_t size) { | |
109 | unsigned quot(0), apos(0); | |
110 | for (const char *value(data), *end(data + size); value != end; ++value) | |
111 | if (*value == '"') | |
112 | ++quot; | |
113 | else if (*value == '\'') | |
114 | ++apos; | |
115 | ||
116 | bool single(quot > apos); | |
117 | ||
118 | str << (single ? '\'' : '"'); | |
119 | ||
120 | for (const char *value(data), *end(data + size); value != end; ++value) | |
121 | switch (*value) { | |
122 | case '\\': str << "\\\\"; break; | |
123 | case '\b': str << "\\b"; break; | |
124 | case '\f': str << "\\f"; break; | |
125 | case '\n': str << "\\n"; break; | |
126 | case '\r': str << "\\r"; break; | |
127 | case '\t': str << "\\t"; break; | |
128 | case '\v': str << "\\v"; break; | |
129 | ||
130 | case '"': | |
131 | if (!single) | |
132 | str << "\\\""; | |
133 | else goto simple; | |
134 | break; | |
135 | ||
136 | case '\'': | |
137 | if (single) | |
138 | str << "\\'"; | |
139 | else goto simple; | |
140 | break; | |
141 | ||
142 | default: | |
b64ab4da | 143 | // this test is designed to be "awesome", generating neither warnings nor incorrect results |
37954781 | 144 | if (*value < 0x20 || *value >= 0x7f) |
51a72a9d | 145 | str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value)); |
37954781 JF |
146 | else simple: |
147 | str << *value; | |
148 | } | |
149 | ||
150 | str << (single ? '\'' : '"'); | |
151 | } | |
152 | ||
153 | void CYNumerify(std::ostringstream &str, double value) { | |
154 | char string[32]; | |
155 | // XXX: I want this to print 1e3 rather than 1000 | |
156 | sprintf(string, "%.17g", value); | |
157 | str << string; | |
158 | } | |
159 | ||
160 | bool CYIsKey(CYUTF8String value) { | |
161 | const char *data(value.data); | |
162 | size_t size(value.size); | |
163 | ||
164 | if (size == 0) | |
165 | return false; | |
166 | ||
167 | if (DigitRange_[data[0]]) { | |
168 | size_t index(CYGetIndex(value)); | |
169 | if (index == _not(size_t)) | |
170 | return false; | |
171 | } else { | |
172 | if (!WordStartRange_[data[0]]) | |
173 | return false; | |
174 | for (size_t i(1); i != size; ++i) | |
175 | if (!WordEndRange_[data[i]]) | |
176 | return false; | |
177 | } | |
178 | ||
179 | return true; | |
180 | } | |
181 | /* }}} */ | |
182 | ||
37954781 JF |
183 | double CYCastDouble(const char *value, size_t size) { |
184 | char *end; | |
185 | double number(strtod(value, &end)); | |
186 | if (end != value + size) | |
187 | return NAN; | |
188 | return number; | |
189 | } | |
190 | ||
191 | double CYCastDouble(const char *value) { | |
192 | return CYCastDouble(value, strlen(value)); | |
193 | } | |
194 | ||
07db5f4d JF |
195 | size_t CYArrayLength(JSContextRef context, JSObjectRef array) { |
196 | return CYCastDouble(context, CYGetProperty(context, array, length_s)); | |
197 | } | |
198 | ||
199 | JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index) { | |
200 | JSValueRef exception(NULL); | |
201 | JSValueRef value(JSObjectGetPropertyAtIndex(context, array, index, &exception)); | |
202 | CYThrow(context, exception); | |
203 | return value; | |
204 | } | |
205 | ||
206 | void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) { | |
207 | JSValueRef exception(NULL); | |
208 | JSValueRef arguments[1]; | |
209 | arguments[0] = value; | |
210 | JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype"))); | |
211 | JSObjectCallAsFunction(context, CYCastJSObject(context, CYGetProperty(context, Array, push_s)), array, 1, arguments, &exception); | |
212 | CYThrow(context, exception); | |
213 | } | |
214 | ||
b4a1ede1 | 215 | extern "C" void CydgetMemoryParse(const uint16_t **data, size_t *size) { |
2eb8215d JF |
216 | CYLocalPool local; |
217 | ||
2eb8215d | 218 | CYUTF8String utf8(CYPoolUTF8String(local, CYUTF16String(*data, *size))); |
d3b63265 JF |
219 | CYStream stream(utf8.data, utf8.data + utf8.size); |
220 | CYDriver driver(stream); | |
59cd74c1 | 221 | |
d3b63265 | 222 | cy::parser parser(driver); |
59cd74c1 JF |
223 | if (parser.parse() != 0 || !driver.errors_.empty()) |
224 | return; | |
225 | ||
029bc65b | 226 | CYOptions options; |
2eb8215d | 227 | CYContext context(options); |
59cd74c1 JF |
228 | driver.program_->Replace(context); |
229 | std::ostringstream str; | |
029bc65b | 230 | CYOutput out(str, options); |
59cd74c1 JF |
231 | out << *driver.program_; |
232 | std::string code(str.str()); | |
233 | ||
b4a1ede1 | 234 | CYPool pool; |
b799113b | 235 | CYUTF16String utf16(CYPoolUTF16String(pool, CYUTF8String(code.c_str(), code.size()))); |
59cd74c1 | 236 | |
b4a1ede1 JF |
237 | size_t bytes(utf16.size * sizeof(uint16_t)); |
238 | uint16_t *copy(reinterpret_cast<uint16_t *>(malloc(bytes))); | |
239 | memcpy(copy, utf16.data, bytes); | |
240 | ||
241 | *data = copy; | |
59cd74c1 JF |
242 | *size = utf16.size; |
243 | } | |
244 | ||
b799113b | 245 | CYPool &CYGetGlobalPool() { |
b799113b JF |
246 | static CYPool pool; |
247 | return pool; | |
37954781 JF |
248 | } |
249 | ||
37954781 JF |
250 | void CYThrow(const char *format, ...) { |
251 | va_list args; | |
b64ab4da | 252 | va_start(args, format); |
37954781 JF |
253 | throw CYPoolError(format, args); |
254 | // XXX: does this matter? :( | |
b64ab4da | 255 | va_end(args); |
37954781 JF |
256 | } |
257 | ||
b799113b JF |
258 | const char *CYPoolError::PoolCString(CYPool &pool) const { |
259 | return pool.strdup(message_); | |
37954781 JF |
260 | } |
261 | ||
0cbeddf8 JF |
262 | CYPoolError::CYPoolError(const CYPoolError &rhs) : |
263 | message_(pool_.strdup(rhs.message_)) | |
264 | { | |
265 | } | |
266 | ||
37954781 JF |
267 | CYPoolError::CYPoolError(const char *format, ...) { |
268 | va_list args; | |
b64ab4da | 269 | va_start(args, format); |
0cbeddf8 JF |
270 | // XXX: there might be a beter way to think about this |
271 | message_ = pool_.vsprintf(64, format, args); | |
b64ab4da | 272 | va_end(args); |
37954781 JF |
273 | } |
274 | ||
275 | CYPoolError::CYPoolError(const char *format, va_list args) { | |
0cbeddf8 JF |
276 | // XXX: there might be a beter way to think about this |
277 | message_ = pool_.vsprintf(64, format, args); | |
37954781 | 278 | } |