From 3610f211cef4dedb917974cf15b322ac3720e829 Mon Sep 17 00:00:00 2001 From: "beard%netscape.com" Date: Wed, 26 Apr 2000 21:27:44 +0000 Subject: [PATCH] added JSMap::setProperty()/getProperty(), and a prototype field. --- js/js2/jstypes.h | 39 +++++++++++++++++++++++++++++++++------ js2/src/jstypes.h | 39 +++++++++++++++++++++++++++++++++------ 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/js/js2/jstypes.h b/js/js2/jstypes.h index a04b5a0fa153..142fe12259a5 100644 --- a/js/js2/jstypes.h +++ b/js/js2/jstypes.h @@ -73,6 +73,7 @@ namespace JSTypes { JSValue() : f64(0.0) {} explicit JSValue(float64 f64) : f64(f64) {} + explicit JSValue(JSObject* obj) : object(obj) {} }; #if defined(XP_MAC) @@ -93,6 +94,8 @@ namespace JSTypes { */ typedef std::vector > JSValues; + extern const JSValue kUndefinedValue; + /** * Basic behavior of all JS objects, mapping a name to a value. * This is provided mainly to avoid having an awkward implementation @@ -101,12 +104,36 @@ namespace JSTypes { */ class JSMap : public gc_base { protected: - std::map, - gc_map_allocator> properties; + typedef std::map, gc_map_allocator> JSProperties; + JSProperties mProperties; + JSMap* mPrototype; public: JSValue& operator[](const String& name) { - return properties[name]; + return mProperties[name]; + } + + const JSValue& getProperty(const String& name) const + { + #ifdef XP_MAC + JSProperties::const_iterator i = mProperties.find(name); + if (i != mProperties.end()) + return i->second; + #else + // XXX should use map.find() to do this efficiently, but + // unfortunately, find() returns an iterator that is different + // on different STL implementations. + if (mProperties.count(name)) + return mProperties[name]; + #endif + if (mPrototype) + return mPrototype->getProperty(name); + return kUndefinedValue; + } + + JSValue& setProperty(const String& name, const JSValue& value) + { + return (mProperties[name] = value); } }; @@ -131,7 +158,7 @@ namespace JSTypes { public: JSValue& defineProperty(const String& name, JSValue &v) { - return (properties[name] = v); + return (mProperties[name] = v); } // FIXME: need to copy the ICodeModule's instruction stream. @@ -139,10 +166,10 @@ namespace JSTypes { { JSValue value; value.function = new JSFunction(iCode); - return properties[name] = value; + return mProperties[name] = value; } }; - + /** * Private representation of a JavaScript array. */ diff --git a/js2/src/jstypes.h b/js2/src/jstypes.h index a04b5a0fa153..142fe12259a5 100644 --- a/js2/src/jstypes.h +++ b/js2/src/jstypes.h @@ -73,6 +73,7 @@ namespace JSTypes { JSValue() : f64(0.0) {} explicit JSValue(float64 f64) : f64(f64) {} + explicit JSValue(JSObject* obj) : object(obj) {} }; #if defined(XP_MAC) @@ -93,6 +94,8 @@ namespace JSTypes { */ typedef std::vector > JSValues; + extern const JSValue kUndefinedValue; + /** * Basic behavior of all JS objects, mapping a name to a value. * This is provided mainly to avoid having an awkward implementation @@ -101,12 +104,36 @@ namespace JSTypes { */ class JSMap : public gc_base { protected: - std::map, - gc_map_allocator> properties; + typedef std::map, gc_map_allocator> JSProperties; + JSProperties mProperties; + JSMap* mPrototype; public: JSValue& operator[](const String& name) { - return properties[name]; + return mProperties[name]; + } + + const JSValue& getProperty(const String& name) const + { + #ifdef XP_MAC + JSProperties::const_iterator i = mProperties.find(name); + if (i != mProperties.end()) + return i->second; + #else + // XXX should use map.find() to do this efficiently, but + // unfortunately, find() returns an iterator that is different + // on different STL implementations. + if (mProperties.count(name)) + return mProperties[name]; + #endif + if (mPrototype) + return mPrototype->getProperty(name); + return kUndefinedValue; + } + + JSValue& setProperty(const String& name, const JSValue& value) + { + return (mProperties[name] = value); } }; @@ -131,7 +158,7 @@ namespace JSTypes { public: JSValue& defineProperty(const String& name, JSValue &v) { - return (properties[name] = v); + return (mProperties[name] = v); } // FIXME: need to copy the ICodeModule's instruction stream. @@ -139,10 +166,10 @@ namespace JSTypes { { JSValue value; value.function = new JSFunction(iCode); - return properties[name] = value; + return mProperties[name] = value; } }; - + /** * Private representation of a JavaScript array. */