]> git.saurik.com Git - apple/javascriptcore.git/blobdiff - API/JSValueRef.cpp
JavaScriptCore-1218.tar.gz
[apple/javascriptcore.git] / API / JSValueRef.cpp
index 468a2d14b6e52977a160861d95f9ab8c76ea02d5..81a2db7b7ac61f51c8632fd3a8ff7feae3087549 100644 (file)
@@ -1,4 +1,3 @@
-// -*- mode: c++; c-basic-offset: 4 -*-
 /*
  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
  *
 /*
  * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
  *
 #include "config.h"
 #include "JSValueRef.h"
 
 #include "config.h"
 #include "JSValueRef.h"
 
-#include <wtf/Platform.h>
 #include "APICast.h"
 #include "APICast.h"
+#include "APIShims.h"
+#include "JSAPIWrapperObject.h"
 #include "JSCallbackObject.h"
 
 #include "JSCallbackObject.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>
+#include <runtime/JSCJSValue.h>
+#include <runtime/JSGlobalObject.h>
+#include <runtime/JSONObject.h>
+#include <runtime/JSString.h>
+#include <runtime/LiteralParser.h>
+#include <runtime/Operations.h>
+#include <runtime/Protect.h>
 
 #include <wtf/Assertions.h>
 
 #include <wtf/Assertions.h>
+#include <wtf/text/StringHash.h>
+#include <wtf/text/WTFString.h>
 
 #include <algorithm> // for std::min
 
 
 #include <algorithm> // for std::min
 
-JSType JSValueGetType(JSContextRef, JSValueRef value)
+#if PLATFORM(MAC)
+#include <mach-o/dyld.h>
+#endif
+
+using namespace JSC;
+
+#if PLATFORM(MAC)
+static bool evernoteHackNeeded()
 {
 {
-    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;
-    }
+    static const int32_t webkitLastVersionWithEvernoteHack = 35133959;
+    static bool hackNeeded = CFEqual(CFBundleGetIdentifier(CFBundleGetMainBundle()), CFSTR("com.evernote.Evernote"))
+        && NSVersionOfLinkTimeLibrary("JavaScriptCore") <= webkitLastVersionWithEvernoteHack;
+
+    return hackNeeded;
 }
 }
+#endif
 
 
-using namespace KJS; // placed here to avoid conflict between KJS::JSType and JSType, above.
+::JSType JSValueGetType(JSContextRef ctx, JSValueRef value)
+{
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return kJSTypeUndefined;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
 
 
-bool JSValueIsUndefined(JSContextRef, JSValueRef value)
+    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 JSValueIsUndefined(JSContextRef ctx, JSValueRef value)
 {
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isUndefined();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
+    return jsValue.isUndefined();
 }
 
 }
 
-bool JSValueIsNull(JSContextRef, JSValueRef value)
+bool JSValueIsNull(JSContextRef ctx, JSValueRef value)
 {
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isNull();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
+    return jsValue.isNull();
 }
 
 }
 
-bool JSValueIsBoolean(JSContextRef, JSValueRef value)
+bool JSValueIsBoolean(JSContextRef ctx, JSValueRef value)
 {
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isBoolean();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
+    return jsValue.isBoolean();
 }
 
 }
 
-bool JSValueIsNumber(JSContextRef, JSValueRef value)
+bool JSValueIsNumber(JSContextRef ctx, JSValueRef value)
 {
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isNumber();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
+    return jsValue.isNumber();
 }
 
 }
 
-bool JSValueIsString(JSContextRef, JSValueRef value)
+bool JSValueIsString(JSContextRef ctx, JSValueRef value)
 {
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isString();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
+    return jsValue.isString();
 }
 
 }
 
-bool JSValueIsObject(JSContextRef, JSValueRef value)
+bool JSValueIsObject(JSContextRef ctx, JSValueRef value)
 {
 {
-    JSValue* jsValue = toJS(value);
-    return jsValue->isObject();
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
+    return jsValue.isObject();
 }
 
 }
 
-bool JSValueIsObjectOfClass(JSContextRef, JSValueRef value, JSClassRef jsClass)
+bool JSValueIsObjectOfClass(JSContextRef ctx, JSValueRef value, JSClassRef jsClass)
 {
 {
-    JSValue* jsValue = toJS(value);
+    if (!ctx || !jsClass) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(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(&JSCallbackObject<JSGlobalObject>::s_info))
+            return jsCast<JSCallbackObject<JSGlobalObject>*>(o)->inherits(jsClass);
+        if (o->inherits(&JSCallbackObject<JSDestructibleObject>::s_info))
+            return jsCast<JSCallbackObject<JSDestructibleObject>*>(o)->inherits(jsClass);
+#if JSC_OBJC_API_ENABLED
+        if (o->inherits(&JSCallbackObject<JSAPIWrapperObject>::s_info))
+            return jsCast<JSCallbackObject<JSAPIWrapperObject>*>(o)->inherits(jsClass);
+#endif
     }
     return false;
 }
 
 bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception)
 {
     }
     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);
     ExecState* exec = toJS(ctx);
-    JSValue* jsA = toJS(a);
-    JSValue* jsB = toJS(b);
+    APIEntryShim entryShim(exec);
 
 
-    bool result = equal(exec, jsA, jsB); // false if an exception is thrown
+    JSValue jsA = toJS(exec, a);
+    JSValue jsB = toJS(exec, b);
+
+    bool result = JSValue::equal(exec, jsA, jsB); // false if an exception is thrown
     if (exec->hadException()) {
         if (exception)
     if (exec->hadException()) {
         if (exception)
-            *exception = toRef(exec->exception());
+            *exception = toRef(exec, exec->exception());
         exec->clearException();
     }
     return result;
         exec->clearException();
     }
     return result;
@@ -134,126 +212,246 @@ bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* ex
 
 bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
 {
 
 bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
 {
-    JSLock lock;
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
     ExecState* exec = toJS(ctx);
     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;
+    APIEntryShim entryShim(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)
 {
 }
 
 bool JSValueIsInstanceOfConstructor(JSContextRef ctx, JSValueRef value, JSObjectRef constructor, JSValueRef* exception)
 {
-    JSLock lock;
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
     ExecState* exec = toJS(ctx);
     ExecState* exec = toJS(ctx);
-    JSValue* jsValue = toJS(value);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
+
     JSObject* jsConstructor = toJS(constructor);
     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)
         return false;
     bool result = jsConstructor->hasInstance(exec, jsValue); // false if an exception is thrown
     if (exec->hadException()) {
         if (exception)
-            *exception = toRef(exec->exception());
+            *exception = toRef(exec, exec->exception());
         exec->clearException();
     }
     return result;
 }
 
         exec->clearException();
     }
     return result;
 }
 
-JSValueRef JSValueMakeUndefined(JSContextRef)
+JSValueRef JSValueMakeUndefined(JSContextRef ctx)
+{
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    return toRef(exec, jsUndefined());
+}
+
+JSValueRef JSValueMakeNull(JSContextRef ctx)
+{
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+    ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(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);
+    APIEntryShim entryShim(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);
+    APIEntryShim entryShim(exec);
+
+    // Our JSValue representation relies on a standard bit pattern for NaN. NaNs
+    // generated internally to JavaScriptCore naturally have that representation,
+    // but an external NaN might not.
+    if (std::isnan(value))
+        value = QNaN;
+
+    return toRef(exec, jsNumber(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);
+    APIEntryShim entryShim(exec);
+
+    return toRef(exec, jsString(exec, 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);
+    APIEntryShim entryShim(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.characters(), 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);
+    APIEntryShim entryShim(exec);
+    JSValue value = toJS(exec, apiValue);
+    String result = JSONStringify(exec, value, indent);
+    if (exception)
+        *exception = 0;
+    if (exec->hadException()) {
+        if (exception)
+            *exception = toRef(exec, exec->exception());
+        exec->clearException();
+        return 0;
+    }
+    return OpaqueJSString::create(result).leakRef();
 }
 
 bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
 {
 }
 
 bool JSValueToBoolean(JSContextRef ctx, JSValueRef value)
 {
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return false;
+    }
     ExecState* exec = toJS(ctx);
     ExecState* exec = toJS(ctx);
-    JSValue* jsValue = toJS(value);
-    return jsValue->toBoolean(exec);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
+    return jsValue.toBoolean(exec);
 }
 
 double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
 {
 }
 
 double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
 {
-    JSLock lock;
-    JSValue* jsValue = toJS(value);
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return QNaN;
+    }
     ExecState* exec = toJS(ctx);
     ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
 
 
-    double number = jsValue->toNumber(exec);
+    double number = jsValue.toNumber(exec);
     if (exec->hadException()) {
         if (exception)
     if (exec->hadException()) {
         if (exception)
-            *exception = toRef(exec->exception());
+            *exception = toRef(exec, exec->exception());
         exec->clearException();
         exec->clearException();
-        number = NaN;
+        number = QNaN;
     }
     return number;
 }
 
 JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
 {
     }
     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);
     ExecState* exec = toJS(ctx);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
     
     
-    JSStringRef stringRef = toRef(jsValue->toString(exec).rep()->ref());
+    RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue.toString(exec)->value(exec)));
     if (exec->hadException()) {
         if (exception)
     if (exec->hadException()) {
         if (exception)
-            *exception = toRef(exec->exception());
+            *exception = toRef(exec, exec->exception());
         exec->clearException();
         exec->clearException();
-        stringRef = 0;
+        stringRef.clear();
     }
     }
-    return stringRef;
+    return stringRef.release().leakRef();
 }
 
 JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
 {
 }
 
 JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
 {
-    JSLock lock;
+    if (!ctx) {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
     ExecState* exec = toJS(ctx);
     ExecState* exec = toJS(ctx);
-    JSValue* jsValue = toJS(value);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJS(exec, value);
     
     
-    JSObjectRef objectRef = toRef(jsValue->toObject(exec));
+    JSObjectRef objectRef = toRef(jsValue.toObject(exec));
     if (exec->hadException()) {
         if (exception)
     if (exec->hadException()) {
         if (exception)
-            *exception = toRef(exec->exception());
+            *exception = toRef(exec, exec->exception());
         exec->clearException();
         objectRef = 0;
     }
     return objectRef;
 }    
 
         exec->clearException();
         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);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJSForGC(exec, value);
     gcProtect(jsValue);
 }
 
     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);
+    APIEntryShim entryShim(exec);
+
+    JSValue jsValue = toJSForGC(exec, value);
     gcUnprotect(jsValue);
 }
     gcUnprotect(jsValue);
 }