fall - 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 Spacekatgal

2015 Nov 23, 11:40
Fallout 4 is basically a kleptomania simulator.
PermalinkComments

Tweet from David_Risney

2015 Jun 14, 9:53
Fallout4 on XB1 supports PC mods. Due to platform similarity? Possible for PS4? https://twitter.com/Polygon/status/610494038685417472 …
PermalinkComments

Tweet from David_Risney

2015 Apr 12, 10:39
Does 'charset=utf8' work anywhere? Or do other browsers fallback to UTF-8 just giving the appearance? @ericlaw http://wp.me/p60i9o-r 
PermalinkComments

Stripe CTF - Level 5

2012 Sep 11, 5:00

Level 5 of the Stripe CTF revolved around a design issue in an OpenID like protocol.

Code

    def authenticated?(body)
body =~ /[^\w]AUTHENTICATED[^\w]*$/
end

...

if authenticated?(body)
session[:auth_user] = username
session[:auth_host] = host
return "Remote server responded with: #{body}." \
" Authenticated as #{username}@#{host}!"

Issue

This level is an implementation of a federated identity protocol. You give it an endpoint URI and a username and password, it posts the username and password to the endpoint URI, and if the response is 'AUTHENTICATED' then access is allowed. It is easy to be authenticated on a server you control, but this level requires you to authenticate from the server running the level. This level only talks to stripe CTF servers so the first step is to upload a document to the level 2 server containing the text 'AUTHENTICATED' and we can now authenticate on a level 2 server. Notice that the level 5 server will dump out the content of the endpoint URI and that the regexp it uses to detect the text 'AUTHENTICATED' can match on that dump. Accordingly I uploaded an authenticated file to

https://level02-2.stripe-ctf.com/user-ajvivlehdt/uploads/authenticated
Using that as my endpoint URI means authenticating as level 2. I can then choose the following endpoint URI to authenticate as level 5.
https://level05-1.stripe-ctf.com/user-qtoyekwrod/?pingback=https%3A%2F%2Flevel02-2.stripe-ctf.com%2Fuser-ajvivlehdt%2Fuploads%2Fauthenticated&username=a&password=a
Navigating to that URI results in the level 5 server telling me I'm authenticated as level 2 and lists the text of the level 2 file 'AUTHENTICATED'. Feeding this back into the level 5 server as my endpoint URI means level 5 seeing 'AUTHENTICATED' coming back from a level 5 URI.

Notes

I didn't see any particular code review red flags, really the issue here is that the regular expression testing for 'AUTHENTICATED' is too permisive and the protocol itself doesn't do enough. The protocol requires only a set piece of common literal text to be returned which makes it easy for a server to accidentally fall into authenticating. Having the endpoint URI have to return variable text based on the input would make it much harder for a server to accidentally authenticate.

PermalinkCommentsinternet openid security stripe-ctf technical web

Everybody hates Firefox updates - Evil Brain Jono's Natural Log

2012 Jul 16, 1:59

Former FireFox developer on the switch to their continuous update cycle. 

Oh no, Chrome is doing such-and-such; we’d better do something equivalent or we’ll fall behind! We thought we needed a rapid update process like Chrome. We were jealous of their rapid update capability, which let them deploy improvements to users continuously. We had to “catch up” with Chrome’s updating capability.

Dealing with servicing on IE for years had led me to some of the same thoughts when I heard FireFox was switching to continuous updates.

PermalinkCommentsfirefox via:ericlaw web-browser technical web browser servicing update software

