JavaScriptVariant.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "JavaScriptVariant.h"
  20. #include "JavaScriptVariant_p.h"
  21. #include "WebPage.h"
  22. #include <JSStringRef.h>
  23. #include <JSValueRef.h>
  24. #include <stdlib.h>
  25. #include <wtf/Vector.h>
  26. namespace BlackBerry {
  27. namespace WebKit {
  28. JavaScriptVariant JSValueRefToBlackBerryJavaScriptVariant(const JSGlobalContextRef& ctx, const JSValueRef& value)
  29. {
  30. JavaScriptVariant returnValue;
  31. switch (JSValueGetType(ctx, value)) {
  32. case kJSTypeNull:
  33. returnValue.setType(JavaScriptVariant::Null);
  34. break;
  35. case kJSTypeBoolean:
  36. returnValue.setBoolean(JSValueToBoolean(ctx, value));
  37. break;
  38. case kJSTypeNumber:
  39. returnValue.setDouble(JSValueToNumber(ctx, value, 0));
  40. break;
  41. case kJSTypeString: {
  42. JSStringRef stringRef = JSValueToStringCopy(ctx, value, 0);
  43. if (!stringRef) {
  44. returnValue.setString(BlackBerry::Platform::String::emptyString());
  45. break;
  46. }
  47. size_t bufferSize = JSStringGetMaximumUTF8CStringSize(stringRef);
  48. WTF::Vector<char> buffer(bufferSize);
  49. size_t rc = JSStringGetUTF8CString(stringRef, buffer.data(), bufferSize);
  50. returnValue.setString(BlackBerry::Platform::String::fromUtf8(buffer.data(), rc - 1));
  51. JSStringRelease(stringRef);
  52. break;
  53. }
  54. case kJSTypeObject:
  55. returnValue.setType(JavaScriptVariant::Object);
  56. break;
  57. case kJSTypeUndefined:
  58. returnValue.setType(JavaScriptVariant::Undefined);
  59. break;
  60. }
  61. return returnValue;
  62. }
  63. JSValueRef BlackBerryJavaScriptVariantToJSValueRef(const JSGlobalContextRef& ctx, const JavaScriptVariant& variant)
  64. {
  65. JSValueRef ref = 0;
  66. switch (variant.type()) {
  67. case JavaScriptVariant::Undefined:
  68. ref = JSValueMakeUndefined(ctx);
  69. break;
  70. case JavaScriptVariant::Null:
  71. ref = JSValueMakeNull(ctx);
  72. break;
  73. case JavaScriptVariant::Boolean:
  74. ref = JSValueMakeBoolean(ctx, variant.booleanValue());
  75. break;
  76. case JavaScriptVariant::Number:
  77. ref = JSValueMakeNumber(ctx, variant.doubleValue());
  78. break;
  79. case JavaScriptVariant::String: {
  80. JSStringRef str = JSStringCreateWithUTF8CString(variant.stringValue().c_str());
  81. ref = JSValueMakeString(ctx, str);
  82. JSStringRelease(str);
  83. break;
  84. }
  85. case JavaScriptVariant::Exception:
  86. case JavaScriptVariant::Object:
  87. ASSERT_NOT_REACHED();
  88. break;
  89. }
  90. return ref;
  91. }
  92. JavaScriptVariant::JavaScriptVariant()
  93. : m_type(Undefined)
  94. {
  95. }
  96. JavaScriptVariant::JavaScriptVariant(double value)
  97. : m_type(Undefined)
  98. {
  99. setDouble(value);
  100. }
  101. JavaScriptVariant::JavaScriptVariant(int value)
  102. : m_type(Undefined)
  103. {
  104. setDouble(value);
  105. }
  106. JavaScriptVariant::JavaScriptVariant(const BlackBerry::Platform::String& value)
  107. : m_type(Undefined)
  108. {
  109. setString(value);
  110. }
  111. JavaScriptVariant::JavaScriptVariant(bool value)
  112. : m_type(Undefined)
  113. {
  114. setBoolean(value);
  115. }
  116. JavaScriptVariant::JavaScriptVariant(const JavaScriptVariant &v)
  117. : m_type(Undefined)
  118. {
  119. this->operator=(v);
  120. }
  121. JavaScriptVariant::~JavaScriptVariant()
  122. {
  123. }
  124. JavaScriptVariant& JavaScriptVariant::operator=(const JavaScriptVariant& v)
  125. {
  126. if (&v == this)
  127. return *this;
  128. switch (v.type()) {
  129. case Boolean:
  130. setBoolean(v.booleanValue());
  131. break;
  132. case Number:
  133. setDouble(v.doubleValue());
  134. break;
  135. case String:
  136. setString(v.stringValue());
  137. break;
  138. default:
  139. setType(v.type());
  140. break;
  141. }
  142. return *this;
  143. }
  144. void JavaScriptVariant::setType(const DataType& type)
  145. {
  146. if (m_type == String)
  147. m_stringValue = BlackBerry::Platform::String::emptyString();
  148. m_type = type;
  149. }
  150. JavaScriptVariant::DataType JavaScriptVariant::type() const
  151. {
  152. return m_type;
  153. }
  154. void JavaScriptVariant::setDouble(double value)
  155. {
  156. setType(Number);
  157. m_doubleValue = value;
  158. }
  159. double JavaScriptVariant::doubleValue() const
  160. {
  161. return m_doubleValue;
  162. }
  163. void JavaScriptVariant::setString(const BlackBerry::Platform::String& value)
  164. {
  165. setType(String);
  166. m_stringValue = value;
  167. }
  168. const BlackBerry::Platform::String& JavaScriptVariant::stringValue() const
  169. {
  170. return m_stringValue;
  171. }
  172. void JavaScriptVariant::setBoolean(bool value)
  173. {
  174. setType(Boolean);
  175. m_booleanValue = value;
  176. }
  177. bool JavaScriptVariant::booleanValue() const
  178. {
  179. return m_booleanValue;
  180. }
  181. }
  182. }