Javascript unable to do dynamic type cast
-
6 martie 2012 01:57
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()
{}
Toate mesajele
-
6 martie 2012 20:57Moderator
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)
- Propus ca răspuns de Jeff SandersMicrosoft Employee, Moderator 6 martie 2012 20:57
-
6 martie 2012 22:36
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? -
6 martie 2012 22:47
In the WIN8 developer preview this problem does not happen for javascript.
-
7 martie 2012 14:53ModeratorThat 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)
-
7 martie 2012 14:54Moderator
hmm, good info John. I will check this out.
Jeff Sanders (MSFT)
-
7 martie 2012 18:03
I did check the Migration document, but this is not listed.
-
7 martie 2012 18:25Moderator
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)
- Propus ca răspuns de Jeff SandersMicrosoft Employee, Moderator 7 martie 2012 18:25
- Marcat ca răspuns de Renjie Huang 8 martie 2012 00:39
- Anulare marcare ca răspuns de Renjie Huang 13 martie 2012 23:14
-
13 martie 2012 18:36
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?