Skip to content

Commit 20d214a

Browse files
author
Chris K
committed
Added get_function and is_minified in NewScript.
1 parent b270dff commit 20d214a

File tree

1 file changed

+123
-1
lines changed

1 file changed

+123
-1
lines changed

src/ecma-debugger/newscript.js

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ window.cls.NewScript.COMMENT_STATE = 4;
3838

3939
window.cls.NewScriptPrototype = function()
4040
{
41+
var WHITESPACE = cls.SimpleJSParser.WHITESPACE;
42+
var LINETERMINATOR = cls.SimpleJSParser.LINETERMINATOR;
43+
var IDENTIFIER = cls.SimpleJSParser.IDENTIFIER;
44+
var NUMBER = cls.SimpleJSParser.NUMBER;
45+
var STRING = cls.SimpleJSParser.STRING;
46+
var PUNCTUATOR = cls.SimpleJSParser.PUNCTUATOR;
47+
var DIV_PUNCTUATOR = cls.SimpleJSParser.DIV_PUNCTUATOR;
48+
var REG_EXP = cls.SimpleJSParser.REG_EXP;
49+
var COMMENT = cls.SimpleJSParser.COMMENT;
50+
var TYPE = 0;
51+
var VALUE = 1;
52+
var _tokenizer = new cls.SimpleJSParser();
53+
4154
/**
4255
* Searches the actual data.
4356
* Updates the script object with the following properties for all matches:
@@ -143,7 +156,116 @@ window.cls.NewScriptPrototype = function()
143156
this.line_offsets_length = [];
144157
this.match_cursor = -1;
145158
this.match_length = 0;
146-
}
159+
};
160+
161+
this.__defineGetter__("is_minified", function()
162+
{
163+
if (!this._is_mified)
164+
{
165+
var MAX_SLICE = 5000;
166+
var LIMIT = 11;
167+
var re = /\s+/g;
168+
var ws = 0;
169+
var m = null;
170+
var src = this.script_data.slice(0, MAX_SLICE);
171+
while (m = re.exec(src))
172+
ws += m[0].length;
173+
174+
this._is_mified = (100 * ws / src.length) < LIMIT;
175+
}
176+
return this._is_mified;
177+
});
178+
179+
this.__defineSetter__("is_minified", function() {});
180+
181+
this.get_function = function(line_number)
182+
{
183+
if (this.is_minified)
184+
return null;
185+
186+
var start_line = -1;
187+
var end_line = -1;
188+
var index = -1;
189+
var tokens = null;
190+
var op_bracket_count = 0;
191+
while (start_line == -1 && line_number > -1)
192+
{
193+
tokens = this._get_tokens_of_line(line_number);
194+
for (var i = 0, token; token = tokens[i]; i++)
195+
{
196+
if (token[TYPE] == IDENTIFIER && token[VALUE] == "function")
197+
{
198+
start_line = line_number;
199+
index = i;
200+
}
201+
}
202+
if (index == -1)
203+
line_number--;
204+
}
205+
while (!op_bracket_count && line_number <= this.line_arr.length)
206+
{
207+
for (var i = index, token; token = tokens[i]; i++)
208+
{
209+
if (token[TYPE] == PUNCTUATOR && token[VALUE] == "{")
210+
{
211+
op_bracket_count++;
212+
index = i + 1;
213+
break;
214+
}
215+
}
216+
if (!token)
217+
{
218+
index = 0;
219+
tokens = this._get_tokens_of_line(++line_number);
220+
}
221+
}
222+
while (op_bracket_count && end_line == -1 && line_number <= this.line_arr.length)
223+
{
224+
for (var i = index, token; token = tokens[i]; i++)
225+
{
226+
if (token[TYPE] == PUNCTUATOR)
227+
{
228+
if (token[VALUE] == "{")
229+
op_bracket_count++;
230+
else if (token[VALUE] == "}")
231+
{
232+
op_bracket_count--;
233+
if (!op_bracket_count)
234+
{
235+
end_line = line_number;
236+
break;
237+
}
238+
}
239+
}
240+
}
241+
if (end_line == -1)
242+
{
243+
index = 0;
244+
tokens = this._get_tokens_of_line(++line_number);
245+
}
246+
}
247+
return start_line > -1 && end_line > -1
248+
? {start_line: start_line, end_line: end_line}
249+
: null;
250+
};
251+
252+
this._get_tokens_of_line = function(line_number)
253+
{
254+
if (!this.line_arr)
255+
this.set_line_states();
256+
257+
var tokens = [];
258+
var line = this.get_line(line_number);
259+
var start_state = this.state_arr[line_number - 1];
260+
if (line)
261+
{
262+
_tokenizer.tokenize(line, function(token_type, token)
263+
{
264+
tokens.push([token_type, token]);
265+
}, false, start_state);
266+
}
267+
return tokens;
268+
};
147269

148270
this.set_line_states = function()
149271
{

0 commit comments

Comments
 (0)