summaryrefslogtreecommitdiff
path: root/Source/JavaScriptCore/bindings/ScriptValue.cpp
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
commit1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch)
tree46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/JavaScriptCore/bindings/ScriptValue.cpp
parent32761a6cee1d0dee366b885b7b9c777e67885688 (diff)
downloadWebKitGtk-tarball-master.tar.gz
Diffstat (limited to 'Source/JavaScriptCore/bindings/ScriptValue.cpp')
-rw-r--r--Source/JavaScriptCore/bindings/ScriptValue.cpp143
1 files changed, 77 insertions, 66 deletions
diff --git a/Source/JavaScriptCore/bindings/ScriptValue.cpp b/Source/JavaScriptCore/bindings/ScriptValue.cpp
index c72ab4634..2a94f3b6e 100644
--- a/Source/JavaScriptCore/bindings/ScriptValue.cpp
+++ b/Source/JavaScriptCore/bindings/ScriptValue.cpp
@@ -11,7 +11,7 @@
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ * 3. Neither the name of Apple Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
@@ -32,11 +32,77 @@
#include "APICast.h"
#include "InspectorValues.h"
+#include "JSCInlines.h"
#include "JSLock.h"
using namespace JSC;
using namespace Inspector;
+namespace Inspector {
+
+static RefPtr<InspectorValue> jsToInspectorValue(ExecState& scriptState, JSValue value, int maxDepth)
+{
+ if (!value) {
+ ASSERT_NOT_REACHED();
+ return nullptr;
+ }
+
+ if (!maxDepth)
+ return nullptr;
+
+ maxDepth--;
+
+ if (value.isUndefinedOrNull())
+ return InspectorValue::null();
+ if (value.isBoolean())
+ return InspectorValue::create(value.asBoolean());
+ if (value.isNumber() && value.isDouble())
+ return InspectorValue::create(value.asNumber());
+ if (value.isNumber() && value.isAnyInt())
+ return InspectorValue::create(static_cast<int>(value.asAnyInt()));
+ if (value.isString())
+ return InspectorValue::create(asString(value)->value(&scriptState));
+
+ if (value.isObject()) {
+ if (isJSArray(value)) {
+ auto inspectorArray = InspectorArray::create();
+ auto& array = *asArray(value);
+ unsigned length = array.length();
+ for (unsigned i = 0; i < length; i++) {
+ auto elementValue = jsToInspectorValue(scriptState, array.getIndex(&scriptState, i), maxDepth);
+ if (!elementValue)
+ return nullptr;
+ inspectorArray->pushValue(WTFMove(elementValue));
+ }
+ return WTFMove(inspectorArray);
+ }
+ auto inspectorObject = InspectorObject::create();
+ auto& object = *value.getObject();
+ PropertyNameArray propertyNames(&scriptState, PropertyNameMode::Strings);
+ object.methodTable()->getOwnPropertyNames(&object, &scriptState, propertyNames, EnumerationMode());
+ for (auto& name : propertyNames) {
+ auto inspectorValue = jsToInspectorValue(scriptState, object.get(&scriptState, name), maxDepth);
+ if (!inspectorValue)
+ return nullptr;
+ inspectorObject->setValue(name.string(), WTFMove(inspectorValue));
+ }
+ return WTFMove(inspectorObject);
+ }
+
+ ASSERT_NOT_REACHED();
+ return nullptr;
+}
+
+RefPtr<InspectorValue> toInspectorValue(ExecState& state, JSValue value)
+{
+ // FIXME: Maybe we should move the JSLockHolder stuff to the callers since this function takes a JSValue directly.
+ // Doing the locking here made sense when we were trying to abstract the difference between multiple JavaScript engines.
+ JSLockHolder holder(&state);
+ return jsToInspectorValue(state, value, InspectorValue::maxDepth);
+}
+
+} // namespace Inspector
+
namespace Deprecated {
ScriptValue::~ScriptValue()
@@ -55,10 +121,13 @@ bool ScriptValue::getString(ExecState* scriptState, String& result) const
String ScriptValue::toString(ExecState* scriptState) const
{
- String result = m_value.get().toString(scriptState)->value(scriptState);
+ VM& vm = scriptState->vm();
+ auto scope = DECLARE_CATCH_SCOPE(vm);
+
+ String result = m_value.get().toWTFString(scriptState);
// Handle the case where an exception is thrown as part of invoking toString on the object.
- if (scriptState->hadException())
- scriptState->clearException();
+ if (UNLIKELY(scope.exception()))
+ scope.clearException();
return result;
}
@@ -66,7 +135,7 @@ bool ScriptValue::isEqual(ExecState* scriptState, const ScriptValue& anotherValu
{
if (hasNoValue())
return anotherValue.hasNoValue();
- return JSValueIsEqual(toRef(scriptState), toRef(scriptState, jsValue()), toRef(scriptState, anotherValue.jsValue()), nullptr);
+ return JSValueIsStrictEqual(toRef(scriptState), toRef(scriptState, jsValue()), toRef(scriptState, anotherValue.jsValue()));
}
bool ScriptValue::isNull() const
@@ -93,71 +162,13 @@ bool ScriptValue::isObject() const
bool ScriptValue::isFunction() const
{
CallData callData;
- return getCallData(m_value.get(), callData) != CallTypeNone;
-}
-
-#if ENABLE(INSPECTOR)
-static PassRefPtr<InspectorValue> jsToInspectorValue(ExecState* scriptState, JSValue value, int maxDepth)
-{
- if (!value) {
- ASSERT_NOT_REACHED();
- return nullptr;
- }
-
- if (!maxDepth)
- return nullptr;
-
- maxDepth--;
-
- if (value.isNull() || value.isUndefined())
- return InspectorValue::null();
- if (value.isBoolean())
- return InspectorBasicValue::create(value.asBoolean());
- if (value.isNumber())
- return InspectorBasicValue::create(value.asNumber());
- if (value.isString()) {
- String s = value.getString(scriptState);
- return InspectorString::create(String(s.deprecatedCharacters(), s.length()));
- }
-
- if (value.isObject()) {
- if (isJSArray(value)) {
- RefPtr<InspectorArray> inspectorArray = InspectorArray::create();
- JSArray* array = asArray(value);
- unsigned length = array->length();
- for (unsigned i = 0; i < length; i++) {
- JSValue element = array->getIndex(scriptState, i);
- RefPtr<InspectorValue> elementValue = jsToInspectorValue(scriptState, element, maxDepth);
- if (!elementValue)
- return nullptr;
- inspectorArray->pushValue(elementValue);
- }
- return inspectorArray;
- }
- RefPtr<InspectorObject> inspectorObject = InspectorObject::create();
- JSObject* object = value.getObject();
- PropertyNameArray propertyNames(scriptState);
- object->methodTable()->getOwnPropertyNames(object, scriptState, propertyNames, ExcludeDontEnumProperties);
- for (size_t i = 0; i < propertyNames.size(); i++) {
- const Identifier& name = propertyNames[i];
- JSValue propertyValue = object->get(scriptState, name);
- RefPtr<InspectorValue> inspectorValue = jsToInspectorValue(scriptState, propertyValue, maxDepth);
- if (!inspectorValue)
- return nullptr;
- inspectorObject->setValue(String(name.deprecatedCharacters(), name.length()), inspectorValue);
- }
- return inspectorObject;
- }
-
- ASSERT_NOT_REACHED();
- return nullptr;
+ return getCallData(m_value.get(), callData) != CallType::None;
}
-PassRefPtr<InspectorValue> ScriptValue::toInspectorValue(ExecState* scriptState) const
+RefPtr<InspectorValue> ScriptValue::toInspectorValue(ExecState* scriptState) const
{
JSLockHolder holder(scriptState);
- return jsToInspectorValue(scriptState, m_value.get(), InspectorValue::maxDepth);
+ return jsToInspectorValue(*scriptState, m_value.get(), InspectorValue::maxDepth);
}
-#endif // ENABLE(INSPECTOR)
} // namespace Deprecated