X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/9dae56ea45a0f5f8136a5c93d6f3a7f99399ca73..4e4e5a6f2694187498445a6ac6f1634ce8141119:/API/JSValueRef.cpp?ds=inline diff --git a/API/JSValueRef.cpp b/API/JSValueRef.cpp index 7080952..905dcbf 100644 --- a/API/JSValueRef.cpp +++ b/API/JSValueRef.cpp @@ -26,24 +26,33 @@ #include "config.h" #include "JSValueRef.h" -#include #include "APICast.h" +#include "APIShims.h" #include "JSCallbackObject.h" #include +#include #include +#include #include #include #include #include #include +#include #include // for std::min -JSType JSValueGetType(JSContextRef, JSValueRef value) +using namespace JSC; + +::JSType JSValueGetType(JSContextRef ctx, JSValueRef value) { - JSC::JSValuePtr jsValue = toJS(value); + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + + JSValue jsValue = toJS(exec, value); + if (jsValue.isUndefined()) return kJSTypeUndefined; if (jsValue.isNull()) @@ -58,47 +67,66 @@ 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); + 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); + 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); + 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); + 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); + 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); + 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); + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + + JSValue jsValue = toJS(exec, value); if (JSObject* o = jsValue.getObject()) { if (o->inherits(&JSCallbackObject::info)) @@ -112,101 +140,141 @@ bool JSValueIsObjectOfClass(JSContextRef, JSValueRef value, JSClassRef jsClass) bool JSValueIsEqual(JSContextRef ctx, JSValueRef a, JSValueRef b, JSValueRef* exception) { 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; + 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) { 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 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()); + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + + return toRef(exec, jsUndefined()); } -JSValueRef JSValueMakeNull(JSContextRef) +JSValueRef JSValueMakeNull(JSContextRef ctx) { - return toRef(jsNull()); + 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)); + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + + return toRef(exec, jsBoolean(value)); } JSValueRef JSValueMakeNumber(JSContextRef ctx, double value) { 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 (isnan(value)) + value = NaN; - return toRef(jsNumber(exec, value)); + return toRef(exec, jsNumber(exec, value)); } JSValueRef JSValueMakeString(JSContextRef ctx, JSStringRef string) { ExecState* exec = toJS(ctx); - exec->globalData().heap.registerThread(); - JSLock lock(exec); + APIEntryShim entryShim(exec); + + return toRef(exec, jsString(exec, string->ustring())); +} + +JSValueRef JSValueMakeFromJSONString(JSContextRef ctx, JSStringRef string) +{ + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + LiteralParser parser(exec, string->ustring(), LiteralParser::StrictJSON); + return toRef(exec, parser.tryLiteralParse()); +} - return toRef(jsString(exec, string->ustring())); +JSStringRef JSValueCreateJSONString(JSContextRef ctx, JSValueRef apiValue, unsigned indent, JSValueRef* exception) +{ + ExecState* exec = toJS(ctx); + APIEntryShim entryShim(exec); + JSValue value = toJS(exec, apiValue); + UString 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).releaseRef(); } bool JSValueToBoolean(JSContextRef ctx, JSValueRef value) { 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) { 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; } @@ -216,15 +284,14 @@ double JSValueToNumber(JSContextRef ctx, JSValueRef value, JSValueRef* exception JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* exception) { 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))); if (exec->hadException()) { if (exception) - *exception = toRef(exec->exception()); + *exception = toRef(exec, exec->exception()); exec->clearException(); stringRef.clear(); } @@ -234,15 +301,14 @@ JSStringRef JSValueToStringCopy(JSContextRef ctx, JSValueRef value, JSValueRef* JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exception) { 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; } @@ -252,19 +318,17 @@ JSObjectRef JSValueToObject(JSContextRef ctx, JSValueRef value, JSValueRef* exce void JSValueProtect(JSContextRef ctx, JSValueRef value) { 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) { 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); }