Skip to content

Commit

Permalink
Array unshift
Browse files Browse the repository at this point in the history
  • Loading branch information
mjc1283 committed Aug 7, 2011
1 parent 958066d commit efa5c4f
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions envs/es5.env
Expand Up @@ -822,7 +822,8 @@ let [[%RangeErrorGlobalFuncObj]] =

{%RangeErrorProto["constructor" = %RangeErrorGlobalFuncObj]}

let [[%ArrayProto]] = { [#proto : %ObjectProto, #class : "Array",] }
let [[%ArrayProto]] = { [#proto : %ObjectProto, #class : "Array",]
"length" : {#value 0, #writable true} }

let [[%ToUint32]] = func(n) {
let (number = %ToNumber(n))
Expand Down Expand Up @@ -1506,6 +1507,53 @@ let [[%splice]] = {[#code : %splicelambda, #proto : %FunctionProto,]}
{[] "value" : {#value %splice, #writable true},
"configurable" : {#value true, #writable true}}) }

let [[%unshiftlambda]] = func(this, args) {
let (O = %ToObject(this))
let (lenVal = O["length"])
let (len = %ToUint32(lenVal))
let (argCount = args["length"])

rec (Oloop = func(k) {
if (prim(">", k, 0)) {
let (from = %ToString(prim("-", k, 1)))
let (to = %ToString(prim("+", k, prim("-", argCount, 1))))
if (prim("hasProperty", O, from)) {
O[to = O[from] ];
Oloop(prim("-", k, 1))
} else {
O[delete to];
Oloop(prim("-", k, 1))
}
} else { undefined }
})
Oloop(len);

let (end = args["length"])
rec (argsLoop = func(argsIndex, j) {
if (prim("<", argsIndex, end)) {
O[%ToString(j) = args[prim("prim->str", argsIndex)] ];
argsLoop(prim("+", argsIndex, 1), prim("+", j, 1))
} else { undefined }
})
argsLoop(0, 0);

let (finalLen = prim("+", len, argCount))
O["length" = finalLen];
finalLen
}

let [[%unshift]] = {[#code : %unshiftlambda, #proto : %FunctionProto,]}

{%defineOwnProperty(%unshift,
"length",
{[] "value" : {#value 1, #writable true},
"enumerable" : {#value false, #writable true}}) }

{%defineOwnProperty(%ArrayProto,
"unshift",
{[] "value" : {#value %unshift, #writable true},
"configurable" : {#value true, #writable true}}) }

let [[%slicelambda]] = func(this, args) {
let (O = %ToObject(this))
let (A = {[#proto : %ArrayProto, #class : "Array",]})
Expand Down Expand Up @@ -1783,15 +1831,15 @@ let [[%set-property]] = func(obj, fld, val) {
}
})
let (setArrayField = func() {
obj[fld = val];
if (isArrayIndex()) {
let (uint = %ToUint32(fld))
let (len = obj[fld])
if (prim("<", len, prim("+", uint, 1))) {
obj["length" = prim("+", uint, 1)]
} else { undefined }
}
else { undefined };
obj[fld = val]
else { undefined }
})
if (prim("get-class", obj) === "Array") {
setArrayField()
Expand Down

0 comments on commit efa5c4f

Please sign in to comment.