Title: [125324] trunk/Source/WebKit
Revision
125324
Author
[email protected]
Date
2012-08-10 13:40:11 -0700 (Fri, 10 Aug 2012)

Log Message

Introduce _javascript_Variant object.
https://bugs.webkit.org/show_bug.cgi?id=93644

The _javascript_Variant can be used for functions that return results from
_javascript_ such as executeJavaScript or functions that take arguments
to pass a variable to _javascript_ and they want to make sure the type
is correct.

Convert executeJavaScriptFunction() to use this data type as the
example, other functions will be converted in the future.

PR 14929

Internally reviewed by Joe Mason
Patch by Benjamin C Meyer <[email protected]> on 2012-08-10
Reviewed by Adam Treat

Source/WebKit:

* PlatformBlackBerry.cmake:

Source/WebKit/blackberry:

* Api/_javascript_Variant.cpp: Added.
(WebKit):
(BlackBerry::WebKit::JSValueRefToBlackBerryJavaScriptVariant):
(BlackBerry::WebKit::BlackBerryJavaScriptVariantToJSValueRef):
(BlackBerry::WebKit::_javascript_Variant::_javascript_Variant):
(BlackBerry::WebKit::_javascript_Variant::~_javascript_Variant):
(BlackBerry::WebKit::_javascript_Variant::operator=):
(BlackBerry::WebKit::_javascript_Variant::setType):
(BlackBerry::WebKit::_javascript_Variant::type):
(BlackBerry::WebKit::_javascript_Variant::setDouble):
(BlackBerry::WebKit::_javascript_Variant::doubleValue):
(BlackBerry::WebKit::_javascript_Variant::setString):
(BlackBerry::WebKit::_javascript_Variant::stringValue):
(BlackBerry::WebKit::_javascript_Variant::setBoolean):
(BlackBerry::WebKit::_javascript_Variant::booleanValue):
* Api/_javascript_Variant.h: Added.
(WebKit):
* Api/_javascript_Variant_p.h: Added.
(WebKit):
* Api/WebPage.cpp:
(BlackBerry::WebKit::WebPage::executeJavaScriptFunction):
* Api/WebPage.h:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (125323 => 125324)


--- trunk/Source/WebKit/ChangeLog	2012-08-10 20:30:45 UTC (rev 125323)
+++ trunk/Source/WebKit/ChangeLog	2012-08-10 20:40:11 UTC (rev 125324)
@@ -1,3 +1,23 @@
+2012-08-10  Benjamin C Meyer  <[email protected]>
+
+        Introduce _javascript_Variant object.
+        https://bugs.webkit.org/show_bug.cgi?id=93644
+
+        The _javascript_Variant can be used for functions that return results from
+        _javascript_ such as executeJavaScript or functions that take arguments
+        to pass a variable to _javascript_ and they want to make sure the type
+        is correct.
+
+        Convert executeJavaScriptFunction() to use this data type as the
+        example, other functions will be converted in the future.
+
+        PR 14929
+
+        Internally reviewed by Joe Mason
+        Reviewed by Adam Treat
+
+        * PlatformBlackBerry.cmake:
+
 2012-08-10  Gyuyoung Kim  <[email protected]>
 
         [BlackBerry] Fix wrong include path for notifications.

Modified: trunk/Source/WebKit/PlatformBlackBerry.cmake (125323 => 125324)


--- trunk/Source/WebKit/PlatformBlackBerry.cmake	2012-08-10 20:30:45 UTC (rev 125323)
+++ trunk/Source/WebKit/PlatformBlackBerry.cmake	2012-08-10 20:40:11 UTC (rev 125324)
@@ -69,6 +69,7 @@
     blackberry/Api/WebSettings.cpp
     blackberry/Api/WebString.cpp
     blackberry/Api/WebViewportArguments.cpp
+    blackberry/Api/_javascript_Variant.cpp
     blackberry/WebCoreSupport/AutofillManager.cpp
     blackberry/WebCoreSupport/CacheClientBlackBerry.cpp
     blackberry/WebCoreSupport/ChromeClientBlackBerry.cpp

Added: trunk/Source/WebKit/blackberry/Api/_javascript_Variant.cpp (0 => 125324)


