collections - Dave's Blog

Search
My timeline on Mastodon

JavaScript Types and WinRT Types

2016 Jan 21, 5:35

MSDN covers the topic of JavaScript and WinRT type conversions provided by Chakra (JavaScript Representation of Windows Runtime Types and Considerations when Using the Windows Runtime API), but for the questions I get about it I’ll try to lay out some specifics of that discussion more plainly. I’ve made a TL;DR JavaScript types and WinRT types summary table.

WinRT Conversion JavaScript
Struct ↔️ JavaScript object with matching property names
Class or interface instance JavaScript object with matching property names
Windows.Foundation.Collections.IPropertySet JavaScript object with arbitrary property names
Any DOM object

Chakra, the JavaScript engine powering the Edge browser and JavaScript Windows Store apps, does the work to project WinRT into JavaScript. It is responsible for, among other things, converting back and forth between JavaScript types and WinRT types. Some basics are intuitive, like a JavaScript string is converted back and forth with WinRT’s string representation. For other basic types check out the MSDN links at the top of the page. For structs, interface instances, class instances, and objects things are more complicated.

A struct, class instance, or interface instance in WinRT is projected into JavaScript as a JavaScript object with corresponding property names and values. This JavaScript object representation of a WinRT type can be passed into other WinRT APIs that take the same underlying type as a parameter. This JavaScript object is special in that Chakra keeps a reference to the underlying WinRT object and so it can be reused with other WinRT APIs.

However, if you start with plain JavaScript objects and want to interact with WinRT APIs that take non-basic WinRT types, your options are less plentiful. You can use a plain JavaScript object as a WinRT struct, so long as the property names on the JavaScript object match the WinRT struct’s. Chakra will implicitly create an instance of the WinRT struct for you when you call a WinRT method that takes that WinRT struct as a parameter and fill in the WinRT struct’s values with the values from the corresponding properties on your JavaScript object.

// C# WinRT component
public struct ExampleStruct
{
public string String;
public int Int;
}

public sealed class ExampleStructContainer
{
ExampleStruct value;
public void Set(ExampleStruct value)
{
this.value = value;
}

public ExampleStruct Get()
{
return this.value;
}
}

// JS code
var structContainer = new ExampleWinRTComponent.ExampleNamespace.ExampleStructContainer();
structContainer.set({ string: "abc", int: 123 });
console.log("structContainer.get(): " + JSON.stringify(structContainer.get()));
// structContainer.get(): {"string":"abc","int":123}

You cannot have a plain JavaScript object and use it as a WinRT class instance or WinRT interface instance. Chakra does not provide such a conversion even with ES6 classes.

You cannot take a JavaScript object with arbitrary property names that are unknown at compile time and don’t correspond to a specific WinRT struct and pass that into a WinRT method. If you need to do this, you have to write additional JavaScript code to explicitly convert your arbitrary JavaScript object into an array of property name and value pairs or something else that could be represented in WinRT.

However, the other direction you can do. An instance of a Windows.Foundation.Collections.IPropertySet implementation in WinRT is projected into JavaScript as a JavaScript object with property names and values corresponding to the key and value pairs in the IPropertySet. In this way you can project a WinRT object as a JavaScript object with arbitrary property names and types. But again, the reverse is not possible. Chakra will not convert an arbitrary JavaScript object into an IPropertySet.

// C# WinRT component
public sealed class PropertySetContainer
{
private Windows.Foundation.Collections.IPropertySet otherValue = null;

public Windows.Foundation.Collections.IPropertySet other
{
get
{
return otherValue;
}
set
{
otherValue = value;
}
}
}

