Ignore:
Timestamp:
Mar 21, 2002, 4:31:57 PM (23 years ago)
Author:
mjs
Message:

Merged changes from LABYRINTH_KDE_3_MERGE branch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/object_object.cpp

    r6 r798  
     1// -*- c-basic-offset: 2 -*-
    12/*
    23 *  This file is part of the KDE libraries
     
    1617 *  License along with this library; if not, write to the Free Software
    1718 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     19 *
     20 *  $Id$
    1821 */
    1922
    20 #include "kjs.h"
     23#include "value.h"
     24#include "object.h"
     25#include "types.h"
     26#include "interpreter.h"
    2127#include "operations.h"
    2228#include "object_object.h"
    23 #include "types.h"
     29#include "function_object.h"
    2430#include <stdio.h>
     31#include <assert.h>
    2532
    2633using namespace KJS;
    2734
    28 ObjectObject::ObjectObject(const Object &funcProto, const Object &objProto)
    29     : ConstructorImp(funcProto, 1)
     35// ------------------------------ ObjectPrototypeImp --------------------------------
     36
     37ObjectPrototypeImp::ObjectPrototypeImp(ExecState *exec,
     38                                       FunctionPrototypeImp *funcProto)
     39  : ObjectImp() // [[Prototype]] is Null()
    3040{
    31   // ECMA 15.2.3.1
    32   setPrototypeProperty(objProto);
     41  Value protect(this);
     42  put(exec,"toString", Object(new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ToString, 0)), DontEnum);
     43  put(exec,"valueOf",  Object(new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ValueOf,  0)), DontEnum);
    3344}
    3445
    35 Completion ObjectObject::execute(const List &args)
     46
     47// ------------------------------ ObjectProtoFuncImp --------------------------------
     48
     49ObjectProtoFuncImp::ObjectProtoFuncImp(ExecState *exec,
     50                                       FunctionPrototypeImp *funcProto,
     51                                       int i, int len)
     52  : InternalFunctionImp(funcProto), id(i)
    3653{
    37   KJSO result;
     54  Value protect(this);
     55  put(exec,"length",Number(len),DontDelete|ReadOnly|DontEnum);
     56}
    3857
    39   List argList;
    40   if (args.isEmpty()) {
    41     result = construct(argList);
    42   } else {
    43     KJSO arg = args[0];
    44     if (arg.isA(NullType) || arg.isA(UndefinedType)) {
    45       argList.append(arg);
    46       result = construct(argList);
    47     } else
    48       result = arg.toObject();
    49   }
    50   return Completion(ReturnValue, result);
     58
     59bool ObjectProtoFuncImp::implementsCall() const
     60{
     61  return true;
     62}
     63
     64// ECMA 15.2.4.2 + 15.2.4.3
     65
     66Value ObjectProtoFuncImp::call(ExecState */*exec*/, Object &thisObj, const List &/*args*/)
     67{
     68  if (id == ValueOf)
     69    return thisObj;
     70  else /* ToString */
     71    return String("[object "+thisObj.className()+"]");
     72}
     73
     74// ------------------------------ ObjectObjectImp --------------------------------
     75
     76ObjectObjectImp::ObjectObjectImp(ExecState *exec,
     77                                 ObjectPrototypeImp *objProto,
     78                                 FunctionPrototypeImp *funcProto)
     79  : InternalFunctionImp(funcProto)
     80{
     81  Value protect(this);
     82  // ECMA 15.2.3.1
     83  put(exec,"prototype", Object(objProto), DontEnum|DontDelete|ReadOnly);
     84
     85  // no. of arguments for constructor
     86  put(exec,"length", Number(1), ReadOnly|DontDelete|DontEnum);
     87}
     88
     89
     90bool ObjectObjectImp::implementsConstruct() const
     91{
     92  return true;
    5193}
    5294
    5395// ECMA 15.2.2
    54 Object ObjectObject::construct(const List &args)
     96Object ObjectObjectImp::construct(ExecState *exec, const List &args)
    5597{
    5698  // if no arguments have been passed ...
    57   if (args.isEmpty())
    58     return Object::create(ObjectClass);
     99  if (args.isEmpty()) {
     100    Object proto = exec->interpreter()->builtinObjectPrototype();
     101    Object result(new ObjectImp(proto));
     102    return result;
     103  }
    59104
    60   KJSO arg = *args.begin();
     105  Value arg = *(args.begin());
    61106  Object obj = Object::dynamicCast(arg);
    62107  if (!obj.isNull()) {
    63     /* TODO: handle host objects */
    64108    return obj;
    65109  }
     
    69113  case BooleanType:
    70114  case NumberType:
    71     return arg.toObject();
     115    return arg.toObject(exec);
    72116  default:
    73117    assert(!"unhandled switch case in ObjectConstructor");
    74118  case NullType:
    75119  case UndefinedType:
    76     return Object::create(ObjectClass);
     120    Object proto = exec->interpreter()->builtinObjectPrototype();
     121    return Object(new ObjectImp(proto));
    77122  }
    78123}
    79124
    80 ObjectPrototype::ObjectPrototype()
    81   : ObjectImp(ObjectClass)
     125bool ObjectObjectImp::implementsCall() const
    82126{
    83   // the spec says that [[Property]] should be `null'.
    84   // Not sure if Null or C's NULL is needed.
     127  return true;
    85128}
    86129
    87 bool ObjectPrototype::hasProperty(const UString &p, bool recursive) const
     130Value ObjectObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
    88131{
    89     if ( p == "toString" || p == "valueOf" )
    90         return true;
    91     return /*recursive &&*/ ObjectImp::hasProperty(p, recursive);
     132  Value result;
     133
     134  List argList;
     135  // Construct a new Object
     136  if (args.isEmpty()) {
     137    result = construct(exec,argList);
     138  } else {
     139    Value arg = args[0];
     140    if (arg.type() == NullType || arg.type() == UndefinedType) {
     141      argList.append(arg);
     142      result = construct(exec,argList);
     143    } else
     144      result = arg.toObject(exec);
     145  }
     146  return result;
    92147}
    93148
    94 KJSO ObjectPrototype::get(const UString &p) const
    95 {
    96   if (p == "toString")
    97     return Function(new ObjectProtoFunc(ToString));
    98   else if (p == "valueOf")
    99     return Function(new ObjectProtoFunc(ValueOf));
    100   else
    101     return Imp::get(p);
    102 }
    103 
    104 ObjectProtoFunc::ObjectProtoFunc(int i)
    105   : id(i)
    106 {
    107 }
    108 
    109 // ECMA 15.2.4.2 + 15.2.4.3
    110 Completion ObjectProtoFunc::execute(const List &)
    111 {
    112   Object thisObj = Object::dynamicCast(thisValue());
    113 
    114   /* TODO: what to do with non-objects. Is this possible at all ? */
    115   // Yes, this happens with Host Object at least (David)
    116   if (thisObj.isNull()) {
    117     UString str = "[object ";
    118     str += thisValue().isNull() ? "null" : thisValue().imp()->typeInfo()->name;
    119     str += "]";
    120     return Completion(ReturnValue, String(str));
    121   }
    122 
    123   // valueOf()
    124   if (id == ObjectPrototype::ValueOf)
    125     /* TODO: host objects*/
    126     return Completion(ReturnValue, thisObj);
    127 
    128   // toString()
    129   UString str;
    130   switch(thisObj.getClass()) {
    131   case StringClass:
    132     str = "[object String]";
    133     break;
    134   case BooleanClass:
    135     str = "[object Boolean]";
    136     break;
    137   case NumberClass:
    138     str = "[object Number]";
    139     break;
    140   case ObjectClass:
    141   {
    142     str = "[object ";
    143     str += thisValue().isNull() ? "Object" : thisValue().imp()->typeInfo()->name;
    144     str += "]";
    145     break;
    146   }
    147   default:
    148     str = "[undefined object]";
    149   }
    150 
    151   return Completion(ReturnValue, String(str));
    152 }
Note: See TracChangeset for help on using the changeset viewer.