minjs 0.4.0 → 0.4.1

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.
@@ -142,16 +142,13 @@ module Minjs
142
142
  #
143
143
  # @see http://www.ecma-international.org/ecma-262 ECMA262 11.1.1
144
144
  class This < Literal
145
- attr_reader :context
146
-
147
- def initialize(context)
148
- @context = context
145
+ def initialize
149
146
  end
150
147
 
151
148
  # duplicate object
152
149
  # @see Base#deep_dup
153
150
  def deep_dup
154
- self.class.new(@context)
151
+ self.class.new
155
152
  end
156
153
 
157
154
  # Traverses this children and itself with given block.
@@ -1000,7 +997,7 @@ module Minjs
1000
997
  # Returns a ECMAScript string containg the representation of element.
1001
998
  # @see Base#to_js
1002
999
  def to_js(options = {})
1003
- "[" + @val.collect{|x| x.to_s}.join(",") + "]"
1000
+ "[" + @val.collect{|x| x ? x.to_js : ""}.join(",") + "]"
1004
1001
  end
1005
1002
 
1006
1003
  # @return [Boolean] true if expression is kind of LeftHandSideExpression.
@@ -1177,22 +1174,21 @@ module Minjs
1177
1174
  #
1178
1175
  # @see http://www.ecma-international.org/ecma-262 ECMA262 7.6
1179
1176
  class IdentifierName < Literal
1180
- attr_accessor :context
1177
+ attr_accessor :exe_context
1181
1178
  attr_reader :val
1182
1179
 
1183
1180
  @@sym = {}
1184
1181
 
1185
- def initialize(context, val)
1186
- @context = context
1182
+ def initialize(val)
1187
1183
  @val = val.to_sym
1188
1184
  end
1189
1185
 
1190
1186
  # get instance
1191
- def self.get(context, val)
1187
+ def self.get(val)
1192
1188
  if reserved?(val)
1193
- @@sym[val] ||= self.new(context, val)
1189
+ @@sym[val] ||= self.new(val)
1194
1190
  else
1195
- self.new(context, val)
1191
+ self.new(val)
1196
1192
  end
1197
1193
  end
1198
1194
 
@@ -1238,7 +1234,7 @@ module Minjs
1238
1234
  # duplicate object
1239
1235
  # @see Base#deep_dup
1240
1236
  def deep_dup
1241
- self.class.new(@context, @val)
1237
+ self.class.new(@val)
1242
1238
  end
1243
1239
 
1244
1240
  # compare object
@@ -1252,19 +1248,19 @@ module Minjs
1252
1248
  val.to_s
1253
1249
  end
1254
1250
 
1251
+ def to_s
1252
+ val.to_s
1253
+ end
1254
+
1255
1255
  # @return [Boolean] true if expression is kind of LeftHandSideExpression.
1256
1256
  def left_hand_side_exp?
1257
1257
  true
1258
1258
  end
1259
1259
 
1260
1260
  # @return [EnvRecord] binding environment
1261
- def binding_env(type = :var)
1262
- return nil if context.nil?
1263
- if type == :var
1264
- v = context.var_env
1265
- else
1266
- v = context.lex_env
1267
- end
1261
+ def binding_env(lex_env)
1262
+ return nil if lex_env.nil?
1263
+ v = lex_env
1268
1264
 
1269
1265
  while v
1270
1266
  if v.record.binding[val]
@@ -1278,67 +1274,67 @@ module Minjs
1278
1274
  end
1279
1275
 
1280
1276
  # reserved word "this"
1281
- ID_THIS = IdentifierName.get(nil, :this)
1277
+ ID_THIS = IdentifierName.get(:this)
1282
1278
  # reserved word "var"
1283
- ID_VAR = IdentifierName.get(nil, :var)
1279
+ ID_VAR = IdentifierName.get(:var)
1284
1280
  # reserved word "in"
1285
- ID_IN = IdentifierName.get(nil, :in)
1281
+ ID_IN = IdentifierName.get(:in)
1286
1282
  # reserved word "instanceof"
1287
- ID_INSTANCEOF = IdentifierName.get(nil, :instanceof)
1283
+ ID_INSTANCEOF = IdentifierName.get(:instanceof)
1288
1284
  # reserved word "function"
