minjs 0.1.5 → 0.1.7

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.
@@ -15,7 +15,7 @@ module Minjs
15
15
  end
16
16
 
17
17
  def priority
18
- 10
18
+ PRIORITY_PRIMARY
19
19
  end
20
20
  end
21
21
 
@@ -98,6 +98,14 @@ module Minjs
98
98
  def self.get
99
99
  @@instance
100
100
  end
101
+
102
+ def to_ecma262_boolean
103
+ false
104
+ end
105
+
106
+ def ecma262_typeof
107
+ :boolean
108
+ end
101
109
  end
102
110
 
103
111
  class Boolean < Literal
@@ -130,7 +138,7 @@ module Minjs
130
138
  @@true = self.new(:true)
131
139
  @@false = self.new(:false)
132
140
  def self.get(val)
133
- if val.to_sym == :true
141
+ if val.to_sym == :true || val == true
134
142
  @@true
135
143
  else
136
144
  @@false
@@ -183,6 +191,18 @@ module Minjs
183
191
  end
184
192
  t << "\""
185
193
  end
194
+
195
+ def to_ecma262_boolean
196
+ if @val.length == 0
197
+ false
198
+ else
199
+ true
200
+ end
201
+ end
202
+
203
+ def ecma262_typeof
204
+ :string
205
+ end
186
206
  end
187
207
 
188
208
  class ECMA262Numeric < Literal
@@ -307,6 +327,27 @@ module Minjs
307
327
  end
308
328
  end
309
329
  end
330
+
331
+ def to_ecma262_boolean
332
+ if @val == :nan or to_ecma262_string == "0"
333
+ false
334
+ else
335
+ true
336
+ end
337
+ end
338
+
339
+ def ecma262_typeof
340
+ :number
341
+ end
342
+
343
+ def ecma262_eval(type)
344
+ case type
345
+ when :boolean
346
+ to_ecma262_boolean
347
+ else
348
+ nil
349
+ end
350
+ end
310
351
  end
311
352
  NUMERIC_NAN = ECMA262Numeric.new(:nan)
312
353
 
@@ -350,6 +391,9 @@ module Minjs
350
391
  def to_js(options = {})
351
392
  "[" + @val.collect{|x| x.to_s}.join(",") + "]"
352
393
  end
394
+ def to_ecma262_boolean
395
+ true
396
+ end
353
397
  end
354
398
 
355
399
  class ECMA262Object < Literal
@@ -408,6 +452,18 @@ module Minjs
408
452
  end
409
453
  }.join(",") + "}"
410
454
  end
455
+ def to_ecma262_boolean
456
+ true
457
+ end
458
+ # def ecma262_eval(type)
459
+ #
460
+ # case type
461
+ # when :boolean
462
+ # to_ecma262_boolean
463
+ # else
464
+ # nil
465
+ # end
466
+ # end
411
467
  end
412
468
 
413
469
  class SingleLineComment < Literal
@@ -58,24 +58,7 @@ module Minjs
58
58
  t = @statement_list.statement_list.select{|s|
59
59
  s.class != StEmpty
60
60
  }
61
- return false if t.length != 1
62
- #
63
- # if(a){ //<= this block must not be removed
64
- # while(true)
65
- # if(b){
66
- # ;
67
- # }
68
- # }
69
- # else{
70
- # ;
71
- # }
72
- #
73
- last_st = t[0].last_statement[-2]
74
- if last_st.kind_of?(StIf) and last_st.else_st.nil?
75
- return false
76
- else
77
- return true
78
- end
61
+ t.length == 1
79
62
  end
80
63
 
81
64
  def to_statement
@@ -167,11 +150,21 @@ module Minjs
167
150
 
168
151
  def remove_paren
169
152
  @vars.each do |x|
170
- if x[1] and x[1].kind_of? ExpParen and x[1].val.priority <= 130
153
+ if x[1] and x[1].kind_of? ExpParen and x[1].val.priority <= PRIORITY_ASSIGNMENT
171
154
  x[1] = x[1].val
172
155
  end
173
156
  end
174
157
  end
158
+
159
+ def add_paren
160
+ @vars.each do |x|
161
+ if x[1] and x[1].priority > PRIORITY_ASSIGNMENT
162
+ x[1] = ExpParen.new(x[1])
163
+ end
164
+ end
165
+ self
166
+ end
167
+
175
168
  def last_statement
176
169
  [self]
177
170
  end
@@ -238,6 +231,11 @@ module Minjs
238
231
  @exp = @exp.val if @exp.remove_paren?
