]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - API/JSValueRef.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / API / JSValueRef.cpp
index 468a2d14b6e52977a160861d95f9ab8c76ea02d5..54405e2af597c248236def769e8bbc6028979d26 100644 (file)
@@ -1,4 +1,3 @@
-// -*- mode: c++; c-basic-offset: 4 -*-
 /*
  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
  *
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  *
- * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include "config.h"
 #include "JSValueRef.h"
 
-#include <wtf/Platform.h>
 #include "APICast.h"
+#include "DateInstance.h"
+#include "Exception.h"
+#include "JSAPIWrapperObject.h"
+#include "JSCInlines.h"
+#include "JSCJSValue.h"
 #include "JSCallbackObject.h"
+#include "JSGlobalObject.h"
+#include "JSONObject.h"
+#include "JSString.h"
+#include "LiteralParser.h"
+#include "Protect.h"
+#include <algorithm>
+#include <wtf/Assertions.h>
+#include <wtf/text/StringHash.h>
+#include <wtf/text/WTFString.h>
 
-#include <kjs/JSType.h>
-#include <kjs/JSGlobalObject.h>
-#include <kjs/internal.h>
-#include <kjs/operations.h>
-#include <kjs/protect.h>
-#include <kjs/ustring.h>
-#include <kjs/value.h>
+#if PLATFORM(MAC)
+#include <mach-o/dyld.h>
+#endif
 
-#include <wtf/Assertions.h>
+#if ENABLE(REMOTE_INSPECTOR)
+#include "JSGlobalObjectInspectorController.h"
+#endif
+
+using namespace JSC;
 
-#include <algorithm> // for std::min
+enum class ExceptionStatus {
+    DidThrow,
+    DidNotThrow
+};
 
-JSType JSValueGetType(JSContextRef, JSValueRef value)
+static ExceptionStatus handleExceptionIfNeeded(ExecState* exec, JSValueRef* returnedExceptionRef)
 {
-    KJS::JSValue* jsValue = toJS(value);
-    switch (jsValue->type()) {
-        case KJS::UndefinedType:
-            return kJSTypeUndefined;
-        case KJS::NullType:
-            return kJSTypeNull;
-        case KJS::BooleanType:
-            return kJSTypeBoolean;
-        case KJS::NumberType:
-            return kJSTypeNumber;
-        case KJS::StringType:
-            return kJSTypeString;
-        case KJS::ObjectType:
-            return kJSTypeObject;
-        default:
-            ASSERT(!"JSValueGetType: unknown type code.\n");
-            return kJSTypeUndefined;
+    if (exec->hadException()) {
+        Exception* exception = exec->exception();
+        if (returnedExceptionRef)
+            *returnedExceptionRef = toRef(exec, exception->value());
+        exec->clearException();
+#if ENABLE(REMOTE_INSPECTOR)
+        exec->vmEntryGlobalObject()->inspectorController().reportAPIException(exec, exception);
+#endif
+        return ExceptionStatus::DidThrow;
     }
+    return ExceptionStatus::DidNotThrow;
 }
 
-using namespace KJS; // placed here to avoid conflict between KJS::JSType and JSType, above.
+#if PLATFORM(MAC)
+static bool evernoteHackNeeded()
+{
+    static const int32_t webkitLastVersionWithEvernoteHack = 35133959;
+    static bool hackNeeded = CFEqual(CFBundleGetIdentifier(CFBundleGetMainBundle()), CFSTR("com.evernote.Evernote"))
+        && NSVersionOfLinkTimeLibrary("JavaScriptCore") <= webkitLastVersionWithEvernoteHack;
+
+    return hackNeeded;
+}
+#endif
 
-bool JSValueIsUndefined(JSContextRef, JSValueRef value)
+::JSType JSValueGetType(JSContextRef ctx, JSValueRef value)
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isUndefined();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return kJSTypeUndefined;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    JSValue jsValue = toJS(exec, value);
+
+    if (jsValue.isUndefined())
+        return kJSTypeUndefined;
+    if (jsValue.isNull())
+        return kJSTypeNull;
+    if (jsValue.isBoolean())
+        return kJSTypeBoolean;
+    if (jsValue.isNumber())
+        return kJSTypeNumber;
+    if (jsValue.isString())
+        return kJSTypeString;
+    ASSERT(jsValue.isObject());
+    return kJSTypeObject;
 }
 
-bool JSValueIsNull(JSContextRef, JSValueRef value)
+bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value)
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isNull();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toJS(exec, value).isUndefined();
 }
 
-bool JSValueIsBoolean(JSContextRef, JSValueRef value)
+bool JSValueIsNull(JSContextRef ctx, JSValueRef value)
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isBoolean();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toJS(exec, value).isNull();
 }
 
-bool JSValueIsNumber(JSContextRef, JSValueRef value)
+bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value)
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isNumber();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toJS(exec, value).isBoolean();
 }
 
-bool JSValueIsString(JSContextRef, JSValueRef value)
+bool JSValueIsNumber(JSContextRef ctx, JSValueRef value)
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isString();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toJS(exec, value).isNumber();
 }
 
-bool JSValueIsObject(JSContextRef, JSValueRef value)
+bool JSValueIsString(JSContextRef ctx, JSValueRef value)
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isObject();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toJS(exec, value).isString();
 }
 
-bool JSValueIsObjectOfClass(JSContextRef, JSValueRef value, JSClassRef jsClass)
+bool JSValueIsObject(JSContextRef ctx, JSValueRef value)
 {
-    JSValue* jsValue = toJS(value);
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toJS(exec, value).isObject();
+}
+
+bool JSValueIsArray(JSContextRef ctx, JSValueRef value)
+{
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toJS(exec, value).inherits(JSArray::info());
+}
+
+bool JSValueIsDate(JSContextRef ctx, JSValueRef value)
+{
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toJS(exec, value).inherits(DateInstance::info());
+}
+
+bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass)
+{
+    if (!ctx || !jsClass) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    JSValue jsValue = toJS(exec, value);
     
-    if (JSObject* o = jsValue->getObject()) {
-        if (o->inherits(&JSCallbackObject<JSGlobalObject>::info))
-            return static_cast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
-        else if (o->inherits(&JSCallbackObject<JSObject>::info))
-            return static_cast<JSCallbackObject<JSObject>*>(o)->inherits(jsClass);
+    if (JSObject* o = jsValue.getObject()) {
+        if (o->inherits(JSProxy::info()))
+            o = jsCast<JSProxy*>(o)->target();
+
+        if (o->inherits(JSCallbackObject<JSGlobalObject>::info()))
+            return jsCast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
+        if (o->inherits(JSCallbackObject<JSDestructibleObject>::info()))
+            return jsCast<JSCallbackObject<JSDestructibleObject>*>(o)->inherits(jsClass);
+#if JSC_OBJC_API_ENABLED
+        if (o->inherits(JSCallbackObject<JSAPIWrapperObject>::info()))
+            return jsCast<JSCallbackObject<JSAPIWrapperObject>*>(o)->inherits(jsClass);
+#endif
     }
     return false;
 }
 
 bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
 {
-    JSLock lock;
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
     ExecState* exec = toJS(ctx);
-    JSValue* jsA = toJS(a);
-    JSValue* jsB = toJS(b);
+    JSLockHolder locker(exec);
 
-    bool result = equal(exec, jsA, jsB); // false if an exception is thrown
-    if (exec->hadException()) {
-        if (exception)
-            *exception = toRef(exec->exception());
-        exec->clearException();
-    }
+    JSValue jsA = toJS(exec, a);
+    JSValue jsB = toJS(exec, b);
+
+    bool result = JSValue::equal(exec, jsA, jsB); // false if an exception is thrown
+    handleExceptionIfNeeded(exec, exception);
+    
     return result;
 }
 
 bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
 {
-    JSLock lock;
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
     ExecState* exec = toJS(ctx);
-    JSValue* jsA = toJS(a);
-    JSValue* jsB = toJS(b);
-    
-    bool result = strictEqual(exec, jsA, jsB); // can't throw because it doesn't perform value conversion
-    ASSERT(!exec->hadException());
-    return result;
+    JSLockHolder locker(exec);
+
+    JSValue jsA = toJS(exec, a);
+    JSValue jsB = toJS(exec, b);
+
+    return JSValue::strictEqual(exec, jsA, jsB);
 }
 
 bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
 {
-    JSLock lock;
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
     ExecState* exec = toJS(ctx);
-    JSValue* jsValue = toJS(value);
+    JSLockHolder locker(exec);
+
+    JSValue jsValue = toJS(exec, value);
+
     JSObject* jsConstructor = toJS(constructor);
-    if (!jsConstructor->implementsHasInstance())
+    if (!jsConstructor->structure()->typeInfo().implementsHasInstance())
         return false;
     bool result = jsConstructor->hasInstance(exec, jsValue); // false if an exception is thrown
-    if (exec->hadException()) {
-        if (exception)
-            *exception = toRef(exec->exception());
-        exec->clearException();
-    }
+    handleExceptionIfNeeded(exec, exception);
     return result;
 }
 
-JSValueRef JSValueMakeUndefined(JSContextRef)
+JSValueRef JSValueMakeUndefined(JSContextRef ctx)
+{
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toRef(exec, jsUndefined());
+}
+
+JSValueRef JSValueMakeNull(JSContextRef ctx)
+{
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toRef(exec, jsNull());
+}
+
+JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool value)
 {
-    return toRef(jsUndefined());
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toRef(exec, jsBoolean(value));
 }
 
-JSValueRef JSValueMakeNull(JSContextRef)
+JSValueRef JSValueMakeNumber(JSContextRef ctx, double value)
 {
-    return toRef(jsNull());
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toRef(exec, jsNumber(purifyNaN(value)));
 }
 
-JSValueRef JSValueMakeBoolean(JSContextRef, bool value)
+JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string)
 {
-    return toRef(jsBoolean(value));
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    return toRef(exec, jsString(exec, string ? string->string() : String()));
 }
 
-JSValueRef JSValueMakeNumber(JSContextRef, double value)
+JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string)
 {
-    JSLock lock;
-    return toRef(jsNumber(value));
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+    String str = string->string();
+    unsigned length = str.length();
+    if (!length || str.is8Bit()) {
+        LiteralParser<LChar> parser(exec, str.characters8(), length, StrictJSON);
+        return toRef(exec, parser.tryLiteralParse());
+    }
+    LiteralParser<UChar> parser(exec, str.characters16(), length, StrictJSON);
+    return toRef(exec, parser.tryLiteralParse());
 }
 
-JSValueRef JSValueMakeString(JSContextRef, JSStringRef string)
+JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception)
 {
-    JSLock lock;
-    UString::Rep* rep = toJS(string);
-    return toRef(jsString(UString(rep)));
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+    JSValue value = toJS(exec, apiValue);
+    String result = JSONStringify(exec, value, indent);
+    if (exception)
+        *exception = 0;
+    if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow)
+        return 0;
+    return OpaqueJSString::create(result).leakRef();
 }
 
 bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
 {
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
     ExecState* exec = toJS(ctx);
-    JSValue* jsValue = toJS(value);
-    return jsValue->toBoolean(exec);
+    JSLockHolder locker(exec);
+
+    JSValue jsValue = toJS(exec, value);
+    return jsValue.toBoolean(exec);
 }
 
 double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
 {
-    JSLock lock;
-    JSValue* jsValue = toJS(value);
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return PNaN;
+    }
     ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
 
-    double number = jsValue->toNumber(exec);
-    if (exec->hadException()) {
-        if (exception)
-            *exception = toRef(exec->exception());
-        exec->clearException();
-        number = NaN;
-    }
+    JSValue jsValue = toJS(exec, value);
+
+    double number = jsValue.toNumber(exec);
+    if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow)
+        number = PNaN;
     return number;
 }
 
 JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
 {
-    JSLock lock;
-    JSValue* jsValue = toJS(value);
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
     ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    JSValue jsValue = toJS(exec, value);
     
-    JSStringRef stringRef = toRef(jsValue->toString(exec).rep()->ref());
-    if (exec->hadException()) {
-        if (exception)
-            *exception = toRef(exec->exception());
-        exec->clearException();
-        stringRef = 0;
-    }
-    return stringRef;
+    RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue.toString(exec)->value(exec)));
+    if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow)
+        stringRef = nullptr;
+    return stringRef.release().leakRef();
 }
 
 JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
 {
-    JSLock lock;
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
     ExecState* exec = toJS(ctx);
-    JSValue* jsValue = toJS(value);
+    JSLockHolder locker(exec);
+
+    JSValue jsValue = toJS(exec, value);
     
-    JSObjectRef objectRef = toRef(jsValue->toObject(exec));
-    if (exec->hadException()) {
-        if (exception)
-            *exception = toRef(exec->exception());
-        exec->clearException();
+    JSObjectRef objectRef = toRef(jsValue.toObject(exec));
+    if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow)
         objectRef = 0;
-    }
     return objectRef;
-}    
+}
 
-void JSValueProtect(JSContextRef, JSValueRef value)
+void JSValueProtect(JSContextRef ctx, JSValueRef value)
 {
-    JSLock lock;
-    JSValue* jsValue = toJS(value);
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return;
+    }
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    JSValue jsValue = toJSForGC(exec, value);
     gcProtect(jsValue);
 }
 
-void JSValueUnprotect(JSContextRef, JSValueRef value)
+void JSValueUnprotect(JSContextRef ctx, JSValueRef value)
 {
-    JSLock lock;
-    JSValue* jsValue = toJS(value);
+#if PLATFORM(MAC)
+    if ((!value || !ctx) && evernoteHackNeeded())
+        return;
+#endif
+
+    ExecState* exec = toJS(ctx);
+    JSLockHolder locker(exec);
+
+    JSValue jsValue = toJSForGC(exec, value);
     gcUnprotect(jsValue);
 }