Coverage Report

Created: 2025-12-11 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hermes/lib/VM/JSLib/Boolean.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
//===----------------------------------------------------------------------===//
9
/// \file
10
/// ES5.1 15.7 Initialize the Boolean constructor.
11
//===----------------------------------------------------------------------===//
12
13
#include "JSLibInternal.h"
14
15
#include "hermes/VM/Operations.h"
16
#include "hermes/VM/PrimitiveBox.h"
17
18
namespace hermes {
19
namespace vm {
20
21
94
Handle<JSObject> createBooleanConstructor(Runtime &runtime) {
22
94
  auto booleanPrototype = Handle<JSBoolean>::vmcast(&runtime.booleanPrototype);
23
24
94
  auto cons = defineSystemConstructor<JSBoolean>(
25
94
      runtime,
26
94
      Predefined::getSymbolID(Predefined::Boolean),
27
94
      booleanConstructor,
28
94
      booleanPrototype,
29
94
      1,
30
94
      CellKind::JSBooleanKind);
31
32
  // Boolean.prototype.xxx methods.
33
94
  defineMethod(
34
94
      runtime,
35
94
      booleanPrototype,
36
94
      Predefined::getSymbolID(Predefined::toString),
37
94
      nullptr,
38
94
      booleanPrototypeToString,
39
94
      0);
40
94
  defineMethod(
41
94
      runtime,
42
94
      booleanPrototype,
43
94
      Predefined::getSymbolID(Predefined::valueOf),
44
94
      nullptr,
45
94
      booleanPrototypeValueOf,
46
94
      0);
47
48
94
  return cons;
49
94
}
50
51
CallResult<HermesValue>
52
0
booleanConstructor(void *, Runtime &runtime, NativeArgs args) {
53
0
  bool value = toBoolean(args.getArg(0));
54
55
0
  if (args.isConstructorCall()) {
56
0
    auto *self = vmcast<JSBoolean>(args.getThisArg());
57
0
    self->setPrimitiveBoolean(value);
58
0
    return args.getThisArg();
59
0
  }
60
61
0
  return HermesValue::encodeBoolValue(value);
62
0
}
63
64
CallResult<HermesValue>
65
0
booleanPrototypeToString(void *, Runtime &runtime, NativeArgs args) {
66
0
  bool value;
67
0
  if (args.getThisArg().isBool()) {
68
0
    value = args.getThisArg().getBool();
69
0
  } else {
70
0
    auto *boolPtr = dyn_vmcast<JSBoolean>(args.getThisArg());
71
0
    if (!boolPtr) {
72
0
      return runtime.raiseTypeError(
73
0
          "Boolean.prototype.valueOf() can only be used on Boolean");
74
0
    }
75
0
    value = boolPtr->getPrimitiveBoolean();
76
0
  }
77
0
  return HermesValue::encodeStringValue(
78
0
      value ? runtime.getPredefinedString(Predefined::trueStr)
79
0
            : runtime.getPredefinedString(Predefined::falseStr));
80
0
}
81
82
CallResult<HermesValue>
83
0
booleanPrototypeValueOf(void *, Runtime &runtime, NativeArgs args) {
84
0
  if (args.getThisArg().isBool()) {
85
0
    return args.getThisArg();
86
0
  }
87
0
  auto *boolPtr = dyn_vmcast<JSBoolean>(args.getThisArg());
88
0
  if (!boolPtr) {
89
0
    return runtime.raiseTypeError(
90
0
        "Boolean.prototype.valueOf() can only be used on Boolean");
91
0
  }
92
0
  return HermesValue::encodeBoolValue(boolPtr->getPrimitiveBoolean());
93
0
}
94
95
} // namespace vm
96
} // namespace hermes