001 
002 /*
003  *  Descripter 1.0 - Java Script Engines
004  *  Copyright (C) 2010-2015  Jianjun Liu (J.J.Liu)
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package org.descripter.js.api.core;
021 
022 import org.descripter.js.api.Core;
023 import org.descripter.js.api.Function;
024 import org.descripter.js.api.Objective;
025 
026 /**
027  * <p>Emulates JavaScript Object objects.</p>
028  * 
029  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.descripter.org" target="_blank">http://www.descripter.org</a>
030  * @since Descripter 1.0
031  */
032 public class CObject extends Objective<CObject>
033 {
034     /**
035      * <p>Emulates the <tt>null</tt> object in JavaScript.</p>
036      * @since Descripter 1.0
037      */
038     public final static CObject _null = new CObject(null) {
039         @Override
040         public String toString() {
041             return "null";
042         }
043     };
044 
045     /**
046      * <p>The <tt>constructor</tt> function of the current object.</p>
047      * @since Descripter 1.0
048      */
049     public final Function<?> constructor;
050 
051     /**
052      * <p>Constructs an {@link Objective} context of this type.</p>
053      * @param constructor The constructor {@link Function} object.
054      * @since Descripter 1.0
055      */
056     public CObject(Function<?> constructor) {
057         super(constructor != null ? constructor.prototype() : null);
058         this.constructor = constructor;
059         if (constructor != null) {
060             put(constructor.core()._constructor, constructor);
061         }
062     }
063 
064     /**
065      * <p>Gets the engine {@link Core} of this {@link Objective} context.</p>
066      * @return The engine {@link Core} of this {@link Objective} context
067      * @since Descripter 1.0
068      */
069     public Core core() {
070         return constructor.core();
071     }
072 
073     /**
074      * <p>Returns a string representation of the current object.</p>
075      * @return The string representation of the current object
076      * @since Descripter 1.0
077      */
078     @Override
079     public String toString() {
080         return "[Object object]";
081     }
082 
083     /**
084      * <p>Converts the current object instance to a string, localized as appropriate 
085      * for the current locale.</p>
086      * @return The string representation of the current object, localized as 
087      * appropriate for the current locale
088      * @since Descripter 1.0
089      */
090     public String toLocaleString() {
091         return toString();
092     }
093 
094     /**
095      * <p>Returns the primitive value associated with this object, if there is one. </p>
096      * @return The primitive value associated with this object, if there is one, or this object itself.
097      * @since Descripter 1.0
098      */
099     public Object valueOf() {
100         return this;
101     }
102 }