239
232
  end
240
233
  end
234
+
235
+ def add_paren
236
+ self
237
+ end
238
+
241
239
  def last_statement
242
240
  [self]
243
241
  end
@@ -258,7 +256,9 @@ module Minjs
258
256
  end
259
257
 
260
258
  def replace(from, to)
261
- if from == @then_st
259
+ if from == @cond
260
+ @cond = to
261
+ elsif from == @then_st
262
262
  @then_st = to
263
263
  elsif from == @else_st
264
264
  @else_st = to
@@ -313,26 +313,22 @@ module Minjs
313
313
 
314
314
  def to_exp(options = {})
315
315
  return nil if to_exp? == false
316
- if @else_st
316
+ if !@else_st
317
317
  then_exp = @then_st.to_exp(options)
318
- else_exp = @else_st.to_exp(options)
318
+ if @cond.kind_of? ExpLogicalNot
319
+ return ExpParen.new(ExpLogicalOr.new(ExpParen.new(@cond.val), ExpParen.new(then_exp)))
320
+ else
321
+ return ExpParen.new(ExpLogicalAnd.new(ExpParen.new(@cond), ExpParen.new(then_exp)))
322
+ end
319
323
  else
320
- then_exp = @then_st.to_exp(options)
321
- return ExpLogicalAnd.new(ExpParen.new(@cond), ExpParen.new(then_exp))
322
- end
323
- if then_exp.kind_of? ExpComma
324
- then_exp = ExpParen.new(then_exp)
325
- end
326
- if else_exp.kind_of? ExpComma
327
- else_exp = ExpParen.new(else_exp)
324
+ then_exp = ExpParen.new(@then_st.to_exp(options))
325
+ else_exp = ExpParen.new(@else_st.to_exp(options))
328
326
  end
329
327
 
330
- if @cond.kind_of? ExpComma
331
- ExpCond.new(ExpParen.new(@cond), then_exp, else_exp)
332
- elsif @cond.kind_of? ExpAssign
333
- ExpCond.new(ExpParen.new(@cond), then_exp, else_exp)
328
+ if @cond.kind_of? ExpLogicalNot
329
+ ExpCond.new(ExpParen.new(@cond.val), else_exp, then_exp)
334
330
  else
335
- ExpCond.new(@cond, then_exp, else_exp)
331
+ ExpCond.new(ExpParen.new(@cond), then_exp, else_exp)
336
332
  end
337
333
  end
338
334
 
@@ -340,6 +336,11 @@ module Minjs
340
336
  if @cond.kind_of? ExpParen
341
337
  @cond = @cond.val
342
338
  end
339
+ self
340
+ end
341
+
342
+ def add_paren
343
+ self
343
344
  end
344
345
 
345
346
  def last_statement
@@ -354,6 +355,8 @@ module Minjs
354
355
 
355
356
  #12.6
356
357
  class StWhile < St
358
+ attr_reader :exp, :statement
359
+
357
360
  def initialize(exp, statement)
358
361
  @exp, @statement = exp, statement
359
362
  end
@@ -388,6 +391,11 @@ module Minjs
388
391
  if @exp.kind_of? ExpParen
389
392
  @exp = @exp.val
390
393
  end
394
+ self
395
+ end
396
+
397
+ def add_paren
398
+ self
391
399
  end
392
400
 
393
401
  def last_statement
@@ -426,11 +434,18 @@ module Minjs
426
434
 
427
435
  concat options, :do, statement, :while, "(", @exp, ")", ";"
428
436
  end
437
+
429
438
  def remove_paren
430
439
  if @exp.kind_of? ExpParen
431
440
  @exp = @exp.val
432
441
  end
442
+ self
443
+ end
444
+
445
+ def add_paren
446
+ self
433
447
  end
448
+
434
449
  def last_statement
435
450
  list = [self]
436
451
  list.concat @statement.last_statement
@@ -441,6 +456,8 @@ module Minjs
441
456
  # 12.6.3 the for statement
442
457
  #
443
458
  class StFor < St
459
+ attr_reader :exp1, :exp2, :exp3, :statement
460
+
444
461
  def initialize(exp1, exp2, exp3, statement)
445
462
  @exp1 = exp1
446
463
  @exp2 = exp2
@@ -456,15 +473,21 @@ module Minjs
456
473
  end
457
474
 
458
475
  def replace(from, to)