Kalle Mattson - Water Falls (Official Video) / HYPNO SF (by...

2012 Jul 13, 6:46


Kalle Mattson - Water Falls (Official Video) / HYPNO SF (by KalleMattson)

Some wonderful sequences in this video!

PermalinkCommentssan-francisco music video music-video

Slow Jam The News with Barack Obama: Late Night with Jimmy...

2012 Apr 25, 3:45


Slow Jam The News with Barack Obama: Late Night with Jimmy Fallon (by latenight)

PermalinkCommentshumor president jimmy-fallon barack-obama loan

Fallout: Nuka Break

2011 Feb 3, 12:57PermalinkCommentsfallout video

The Vault, the Fallout wiki - Fallout: New Vegas and more

2010 Nov 21, 11:07PermalinkCommentsfallout fallout4 wiki new-vegas videogame game video-game

YouTube - Fallout New Vegas E3 2010 Official Trailer

2010 Aug 14, 3:29PermalinkCommentsvideo fallout fallout4 videogame avlater

Hitler tries a DMCA takedown | Brad Ideas

2010 Apr 21, 1:47So... There's Downfall a 2004 film about the final days of Hitler's life. Then folks take the most dramatic scene and parody it with new subtitles having Hitler yell about various things like his cell phone or Burning Man. It becomes a meme and meta Downfall parodies show up with Hitler yelling about the Downfall parodies. Now the studio producing the film has sent DMCA takedown notices to Youtube and many of the videos are disappearing. In response is a new Downfall parody in which Hitler issues DMCA notices to Youtube...PermalinkCommentscensorship hitler humor copyright dmca eff legal youtube video fairuse meme web internet technical

Atlantis Artificial Water Fall

2010 Feb 28, 11:35

sequelguy posted a photo:

Atlantis Artificial Water Fall

PermalinkCommentswaterfall bahamas nassau atlantisresort

McSweeney's Internet Tendency: It's Decorative Gourd Season, Motherf*rs.

2009 Oct 27, 6:16It's Decorative Gourd Season, Motherf*rs, By COLIN NISSANPermalinkCommentshumor satire halloween gourd pumpkin fall essay

Sarah's Fallout 3 Bear Bed

2009 Sep 27, 11:28

sequelguy posted a photo:

Sarah's Fallout 3 Bear Bed

Sarah's Fallout 3 teddy bear collection in Megaton.

PermalinkCommentssarah teddybear megaton fallout3

Are Violent Video Games Adequately Preparing Children For The Apocalypse? | The Onion - America's Finest News Source

2009 Jul 10, 7:31"72% of kids said they know how to find items to barter at weapon shops and how to use medicine packs to heal zombie bites"PermalinkCommentshumor video internet videogames onion parody apocalypse fallout3 zombie

Video: Project Natal invades Late Night with Jimmy Fallon

2009 Jun 12, 12:37"Last night on Late Night with Jimmy Fallon, Microsoft's Kudo Tsunoda brought along his baby, Project Natal, and let Jimmy Fallon, John Krasinski, and Stephen Moyer go to town. The footage has made its way onto Hulu and while these are pretty much the same demos for Ricochet and Burnout Paradise that we saw at E3 last week, they're still impressive."PermalinkCommentsvideo humor videogame natal xbox360 jimmy-fallon

Foruauto Part-III: introducing Fallout 3, the 70s Japanese cop-drama - Offworld

2009 May 19, 2:09"Today's other best Fallout 3 development: Japan's 'agoministrator' re-imagines the game as a 70s TV drama"PermalinkCommentsfor:hellosarah fallout3 video humor tv

Fallout 3 'Broken Steel' DLC Preview - Shacknews - PC Games, PlayStation, Xbox 360 and Wii video game news, previews and downloads

2009 Apr 21, 1:28Fallout 3's May 5th DLC removes old ending, adds new quests, new levels, new perks. Sounds good! "In a nutshell, Broken Steel will remove the game's ending entirely, with Bethesda's Pete Hines saying simply to fans that called for an open-ended resolution, "We got the idea." Players will still have to make the final choice, but following that climax the game will continue, presenting new epilogue quests, another 10 levels to gain, and new perks, monsters and achievements to keep the climb interesting."PermalinkCommentsgame videogame news fallout3 fallout

Time Travel Essentials - TopatoCo: We Sell T-shirts by the e-Shore

2009 Apr 15, 7:42"If you're like us, you live in constant fear of slipping into a wormhole and getting spit out in the 13th century, and the only real useful knowledge you have for your ignorant ancestors is 'watch out for that Hitler guy' and 'some of the Popes are evil.'" Its like they're inside my head! Love this shirt and poster telling you everything you need to know in case you accidentally fall back in time.PermalinkCommentstime-travel time shirt poster wishlist gift awesome
Older Entries Creative Commons License Some rights reserved.