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