]> git.saurik.com Git - cycript.git/blame - Library.cpp
Fix quote when stringifying C/Objective-C strings.
[cycript.git] / Library.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c1d3e52e 2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
37954781
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
37954781 6/*
f95d2598
JF
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
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
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/>.
b3378a02 19**/
37954781
JF
20/* }}} */
21
37954781
JF
22#include "cycript.hpp"
23
37954781 24#include <iostream>
37954781
JF
25#include <set>
26#include <map>
27#include <iomanip>
28#include <sstream>
29#include <cmath>
30
20052ff7
JF
31#include <dlfcn.h>
32
33#include <sys/mman.h>
34
35#include "ConvertUTF.h"
8a392978 36#include "Driver.hpp"
37954781 37#include "Error.hpp"
2f51d6ab 38#include "Execute.hpp"
20052ff7 39#include "Pooling.hpp"
b12a9965 40#include "String.hpp"
20052ff7 41#include "Syntax.hpp"
59cd74c1 42
fa3c5be8
JF
43template <>
44::pthread_key_t CYLocal<CYPool>::key_ = Key_();
45
142cb9e3 46/* C Strings {{{ */
b799113b 47CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF16String utf16) {
59cd74c1
JF
48 // XXX: this is wrong
49 size_t size(utf16.size * 5);
142cb9e3 50 char *temp(new(pool) char[size]);
59cd74c1 51
142cb9e3
JF
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);
59cd74c1 55
142cb9e3
JF
56 *rhs = 0;
57 return CYUTF8String(temp, reinterpret_cast<char *>(rhs) - temp);
59cd74c1
JF
58}
59
b799113b 60CYUTF16String CYPoolUTF16String(CYPool &pool, CYUTF8String utf8) {
59cd74c1
JF
61 // XXX: this is wrong
62 size_t size(utf8.size * 5);
63 uint16_t *temp(new (pool) uint16_t[size]);
59cd74c1 64
142cb9e3
JF
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);
59cd74c1 68
142cb9e3
JF
69 *rhs = 0;
70 return CYUTF16String(temp, rhs - temp);
59cd74c1
JF
71}
72/* }}} */
37954781
JF
73/* Index Offsets {{{ */
74size_t CYGetIndex(const CYUTF8String &value) {
75 if (value.data[0] != '0') {
ba4fa42f
JF
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)
37954781 85 return 0;
ba4fa42f
JF
86 else
87 return _not(size_t);
37954781
JF
88}
89
ba4fa42f 90// XXX: this isn't actually right
37954781
JF
91bool 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/* }}} */
37954781 105/* JavaScript *ify {{{ */
18d654b3
JF
106void CYStringify(std::ostringstream &str, const char *data, size_t size, bool c) {
107 bool single;
108 if (c)
109 single = false;
110 else {
111 unsigned quot(0), apos(0);
112 for (const char *value(data), *end(data + size); value != end; ++value)
113 if (*value == '"')
114 ++quot;
115 else if (*value == '\'')
116 ++apos;
117
118 single = quot > apos;
119 }
37954781
JF
120
121 str << (single ? '\'' : '"');
122
123 for (const char *value(data), *end(data + size); value != end; ++value)
ee6c04ef 124 switch (uint8_t next = *value) {
37954781
JF
125 case '\\': str << "\\\\"; break;
126 case '\b': str << "\\b"; break;
127 case '\f': str << "\\f"; break;
128 case '\n': str << "\\n"; break;
129 case '\r': str << "\\r"; break;
130 case '\t': str << "\\t"; break;
131 case '\v': str << "\\v"; break;
132
133 case '"':
134 if (!single)
135 str << "\\\"";
136 else goto simple;
137 break;
138
139 case '\'':
140 if (single)
141 str << "\\'";
142 else goto simple;
143 break;
144
ee6c04ef
JF
145 case '\0':
146 if (value[1] >= '0' && value[1] <= '9')
147 str << "\\x00";
148 else
149 str << "\\0";
150 break;
151
37954781 152 default:
ee6c04ef 153 if (next >= 0x20 && next < 0x7f) simple:
37954781 154 str << *value;
ee6c04ef
JF
155 else {
156 unsigned levels(1);
157 if ((next & 0x80) != 0)
158 while ((next & 0x80 >> ++levels) != 0);
159
160 unsigned point(next & 0xff >> levels);
161 while (--levels != 0)
162 point = point << 6 | uint8_t(*++value) & 0x3f;
163
164 if (point < 0x100)
165 str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << point;
166 else if (point < 0x10000)
167 str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << point;
168 else {
169 point -= 0x10000;
170 str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xd800 | point >> 0x0a);
171 str << "\\u" << std::setbase(16) << std::setw(4) << std::setfill('0') << (0xdc00 | point & 0x3ff);
172 }
173 }
37954781
JF
174 }
175
176 str << (single ? '\'' : '"');
177}
178
179void CYNumerify(std::ostringstream &str, double value) {
5f6902c2
JF
180 if (std::isinf(value)) {
181 if (value < 0)
182 str << '-';
183 str << "Infinity";
184 return;
185 }
186
37954781
JF
187 char string[32];
188 // XXX: I want this to print 1e3 rather than 1000
189 sprintf(string, "%.17g", value);
190 str << string;
191}
192
193bool CYIsKey(CYUTF8String value) {
194 const char *data(value.data);
195 size_t size(value.size);
196
197 if (size == 0)
198 return false;
199
200 if (DigitRange_[data[0]]) {
201 size_t index(CYGetIndex(value));
202 if (index == _not(size_t))
203 return false;
204 } else {
205 if (!WordStartRange_[data[0]])
206 return false;
207 for (size_t i(1); i != size; ++i)
208 if (!WordEndRange_[data[i]])
209 return false;
210 }
211
212 return true;
213}
214/* }}} */
215
37954781
JF
216double CYCastDouble(const char *value, size_t size) {
217 char *end;
218 double number(strtod(value, &end));
219 if (end != value + size)
220 return NAN;
221 return number;
222}
223
224double CYCastDouble(const char *value) {
225 return CYCastDouble(value, strlen(value));
226}
227
e66ced89
JF
228_visible bool CYStartsWith(const CYUTF8String &haystack, const CYUTF8String &needle) {
229 return haystack.size >= needle.size && strncmp(haystack.data, needle.data, needle.size) == 0;
230}
231
c3d9dbc7 232CYUTF8String CYPoolCode(CYPool &pool, std::streambuf &stream) {
2eb8215d 233 CYLocalPool local;
2c1d569a 234 CYDriver driver(local, stream);
8a392978 235 _assert(!driver.Parse());
57930129 236 _assert(driver.errors_.empty());
59cd74c1 237
029bc65b 238 CYOptions options;
2eb8215d 239 CYContext context(options);
a7d8b413 240 driver.script_->Replace(context);
57930129 241
efd689d8 242 std::stringbuf str;
029bc65b 243 CYOutput out(str, options);
a7d8b413 244 out << *driver.script_;
a2b8d7ad 245 return $pool.strdup(str.str().c_str());
57930129 246}
59cd74c1 247
b799113b 248CYPool &CYGetGlobalPool() {
b799113b
JF
249 static CYPool pool;
250 return pool;
37954781
JF
251}
252
d9c91152 253_visible void CYThrow(const char *format, ...) {
37954781 254 va_list args;
b64ab4da 255 va_start(args, format);
37954781
JF
256 throw CYPoolError(format, args);
257 // XXX: does this matter? :(
b64ab4da 258 va_end(args);
37954781
JF
259}
260
b799113b
JF
261const char *CYPoolError::PoolCString(CYPool &pool) const {
262 return pool.strdup(message_);
37954781
JF
263}
264
0cbeddf8
JF
265CYPoolError::CYPoolError(const CYPoolError &rhs) :
266 message_(pool_.strdup(rhs.message_))
267{
268}
269
37954781
JF
270CYPoolError::CYPoolError(const char *format, ...) {
271 va_list args;
b64ab4da 272 va_start(args, format);
0cbeddf8
JF
273 // XXX: there might be a beter way to think about this
274 message_ = pool_.vsprintf(64, format, args);
b64ab4da 275 va_end(args);
37954781
JF
276}
277
278CYPoolError::CYPoolError(const char *format, va_list args) {
0cbeddf8
JF
279 // XXX: there might be a beter way to think about this
280 message_ = pool_.vsprintf(64, format, args);
37954781 281}