locked
Javascript unable to do dynamic type cast

    Question

  • It looks like javascript in WinRT is unable to do dynamic type cast when calling into WINRT DLL component. Here is a simple example:

    // In js/default.js, I added the following lines:

    +   var testObj = new WinRTComponentDll.Test();
    +   var derivedClass = new WinRTComponentDll.DerivedClass();
    +   testObj.testTypeDynamicCast(derivedClass);

    Here we pass a DerivedClass object pointer although the method is defined with an argument of BaseClass type. When running the app, javascript fails to dynamically cast DerivedClass type to BaseClass type, throws a "Type mismatch" exception:

    "Unhandled exception at line 33, column 5 in ms-appx://e10bc6bb-8304-4b2a-a9a4-7311b5807830-4sdaedqydyjdm/js/default.js
    0x800a000d - JavaScript runtime error: Type mismatch
    File: default.js, line: 33 column: 5"


    The definition of the BaseClass, DerivedClass and Test classes is as follows:
    ////////////////////////////////////////////////////////////////

    // WinRTComponent.h

    #pragma once

    namespace WinRTComponentDll
    {
        public ref class BaseClass
        {
        public:
            BaseClass();
        };

     public ref class DerivedClass sealed : public BaseClass
        {
        public:
            DerivedClass();
        };

     public ref class Test sealed
     {
     public:
      void TestTypeDynamicCast (BaseClass^ base) {}
     };
    };

    // WinRTComponent.cpp
    #include "pch.h"
    #include "WinRTComponent.h"

    using namespace WinRTComponentDll;
    using namespace Platform;

    BaseClass::BaseClass()
    {}

    DerivedClass::DerivedClass()
    {}

    Tuesday, March 06, 2012 1:57 AM

All replies

  • That does not surprise me.  Remember you need to ensure only classes that are sealed can be passed through WinRT.  You cannot derive from sealed classes so I would expect this to fail.


    Jeff Sanders (MSFT)

    Tuesday, March 06, 2012 8:57 PM
    Moderator
  • That does not surprise me.  Remember you need to ensure only classes that are sealed can be passed through WinRT.  You cannot derive from sealed classes so I would expect this to fail.


    Jeff Sanders (MSFT)


    This same sample works in C#. Is the CLI for javascript missing this?
    Tuesday, March 06, 2012 10:36 PM
  • In the WIN8 developer preview this problem does not happen for javascript.

    Tuesday, March 06, 2012 10:47 PM
  • That is great info, thanks.  I have not had time to look yet but did you check the Migration document to see if this is listed?

    Jeff Sanders (MSFT)

    Wednesday, March 07, 2012 2:53 PM
    Moderator
  • hmm, good info John.  I will check this out.


    Jeff Sanders (MSFT)

    Wednesday, March 07, 2012 2:54 PM
    Moderator
  • I did check the Migration document, but this is not listed.

    Wednesday, March 07, 2012 6:03 PM
  • I got some more background on this.  WinJS will not support the class unless it is marked Sealed.  JS can only consume sealed classes.  This is by design.


    Jeff Sanders (MSFT)

    Wednesday, March 07, 2012 6:25 PM
    Moderator
  • I got some more background on this.  WinJS will not support the class unless it is marked Sealed.  JS can only consume sealed classes.  This is by design.


    Jeff Sanders (MSFT)

    Jeff,

    Since Developer Preview (before the type system was complete), we observed that both JS and C# CLI bindings had no problems with accessing types that at least had their parent sealed. C# in Consumer Preview still supports this. Should JS be less functional than C# or is C# broken in that it should never have supported this?

    Tuesday, March 13, 2012 6:36 PM