object.tq 6.56 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 runtime {
  extern transitioning runtime
7
  ObjectIsExtensible(implicit context: Context)(JSAny): JSAny;
8 9 10

  extern transitioning runtime
  JSReceiverPreventExtensionsThrow(implicit context: Context)(JSReceiver):
11
      JSAny;
12 13 14

  extern transitioning runtime
  JSReceiverPreventExtensionsDontThrow(implicit context: Context)(JSReceiver):
15
      JSAny;
16 17

  extern transitioning runtime
18
  JSReceiverGetPrototypeOf(implicit context: Context)(JSReceiver): JSAny;
19 20

  extern transitioning runtime
21 22
  JSReceiverSetPrototypeOfThrow(implicit context: Context)(JSReceiver, JSAny):
      JSAny;
23 24 25

  extern transitioning runtime
  JSReceiverSetPrototypeOfDontThrow(implicit context:
26
                                        Context)(JSReceiver, JSAny): JSAny;
27 28 29

  extern transitioning runtime ObjectCreate(implicit context:
                                                Context)(JSAny, JSAny): JSAny;
30 31 32 33
}  // namespace runtime

namespace object {
  transitioning macro
34
  ObjectIsExtensibleImpl(implicit context: Context)(object: JSAny): JSAny {
35 36 37 38 39
    const objectJSReceiver = Cast<JSReceiver>(object) otherwise return False;
    const objectJSProxy = Cast<JSProxy>(objectJSReceiver)
        otherwise return runtime::ObjectIsExtensible(objectJSReceiver);
    return proxy::ProxyIsExtensible(objectJSProxy);
  }
40 41

  transitioning macro
42 43
  ObjectPreventExtensionsThrow(implicit context: Context)(object: JSAny):
      JSAny {
44 45 46 47
    const objectJSReceiver = Cast<JSReceiver>(object) otherwise return object;
    const objectJSProxy = Cast<JSProxy>(objectJSReceiver)
        otherwise return runtime::JSReceiverPreventExtensionsThrow(
        objectJSReceiver);
48
    proxy::ProxyPreventExtensions(objectJSProxy, True);
49
    return objectJSReceiver;
50 51 52
  }

  transitioning macro
53 54
  ObjectPreventExtensionsDontThrow(implicit context: Context)(object: JSAny):
      JSAny {
55 56 57 58 59 60
    const objectJSReceiver = Cast<JSReceiver>(object) otherwise return False;
    const objectJSProxy = Cast<JSProxy>(objectJSReceiver)
        otherwise return runtime::JSReceiverPreventExtensionsDontThrow(
        objectJSReceiver);
    return proxy::ProxyPreventExtensions(objectJSProxy, False);
  }
61 62

  transitioning macro
63
  ObjectGetPrototypeOfImpl(implicit context: Context)(object: JSAny): JSAny {
64 65 66 67 68 69
    const objectJSReceiver: JSReceiver = ToObject_Inline(context, object);
    return object::JSReceiverGetPrototypeOf(objectJSReceiver);
  }

  transitioning macro
  JSReceiverGetPrototypeOf(implicit context: Context)(object: JSReceiver):
70
      JSAny {
71 72 73 74
    const objectJSProxy = Cast<JSProxy>(object)
        otherwise return runtime::JSReceiverGetPrototypeOf(object);
    return proxy::ProxyGetPrototypeOf(objectJSProxy);
  }
75 76 77

  transitioning macro
  ObjectSetPrototypeOfThrow(implicit context: Context)(
78
      object: JSAny, proto: JSReceiver|Null): JSAny {
79 80 81 82 83 84 85 86 87 88
    const objectJSReceiver = Cast<JSReceiver>(object) otherwise return object;
    const objectJSProxy = Cast<JSProxy>(objectJSReceiver)
        otherwise return runtime::JSReceiverSetPrototypeOfThrow(
        objectJSReceiver, proto);
    proxy::ProxySetPrototypeOf(objectJSProxy, proto, True);
    return objectJSReceiver;
  }

