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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8242f875c695e92b930cd716c7c8979fe8164607
4
- data.tar.gz: 9ca6b42d5d6db0894c65fd64ab2f89cde0e16e6e
3
+ metadata.gz: dff56ddbe102f06b6d07d67ca3cbb7076dec2bc2
4
+ data.tar.gz: 2bf1104e9aeda92155019cd2784e4313614fc958
5
5
  SHA512:
6
- metadata.gz: b9948715e0ac544058ea8437b1bdaeab2a16709d9aaf3b784d42e7e0b730ba07bc35540533f6c299f0320df079cf77b6ebf5aadcb2d2680e61184bc5ab6ac5bb
7
- data.tar.gz: 24400fa76290a15563d3c4384ed9697761f889e69313aa445faacc399b1774ea0011e2ded343727f442385c740c40928c38f657a6d2d255c23b12b4ddf8980d4
6
+ metadata.gz: 546ff6b86865bc79856e034b4bba733093170ccc4f514de33f390b597bb9cd0cf6608e2c00c8619ccfe2cd3e3ab3977df50fb3f022def02982b06a99f4dcee80
7
+ data.tar.gz: 4ce28cc2d02368b99335fd1fe5bd69a7d03ccd72b4399fb775782e3186bf42d839df584f30468b83e6be4b9040a3c6054722ff266b79a96a177fee53c25ae9e4
@@ -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 #buggy
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('0', 0)))
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('1', 1)))
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 return_after
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::StList
351
- idx = parent.index(st)
352
- idx += 1
353
- while parent.statement_list[idx]
354
- parent.statement_list[idx] = ECMA262::StEmpty.new;
355
- idx += 1
356
- end
357
- elsif parent.kind_of? ECMA262::Prog
358
- idx = parent.index(st)
359
- idx += 1
360
- while parent.source_elements[idx]
361
- parent.source_elements[idx] = ECMA262::StEmpty.new;
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
- if st.exp.nil?
365
- parent.replace(st, ECMA262::StEmpty.new)
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
@@ -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