123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- /*
- * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 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.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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
- * 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.
- */
- #include "config.h"
- #include "JSValueRef.h"
- #include "APICast.h"
- #include "APIShims.h"
- #include "JSAPIWrapperObject.h"
- #include "JSCallbackObject.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/text/StringHash.h>
- #include <wtf/text/WTFString.h>
- #include <algorithm> // for std::min
- #if PLATFORM(MAC)
- #include <mach-o/dyld.h>
- #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)
- {
- 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())
- 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)
- {
- 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 ctx, JSValueRef 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 ctx, JSValueRef 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 ctx, JSValueRef 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 ctx, JSValueRef 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 ctx, JSValueRef 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 ctx, JSValueRef value, JSClassRef jsClass)
- {
- 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>::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)
- {
- if (!ctx) {
- ASSERT_NOT_REACHED();
- return false;
- }
- ExecState* exec = toJS(ctx);
- APIEntryShim entryShim(exec);
- 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)
- *exception = toRef(exec, exec->exception());
- exec->clearException();
- }
- return result;
- }
- bool JSValueIsStrictEqual(JSContextRef ctx, JSValueRef a, JSValueRef b)
- {
- 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);
- APIEntryShim entryShim(exec);
- JSValue jsValue = toJS(exec, value);
- JSObject* jsConstructor = toJS(constructor);
- 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, exec->exception());
- exec->clearException();
- }
- return result;
- }
- 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)
- {
- 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);
- 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 JSValueMakeString(JSContextRef ctx, JSStringRef string)
- {
- if (!ctx) {
- ASSERT_NOT_REACHED();
- return 0;
- }
- ExecState* exec = toJS(ctx);
- APIEntryShim entryShim(exec);
- 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<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());
- }
- 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);
- 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);
- APIEntryShim entryShim(exec);
- JSValue jsValue = toJS(exec, value);
- double number = jsValue.toNumber(exec);
- if (exec->hadException()) {
- if (exception)
- *exception = toRef(exec, exec->exception());
- exec->clearException();
- number = QNaN;
- }
- return number;
- }
- JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
- {
- if (!ctx) {
- ASSERT_NOT_REACHED();
- return 0;
- }
- ExecState* exec = toJS(ctx);
- APIEntryShim entryShim(exec);
- JSValue jsValue = toJS(exec, value);
-
- RefPtr<OpaqueJSString> stringRef(OpaqueJSString::create(jsValue.toString(exec)->value(exec)));
- if (exec->hadException()) {
- if (exception)
- *exception = toRef(exec, exec->exception());
- exec->clearException();
- stringRef.clear();
- }
- return stringRef.release().leakRef();
- }
- JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception)
- {
- if (!ctx) {
- ASSERT_NOT_REACHED();
- return 0;
- }
- ExecState* exec = toJS(ctx);
- APIEntryShim entryShim(exec);
- JSValue jsValue = toJS(exec, value);
-
- JSObjectRef objectRef = toRef(jsValue.toObject(exec));
- if (exec->hadException()) {
- if (exception)
- *exception = toRef(exec, exec->exception());
- exec->clearException();
- objectRef = 0;
- }
- return objectRef;
- }
- void JSValueProtect(JSContextRef ctx, JSValueRef value)
- {
- if (!ctx) {
- ASSERT_NOT_REACHED();
- return;
- }
- ExecState* exec = toJS(ctx);
- APIEntryShim entryShim(exec);
- 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);
- APIEntryShim entryShim(exec);
- JSValue jsValue = toJSForGC(exec, value);
- gcUnprotect(jsValue);
- }
|