public sealed class PropertySet : Windows.Foundation.Collections.IPropertySet
{
private IDictionary map = new Dictionary();

public PropertySet()
{
map.Add("abc", "def");
map.Add("ghi", "jkl");
map.Add("mno", "pqr");
}
// ... rest of PropertySet implementation is simple wrapper around the map member.


// JS code
var propertySet = new ExampleWinRTComponent.ExampleNamespace.PropertySet();
console.log("propertySet: " + JSON.stringify(propertySet));
// propertySet: {"abc":"def","ghi":"jkl","mno":"pqr"}

var propertySetContainer = new ExampleWinRTComponent.ExampleNamespace.PropertySetContainer();
propertySetContainer.other = propertySet;
console.log("propertySetContainer.other: " + JSON.stringify(propertySetContainer.other));
// propertySetContainer.other: {"abc":"def","ghi":"jkl","mno":"pqr"}

try {
propertySetContainer.other = { "123": "456", "789": "012" };
}
catch (e) {
console.error("Error setting propertySetContainer.other: " + e);
// Error setting propertySetContainer.other: TypeError: Type mismatch
}

There’s also no way to implicitly convert a DOM object into a WinRT type. If you want to write third party WinRT code that interacts with the DOM, you must do so indirectly and explicitly in JavaScript code that is interacting with your third party WinRT. You’ll have to extract the information you want from your DOM objects to pass into WinRT methods and similarly have to pass messages out from WinRT that say what actions the JavaScript should perform on the DOM.

PermalinkCommentschakra development javascript winrt

WinRT PropertySet Changed Event Danger

2013 Jul 8, 1:46

The Windows Runtime API Windows.Foundation.Collections.PropertySet class​ is a nice string name to object value map that has a changed event that fires when the contents of the map is modified. Be careful with this event because it fires synchronously from the thread on which the PropertySet was modified. If modified from the UI thread, the UI thread will then wait as it synchronously dispatches the changed event to all listeners which could lead to performance issues or especially from the UI thread deadlock. For instance, deadlock if you have two threads both trying to tell each other about changed events for different PropertySets.

PermalinkCommentsdeadlock development propertyset windows windows-runtime winrt

Photosynth of my Office

2008 Aug 26, 11:08

I've had a little fun messing around with Photosynth, a Microsoft research project turned into a Live service. You upload a bunch of photos from around the same area and it makes a 3D panorama out of them. For instance, here's National Geographic's photosynth of the sphinx and pyramids in Egypt. Messing around with this I've made one of half a vase of roses, and a larger photosynth of my office.

PermalinkCommentsmicrosoft photosynth photo office nontechnical

Ogle Earth: Drawing in Google Maps vs drawing in Live Local: Which is better?

2008 Mar 4, 11:01Google's My Maps: "And now, this morning, Google has updated its Maps product yet again, adding "My Maps", user-generated collections of annotated placemarks, lines, and polygons."PermalinkCommentsgoogle map microsoft live georss blog article

Virtual Earth / Live Maps: New Version of Live Search Maps Launches!

2008 Mar 4, 10:57Live Maps supports exporting collections you make in Live Maps as a GeoRSS feed. That's neat, but what I want to do is add my flickr GeoRSS feed to a collection in Live Maps...PermalinkCommentsgeorss live microsoft maps blog article

Flickr: The Commons

2008 Jan 16, 2:58Library of Congress puts up photo collections on Flickr. Neat!PermalinkCommentscopyright flickr library-of-congress library congress photo photography tagging community

San Francisco Trip

2007 May 11, 7:48Hotel Diva BedAfter Carissa and Elijah's wedding Sarah and I went to San Francisco. We drove in, well Sarah drove anyway, still in the PT Cruiser Sunday morning and checked into our hotel, Hotel Diva. I was originally concerned that I wouldn't fit in as I don't really consider myself a diva, however the hotel was cool. They have Internet rooms setup in various themes, the front desk is always staffed, our room had a very modern look, and when we entered the flat-screen over the front desk was playing an episode of Aqua Teen Hunger Force.

Outside the SF Museum of Modern ArtWe walked around a bit before going to the SF Museum of Modern Art. There was a Picasso exhibit at the time which we could see for only $3 more. It felt kind of wrong like my ticket was super-sized. I think the most memorable piece I saw was three white panels which consisted of three blank panels. Art. Sure. After that Sarah wanted to see the giant Hello Kitty store she had heard of from her sister. We ended up going to the Westfield Shopping center which has a disappointingly average sized Hello Kitty store. Apparently the giant one is gone. That night we went to First Crush for dinner. I had a flight of wine which consists of three one-third sized glasses of various but complimentary wines. It was a great restaurant in terms of food, drink, atmosphere and service.

Sarah & I Pier 39The next morning we were even more the tourists when we went down to Fisherman's Wharf and Pier 39. We visited the famous wax museum and purchased multiple pounds of taffy. On the way back to the Oakland airport we got to experience a little traffic as part of the 580 freeway had collapsed the morning we arrived and was still under repair on our way out. We survived of course and I think the trip went rather well.PermalinkCommentssanfrancisco personal california sfmoma nontechnical

WordNet - Princeton University Cognitive Science Laboratory

2007 Apr 19, 3:41"WordNet is a large lexical database of English... Nouns, verbs, adjectives and adverbs are grouped into sets of cognitive synonyms (synsets), each expressing a distinct concept...The resulting network of meaningfully related words and concepts can be navPermalinkCommentsalgorithm collections etymology dictionary visualization xml language ontology tool free

Squirl: A Site for Collectors

2006 Dec 27, 3:25Another one of these crazy social collaboration deals. In this one its all about collections of arbitrary things. "Squirl is the best way to catalog, organize and share your records, movies, books, comic books, stamps, coins, or practically anything elsPermalinkCommentscollections web2.0 social collecting

Pass the POI

2006 Nov 15, 12:10Share your collections and view other's collections of mapped places via Microsoft's Live Maps.PermalinkCommentsblog microsoft map search web2.0 travel poi collections place social

RFC4198 - Federated Content URN Namespace

2006 Jun 27, 11:55This document describes a URN (Uniform Resource Name) namespace for identifying content resources within federated content collections. A federated content collection often does not have a strong centralized authority but relies upon sharedPermalinkCommentsurn uri fdc internet rfc specification
Older Entries Creative Commons License Some rights reserved.