reflect.tq 3.83 KB
Newer Older
1 2 3 4 5 6
// Copyright 2019 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.

namespace reflect {
  // ES6 section 26.1.10 Reflect.isExtensible
7
  transitioning javascript builtin
8 9
  ReflectIsExtensible(js-implicit context: NativeContext)(object: JSAny):
      JSAny {
10
    const objectJSReceiver = Cast<JSReceiver>(object)
11 12
        otherwise ThrowTypeError(
        MessageTemplate::kCalledOnNonObject, 'Reflect.isExtensible');
13
    return object::ObjectIsExtensibleImpl(objectJSReceiver);
14
  }
15 16

  // ES6 section 26.1.12 Reflect.preventExtensions
17
  transitioning javascript builtin
18 19
  ReflectPreventExtensions(js-implicit context: NativeContext)(object: JSAny):
      JSAny {
20
    const objectJSReceiver = Cast<JSReceiver>(object)
21 22
        otherwise ThrowTypeError(
        MessageTemplate::kCalledOnNonObject, 'Reflect.preventExtensions');
23 24
    return object::ObjectPreventExtensionsDontThrow(objectJSReceiver);
  }
25 26

  // ES6 section 26.1.8 Reflect.getPrototypeOf
27
  transitioning javascript builtin
28 29
  ReflectGetPrototypeOf(js-implicit context: NativeContext)(object: JSAny):
      JSAny {
30
    const objectJSReceiver = Cast<JSReceiver>(object)
31 32
        otherwise ThrowTypeError(
        MessageTemplate::kCalledOnNonObject, 'Reflect.getPrototypeOf');
33 34
    return object::JSReceiverGetPrototypeOf(objectJSReceiver);
  }
35 36 37

  // ES6 section 26.1.14 Reflect.setPrototypeOf
  transitioning javascript builtin ReflectSetPrototypeOf(
38
      js-implicit context: NativeContext)(object: JSAny, proto: JSAny): JSAny {
39
    const objectJSReceiver = Cast<JSReceiver>(object)
40 41
        otherwise ThrowTypeError(
        MessageTemplate::kCalledOnNonObject, 'Reflect.setPrototypeOf');
42
    typeswitch (proto) {
43
      case (proto: JSReceiver|Null): {
44 45 46
        return object::ObjectSetPrototypeOfDontThrow(objectJSReceiver, proto);
      }
      case (JSAny): {
47
        ThrowTypeError(MessageTemplate::kProtoObjectOrNull, proto);
48
      }
49 50
    }
  }
51

52 53
  extern transitioning builtin ToName(implicit context: Context)(JSAny):
      AnyName;
54 55 56 57 58
  type OnNonExistent constexpr 'OnNonExistent';
  const kReturnUndefined: constexpr OnNonExistent
  generates 'OnNonExistent::kReturnUndefined';
  extern macro SmiConstant(constexpr OnNonExistent): Smi;
  extern transitioning builtin GetPropertyWithReceiver(
59
      implicit context: Context)(JSAny, Name, JSAny, Smi): JSAny;
60 61 62

  // ES6 section 26.1.6 Reflect.get
  transitioning javascript builtin
63
  ReflectGet(js-implicit context: NativeContext)(...arguments): JSAny {
64
    const length = arguments.length;
65
    const object: JSAny = length > 0 ? arguments[0] : Undefined;
66
    const objectJSReceiver = Cast<JSReceiver>(object)
67 68
        otherwise ThrowTypeError(
        MessageTemplate::kCalledOnNonObject, 'Reflect.get');
69 70 71
    const propertyKey: JSAny = length > 1 ? arguments[1] : Undefined;
    const name: AnyName = ToName(propertyKey);
    const receiver: JSAny = length > 2 ? arguments[2] : objectJSReceiver;
72 73 74
    return GetPropertyWithReceiver(
        objectJSReceiver, name, receiver, SmiConstant(kReturnUndefined));
  }
75 76 77

  // ES6 section 26.1.4 Reflect.deleteProperty
  transitioning javascript builtin ReflectDeleteProperty(
78
      js-implicit context: NativeContext)(object: JSAny, key: JSAny): JSAny {
79
    const objectJSReceiver = Cast<JSReceiver>(object)
80 81 82
        otherwise ThrowTypeError(
        MessageTemplate::kCalledOnNonObject, 'Reflect.deleteProperty');
    return DeleteProperty(objectJSReceiver, key, LanguageMode::kSloppy);
83
  }
84 85 86

  // ES section #sec-reflect.has
  transitioning javascript builtin
87 88
  ReflectHas(js-implicit context: NativeContext)(object: JSAny, key: JSAny):
      JSAny {
89
    const objectJSReceiver = Cast<JSReceiver>(object)
90 91
        otherwise ThrowTypeError(
        MessageTemplate::kCalledOnNonObject, 'Reflect.has');
92 93
    return HasProperty(objectJSReceiver, key);
  }
94
}  // namespace reflect