07.09.10

Now in SpiderMonkey and Firefox: ES5‘s Function.prototype.bind

This is just a brief note to point out that, as of the August 29th Firefox nightly (and I think as of the latest beta, but don’t quote me), SpiderMonkey (and Firefox) now implements ES5‘s new Function.prototype.bind method — native support for creating functions bound to a pre-specified this value:

var property = 42;
var obj =
  {
    property: 17,
    method: function() { return this.property; }
  };

var bound = obj.method.bind(obj);
assertEq(bound(), 17);

…or with pre-specified leading arguments:

function multiply()
{
  var product = 1;
  for (var i = 0, sz = arguments.length; i < sz; i++)
    product *= arguments[i];
  return product;
}

var productTimesFive = multiply.bind(null /* this */, 5);
assertEq(productTimesFive(4, 3, 2, 1), 120);

…and, in a feature present only in the ES5 bind implementation (and not in any of the numerous precursors), they even work with new:

function Vector()
{
  var args = arguments;
  this.length = arguments.length;
  this.get = function(i) { return args[i]; };
  this.set = function(i, v) { args[i] = v; };
}

var PartialVector = Vector.bind(null /* this, ignored with new */, 3, 7);

var threeSevenTen = new PartialVector(10); // new Vector(3, 7, 10)

For more information, see the article on Function.prototype.bind on MDC. For the utmost information, see the ES5 specification for the method.

As always, you can experiment with a version of Firefox with Function.prototype.bind by downloading a nightly from nightly.mozilla.org. (Don’t forget to use the profile manager if you want to keep the settings you use with your primary Firefox installation pristine.)

26.08.10

For future reference when debugging inside JS modules (where no dump function is available)

Tags: , , , , , , , — Jeff @ 19:59
function dump(msg)
{
  var mod = {};
  Components.utils.import("resource://gre/modules/ctypes.jsm", mod);
  var ctypes = mod.ctypes;
  var libc = ctypes.open("libc.so.6");
  var puts =
    libc.declare("puts", ctypes.default_abi, ctypes.int, ctypes.char.ptr);
  puts("!!!!!!!!!!!!!!!!!!!\n" +
       "!!! " + msg + "\n" +
       "!!!!!!!!!!!!!!!!!!!");
  libc.close();
}

22.08.10

Incompatible ES5 change: literal getter and setter functions must now have exactly zero or one arguments

ECMAScript accessor syntax in SpiderMonkey

For quite some time SpiderMonkey and Mozilla-based browsers have supported user-defined getter and setter functions (collectively, accessors), both programmatically and syntactically. The syntaxes for accessors were once legion, but SpiderMonkey has pared them back almost to the syntax recently codified in ES5 (and added new syntax where required by ES5).

// All valid in ES5
var a = { get x() { } };
var b = { get "y"() { } };
var c = { get 2() { } };

var e = { set x(v) { } };
var f = { set "y"(v) { } };
var g = { set 2(v) { } };

SpiderMonkey has historically parsed literal accessors using a slightly-tweaked version of its function parsing code. Therefore, as previously explained SpiderMonkey would accept essentially anything which could follow function in a function expression as valid accessor syntax in object literals.

ES5 requires accessors have exact numbers of arguments

A consequence of parsing accessors using generalized function parsing is that SpiderMonkey accepted some nonsensicalities, such as no-argument setters or multiple-argument getters or setters:

var o1 = { get p(a, b, c, d, e, f, g) { /* why have any arguments? */ } };
var o2 = { set p() { /* to what value? */ } };
var o3 = { set p(a, b, c) { /* why more than one? */ } };

ES5 accessor syntax sensibly deems such constructs errors: a conforming ES5 implementation would reject all of the above statements.

SpiderMonkey is changing to follow ES5: getters require no arguments, setters require one argument

SpiderMonkey has now been changed to follow ES5. There seemed little to no gain in continuing to support bizarre numbers of arguments when the spec counseled otherwise, and any code which does end up broken is easily fixed.

As always, you can experiment with a version of Firefox with these changes to accessor syntax by downloading a nightly from nightly.mozilla.org. (Don’t forget to use the profile manager if you want to keep the settings you use with your primary Firefox installation pristine.)

06.08.10

Dear Automobile magazine

I suppose I should appreciate inexplicably being entered in your subscriber database since the May 2010 issue.

I suppose I should appreciate being sent glossy pages full of pictures of beautiful new cars. I do enjoy slick cars, after all (although to be honest, I’d take a classic car any day over something new). (Still, I much prefer to see the physical versions over mere pictures.)

But that doesn’t change the fact that I am perhaps the least likely person to ever succumb to the temptations posed most directly by the cars in your pages, or the accessories of all sorts advertised amongst them. I am a complete losing proposition for you: I don’t pay for your magazine, and I won’t pay your advertisers for it, either.

Maybe you think, because I’m in the 18-24 male demographic, the products in your pages will entice me. I think you will find few people so anomalous as me in that population. I don’t own a car (I bike), I rarely need a car (and on those occasions, borrowing or renting is significantly cheaper), and I don’t plan to own a car in the foreseeable future. And, for as long as I live in the Bay Area, that’s very unlikely to change.

I do appreciate your willingness to send me something for nothing. My office appreciates this, too. (Or at least it appreciates it no less than I do.)

But a friendly suggestion: if you really want to give someone a free subscription to your magazine, give it to someone who might actually read it.

(And in the unlikely event that someone decided to give me a subscription [presumably despite knowing just how little I care about cars] and I somehow missed the new-gift-subscription notice, I’m really sorry, but this magazine just isn’t for me: it’s complete deadweight loss. How about in the future we go do something fun together, mutually agreed upon, instead?)

18.06.10

« NewerOlder »