Skip to content

Instantly share code, notes, and snippets.

@bterlson
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bterlson/10077836 to your computer and use it in GitHub Desktop.
Save bterlson/10077836 to your computer and use it in GitHub Desktop.
Tweak unshift to only process elements when no arguments are passed (See new step 7). Note that all implementations still assign length when no arguments are passed.
1. Let O be the result of calling ToObject passing the this value as the argument.
2. ReturnIfAbrupt(O).
3. Let lenVal be the result of Get(O, "length")
4. Let len be ToLength(lenVal).
5. ReturnIfAbrupt(len).
6. Let argCount be the number of actual arguments.
7. If argCount > 0 then
a. Let k be len.
b. Repeat, while k > 0,
i. Let from be ToString(k–1).
ii. Let to be ToString(k+argCount –1).
iii. Let fromPresent be the result of HasProperty(O, from).
iv. ReturnIfAbrupt(fromPresent).
v. If fromPresent is true, then
1.Let fromValue be the result of Get(O, from).
2. ReturnIfAbrupt(fromValue).
3. Let putStatus be the result of Put(O, to, fromValue, true).
4. ReturnIfAbrupt(putStatus).
vi. Else fromPresent is false,
1. Let deleteStatus be the result of DeletePropertyOrThrow(O, to).
2. ReturnIfAbrupt(deleteStatus).
vii. Decrease k by 1.
c. Let j be 0.
d. Let items be a List whose elements are, in left to right order, the arguments that were passed to this function invocation.
e. Repeat, while items is not empty
i. Remove the first element from items and let E be the value of that element.
ii. Let putStatus be the result of Put(O, ToString(j), E, true).
iii. ReturnIfAbrupt(putStatus).
iv. Increase j by 1.
8. Let putStatus be the result of Put(O, "length", len+argCount, true).
9. ReturnIfAbrupt(putStatus).
10. Return len+argCount.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment