home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jsbool.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.5 KB  |  177 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * JS boolean implementation.
  21.  */
  22. #include "prtypes.h"
  23. #include "jsapi.h"
  24. #include "jsatom.h"
  25. #include "jsbool.h"
  26. #include "jscntxt.h"
  27. #include "jslock.h"
  28. #include "jsnum.h"
  29. #include "jsobj.h"
  30. #include "jsstr.h"
  31.  
  32. static JSClass boolean_class = {
  33.     "Boolean",
  34.     JSCLASS_HAS_PRIVATE,
  35.     JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,  JS_PropertyStub,
  36.     JS_EnumerateStub, JS_ResolveStub,   JS_ConvertStub,   JS_FinalizeStub
  37. };
  38.  
  39. static JSBool
  40. bool_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
  41.           jsval *rval)
  42. {
  43.     jsval v;
  44.     JSAtom *atom;
  45.     JSString *str;
  46.  
  47.     if (!JS_InstanceOf(cx, obj, &boolean_class, argv))
  48.     return JS_FALSE;
  49.     JS_LOCK_VOID(cx, v = js_GetSlot(cx, obj, JSSLOT_PRIVATE));
  50.     atom = cx->runtime->atomState.booleanAtoms[JSVAL_TO_BOOLEAN(v) ? 1 : 0];
  51.     str = ATOM_TO_STRING(atom);
  52.     if (!str)
  53.     return JS_FALSE;
  54.     *rval = STRING_TO_JSVAL(str);
  55.     return JS_TRUE;
  56. }
  57.  
  58. static JSBool
  59. bool_valueOf(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
  60. {
  61.     if (!JS_InstanceOf(cx, obj, &boolean_class, argv))
  62.     return JS_FALSE;
  63.     JS_LOCK_VOID(cx, *rval = js_GetSlot(cx, obj, JSSLOT_PRIVATE));
  64.     return JS_TRUE;
  65. }
  66.  
  67. static JSFunctionSpec boolean_methods[] = {
  68.     {js_toString_str,    bool_toString,        0},
  69.     {js_valueOf_str,    bool_valueOf,        0},
  70.     {0}
  71. };
  72.  
  73. #ifdef XP_MAC
  74. #undef Boolean
  75. #define Boolean js_Boolean
  76. #endif
  77.  
  78. static JSBool
  79. Boolean(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
  80. {
  81.     JSBool b;
  82.     jsval bval;
  83.  
  84.     if (argc != 0) {
  85.     if (!JS_ValueToBoolean(cx, argv[0], &b))
  86.         return JS_FALSE;
  87.     bval = BOOLEAN_TO_JSVAL(b);
  88.     } else {
  89.     bval = JSVAL_FALSE;
  90.     }
  91.     if (obj->map->clasp != &boolean_class) {
  92.     *rval = bval;
  93.     return JS_TRUE;
  94.     }
  95.     if (!js_SetSlot(cx, obj, JSSLOT_PRIVATE, bval))
  96.     return JS_FALSE;
  97.     *rval = OBJECT_TO_JSVAL(obj);
  98.     return JS_TRUE;
  99. }
  100.  
  101. JSObject *
  102. js_InitBooleanClass(JSContext *cx, JSObject *obj)
  103. {
  104.     JSObject *proto;
  105.  
  106.     proto = JS_InitClass(cx, obj, NULL, &boolean_class, Boolean, 1,
  107.             NULL, boolean_methods, NULL, NULL);
  108.     if (!proto || !js_SetSlot(cx, proto, JSSLOT_PRIVATE, JSVAL_FALSE))
  109.     return NULL;
  110.     return proto;
  111. }
  112.  
  113. JSObject *
  114. js_BooleanToObject(JSContext *cx, JSBool b)
  115. {
  116.     JSObject *obj;
  117.  
  118.     obj = js_NewObject(cx, &boolean_class, NULL, NULL);
  119.     if (!obj)
  120.     return NULL;
  121.     if (!js_SetSlot(cx, obj, JSSLOT_PRIVATE, BOOLEAN_TO_JSVAL(b))) {
  122.     cx->newborn[GCX_OBJECT] = NULL;
  123.     return NULL;
  124.     }
  125.     return obj;
  126. }
  127.  
  128. JSString *
  129. js_BooleanToString(JSContext *cx, JSBool b)
  130. {
  131.     return ATOM_TO_STRING(cx->runtime->atomState.booleanAtoms[b ? 1 : 0]);
  132. }
  133.  
  134. JSBool
  135. js_ValueToBoolean(JSContext *cx, jsval v, JSBool *bp)
  136. {
  137.     JSBool b;
  138.     JSObject *obj;
  139.     jsdouble d;
  140.  
  141. #if defined XP_PC && defined _MSC_VER &&_MSC_VER <= 800
  142.     /* MSVC1.5 coredumps */
  143.     if (!bp)
  144.     return JS_TRUE;
  145. #endif
  146.  
  147.     /* XXX this should be an if-else chain, but MSVC1.5 really sucks. */
  148.     if (JSVAL_IS_NULL(v) || JSVAL_IS_VOID(v)) {
  149.     /* Must return early to avoid falling thru to JSVAL_IS_OBJECT case. */
  150.     *bp = JS_FALSE;
  151.     return JS_TRUE;
  152.     }
  153.     if (JSVAL_IS_OBJECT(v)) {
  154.     obj = JSVAL_TO_OBJECT(v);
  155.     if (!obj->map->clasp->convert(cx, obj, JSTYPE_BOOLEAN, &v))
  156.         return JS_FALSE;
  157.     if (!JSVAL_IS_BOOLEAN(v))
  158.         v = JSVAL_TRUE;        /* non-null object is true */
  159.     b = JSVAL_TO_BOOLEAN(v);
  160.     }
  161.     if (JSVAL_IS_STRING(v)) {
  162.     b = JSVAL_TO_STRING(v)->length ? JS_TRUE : JS_FALSE;
  163.     }
  164.     if (JSVAL_IS_INT(v)) {
  165.     b = JSVAL_TO_INT(v) ? JS_TRUE : JS_FALSE;
  166.     }
  167.     if (JSVAL_IS_DOUBLE(v)) {
  168.     d = *JSVAL_TO_DOUBLE(v);
  169.     b = (!JSDOUBLE_IS_NaN(d) && d != 0) ? JS_TRUE : JS_FALSE;
  170.     }
  171.     if (JSVAL_IS_BOOLEAN(v)) {
  172.     b = JSVAL_TO_BOOLEAN(v);
  173.     }
  174.     *bp = b;
  175.     return JS_TRUE;
  176. }
  177.