• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2004 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_PLUGIN_API)
29 
30 #include "c_runtime.h"
31 
32 #include "c_instance.h"
33 #include "c_utility.h"
34 #include "npruntime_impl.h"
35 #ifdef ANDROID_NPN_SETEXCEPTION
36 #include "runtime/Error.h"
37 #endif  // ANDROID_NPN_SETEXCEPTION
38 #include <runtime/JSLock.h>
39 
40 namespace JSC {
41 namespace Bindings {
42 
43 #ifdef ANDROID_NPN_SETEXCEPTION
44 /*
45  * When throwing an exception, we need to use the current ExecState.
46  * The following two methods implement a similar solution to the
47  * Objective-C implementation, where _NPN_SetException set a global
48  * exception (using SetGlobalException).
49  * We then test (using MoveGlobalExceptionToExecState) if the exception
50  * is set, after each javascript call that might result in an exception.
51  * If the exception is set we throw it with the passed ExecState.
52  */
53 
54 static UString* globalLastException = 0;
55 
SetGlobalException(const NPUTF8 * exception)56 void SetGlobalException(const NPUTF8* exception)
57 {
58     if (globalLastException != 0) {
59         delete globalLastException;
60         globalLastException = 0;
61     }
62     if (exception != 0)
63         globalLastException = new UString(exception);
64 }
65 
MoveGlobalExceptionToExecState(ExecState * exec)66 void MoveGlobalExceptionToExecState(ExecState* exec)
67 {
68     if (!globalLastException)
69         return;
70     JSLock lock(exec);
71     throwError(exec, GeneralError, *globalLastException);
72     SetGlobalException(0);
73 }
74 #endif // ANDROID_NPN_SETEXCEPTION
75 
valueFromInstance(ExecState * exec,const Instance * inst) const76 JSValue CField::valueFromInstance(ExecState* exec, const Instance* inst) const
77 {
78     const CInstance* instance = static_cast<const CInstance*>(inst);
79     NPObject* obj = instance->getObject();
80     if (obj->_class->getProperty) {
81         NPVariant property;
82         VOID_TO_NPVARIANT(property);
83 
84 #ifdef ANDROID_NPN_SETEXCEPTION
85         SetGlobalException(0);
86 #endif  // ANDROID_NPN_SETEXCEPTION
87         bool result;
88         {
89             JSLock::DropAllLocks dropAllLocks(SilenceAssertionsOnly);
90             result = obj->_class->getProperty(obj, _fieldIdentifier, &property);
91         }
92 #ifdef ANDROID_NPN_SETEXCEPTION
93         MoveGlobalExceptionToExecState(exec);
94 #endif  // ANDROID_NPN_SETEXCEPTION
95         if (result) {
96             JSValue result = convertNPVariantToValue(exec, &property, instance->rootObject());
97             _NPN_ReleaseVariantValue(&property);
98             return result;
99         }
100     }
101     return jsUndefined();
102 }
103 
setValueToInstance(ExecState * exec,const Instance * inst,JSValue aValue) const104 void CField::setValueToInstance(ExecState *exec, const Instance *inst, JSValue aValue) const
105 {
106     const CInstance* instance = static_cast<const CInstance*>(inst);
107     NPObject* obj = instance->getObject();
108     if (obj->_class->setProperty) {
109         NPVariant variant;
110         convertValueToNPVariant(exec, aValue, &variant);
111 
112 #ifdef ANDROID_NPN_SETEXCEPTION
113         SetGlobalException(0);
114 #endif  // ANDROID_NPN_SETEXCEPTION
115         {
116             JSLock::DropAllLocks dropAllLocks(SilenceAssertionsOnly);
117             obj->_class->setProperty(obj, _fieldIdentifier, &variant);
118         }
119 
120         _NPN_ReleaseVariantValue(&variant);
121 #ifdef ANDROID_NPN_SETEXCEPTION
122         MoveGlobalExceptionToExecState(exec);
123 #endif  // ANDROID_NPN_SETEXCEPTION
124     }
125 }
126 
127 } }
128 
129 #endif // ENABLE(NETSCAPE_PLUGIN_API)
130