Skip to content

Commit

Permalink
Fixed accessor and property key function names. Fixes dop251#314.
Browse files Browse the repository at this point in the history
Signed-off-by: Gabri <gabriele.cimato@gmail.com>
  • Loading branch information
dop251 authored and Gabri3l committed Jan 24, 2022
1 parent 9cf7f40 commit 67780b8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
2 changes: 0 additions & 2 deletions builtin_symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ func (r *Runtime) builtin_symbol(call FunctionCall) Value {
var desc valueString
if arg := call.Argument(0); !IsUndefined(arg) {
desc = arg.toString()
} else {
desc = stringEmpty
}
return newSymbol(desc)
}
Expand Down
4 changes: 2 additions & 2 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (o *baseObject) deleteIdx(idx valueInt, throw bool) bool {
func (o *baseObject) deleteSym(s *Symbol, throw bool) bool {
if o.symValues != nil {
if val := o.symValues.get(s); val != nil {
if !o.checkDelete(s.desc.string(), val, throw) {
if !o.checkDelete(s.descriptiveString().string(), val, throw) {
return false
}
o.symValues.remove(s)
Expand Down Expand Up @@ -756,7 +756,7 @@ func (o *baseObject) defineOwnPropertySym(s *Symbol, descr PropertyDescriptor, t
if o.symValues != nil {
existingVal = o.symValues.get(s)
}
if v, ok := o._defineOwnProperty(s.desc.string(), existingVal, descr, throw); ok {
if v, ok := o._defineOwnProperty(s.descriptiveString().string(), existingVal, descr, throw); ok {
if o.symValues == nil {
o.symValues = newOrderedMap(nil)
}
Expand Down
60 changes: 60 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,66 @@ func TestDestructSymbol(t *testing.T) {
testScript1(TESTLIBX+SCRIPT, _undefined, t)
}

func TestAccessorFuncName(t *testing.T) {
const SCRIPT = `
const namedSym = Symbol('test262');
const emptyStrSym = Symbol("");
const anonSym = Symbol();
const o = {
get id() {},
get [anonSym]() {},
get [namedSym]() {},
get [emptyStrSym]() {},
set id(v) {},
set [anonSym](v) {},
set [namedSym](v) {},
set [emptyStrSym](v) {}
};
let prop;
prop = Object.getOwnPropertyDescriptor(o, 'id');
assert.sameValue(prop.get.name, 'get id');
assert.sameValue(prop.set.name, 'set id');
prop = Object.getOwnPropertyDescriptor(o, anonSym);
assert.sameValue(prop.get.name, 'get ');
assert.sameValue(prop.set.name, 'set ');
prop = Object.getOwnPropertyDescriptor(o, emptyStrSym);
assert.sameValue(prop.get.name, 'get []');
assert.sameValue(prop.set.name, 'set []');
prop = Object.getOwnPropertyDescriptor(o, namedSym);
assert.sameValue(prop.get.name, 'get [test262]');
assert.sameValue(prop.set.name, 'set [test262]');
`
testScript1(TESTLIB+SCRIPT, _undefined, t)
}

func TestCoverFuncName(t *testing.T) {
const SCRIPT = `
var namedSym = Symbol('');
var anonSym = Symbol();
var o;
o = {
xId: (0, function() {}),
id: (function() {}),
id1: function x() {},
[anonSym]: (function() {}),
[namedSym]: (function() {})
};
assert(o.xId.name !== 'xId');
assert.sameValue(o.id1.name, 'x');
assert.sameValue(o.id.name, 'id', 'via IdentifierName');
assert.sameValue(o[anonSym].name, '', 'via anonymous Symbol');
assert.sameValue(o[namedSym].name, '[]', 'via Symbol');
`
testScript1(TESTLIB+SCRIPT, _undefined, t)
}

/*
func TestArrayConcatSparse(t *testing.T) {
function foo(a,b,c)
Expand Down
32 changes: 27 additions & 5 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,11 +996,17 @@ func (s *Symbol) ToString() Value {
}

func (s *Symbol) String() string {
return s.desc.String()
if s.desc != nil {
return s.desc.String()
}
return ""
}

func (s *Symbol) string() unistring.String {
return s.desc.string()
if s.desc != nil {
return s.desc.string()
}
return ""
}

func (s *Symbol) ToFloat() float64 {
Expand Down Expand Up @@ -1078,10 +1084,26 @@ func NewSymbol(s string) *Symbol {
}

func (s *Symbol) descriptiveString() valueString {
if s.desc == nil {
return stringEmpty
desc := s.desc
if desc == nil {
desc = stringEmpty
}
return asciiString("Symbol(").concat(desc).concat(asciiString(")"))
}

func funcName(prefix string, n Value) valueString {
var b valueStringBuilder
b.WriteString(asciiString(prefix))
if sym, ok := n.(*Symbol); ok {
if sym.desc != nil {
b.WriteRune('[')
b.WriteString(sym.desc)
b.WriteRune(']')
}
} else {
b.WriteString(n.toString())
}
return asciiString("Symbol(").concat(s.desc).concat(asciiString(")"))
return b.String()
}

func init() {
Expand Down
10 changes: 5 additions & 5 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ func (_setElem1Named) exec(vm *vm) {
propName := vm.stack[vm.sp-2]
val := vm.stack[vm.sp-1]
vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
Value: propName,
Value: funcName("", propName),
Configurable: FLAG_TRUE,
}, true)
obj.setOwn(propName, val, true)
Expand Down Expand Up @@ -1510,7 +1510,7 @@ func (s setPropGetter) exec(vm *vm) {
obj := vm.r.toObject(vm.stack[vm.sp-2])
val := vm.stack[vm.sp-1]
vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
Value: asciiString("get ").concat(stringValueFromRaw(val.string())),
Value: asciiString("get ").concat(stringValueFromRaw(unistring.String(s))),
Configurable: FLAG_TRUE,
}, true)

Expand All @@ -1533,7 +1533,7 @@ func (s setPropSetter) exec(vm *vm) {
val := vm.stack[vm.sp-1]

vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
Value: asciiString("set ").concat(stringValueFromRaw(val.string())),
Value: asciiString("set ").concat(stringValueFromRaw(unistring.String(s))),
Configurable: FLAG_TRUE,
}, true)

Expand All @@ -1558,7 +1558,7 @@ func (s _setPropGetter1) exec(vm *vm) {
propName := vm.stack[vm.sp-2]
val := vm.stack[vm.sp-1]
vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
Value: asciiString("get ").concat(stringValueFromRaw(val.string())),
Value: funcName("get ", propName),
Configurable: FLAG_TRUE,
}, true)

Expand All @@ -1584,7 +1584,7 @@ func (s _setPropSetter1) exec(vm *vm) {
val := vm.stack[vm.sp-1]

vm.r.toObject(val).self.defineOwnPropertyStr("name", PropertyDescriptor{
Value: asciiString("set ").concat(stringValueFromRaw(val.string())),
Value: funcName("set ", propName),
Configurable: FLAG_TRUE,
}, true)

Expand Down

0 comments on commit 67780b8

Please sign in to comment.