/platform/external/webkit/WebCore/bindings/js/ScriptObject.cpp

https://github.com/aharish/totoro-gb-opensource-update2 · C++ · 207 lines · 149 code · 29 blank · 29 comment · 5 complexity · 21df9834f69978b7ba1b0466998f51f6 MD5 · raw file

  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. #include "config.h"
  31. #include "ScriptObject.h"
  32. #include "JSDOMBinding.h"
  33. #include <runtime/JSLock.h>
  34. #if ENABLE(INSPECTOR)
  35. #include "JSInjectedScriptHost.h"
  36. #include "JSInspectorBackend.h"
  37. #include "JSInspectorFrontendHost.h"
  38. #endif
  39. using namespace JSC;
  40. namespace WebCore {
  41. ScriptObject::ScriptObject(ScriptState* scriptState, JSObject* object)
  42. : ScriptValue(object)
  43. , m_scriptState(scriptState)
  44. {
  45. }
  46. static bool handleException(ScriptState* scriptState)
  47. {
  48. if (!scriptState->hadException())
  49. return true;
  50. reportException(scriptState, scriptState->exception());
  51. return false;
  52. }
  53. bool ScriptObject::set(const String& name, const String& value)
  54. {
  55. JSLock lock(SilenceAssertionsOnly);
  56. PutPropertySlot slot;
  57. jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsString(m_scriptState, value), slot);
  58. return handleException(m_scriptState);
  59. }
  60. bool ScriptObject::set(const char* name, const ScriptObject& value)
  61. {
  62. if (value.scriptState() != m_scriptState) {
  63. ASSERT_NOT_REACHED();
  64. return false;
  65. }
  66. JSLock lock(SilenceAssertionsOnly);
  67. PutPropertySlot slot;
  68. jsObject()->put(m_scriptState, Identifier(m_scriptState, name), value.jsObject(), slot);
  69. return handleException(m_scriptState);
  70. }
  71. bool ScriptObject::set(const char* name, const String& value)
  72. {
  73. JSLock lock(SilenceAssertionsOnly);
  74. PutPropertySlot slot;
  75. jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsString(m_scriptState, value), slot);
  76. return handleException(m_scriptState);
  77. }
  78. bool ScriptObject::set(const char* name, double value)
  79. {
  80. JSLock lock(SilenceAssertionsOnly);
  81. PutPropertySlot slot;
  82. jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
  83. return handleException(m_scriptState);
  84. }
  85. bool ScriptObject::set(const char* name, long 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. bool ScriptObject::set(const char* name, long long value)
  93. {
  94. JSLock lock(SilenceAssertionsOnly);
  95. PutPropertySlot slot;
  96. jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
  97. return handleException(m_scriptState);
  98. }
  99. bool ScriptObject::set(const char* name, int value)
  100. {
  101. JSLock lock(SilenceAssertionsOnly);
  102. PutPropertySlot slot;
  103. jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
  104. return handleException(m_scriptState);
  105. }
  106. bool ScriptObject::set(const char* name, unsigned value)
  107. {
  108. JSLock lock(SilenceAssertionsOnly);
  109. PutPropertySlot slot;
  110. jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
  111. return handleException(m_scriptState);
  112. }
  113. bool ScriptObject::set(const char* name, unsigned long value)
  114. {
  115. JSLock lock(SilenceAssertionsOnly);
  116. PutPropertySlot slot;
  117. jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsNumber(m_scriptState, value), slot);
  118. return handleException(m_scriptState);
  119. }
  120. bool ScriptObject::set(const char* name, bool value)
  121. {
  122. JSLock lock(SilenceAssertionsOnly);
  123. PutPropertySlot slot;
  124. jsObject()->put(m_scriptState, Identifier(m_scriptState, name), jsBoolean(value), slot);
  125. return handleException(m_scriptState);
  126. }
  127. ScriptObject ScriptObject::createNew(ScriptState* scriptState)
  128. {
  129. JSLock lock(SilenceAssertionsOnly);
  130. return ScriptObject(scriptState, constructEmptyObject(scriptState));
  131. }
  132. bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, const ScriptObject& value)
  133. {
  134. JSLock lock(SilenceAssertionsOnly);
  135. scriptState->lexicalGlobalObject()->putDirect(Identifier(scriptState, name), value.jsObject());
  136. return handleException(scriptState);
  137. }
  138. #if ENABLE(INSPECTOR)
  139. bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InspectorBackend* value)
  140. {
  141. JSLock lock(SilenceAssertionsOnly);
  142. JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
  143. globalObject->putDirect(Identifier(scriptState, name), toJS(scriptState, globalObject, value));
  144. return handleException(scriptState);
  145. }
  146. bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InspectorFrontendHost* value)
  147. {
  148. JSLock lock(SilenceAssertionsOnly);
  149. JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
  150. globalObject->putDirect(Identifier(scriptState, name), toJS(scriptState, globalObject, value));
  151. return handleException(scriptState);
  152. }
  153. bool ScriptGlobalObject::set(ScriptState* scriptState, const char* name, InjectedScriptHost* value)
  154. {
  155. JSLock lock(SilenceAssertionsOnly);
  156. JSDOMGlobalObject* globalObject = static_cast<JSDOMGlobalObject*>(scriptState->lexicalGlobalObject());
  157. globalObject->putDirect(Identifier(scriptState, name), toJS(scriptState, globalObject, value));
  158. return handleException(scriptState);
  159. }
  160. #endif // ENABLE(INSPECTOR)
  161. bool ScriptGlobalObject::get(ScriptState* scriptState, const char* name, ScriptObject& value)
  162. {
  163. JSLock lock(SilenceAssertionsOnly);
  164. JSValue jsValue = scriptState->lexicalGlobalObject()->get(scriptState, Identifier(scriptState, name));
  165. if (!jsValue)
  166. return false;
  167. if (!jsValue.isObject())
  168. return false;
  169. value = ScriptObject(scriptState, asObject(jsValue));
  170. return true;
  171. }
  172. bool ScriptGlobalObject::remove(ScriptState* scriptState, const char* name)
  173. {
  174. JSLock lock(SilenceAssertionsOnly);
  175. scriptState->lexicalGlobalObject()->deleteProperty(scriptState, Identifier(scriptState, name));
  176. return handleException(scriptState);
  177. }
  178. } // namespace WebCore