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 "ScriptGlobalObject.h"
33
34#include "JSDOMBinding.h"
35#include <bindings/ScriptObject.h>
36#include <runtime/IdentifierInlines.h>
37#include <runtime/JSLock.h>
38
39#if ENABLE(INSPECTOR)
40#include "JSInspectorFrontendHost.h"
41#endif
42
43using namespace JSC;
44
45namespace WebCore {
46
47static bool handleException(JSC::ExecState* scriptState)
48{
49    if (!scriptState->hadException())
50        return true;
51
52    reportException(scriptState, scriptState->exception());
53    return false;
54}
55
56bool ScriptGlobalObject::set(JSC::ExecState* scriptState, const char* name, const Deprecated::ScriptObject& value)
57{
58    JSLockHolder lock(scriptState);
59    scriptState->lexicalGlobalObject()->putDirect(scriptState->vm(), Identifier(scriptState, name), value.jsObject());
60    return handleException(scriptState);
61}
62
63#if ENABLE(INSPECTOR)
64bool ScriptGlobalObject::set(JSC::ExecState* scriptState, const char* name, InspectorFrontendHost* value)
65{
66    JSLockHolder lock(scriptState);
67    JSDOMGlobalObject* globalObject = jsCast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
68    globalObject->putDirect(scriptState->vm(), Identifier(scriptState, name), toJS(scriptState, globalObject, value));
69    return handleException(scriptState);
70}
71#endif // ENABLE(INSPECTOR)
72
73bool ScriptGlobalObject::get(JSC::ExecState* scriptState, const char* name, Deprecated::ScriptObject& value)
74{
75    JSLockHolder lock(scriptState);
76    JSValue jsValue = scriptState->lexicalGlobalObject()->get(scriptState, Identifier(scriptState, name));
77    if (!jsValue)
78        return false;
79
80    if (!jsValue.isObject())
81        return false;
82
83    value = Deprecated::ScriptObject(scriptState, asObject(jsValue));
84    return true;
85}
86
87bool ScriptGlobalObject::remove(JSC::ExecState* scriptState, const char* name)
88{
89    JSLockHolder lock(scriptState);
90    scriptState->lexicalGlobalObject()->methodTable()->deleteProperty(scriptState->lexicalGlobalObject(), scriptState, Identifier(scriptState, name));
91    return handleException(scriptState);
92}
93
94} // namespace WebCore
95