minjs 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/minjs/compressor.rb +87 -22
- data/lib/minjs/ctype.rb +5 -0
- data/lib/minjs/ecma262/base.rb +10 -0
- data/lib/minjs/ecma262/exp.rb +194 -27
- data/lib/minjs/ecma262/lit.rb +59 -108
- data/lib/minjs/ecma262/st.rb +93 -22
- data/lib/minjs/exceptions.rb +10 -2
- data/lib/minjs/expression.rb +15 -17
- data/lib/minjs/func.rb +11 -4
- data/lib/minjs/lex.rb +85 -36
- data/lib/minjs/program.rb +0 -4
- data/lib/minjs/statement.rb +11 -17
- data/lib/minjs/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dff56ddbe102f06b6d07d67ca3cbb7076dec2bc2
|
4
|
+
data.tar.gz: 2bf1104e9aeda92155019cd2784e4313614fc958
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 546ff6b86865bc79856e034b4bba733093170ccc4f514de33f390b597bb9cd0cf6608e2c00c8619ccfe2cd3e3ab3977df50fb3f022def02982b06a99f4dcee80
|
7
|
+
data.tar.gz: 4ce28cc2d02368b99335fd1fe5bd69a7d03ccd72b4399fb775782e3186bf42d839df584f30468b83e6be4b9040a3c6054722ff266b79a96a177fee53c25ae9e4
|
data/lib/minjs/compressor.rb
CHANGED
@@ -38,15 +38,17 @@ module Minjs
|
|
38
38
|
parse(data)
|
39
39
|
|
40
40
|
reorder_function_decl
|
41
|
-
return_after
|
42
41
|
simple_replacement
|
43
42
|
reorder_var
|
44
43
|
assignment_after_var
|
45
44
|
grouping_statement
|
46
45
|
block_to_exp
|
47
|
-
if_to_cond
|
46
|
+
if_to_cond
|
48
47
|
compress_var
|
49
48
|
reduce_exp
|
49
|
+
remove_paren
|
50
|
+
#feature
|
51
|
+
#return_to_exp
|
50
52
|
@heading_comments.reverse.each do |c|
|
51
53
|
@prog.source_elements.unshift(c)
|
52
54
|
end
|
@@ -141,7 +143,7 @@ module Minjs
|
|
141
143
|
|
142
144
|
def reorder_function_decl
|
143
145
|
self.traverse {|st, parent|
|
144
|
-
if st.kind_of? ECMA262::StFunc and parent.kind_of? ECMA262::Prog and st.decl
|
146
|
+
if st.kind_of? ECMA262::StFunc and parent.kind_of? ECMA262::Prog and st.decl?
|
145
147
|
if parent.index(st)
|
146
148
|
parent.remove(st)
|
147
149
|
parent.source_elements.unshift(st)
|
@@ -193,6 +195,28 @@ module Minjs
|
|
193
195
|
remove_block_in_block
|
194
196
|
end
|
195
197
|
|
198
|
+
#feature
|
199
|
+
def remove_paren
|
200
|
+
self.traverse {|st, parent|
|
201
|
+
if st.kind_of? ECMA262::ExpParen
|
202
|
+
#
|
203
|
+
# ECMA262 say:
|
204
|
+
# expression statement cannot start with "function"
|
205
|
+
#
|
206
|
+
if parent.priority(st) > st.val.priority(nil)
|
207
|
+
if st.val.to_js.match(/^function/)
|
208
|
+
;
|
209
|
+
elsif st.val.to_js.match(/^{/)
|
210
|
+
;
|
211
|
+
else
|
212
|
+
parent.replace(st, st.val)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
}
|
217
|
+
self
|
218
|
+
end
|
219
|
+
|
196
220
|
def remove_block_in_block
|
197
221
|
while true
|
198
222
|
_retry = false
|
@@ -227,13 +251,47 @@ module Minjs
|
|
227
251
|
}
|
228
252
|
end
|
229
253
|
|
254
|
+
def block_to_statement
|
255
|
+
self.traverse {|st, parent|
|
256
|
+
if st.kind_of? ECMA262::StBlock and st.to_statement?
|
257
|
+
if parent.kind_of? ECMA262::StTry
|
258
|
+
else
|
259
|
+
parent.replace(st, st.to_statement)
|
260
|
+
end
|
261
|
+
end
|
262
|
+
}
|
263
|
+
end
|
264
|
+
|
230
265
|
def if_to_cond
|
231
|
-
#traverse all statemtns and expression
|
232
266
|
self.traverse {|st, parent|
|
233
267
|
if st.kind_of? ECMA262::StIf and st.to_exp?
|
234
268
|
if t = ECMA262::StExp.new(st.to_exp({}))
|
235
269
|
parent.replace(st, t)
|
236
270
|
end
|
271
|
+
# feature...
|
272
|
+
=begin
|
273
|
+
elsif st.kind_of? ECMA262::StIf and st.to_return?
|
274
|
+
#
|
275
|
+
# if(...)
|
276
|
+
# return a;
|
277
|
+
# return b;
|
278
|
+
#
|
279
|
+
# => if(...)return a;else return b;
|
280
|
+
#
|
281
|
+
if parent.kind_of? ECMA262::StList and st.else_st.nil? and (nxt = parent[parent.index(st) + 1]).kind_of? ECMA262::StReturn
|
282
|
+
st.replace(st.else_st, nxt)
|
283
|
+
parent.replace(nxt, ECMA262::StEmpty.new())
|
284
|
+
parent.replace(st, st.to_return)
|
285
|
+
#
|
286
|
+
# if(...)
|
287
|
+
# return a;
|
288
|
+
# else
|
289
|
+
# return b;
|
290
|
+
#
|
291
|
+
else
|
292
|
+
parent.replace(st, st.to_return)
|
293
|
+
end
|
294
|
+
=end
|
237
295
|
end
|
238
296
|
}
|
239
297
|
end
|
@@ -311,6 +369,7 @@ module Minjs
|
|
311
369
|
end
|
312
370
|
}
|
313
371
|
end
|
372
|
+
|
314
373
|
def reduce_exp
|
315
374
|
self.traverse {|st, parent|
|
316
375
|
if st.kind_of? ECMA262::Exp
|
@@ -325,9 +384,9 @@ module Minjs
|
|
325
384
|
#false => !1
|
326
385
|
if st.kind_of? ECMA262::Boolean
|
327
386
|
if st.true?
|
328
|
-
parent.replace(st, ECMA262::ExpLogicalNot.new(ECMA262::ECMA262Numeric.new(
|
387
|
+
parent.replace(st, ECMA262::ExpParen.new(ECMA262::ExpLogicalNot.new(ECMA262::ECMA262Numeric.new(0))))
|
329
388
|
else
|
330
|
-
parent.replace(st, ECMA262::ExpLogicalNot.new(ECMA262::ECMA262Numeric.new(
|
389
|
+
parent.replace(st, ECMA262::ExpParen.new(ECMA262::ExpLogicalNot.new(ECMA262::ECMA262Numeric.new(1))))
|
331
390
|
end
|
332
391
|
#if(true){<then>}else{<else>} => then
|
333
392
|
elsif st.kind_of? ECMA262::StIf
|
@@ -344,29 +403,35 @@ module Minjs
|
|
344
403
|
}
|
345
404
|
end
|
346
405
|
|
347
|
-
def
|
406
|
+
def return_to_exp
|
348
407
|
self.traverse {|st, parent|
|
349
408
|
if st.kind_of? ECMA262::StReturn
|
350
|
-
if parent.kind_of? ECMA262::
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
idx += 1
|
409
|
+
if parent.kind_of? ECMA262::Prog
|
410
|
+
parent.remove_empty_statement
|
411
|
+
#
|
412
|
+
# a=1;return b; => return a=1,b;
|
413
|
+
#
|
414
|
+
# check statement:
|
415
|
+
# last one is return and its previous is expression or not
|
416
|
+
if parent.source_elements[-1] == st and (prev=parent.source_elements[-2]).class == ECMA262::StExp
|
417
|
+
if st.exp
|
418
|
+
st.replace(st.exp, ECMA262::ExpComma.new(prev.exp, st.exp))
|
419
|
+
parent.replace(prev, ECMA262::StEmpty.new())
|
420
|
+
end
|
363
421
|
end
|
364
|
-
|
365
|
-
|
422
|
+
elsif parent.kind_of? ECMA262::StList
|
423
|
+
parent.remove_empty_statement
|
424
|
+
if parent.statement_list[-1] == st and (prev=parent.statement_list[-2]).class == ECMA262::StExp
|
425
|
+
if st.exp
|
426
|
+
st.replace(st.exp, ECMA262::ExpComma.new(prev.exp, st.exp))
|
427
|
+
parent.replace(prev, ECMA262::StEmpty.new())
|
428
|
+
end
|
366
429
|
end
|
367
430
|
end
|
368
431
|
end
|
369
432
|
}
|
433
|
+
block_to_statement
|
434
|
+
if_to_cond
|
370
435
|
end
|
371
436
|
#
|
372
437
|
# var a; a=1
|
data/lib/minjs/ctype.rb
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
module Minjs
|
2
2
|
module Ctype
|
3
|
+
def hex_number?(code)
|
4
|
+
code >= 0x30 && code <= 0x39 or
|
5
|
+
code >= 0x41 && code <= 0x46 or
|
6
|
+
code >= 0x61 && code <= 0x66
|
7
|
+
end
|
3
8
|
def white_space?(code)
|
4
9
|
code == 0x20 || code == 0x9 || code == 0xb || code == 0xc || code == 0xa0 || code == 0xfeff
|
5
10
|
end
|
data/lib/minjs/ecma262/base.rb
CHANGED
@@ -43,6 +43,10 @@ module Minjs
|
|
43
43
|
end
|
44
44
|
j.join("")
|
45
45
|
end
|
46
|
+
|
47
|
+
def replace(from, to)
|
48
|
+
puts "warning: #{self.class}: replace not implement"
|
49
|
+
end
|
46
50
|
end
|
47
51
|
|
48
52
|
class Prog < Base
|
@@ -110,6 +114,12 @@ module Minjs
|
|
110
114
|
@source_elements.delete(st)
|
111
115
|
end
|
112
116
|
|
117
|
+
def remove_empty_statement
|
118
|
+
@source_elements.reject!{|x|
|
119
|
+
x.class == StEmpty
|
120
|
+
}
|
121
|
+
end
|
122
|
+
|
113
123
|
def each(&block)
|
114
124
|
@source_elements.each(&block)
|
115
125
|
end
|