/js/src/jsbool.cpp

http://github.com/zpao/v8monkey · C++ · 238 lines · 155 code · 35 blank · 48 comment · 26 complexity · e6c855d7f1e511542594470b84afa32f MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is Mozilla Communicator client code, released
  17. * March 31, 1998.
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Netscape Communications Corporation.
  21. * Portions created by the Initial Developer are Copyright (C) 1998
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either of the GNU General Public License Version 2 or later (the "GPL"),
  28. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  39. /*
  40. * JS boolean implementation.
  41. */
  42. #include "jstypes.h"
  43. #include "jsutil.h"
  44. #include "jsapi.h"
  45. #include "jsatom.h"
  46. #include "jsbool.h"
  47. #include "jscntxt.h"
  48. #include "jsinfer.h"
  49. #include "jsversion.h"
  50. #include "jslock.h"
  51. #include "jsnum.h"
  52. #include "jsobj.h"
  53. #include "jsstr.h"
  54. #include "vm/BooleanObject-inl.h"
  55. #include "vm/GlobalObject.h"
  56. #include "jsinferinlines.h"
  57. #include "jsobjinlines.h"
  58. #include "jsstrinlines.h"
  59. using namespace js;
  60. using namespace js::types;
  61. Class js::BooleanClass = {
  62. "Boolean",
  63. JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_HAS_CACHED_PROTO(JSProto_Boolean), JS_PropertyStub, /* addProperty */
  64. JS_PropertyStub, /* delProperty */
  65. JS_PropertyStub, /* getProperty */
  66. JS_StrictPropertyStub, /* setProperty */
  67. JS_EnumerateStub,
  68. JS_ResolveStub,
  69. JS_ConvertStub
  70. };
  71. #if JS_HAS_TOSOURCE
  72. static JSBool
  73. bool_toSource(JSContext *cx, uintN argc, Value *vp)
  74. {
  75. CallArgs args = CallArgsFromVp(argc, vp);
  76. bool b, ok;
  77. if (!BoxedPrimitiveMethodGuard(cx, args, bool_toSource, &b, &ok))
  78. return ok;
  79. StringBuffer sb(cx);
  80. if (!sb.append("(new Boolean(") || !BooleanToStringBuffer(cx, b, sb) || !sb.append("))"))
  81. return false;
  82. JSString *str = sb.finishString();
  83. if (!str)
  84. return false;
  85. args.rval().setString(str);
  86. return true;
  87. }
  88. #endif
  89. static JSBool
  90. bool_toString(JSContext *cx, uintN argc, Value *vp)
  91. {
  92. CallArgs args = CallArgsFromVp(argc, vp);
  93. bool b, ok;
  94. if (!BoxedPrimitiveMethodGuard<bool>(cx, args, bool_toString, &b, &ok))
  95. return ok;
  96. args.rval().setString(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
  97. return true;
  98. }
  99. static JSBool
  100. bool_valueOf(JSContext *cx, uintN argc, Value *vp)
  101. {
  102. CallArgs args = CallArgsFromVp(argc, vp);
  103. bool b, ok;
  104. if (!BoxedPrimitiveMethodGuard(cx, args, bool_valueOf, &b, &ok))
  105. return ok;
  106. args.rval().setBoolean(b);
  107. return true;
  108. }
  109. static JSFunctionSpec boolean_methods[] = {
  110. #if JS_HAS_TOSOURCE
  111. JS_FN(js_toSource_str, bool_toSource, 0, 0),
  112. #endif
  113. JS_FN(js_toString_str, bool_toString, 0, 0),
  114. JS_FN(js_valueOf_str, bool_valueOf, 0, 0),
  115. JS_FS_END
  116. };
  117. static JSBool
  118. Boolean(JSContext *cx, uintN argc, Value *vp)
  119. {
  120. CallArgs args = CallArgsFromVp(argc, vp);
  121. bool b = args.length() != 0 ? js_ValueToBoolean(args[0]) : false;
  122. if (IsConstructing(vp)) {
  123. JSObject *obj = BooleanObject::create(cx, b);
  124. args.rval().setObject(*obj);
  125. } else {
  126. args.rval().setBoolean(b);
  127. }
  128. return true;
  129. }
  130. JSObject *
  131. js_InitBooleanClass(JSContext *cx, JSObject *obj)
  132. {
  133. JS_ASSERT(obj->isNative());
  134. GlobalObject *global = &obj->asGlobal();
  135. JSObject *booleanProto = global->createBlankPrototype(cx, &BooleanClass);
  136. if (!booleanProto)
  137. return NULL;
  138. booleanProto->setPrimitiveThis(BooleanValue(false));
  139. JSFunction *ctor = global->createConstructor(cx, Boolean, &BooleanClass,
  140. CLASS_ATOM(cx, Boolean), 1);
  141. if (!ctor)
  142. return NULL;
  143. if (!LinkConstructorAndPrototype(cx, ctor, booleanProto))
  144. return NULL;
  145. if (!DefinePropertiesAndBrand(cx, booleanProto, NULL, boolean_methods))
  146. return NULL;
  147. if (!DefineConstructorAndPrototype(cx, global, JSProto_Boolean, ctor, booleanProto))
  148. return NULL;
  149. return booleanProto;
  150. }
  151. JSString *
  152. js_BooleanToString(JSContext *cx, JSBool b)
  153. {
  154. return cx->runtime->atomState.booleanAtoms[b ? 1 : 0];
  155. }
  156. /* This function implements E-262-3 section 9.8, toString. */
  157. bool
  158. js::BooleanToStringBuffer(JSContext *cx, JSBool b, StringBuffer &sb)
  159. {
  160. return b ? sb.append("true") : sb.append("false");
  161. }
  162. namespace js {
  163. bool
  164. BooleanGetPrimitiveValueSlow(JSContext *cx, JSObject &obj, Value *vp)
  165. {
  166. JS_ASSERT(ObjectClassIs(obj, ESClass_Boolean, cx));
  167. JS_ASSERT(obj.isProxy());
  168. /*
  169. * To respect the proxy abstraction, we can't simply unwrap and call
  170. * getPrimitiveThis on the wrapped object. All we know is that obj says
  171. * its [[Class]] is "Boolean". Boolean.prototype.valueOf is specified to
  172. * return the [[PrimitiveValue]] internal property, so call that instead.
  173. */
  174. InvokeArgsGuard ag;
  175. if (!cx->stack.pushInvokeArgs(cx, 0, &ag))
  176. return false;
  177. ag.calleev().setUndefined();
  178. ag.thisv().setObject(obj);
  179. if (!GetProxyHandler(&obj)->nativeCall(cx, &obj, &BooleanClass, bool_valueOf, ag))
  180. return false;
  181. *vp = ag.rval();
  182. return true;
  183. }
  184. } /* namespace js */
  185. JSBool
  186. js_ValueToBoolean(const Value &v)
  187. {
  188. if (v.isInt32())
  189. return v.toInt32() != 0;
  190. if (v.isString())
  191. return v.toString()->length() != 0;
  192. if (v.isObject())
  193. return JS_TRUE;
  194. if (v.isNullOrUndefined())
  195. return JS_FALSE;
  196. if (v.isDouble()) {
  197. jsdouble d;
  198. d = v.toDouble();
  199. return !JSDOUBLE_IS_NaN(d) && d != 0;
  200. }
  201. JS_ASSERT(v.isBoolean());
  202. return v.toBoolean();
  203. }