1 /*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "ScriptObject.h"
33
34 #include "JSDOMBinding.h"
35
36 #if ENABLE(JAVASCRIPT_DEBUGGER)
37 #include "JSInspectorBackend.h"
38 #endif
39
40 #include <runtime/JSLock.h>
41
42 using namespace JSC;
43
44 namespace WebCore {
45
ScriptObject(ScriptState * scriptState,JSObject * object)46 ScriptObject::ScriptObject(ScriptState* scriptState, JSObject* object)
47 : ScriptValue(object)
48 , m_scriptState(scriptState)
49 {
50 }
51
handleException(ScriptState * scriptState)52 static bool handleException(ScriptState* scriptState)
53 {
54 if (!scriptState->hadException())
55 return true;
56
57 reportException(scriptState, scriptState->exception());
58 return false;
59 }
60
set(const String & name,const String & value)61 bool ScriptObject::set(const String& name, const String& value)
62 {
63 JSLock lock(SilenceAssertionsOnly);
64 PutPropertySlot slot;
65 jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsString(m_scriptState, value), slot);
66 return handleException(m_scriptState);
67 }
68
set(const char * name,const ScriptObject & value)69 bool ScriptObject::set(const char* name, const ScriptObject& value)
70 {
71 JSLock lock(SilenceAssertionsOnly);
72 PutPropertySlot slot;
73 jsObject()->put(m_scriptState, Identifier(m_scriptState, name), value.jsObject(), slot);
74 return handleException(m_scriptState);
75 }
76
set(const char * name,const String & value)77 bool ScriptObject::set(const char* name, const String& value)
78 {
79 JSLock lock(SilenceAssertionsOnly);
80 PutPropertySlot slot;
81 jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsString(m_scriptState, value), slot);
82 return handleException(m_scriptState);
83 }
84
set(const char * name,double value)85 bool ScriptObject::set(const char* name, double value)
86 {
87 JSLock lock(SilenceAssertionsOnly);
88 PutPropertySlot slot;
89 jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
90 return handleException(m_scriptState);
91 }
92
set(const char * name,long long value)93 bool ScriptObject::set(const char* name, long long value)
94 {
95 JSLock lock(SilenceAssertionsOnly);
96 PutPropertySlot slot;
97 jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
98 return handleException(m_scriptState);
99 }
100
set(const char * name,int value)101 bool ScriptObject::set(const char* name, int value)
102 {
103 JSLock lock(SilenceAssertionsOnly);
104 PutPropertySlot slot;
105 jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
106 return handleException(m_scriptState);
107 }
108
set(const char * name,bool value)109 bool ScriptObject::set(const char* name, bool value)
110 {
111 JSLock lock(SilenceAssertionsOnly);
112 PutPropertySlot slot;
113 jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsBoolean(value), slot);
114 return handleException(m_scriptState);
115 }
116
createNew(ScriptState * scriptState)117 ScriptObject ScriptObject::createNew(ScriptState* scriptState)
118 {
119 JSLock lock(SilenceAssertionsOnly);
120 return ScriptObject(scriptState, constructEmptyObject(scriptState));
121 }
122
set(ScriptState * scriptState,const char * name,const ScriptObject & value)123 bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, const ScriptObject& value)
124 {
125 JSLock lock(SilenceAssertionsOnly);
126 scriptState->lexicalGlobalObject()->putDirect(Identifier(scriptState, name), value.jsObject());
127 return handleException(scriptState);
128 }
129
130 #if ENABLE(JAVASCRIPT_DEBUGGER)
set(ScriptState * scriptState,const char * name,InspectorBackend * value)131 bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InspectorBackend* value)
132 {
133 JSLock lock(SilenceAssertionsOnly);
134 JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
135 globalObject->putDirect(Identifier(scriptState, name), toJS(scriptState, globalObject, value));
136 return handleException(scriptState);
137 }
138 #endif
139
get(ScriptState * scriptState,const char * name,ScriptObject & value)140 bool ScriptGlobalObject::get(ScriptState* scriptState, const char* name, ScriptObject& value)
141 {
142 JSLock lock(SilenceAssertionsOnly);
143 JSValue jsValue = scriptState->lexicalGlobalObject()->get(scriptState, Identifier(scriptState, name));
144 if (!jsValue)
145 return false;
146
147 if (!jsValue.isObject())
148 return false;
149
150 value = ScriptObject(scriptState, asObject(jsValue));
151 return true;
152 }
153
remove(ScriptState * scriptState,const char * name)154 bool ScriptGlobalObject::remove(ScriptState* scriptState, const char* name)
155 {
156 JSLock lock(SilenceAssertionsOnly);
157 scriptState->lexicalGlobalObject()->deleteProperty(scriptState, Identifier(scriptState, name));
158 return handleException(scriptState);
159 }
160
161 } // namespace WebCore
162