X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/9dae56ea45a0f5f8136a5c93d6f3a7f99399ca73..93a3786624b2768d89bfa27e46598dc64e2fb70a:/API/JSValueRef.cpp diff --git a/API/JSValueRef.cpp b/API/JSValueRef.cpp index 7080952..81a2db7 100644 --- a/API/JSValueRef.cpp +++ b/API/JSValueRef.cpp @@ -26,24 +26,53 @@ #include "config.h" #include "JSValueRef.h" -#include #include "APICast.h" +#include "APIShims.h" +#include "JSAPIWrapperObject.h" #include "JSCallbackObject.h" +#include #include +#include #include +#include #include #include -#include -#include #include +#include +#include #include // for std::min -JSType JSValueGetType(JSContextRef, JSValueRef value) +#if PLATFORM(MAC) +#include +#endif + +using namespace JSC; + +#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 + +::JSType JSValueGetType(JSContextRef ctx, JSValueRef value) { - JSC::JSValuePtr jsValue = toJS(value); + if (!ctx) { + ASSERT_NOT_REACHED(); + return kJSTypeUndefined; + } + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + + JSValue jsValue = toJS(exec, value); + if (jsValue.isUndefined()) return kJSTypeUndefined; if (jsValue.isNull()) @@ -58,191 +87,342 @@ JSType JSValueGetType(JSContextRef, JSValueRef value) return kJSTypeObject; } -using namespace JSC; // placed here to avoid conflict between JSC::JSType and JSType, above. - -bool JSValueIsUndefined(JSContextRef, JSValueRef value) +bool JSValueIsUndefined(JSContextRef ctx, JSValueRef value) { - JSValuePtr jsValue = toJS(value); + 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) { - JSValuePtr jsValue = toJS(value); + 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) { - JSValuePtr jsValue = toJS(value); + 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) { - JSValuePtr jsValue = toJS(value); + 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) { - JSValuePtr jsValue = toJS(value); + 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) { - JSValuePtr jsValue = toJS(value); + 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) { - JSValuePtr 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::info)) - return static_cast*>(o)->inherits(jsClass); - else if (o->inherits(&JSCallbackObject::info)) - return static_cast*>(o)->inherits(jsClass); + if (o->inherits(&JSCallbackObject::s_info)) + return jsCast*>(o)->inherits(jsClass); + if (o->inherits(&JSCallbackObject::s_info)) + return jsCast*>(o)->inherits(jsClass); +#if JSC_OBJC_API_ENABLED + if (o->inherits(&JSCallbackObject::s_info)) + return jsCast*>(o)->inherits(jsClass); +#endif } return false; } bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return false; + } ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); - JSValuePtr jsA = toJS(a); - JSValuePtr jsB = toJS(b); + JSValue jsA = toJS(exec, a); + JSValue jsB = toJS(exec, b); - bool result = JSValuePtr::equal(exec, jsA, jsB); // false if an exception is thrown + bool result = JSValue::equal(exec, jsA, jsB); // false if an exception is thrown if (exec->hadException()) { if (exception) - *exception = toRef(exec->exception()); + *exception = toRef(exec, exec->exception()); exec->clearException(); } return result; } -bool JSValueIsStrictEqual(JSContextRef, JSValueRef a, JSValueRef b) +bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b) { - JSValuePtr jsA = toJS(a); - JSValuePtr jsB = toJS(b); - - bool result = JSValuePtr::strictEqual(jsA, jsB); - return result; + if (!ctx) { + ASSERT_NOT_REACHED(); + return false; + } + ExecState* exec = toJS(ctx); + 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) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return false; + } ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); + + JSValue jsValue = toJS(exec, value); - JSValuePtr jsValue = toJS(value); JSObject* jsConstructor = toJS(constructor); if (!jsConstructor->structure()->typeInfo().implementsHasInstance()) return false; - bool result = jsConstructor->hasInstance(exec, jsValue, jsConstructor->get(exec, exec->propertyNames().prototype)); // false if an exception is thrown + 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; } -JSValueRef JSValueMakeUndefined(JSContextRef) +JSValueRef JSValueMakeUndefined(JSContextRef ctx) { - return toRef(jsUndefined()); + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + + return toRef(exec, jsUndefined()); } -JSValueRef JSValueMakeNull(JSContextRef) +JSValueRef JSValueMakeNull(JSContextRef ctx) { - return toRef(jsNull()); + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + + return toRef(exec, jsNull()); } -JSValueRef JSValueMakeBoolean(JSContextRef, bool value) +JSValueRef JSValueMakeBoolean(JSContextRef ctx, bool value) { - return toRef(jsBoolean(value)); + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + + return toRef(exec, jsBoolean(value)); } JSValueRef JSValueMakeNumber(JSContextRef ctx, double value) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + 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(jsNumber(exec, value)); + return toRef(exec, jsNumber(value)); } JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); - return toRef(jsString(exec, string->ustring())); + return toRef(exec, jsString(exec, string->string())); +} + +JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string) +{ + 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 parser(exec, str.characters8(), length, StrictJSON); + return toRef(exec, parser.tryLiteralParse()); + } + LiteralParser parser(exec, str.characters(), length, StrictJSON); + return toRef(exec, parser.tryLiteralParse()); +} + +JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception) +{ + 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) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return false; + } ExecState* exec = toJS(ctx); - JSValuePtr jsValue = toJS(value); + APIEntryShim entryShim(exec); + + JSValue jsValue = toJS(exec, value); return jsValue.toBoolean(exec); } double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return QNaN; + } ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); - JSValuePtr jsValue = toJS(value); + JSValue jsValue = toJS(exec, value); double number = jsValue.toNumber(exec); if (exec->hadException()) { if (exception) - *exception = toRef(exec->exception()); + *exception = toRef(exec, exec->exception()); exec->clearException(); - number = NaN; + number = QNaN; } return number; } JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); - JSValuePtr jsValue = toJS(value); + JSValue jsValue = toJS(exec, value); - RefPtr stringRef(OpaqueJSString::create(jsValue.toString(exec))); + RefPtr stringRef(OpaqueJSString::create(jsValue.toString(exec)->value(exec))); if (exec->hadException()) { if (exception) - *exception = toRef(exec->exception()); + *exception = toRef(exec, exec->exception()); exec->clearException(); stringRef.clear(); } - return stringRef.release().releaseRef(); + return stringRef.release().leakRef(); } JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); - JSValuePtr jsValue = toJS(value); + JSValue jsValue = toJS(exec, value); JSObjectRef objectRef = toRef(jsValue.toObject(exec)); if (exec->hadException()) { if (exception) - *exception = toRef(exec->exception()); + *exception = toRef(exec, exec->exception()); exec->clearException(); objectRef = 0; } @@ -251,20 +431,27 @@ JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exce void JSValueProtect(JSContextRef ctx, JSValueRef value) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return; + } ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); - JSValuePtr jsValue = toJS(value); + JSValue jsValue = toJSForGC(exec, value); gcProtect(jsValue); } void JSValueUnprotect(JSContextRef ctx, JSValueRef value) { +#if PLATFORM(MAC) + if ((!value || !ctx) && evernoteHackNeeded()) + return; +#endif + ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); - JSValuePtr jsValue = toJS(value); + JSValue jsValue = toJSForGC(exec, value); gcUnprotect(jsValue); }