459
- if from == @statement
476
+ if from == @exp1
477
+ @exp1 = to
478
+ elsif from == @exp2
479
+ @exp2 = to
480
+ elsif from == @exp3
481
+ @exp3 = to
482
+ elsif from == @statement
460
483
  @statement = to
461
484
  end
462
485
  end
463
486
 
464
487
  def traverse(parent, &block)
465
- @exp1.traverse(self, &block)
466
- @exp2.traverse(self, &block)
467
- @exp3.traverse(self, &block)
488
+ @exp1.traverse(self, &block) if @exp1
489
+ @exp2.traverse(self, &block) if @exp2
490
+ @exp3.traverse(self, &block) if @exp3
468
491
  @statement.traverse(self, &block)
469
492
  yield self, parent
470
493
  end
@@ -489,7 +512,13 @@ module Minjs
489
512
  if @exp3.kind_of? ExpParen
490
513
  @exp3 = @exp3.val
491
514
  end
515
+ self
492
516
  end
517
+
518
+ def add_paren
519
+ self
520
+ end
521
+
493
522
  def last_statement
494
523
  list = [self]
495
524
  list.concat @statement.last_statement
@@ -537,8 +566,8 @@ module Minjs
537
566
  x[1].traverse(self, &block)
538
567
  end
539
568
  end
540
- @exp2.traverse(self, &block)
541
- @exp3.traverse(self, &block)
569
+ @exp2.traverse(self, &block) if @exp2
570
+ @exp3.traverse(self, &block) if @exp3
542
571
  @statement.traverse(self, &block)
543
572
  yield self, parent
544
573
  end
@@ -593,7 +622,13 @@ module Minjs
593
622
  if @exp3.kind_of? ExpParen
594
623
  @exp3 = @exp3.val
595
624
  end
625
+ self
596
626
  end
627
+
628
+ def add_paren
629
+ self
630
+ end
631
+
597
632
  def last_statement
598
633
  list = [self]
599
634
  list.concat @statement.last_statement
@@ -635,13 +670,22 @@ module Minjs
635
670
  end
636
671
 
637
672
  def remove_paren
638
- if @exp1.kind_of? ExpParen and @exp1.priority <= 20 #left-hand
673
+ if @exp1.kind_of? ExpParen and @exp1.val.priority <= PRIORITY_LEFT_HAND_SIDE
639
674
  @exp1 = @exp1.val
640
675
  end
641
676
  if @exp2.kind_of? ExpParen
642
677
  @exp2 = @exp2.val
643
678
  end
679
+ self
644
680
  end
681
+
682
+ def add_paren
683
+ if @exp1.priority > PRIORITY_LEFT_HAND_SIDE
684
+ @exp1 = ExpParen.new(@exp1)
685
+ end
686
+ self
687
+ end
688
+
645
689
  def last_statement
646
690
  list = [self]
647
691
  list.concat @statement.last_statement
@@ -687,6 +731,7 @@ module Minjs
687
731
  end
688
732
  StForIn.new(t, @exp2, @statement)
689
733
  end
734
+
690
735
  def to_js(options = {})
691
736
  if @statement.kind_of? StBlock and @statement.statement_list.length == 1
692
737
  statement = @statement.statement_list.statement_list[0]
@@ -710,6 +755,11 @@ module Minjs
710
755
  if @exp2.kind_of? ExpParen
711
756
  @exp2 = @exp2.val
712
757
  end
758
+ self
759
+ end
760
+
761
+ def add_paren
762
+ self
713
763
  end
714
764
 
715
765
  def last_statement
@@ -733,6 +783,7 @@ module Minjs
733
783
  @exp.traverse(self, &block) if @exp
734
784
  yield self, parent
735
785
  end
786
+
736
787
  def to_js(options = {})
737
788
  if @exp
738
789
  concat options, :continue, @exp, ";"
@@ -740,6 +791,7 @@ module Minjs
740
791
  concat options, :continue, ";"
741
792
  end
742
793
  end
794
+
743
795
  def last_statement
744
796
  [self]
745
797
  end
@@ -767,6 +819,7 @@ module Minjs
767
819
  concat options, :break, ";"
768
820
  end
769
821
  end
822
+
770
823
  def last_statement
771
824
  [self]
772
825
  end
@@ -814,11 +867,18 @@ module Minjs
814
867
  concat options, :return, ";"
815
868
  end
816
869
  end
870
+
817
871
  def remove_paren
818
872
  if @exp.kind_of? ExpParen
819
873
  @exp = @exp.val
820
874
  end
875
+ self
876
+ end
877
+
878
+ def add_paren
879
+ self
821
880
  end
