1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Improve arguments property handling

Also, fix an issue with double enumeration of arguments
This commit is contained in:
Robert Krimen 2014-01-19 21:08:05 -08:00
parent 4c1bc5857b
commit c47cf7be6e
4 changed files with 59 additions and 19 deletions

View File

@ -93,6 +93,39 @@ func TestFunctionDeclarationInFunction(t *testing.T) {
`, "function")
}
func TestArguments_defineOwnProperty(t *testing.T) {
Terst(t)
test := runTest()
test(`
var abc;
var def = true;
var ghi = {};
(function (a, b, c) {
Object.defineProperty(arguments, "0", {
value: 42,
writable: false,
enumerable: false,
configurable: false
});
Object.defineProperty(arguments, "1", {
value: 3.14,
configurable: true,
enumerable: true
});
abc = Object.getOwnPropertyDescriptor(arguments, "0");
for (var name in arguments) {
ghi[name] = (ghi[name] || 0) + 1;
if (name === "0") {
def = false;
}
}
}(0, 1, 2));
[ abc.value, abc.writable, abc.enumerable, abc.configurable, def, ghi["1"] ];
`, "42,false,false,false,true,1")
}
func TestFunction_bind(t *testing.T) {
Terst(t)

View File

@ -90,7 +90,8 @@ func init() {
objectHasOwnProperty,
argumentsDefineOwnProperty,
argumentsDelete,
argumentsEnumerate,
//argumentsEnumerate,
objectEnumerate,
//argumentsClone
objectClone,
}

View File

@ -420,6 +420,18 @@ func TestObject_defineOwnProperty(t *testing.T) {
[ abc, def ];
`, "true,false")
test(`
var object = [0, 1, 2];
Object.defineProperty(object, "0", {
value: 42,
writable: false,
enumerable: false,
configurable: false
});
var abc = Object.getOwnPropertyDescriptor(object, "0");
[ abc.value, abc.writable, abc.enumerable, abc.configurable ];
`, "42,false,false,false")
}
func Test_assignmentEvaluationOrder(t *testing.T) {

View File

@ -7,6 +7,11 @@ import (
func (runtime *_runtime) newArgumentsObject(indexOfParameterName []string, environment _environment, length int) *_object {
self := runtime.newClassObject("Arguments")
for index, _ := range indexOfParameterName {
name := strconv.FormatInt(int64(index), 10)
objectDefineOwnProperty(self, name, _property{Value{}, 0111}, false)
}
self.objectClass = _classArguments
self.value = _argumentsObject{
indexOfParameterName: indexOfParameterName,
@ -70,10 +75,11 @@ func argumentsGet(self *_object, name string) Value {
}
func argumentsGetOwnProperty(self *_object, name string) *_property {
property := objectGetOwnProperty(self, name)
if value, exists := self.value.(_argumentsObject).get(name); exists {
return &_property{value, 0111}
property.value = value
}
return objectGetOwnProperty(self, name)
return property
}
func argumentsDefineOwnProperty(self *_object, name string, descriptor _property, throw bool) bool {
@ -90,23 +96,11 @@ func argumentsDefineOwnProperty(self *_object, name string, descriptor _property
}
func argumentsDelete(self *_object, name string, throw bool) bool {
if !objectDelete(self, name, throw) {
return false
}
if _, exists := self.value.(_argumentsObject).get(name); exists {
self.value.(_argumentsObject).delete(name)
return true
}
return objectDelete(self, name, throw)
}
func argumentsEnumerate(self *_object, all bool, each func(string) bool) {
{
object := self.value.(_argumentsObject)
for index, value := range object.indexOfParameterName {
if value != "" {
if !each(strconv.FormatInt(int64(index), 10)) {
return
}
}
}
objectEnumerate(self, all, each)
}
return true
}