Posts

Showing posts from July, 2012

Loading JavaScript modules via IFRAME

Another in the list of "I don't have a need for it now, but..." - this is also old hat on the Web but was new to me. I've been intrigued by the Modules proposal for ECMAScript "Harmony" . Since I like polyfills (so soothing...) I've been wondering what the subset is that could be implemented as a polyfill is, so that you can write code that runs in both ES5 and ES6 environments (i.e. the Web). I noodled around a bit with this many months ago and the key piece that was missing is sandboxed evaluation of the code, with unique "intrinsics" i.e. Object, Array, their prototypes, etc. The scenario is that you're running code like: Object.prototype.awesome = function() { return "foo"; }; But you load a third party module that one day decides to add: Object.prototype.awesome = function() { return "bar"; }; If partying on the Object prototype isn't part of the contract with the module (and it shouldn't be!...

Resource "using" in JavaScript

Based on a discussion in v8-users I was playing with a nice style for lifetime-bounded resource uses in JavaScript. I don't actually have any practical need for it at the moment, but thought I'd share. Imagine you have some type Resource that is "expensive" - typically, it holds onto something that you want to get rid of as soon as you're done using it. An example might be a file descriptor or a memory buffer. In JavaScript or C# you generally rely on garbage collection to detect that you're done with something and release the resource, but in some circumstances you want to do so explicitly, so the Resource type exposes a method release() that you can call. Perhaps, behind the scenes, this is a host object and this will cause the native resources to be freed. The contract will usually be that using Resource past this point will raise exceptions, so it assumed you know what you're doing. The basic JavaScript usage would then be: var res = new Resource...