390 likes | 624 Views
PLAT-876T. Lessons learned designing the Windows Runtime. Brent Rector Senior Program Manager Microsoft Corporation. Agenda. Design considerations building a multi-language OS runtime How these decisions affect coding in your preferred language Apply this knowledge in your Metro style apps.
E N D
PLAT-876T Lessons learned designing the Windows Runtime Brent Rector Senior Program Manager Microsoft Corporation
Agenda • Design considerations building a multi-language OS runtime • How these decisions affect coding in your preferred language • Apply this knowledge in your Metro style apps
Agenda Please • Don’t make me write this again! RT
Windows Runtime APIs • Available to all programming languages • Requires a language neutral type system
Casing of APIs in multiple languages • Many languages use PascalCasing for names of types, methods, properties, and events • JavaScript has well established naming conventions • “Types” are PascalCased • Methods and Properties are camelCased • Events are all lowercase • Windows Runtime uses PascalCasing for types and members • JavaScript maps methods, properties and event names to its conventions • Causes oddities occasionally – UIElement -> uielement
Numeric types • Behave differently in .NET/C++ compared to JavaScript • C++/.NET - Signed, Unsigned; 8, 16, 32, 64 bit int; 32, 64 bit float • JavaScript – double-precision 64-bit binary format IEEE 754 value • Windows Runtime – Same as C++/.NET without signed 8-bit integer • But… • Operating system uses 64-bit integers in some places • file sizes, position, etc. • JavaScript number can only represent 53 bits of precision
Numeric types • ECMAScript (ECMA-262) Standard • Requires numbers treated as IEEE-754 64-bit floating point values • JavaScript applications using the Chakra runtime cannot accurately determine the values of some 64-bit integers • But such values can be received from and passed back to WinRT APIs • Many (most) values will work fine • Use caution with 64 bit values whose absolute value is greater than 2^53 (9,007,199,254,740,992)
publicrefclassMyClasssealed { • __int64 _largeNumber; • MyClass() { _largeNumber= 9007199254740992 + 7; } • property__int64LargeNumber { • __int64 get() { return _largeNumber; } • voidset(__int64value) { _largeNumber= value; } • } • boolCompareInt64(__int64 a, __int64 b) { return a == b; } • };
var b = newMyLibrary.MyClass(); • var val1 = b.largeNumber; • var val2 = b.largeNumber; • varisEqual = b.compareInt64(val1, val2); // true • val1 = val1 + 0; • isEqual = b.compareInt64(val1, val2); // false
Strings • Immutable or mutable? • Immutable – JavaScript, .NET; Mutable – C++ • Null • JavaScript: null is an object, string is a type • C++: std::string has no 'null' semantics • .NET System.String: reference type has a 'null' distinguished value • Windows Runtime: string's immutable, no null representation
publicrefclassMyClasssealed { • Platform::String^ _myString; • MyClass() {_myString = nullptr; } • propertyPlatform::String^ MyString { • Platform::String^ get() { return _myString; } • void set(Platform::String^ value) { • _myString = value; } • } • };
vars = b.myString; • // What is the value of ‘s’? • b.myString= null; • vars = b.myString; • // What is the value of ‘s’? • b.myString = ""; • vars = b.myString; • // What is the value of ‘s’
Structures • Handled differently by languages • C++/CLR: Structures are value types, can contain all types • JavaScript: Has no structure construct but structures can be simulated • Windows Runtime supports structures, with restrictions on content • Can only contain numbers, strings or structures • Windows Runtime structures cannot contain pointers • Windows Runtime structures are value types • As a result… • Structure valued property behavior differs in languages
using namespace Windows::Foundation; • publicrefclassMyClasssealed { • Rect _myBounds; • propertyRectMyBounds{ • Rectget() { return _myBounds; } • void set(Rect value) { _myBounds= value; } • } • };
b.myBounds = {x: 100, y: 200, width: 300, height: 400 }; • b.myBounds.width= 42; • // What is the value of ‘width’? • b.myBounds = {x:b.myBounds.x, • y:b.myBounds.y, • width:42, • height:b.myBounds.height}; • var bounds = b.myBounds; • bounds.width = 42; • // What is the value of ‘b.myBounds.width’?
References & pointers • Target languages handle pointers and references differently • C++: All types can be passed by value or by reference • .NET: Objects are passed by reference, value types by value • JavaScript: Objects passed by reference, numbers passed by value • Windows Runtime: Objects (Interfaces) passed by reference, all other types passed by value • As a result… • Method parameters are [in] or [out], never [in, out]
Arrays • Reference or value types? • JavaScript, .NET, C++: Reference types • Windows Runtime • Arrays are value types so marshal by value • ONLY WHEN they are marshaled!
publicrefclassMyClasssealed { • voidArrayMethod (array<int>^ arr) { arr[0] = 42; } • void ArrayMethod2 (array<double>^ arr) { arr[0] = 42.0; } • };
vararr = { 1, 2, 3, 4, }; • b.ArrayMethod (arr); • // What is the value of arr[0]? • b.ArrayMethod2(arr); • // What is the value of arr[0]?
Events • Event syntax differs across languages • C++/CLR – Clients add events with += operator • JavaScript – Clients add events with either the addEventListener function or by setting the “on<eventname>” property on the object. • Be aware… • JavaScript event name casing is all lowercase • Assigning to the property implies one listener while addEventListener allows multiple listeners • Misspelling the event name in either case silently fails
public ref class SignaledEventArgs sealed { • public: SignaledEventArgs() { }; • }; • public delegate void SignaledEventHandler( • Platform::Object^ sender, • SignaledEventArgsargs); • publicrefclassMyClasssealed { • event SignaledEventHandler^ Signaled; • };
var b = newMyLibrary.MyClass(); • b.addEventListener ("Signaled", function () { … }); • // What just happened? • b.addEventListener("signaled", function () { … }); • b.onSignaled = function () { … }; • // What just happened? • b.onsignaled= function () { … };
Events vs. delegates • Windows Runtime prefers events over delegate-typed properties • Both are (potentially) multi-cast • JavaScript nicely maps events to JavaScript event conventions • addEventListener (“eventname”, func) • oneventname property • Delegate properties are just that: properties • No addEvetListener form • Normal camelCasing of property name occurs: eventName
Collections • Windows Runtime collections are relatively straightforward • Vectors – similar to array, projected with array syntax • Maps – key/value pairs, projected as dictionary to CLR apps • Surprise… • Because collection value accesses require a method call, they can be unexpected performance bottlenecks • Can use the GetMany method for bulk retrieval
Method overloading • Two criteria used when handling overloads • Overload on number of params (arity) • JavaScript, .NET, C++ all distinguish methods by arity • Overload on parameter type • .NET, C++ all distinguish methods by type of parameters • JavaScript has a limited notion of type • Windows Runtime methods • Overload on arity but not on type
publicrefclassMyClasssealed { • Platform::String^ AnOverload (Platform::String^ a) { • return a; • } • Platform::String^ AnOverload (Platform::String^ a, • Platform::String^ b) { • return a + b • } • };
varstr = b.AnOverload ("Hello"); // What just happened? • varstr = b.anOverload("Hello"); // What just happened? • varstr = b.anOverload ("Hello", " audience"); • // What just happened? • varstr = b.anOverload ("Hello", " audience", " at BUILD"); • // What just happened?
Asynchronous APIs • Platform design goal that APIs cannot block the UI thread • Guidance: Anything taking > 50 ms should be asynchronous • Resulted in prevalent use of Async Pattern in platform • Established a naming pattern: <verb>[<noun>]Async • GetThumbnailAsync • Return value mapped to a promise for a future result • In .NET – await obj.GetThumbnailAsync() • In JavaScript – obj.GetThumbNailAsync().then (func);
Further reading and documentation • C++ language extension summary • The Windows Runtime • Windows Runtime Design
thank you Feedback and questions http://forums.dev.windows.com Session feedbackhttp://bldw.in/SessionFeedback
© 2011 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.