881
+
822
882
  def last_statement
823
883
  [self]
824
884
  end
@@ -843,11 +903,18 @@ module Minjs
843
903
  def to_js(options = {})
844
904
  concat options, :with, "(", @exp, ")", @statement
845
905
  end
906
+
846
907
  def remove_paren
847
908
  if @exp.kind_of? ExpParen
848
909
  @exp = @exp.val
849
910
  end
911
+ self
912
+ end
913
+
914
+ def add_paren
915
+ self
850
916
  end
917
+
851
918
  def last_statement
852
919
  list = [self]
853
920
  list.concat @statement.last_statement
@@ -910,7 +977,13 @@ module Minjs
910
977
  b[0] = b[0].val
911
978
  end
912
979
  end
980
+ self
981
+ end
982
+
983
+ def add_paren
984
+ self
913
985
  end
986
+
914
987
  def last_statement
915
988
  list = [self]
916
989
  end
@@ -976,7 +1049,13 @@ module Minjs
976
1049
 
977
1050
  #12.14
978
1051
  class StTry < St
979
- def initialize(try, catch, finally)
1052
+ attr_reader :context
1053
+ attr_reader :catch_context
1054
+ attr_reader :try, :catch, :finally
1055
+
1056
+ def initialize(context, catch_context, try, catch, finally)
1057
+ @context = context
1058
+ @catch_context = catch_context
980
1059
  @try = try
981
1060
  @catch = catch
982
1061
  @finally = finally
@@ -1021,6 +1100,7 @@ module Minjs
1021
1100
  [self]
1022
1101
  end
1023
1102
  end
1103
+
1024
1104
  #12.15
1025
1105
  class StDebugger < St
1026
1106
  def deep_dup
data/lib/minjs/func.rb CHANGED
@@ -21,6 +21,8 @@ module Minjs
21
21
 
22
22
  context.var_env.record.create_mutable_binding(id, nil)
23
23
  context.var_env.record.set_mutable_binding(id, f, nil)
24
+ context.lex_env.record.create_mutable_binding(id, nil)
25
+ context.lex_env.record.set_mutable_binding(id, f, nil)
24
26
  f
25
27
  else
26
28
  if b
@@ -52,6 +54,8 @@ module Minjs
52
54
  if id_opt
53
55
  new_context.var_env.record.create_mutable_binding(id_opt, nil)
54
56
  new_context.var_env.record.set_mutable_binding(id_opt, f, nil)
57
+ new_context.lex_env.record.create_mutable_binding(id_opt, nil)
58
+ new_context.lex_env.record.set_mutable_binding(id_opt, f, nil)
55
59
  id_opt.context = new_context
56
60
  end
57
61
  f
@@ -80,6 +84,8 @@ module Minjs
80
84
  ret.each do |argName|
81
85
  context.var_env.record.create_mutable_binding(argName, nil)
82
86
  context.var_env.record.set_mutable_binding(argName, :undefined, nil, {:_parameter_list => true})
87
+ context.lex_env.record.create_mutable_binding(argName, nil)
88
+ context.lex_env.record.set_mutable_binding(argName, :undefined, nil, {:_parameter_list => true})
83
89
  end
84
90
  ret
85
91
  }
data/lib/minjs/literal.rb CHANGED
@@ -34,9 +34,9 @@ module Minjs
34
34
  #
35
35
  def boolean_literal(lex, context)
36
36
  if lex.match_lit(ECMA262::ID_TRUE)
37
- ECMA262::Boolean.new(:true)
37
+ ECMA262::Boolean.get(:true)
38
38
  elsif lex.match_lit(ECMA262::ID_FALSE)
39
- ECMA262::Boolean.new(:false)
39
+ ECMA262::Boolean.get(:false)
40
40
  else
41
41
  nil
42
42
  end
@@ -20,7 +20,7 @@ module Minjs
20
20
  def evaluate(context, locals, &block)
21
21
  case context.content_type
22
22
  when 'application/javascript'
23
- if logger.debug?
23
+ if logger.info?
24
24
  @@c = 0 unless defined?(@@c)
25
25
  puts "start: compressing"
26
26
  file = "tmp#{@@c}.js"
@@ -34,7 +34,7 @@ module Minjs
34
34
  end
35
35
  #TODO
36
36
  t = Minjs::Compressor.new(:logger => logger).compress(data)
37
- if logger.debug?
37
+ if logger.info?
38
38
  tmp = open(output, "w")
39
39
  tmp.write(t)