  transitioning macro
  ObjectSetPrototypeOfDontThrow(implicit context: Context)(
89
      object: JSAny, proto: JSReceiver|Null): JSAny {
90 91 92 93 94 95
    const objectJSReceiver = Cast<JSReceiver>(object) otherwise return False;
    const objectJSProxy = Cast<JSProxy>(objectJSReceiver)
        otherwise return runtime::JSReceiverSetPrototypeOfDontThrow(
        objectJSReceiver, proto);
    return proxy::ProxySetPrototypeOf(objectJSProxy, proto, False);
  }
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  transitioning builtin CreateObjectWithoutProperties(
      implicit context: Context)(prototype: JSAny): JSAny {
    const nativeContext = LoadNativeContext(context);

    try {
      let map: Map;
      let properties: NameDictionary|EmptyFixedArray;
      typeswitch (prototype) {
        case (Null): {
          map = UnsafeCast<Map>(
              nativeContext[SLOW_OBJECT_WITH_NULL_PROTOTYPE_MAP]);
          properties = AllocateNameDictionary(kNameDictionaryInitialCapacity);
        }
        case (prototype: JSReceiver): {
          properties = kEmptyFixedArray;
          const objectFunction =
              UnsafeCast<JSFunction>(nativeContext[OBJECT_FUNCTION_INDEX]);
          map = UnsafeCast<Map>(objectFunction.prototype_or_initial_map);
          if (prototype != map.prototype) {
            const prototypeInfo =
                prototype.map.PrototypeInfo() otherwise Runtime;
            typeswitch (prototypeInfo.object_create_map) {
              case (Undefined): {
                goto Runtime;
              }
              case (weak_map: Weak<Map>): {
                map = WeakToStrong(weak_map) otherwise Runtime;
              }
            }
          }
        }
        case (JSAny): {
          goto Runtime;
        }
      }
      return AllocateJSObjectFromMap(map, properties);
    }
    label Runtime deferred {
      return runtime::ObjectCreate(prototype, Undefined);
    }
  }

139
  // ES6 section 19.1.2.11 Object.isExtensible ( O )
140
  transitioning javascript builtin
141
  ObjectIsExtensible(js-implicit context: NativeContext)(object: JSAny): JSAny {
142
    return object::ObjectIsExtensibleImpl(object);
143
  }
144

145
  // ES6 section 19.1.2.18 Object.preventExtensions ( O )
146
  transitioning javascript builtin
147 148
  ObjectPreventExtensions(js-implicit context: NativeContext)(object: JSAny):
      JSAny {
149 150
    return object::ObjectPreventExtensionsThrow(object);
  }
151 152

  // ES6 section 19.1.2.9 Object.getPrototypeOf ( O )
153
  transitioning javascript builtin
154 155
  ObjectGetPrototypeOf(js-implicit context: NativeContext)(object: JSAny):
      JSAny {
156
    return object::ObjectGetPrototypeOfImpl(object);
157
  }
158 159 160

  // ES6 section 19.1.2.21 Object.setPrototypeOf ( O, proto )
  transitioning javascript builtin ObjectSetPrototypeOf(
161
      js-implicit context: NativeContext)(object: JSAny, proto: JSAny): JSAny {
162
    // 1. Set O to ? RequireObjectCoercible(O).
163 164
    RequireObjectCoercible(object, 'Object.setPrototypeOf');

165 166 167 168 169 170
    // 2. If Type(proto) is neither Object nor Null, throw a TypeError
    // exception.
    // 3. If Type(O) is not Object, return O.
    // 4. Let status be ? O.[[SetPrototypeOf]](proto).
    // 5. If status is false, throw a TypeError exception.
    // 6. Return O.
171
    typeswitch (proto) {
172
      case (proto: JSReceiver|Null): {
173 174 175 176 177
        return object::ObjectSetPrototypeOfThrow(object, proto);
      }
      case (JSAny): {
        ThrowTypeError(kProtoObjectOrNull, proto);
      }
178 179
    }
  }
180
}  // namespace object