Explorar o código

Fixed accessor and property key function names. Fixes #314.

Dmitry Panov %!s(int64=4) %!d(string=hai) anos
pai
achega
4c062c2b35
Modificáronse 5 ficheiros con 94 adicións e 14 borrados
  1. 0 2
      builtin_symbol.go
  2. 2 2
      object.go
  3. 60 0
      runtime_test.go
  4. 27 5
      value.go
  5. 5 5
      vm.go

+ 0 - 2
builtin_symbol.go

@@ -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)
 }

+ 2 - 2
object.go

@@ -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)
@@ -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)
 		}

+ 60 - 0
runtime_test.go

@@ -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)

+ 27 - 5
value.go

@@ -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 {
@@ -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() {

+ 5 - 5
vm.go

@@ -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)
@@ -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)
 
@@ -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)
 
@@ -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)
 
@@ -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)