1 / 39

Lessons learned designing the Windows Runtime

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.

more
Download Presentation

Lessons learned designing the Windows Runtime

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PLAT-876T Lessons learned designing the Windows Runtime Brent Rector Senior Program Manager Microsoft Corporation

  2. 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

  3. Agenda Please • Don’t make me write this again! RT

  4. Windows Runtime APIs • Available to all programming languages • Requires a language neutral type system

  5. 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

  6. Windows Runtime types

  7. 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

  8. 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)

  9. 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; } • };

  10. 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

  11. Windows Runtime types

  12. 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

  13. publicrefclassMyClasssealed { • Platform::String^ _myString; • MyClass() {_myString = nullptr; } • propertyPlatform::String^ MyString { • Platform::String^ get() { return _myString; } • void set(Platform::String^ value) { • _myString = value; } • } • };

  14. 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’

  15. Windows Runtime types

  16. 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

  17. using namespace Windows::Foundation; • publicrefclassMyClasssealed { • Rect _myBounds; • propertyRectMyBounds{ • Rectget() { return _myBounds; } • void set(Rect value) { _myBounds= value; } • } • };

  18. 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’?

  19. Windows Runtime types

  20. 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]

  21. Windows Runtime types

  22. 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!

  23. publicrefclassMyClasssealed { • voidArrayMethod (array<int>^ arr) { arr[0] = 42; } • void ArrayMethod2 (array<double>^ arr) { arr[0] = 42.0; } • };

  24. 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]?

  25. Windows Runtime types

  26. 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

  27. public ref class SignaledEventArgs sealed { • public: SignaledEventArgs() { }; • }; • public delegate void SignaledEventHandler( • Platform::Object^ sender, • SignaledEventArgsargs); • publicrefclassMyClasssealed { • event SignaledEventHandler^ Signaled; • };

  28. var b = newMyLibrary.MyClass(); • b.addEventListener ("Signaled", function () { … }); • // What just happened? • b.addEventListener("signaled", function () { … }); • b.onSignaled = function () { … }; • // What just happened? • b.onsignaled= function () { … };

  29. 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

  30. Windows Runtime types

  31. 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

  32. 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

  33. publicrefclassMyClasssealed { • Platform::String^ AnOverload (Platform::String^ a) { • return a; • } • Platform::String^ AnOverload (Platform::String^ a, • Platform::String^ b) { • return a + b • } • };

  34. 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?

  35. 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);

  36. Further reading and documentation • C++ language extension summary • The Windows Runtime • Windows Runtime Design

  37. thank you Feedback and questions http://forums.dev.windows.com Session feedbackhttp://bldw.in/SessionFeedback

  38. © 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.

More Related