boolean.tq 1.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
// 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 boolean {
  const kNameDictionaryInitialCapacity:
      constexpr int32 generates 'NameDictionary::kInitialCapacity';

  extern macro ConstructorBuiltinsAssembler::IsDictionaryMap(Map): bool;
  extern macro CodeStubAssembler::AllocateNameDictionary(constexpr int32):
      NameDictionary;

  // TODO(v8:9120): This is a workaround to get access to target and new.target
  // in javascript builtins. Requires cleanup once this is fully supported by
  // torque.
  const NEW_TARGET_INDEX:
      constexpr int32 generates 'Descriptor::kJSNewTarget';
  const TARGET_INDEX: constexpr int32 generates 'Descriptor::kJSTarget';
  extern macro Parameter(constexpr int32): Object;

  javascript builtin
  BooleanConstructor(context: Context, receiver: Object, ...arguments): Object {
    const value = SelectBooleanConstant(ToBoolean(arguments[0]));

    const newTarget = Parameter(NEW_TARGET_INDEX);
    if (newTarget == Undefined) {
      return value;
    }

    const target = UnsafeCast<JSFunction>(Parameter(TARGET_INDEX));
    const map = GetDerivedMap(target, UnsafeCast<JSReceiver>(newTarget));
    let properties = kEmptyFixedArray;
    if (IsDictionaryMap(map)) {
      properties = AllocateNameDictionary(kNameDictionaryInitialCapacity);
    }

    const obj = UnsafeCast<JSValue>(AllocateJSObjectFromMap(
        map, properties, kEmptyFixedArray, kNone, kWithSlackTracking));
    obj.value = value;
    return obj;
  }
}