1289
- ID_FUNCTION = IdentifierName.get(nil, :function)
1285
+ ID_FUNCTION = IdentifierName.get(:function)
1290
1286
  # reserved word "null"
1291
- ID_NULL = IdentifierName.get(nil, :null)
1287
+ ID_NULL = IdentifierName.get(:null)
1292
1288
  # reserved word "true"
1293
- ID_TRUE = IdentifierName.get(nil, :true)
1289
+ ID_TRUE = IdentifierName.get(:true)
1294
1290
  # reserved word "false"
1295
- ID_FALSE = IdentifierName.get(nil, :false)
1291
+ ID_FALSE = IdentifierName.get(:false)
1296
1292
  # reserved word "new"
1297
- ID_NEW = IdentifierName.get(nil, :new)
1293
+ ID_NEW = IdentifierName.get(:new)
1298
1294
  # reserved word "delete"
1299
- ID_DELETE = IdentifierName.get(nil, :delete)
1295
+ ID_DELETE = IdentifierName.get(:delete)
1300
1296
  # reserved word "void"
1301
- ID_VOID = IdentifierName.get(nil, :void)
1297
+ ID_VOID = IdentifierName.get(:void)
1302
1298
  # reserved word "typeof"
1303
- ID_TYPEOF = IdentifierName.get(nil, :typeof)
1299
+ ID_TYPEOF = IdentifierName.get(:typeof)
1304
1300
  # reserved word "if"
1305
- ID_IF = IdentifierName.get(nil, :if)
1301
+ ID_IF = IdentifierName.get(:if)
1306
1302
  # reserved word "else"
1307
- ID_ELSE = IdentifierName.get(nil, :else)
1303
+ ID_ELSE = IdentifierName.get(:else)
1308
1304
  # reserved word "for"
1309
- ID_FOR = IdentifierName.get(nil, :for)
1305
+ ID_FOR = IdentifierName.get(:for)
1310
1306
  # reserved word "while"
1311
- ID_WHILE = IdentifierName.get(nil, :while)
1307
+ ID_WHILE = IdentifierName.get(:while)
1312
1308
  # reserved word "do"
1313
- ID_DO = IdentifierName.get(nil, :do)
1309
+ ID_DO = IdentifierName.get(:do)
1314
1310
  # reserved word "continue"
1315
- ID_CONTINUE = IdentifierName.get(nil, :continue)
1311
+ ID_CONTINUE = IdentifierName.get(:continue)
1316
1312
  # reserved word "break"
1317
- ID_BREAK = IdentifierName.get(nil, :break)
1313
+ ID_BREAK = IdentifierName.get(:break)
1318
1314
  # reserved word "return"
1319
- ID_RETURN = IdentifierName.get(nil, :return)
1315
+ ID_RETURN = IdentifierName.get(:return)
1320
1316
  # reserved word "with"
1321
- ID_WITH = IdentifierName.get(nil, :with)
1317
+ ID_WITH = IdentifierName.get(:with)
1322
1318
  # reserved word "switch"
1323
- ID_SWITCH = IdentifierName.get(nil, :switch)
1319
+ ID_SWITCH = IdentifierName.get(:switch)
1324
1320
  # reserved word "throw"
1325
- ID_THROW = IdentifierName.get(nil, :throw)
1321
+ ID_THROW = IdentifierName.get(:throw)
1326
1322
  # reserved word "try"
1327
- ID_TRY = IdentifierName.get(nil, :try)
1323
+ ID_TRY = IdentifierName.get(:try)
1328
1324
  # reserved word "catch"
1329
- ID_CATCH = IdentifierName.get(nil, :catch)
1325
+ ID_CATCH = IdentifierName.get(:catch)
1330
1326
  # reserved word "finally"
1331
- ID_FINALLY = IdentifierName.get(nil, :finally)
1327
+ ID_FINALLY = IdentifierName.get(:finally)
1332
1328
  # reserved word "debugger"
1333
- ID_DEBUGGER = IdentifierName.get(nil, :debugger)
1329
+ ID_DEBUGGER = IdentifierName.get(:debugger)
1334
1330
  # reserved word "case"