40
40
  tmp.close
@@ -124,11 +124,9 @@ module Minjs
124
124
 
125
125
  def var_decl(lex, context, options)
126
126
  lex.eval_lit {
127
- a = lex.fwd_lit
127
+ a = identifier(lex, context)
128
128
  if !a
129
- nil
130
- elsif !a.kind_of?(ECMA262::IdentifierName)
131
- nil
129
+ raise ParseError.new("bad identifier");
132
130
  else
133
131
  b = initialiser(lex, context, options)
134
132
  [a, b]
@@ -236,8 +234,8 @@ module Minjs
236
234
  # for(var i ; cond ; exp)
237
235
  next nil unless lex.match_lit(ECMA262::PUNC_LPARENTHESIS)
238
236
  if lex.match_lit(ECMA262::ID_VAR) and vl=var_decl_list(lex, context, :no_in =>true) and lex.match_lit(ECMA262::PUNC_SEMICOLON) and (e=exp(lex, context, {})||true) and lex.match_lit(ECMA262::PUNC_SEMICOLON) and (e2=exp(lex, context, {})||true) and lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and s=statement(lex, context)
239
- e = ECMA262::ExpEmpty.new if e == true
240
- e2 = ECMA262::ExpEmpty.new if e2 == true
237
+ e = nil if e == true
238
+ e2 = nil if e2 == true
241
239
  #10.5
242
240
  vl.each do |v|
243
241
  dn = v[0]
@@ -260,9 +258,9 @@ module Minjs
260
258
  # for(i ; cond; exp)
261
259
  next nil unless lex.match_lit(ECMA262::PUNC_LPARENTHESIS)
262
260
  if (v=exp(lex, context, :no_in => true) || true) and lex.match_lit(ECMA262::PUNC_SEMICOLON) and (e=exp(lex, context, {}) || true) and lex.match_lit(ECMA262::PUNC_SEMICOLON) and (e2=exp(lex, context, {})||true) and lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and s=statement(lex, context)
263
- v = ECMA262::ExpEmpty.new if v == true
264
- e = ECMA262::ExpEmpty.new if e == true
265
- e2 = ECMA262::ExpEmpty.new if e2 == true
261
+ v = nil if v == true
262
+ e = nil if e == true
263
+ e2 = nil if e2 == true
266
264
  ECMA262::StFor.new(v, e, e2, s)
267
265
  else
268
266
  nil
@@ -405,25 +403,33 @@ module Minjs
405
403
  def try_statement(lex, context)
406
404
  return nil unless lex.match_lit(ECMA262::ID_TRY)
407
405
  lex.eval_lit {
406
+ catch_context = ECMA262::Context.new
407
+ catch_env = context.lex_env.new_declarative_env()
408
+ catch_context.lex_env = catch_env
409
+ catch_context.var_env = context.var_env
410
+
408
411
  t = block(lex, context)
409
412
  break nil unless t
410
413
 
411
414
  lex.eval_lit{
412
- c = try_catch(lex, context)
415
+ c = try_catch(lex, catch_context)
413
416
  break nil unless c
414
417
 
415
418
  f = try_finally(lex, context)
416
- ECMA262::StTry.new(t, c, f)
419
+ ECMA262::StTry.new(context, catch_context, t, c, f)
417
420
  } || lex.eval_lit{
418
421
  f = try_finally(lex, context)
419
422
  break nil unless f
420
- ECMA262::StTry.new(t, nil, f)
423
+ ECMA262::StTry.new(context, catch_context, t, nil, f)
421
424
  }
422
425
  }
423
426
  end
424
- def try_catch(lex, context)
427
+ def try_catch(lex, catch_context)
425
428
  return nil unless lex.match_lit(ECMA262::ID_CATCH)
426
- if lex.match_lit(ECMA262::PUNC_LPARENTHESIS) and i=identifier(lex, context) and lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and b=block(lex, context)
429
+
430
+ if lex.match_lit(ECMA262::PUNC_LPARENTHESIS) and i=identifier(lex, catch_context) and lex.match_lit(ECMA262::PUNC_RPARENTHESIS) and b=block(lex, catch_context)
431
+ catch_context.lex_env.record.create_mutable_binding(i, nil)
432
+ catch_context.lex_env.record.set_mutable_binding(i, :undefined, nil)
427
433
  [i, b]
428
434
  else
429
435
  nil
data/lib/minjs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Minjs
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Issei Numata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-05-15 00:00:00.000000000 Z
11
+ date: 2015-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler