Skip to content

Commit

Permalink
Added Object.GetOwnPropertyNames(). Closes #584.
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Jun 27, 2024
1 parent 6639a88 commit eb1f15e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
8 changes: 8 additions & 0 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
20 changes: 15 additions & 5 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
Expand All @@ -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,
Expand All @@ -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))
Expand Down

0 comments on commit eb1f15e

Please sign in to comment.