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