builtins-reflect.cc 4.53 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/builtins/builtins-utils-inl.h"
6
#include "src/builtins/builtins.h"
7
#include "src/logging/counters.h"
8
#include "src/objects/keys.h"
9
#include "src/objects/lookup.h"
10
#include "src/objects/objects-inl.h"
11
#include "src/objects/property-descriptor.h"
12 13 14 15 16 17 18 19 20 21

namespace v8 {
namespace internal {

// -----------------------------------------------------------------------------
// ES6 section 26.1 The Reflect Object

// ES6 section 26.1.3 Reflect.defineProperty
BUILTIN(ReflectDefineProperty) {
  HandleScope scope(isolate);
22
  DCHECK_LE(4, args.length());
23 24 25
  Handle<Object> target = args.at(1);
  Handle<Object> key = args.at(2);
  Handle<Object> attributes = args.at(3);
26 27 28 29 30 31 32 33 34 35 36 37 38 39

  if (!target->IsJSReceiver()) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate, NewTypeError(MessageTemplate::kCalledOnNonObject,
                              isolate->factory()->NewStringFromAsciiChecked(
                                  "Reflect.defineProperty")));
  }

  Handle<Name> name;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name,
                                     Object::ToName(isolate, key));

  PropertyDescriptor desc;
  if (!PropertyDescriptor::ToPropertyDescriptor(isolate, attributes, &desc)) {
40
    return ReadOnlyRoots(isolate).exception();
41 42
  }

43
  Maybe<bool> result = JSReceiver::DefineOwnProperty(
44
      isolate, Handle<JSReceiver>::cast(target), name, &desc, Just(kDontThrow));
45
  MAYBE_RETURN(result, ReadOnlyRoots(isolate).exception());
46 47 48 49 50 51
  return *isolate->factory()->ToBoolean(result.FromJust());
}

// ES6 section 26.1.7 Reflect.getOwnPropertyDescriptor
BUILTIN(ReflectGetOwnPropertyDescriptor) {
  HandleScope scope(isolate);
52
  DCHECK_LE(3, args.length());
53 54
  Handle<Object> target = args.at(1);
  Handle<Object> key = args.at(2);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

  if (!target->IsJSReceiver()) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate, NewTypeError(MessageTemplate::kCalledOnNonObject,
                              isolate->factory()->NewStringFromAsciiChecked(
                                  "Reflect.getOwnPropertyDescriptor")));
  }

  Handle<Name> name;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name,
                                     Object::ToName(isolate, key));

  PropertyDescriptor desc;
  Maybe<bool> found = JSReceiver::GetOwnPropertyDescriptor(
      isolate, Handle<JSReceiver>::cast(target), name, &desc);
70 71
  MAYBE_RETURN(found, ReadOnlyRoots(isolate).exception());
  if (!found.FromJust()) return ReadOnlyRoots(isolate).undefined_value();
72 73 74 75 76 77
  return *desc.ToObject(isolate);
}

// ES6 section 26.1.11 Reflect.ownKeys
BUILTIN(ReflectOwnKeys) {
  HandleScope scope(isolate);
78
  DCHECK_LE(2, args.length());
79
  Handle<Object> target = args.at(1);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

  if (!target->IsJSReceiver()) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate, NewTypeError(MessageTemplate::kCalledOnNonObject,
                              isolate->factory()->NewStringFromAsciiChecked(
                                  "Reflect.ownKeys")));
  }

  Handle<FixedArray> keys;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
      isolate, keys,
      KeyAccumulator::GetKeys(Handle<JSReceiver>::cast(target),
                              KeyCollectionMode::kOwnOnly, ALL_PROPERTIES,
                              GetKeysConversion::kConvertToString));
  return *isolate->factory()->NewJSArrayWithElements(keys);
}

// ES6 section 26.1.13 Reflect.set
BUILTIN(ReflectSet) {
  HandleScope scope(isolate);
  Handle<Object> target = args.atOrUndefined(isolate, 1);
  Handle<Object> key = args.atOrUndefined(isolate, 2);
  Handle<Object> value = args.atOrUndefined(isolate, 3);
103
  Handle<Object> receiver = args.length() > 4 ? args.at(4) : target;
104 105 106 107 108 109 110 111 112 113 114 115

  if (!target->IsJSReceiver()) {
    THROW_NEW_ERROR_RETURN_FAILURE(
        isolate, NewTypeError(MessageTemplate::kCalledOnNonObject,
                              isolate->factory()->NewStringFromAsciiChecked(
                                  "Reflect.set")));
  }

  Handle<Name> name;
  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name,
                                     Object::ToName(isolate, key));

116
  PropertyKey lookup_key(isolate, name);
117 118
  LookupIterator it(isolate, receiver, lookup_key,
                    Handle<JSReceiver>::cast(target));
119
  Maybe<bool> result = Object::SetSuperProperty(
120
      &it, value, StoreOrigin::kMaybeKeyed, Just(ShouldThrow::kDontThrow));
121
  MAYBE_RETURN(result, ReadOnlyRoots(isolate).exception());
122 123 124 125 126
  return *isolate->factory()->ToBoolean(result.FromJust());
}

}  // namespace internal
}  // namespace v8