1335
- ID_CASE = IdentifierName.get(nil, :case)
1331
+ ID_CASE = IdentifierName.get(:case)
1336
1332
  # reserved word "default"
1337
- ID_DEFAULT = IdentifierName.get(nil, :default)
1333
+ ID_DEFAULT = IdentifierName.get(:default)
1338
1334
  # get (non-reserved word)
1339
- ID_GET = IdentifierName.get(nil, :get)
1335
+ ID_GET = IdentifierName.get(:get)
1340
1336
  # set (non-reserved word)
1341
- ID_SET = IdentifierName.get(nil, :set)
1337
+ ID_SET = IdentifierName.get(:set)
1342
1338
 
1343
1339
  end
1344
1340
  end
@@ -87,7 +87,6 @@ module Minjs
87
87
  @statement_list.statement_list.select{|s|
88
88
  s.class != StEmpty
89
89
  }[0].to_exp.deep_dup
90
-
91
90
  end
92
91
 
93
92
  # @return true if block can convert to single statement.
@@ -145,20 +144,20 @@ module Minjs
145
144
  # @see http://www.ecma-international.org/ecma-262 ECMA262 12.2
146
145
  class StVar < Statement
147
146
  attr_reader :vars
148
- attr_reader :context
147
+ attr_reader :var_env
149
148
  #
150
149
  # vars:
151
150
  # [[name0,init0],[name1,init1],...]
152
151
  #
153
- def initialize(context, vars)
152
+ def initialize(var_env, vars)
154
153
  @vars = vars
155
- @context = context
154
+ @var_env = var_env
156
155
  end
157
156
 
158
157
  # duplicate object
159
158
  # @see Base#deep_dup
160
159
  def deep_dup
