Skip to content

Instantly share code, notes, and snippets.

@ryzokuken
Created August 27, 2018 20:37
Show Gist options
  • Save ryzokuken/78ef8786748861727402a359d9d6f039 to your computer and use it in GitHub Desktop.
Save ryzokuken/78ef8786748861727402a359d9d6f039 to your computer and use it in GitHub Desktop.

Proposed method for bound prototype methods

Sample method, Collator.prototype.compare (https://tc39.github.io/ecma402/#sec-intl.collator.prototype.compare)

  1. Let collator be this value.
  2. If Type(collator) is not Object, throw a TypeError exception.
  3. If collator does not have an [[InitializedCollator]] internal slot, throw a TypeError exception.
  4. If collator.[[BoundCompare]] is undefined, then a. Let F be a new built-in function object as defined in 10.3.3.1. b. Set F.[[Collator]] to collator. c. Set collator.[[BoundCompare]] to F.
  5. Return collator.[[BoundCompare]].

Similar to NumberFormat.prototype.format and DateTimeFormat.prototype.format.

Patterns:

  1. The object to be operated on is assumed to be the this value.
  2. If the object isn't a valid object, throw a TypeError.
  3. If the object has a valid unwrap method, then x = UnwrapExpectedTypeOfX(x), otherwise throw a TypeError if it isn't already a valid object of said type.
  4. If the method doesn't yet exist (it's a bound method stored in an internal slot), make a new function as required from the instruction set pertaining to said method, and set the internal slot to the newly-made function.
  5. Return the method.

Proposed Definition

AddBoundMethod(cls, method)

  1. Let obj be the this value.
  2. If Type(obj) is not Object, throw a TypeError exception.
  3. If cls has an associated Unwrap method, obj = cls.Unwrap(obj), otherwise if cls.Valid(obj) is false, throw a TypeError exception. (Note: This is because there might be different conditions for each class of objects to be valid. In v8, internal slots aren't used for this as it's implemented in a typed environment (C++)).
  4. If obj does not have method stored in an internal slot (called Bound || method.name in this case), a. Initialize a new method of type method. If the method name is passed, another argument should be passed that could be used to construct said method, otherwise method could be an object somewhat like { name: 'xyz', getInstance: function() {...}). b. Store a reference to obj inside the newly-instantiated method. c. Set the internal slot under question to the newly-instantiated method.
  5. Return the value stored at the said internal slot.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment