jsbool.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * JS boolean implementation.
  7. */
  8. #include "jsboolinlines.h"
  9. #include "jsapi.h"
  10. #include "jsatom.h"
  11. #include "jscntxt.h"
  12. #include "jsobj.h"
  13. #include "jstypes.h"
  14. #include "vm/GlobalObject.h"
  15. #include "vm/ProxyObject.h"
  16. #include "vm/StringBuffer.h"
  17. #include "vm/BooleanObject-inl.h"
  18. using namespace js;
  19. const Class BooleanObject::class_ = {
  20. "Boolean",
  21. JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean)
  22. };
  23. MOZ_ALWAYS_INLINE bool
  24. IsBoolean(HandleValue v)
  25. {
  26. return v.isBoolean() || (v.isObject() && v.toObject().is<BooleanObject>());
  27. }
  28. #if JS_HAS_TOSOURCE
  29. MOZ_ALWAYS_INLINE bool
  30. bool_toSource_impl(JSContext* cx, const CallArgs& args)
  31. {
  32. HandleValue thisv = args.thisv();
  33. MOZ_ASSERT(IsBoolean(thisv));
  34. bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as<BooleanObject>().unbox();
  35. StringBuffer sb(cx);
  36. if (!sb.append("(new Boolean(") || !BooleanToStringBuffer(b, sb) || !sb.append("))"))
  37. return false;
  38. JSString* str = sb.finishString();
  39. if (!str)
  40. return false;
  41. args.rval().setString(str);
  42. return true;
  43. }
  44. static bool
  45. bool_toSource(JSContext* cx, unsigned argc, Value* vp)
  46. {
  47. CallArgs args = CallArgsFromVp(argc, vp);
  48. return CallNonGenericMethod<IsBoolean, bool_toSource_impl>(cx, args);
  49. }
  50. #endif
  51. MOZ_ALWAYS_INLINE bool
  52. bool_toString_impl(JSContext* cx, const CallArgs& args)
  53. {
  54. HandleValue thisv = args.thisv();
  55. MOZ_ASSERT(IsBoolean(thisv));
  56. bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as<BooleanObject>().unbox();
  57. args.rval().setString(BooleanToString(cx, b));
  58. return true;
  59. }
  60. static bool
  61. bool_toString(JSContext* cx, unsigned argc, Value* vp)
  62. {
  63. CallArgs args = CallArgsFromVp(argc, vp);
  64. return CallNonGenericMethod<IsBoolean, bool_toString_impl>(cx, args);
  65. }
  66. MOZ_ALWAYS_INLINE bool
  67. bool_valueOf_impl(JSContext* cx, const CallArgs& args)
  68. {
  69. HandleValue thisv = args.thisv();
  70. MOZ_ASSERT(IsBoolean(thisv));
  71. bool b = thisv.isBoolean() ? thisv.toBoolean() : thisv.toObject().as<BooleanObject>().unbox();
  72. args.rval().setBoolean(b);
  73. return true;
  74. }
  75. static bool
  76. bool_valueOf(JSContext* cx, unsigned argc, Value* vp)
  77. {
  78. CallArgs args = CallArgsFromVp(argc, vp);
  79. return CallNonGenericMethod<IsBoolean, bool_valueOf_impl>(cx, args);
  80. }
  81. static const JSFunctionSpec boolean_methods[] = {
  82. #if JS_HAS_TOSOURCE
  83. JS_FN(js_toSource_str, bool_toSource, 0, 0),
  84. #endif
  85. JS_FN(js_toString_str, bool_toString, 0, 0),
  86. JS_FN(js_valueOf_str, bool_valueOf, 0, 0),
  87. JS_FS_END
  88. };
  89. static bool
  90. Boolean(JSContext* cx, unsigned argc, Value* vp)
  91. {
  92. CallArgs args = CallArgsFromVp(argc, vp);
  93. bool b = args.length() != 0 ? JS::ToBoolean(args[0]) : false;
  94. if (args.isConstructing()) {
  95. RootedObject newTarget (cx, &args.newTarget().toObject());
  96. RootedObject proto(cx);
  97. if (!GetPrototypeFromConstructor(cx, newTarget, &proto))
  98. return false;
  99. JSObject* obj = BooleanObject::create(cx, b, proto);
  100. if (!obj)
  101. return false;
  102. args.rval().setObject(*obj);
  103. } else {
  104. args.rval().setBoolean(b);
  105. }
  106. return true;
  107. }
  108. JSObject*
  109. js::InitBooleanClass(JSContext* cx, HandleObject obj)
  110. {
  111. MOZ_ASSERT(obj->isNative());
  112. Handle<GlobalObject*> global = obj.as<GlobalObject>();
  113. Rooted<BooleanObject*> booleanProto(cx, GlobalObject::createBlankPrototype<BooleanObject>(cx, global));
  114. if (!booleanProto)
  115. return nullptr;
  116. booleanProto->setFixedSlot(BooleanObject::PRIMITIVE_VALUE_SLOT, BooleanValue(false));
  117. RootedFunction ctor(cx, GlobalObject::createConstructor(cx, Boolean, cx->names().Boolean, 1));
  118. if (!ctor)
  119. return nullptr;
  120. if (!LinkConstructorAndPrototype(cx, ctor, booleanProto))
  121. return nullptr;
  122. if (!DefinePropertiesAndFunctions(cx, booleanProto, nullptr, boolean_methods))
  123. return nullptr;
  124. if (!GlobalObject::initBuiltinConstructor(cx, global, JSProto_Boolean, ctor, booleanProto))
  125. return nullptr;
  126. return booleanProto;
  127. }
  128. JSString*
  129. js::BooleanToString(ExclusiveContext* cx, bool b)
  130. {
  131. return b ? cx->names().true_ : cx->names().false_;
  132. }
  133. JS_PUBLIC_API(bool)
  134. js::ToBooleanSlow(HandleValue v)
  135. {
  136. if (v.isString())
  137. return v.toString()->length() != 0;
  138. MOZ_ASSERT(v.isObject());
  139. return !EmulatesUndefined(&v.toObject());
  140. }