X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/b37bf2e156556c589aea3e1f58a377f2b1189665..refs/heads/master:/API/JSObjectRef.cpp diff --git a/API/JSObjectRef.cpp b/API/JSObjectRef.cpp index c6cda25..faf38ff 100644 --- a/API/JSObjectRef.cpp +++ b/API/JSObjectRef.cpp @@ -1,6 +1,6 @@ -// -*- mode: c++; c-basic-offset: 4 -*- /* - * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. + * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2008 Kelvin W Sherlock (ksherlock@gmail.com) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -11,347 +11,640 @@ * 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 "JSObjectRef.h" +#include "JSObjectRefPrivate.h" -#include #include "APICast.h" -#include "JSValueRef.h" +#include "ButterflyInlines.h" +#include "CodeBlock.h" +#include "CopiedSpaceInlines.h" +#include "DateConstructor.h" +#include "ErrorConstructor.h" +#include "Exception.h" +#include "FunctionConstructor.h" +#include "Identifier.h" +#include "InitializeThreading.h" +#include "JSAPIWrapperObject.h" +#include "JSArray.h" #include "JSCallbackConstructor.h" #include "JSCallbackFunction.h" #include "JSCallbackObject.h" #include "JSClassRef.h" +#include "JSFunction.h" #include "JSGlobalObject.h" - +#include "JSObject.h" +#include "JSRetainPtr.h" +#include "JSString.h" +#include "JSValueRef.h" +#include "ObjectConstructor.h" +#include "ObjectPrototype.h" +#include "JSCInlines.h" #include "PropertyNameArray.h" -#include "function.h" -#include "function_object.h" -#include "identifier.h" -#include "internal.h" -#include "object.h" -#include "object_object.h" +#include "RegExpConstructor.h" -using namespace KJS; +#if ENABLE(REMOTE_INSPECTOR) +#include "JSGlobalObjectInspectorController.h" +#endif + +using namespace JSC; + +enum class ExceptionStatus { + DidThrow, + DidNotThrow +}; + +static ExceptionStatus handleExceptionIfNeeded(ExecState* exec, JSValueRef* returnedExceptionRef) +{ + 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; +} JSClassRef JSClassCreate(const JSClassDefinition* definition) { - JSLock lock; - JSClassRef jsClass = (definition->attributes & kJSClassAttributeNoAutomaticPrototype) + initializeThreading(); + RefPtr jsClass = (definition->attributes & kJSClassAttributeNoAutomaticPrototype) ? OpaqueJSClass::createNoAutomaticPrototype(definition) : OpaqueJSClass::create(definition); - return JSClassRetain(jsClass); + return jsClass.release().leakRef(); } JSClassRef JSClassRetain(JSClassRef jsClass) { - JSLock lock; jsClass->ref(); return jsClass; } void JSClassRelease(JSClassRef jsClass) { - JSLock lock; jsClass->deref(); } JSObjectRef JSObjectMake(JSContextRef ctx, JSClassRef jsClass, void* data) { - JSLock lock; + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); if (!jsClass) - return toRef(new JSObject(exec->lexicalGlobalObject()->objectPrototype())); // slightly more efficient + return toRef(constructEmptyObject(exec)); - JSValue* jsPrototype = jsClass->prototype(ctx); - if (!jsPrototype) - jsPrototype = exec->lexicalGlobalObject()->objectPrototype(); + JSCallbackObject* object = JSCallbackObject::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->callbackObjectStructure(), jsClass, data); + if (JSObject* prototype = jsClass->prototype(exec)) + object->setPrototype(exec->vm(), prototype); - return toRef(new JSCallbackObject(exec, jsClass, jsPrototype, data)); + return toRef(object); } JSObjectRef JSObjectMakeFunctionWithCallback(JSContextRef ctx, JSStringRef name, JSObjectCallAsFunctionCallback callAsFunction) { - JSLock lock; + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous"); - - return toRef(new JSCallbackFunction(exec, callAsFunction, nameID)); + JSLockHolder locker(exec); + return toRef(JSCallbackFunction::create(exec->vm(), exec->lexicalGlobalObject(), callAsFunction, name ? name->string() : ASCIILiteral("anonymous"))); } JSObjectRef JSObjectMakeConstructor(JSContextRef ctx, JSClassRef jsClass, JSObjectCallAsConstructorCallback callAsConstructor) { - JSLock lock; + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - - JSValue* jsPrototype = jsClass - ? jsClass->prototype(ctx) - : exec->dynamicGlobalObject()->objectPrototype(); - - JSCallbackConstructor* constructor = new JSCallbackConstructor(exec, jsClass, callAsConstructor); - constructor->putDirect(exec->propertyNames().prototype, jsPrototype, DontEnum | DontDelete | ReadOnly); + JSLockHolder locker(exec); + + JSValue jsPrototype = jsClass ? jsClass->prototype(exec) : 0; + if (!jsPrototype) + jsPrototype = exec->lexicalGlobalObject()->objectPrototype(); + + JSCallbackConstructor* constructor = JSCallbackConstructor::create(exec, exec->lexicalGlobalObject(), exec->lexicalGlobalObject()->callbackConstructorStructure(), jsClass, callAsConstructor); + constructor->putDirect(exec->vm(), exec->propertyNames().prototype, jsPrototype, DontEnum | DontDelete | ReadOnly); return toRef(constructor); } JSObjectRef JSObjectMakeFunction(JSContextRef ctx, JSStringRef name, unsigned parameterCount, const JSStringRef parameterNames[], JSStringRef body, JSStringRef sourceURL, int startingLineNumber, JSValueRef* exception) { - JSLock lock; - + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - UString::Rep* bodyRep = toJS(body); - UString::Rep* sourceURLRep = sourceURL ? toJS(sourceURL) : &UString::Rep::null; - - Identifier nameID = name ? Identifier(toJS(name)) : Identifier("anonymous"); + JSLockHolder locker(exec); + + startingLineNumber = std::max(1, startingLineNumber); + Identifier nameID = name ? name->identifier(&exec->vm()) : Identifier::fromString(exec, "anonymous"); - List args; + MarkedArgumentBuffer args; for (unsigned i = 0; i < parameterCount; i++) - args.append(jsString(UString(toJS(parameterNames[i])))); - args.append(jsString(UString(bodyRep))); + args.append(jsString(exec, parameterNames[i]->string())); + args.append(jsString(exec, body->string())); - JSObject* result = exec->dynamicGlobalObject()->functionConstructor()->construct(exec, args, nameID, UString(sourceURLRep), startingLineNumber); - if (exec->hadException()) { - if (exception) - *exception = toRef(exec->exception()); - exec->clearException(); + JSObject* result = constructFunction(exec, exec->lexicalGlobalObject(), args, nameID, sourceURL ? sourceURL->string() : String(), TextPosition(OrdinalNumber::fromOneBasedInt(startingLineNumber), OrdinalNumber::first())); + if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow) result = 0; + return toRef(result); +} + +JSObjectRef JSObjectMakeArray(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) +{ + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; } + ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + + JSObject* result; + if (argumentCount) { + MarkedArgumentBuffer argList; + for (size_t i = 0; i < argumentCount; ++i) + argList.append(toJS(exec, arguments[i])); + + result = constructArray(exec, static_cast(0), argList); + } else + result = constructEmptyArray(exec, 0); + + if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow) + result = 0; + return toRef(result); } -JSValueRef JSObjectGetPrototype(JSContextRef, JSObjectRef object) +JSObjectRef JSObjectMakeDate(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { - JSObject* jsObject = toJS(object); - return toRef(jsObject->prototype()); + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } + ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + + MarkedArgumentBuffer argList; + for (size_t i = 0; i < argumentCount; ++i) + argList.append(toJS(exec, arguments[i])); + + JSObject* result = constructDate(exec, exec->lexicalGlobalObject(), argList); + if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow) + result = 0; + + return toRef(result); +} + +JSObjectRef JSObjectMakeError(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) +{ + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } + ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + + JSValue message = argumentCount ? toJS(exec, arguments[0]) : jsUndefined(); + Structure* errorStructure = exec->lexicalGlobalObject()->errorStructure(); + JSObject* result = ErrorInstance::create(exec, errorStructure, message); + + if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow) + result = 0; + + return toRef(result); } -void JSObjectSetPrototype(JSContextRef, JSObjectRef object, JSValueRef value) +JSObjectRef JSObjectMakeRegExp(JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } + ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + + MarkedArgumentBuffer argList; + for (size_t i = 0; i < argumentCount; ++i) + argList.append(toJS(exec, arguments[i])); + + JSObject* result = constructRegExp(exec, exec->lexicalGlobalObject(), argList); + if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow) + result = 0; + + return toRef(result); +} + +JSValueRef JSObjectGetPrototype(JSContextRef ctx, JSObjectRef object) +{ + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } + ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + JSObject* jsObject = toJS(object); - JSValue* jsValue = toJS(value); + return toRef(exec, jsObject->prototype()); +} - jsObject->setPrototype(jsValue); +void JSObjectSetPrototype(JSContextRef ctx, JSObjectRef object, JSValueRef value) +{ + if (!ctx) { + ASSERT_NOT_REACHED(); + return; + } + ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + + JSObject* jsObject = toJS(object); + JSValue jsValue = toJS(exec, value); + + if (JSProxy* proxy = jsDynamicCast(jsObject)) { + if (JSGlobalObject* globalObject = jsDynamicCast(proxy->target())) { + globalObject->resetPrototype(exec->vm(), jsValue.isObject() ? jsValue : jsNull()); + return; + } + // Someday we might use proxies for something other than JSGlobalObjects, but today is not that day. + RELEASE_ASSERT_NOT_REACHED(); + } + jsObject->setPrototypeWithCycleCheck(exec, jsValue.isObject() ? jsValue : jsNull()); } bool JSObjectHasProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName) { - JSLock lock; + if (!ctx) { + ASSERT_NOT_REACHED(); + return false; + } ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + JSObject* jsObject = toJS(object); - UString::Rep* nameRep = toJS(propertyName); - return jsObject->hasProperty(exec, Identifier(nameRep)); + return jsObject->hasProperty(exec, propertyName->identifier(&exec->vm())); } JSValueRef JSObjectGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { - JSLock lock; + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + JSObject* jsObject = toJS(object); - UString::Rep* nameRep = toJS(propertyName); - JSValue* jsValue = jsObject->get(exec, Identifier(nameRep)); - if (exec->hadException()) { - if (exception) - *exception = toRef(exec->exception()); - exec->clearException(); - } - return toRef(jsValue); + JSValue jsValue = jsObject->get(exec, propertyName->identifier(&exec->vm())); + handleExceptionIfNeeded(exec, exception); + return toRef(exec, jsValue); } void JSObjectSetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSPropertyAttributes attributes, JSValueRef* exception) { - JSLock lock; + if (!ctx) { + ASSERT_NOT_REACHED(); + return; + } ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + JSObject* jsObject = toJS(object); - UString::Rep* nameRep = toJS(propertyName); - JSValue* jsValue = toJS(value); - - jsObject->put(exec, Identifier(nameRep), jsValue, attributes); - if (exec->hadException()) { - if (exception) - *exception = toRef(exec->exception()); - exec->clearException(); + Identifier name(propertyName->identifier(&exec->vm())); + JSValue jsValue = toJS(exec, value); + + if (attributes && !jsObject->hasProperty(exec, name)) { + PropertyDescriptor desc(jsValue, attributes); + jsObject->methodTable()->defineOwnProperty(jsObject, exec, name, desc, false); + } else { + PutPropertySlot slot(jsObject); + jsObject->methodTable()->put(jsObject, exec, name, jsValue, slot); } + + handleExceptionIfNeeded(exec, exception); } JSValueRef JSObjectGetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef* exception) { - JSLock lock; + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + JSObject* jsObject = toJS(object); - JSValue* jsValue = jsObject->get(exec, propertyIndex); - if (exec->hadException()) { - if (exception) - *exception = toRef(exec->exception()); - exec->clearException(); - } - return toRef(jsValue); + JSValue jsValue = jsObject->get(exec, propertyIndex); + handleExceptionIfNeeded(exec, exception); + return toRef(exec, jsValue); } void JSObjectSetPropertyAtIndex(JSContextRef ctx, JSObjectRef object, unsigned propertyIndex, JSValueRef value, JSValueRef* exception) { - JSLock lock; + if (!ctx) { + ASSERT_NOT_REACHED(); + return; + } ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + JSObject* jsObject = toJS(object); - JSValue* jsValue = toJS(value); + JSValue jsValue = toJS(exec, value); - jsObject->put(exec, propertyIndex, jsValue); - if (exec->hadException()) { - if (exception) - *exception = toRef(exec->exception()); - exec->clearException(); - } + jsObject->methodTable()->putByIndex(jsObject, exec, propertyIndex, jsValue, false); + handleExceptionIfNeeded(exec, exception); } bool JSObjectDeleteProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { - JSLock lock; + if (!ctx) { + ASSERT_NOT_REACHED(); + return false; + } ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + JSObject* jsObject = toJS(object); - UString::Rep* nameRep = toJS(propertyName); - bool result = jsObject->deleteProperty(exec, Identifier(nameRep)); - if (exec->hadException()) { - if (exception) - *exception = toRef(exec->exception()); - exec->clearException(); - } + bool result = jsObject->methodTable()->deleteProperty(jsObject, exec, propertyName->identifier(&exec->vm())); + handleExceptionIfNeeded(exec, exception); return result; } void* JSObjectGetPrivate(JSObjectRef object) { - JSObject* jsObject = toJS(object); - - if (jsObject->inherits(&JSCallbackObject::info)) - return static_cast*>(jsObject)->getPrivate(); - else if (jsObject->inherits(&JSCallbackObject::info)) - return static_cast*>(jsObject)->getPrivate(); + JSObject* jsObject = uncheckedToJS(object); + + // Get wrapped object if proxied + if (jsObject->inherits(JSProxy::info())) + jsObject = jsCast(jsObject)->target(); + + if (jsObject->inherits(JSCallbackObject::info())) + return jsCast*>(jsObject)->getPrivate(); + if (jsObject->inherits(JSCallbackObject::info())) + return jsCast*>(jsObject)->getPrivate(); +#if JSC_OBJC_API_ENABLED + if (jsObject->inherits(JSCallbackObject::info())) + return jsCast*>(jsObject)->getPrivate(); +#endif return 0; } bool JSObjectSetPrivate(JSObjectRef object, void* data) { - JSObject* jsObject = toJS(object); - - if (jsObject->inherits(&JSCallbackObject::info)) { - static_cast*>(jsObject)->setPrivate(data); + JSObject* jsObject = uncheckedToJS(object); + + // Get wrapped object if proxied + if (jsObject->inherits(JSProxy::info())) + jsObject = jsCast(jsObject)->target(); + + if (jsObject->inherits(JSCallbackObject::info())) { + jsCast*>(jsObject)->setPrivate(data); return true; - } else if (jsObject->inherits(&JSCallbackObject::info)) { - static_cast*>(jsObject)->setPrivate(data); + } + if (jsObject->inherits(JSCallbackObject::info())) { + jsCast*>(jsObject)->setPrivate(data); + return true; + } +#if JSC_OBJC_API_ENABLED + if (jsObject->inherits(JSCallbackObject::info())) { + jsCast*>(jsObject)->setPrivate(data); return true; } +#endif return false; } -bool JSObjectIsFunction(JSContextRef, JSObjectRef object) +JSValueRef JSObjectGetPrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName) +{ + ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + JSObject* jsObject = toJS(object); + JSValue result; + Identifier name(propertyName->identifier(&exec->vm())); + + // Get wrapped object if proxied + if (jsObject->inherits(JSProxy::info())) + jsObject = jsCast(jsObject)->target(); + + if (jsObject->inherits(JSCallbackObject::info())) + result = jsCast*>(jsObject)->getPrivateProperty(name); + else if (jsObject->inherits(JSCallbackObject::info())) + result = jsCast*>(jsObject)->getPrivateProperty(name); +#if JSC_OBJC_API_ENABLED + else if (jsObject->inherits(JSCallbackObject::info())) + result = jsCast*>(jsObject)->getPrivateProperty(name); +#endif + return toRef(exec, result); +} + +bool JSObjectSetPrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef value) +{ + ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + JSObject* jsObject = toJS(object); + JSValue jsValue = value ? toJS(exec, value) : JSValue(); + Identifier name(propertyName->identifier(&exec->vm())); + + // Get wrapped object if proxied + if (jsObject->inherits(JSProxy::info())) + jsObject = jsCast(jsObject)->target(); + + if (jsObject->inherits(JSCallbackObject::info())) { + jsCast*>(jsObject)->setPrivateProperty(exec->vm(), name, jsValue); + return true; + } + if (jsObject->inherits(JSCallbackObject::info())) { + jsCast*>(jsObject)->setPrivateProperty(exec->vm(), name, jsValue); + return true; + } +#if JSC_OBJC_API_ENABLED + if (jsObject->inherits(JSCallbackObject::info())) { + jsCast*>(jsObject)->setPrivateProperty(exec->vm(), name, jsValue); + return true; + } +#endif + return false; +} + +bool JSObjectDeletePrivateProperty(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName) { + ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); JSObject* jsObject = toJS(object); - return jsObject->implementsCall(); + Identifier name(propertyName->identifier(&exec->vm())); + + // Get wrapped object if proxied + if (jsObject->inherits(JSProxy::info())) + jsObject = jsCast(jsObject)->target(); + + if (jsObject->inherits(JSCallbackObject::info())) { + jsCast*>(jsObject)->deletePrivateProperty(name); + return true; + } + if (jsObject->inherits(JSCallbackObject::info())) { + jsCast*>(jsObject)->deletePrivateProperty(name); + return true; + } +#if JSC_OBJC_API_ENABLED + if (jsObject->inherits(JSCallbackObject::info())) { + jsCast*>(jsObject)->deletePrivateProperty(name); + return true; + } +#endif + return false; +} + +bool JSObjectIsFunction(JSContextRef ctx, JSObjectRef object) +{ + if (!object) + return false; + JSLockHolder locker(toJS(ctx)); + CallData callData; + JSCell* cell = toJS(object); + return cell->methodTable()->getCallData(cell, callData) != CallTypeNone; } JSValueRef JSObjectCallAsFunction(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { - JSLock lock; ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + + if (!object) + return 0; + JSObject* jsObject = toJS(object); JSObject* jsThisObject = toJS(thisObject); if (!jsThisObject) - jsThisObject = exec->dynamicGlobalObject(); - - List argList; + jsThisObject = exec->globalThisValue(); + + MarkedArgumentBuffer argList; for (size_t i = 0; i < argumentCount; i++) - argList.append(toJS(arguments[i])); + argList.append(toJS(exec, arguments[i])); - JSValueRef result = toRef(jsObject->call(exec, jsThisObject, argList)); // returns NULL if object->implementsCall() is false - if (exec->hadException()) { - if (exception) - *exception = toRef(exec->exception()); - exec->clearException(); + CallData callData; + CallType callType = jsObject->methodTable()->getCallData(jsObject, callData); + if (callType == CallTypeNone) + return 0; + + JSValueRef result = toRef(exec, call(exec, jsObject, callType, callData, jsThisObject, argList)); + if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow) result = 0; - } return result; } bool JSObjectIsConstructor(JSContextRef, JSObjectRef object) { + if (!object) + return false; JSObject* jsObject = toJS(object); - return jsObject->implementsConstruct(); + ConstructData constructData; + return jsObject->methodTable()->getConstructData(jsObject, constructData) != ConstructTypeNone; } JSObjectRef JSObjectCallAsConstructor(JSContextRef ctx, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { - JSLock lock; ExecState* exec = toJS(ctx); + JSLockHolder locker(exec); + + if (!object) + return 0; + JSObject* jsObject = toJS(object); - - List argList; + + ConstructData constructData; + ConstructType constructType = jsObject->methodTable()->getConstructData(jsObject, constructData); + if (constructType == ConstructTypeNone) + return 0; + + MarkedArgumentBuffer argList; for (size_t i = 0; i < argumentCount; i++) - argList.append(toJS(arguments[i])); - - JSObjectRef result = toRef(jsObject->construct(exec, argList)); // returns NULL if object->implementsCall() is false - if (exec->hadException()) { - if (exception) - *exception = toRef(exec->exception()); - exec->clearException(); + argList.append(toJS(exec, arguments[i])); + JSObjectRef result = toRef(construct(exec, jsObject, constructType, constructData, argList)); + if (handleExceptionIfNeeded(exec, exception) == ExceptionStatus::DidThrow) result = 0; - } return result; } -struct OpaqueJSPropertyNameArray -{ - OpaqueJSPropertyNameArray() : refCount(0) +struct OpaqueJSPropertyNameArray { + WTF_MAKE_FAST_ALLOCATED; +public: + OpaqueJSPropertyNameArray(VM* vm) + : refCount(0) + , vm(vm) { } unsigned refCount; - PropertyNameArray array; + VM* vm; + Vector> array; }; JSPropertyNameArrayRef JSObjectCopyPropertyNames(JSContextRef ctx, JSObjectRef object) { - JSLock lock; - JSObject* jsObject = toJS(object); + if (!ctx) { + ASSERT_NOT_REACHED(); + return 0; + } ExecState* exec = toJS(ctx); - - JSPropertyNameArrayRef propertyNames = new OpaqueJSPropertyNameArray(); - jsObject->getPropertyNames(exec, propertyNames->array); + JSLockHolder locker(exec); + + VM* vm = &exec->vm(); + + JSObject* jsObject = toJS(object); + JSPropertyNameArrayRef propertyNames = new OpaqueJSPropertyNameArray(vm); + PropertyNameArray array(vm); + jsObject->methodTable()->getPropertyNames(jsObject, exec, array, EnumerationMode()); + + size_t size = array.size(); + propertyNames->array.reserveInitialCapacity(size); + for (size_t i = 0; i < size; ++i) + propertyNames->array.uncheckedAppend(JSRetainPtr(Adopt, OpaqueJSString::create(array[i].string()).leakRef())); return JSPropertyNameArrayRetain(propertyNames); } JSPropertyNameArrayRef JSPropertyNameArrayRetain(JSPropertyNameArrayRef array) { - JSLock lock; ++array->refCount; return array; } void JSPropertyNameArrayRelease(JSPropertyNameArrayRef array) { - JSLock lock; - if (--array->refCount == 0) + if (--array->refCount == 0) { + JSLockHolder locker(array->vm); delete array; + } } size_t JSPropertyNameArrayGetCount(JSPropertyNameArrayRef array) @@ -361,14 +654,12 @@ size_t JSPropertyNameArrayGetCount(JSPropertyNameArrayRef array) JSStringRef JSPropertyNameArrayGetNameAtIndex(JSPropertyNameArrayRef array, size_t index) { - return toRef(array->array[static_cast(index)].ustring().rep()); + return array->array[static_cast(index)].get(); } void JSPropertyNameAccumulatorAddName(JSPropertyNameAccumulatorRef array, JSStringRef propertyName) { - JSLock lock; PropertyNameArray* propertyNames = toJS(array); - UString::Rep* rep = toJS(propertyName); - - propertyNames->add(Identifier(rep)); + JSLockHolder locker(propertyNames->vm()); + propertyNames->add(propertyName->identifier(propertyNames->vm())); }