]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Optimizing JavaScript Compiler/Runtime | |
2 | * Copyright (C) 2009-2015 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* GNU Affero General Public License, Version 3 {{{ */ | |
6 | /* | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU Affero General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | ||
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU Affero General Public License for more details. | |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | **/ | |
20 | /* }}} */ | |
21 | ||
22 | #include "cycript.hpp" | |
23 | ||
24 | #include <iostream> | |
25 | #include <set> | |
26 | #include <map> | |
27 | #include <iomanip> | |
28 | #include <sstream> | |
29 | #include <cmath> | |
30 | ||
31 | #include <dlfcn.h> | |
32 | ||
33 | #include <sys/mman.h> | |
34 | ||
35 | #include "ConvertUTF.h" | |
36 | #include "Driver.hpp" | |
37 | #include "Error.hpp" | |
38 | #include "Execute.hpp" | |
39 | #include "Pooling.hpp" | |
40 | #include "String.hpp" | |
41 | #include "Syntax.hpp" | |
42 | ||
43 | template <> | |
44 | ::pthread_key_t CYLocal<CYPool>::key_ = Key_(); | |
45 | ||
46 | /* C Strings {{{ */ | |
47 | CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16) { | |
48 | // XXX: this is wrong | |
49 | size_t size(utf16.size * 5); | |
50 | char *temp(new(pool) char[size]); | |
51 | ||
52 | const uint16_t *lhs(utf16.data); | |
53 | uint8_t *rhs(reinterpret_cast<uint8_t *>(temp)); | |
54 | _assert(ConvertUTF16toUTF8(&lhs, lhs + utf16.size, &rhs, rhs + size, lenientConversion) == conversionOK); | |
55 | ||
56 | *rhs = 0; | |
57 | return CYUTF8String(temp, reinterpret_cast<char *>(rhs) - temp); | |
58 | } | |
59 | ||
60 | CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8) { | |
61 | // XXX: this is wrong | |
62 | size_t size(utf8.size * 5); | |
63 | uint16_t *temp(new (pool) uint16_t[size]); | |
64 | ||
65 | const uint8_t *lhs(reinterpret_cast<const uint8_t *>(utf8.data)); | |
66 | uint16_t *rhs(temp); | |
67 | _assert(ConvertUTF8toUTF16(&lhs, lhs + utf8.size, &rhs, rhs + size, lenientConversion) == conversionOK); | |
68 | ||
69 | *rhs = 0; | |
70 | return CYUTF16String(temp, rhs - temp); | |
71 | } | |
72 | /* }}} */ | |
73 | /* Index Offsets {{{ */ | |
74 | size_t CYGetIndex(const CYUTF8String &value) { | |
75 | if (value.data[0] != '0') { | |
76 | size_t index(0); | |
77 | for (size_t i(0); i != value.size; ++i) { | |
78 | if (!DigitRange_[value.data[i]]) | |
79 | return _not(size_t); | |
80 | index *= 10; | |
81 | index += value.data[i] - '0'; | |
82 | } | |
83 | return index; | |
84 | } else if (value.size == 1) | |
85 | return 0; | |
86 | else | |
87 | return _not(size_t); | |
88 | } | |
89 | ||
90 | // XXX: this isn't actually right | |
91 | bool CYGetOffset(const char *value, ssize_t &index) { | |
92 | if (value[0] != '0') { | |
93 | char *end; | |
94 | index = strtol(value, &end, 10); | |
95 | if (value + strlen(value) == end) | |
96 | return true; | |
97 | } else if (value[1] == '\0') { | |
98 | index = 0; | |
99 | return true; | |
100 | } | |
101 | ||
102 | return false; | |
103 | } | |
104 | /* }}} */ | |
105 | /* JavaScript *ify {{{ */ | |
106 | void CYStringify(std::ostringstream &str, const char *data, size_t size) { | |
107 | unsigned quot(0), apos(0); | |
108 | for (const char *value(data), *end(data + size); value != end; ++value) | |
109 | if (*value == '"') | |
110 | ++quot; | |
111 | else if (*value == '\'') | |
112 | ++apos; | |
113 | ||
114 | bool single(quot > apos); | |
115 | ||
116 | str << (single ? '\'' : '"'); | |
117 | ||
118 | for (const char *value(data), *end(data + size); value != end; ++value) | |
119 | switch (uint8_t next = *value) { | |
120 | case '\\': str << "\\\\"; break; | |
121 | case '\b': str << "\\b"; break; | |
122 | case '\f': str << "\\f"; break; | |
123 | case '\n': str << "\\n"; break; | |
124 | case '\r': str << "\\r"; break; | |
125 | case '\t': str << "\\t"; break; | |
126 | case '\v': str << "\\v"; break; | |
127 | ||
128 | case '"': | |
129 | if (!single) | |
130 | str << "\\\""; | |
131 | else goto simple; | |
132 | break; | |
133 | ||
134 | case '\'': | |
135 | if (single) | |
136 | str << "\\'"; | |
137 | else goto simple; | |
138 | break; | |
139 | ||
140 | case '\0': | |
141 | if (value[1] >= '0' && value[1] <= '9') | |
142 | str << "\\x00"; | |
143 | else | |
144 | str << "\\0"; | |
145 | break; | |
146 | ||
147 | default: | |
148 | if (next >= 0x20 && next < 0x7f) simple: | |
149 | str << *value; | |
150 | else { | |
151 | unsigned levels(1); | |
152 | if ((next & 0x80) != 0) | |
153 | while ((next & 0x80 >> ++levels) != 0); | |
154 | ||
155 | unsigned point(next & 0xff >> levels); | |
156 | while (--levels != 0) | |
157 | point = point << 6 | uint8_t(*++value) & 0x3f; | |
158 | ||
159 | if (point < 0x100) | |
160 | str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << point; | |
161 | else if (point < 0x10000) | |
162 | str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << point; | |
163 | else { | |
164 | point -= 0x10000; | |
165 | str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xd800 | point >> 0x0a); | |
166 | str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xdc00 | point & 0x3ff); | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | str << (single ? '\'' : '"'); | |
172 | } | |
173 | ||
174 | void CYNumerify(std::ostringstream &str, double value) { | |
175 | if (std::isinf(value)) { | |
176 | if (value < 0) | |
177 | str << '-'; | |
178 | str << "Infinity"; | |
179 | return; | |
180 | } | |
181 | ||
182 | char string[32]; | |
183 | // XXX: I want this to print 1e3 rather than 1000 | |
184 | sprintf(string, "%.17g", value); | |
185 | str << string; | |
186 | } | |
187 | ||
188 | bool CYIsKey(CYUTF8String value) { | |
189 | const char *data(value.data); | |
190 | size_t size(value.size); | |
191 | ||
192 | if (size == 0) | |
193 | return false; | |
194 | ||
195 | if (DigitRange_[data[0]]) { | |
196 | size_t index(CYGetIndex(value)); | |
197 | if (index == _not(size_t)) | |
198 | return false; | |
199 | } else { | |
200 | if (!WordStartRange_[data[0]]) | |
201 | return false; | |
202 | for (size_t i(1); i != size; ++i) | |
203 | if (!WordEndRange_[data[i]]) | |
204 | return false; | |
205 | } | |
206 | ||
207 | return true; | |
208 | } | |
209 | /* }}} */ | |
210 | ||
211 | double CYCastDouble(const char *value, size_t size) { | |
212 | char *end; | |
213 | double number(strtod(value, &end)); | |
214 | if (end != value + size) | |
215 | return NAN; | |
216 | return number; | |
217 | } | |
218 | ||
219 | double CYCastDouble(const char *value) { | |
220 | return CYCastDouble(value, strlen(value)); | |
221 | } | |
222 | ||
223 | _visible bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &needle) { | |
224 | return haystack.size >= needle.size && strncmp(haystack.data, needle.data, needle.size) == 0; | |
225 | } | |
226 | ||
227 | CYUTF8String CYPoolCode(CYPool &pool, std::istream &stream) { | |
228 | CYLocalPool local; | |
229 | CYDriver driver(local, stream); | |
230 | _assert(!driver.Parse()); | |
231 | _assert(driver.errors_.empty()); | |
232 | ||
233 | CYOptions options; | |
234 | CYContext context(options); | |
235 | driver.script_->Replace(context); | |
236 | ||
237 | std::stringbuf str; | |
238 | CYOutput out(str, options); | |
239 | out << *driver.script_; | |
240 | return $pool.strdup(str.str().c_str()); | |
241 | } | |
242 | ||
243 | CYPool &CYGetGlobalPool() { | |
244 | static CYPool pool; | |
245 | return pool; | |
246 | } | |
247 | ||
248 | _visible void CYThrow(const char *format, ...) { | |
249 | va_list args; | |
250 | va_start(args, format); | |
251 | throw CYPoolError(format, args); | |
252 | // XXX: does this matter? :( | |
253 | va_end(args); | |
254 | } | |
255 | ||
256 | const char *CYPoolError::PoolCString(CYPool &pool) const { | |
257 | return pool.strdup(message_); | |
258 | } | |
259 | ||
260 | CYPoolError::CYPoolError(const CYPoolError &rhs) : | |
261 | message_(pool_.strdup(rhs.message_)) | |
262 | { | |
263 | } | |
264 | ||
265 | CYPoolError::CYPoolError(const char *format, ...) { | |
266 | va_list args; | |
267 | va_start(args, format); | |
268 | // XXX: there might be a beter way to think about this | |
269 | message_ = pool_.vsprintf(64, format, args); | |
270 | va_end(args); | |
271 | } | |
272 | ||
273 | CYPoolError::CYPoolError(const char *format, va_list args) { | |
274 | // XXX: there might be a beter way to think about this | |
275 | message_ = pool_.vsprintf(64, format, args); | |
276 | } |