]> git.saurik.com Git - cycript.git/blame - Complete.cpp
Work around iOS "<redacted>" for Message's toCYON.
[cycript.git] / Complete.cpp
CommitLineData
7341eedb
JF
1/* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
d9c91152
JF
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
e257405a
JF
22#include <typeinfo>
23
d9c91152
JF
24#include "cycript.hpp"
25
26#include "Driver.hpp"
d9c91152
JF
27#include "Replace.hpp"
28#include "String.hpp"
29
2c1d569a 30static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) {
d9c91152
JF
31 std::stringstream stream;
32 stream << '(' << code << ')';
c3d9dbc7 33 CYDriver driver(pool, *stream.rdbuf());
8a392978 34 if (driver.Parse() || !driver.errors_.empty())
d9c91152
JF
35 return NULL;
36
37 CYOptions options;
38 CYContext context(options);
39
a7d8b413 40 CYStatement *statement(driver.script_->code_);
d9c91152
JF
41 _assert(statement != NULL);
42 _assert(statement->next_ == NULL);
43
a7d8b413 44 CYExpress *express(dynamic_cast<CYExpress *>(driver.script_->code_));
d9c91152
JF
45 _assert(express != NULL);
46
47 CYParenthetical *parenthetical(dynamic_cast<CYParenthetical *>(express->expression_));
48 _assert(parenthetical != NULL);
49
50 return parenthetical->expression_;
51}
52
53_visible char **CYComplete(const char *word, const std::string &line, CYUTF8String (*run)(CYPool &pool, const std::string &)) {
54 CYLocalPool pool;
55
c3d9dbc7 56 std::stringbuf stream(line);
2c1d569a 57 CYDriver driver(pool, stream);
d9c91152
JF
58
59 driver.auto_ = true;
60
8a392978 61 if (driver.Parse() || !driver.errors_.empty())
d9c91152
JF
62 return NULL;
63
64 if (driver.mode_ == CYDriver::AutoNone)
65 return NULL;
66
67 CYExpression *expression;
68
69 CYOptions options;
70 CYContext context(options);
71
72 std::ostringstream prefix;
73
74 switch (driver.mode_) {
75 case CYDriver::AutoPrimary:
76 expression = $ CYThis();
77 break;
78
79 case CYDriver::AutoDirect:
80 expression = driver.context_;
81 break;
82
83 case CYDriver::AutoIndirect:
84 expression = $ CYIndirect(driver.context_);
85 break;
86
87 case CYDriver::AutoMessage: {
88 CYDriver::Context &thing(driver.contexts_.back());
3d2d95a0 89 expression = $M($C1($V("object_getClass"), thing.context_), $S("prototype"));
d9c91152
JF
90 for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part)
91 prefix << (*part)->word_ << ':';
92 } break;
93
2fad14e5
JF
94 case CYDriver::AutoResolve:
95 expression = $M(driver.context_, $S("$cyr"));
96 break;
97
84096608
JF
98 case CYDriver::AutoStruct:
99 expression = $ CYThis();
100 prefix << "$cys";
101 break;
102
103 case CYDriver::AutoEnum:
104 expression = $ CYThis();
105 prefix << "$cye";
106 break;
107
d9c91152
JF
108 default:
109 _assert(false);
110 }
111
112 std::string begin(prefix.str());
113
3d2d95a0
JF
114 CYBoolean *message;
115 if (driver.mode_ == CYDriver::AutoMessage)
116 message = $ CYTrue();
117 else
118 message = $ CYFalse();
119
120 driver.script_ = $ CYScript($ CYExpress($C4(ParseExpression(pool,
e257405a
JF
121 " function(value, prefix, word, message) {\n"
122 " var object = value;\n"
d9c91152
JF
123 " var names = [];\n"
124 " var before = prefix.length;\n"
125 " prefix += word;\n"
126 " var entire = prefix.length;\n"
24d9135d
JF
127 " if (false) {\n"
128 " for (var name in object)\n"
129 " if (name.substring(0, entire) == prefix)\n"
84096608 130 " names.push(name);\n"
24d9135d 131 " } else do {\n"
e257405a
JF
132 " if (object.hasOwnProperty(\"cy$complete\"))\n"
133 " names = names.concat(object.cy$complete.call(value, prefix, message));\n"
24d9135d
JF
134 " try {\n"
135 " var local = Object.getOwnPropertyNames(object);\n"
136 " } catch (e) {\n"
137 " continue;\n"
138 " }\n"
139 " for (var name of local)\n"
140 " if (name.substring(0, entire) == prefix)\n"
84096608 141 " names.push(name);\n"
24d9135d 142 " } while (object = typeof object === 'object' ? Object.getPrototypeOf(object) : object.__proto__);\n"
d9c91152
JF
143 " return names;\n"
144 " }\n"
3d2d95a0 145 ), expression, $S(begin.c_str()), $S(word), message)));
d9c91152 146
a7d8b413 147 driver.script_->Replace(context);
d9c91152
JF
148
149 std::stringbuf str;
150 CYOutput out(str, options);
a7d8b413 151 out << *driver.script_;
d9c91152
JF
152
153 std::string code(str.str());
154 CYUTF8String json(run(pool, code));
155 // XXX: if this fails we should not try to parse it
156
2c1d569a 157 CYExpression *result(ParseExpression(pool, json));
d9c91152
JF
158 if (result == NULL)
159 return NULL;
160
161 CYArray *array(dynamic_cast<CYArray *>(result->Primitive(context)));
162 if (array == NULL)
163 return NULL;
164
165 // XXX: use an std::set?
5883fc70 166 typedef std::vector<CYUTF8String> Completions;
d9c91152
JF
167 Completions completions;
168
169 std::string common;
170 bool rest(false);
171
fc8fc33d
JF
172 for (CYElement *element(array->elements_); element != NULL; ) {
173 CYElementValue *value(dynamic_cast<CYElementValue *>(element));
174 _assert(value != NULL);
175 element = value->next_;
176
e257405a 177 _assert(value->value_ != NULL);
37dadc21 178 CYString *string(value->value_->String(context));
e257405a
JF
179 if (string == NULL)
180 CYThrow("string was actually %s", typeid(*value->value_).name());
d9c91152 181
5883fc70 182 CYUTF8String completion;
d9c91152 183 if (string->size_ != 0)
5883fc70 184 completion = {string->value_, string->size_};
d9c91152
JF
185 else if (driver.mode_ == CYDriver::AutoMessage)
186 completion = "]";
187 else
188 continue;
189
84096608
JF
190 completion.data += begin.size();
191 completion.size -= begin.size();
192
5883fc70
JF
193 if (CYStartsWith(completion, "$cy"))
194 continue;
d9c91152
JF
195 completions.push_back(completion);
196
197 if (!rest) {
198 common = completion;
199 rest = true;
200 } else {
5883fc70 201 size_t limit(completion.size), size(common.size());
d9c91152
JF
202 if (size > limit)
203 common = common.substr(0, limit);
204 else
205 limit = size;
206 for (limit = 0; limit != size; ++limit)
5883fc70 207 if (common[limit] != completion.data[limit])
d9c91152
JF
208 break;
209 if (limit != size)
210 common = common.substr(0, limit);
211 }
212 }
213
214 size_t count(completions.size());
215 if (count == 0)
216 return NULL;
217
218 size_t colon(common.find(':'));
219 if (colon != std::string::npos)
220 common = common.substr(0, colon + 1);
221 if (completions.size() == 1)
222 common += ' ';
223
224 char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2))));
225
226 results[0] = strdup(common.c_str());
227 size_t index(0);
228 for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i)
5883fc70 229 results[++index] = strdup(i->data);
d9c91152
JF
230 results[count + 1] = NULL;
231
232 return results;
233}