浏览代码

Fix code generation for "IS".

Vladislav Folts 11 年之前
父节点
当前提交
5c32e2ed38
共有 3 个文件被更改,包括 10 次插入4 次删除
  1. 7 4
      src/context.js
  2. 1 0
      test/expected/is.js
  3. 2 0
      test/input/is.ob

+ 7 - 4
src/context.js

@@ -271,6 +271,11 @@ exports.Identdef = ChainedContext.extend({
     }
 });
 
+function castCode(type, context){
+    var baseType = type instanceof Type.Pointer ? type.baseType() : type;
+    return context.qualifyScope(baseType.scope()) + baseType.cons();
+}
+
 exports.Designator = ChainedContext.extend({
     init: function Context$Designator(context){
         ChainedContext.prototype.init.call(this, context);
@@ -380,9 +385,7 @@ exports.Designator = ChainedContext.extend({
 
         checkTypeCast(this.__currentType, type, "invalid type cast");
 
-        var baseType = type instanceof Type.Pointer ? type.baseType() : type;
-        var castName = this.qualifyScope(baseType.scope()) + baseType.cons();
-        var code = this.rtl().typeGuard(this.__code, castName);
+        var code = this.rtl().typeGuard(this.__code, castCode(type, this));
         this.__code = code;
 
         this.__currentType = type;
@@ -1139,7 +1142,7 @@ exports.Expression = ChainedContext.extend({
 
             checkTypeCast(leftType, rightType, "invalid type test");
 
-            code = leftCode + " instanceof " + rightCode;
+            code = leftCode + " instanceof " + castCode(rightType, this);
         }
         else {
             leftExpression = promoteTypeInExpression(leftExpression, rightType);

+ 1 - 0
test/expected/is.js

@@ -39,6 +39,7 @@ pd2 = new Derived2();
 pb = pd2;
 pd1 = pd2;
 b = pb instanceof Derived1;
+b = pb instanceof Derived1;
 b = pb instanceof Derived2;
 b = pd1 instanceof Derived2;
 }();

+ 2 - 0
test/input/is.ob

@@ -3,6 +3,7 @@ MODULE m;
 TYPE
 	Base = RECORD END;
 	Derived1 = RECORD(Base) field1: INTEGER END;
+	PDerived1 = POINTER TO Derived1;
 	Derived2 = RECORD(Derived1) field2: INTEGER END;
 VAR
 	pb: POINTER TO Base;
@@ -14,6 +15,7 @@ BEGIN
     pb := pd2;
     pd1 := pd2;
     b := pb^ IS Derived1;
+    b := pb IS PDerived1;
     b := pb^ IS Derived2;
     b := pd1^ IS Derived2
 END m.