--- trunk/Source/WebKit/blackberry/Api/_javascript_Variant.cpp	                        (rev 0)
+++ trunk/Source/WebKit/blackberry/Api/_javascript_Variant.cpp	2012-08-10 20:40:11 UTC (rev 125324)
@@ -0,0 +1,215 @@
+/*
+ * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "config.h"
+#include "_javascript_Variant.h"
+
+#include "_javascript_Variant_p.h"
+#include "WebPage.h"
+#include <JSStringRef.h>
+#include <JSValueRef.h>
+#include <stdlib.h>
+#include <wtf/Vector.h>
+
+namespace BlackBerry {
+namespace WebKit {
+
+_javascript_Variant JSValueRefToBlackBerryJavaScriptVariant(const JSGlobalContextRef& ctx, const JSValueRef& value)
+{
+    _javascript_Variant returnValue;
+
+    switch (JSValueGetType(ctx, value)) {
+    case kJSTypeNull:
+        returnValue.setType(_javascript_Variant::Null);
+        break;
+    case kJSTypeBoolean:
+        returnValue.setBoolean(JSValueToBoolean(ctx, value));
+        break;
+    case kJSTypeNumber:
+        returnValue.setDouble(JSValueToNumber(ctx, value, 0));
+        break;
+    case kJSTypeString: {
+        JSStringRef stringRef = JSValueToStringCopy(ctx, value, 0);
+        size_t bufferSize = JSStringGetMaximumUTF8CStringSize(stringRef);
+        WTF::Vector<char> buffer(bufferSize);
+        JSStringGetUTF8CString(stringRef, buffer.data(), bufferSize);
+        returnValue.setString(WebString::fromUtf8(buffer.data()).utf8().c_str());
+        break;
+    }
+    case kJSTypeObject:
+        returnValue.setType(_javascript_Variant::Object);
+        break;
+    case kJSTypeUndefined:
+        returnValue.setType(_javascript_Variant::Undefined);
+        break;
+    }
+    return returnValue;
+}
+
+JSValueRef BlackBerryJavaScriptVariantToJSValueRef(const JSGlobalContextRef& ctx, const _javascript_Variant& variant)
+{
+    JSValueRef ref = 0;
+    switch (variant.type()) {
+    case _javascript_Variant::Undefined:
+        ref = JSValueMakeUndefined(ctx);
+        break;
+    case _javascript_Variant::Null:
+        ref = JSValueMakeNull(ctx);
+        break;
+    case _javascript_Variant::Boolean:
+        ref = JSValueMakeBoolean(ctx, variant.booleanValue());
+        break;
+    case _javascript_Variant::Number:
+        ref = JSValueMakeNumber(ctx, variant.doubleValue());
+        break;
+    case _javascript_Variant::String: {
+        JSStringRef str = JSStringCreateWithUTF8CString(variant.stringValue());
+        ref = JSValueMakeString(ctx, str);
+        JSStringRelease(str);
+        break;
+    }
+    case _javascript_Variant::Exception:
+    case _javascript_Variant::Object:
+        ASSERT_NOT_REACHED();
+        break;
+    }
+    return ref;
+}
+
+_javascript_Variant::_javascript_Variant()
+    : m_type(Undefined)
+    , m_stringValue(0)
+{
+}
+
+_javascript_Variant::_javascript_Variant(double value)
+    : m_type(Undefined)
+    , m_stringValue(0)
+{
+    setDouble(value);
+}
+
+_javascript_Variant::_javascript_Variant(int value)
+    : m_type(Undefined)
+    , m_stringValue(0)
+{
+    setDouble(value);
+}
+
+_javascript_Variant::_javascript_Variant(const char* value)
+    : m_type(Undefined)
+    , m_stringValue(0)
+{
+    setString(value);
+}
+
+_javascript_Variant::_javascript_Variant(const std::string& value)
+    : m_type(Undefined)
+    , m_stringValue(0)
+{
+    setString(value.c_str());
+}
+
+_javascript_Variant::_javascript_Variant(bool value)
+    : m_type(Undefined)
+    , m_stringValue(0)
+{
+    setBoolean(value);
+}
+
+_javascript_Variant::_javascript_Variant(const _javascript_Variant &v)
+    : m_type(Undefined)
+    , m_stringValue(0)
+{
+    this->operator=(v);
+}
+
+_javascript_Variant::~_javascript_Variant()
+{
+    // Prevent memory leaks if we have strings
+    setType(Undefined);
+}
+
+_javascript_Variant& _javascript_Variant::operator=(const _javascript_Variant& v)
+{
+    switch (v.type()) {
+    case Boolean:
+        setBoolean(v.booleanValue());
+        break;
+    case Number:
+        setDouble(v.doubleValue());
+        break;
+    case String:
+        setString(v.stringValue());
+        break;
+    default:
+        setType(v.type());
+        break;
+    }
+
+    return *this;
+}
+
+void _javascript_Variant::setType(const DataType& type)
+{
+    if (m_type == String)
+        free(m_stringValue);
+    m_type = type;
+    m_stringValue = 0;
+}
+
+_javascript_Variant::DataType _javascript_Variant::type() const
+{
+    return m_type;
+}
+
+void _javascript_Variant::setDouble(double value)
+{
+    setType(Number);
+    m_doubleValue = value;
+}
+
+double _javascript_Variant::doubleValue() const
+{
+    return m_doubleValue;
+}
+
+void _javascript_Variant::setString(const char* value)
+{
+    setType(String);
+    m_stringValue = strdup(value);
+}
+
+char* _javascript_Variant::stringValue() const
+{
+    return m_stringValue;
+}
+
+void _javascript_Variant::setBoolean(bool value)
+{
+    setType(Boolean);
+    m_booleanValue = value;
+}
+
+bool _javascript_Variant::booleanValue() const
+{
+    return m_booleanValue;
+}
+
+}
+}

Added: trunk/Source/WebKit/blackberry/Api/_javascript_Variant.h (0 => 125324)


--- trunk/Source/WebKit/blackberry/Api/_javascript_Variant.h	                        (rev 0)
+++ trunk/Source/WebKit/blackberry/Api/_javascript_Variant.h	2012-08-10 20:40:11 UTC (rev 125324)
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _javascript_Variant_h
+#define _javascript_Variant_h
+
+#include "BlackBerryGlobal.h"
+
+#include <string>
+
+namespace BlackBerry {
+namespace WebKit {
+
+class BLACKBERRY_EXPORT _javascript_Variant {
+public:
+    enum DataType {
+        Undefined = 0,
+        Null,
+        Boolean,
+        Number,
+        String,
+        Object,
+        Exception
+    };
+
+    _javascript_Variant();
+    _javascript_Variant(const _javascript_Variant&);
+    _javascript_Variant(double);
+    _javascript_Variant(int);
+    _javascript_Variant(const char*);
+    _javascript_Variant(bool);
+    _javascript_Variant(const std::string&);
+    ~_javascript_Variant();
+
+    _javascript_Variant& operator=(const _javascript_Variant&);
+
+    void setType(const DataType&);
+    DataType type() const;
+
+    void setDouble(double);
+    double doubleValue() const;
+
+    void setString(const char*);
+    char* stringValue() const;
+
+    void setBoolean(bool);
+    bool booleanValue() const;
+
+private:
+    DataType m_type;
+
+    union {
+        bool m_booleanValue;
+        double m_doubleValue;
+        char* m_stringValue;
+    };
+};
+
+}
+}
+
+#endif // _javascript_Variant_h

Added: trunk/Source/WebKit/blackberry/Api/_javascript_Variant_p.h (0 => 125324)


--- trunk/Source/WebKit/blackberry/Api/_javascript_Variant_p.h	                        (rev 0)
+++ trunk/Source/WebKit/blackberry/Api/_javascript_Variant_p.h	2012-08-10 20:40:11 UTC (rev 125324)
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _javascript_Variant_p_h
+#define _javascript_Variant_p_h
+
+#include "_javascript_Variant.h"
+
+#include <JSContextRef.h>
+#include <JSValueRef.h>
+
+namespace BlackBerry {
+namespace WebKit {
+
+_javascript_Variant JSValueRefToBlackBerryJavaScriptVariant(const JSGlobalContextRef&, const JSValueRef&);
+JSValueRef BlackBerryJavaScriptVariantToJSValueRef(const JSGlobalContextRef&, const _javascript_Variant&);
+
+}
+}
+
+#endif // _javascript_Variant_p_h

Modified: trunk/Source/WebKit/blackberry/Api/WebPage.cpp (125323 => 125324)


--- trunk/Source/WebKit/blackberry/Api/WebPage.cpp	2012-08-10 20:30:45 UTC (rev 125323)
+++ trunk/Source/WebKit/blackberry/Api/WebPage.cpp	2012-08-10 20:40:11 UTC (rev 125324)
@@ -78,6 +78,7 @@
 #include "InspectorController.h"
 #include "InspectorOverlay.h"
 #include "_javascript_DebuggerBlackBerry.h"
+#include "_javascript_Variant_p.h"
 #include "LayerWebKitThread.h"
 #include "NetworkManager.h"
 #include "NodeRenderStyle.h"
@@ -846,24 +847,25 @@
     return d->executeJavaScriptInIsolatedWorld(sourceCode, returnType, returnValue);
 }
 
-bool WebPage::executeJavaScriptFunction(const std::vector<std::string> &function, const std::vector<std::string> &args, _javascript_DataType& returnType, WebString& returnValue)
+void WebPage::executeJavaScriptFunction(const std::vector<std::string> &function, const std::vector<_javascript_Variant> &args, _javascript_Variant& returnValue)
 {
-    if (!d->m_mainFrame)
-        return false;
+    if (!d->m_mainFrame) {
+        returnValue.setType(_javascript_Variant::Exception);
+        return;
+    }
+
     JSC::Bindings::RootObject* root = d->m_mainFrame->script()->bindingRootObject();
-    if (!root)
-        return false;
+    if (!root) {
+        returnValue.setType(_javascript_Variant::Exception);
+        return;
+    }
+
     JSC::ExecState* exec = root->globalObject()->globalExec();
     JSGlobalContextRef ctx = toGlobalRef(exec);
 
-    WTF::Vector<JSStringRef> argList(args.size());
     WTF::Vector<JSValueRef> argListRef(args.size());
-    for (unsigned i = 0; i < args.size(); ++i) {
-        JSStringRef str = JSStringCreateWithUTF8CString(args[i].c_str());
-        argList[i] = str;
-        JSValueRef strRef = JSValueMakeString(ctx, str);
-        argListRef[i] = strRef;
-    }
+    for (unsigned i = 0; i < args.size(); ++i)
+        argListRef[i] = BlackBerryJavaScriptVariantToJSValueRef(ctx, args[i]);
 
     JSValueRef windowObjectValue = windowObject();
     JSObjectRef obj = JSValueToObject(ctx, windowObjectValue, 0);
@@ -882,49 +884,13 @@
     if (functionObject && thisObject)
         result = JSObjectCallAsFunction(ctx, functionObject, thisObject, args.size(), argListRef.data(), 0);
 
-    for (unsigned i = 0; i < args.size(); ++i)
-        JSStringRelease(argList[i]);
-
     JSC::JSValue value = toJS(exec, result);
-
     if (!value) {
-        returnType = JSException;
-        return false;
+        returnValue.setType(_javascript_Variant::Exception);
+        return;
     }
 
-    JSType type = JSValueGetType(ctx, result);
-
-    switch (type) {
-    case kJSTypeNull:
-        returnType = JSNull;
-        break;
-    case kJSTypeBoolean:
-        returnType = JSBoolean;
-        break;
-    case kJSTypeNumber:
-        returnType = JSNumber;
-        break;
-    case kJSTypeString:
-        returnType = JSString;
-        break;
-    case kJSTypeObject:
-        returnType = JSObject;
-        break;
-    case kJSTypeUndefined:
-    default:
-        returnType = JSUndefined;
-        break;
-    }
-
-    if (returnType == JSBoolean || returnType == JSNumber || returnType == JSString || returnType == JSObject) {
-        JSStringRef stringRef = JSValueToStringCopy(ctx, result, 0);
-        size_t bufferSize = JSStringGetMaximumUTF8CStringSize(stringRef);
-        WTF::Vector<char> buffer(bufferSize);
-        JSStringGetUTF8CString(stringRef, buffer.data(), bufferSize);
-        returnValue = WebString::fromUtf8(buffer.data());
-    }
-
-    return true;
+    returnValue = JSValueRefToBlackBerryJavaScriptVariant(ctx, result);
 }
 
 void WebPagePrivate::stopCurrentLoad()

Modified: trunk/Source/WebKit/blackberry/Api/WebPage.h (125323 => 125324)


--- trunk/Source/WebKit/blackberry/Api/WebPage.h	2012-08-10 20:30:45 UTC (rev 125323)
+++ trunk/Source/WebKit/blackberry/Api/WebPage.h	2012-08-10 20:40:11 UTC (rev 125324)
@@ -20,6 +20,7 @@
 #define WebPage_h
 
 #include "BlackBerryGlobal.h"
+#include "_javascript_Variant.h"
 #include "WebString.h"
 
 #include <BlackBerryPlatformGuardedPointer.h>
@@ -106,7 +107,7 @@
     // Takes a UTF16 encoded script that is used explicitly by the pattern matching code
     bool executeJavaScriptInIsolatedWorld(const std::wstring& script, _javascript_DataType& returnType, WebString& returnValue);
 
-    bool executeJavaScriptFunction(const std::vector<std::string> &script, const std::vector<std::string> &args, _javascript_DataType& returnType, WebString& returnValue);
+    void executeJavaScriptFunction(const std::vector<std::string> &function, const std::vector<_javascript_Variant> &args, _javascript_Variant& returnValue);
 
     void initializeIconDataBase();
 

Modified: trunk/Source/WebKit/blackberry/ChangeLog (125323 => 125324)


--- trunk/Source/WebKit/blackberry/ChangeLog	2012-08-10 20:30:45 UTC (rev 125323)
+++ trunk/Source/WebKit/blackberry/ChangeLog	2012-08-10 20:40:11 UTC (rev 125324)
@@ -1,3 +1,44 @@
+2012-08-10  Benjamin C Meyer  <[email protected]>
+
+        Introduce _javascript_Variant object.
+        https://bugs.webkit.org/show_bug.cgi?id=93644
+
+        The _javascript_Variant can be used for functions that return results from
+        _javascript_ such as executeJavaScript or functions that take arguments
+        to pass a variable to _javascript_ and they want to make sure the type
+        is correct.
+
+        Convert executeJavaScriptFunction() to use this data type as the
+        example, other functions will be converted in the future.
+
+        PR 14929
+
+        Internally reviewed by Joe Mason
+        Reviewed by Adam Treat
+
+        * Api/_javascript_Variant.cpp: Added.
+        (WebKit):
+        (BlackBerry::WebKit::JSValueRefToBlackBerryJavaScriptVariant):
+        (BlackBerry::WebKit::BlackBerryJavaScriptVariantToJSValueRef):
+        (BlackBerry::WebKit::_javascript_Variant::_javascript_Variant):
+        (BlackBerry::WebKit::_javascript_Variant::~_javascript_Variant):
+        (BlackBerry::WebKit::_javascript_Variant::operator=):
+        (BlackBerry::WebKit::_javascript_Variant::setType):
+        (BlackBerry::WebKit::_javascript_Variant::type):
+        (BlackBerry::WebKit::_javascript_Variant::setDouble):
+        (BlackBerry::WebKit::_javascript_Variant::doubleValue):
+        (BlackBerry::WebKit::_javascript_Variant::setString):
+        (BlackBerry::WebKit::_javascript_Variant::stringValue):
+        (BlackBerry::WebKit::_javascript_Variant::setBoolean):
+        (BlackBerry::WebKit::_javascript_Variant::booleanValue):
+        * Api/_javascript_Variant.h: Added.
+        (WebKit):
+        * Api/_javascript_Variant_p.h: Added.
+        (WebKit):
+        * Api/WebPage.cpp:
+        (BlackBerry::WebKit::WebPage::executeJavaScriptFunction):
+        * Api/WebPage.h:
+
 2012-08-09  Leo Yang  <[email protected]>
 
         [BlackBerry] Export InRegionScroller to fix build
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to