ca page 7 - 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

Retweet of neiltyson

2016 Jan 13, 6:42
With Lotteries funding State education budgets, the best way to sustain them is to NOT teach probability&statistics in school
PermalinkComments

Retweet of aragusea

2016 Jan 12, 10:36
Is the @StarTrek TNG episode "Darmok" linguistically implausible? I and @drpizza have it out. https://storify.com/aragusea/darmok  #important
PermalinkComments

Retweet of codepo8

2016 Jan 12, 12:53
Chewbacca... his arms open. pic.twitter.com/fABwocHCOu
PermalinkComments

Retweet of kumailn

2016 Jan 11, 7:30
I got to be in an ep of @thexfiles. While shooting late I got to record a casual convo w @GillianA & @davidduchovny. http://www.feralaudio.com/55-kumail-talks-to-david-duchovny-and-gillian-anderson-on-the-set-of-the-x-files/ …
PermalinkComments

Tweet from David_Risney

2016 Jan 7, 9:31
Firefox to remove appcache offline support. https://www.fxsitecompat.com/en-US/docs/2016/application-cache-support-will-be-removed/ … https://twitter.com/FxSiteCompat/status/685332720281645056 …
PermalinkComments

Tweet from David_Risney

2016 Jan 7, 9:30
'Works "produced by nature, animals, or plants" cannot be granted copyright protection the US Copyright Office said' http://arstechnica.com/tech-policy/2016/01/judge-says-monkey-cannot-own-copyright-to-famous-selfies/ …
PermalinkComments

Retweet of rcallimachi

2016 Jan 7, 2:51
Wow: When is the last time a sitting American president penned an OpEd in the New York Times? Barack Obama just did http://mobile.nytimes.com/2016/01/08/opinion/president-barack-obama-guns-are-our-shared-responsibility.html?emc=edit_na_20160107&nlid=69115476&ref=cta&_r=0&referer= …
PermalinkComments

Retweet of TipoTipos

2016 Jan 4, 12:12
Someday, someone will think deeply about the use value of social media and invent something better. It will be called RSS.
PermalinkComments

Retweet of FakeUnicode

2016 Jan 3, 10:26
I mean, look at this crap. All these full-alphabet variants "Because math!" @asrivkin @PlanetDr @lukedones @grierja pic.twitter.com/qQ1POiqniv
PermalinkComments

Retweet of justinkan

2016 Jan 3, 8:57
This seems like the way car ownership declines https://news.ycombinator.com/item?id=10837169 … pic.twitter.com/qexuUwXuj5
PermalinkComments

Retweet of AlecMuffett

2016 Jan 1, 10:04
Why do I want to move my blog to HTTPS + optional Onion? Because my Mobile ISP transparently MITMs all HTTP: pic.twitter.com/JII79lzQvx
PermalinkComments

Retweet of TheOnion

2015 Dec 30, 1:00
"We present it to you with the utmost respect and deference." http://onion.com/1NVUlBC  pic.twitter.com/NPQQA8GsoO
PermalinkComments

Tweet from David_Risney

2015 Dec 24, 5:53
Playing Guess Who with 4yo. First turn after explaining rules: "Daddy did you pick the birthday cake, like I did?"
PermalinkComments

Tweet from David_Risney

2015 Dec 23, 9:23
Doctor Who rerun on after watching Jessica Jones. The Doctor suddenly seems so sinister. Don't read the psychic paper!
PermalinkComments

Tweet from David_Risney

2015 Dec 11, 3:14
Big drones with nets capture smaller drones in Tokyo no-fly zone. But where does it end?! http://arstechnica.com/tech-policy/2015/12/tokyos-drone-squad-will-deploy-10ft-drones-armed-with-nets-to-police-the-sky/ …
PermalinkComments

Retweet of JustRogDigiTec

2015 Dec 10, 9:29
Diversity and equality in the tech industry? Sign me up! I have capacity to mentor people who are minorities in tech https://twitter.com/ricomariani/status/675192468774756352 …
PermalinkComments

Retweet of JustinRoiland

2015 Dec 6, 9:16
Everyone is aware of this right? @DoubleFine announced #Psychonauts2 psychonauts2 https://www.fig.co/campaigns/psychonauts-2 …
PermalinkComments

Retweet of waxpancake

2015 Dec 3, 10:39
So excited to see @TimOfLegend and the @DoubleFine crew working on Psychonauts 2: https://www.fig.co/campaigns/psychonauts-2 … Can't wait to see the documentary.
PermalinkComments

Retweet of ClickHole

2015 Nov 30, 9:29
Amazing! How many times can @neiltyson win the internet? http://www.clickhole.com/r/3565tsd  pic.twitter.com/6Dgm8oE2YV
PermalinkComments
Older EntriesNewer Entries Creative Commons License Some rights reserved.