2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved. 
   4  * Redistribution and use in source and binary forms, with or without 
   5  * modification, are permitted provided that the following conditions 
   7  * 1. Redistributions of source code must retain the above copyright 
   8  *    notice, this list of conditions and the following disclaimer. 
   9  * 2. Redistributions in binary form must reproduce the above copyright 
  10  *    notice, this list of conditions and the following disclaimer in the 
  11  *    documentation and/or other materials provided with the distribution. 
  13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 
  14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR 
  17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
  20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
  21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
  22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
  23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
  28 #if ENABLE(NETSCAPE_API) 
  30 #include "c_instance.h" 
  33 #include "c_runtime.h" 
  34 #include "c_utility.h" 
  36 #include "npruntime_impl.h" 
  37 #include "PropertyNameArray.h" 
  38 #include "runtime_root.h" 
  39 #include <wtf/Assertions.h> 
  40 #include <wtf/StringExtras.h> 
  41 #include <wtf/Vector.h> 
  46 CInstance::CInstance(NPObject
* o
, PassRefPtr
<RootObject
> rootObject
) 
  47     : Instance(rootObject
) 
  49     _object 
= _NPN_RetainObject(o
); 
  53 CInstance::~CInstance()  
  55     _NPN_ReleaseObject(_object
); 
  58 Class 
*CInstance::getClass() const 
  61         _class 
= CClass::classForIsA(_object
->_class
); 
  65 void CInstance::begin() 
  75 bool CInstance::implementsCall() const 
  77     return (_object
->_class
->invokeDefault 
!= 0); 
  80 JSValue
* CInstance::invokeMethod(ExecState
* exec
, const MethodList
& methodList
, const List
& args
) 
  82     // Overloading methods are not allowed by NPObjects.  Should only be one 
  83     // name match for a particular method. 
  84     ASSERT(methodList
.size() == 1); 
  86     CMethod
* method 
= static_cast<CMethod
*>(methodList
[0]); 
  88     NPIdentifier ident 
= _NPN_GetStringIdentifier(method
->name()); 
  89     if (!_object
->_class
->hasMethod(_object
, ident
)) 
  92     unsigned count 
= args
.size(); 
  93     Vector
<NPVariant
, 128> cArgs(count
); 
  96     for (i 
= 0; i 
< count
; i
++) 
  97         convertValueToNPVariant(exec
, args
.at(i
), &cArgs
[i
]); 
  99     // Invoke the 'C' method. 
 100     NPVariant resultVariant
; 
 101     VOID_TO_NPVARIANT(resultVariant
); 
 104        JSLock::DropAllLocks dropAllLocks
; 
 105         _object
->_class
->invoke(_object
, ident
, cArgs
.data(), count
, &resultVariant
); 
 108     for (i 
= 0; i 
< count
; i
++) 
 109         _NPN_ReleaseVariantValue(&cArgs
[i
]); 
 111     JSValue
* resultValue 
= convertNPVariantToValue(exec
, &resultVariant
, _rootObject
.get()); 
 112     _NPN_ReleaseVariantValue(&resultVariant
); 
 117 JSValue
* CInstance::invokeDefaultMethod(ExecState
* exec
, const List
& args
) 
 119     if (!_object
->_class
->invokeDefault
) 
 120         return jsUndefined(); 
 122     unsigned count 
= args
.size(); 
 123     Vector
<NPVariant
, 128> cArgs(count
); 
 126     for (i 
= 0; i 
< count
; i
++) 
 127         convertValueToNPVariant(exec
, args
.at(i
), &cArgs
[i
]); 
 129     // Invoke the 'C' method. 
 130     NPVariant resultVariant
; 
 131     VOID_TO_NPVARIANT(resultVariant
); 
 133        JSLock::DropAllLocks dropAllLocks
; 
 134         _object
->_class
->invokeDefault(_object
, cArgs
.data(), count
, &resultVariant
); 
 137     for (i 
= 0; i 
< count
; i
++) 
 138         _NPN_ReleaseVariantValue(&cArgs
[i
]); 
 140     JSValue
* resultValue 
= convertNPVariantToValue(exec
, &resultVariant
, _rootObject
.get()); 
 141     _NPN_ReleaseVariantValue(&resultVariant
); 
 146 JSValue
* CInstance::defaultValue(JSType hint
) const 
 148     if (hint 
== StringType
) 
 149         return stringValue(); 
 150     if (hint 
== NumberType
) 
 151         return numberValue(); 
 152    if (hint 
== BooleanType
) 
 153         return booleanValue(); 
 157 JSValue
* CInstance::stringValue() const 
 160     snprintf(buf
, sizeof(buf
), "NPObject %p, NPClass %p", _object
, _object
->_class
); 
 161     return jsString(buf
); 
 164 JSValue
* CInstance::numberValue() const 
 166     // FIXME: Implement something sensible. 
 170 JSValue
* CInstance::booleanValue() const 
 172     // FIXME: Implement something sensible. 
 173     return jsBoolean(false); 
 176 JSValue
* CInstance::valueOf() const  
 178     return stringValue(); 
 181 void CInstance::getPropertyNames(ExecState
*, PropertyNameArray
& nameArray
)  
 183     if (!NP_CLASS_STRUCT_VERSION_HAS_ENUM(_object
->_class
) || 
 184         !_object
->_class
->enumerate
) 
 188     NPIdentifier
* identifiers
; 
 191         JSLock::DropAllLocks dropAllLocks
; 
 192         if (!_object
->_class
->enumerate(_object
, &identifiers
, &count
)) 
 196     for (unsigned i 
= 0; i 
< count
; i
++) { 
 197         PrivateIdentifier
* identifier 
= static_cast<PrivateIdentifier
*>(identifiers
[i
]); 
 199         if (identifier
->isString
) 
 200             nameArray
.add(identifierFromNPIdentifier(identifier
->value
.string
)); 
 202             nameArray
.add(Identifier::from(identifier
->value
.number
)); 
 205     // FIXME: This should really call NPN_MemFree but that's in WebKit 
 212 #endif // ENABLE(NETSCAPE_API)