Преглед изворни кода

Added Object.GetOwnPropertyNames(). Closes #584.

Dmitry Panov пре 1 година
родитељ
комит
eb1f15ee67
3 измењених фајлова са 25 додато и 7 уклоњено
  1. 8 0
      object_test.go
  2. 2 2
      runtime.go
  3. 15 5
      value.go

+ 8 - 0
object_test.go

@@ -495,6 +495,14 @@ func ExampleObject_Delete() {
 	// Output: before: true, after: <nil>
 }
 
+func ExampleObject_GetOwnPropertyNames() {
+	vm := New()
+	obj := vm.NewObject()
+	obj.DefineDataProperty("test", vm.ToValue(true), FLAG_TRUE, FLAG_TRUE, FLAG_FALSE) // define a non-enumerable property that Keys() would not return
+	fmt.Print(obj.GetOwnPropertyNames())
+	// Output: [test]
+}
+
 func TestObjectEquality(t *testing.T) {
 	type CustomInt int
 	type S struct {

+ 2 - 2
runtime.go

@@ -2366,7 +2366,7 @@ func (r *Runtime) Set(name string, value interface{}) error {
 // Equivalent to dereferencing a variable by name in non-strict mode. If variable is not defined returns nil.
 // Note, this is not the same as GlobalObject().Get(name),
 // because if a global lexical binding (let or const) exists, it is used instead.
-// This method will panic with an *Exception if a JavaScript exception is thrown in the process.
+// This method will panic with an *Exception if a JavaScript exception is thrown in the process. Use Runtime.Try to catch these.
 func (r *Runtime) Get(name string) Value {
 	n := unistring.NewFromString(name)
 	if v, exists := r.global.stash.getByName(n); exists {
@@ -3141,7 +3141,7 @@ func assertCallable(v Value) (func(FunctionCall) Value, bool) {
 }
 
 // InstanceOf is an equivalent of "left instanceof right".
-// This method will panic with an *Exception if a JavaScript exception is thrown in the process.
+// This method will panic with an *Exception if a JavaScript exception is thrown in the process. Use Runtime.Try to catch these.
 func (r *Runtime) InstanceOf(left Value, right *Object) (res bool) {
 	return instanceOfOperator(left, right)
 }

+ 15 - 5
value.go

@@ -772,7 +772,7 @@ func (o *Object) baseObject(*Runtime) *Object {
 //
 // In all other cases returns own enumerable non-symbol properties as map[string]interface{}.
 //
-// This method will panic with an *Exception if a JavaScript exception is thrown in the process.
+// This method will panic with an *Exception if a JavaScript exception is thrown in the process. Use Runtime.Try to catch these.
 func (o *Object) Export() interface{} {
 	return o.self.export(&objectExportCtx{})
 }
@@ -787,20 +787,20 @@ func (o *Object) hash(*maphash.Hash) uint64 {
 }
 
 // Get an object's property by name.
-// This method will panic with an *Exception if a JavaScript exception is thrown in the process.
+// This method will panic with an *Exception if a JavaScript exception is thrown in the process. Use Runtime.Try to catch these.
 func (o *Object) Get(name string) Value {
 	return o.self.getStr(unistring.NewFromString(name), nil)
 }
 
 // GetSymbol returns the value of a symbol property. Use one of the Sym* values for well-known
 // symbols (such as SymIterator, SymToStringTag, etc...).
-// This method will panic with an *Exception if a JavaScript exception is thrown in the process.
+// This method will panic with an *Exception if a JavaScript exception is thrown in the process. Use Runtime.Try to catch these.
 func (o *Object) GetSymbol(sym *Symbol) Value {
 	return o.self.getSym(sym, nil)
 }
 
 // Keys returns a list of Object's enumerable keys.
-// This method will panic with an *Exception if a JavaScript exception is thrown in the process.
+// This method will panic with an *Exception if a JavaScript exception is thrown in the process. Use Runtime.Try to catch these.
 func (o *Object) Keys() (keys []string) {
 	iter := &enumerableIter{
 		o:       o,
@@ -813,8 +813,18 @@ func (o *Object) Keys() (keys []string) {
 	return
 }
 
+// GetOwnPropertyNames returns a list of all own string properties of the Object, similar to Object.getOwnPropertyNames()
+// This method will panic with an *Exception if a JavaScript exception is thrown in the process. Use Runtime.Try to catch these.
+func (o *Object) GetOwnPropertyNames() (keys []string) {
+	for item, next := o.self.iterateStringKeys()(); next != nil; item, next = next() {
+		keys = append(keys, item.name.String())
+	}
+
+	return
+}
+
 // Symbols returns a list of Object's enumerable symbol properties.
-// This method will panic with an *Exception if a JavaScript exception is thrown in the process.
+// This method will panic with an *Exception if a JavaScript exception is thrown in the process. Use Runtime.Try to catch these.
 func (o *Object) Symbols() []*Symbol {
 	symbols := o.self.symbols(false, nil)
 	ret := make([]*Symbol, len(symbols))