161
- self.class.new(@context,
160
+ self.class.new(@var_env,
162
161
  @vars.collect{|x,y|
163
162
  [x.deep_dup, y ? y.deep_dup : nil]
164
163
  })
@@ -717,15 +716,15 @@ module Minjs
717
716
  #
718
717
  # @see http://www.ecma-international.org/ecma-262 ECMA262 12.6.3
719
718
  class StForVar < Statement
720
- attr_reader :context
719
+ attr_reader :var_env
721
720
  attr_reader :var_decl_list, :exp2, :exp3, :statement
722
721
 
723
722
  #
724
723
  # var_decl_list
725
724
  # [[name0, init0],[name1, init1], ...]
726
725
  #
727
- def initialize(context, var_decl_list, exp2, exp3, statement)
728
- @context = context
726
+ def initialize(var_env, var_decl_list, exp2, exp3, statement)
727
+ @var_env = var_env
729
728
  @var_decl_list = var_decl_list
730
729
  @exp2 = exp2
731
730
  @exp3 = exp3
@@ -735,7 +734,7 @@ module Minjs
735
734
  # duplicate object
736
735
  # @see Base#deep_dup
737
736
  def deep_dup
738
- self.class.new(@context,
737
+ self.class.new(@var_env,
739
738
  @var_decl_list.collect{|x,y|
740
739
  [x.deep_dup, y.deep_dup]
741
740
  },
@@ -913,11 +912,11 @@ module Minjs
913
912
  #
914
913
  # @see http://www.ecma-international.org/ecma-262 ECMA262 12.6.4
915
914
  class StForInVar < Statement
916
- attr_reader :context
915
+ attr_reader :var_env
917
916
  attr_reader :var_decl, :exp2, :statement
918
917
 
919
- def initialize(context, var_decl, exp2, statement)
920
- @context = context
918
+ def initialize(var_env, var_decl, exp2, statement)
919
+ @var_env = var_env
921
920
  @var_decl = var_decl
922
921
  @exp2 = exp2
923
922
  @statement = statement
@@ -926,7 +925,7 @@ module Minjs
926
925
  # duplicate object
927
926
  # @see Base#deep_dup
928
927
  def deep_dup
929
- self.class.new(@context,
928
+ self.class.new(@var_env,
930
929
  [@var_decl[0].deep_dup, @var_decl[1] ? @var_decl[1].deep_dup : nil],
931
930
  @exp2.deep_dup,
932
931
  @statement.deep_dup)
@@ -1154,10 +1153,10 @@ module Minjs
1154
1153
  #
1155
1154
  # @see http://www.ecma-international.org/ecma-262 ECMA262 12.10
1156
1155
  class StWith < Statement
1157
- attr_reader :exp, :statement, :context
1156
+ attr_reader :exp, :statement, :var_env
1158
1157
 
1159
- def initialize(context, exp, statement)
1160
- @context = context
1158
+ def initialize(var_env, exp, statement)
1159
+ @var_env = var_env
1161
1160
  @exp = exp
1162
1161
  @statement = statement
1163
1162
  end
@@ -1165,7 +1164,7 @@ module Minjs
1165
1164
  # duplicate object
1166
1165
  # @see Base#deep_dup
1167
1166
  def deep_dup
1168
- self.class.new(@context, @exp.deep_dup, @statement.deep_dup)
1167
+ self.class.new(@var_env, @exp.deep_dup, @statement.deep_dup)
1169
1168
  end
1170
1169
 
1171
1170
  # Replaces children object.
@@ -1382,15 +1381,67 @@ module Minjs
1382
1381
  end
1383
1382
  end
1384
1383
 
1384
+ class StTryCatch < Statement
1385
+ attr_reader :var_env
1386
+ attr_reader :arg
1387
+ attr_reader :block
1388
+ attr_accessor :exe_context
1389
+
1390
+ def initialize(var_env, arg, block)
1391
+ @var_env = var_env
1392
+ @arg = arg
1393
+ @block = block
1394
+ end
1395
+
1396
+ def to_js(options = {})
1397
+ concat(options, :catch, "(", @arg, ")", @block)
1398
+ end
1399
+
1400
+ # duplicate object
1401
+ # @see Base#deep_dup
1402
+ def deep_dup
1403
+ self.class.new(@var_env,
1404
+ @arg.deep_dup,
1405
+ @block.deep_dup)
1406
+ end
1407
+
1408
+ # Traverses this children and itself with given block.
1409
+ def traverse(parent, &block)
1410
+ @arg.traverse(self, &block)
1411
+ @block.traverse(self, &block)
1412
+ yield parent, self
1413
+ end
1414
+
1415
+ # Replaces children object.
1416
+ # @see Base#replace
1417
+ def replace(from, to)
1418
+ if from .eql? @arg
1419
+ @arg = to
1420
+ elsif from .eql? @block
1421
+ @block = to
1422
+ end
1423
+ end
1424
+
1425
+ def enter(outer_exe_context)
1426
+ catch_env = LexEnv.new_declarative_env(outer_exe_context.lex_env)
1427
+ catch_env.record.create_mutable_binding(@arg, nil)
1428
+ catch_env.record.set_mutable_binding(@arg, :undefined, nil, nil)
1429
+
1430
+ new_exe_context = ExeContext.new
1431
+ new_exe_context.lex_env = catch_env
1432
+ new_exe_context
1433
+ end
1434
+ end
1435
+
1385
1436
  # Base class of ECMA262 TryStatement element.
1386
1437
  #
1387
1438
  # @see http://www.ecma-international.org/ecma-262 ECMA262 12.14
1388
1439
  class StTry < Statement
1389
- attr_reader :context
1440
+ attr_reader :var_env
1390
1441
  attr_reader :try, :catch, :finally
1391
1442
 
1392
- def initialize(context, try, catch, finally)
1393
- @context = context
1443
+ def initialize(var_env, try, catch, finally)
1444
+ @var_env = var_env
1394
1445
  @try = try
1395
1446
  @catch = catch
1396
1447
  @finally = finally
@@ -1399,9 +1450,9 @@ module Minjs
1399
1450
  # duplicate object
1400
1451
  # @see Base#deep_dup
1401
1452
  def deep_dup
1402
- self.class.new(@context,
1453
+ self.class.new(@var_env,
1403
1454
  @try.deep_dup,
1404
- @catch ? [@catch[0].deep_dup, @catch[1].deep_dup] : nil,
1455
+ @catch ? @catch.deep_dup : nil,
1405
1456
  @finally ? @finally.deep_dup : nil)
1406
1457
  end
1407
1458
 
@@ -1410,10 +1461,8 @@ module Minjs
1410
1461
  def replace(from, to)
1411
1462
  if from .eql? @try
1412
1463
  @try = to
1413
- elsif from .eql? @catch[0]
1414
- @catch[0] = to
1415
- elsif from .eql? @catch[1]
1416
- @catch[1] = to
1464
+ elsif from .eql? @catch
1465
+ @catch = to
1417
1466
  elsif from .eql? @finally
1418
1467
  @finally = to
1419
1468
  end
@@ -1422,10 +1471,7 @@ module Minjs
1422
1471
  # Traverses this children and itself with given block.
1423
1472
  def traverse(parent, &block)
1424
1473
  @try.traverse(self, &block)
1425
- if @catch
1426
- @catch[0].traverse(self, &block)
1427
- @catch[1].traverse(self, &block)
1428
- end
1474
+ @catch.traverse(self, &block) if @catch
1429
1475
  @finally.traverse(self, &block) if @finally
1430
1476
  yield parent, self
1431
1477
  end
@@ -1442,13 +1488,14 @@ module Minjs
1442
1488
  # @see Base#to_js
1443
1489
  def to_js(options = {})
1444
1490
  if @catch and @finally
1445
- concat(options, :try, @try, :catch, "(", @catch[0], ")", @catch[1], :finally, @finally)
1491
+ concat(options, :try, @try, @catch, :finally, @finally)
1446
1492
  elsif @catch
1447
- concat(options, :try, @try, :catch, "(", @catch[0], ")", @catch[1])
1493
+ concat(options, :try, @try, @catch)
1448
1494
  else
1449
1495
  concat(options, :try, @try, :finally, @finally)
1450
1496
  end
1451
1497
  end
1498
+
1452
1499
  end
1453
1500
 
1454
1501
  # Base class of ECMA262 DebuggerStatement element.
@@ -1486,10 +1533,11 @@ module Minjs
1486
1533
  attr_reader :name
1487
1534
  attr_reader :args
1488
1535
  attr_reader :statements
1489
- attr_reader :context
1536
+ attr_reader :var_env
1537
+ attr_accessor :exe_context
1490
1538
 
1491
- def initialize(context, name, args, statements, options = {})
1492
- @context = context
1539
+ def initialize(var_env, name, args, statements, options = {})
1540
+ @var_env = var_env
1493
1541
  @name = name
1494
1542
  @args = args #=> array
1495
1543
  @statements = statements #=> Prog
@@ -1506,10 +1554,10 @@ module Minjs
1506
1554
  # duplicate object
1507
1555
  # @see Base#deep_dup
1508
1556
  def deep_dup
1509
- self.class.new(@context, @name ? @name.deep_dup : nil,
1510
- @args.collect{|args|args.deep_dup},
1511
- @statements.deep_dup,
1512
- {decl: @decl, getter: @getter, setter: @setter})
1557
+ self.class.new(@var_env, @name ? @name.deep_dup : nil,
1558
+ @args.collect{|args|args.deep_dup},
1559
+ @statements.deep_dup,
1560
+ {decl: @decl, getter: @getter, setter: @setter})
1513
1561
  end
1514
1562
 
1515
1563
  # Traverses this children and itself with given block.
@@ -1562,6 +1610,19 @@ module Minjs
1562
1610
  def decl?
1563
1611
  @decl
1564
1612
  end
1613
+
1614
+ def enter(outer_exe_context)
1615
+ local_env = LexEnv.new_declarative_env(outer_exe_context.lex_env)
1616
+ var_env.record.binding.each do |k, v|
1617
+ local_env.record.create_mutable_binding(k, nil)
1618
+ val = v.delete(:value)
1619
+ local_env.record.set_mutable_binding(k, val, nil, v)
1620
+ end
1621
+ new_exe_context = ExeContext.new
1622
+ new_exe_context.lex_env = local_env
1623
+ new_exe_context.var_env = var_env
1624
+ new_exe_context
1625
+ end
1565
1626
  end
1566
1627
  end
1567
1628
  end