]> git.saurik.com Git - apple/javascriptcore.git/blob - bindings/c/c_instance.cpp
JavaScriptCore-461.tar.gz
[apple/javascriptcore.git] / bindings / c / c_instance.cpp
1 /*
2 * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26 #include "config.h"
27
28 #if ENABLE(NETSCAPE_API)
29
30 #include "c_instance.h"
31
32 #include "c_class.h"
33 #include "c_runtime.h"
34 #include "c_utility.h"
35 #include "list.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>
42
43 namespace KJS {
44 namespace Bindings {
45
46 CInstance::CInstance(NPObject* o, PassRefPtr<RootObject> rootObject)
47 : Instance(rootObject)
48 {
49 _object = _NPN_RetainObject(o);
50 _class = 0;
51 }
52
53 CInstance::~CInstance()
54 {
55 _NPN_ReleaseObject(_object);
56 }
57
58 Class *CInstance::getClass() const
59 {
60 if (!_class)
61 _class = CClass::classForIsA(_object->_class);
62 return _class;
63 }
64
65 void CInstance::begin()
66 {
67 // Do nothing.
68 }
69
70 void CInstance::end()
71 {
72 // Do nothing.
73 }
74
75 bool CInstance::implementsCall() const
76 {
77 return (_object->_class->invokeDefault != 0);
78 }
79
80 JSValue* CInstance::invokeMethod(ExecState* exec, const MethodList& methodList, const List& args)
81 {
82 // Overloading methods are not allowed by NPObjects. Should only be one
83 // name match for a particular method.
84 ASSERT(methodList.size() == 1);
85
86 CMethod* method = static_cast<CMethod*>(methodList[0]);
87
88 NPIdentifier ident = _NPN_GetStringIdentifier(method->name());
89 if (!_object->_class->hasMethod(_object, ident))
90 return jsUndefined();
91
92 unsigned count = args.size();
93 Vector<NPVariant, 128> cArgs(count);
94
95 unsigned i;
96 for (i = 0; i < count; i++)
97 convertValueToNPVariant(exec, args.at(i), &cArgs[i]);
98
99 // Invoke the 'C' method.
100 NPVariant resultVariant;
101 VOID_TO_NPVARIANT(resultVariant);
102
103 {
104 JSLock::DropAllLocks dropAllLocks;
105 _object->_class->invoke(_object, ident, cArgs.data(), count, &resultVariant);
106 }
107
108 for (i = 0; i < count; i++)
109 _NPN_ReleaseVariantValue(&cArgs[i]);
110
111 JSValue* resultValue = convertNPVariantToValue(exec, &resultVariant, _rootObject.get());
112 _NPN_ReleaseVariantValue(&resultVariant);
113 return resultValue;
114 }
115
116
117 JSValue* CInstance::invokeDefaultMethod(ExecState* exec, const List& args)
118 {
119 if (!_object->_class->invokeDefault)
120 return jsUndefined();
121
122 unsigned count = args.size();
123 Vector<NPVariant, 128> cArgs(count);
124
125 unsigned i;
126 for (i = 0; i < count; i++)
127 convertValueToNPVariant(exec, args.at(i), &cArgs[i]);
128
129 // Invoke the 'C' method.
130 NPVariant resultVariant;
131 VOID_TO_NPVARIANT(resultVariant);
132 {
133 JSLock::DropAllLocks dropAllLocks;
134 _object->_class->invokeDefault(_object, cArgs.data(), count, &resultVariant);
135 }
136
137 for (i = 0; i < count; i++)
138 _NPN_ReleaseVariantValue(&cArgs[i]);
139
140 JSValue* resultValue = convertNPVariantToValue(exec, &resultVariant, _rootObject.get());
141 _NPN_ReleaseVariantValue(&resultVariant);
142 return resultValue;
143 }
144
145
146 JSValue* CInstance::defaultValue(JSType hint) const
147 {
148 if (hint == StringType)
149 return stringValue();
150 if (hint == NumberType)
151 return numberValue();
152 if (hint == BooleanType)
153 return booleanValue();
154 return valueOf();
155 }
156
157 JSValue* CInstance::stringValue() const
158 {
159 char buf[1024];
160 snprintf(buf, sizeof(buf), "NPObject %p, NPClass %p", _object, _object->_class);
161 return jsString(buf);
162 }
163
164 JSValue* CInstance::numberValue() const
165 {
166 // FIXME: Implement something sensible.
167 return jsNumber(0);
168 }
169
170 JSValue* CInstance::booleanValue() const
171 {
172 // FIXME: Implement something sensible.
173 return jsBoolean(false);
174 }
175
176 JSValue* CInstance::valueOf() const
177 {
178 return stringValue();
179 }
180
181 void CInstance::getPropertyNames(ExecState*, PropertyNameArray& nameArray)
182 {
183 if (!NP_CLASS_STRUCT_VERSION_HAS_ENUM(_object->_class) ||
184 !_object->_class->enumerate)
185 return;
186
187 unsigned count;
188 NPIdentifier* identifiers;
189
190 {
191 JSLock::DropAllLocks dropAllLocks;
192 if (!_object->_class->enumerate(_object, &identifiers, &count))
193 return;
194 }
195
196 for (unsigned i = 0; i < count; i++) {
197 PrivateIdentifier* identifier = static_cast<PrivateIdentifier*>(identifiers[i]);
198
199 if (identifier->isString)
200 nameArray.add(identifierFromNPIdentifier(identifier->value.string));
201 else
202 nameArray.add(Identifier::from(identifier->value.number));
203 }
204
205 // FIXME: This should really call NPN_MemFree but that's in WebKit
206 free(identifiers);
207 }
208
209 }
210 }
211
212 #endif // ENABLE(NETSCAPE_API)