| # kjs Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# object_object.cpp

    00001 // -*- c-basic-offset: 2 -*-
    00002 /*
    00003  *  This file is part of the KDE libraries
    00004  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
    00005  *
    00006  *  This library is free software; you can redistribute it and/or
    00007  *  modify it under the terms of the GNU Lesser General Public
    00008  *  License as published by the Free Software Foundation; either
    00009  *  version 2 of the License, or (at your option) any later version.
    00010  *
    00011  *  This library is distributed in the hope that it will be useful,
    00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014  *  Lesser General Public License for more details.
    00015  *
    00016  *  You should have received a copy of the GNU Lesser General Public
    00017  *  License along with this library; if not, write to the Free Software
    00018  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00019  *
    00020  *  $Id: object_object.cpp,v 1.23 2002/11/16 09:42:40 porten Exp $
    00021  */
    00022
    00023 #include "value.h"
    00024 #include "object.h"
    00025 #include "types.h"
    00026 #include "interpreter.h"
    00027 #include "operations.h"
    00028 #include "object_object.h"
    00029 #include "function_object.h"
    00030 #include <stdio.h>
    00031 #include <assert.h>
    00032
    00033 using namespace KJS;
    00034
    00035 // ------------------------------ ObjectPrototypeImp --------------------------------
    00036
    00037 ObjectPrototypeImp::ObjectPrototypeImp(ExecState *exec,
    00038                                        FunctionPrototypeImp *funcProto)
    00039   : ObjectImp() // [[Prototype]] is Null()
    00040 {
    00041   Value protect(this);
    00042   put(exec,"toString", Object(new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ToString, 0)), DontEnum);
    00043   put(exec,"valueOf",  Object(new ObjectProtoFuncImp(exec,funcProto,ObjectProtoFuncImp::ValueOf,  0)), DontEnum);
    00044 #ifndef KJS_PURE_ECMA // standard compliance location is the Global object
    00045   // see http://www.devguru.com/Technologies/ecmascript/quickref/object.html
    00046   put(exec, "eval",
    00047       Object(new GlobalFuncImp(exec, funcProto,GlobalFuncImp::Eval, 1)),
    00048       DontEnum);
    00049 #endif
    00050 }
    00051
    00052
    00053 // ------------------------------ ObjectProtoFuncImp --------------------------------
    00054
    00055 ObjectProtoFuncImp::ObjectProtoFuncImp(ExecState *exec,
    00056                                        FunctionPrototypeImp *funcProto,
    00057                                        int i, int len)
    00058   : InternalFunctionImp(funcProto), id(i)
    00059 {
    00060   Value protect(this);
    00061   put(exec,"length",Number(len),DontDelete|ReadOnly|DontEnum);
    00062 }
    00063
    00064
    00065 bool ObjectProtoFuncImp::implementsCall() const
    00066 {
    00067   return true;
    00068 }
    00069
    00070 // ECMA 15.2.4.2 + 15.2.4.3
    00071
    00072 Value ObjectProtoFuncImp::call(ExecState */*exec*/, Object &thisObj, const List &/*args*/)
    00073 {
    00074   if (id == ValueOf)
    00075     return thisObj;
    00076   else /* ToString */
    00077     return String("[object "+thisObj.className()+"]");
    00078 }
    00079
    00080 // ------------------------------ ObjectObjectImp --------------------------------
    00081
    00082 ObjectObjectImp::ObjectObjectImp(ExecState *exec,
    00083                                  ObjectPrototypeImp *objProto,
    00084                                  FunctionPrototypeImp *funcProto)
    00085   : InternalFunctionImp(funcProto)
    00086 {
    00087   Value protect(this);
    00088   // ECMA 15.2.3.1
    00089   put(exec,"prototype", Object(objProto), DontEnum|DontDelete|ReadOnly);
    00090
    00091   // no. of arguments for constructor
    00092   put(exec,"length", Number(1), ReadOnly|DontDelete|DontEnum);
    00093 }
    00094
    00095
    00096 bool ObjectObjectImp::implementsConstruct() const
    00097 {
    00098   return true;
    00099 }
    00100
    00101 // ECMA 15.2.2
    00102 Object ObjectObjectImp::construct(ExecState *exec, const List &args)
    00103 {
    00104   // if no arguments have been passed ...
    00105   if (args.isEmpty()) {
    00106     Object proto = exec->interpreter()->builtinObjectPrototype();
    00107     Object result(new ObjectImp(proto));
    00108     return result;
    00109   }
    00110
    00111   Value arg = *(args.begin());
    00112   Object obj = Object::dynamicCast(arg);
    00113   if (!obj.isNull()) {
    00114     return obj;
    00115   }
    00116
    00117   switch (arg.type()) {
    00118   case StringType:
    00119   case BooleanType:
    00120   case NumberType:
    00121     return arg.toObject(exec);
    00122   default:
    00123     assert(!"unhandled switch case in ObjectConstructor");
    00124   case NullType:
    00125   case UndefinedType:
    00126     Object proto = exec->interpreter()->builtinObjectPrototype();
    00127     return Object(new ObjectImp(proto));
    00128   }
    00129 }
    00130
    00131 bool ObjectObjectImp::implementsCall() const
    00132 {
    00133   return true;
    00134 }
    00135
    00136 Value ObjectObjectImp::call(ExecState *exec, Object &/*thisObj*/, const List &args)
    00137 {
    00138   Value result;
    00139
    00140   List argList;
    00141   // Construct a new Object
    00142   if (args.isEmpty()) {
    00143     result = construct(exec,argList);
    00144   } else {
    00145     Value arg = args[0];
    00146     if (arg.type() == NullType || arg.type() == UndefinedType) {
    00147       argList.append(arg);
    00148       result = construct(exec,argList);
    00149     } else
    00150       result = arg.toObject(exec);
    00151   }
    00152   return result;
    00153 }
    00154

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:08:33 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001