diff options
Diffstat (limited to 'Source/WebCore/bridge')
33 files changed, 914 insertions, 330 deletions
diff --git a/Source/WebCore/bridge/Bridge.h b/Source/WebCore/bridge/Bridge.h index 41f6aa54a..c6c2763db 100644 --- a/Source/WebCore/bridge/Bridge.h +++ b/Source/WebCore/bridge/Bridge.h @@ -11,10 +11,10 @@ * 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 diff --git a/Source/WebCore/bridge/IdentifierRep.cpp b/Source/WebCore/bridge/IdentifierRep.cpp index 3af7a37fa..303770100 100644 --- a/Source/WebCore/bridge/IdentifierRep.cpp +++ b/Source/WebCore/bridge/IdentifierRep.cpp @@ -10,10 +10,10 @@ * 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 @@ -28,6 +28,7 @@ #include "JSDOMBinding.h" #include <wtf/HashMap.h> +#include <wtf/NeverDestroyed.h> #include <wtf/StdLibExtras.h> #include <wtf/text/WTFString.h> @@ -39,7 +40,7 @@ typedef HashSet<IdentifierRep*> IdentifierSet; static IdentifierSet& identifierSet() { - DEFINE_STATIC_LOCAL(IdentifierSet, identifierSet, ()); + static NeverDestroyed<IdentifierSet> identifierSet; return identifierSet; } @@ -47,7 +48,7 @@ typedef HashMap<int, IdentifierRep*> IntIdentifierMap; static IntIdentifierMap& intIdentifierMap() { - DEFINE_STATIC_LOCAL(IntIdentifierMap, intIdentifierMap, ()); + static NeverDestroyed<IntIdentifierMap> intIdentifierMap; return intIdentifierMap; } @@ -81,7 +82,7 @@ typedef HashMap<RefPtr<StringImpl>, IdentifierRep*> StringIdentifierMap; static StringIdentifierMap& stringIdentifierMap() { - DEFINE_STATIC_LOCAL(StringIdentifierMap, stringIdentifierMap, ()); + static NeverDestroyed<StringIdentifierMap> stringIdentifierMap; return stringIdentifierMap; } diff --git a/Source/WebCore/bridge/IdentifierRep.h b/Source/WebCore/bridge/IdentifierRep.h index 5a57ed8ae..248ef8931 100644 --- a/Source/WebCore/bridge/IdentifierRep.h +++ b/Source/WebCore/bridge/IdentifierRep.h @@ -10,10 +10,10 @@ * 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 @@ -36,10 +36,10 @@ namespace WebCore { class IdentifierRep { WTF_MAKE_FAST_ALLOCATED; public: - static IdentifierRep* get(int); - static IdentifierRep* get(const char*); + WEBCORE_EXPORT static IdentifierRep* get(int); + WEBCORE_EXPORT static IdentifierRep* get(const char*); - static bool isValid(IdentifierRep*); + WEBCORE_EXPORT static bool isValid(IdentifierRep*); bool isString() const { return m_isString; } diff --git a/Source/WebCore/bridge/NP_jsobject.cpp b/Source/WebCore/bridge/NP_jsobject.cpp index 34dd14696..343b468f8 100644 --- a/Source/WebCore/bridge/NP_jsobject.cpp +++ b/Source/WebCore/bridge/NP_jsobject.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004, 2006 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -29,12 +29,10 @@ #include "NP_jsobject.h" -#include "PluginView.h" #include "c_utility.h" #include "c_instance.h" #include "IdentifierRep.h" #include "JSDOMBinding.h" -#include "npruntime_impl.h" #include "npruntime_priv.h" #include "runtime_root.h" #include <runtime/Error.h> @@ -43,8 +41,13 @@ #include <runtime/PropertyNameArray.h> #include <parser/SourceCode.h> #include <runtime/Completion.h> +#include <wtf/NeverDestroyed.h> #include <wtf/text/WTFString.h> +#pragma GCC visibility push(default) +#include "npruntime_impl.h" +#pragma GCC visibility pop + using namespace JSC; using namespace JSC::Bindings; using namespace WebCore; @@ -85,7 +88,7 @@ public: private: struct RootObjectInvalidationCallback : public RootObject::InvalidationCallback { - virtual void operator()(RootObject*); + void operator()(RootObject*) override; }; RootObjectInvalidationCallback m_invalidationCallback; @@ -97,7 +100,7 @@ private: static ObjectMap& objectMap() { - DEFINE_STATIC_LOCAL(ObjectMap, map, ()); + static NeverDestroyed<ObjectMap> map; return map; } @@ -138,7 +141,7 @@ static NPClass noScriptClass = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; NPClass* NPScriptObjectClass = &javascriptClass; static NPClass* NPNoScriptObjectClass = &noScriptClass; -NPObject* _NPN_CreateScriptObject(NPP npp, JSObject* imp, PassRefPtr<RootObject> rootObject) +NPObject* _NPN_CreateScriptObject(NPP npp, JSObject* imp, RefPtr<RootObject>&& rootObject) { if (NPObject* object = objectMap().get(rootObject.get(), imp)) return _NPN_RetainObject(object); @@ -174,14 +177,18 @@ bool _NPN_InvokeDefault(NPP, NPObject* o, const NPVariant* args, uint32_t argCou if (!rootObject || !rootObject->isValid()) return false; - ExecState* exec = rootObject->globalObject()->globalExec(); - JSLockHolder lock(exec); + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + ExecState* exec = globalObject->globalExec(); // Call the function object. JSValue function = obj->imp; CallData callData; CallType callType = getCallData(function, callData); - if (callType == CallTypeNone) + if (callType == CallType::None) return false; MarkedArgumentBuffer argList; @@ -190,7 +197,7 @@ bool _NPN_InvokeDefault(NPP, NPObject* o, const NPVariant* args, uint32_t argCou // Convert and return the result of the function call. convertValueToNPVariant(exec, resultV, result); - exec->clearException(); + scope.clearException(); return true; } @@ -222,12 +229,17 @@ bool _NPN_Invoke(NPP npp, NPObject* o, NPIdentifier methodName, const NPVariant* RootObject* rootObject = obj->rootObject; if (!rootObject || !rootObject->isValid()) return false; - ExecState* exec = rootObject->globalObject()->globalExec(); - JSLockHolder lock(exec); + + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + ExecState* exec = globalObject->globalExec(); JSValue function = obj->imp->get(exec, identifierFromNPIdentifier(exec, i->string())); CallData callData; CallType callType = getCallData(function, callData); - if (callType == CallTypeNone) + if (callType == CallType::None) return false; // Call the function object. @@ -237,7 +249,7 @@ bool _NPN_Invoke(NPP npp, NPObject* o, NPIdentifier methodName, const NPVariant* // Convert and return the result of the function call. convertValueToNPVariant(exec, resultV, result); - exec->clearException(); + scope.clearException(); return true; } @@ -248,7 +260,7 @@ bool _NPN_Invoke(NPP npp, NPObject* o, NPIdentifier methodName, const NPVariant* return true; } -bool _NPN_Evaluate(NPP instance, NPObject* o, NPString* s, NPVariant* variant) +bool _NPN_Evaluate(NPP, NPObject* o, NPString* s, NPVariant* variant) { if (o->_class == NPScriptObjectClass) { JavaScriptObject* obj = reinterpret_cast<JavaScriptObject*>(o); @@ -257,18 +269,18 @@ bool _NPN_Evaluate(NPP instance, NPObject* o, NPString* s, NPVariant* variant) if (!rootObject || !rootObject->isValid()) return false; - // There is a crash in Flash when evaluating a script that destroys the - // PluginView, so we destroy it asynchronously. - PluginView::keepAlive(instance); + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); - ExecState* exec = rootObject->globalObject()->globalExec(); - JSLockHolder lock(exec); + ExecState* exec = globalObject->globalExec(); String scriptString = convertNPStringToUTF16(s); - JSValue returnValue = JSC::evaluate(rootObject->globalObject()->globalExec(), makeSource(scriptString), JSC::JSValue()); + JSValue returnValue = JSC::evaluate(exec, JSC::makeSource(scriptString, { }), JSC::JSValue()); convertValueToNPVariant(exec, returnValue, variant); - exec->clearException(); + scope.clearException(); return true; } @@ -285,10 +297,14 @@ bool _NPN_GetProperty(NPP, NPObject* o, NPIdentifier propertyName, NPVariant* va if (!rootObject || !rootObject->isValid()) return false; - ExecState* exec = rootObject->globalObject()->globalExec(); + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + ExecState* exec = globalObject->globalExec(); IdentifierRep* i = static_cast<IdentifierRep*>(propertyName); - JSLockHolder lock(exec); JSValue result; if (i->isString()) result = obj->imp->get(exec, identifierFromNPIdentifier(exec, i->string())); @@ -296,7 +312,7 @@ bool _NPN_GetProperty(NPP, NPObject* o, NPIdentifier propertyName, NPVariant* va result = obj->imp->get(exec, i->number()); convertValueToNPVariant(exec, result, variant); - exec->clearException(); + scope.clearException(); return true; } @@ -319,8 +335,12 @@ bool _NPN_SetProperty(NPP, NPObject* o, NPIdentifier propertyName, const NPVaria if (!rootObject || !rootObject->isValid()) return false; - ExecState* exec = rootObject->globalObject()->globalExec(); - JSLockHolder lock(exec); + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + ExecState* exec = globalObject->globalExec(); IdentifierRep* i = static_cast<IdentifierRep*>(propertyName); if (i->isString()) { @@ -328,7 +348,7 @@ bool _NPN_SetProperty(NPP, NPObject* o, NPIdentifier propertyName, const NPVaria obj->imp->methodTable()->put(obj->imp, exec, identifierFromNPIdentifier(exec, i->string()), convertNPVariantToValue(exec, variant, rootObject), slot); } else obj->imp->methodTable()->putByIndex(obj->imp, exec, i->number(), convertNPVariantToValue(exec, variant, rootObject), false); - exec->clearException(); + scope.clearException(); return true; } @@ -347,27 +367,32 @@ bool _NPN_RemoveProperty(NPP, NPObject* o, NPIdentifier propertyName) if (!rootObject || !rootObject->isValid()) return false; - ExecState* exec = rootObject->globalObject()->globalExec(); + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + ExecState* exec = globalObject->globalExec(); + IdentifierRep* i = static_cast<IdentifierRep*>(propertyName); if (i->isString()) { if (!obj->imp->hasProperty(exec, identifierFromNPIdentifier(exec, i->string()))) { - exec->clearException(); + scope.clearException(); return false; } } else { if (!obj->imp->hasProperty(exec, i->number())) { - exec->clearException(); + scope.clearException(); return false; } } - JSLockHolder lock(exec); if (i->isString()) obj->imp->methodTable()->deleteProperty(obj->imp, exec, identifierFromNPIdentifier(exec, i->string())); else obj->imp->methodTable()->deletePropertyByIndex(obj->imp, exec, i->number()); - exec->clearException(); + scope.clearException(); return true; } return false; @@ -382,17 +407,21 @@ bool _NPN_HasProperty(NPP, NPObject* o, NPIdentifier propertyName) if (!rootObject || !rootObject->isValid()) return false; - ExecState* exec = rootObject->globalObject()->globalExec(); + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + ExecState* exec = globalObject->globalExec(); IdentifierRep* i = static_cast<IdentifierRep*>(propertyName); - JSLockHolder lock(exec); if (i->isString()) { bool result = obj->imp->hasProperty(exec, identifierFromNPIdentifier(exec, i->string())); - exec->clearException(); + scope.clearException(); return result; } bool result = obj->imp->hasProperty(exec, i->number()); - exec->clearException(); + scope.clearException(); return result; } @@ -415,10 +444,14 @@ bool _NPN_HasMethod(NPP, NPObject* o, NPIdentifier methodName) if (!rootObject || !rootObject->isValid()) return false; - ExecState* exec = rootObject->globalObject()->globalExec(); - JSLockHolder lock(exec); + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + ExecState* exec = globalObject->globalExec(); JSValue func = obj->imp->get(exec, identifierFromNPIdentifier(exec, i->string())); - exec->clearException(); + scope.clearException(); return !func.isUndefined(); } @@ -444,11 +477,15 @@ bool _NPN_Enumerate(NPP, NPObject* o, NPIdentifier** identifier, uint32_t* count if (!rootObject || !rootObject->isValid()) return false; - ExecState* exec = rootObject->globalObject()->globalExec(); - JSLockHolder lock(exec); - PropertyNameArray propertyNames(exec); + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); - obj->imp->methodTable()->getPropertyNames(obj->imp, exec, propertyNames, ExcludeDontEnumProperties); + ExecState* exec = globalObject->globalExec(); + PropertyNameArray propertyNames(exec, PropertyNameMode::Strings); + + obj->imp->methodTable()->getPropertyNames(obj->imp, exec, propertyNames, EnumerationMode()); unsigned size = static_cast<unsigned>(propertyNames.size()); // FIXME: This should really call NPN_MemAlloc but that's in WebKit NPIdentifier* identifiers = static_cast<NPIdentifier*>(malloc(sizeof(NPIdentifier) * size)); @@ -459,7 +496,7 @@ bool _NPN_Enumerate(NPP, NPObject* o, NPIdentifier** identifier, uint32_t* count *identifier = identifiers; *count = size; - exec->clearException(); + scope.clearException(); return true; } @@ -480,15 +517,19 @@ bool _NPN_Construct(NPP, NPObject* o, const NPVariant* args, uint32_t argCount, RootObject* rootObject = obj->rootObject; if (!rootObject || !rootObject->isValid()) return false; - - ExecState* exec = rootObject->globalObject()->globalExec(); - JSLockHolder lock(exec); + + auto globalObject = rootObject->globalObject(); + VM& vm = globalObject->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_CATCH_SCOPE(vm); + + ExecState* exec = globalObject->globalExec(); // Call the constructor object. JSValue constructor = obj->imp; ConstructData constructData; ConstructType constructType = getConstructData(constructor, constructData); - if (constructType == ConstructTypeNone) + if (constructType == ConstructType::None) return false; MarkedArgumentBuffer argList; @@ -497,7 +538,7 @@ bool _NPN_Construct(NPP, NPObject* o, const NPVariant* args, uint32_t argCount, // Convert and return the result. convertValueToNPVariant(exec, resultV, result); - exec->clearException(); + scope.clearException(); return true; } diff --git a/Source/WebCore/bridge/NP_jsobject.h b/Source/WebCore/bridge/NP_jsobject.h index 6c49d1bbd..cf9e9fde9 100644 --- a/Source/WebCore/bridge/NP_jsobject.h +++ b/Source/WebCore/bridge/NP_jsobject.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004, 2006 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -47,8 +47,8 @@ struct JavaScriptObject JSC::Bindings::RootObject* rootObject; }; -NPObject* _NPN_CreateScriptObject(NPP npp, JSC::JSObject*, PassRefPtr<JSC::Bindings::RootObject> rootObject); -NPObject* _NPN_CreateNoScriptObject(void); +WEBCORE_EXPORT NPObject* _NPN_CreateScriptObject(NPP, JSC::JSObject*, RefPtr<JSC::Bindings::RootObject>&&); +WEBCORE_EXPORT NPObject* _NPN_CreateNoScriptObject(void); #endif // ENABLE(NETSCAPE_PLUGIN_API) diff --git a/Source/WebCore/bridge/c/CRuntimeObject.cpp b/Source/WebCore/bridge/c/CRuntimeObject.cpp index a015687b3..2a93a514d 100644 --- a/Source/WebCore/bridge/c/CRuntimeObject.cpp +++ b/Source/WebCore/bridge/c/CRuntimeObject.cpp @@ -13,7 +13,7 @@ * 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 @@ -35,17 +35,17 @@ namespace JSC { namespace Bindings { -const ClassInfo CRuntimeObject::s_info = { "CRuntimeObject", &RuntimeObject::s_info, 0, 0, CREATE_METHOD_TABLE(CRuntimeObject) }; +const ClassInfo CRuntimeObject::s_info = { "CRuntimeObject", &RuntimeObject::s_info, 0, CREATE_METHOD_TABLE(CRuntimeObject) }; -CRuntimeObject::CRuntimeObject(VM& vm, Structure* structure, PassRefPtr<CInstance> instance) - : RuntimeObject(vm, structure, instance) +CRuntimeObject::CRuntimeObject(VM& vm, Structure* structure, RefPtr<CInstance>&& instance) + : RuntimeObject(vm, structure, WTFMove(instance)) { } void CRuntimeObject::finishCreation(VM& vm) { Base::finishCreation(vm); - ASSERT(inherits(info())); + ASSERT(inherits(vm, info())); } CInstance* CRuntimeObject::getInternalCInstance() const diff --git a/Source/WebCore/bridge/c/CRuntimeObject.h b/Source/WebCore/bridge/c/CRuntimeObject.h index f0781f92d..a0b4a24ed 100644 --- a/Source/WebCore/bridge/c/CRuntimeObject.h +++ b/Source/WebCore/bridge/c/CRuntimeObject.h @@ -13,7 +13,7 @@ * 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 @@ -44,9 +44,9 @@ class CRuntimeObject : public RuntimeObject { public: typedef RuntimeObject Base; - static CRuntimeObject* create(VM& vm, Structure* structure, PassRefPtr<CInstance> instance) + static CRuntimeObject* create(VM& vm, Structure* structure, RefPtr<CInstance>&& instance) { - CRuntimeObject* object = new (NotNull, allocateCell<CRuntimeObject>(vm.heap)) CRuntimeObject(vm, structure, instance); + CRuntimeObject* object = new (NotNull, allocateCell<CRuntimeObject>(vm.heap)) CRuntimeObject(vm, structure, WTFMove(instance)); object->finishCreation(vm); return object; } @@ -61,7 +61,7 @@ public: } private: - CRuntimeObject(VM&, Structure*, PassRefPtr<CInstance>); + CRuntimeObject(VM&, Structure*, RefPtr<CInstance>&&); void finishCreation(VM&); }; diff --git a/Source/WebCore/bridge/c/c_class.cpp b/Source/WebCore/bridge/c/c_class.cpp index e96452086..dfc0d7ac0 100644 --- a/Source/WebCore/bridge/c/c_class.cpp +++ b/Source/WebCore/bridge/c/c_class.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2003, 2006 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -70,6 +70,8 @@ CClass* CClass::classForIsA(NPClass* isa) Method* CClass::methodNamed(PropertyName propertyName, Instance* instance) const { String name(propertyName.publicName()); + if (name.isNull()) + return nullptr; if (Method* method = m_methods.get(name.impl())) return method; @@ -78,17 +80,20 @@ Method* CClass::methodNamed(PropertyName propertyName, Instance* instance) const const CInstance* inst = static_cast<const CInstance*>(instance); NPObject* obj = inst->getObject(); if (m_isa->hasMethod && m_isa->hasMethod(obj, ident)) { - Method* method = new CMethod(ident); - m_methods.set(name.impl(), adoptPtr(method)); - return method; + auto method = std::make_unique<CMethod>(ident); + CMethod* ret = method.get(); + m_methods.set(name.impl(), WTFMove(method)); + return ret; } - return 0; + return nullptr; } Field* CClass::fieldNamed(PropertyName propertyName, Instance* instance) const { String name(propertyName.publicName()); + if (name.isNull()) + return nullptr; if (Field* field = m_fields.get(name.impl())) return field; @@ -97,12 +102,13 @@ Field* CClass::fieldNamed(PropertyName propertyName, Instance* instance) const const CInstance* inst = static_cast<const CInstance*>(instance); NPObject* obj = inst->getObject(); if (m_isa->hasProperty && m_isa->hasProperty(obj, ident)) { - Field* field = new CField(ident); - m_fields.set(name.impl(), adoptPtr(field)); - return field; + auto field = std::make_unique<CField>(ident); + CField* ret = field.get(); + m_fields.set(name.impl(), WTFMove(field)); + return ret; } - return 0; + return nullptr; } } } // namespace JSC::Bindings diff --git a/Source/WebCore/bridge/c/c_class.h b/Source/WebCore/bridge/c/c_class.h index 3274f7846..d4b6b0ee6 100644 --- a/Source/WebCore/bridge/c/c_class.h +++ b/Source/WebCore/bridge/c/c_class.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2003 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -43,13 +43,13 @@ public: static CClass* classForIsA(NPClass*); virtual ~CClass(); - virtual Method* methodNamed(PropertyName, Instance*) const override; - virtual Field* fieldNamed(PropertyName, Instance*) const override; + Method* methodNamed(PropertyName, Instance*) const override; + Field* fieldNamed(PropertyName, Instance*) const override; private: NPClass* m_isa; - mutable HashMap<RefPtr<StringImpl>, OwnPtr<Method>> m_methods; - mutable HashMap<RefPtr<StringImpl>, OwnPtr<Field>> m_fields; + mutable HashMap<RefPtr<StringImpl>, std::unique_ptr<Method>> m_methods; + mutable HashMap<RefPtr<StringImpl>, std::unique_ptr<Field>> m_fields; }; } // namespace Bindings diff --git a/Source/WebCore/bridge/c/c_instance.cpp b/Source/WebCore/bridge/c/c_instance.cpp index 441c20ed7..e69d7a06e 100644 --- a/Source/WebCore/bridge/c/c_instance.cpp +++ b/Source/WebCore/bridge/c/c_instance.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2003, 2006, 2016 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -45,6 +45,7 @@ #include <runtime/JSLock.h> #include <runtime/PropertyNameArray.h> #include <wtf/Assertions.h> +#include <wtf/NeverDestroyed.h> #include <wtf/StdLibExtras.h> #include <wtf/StringExtras.h> #include <wtf/Vector.h> @@ -56,7 +57,7 @@ namespace Bindings { static String& globalExceptionString() { - DEFINE_STATIC_LOCAL(String, exceptionStr, ()); + static NeverDestroyed<String> exceptionStr; return exceptionStr; } @@ -71,15 +72,17 @@ void CInstance::moveGlobalExceptionToExecState(ExecState* exec) return; { - JSLockHolder lock(exec); - exec->vm().throwException(exec, createError(exec, globalExceptionString())); + VM& vm = exec->vm(); + JSLockHolder lock(vm); + auto scope = DECLARE_THROW_SCOPE(vm); + throwException(exec, scope, createError(exec, globalExceptionString())); } globalExceptionString() = String(); } -CInstance::CInstance(NPObject* o, PassRefPtr<RootObject> rootObject) - : Instance(rootObject) +CInstance::CInstance(NPObject* o, RefPtr<RootObject>&& rootObject) + : Instance(WTFMove(rootObject)) { _object = _NPN_RetainObject(o); _class = 0; @@ -138,12 +141,12 @@ private: void finishCreation(VM& vm, const String& name) { Base::finishCreation(vm, name); - ASSERT(inherits(info())); + ASSERT(inherits(vm, info())); } }; -const ClassInfo CRuntimeMethod::s_info = { "CRuntimeMethod", &RuntimeMethod::s_info, 0, 0, CREATE_METHOD_TABLE(CRuntimeMethod) }; +const ClassInfo CRuntimeMethod::s_info = { "CRuntimeMethod", &RuntimeMethod::s_info, 0, CREATE_METHOD_TABLE(CRuntimeMethod) }; JSValue CInstance::getMethod(ExecState* exec, PropertyName propertyName) { @@ -153,8 +156,11 @@ JSValue CInstance::getMethod(ExecState* exec, PropertyName propertyName) JSValue CInstance::invokeMethod(ExecState* exec, RuntimeMethod* runtimeMethod) { - if (!asObject(runtimeMethod)->inherits(CRuntimeMethod::info())) - return exec->vm().throwException(exec, createTypeError(exec, "Attempt to invoke non-plug-in method on plug-in object.")); + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + if (!asObject(runtimeMethod)->inherits(vm, CRuntimeMethod::info())) + return throwTypeError(exec, scope, ASCIILiteral("Attempt to invoke non-plug-in method on plug-in object.")); CMethod* method = static_cast<CMethod*>(runtimeMethod->method()); ASSERT(method); @@ -183,7 +189,7 @@ JSValue CInstance::invokeMethod(ExecState* exec, RuntimeMethod* runtimeMethod) } if (!retval) - exec->vm().throwException(exec, createError(exec, ASCIILiteral("Error calling method on NPObject."))); + throwException(exec, scope, createError(exec, ASCIILiteral("Error calling method on NPObject."))); for (i = 0; i < count; i++) _NPN_ReleaseVariantValue(&cArgs[i]); @@ -196,6 +202,9 @@ JSValue CInstance::invokeMethod(ExecState* exec, RuntimeMethod* runtimeMethod) JSValue CInstance::invokeDefaultMethod(ExecState* exec) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + if (!_object->_class->invokeDefault) return jsUndefined(); @@ -218,7 +227,7 @@ JSValue CInstance::invokeDefaultMethod(ExecState* exec) } if (!retval) - exec->vm().throwException(exec, createError(exec, ASCIILiteral("Error calling method on NPObject."))); + throwException(exec, scope, createError(exec, ASCIILiteral("Error calling method on NPObject."))); for (i = 0; i < count; i++) _NPN_ReleaseVariantValue(&cArgs[i]); @@ -235,6 +244,9 @@ bool CInstance::supportsConstruct() const JSValue CInstance::invokeConstruct(ExecState* exec, const ArgList& args) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + if (!_object->_class->construct) return jsUndefined(); @@ -257,7 +269,7 @@ JSValue CInstance::invokeConstruct(ExecState* exec, const ArgList& args) } if (!retval) - exec->vm().throwException(exec, createError(exec, ASCIILiteral("Error calling method on NPObject."))); + throwException(exec, scope, createError(exec, ASCIILiteral("Error calling method on NPObject."))); for (i = 0; i < count; i++) _NPN_ReleaseVariantValue(&cArgs[i]); @@ -310,6 +322,9 @@ JSValue CInstance::valueOf(ExecState* exec) const bool CInstance::toJSPrimitive(ExecState* exec, const char* name, JSValue& resultValue) const { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + NPIdentifier ident = _NPN_GetStringIdentifier(name); if (!_object->_class->hasMethod(_object, ident)) return false; @@ -327,7 +342,7 @@ bool CInstance::toJSPrimitive(ExecState* exec, const char* name, JSValue& result } if (!retval) - exec->vm().throwException(exec, createError(exec, ASCIILiteral("Error calling method on NPObject."))); + throwException(exec, scope, createError(exec, ASCIILiteral("Error calling method on NPObject."))); resultValue = convertNPVariantToValue(exec, &resultVariant, m_rootObject.get()); _NPN_ReleaseVariantValue(&resultVariant); diff --git a/Source/WebCore/bridge/c/c_instance.h b/Source/WebCore/bridge/c/c_instance.h index db6335319..ee866280e 100644 --- a/Source/WebCore/bridge/c/c_instance.h +++ b/Source/WebCore/bridge/c/c_instance.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2003 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -30,7 +30,6 @@ #include "BridgeJSC.h" #include "runtime_root.h" -#include <wtf/PassRefPtr.h> #include <wtf/text/WTFString.h> typedef struct NPObject NPObject; @@ -43,9 +42,9 @@ class CClass; class CInstance : public Instance { public: - static PassRefPtr<CInstance> create(NPObject* object, PassRefPtr<RootObject> rootObject) + static Ref<CInstance> create(NPObject* object, RefPtr<RootObject>&& rootObject) { - return adoptRef(new CInstance(object, rootObject)); + return adoptRef(*new CInstance(object, WTFMove(rootObject))); } static void setGlobalException(String); @@ -53,20 +52,20 @@ public: virtual ~CInstance(); - virtual Class *getClass() const override; + Class *getClass() const override; - virtual JSValue valueOf(ExecState*) const override; - virtual JSValue defaultValue(ExecState*, PreferredPrimitiveType) const override; + JSValue valueOf(ExecState*) const override; + JSValue defaultValue(ExecState*, PreferredPrimitiveType) const override; - virtual JSValue getMethod(ExecState*, PropertyName) override; - virtual JSValue invokeMethod(ExecState*, RuntimeMethod*) override; - virtual bool supportsInvokeDefaultMethod() const override; - virtual JSValue invokeDefaultMethod(ExecState*) override; + JSValue getMethod(ExecState*, PropertyName) override; + JSValue invokeMethod(ExecState*, RuntimeMethod*) override; + bool supportsInvokeDefaultMethod() const override; + JSValue invokeDefaultMethod(ExecState*) override; - virtual bool supportsConstruct() const override; - virtual JSValue invokeConstruct(ExecState*, const ArgList&) override; + bool supportsConstruct() const override; + JSValue invokeConstruct(ExecState*, const ArgList&) override; - virtual void getPropertyNames(ExecState*, PropertyNameArray&) override; + void getPropertyNames(ExecState*, PropertyNameArray&) override; JSValue stringValue(ExecState*) const; JSValue numberValue(ExecState*) const; @@ -75,9 +74,9 @@ public: NPObject *getObject() const { return _object; } private: - CInstance(NPObject*, PassRefPtr<RootObject>); + CInstance(NPObject*, RefPtr<RootObject>&&); - virtual RuntimeObject* newRuntimeObject(ExecState*) override; + RuntimeObject* newRuntimeObject(ExecState*) override; bool toJSPrimitive(ExecState*, const char*, JSValue&) const; diff --git a/Source/WebCore/bridge/c/c_runtime.cpp b/Source/WebCore/bridge/c/c_runtime.cpp index 7aa879864..3efa71421 100644 --- a/Source/WebCore/bridge/c/c_runtime.cpp +++ b/Source/WebCore/bridge/c/c_runtime.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -61,7 +61,7 @@ JSValue CField::valueFromInstance(ExecState* exec, const Instance* inst) const return jsUndefined(); } -void CField::setValueToInstance(ExecState *exec, const Instance *inst, JSValue aValue) const +bool CField::setValueToInstance(ExecState *exec, const Instance *inst, JSValue aValue) const { const CInstance* instance = static_cast<const CInstance*>(inst); NPObject* obj = instance->getObject(); @@ -69,14 +69,17 @@ void CField::setValueToInstance(ExecState *exec, const Instance *inst, JSValue a NPVariant variant; convertValueToNPVariant(exec, aValue, &variant); + bool result = false; { JSLock::DropAllLocks dropAllLocks(exec); - obj->_class->setProperty(obj, _fieldIdentifier, &variant); + result = obj->_class->setProperty(obj, _fieldIdentifier, &variant); CInstance::moveGlobalExceptionToExecState(exec); } _NPN_ReleaseVariantValue(&variant); + return result; } + return false; } } } diff --git a/Source/WebCore/bridge/c/c_runtime.h b/Source/WebCore/bridge/c/c_runtime.h index c7f7b9ba1..219fefee4 100644 --- a/Source/WebCore/bridge/c/c_runtime.h +++ b/Source/WebCore/bridge/c/c_runtime.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004, 2006 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -38,8 +38,8 @@ class CField : public Field { public: CField(NPIdentifier ident) : _fieldIdentifier(ident) { } - virtual JSValue valueFromInstance(ExecState*, const Instance*) const override; - virtual void setValueToInstance(ExecState*, const Instance*, JSValue) const override; + JSValue valueFromInstance(ExecState*, const Instance*) const override; + bool setValueToInstance(ExecState*, const Instance*, JSValue) const override; NPIdentifier identifier() const { return _fieldIdentifier; } @@ -54,7 +54,7 @@ public: CMethod(NPIdentifier ident) : _methodIdentifier(ident) { } NPIdentifier identifier() const { return _methodIdentifier; } - virtual int numParameters() const override { return 0; } + int numParameters() const override { return 0; } private: NPIdentifier _methodIdentifier; diff --git a/Source/WebCore/bridge/c/c_utility.cpp b/Source/WebCore/bridge/c/c_utility.cpp index 8c5f473ba..66d1e15a1 100644 --- a/Source/WebCore/bridge/c/c_utility.cpp +++ b/Source/WebCore/bridge/c/c_utility.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2006, 2013 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004, 2006, 2013 Apple Inc. All rights reserved. * Copyright (C) 2006 Alexey Proskuryakov (ap@nypop.com) * * Redistribution and use in source and binary forms, with or without @@ -11,10 +11,10 @@ * 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 @@ -31,6 +31,7 @@ #include "c_utility.h" #include "CRuntimeObject.h" +#include "DOMWindow.h" #include "JSDOMBinding.h" #include "JSDOMWindow.h" #include "NP_jsobject.h" @@ -69,11 +70,12 @@ static String convertUTF8ToUTF16WithLatin1Fallback(const NPUTF8* UTF8Chars, int void convertValueToNPVariant(ExecState* exec, JSValue value, NPVariant* result) { JSLockHolder lock(exec); - + VM& vm = exec->vm(); + VOID_TO_NPVARIANT(*result); if (value.isString()) { - String ustring = value.toString(exec)->value(exec); + String ustring = value.toWTFString(exec); CString cstring = ustring.utf8(); NPString string = { (const NPUTF8*)cstring.data(), static_cast<uint32_t>(cstring.length()) }; NPN_InitializeVariantWithStringCopy(result, &string); @@ -85,7 +87,7 @@ void convertValueToNPVariant(ExecState* exec, JSValue value, NPVariant* result) NULL_TO_NPVARIANT(*result); } else if (value.isObject()) { JSObject* object = asObject(value); - if (object->classInfo() == CRuntimeObject::info()) { + if (object->classInfo(vm) == CRuntimeObject::info()) { CRuntimeObject* runtimeObject = static_cast<CRuntimeObject*>(object); CInstance* instance = runtimeObject->getInternalCInstance(); if (instance) { @@ -122,7 +124,7 @@ JSValue convertNPVariantToValue(ExecState* exec, const NPVariant* variant, RootO if (type == NPVariantType_Double) return jsNumber(NPVARIANT_TO_DOUBLE(*variant)); if (type == NPVariantType_String) - return WebCore::jsStringWithCache(exec, convertNPStringToUTF16(&variant->value.stringValue)); + return jsStringWithCache(exec, convertNPStringToUTF16(&variant->value.stringValue)); if (type == NPVariantType_Object) { NPObject* obj = variant->value.objectValue; @@ -144,7 +146,7 @@ String convertNPStringToUTF16(const NPString* string) Identifier identifierFromNPIdentifier(ExecState* exec, const NPUTF8* name) { - return Identifier(exec, convertUTF8ToUTF16WithLatin1Fallback(name, -1)); + return Identifier::fromString(exec, convertUTF8ToUTF16WithLatin1Fallback(name, -1)); } } } diff --git a/Source/WebCore/bridge/c/c_utility.h b/Source/WebCore/bridge/c/c_utility.h index 7901d735b..fbb71d6c7 100644 --- a/Source/WebCore/bridge/c/c_utility.h +++ b/Source/WebCore/bridge/c/c_utility.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -29,8 +29,8 @@ #if ENABLE(NETSCAPE_PLUGIN_API) #include "npruntime_internal.h" +#include <runtime/JSCInlines.h> #include <runtime/JSCJSValue.h> -#include <runtime/Operations.h> #include <wtf/Forward.h> namespace JSC { diff --git a/Source/WebCore/bridge/jsc/BridgeJSC.cpp b/Source/WebCore/bridge/jsc/BridgeJSC.cpp index 6ed148552..f17b1df14 100644 --- a/Source/WebCore/bridge/jsc/BridgeJSC.cpp +++ b/Source/WebCore/bridge/jsc/BridgeJSC.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003, 2006, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2006, 2008, 2015 Apple Inc. All rights reserved. * Copyright 2010, The Android Open Source Project * * Redistribution and use in source and binary forms, with or without @@ -11,10 +11,10 @@ * 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 @@ -27,6 +27,7 @@ #include "config.h" #include "BridgeJSC.h" +#include "DOMWindow.h" #include "JSDOMWindowBase.h" #include "runtime_object.h" #include "runtime_root.h" @@ -37,8 +38,8 @@ namespace JSC { namespace Bindings { -Array::Array(PassRefPtr<RootObject> rootObject) - : m_rootObject(rootObject) +Array::Array(RefPtr<RootObject>&& rootObject) + : m_rootObject(WTFMove(rootObject)) { ASSERT(m_rootObject); } @@ -47,8 +48,8 @@ Array::~Array() { } -Instance::Instance(PassRefPtr<RootObject> rootObject) - : m_rootObject(rootObject) +Instance::Instance(RefPtr<RootObject>&& rootObject) + : m_rootObject(WTFMove(rootObject)) { ASSERT(m_rootObject); } diff --git a/Source/WebCore/bridge/jsc/BridgeJSC.h b/Source/WebCore/bridge/jsc/BridgeJSC.h index a2ef87896..5fe85f5f4 100644 --- a/Source/WebCore/bridge/jsc/BridgeJSC.h +++ b/Source/WebCore/bridge/jsc/BridgeJSC.h @@ -11,10 +11,10 @@ * 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 @@ -28,8 +28,8 @@ #define BridgeJSC_h #include "Bridge.h" +#include <runtime/JSCInlines.h> #include <runtime/JSString.h> -#include <runtime/Operations.h> #include <wtf/HashMap.h> #include <wtf/RefCounted.h> #include <wtf/Vector.h> @@ -52,7 +52,7 @@ class RuntimeObject; class Field { public: virtual JSValue valueFromInstance(ExecState*, const Instance*) const = 0; - virtual void setValueToInstance(ExecState*, const Instance*, JSValue) const = 0; + virtual bool setValueToInstance(ExecState*, const Instance*, JSValue) const = 0; virtual ~Field() { } }; @@ -70,7 +70,7 @@ public: class Instance : public RefCounted<Instance> { public: - Instance(PassRefPtr<RootObject>); + WEBCORE_EXPORT Instance(RefPtr<RootObject>&&); // These functions are called before and after the main entry points into // the native implementations. They can be used to establish and cleanup @@ -79,7 +79,7 @@ public: void end(); virtual Class* getClass() const = 0; - JSObject* createRuntimeObject(ExecState*); + WEBCORE_EXPORT JSObject* createRuntimeObject(ExecState*); void willInvalidateRuntimeObject(); // Returns false if the value was not set successfully. @@ -102,15 +102,15 @@ public: RootObject* rootObject() const; - virtual ~Instance(); + WEBCORE_EXPORT virtual ~Instance(); virtual bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&) { return false; } - virtual void put(JSObject*, ExecState*, PropertyName, JSValue, PutPropertySlot&) { } + virtual bool put(JSObject*, ExecState*, PropertyName, JSValue, PutPropertySlot&) { return false; } protected: virtual void virtualBegin() { } virtual void virtualEnd() { } - virtual RuntimeObject* newRuntimeObject(ExecState*); + WEBCORE_EXPORT virtual RuntimeObject* newRuntimeObject(ExecState*); RefPtr<RootObject> m_rootObject; @@ -121,10 +121,10 @@ private: class Array { WTF_MAKE_NONCOPYABLE(Array); public: - Array(PassRefPtr<RootObject>); + explicit Array(RefPtr<RootObject>&&); virtual ~Array(); - virtual void setValueAt(ExecState*, unsigned index, JSValue) const = 0; + virtual bool setValueAt(ExecState*, unsigned index, JSValue) const = 0; virtual JSValue valueAt(ExecState*, unsigned index) const = 0; virtual unsigned int getLength() const = 0; diff --git a/Source/WebCore/bridge/make_testbindings b/Source/WebCore/bridge/make_testbindings new file mode 100755 index 000000000..1f528feb7 --- /dev/null +++ b/Source/WebCore/bridge/make_testbindings @@ -0,0 +1,2 @@ +cc -g -o testbindingsM testbindings.mm -I../kjs -F$SYMROOTS -framework JavaScriptCore -framework Foundation -lstdc++ +cc -g -o testbindingsC testbindings.cpp -I../kjs -F$SYMROOTS -framework JavaScriptCore -framework Foundation -lstdc++ diff --git a/Source/WebCore/bridge/npruntime.cpp b/Source/WebCore/bridge/npruntime.cpp index 93693e646..5905ea800 100644 --- a/Source/WebCore/bridge/npruntime.cpp +++ b/Source/WebCore/bridge/npruntime.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004, 2006 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -29,16 +29,16 @@ #include "IdentifierRep.h" #include "npruntime_internal.h" -#include "npruntime_impl.h" #include "npruntime_priv.h" #include "c_utility.h" #include <runtime/Identifier.h> -#include <runtime/JSLock.h> #include <wtf/Assertions.h> -#include <wtf/HashMap.h> -using namespace JSC::Bindings; +#pragma GCC visibility push(default) +#include "npruntime_impl.h" +#pragma GCC visibility pop + using namespace WebCore; NPIdentifier _NPN_GetStringIdentifier(const NPUTF8* name) diff --git a/Source/WebCore/bridge/npruntime_impl.h b/Source/WebCore/bridge/npruntime_impl.h index 559f340ba..0618042cc 100644 --- a/Source/WebCore/bridge/npruntime_impl.h +++ b/Source/WebCore/bridge/npruntime_impl.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 diff --git a/Source/WebCore/bridge/npruntime_priv.h b/Source/WebCore/bridge/npruntime_priv.h index 301c163f1..59dc0a17d 100644 --- a/Source/WebCore/bridge/npruntime_priv.h +++ b/Source/WebCore/bridge/npruntime_priv.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2003, 2006 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 diff --git a/Source/WebCore/bridge/runtime_array.cpp b/Source/WebCore/bridge/runtime_array.cpp index 11ca04145..66203ea78 100644 --- a/Source/WebCore/bridge/runtime_array.cpp +++ b/Source/WebCore/bridge/runtime_array.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2008, 2016 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -35,7 +35,7 @@ using namespace WebCore; namespace JSC { -const ClassInfo RuntimeArray::s_info = { "RuntimeArray", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(RuntimeArray) }; +const ClassInfo RuntimeArray::s_info = { "RuntimeArray", &Base::s_info, 0, CREATE_METHOD_TABLE(RuntimeArray) }; RuntimeArray::RuntimeArray(ExecState* exec, Structure* structure) : JSArray(exec->vm(), structure, 0) @@ -46,7 +46,7 @@ RuntimeArray::RuntimeArray(ExecState* exec, Structure* structure) void RuntimeArray::finishCreation(VM& vm, Bindings::Array* array) { Base::finishCreation(vm); - ASSERT(inherits(info())); + ASSERT(inherits(vm, info())); m_array = array; } @@ -60,20 +60,17 @@ void RuntimeArray::destroy(JSCell* cell) static_cast<RuntimeArray*>(cell)->RuntimeArray::~RuntimeArray(); } -EncodedJSValue RuntimeArray::lengthGetter(ExecState* exec, EncodedJSValue, EncodedJSValue thisValue, PropertyName) +EncodedJSValue RuntimeArray::lengthGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName) { - RuntimeArray* thisObject = jsDynamicCast<RuntimeArray*>(JSValue::decode(thisValue)); + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + RuntimeArray* thisObject = jsDynamicDowncast<RuntimeArray*>(vm, JSValue::decode(thisValue)); if (!thisObject) - return throwVMTypeError(exec); + return throwVMTypeError(exec, scope); return JSValue::encode(jsNumber(thisObject->getLength())); } -EncodedJSValue RuntimeArray::indexGetter(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue, unsigned index) -{ - RuntimeArray* thisObj = jsCast<RuntimeArray*>(JSValue::decode(slotBase)); - return JSValue::encode(thisObj->getConcreteArray()->valueAt(exec, index)); -} - void RuntimeArray::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode) { RuntimeArray* thisObject = jsCast<RuntimeArray*>(object); @@ -81,7 +78,7 @@ void RuntimeArray::getOwnPropertyNames(JSObject* object, ExecState* exec, Proper for (unsigned i = 0; i < length; ++i) propertyNames.add(Identifier::from(exec, i)); - if (mode == IncludeDontEnumProperties) + if (mode.includeDontEnumProperties()) propertyNames.add(exec->propertyNames().length); JSObject::getOwnPropertyNames(thisObject, exec, propertyNames, mode); @@ -95,10 +92,10 @@ bool RuntimeArray::getOwnPropertySlot(JSObject* object, ExecState* exec, Propert return true; } - unsigned index = propertyName.asIndex(); - if (index < thisObject->getLength()) { - ASSERT(index != PropertyName::NotAnIndex); - slot.setCustomIndex(thisObject, DontDelete | DontEnum, index, thisObject->indexGetter); + std::optional<uint32_t> index = parseIndex(propertyName); + if (index && index.value() < thisObject->getLength()) { + slot.setValue(thisObject, DontDelete | DontEnum, + thisObject->getConcreteArray()->valueAt(exec, index.value())); return true; } @@ -109,39 +106,44 @@ bool RuntimeArray::getOwnPropertySlotByIndex(JSObject* object, ExecState *exec, { RuntimeArray* thisObject = jsCast<RuntimeArray*>(object); if (index < thisObject->getLength()) { - slot.setCustomIndex(thisObject, DontDelete | DontEnum, index, thisObject->indexGetter); + slot.setValue(thisObject, DontDelete | DontEnum, + thisObject->getConcreteArray()->valueAt(exec, index)); return true; } return JSObject::getOwnPropertySlotByIndex(thisObject, exec, index, slot); } -void RuntimeArray::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot) +bool RuntimeArray::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell); if (propertyName == exec->propertyNames().length) { - exec->vm().throwException(exec, createRangeError(exec, "Range error")); - return; - } - - unsigned index = propertyName.asIndex(); - if (index != PropertyName::NotAnIndex) { - thisObject->getConcreteArray()->setValueAt(exec, index, value); - return; + throwException(exec, scope, createRangeError(exec, "Range error")); + return false; } - JSObject::put(thisObject, exec, propertyName, value, slot); + if (std::optional<uint32_t> index = parseIndex(propertyName)) + return thisObject->getConcreteArray()->setValueAt(exec, index.value(), value); + + scope.release(); + return JSObject::put(thisObject, exec, propertyName, value, slot); } -void RuntimeArray::putByIndex(JSCell* cell, ExecState* exec, unsigned index, JSValue value, bool) +bool RuntimeArray::putByIndex(JSCell* cell, ExecState* exec, unsigned index, JSValue value, bool) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + RuntimeArray* thisObject = jsCast<RuntimeArray*>(cell); if (index >= thisObject->getLength()) { - exec->vm().throwException(exec, createRangeError(exec, "Range error")); - return; + throwException(exec, scope, createRangeError(exec, "Range error")); + return false; } - thisObject->getConcreteArray()->setValueAt(exec, index, value); + return thisObject->getConcreteArray()->setValueAt(exec, index, value); } bool RuntimeArray::deleteProperty(JSCell*, ExecState*, PropertyName) diff --git a/Source/WebCore/bridge/runtime_array.h b/Source/WebCore/bridge/runtime_array.h index 5e01f73ae..44302ad11 100644 --- a/Source/WebCore/bridge/runtime_array.h +++ b/Source/WebCore/bridge/runtime_array.h @@ -10,10 +10,10 @@ * 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 @@ -35,6 +35,7 @@ namespace JSC { class RuntimeArray : public JSArray { public: typedef JSArray Base; + static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | OverridesGetPropertyNames; static RuntimeArray* create(ExecState* exec, Bindings::Array* array) { @@ -55,8 +56,8 @@ public: static void getOwnPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode); static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&); static bool getOwnPropertySlotByIndex(JSObject*, ExecState*, unsigned, PropertySlot&); - static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&); - static void putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow); + static bool put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&); + static bool putByIndex(JSCell*, ExecState*, unsigned propertyName, JSValue, bool shouldThrow); static bool deleteProperty(JSCell*, ExecState*, PropertyName); static bool deletePropertyByIndex(JSCell*, ExecState*, unsigned propertyName); @@ -74,18 +75,15 @@ public: static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) { - return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info(), ArrayClass); + return Structure::create(vm, globalObject, prototype, TypeInfo(DerivedArrayType, StructureFlags), info(), ArrayClass); } protected: void finishCreation(VM&, Bindings::Array*); - static const unsigned StructureFlags = OverridesGetOwnPropertySlot | InterceptsGetOwnPropertySlotByIndexEvenWhenLengthIsNotZero | OverridesGetPropertyNames | JSArray::StructureFlags; - private: RuntimeArray(ExecState*, Structure*); - static EncodedJSValue lengthGetter(ExecState*, EncodedJSValue, EncodedJSValue, PropertyName); - static EncodedJSValue indexGetter(ExecState*, EncodedJSValue, EncodedJSValue, unsigned); + static EncodedJSValue lengthGetter(ExecState*, EncodedJSValue, PropertyName); BindingsArray* m_array; }; diff --git a/Source/WebCore/bridge/runtime_method.cpp b/Source/WebCore/bridge/runtime_method.cpp index eb4ee9aa1..fe55f76dc 100644 --- a/Source/WebCore/bridge/runtime_method.cpp +++ b/Source/WebCore/bridge/runtime_method.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2008, 2016 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -39,7 +39,7 @@ namespace JSC { using namespace Bindings; -const ClassInfo RuntimeMethod::s_info = { "RuntimeMethod", &InternalFunction::s_info, 0, 0, CREATE_METHOD_TABLE(RuntimeMethod) }; +WEBCORE_EXPORT const ClassInfo RuntimeMethod::s_info = { "RuntimeMethod", &InternalFunction::s_info, 0, CREATE_METHOD_TABLE(RuntimeMethod) }; RuntimeMethod::RuntimeMethod(JSGlobalObject* globalObject, Structure* structure, Method* method) // Callers will need to pass in the right global object corresponding to this native object "method". @@ -51,14 +51,17 @@ RuntimeMethod::RuntimeMethod(JSGlobalObject* globalObject, Structure* structure, void RuntimeMethod::finishCreation(VM& vm, const String& ident) { Base::finishCreation(vm, ident); - ASSERT(inherits(info())); + ASSERT(inherits(vm, info())); } -EncodedJSValue RuntimeMethod::lengthGetter(ExecState* exec, EncodedJSValue, EncodedJSValue thisValue, PropertyName) +EncodedJSValue RuntimeMethod::lengthGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName) { - RuntimeMethod* thisObject = jsDynamicCast<RuntimeMethod*>(JSValue::decode(thisValue)); + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + RuntimeMethod* thisObject = jsDynamicDowncast<RuntimeMethod*>(vm, JSValue::decode(thisValue)); if (!thisObject) - return throwVMTypeError(exec); + return throwVMTypeError(exec, scope); return JSValue::encode(jsNumber(thisObject->m_method->numParameters())); } @@ -75,25 +78,28 @@ bool RuntimeMethod::getOwnPropertySlot(JSObject* object, ExecState* exec, Proper static EncodedJSValue JSC_HOST_CALL callRuntimeMethod(ExecState* exec) { - RuntimeMethod* method = static_cast<RuntimeMethod*>(exec->callee()); + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + RuntimeMethod* method = static_cast<RuntimeMethod*>(exec->jsCallee()); if (!method->method()) return JSValue::encode(jsUndefined()); RefPtr<Instance> instance; - JSValue thisValue = exec->hostThisValue(); - if (thisValue.inherits(RuntimeObject::info())) { + JSValue thisValue = exec->thisValue(); + if (thisValue.inherits(vm, RuntimeObject::info())) { RuntimeObject* runtimeObject = static_cast<RuntimeObject*>(asObject(thisValue)); instance = runtimeObject->getInternalInstance(); if (!instance) - return JSValue::encode(RuntimeObject::throwInvalidAccessError(exec)); + return JSValue::encode(RuntimeObject::throwInvalidAccessError(exec, scope)); } else { // Calling a runtime object of a plugin element? - if (thisValue.inherits(JSHTMLElement::info())) - instance = pluginInstance(jsCast<JSHTMLElement*>(asObject(thisValue))->impl()); + if (thisValue.inherits(vm, JSHTMLElement::info())) + instance = pluginInstance(jsCast<JSHTMLElement*>(asObject(thisValue))->wrapped()); if (!instance) - return throwVMTypeError(exec); + return throwVMTypeError(exec, scope); } ASSERT(instance); @@ -106,7 +112,7 @@ static EncodedJSValue JSC_HOST_CALL callRuntimeMethod(ExecState* exec) CallType RuntimeMethod::getCallData(JSCell*, CallData& callData) { callData.native.function = callRuntimeMethod; - return CallTypeHost; + return CallType::Host; } } diff --git a/Source/WebCore/bridge/runtime_method.h b/Source/WebCore/bridge/runtime_method.h index e141968ae..d86d59475 100644 --- a/Source/WebCore/bridge/runtime_method.h +++ b/Source/WebCore/bridge/runtime_method.h @@ -10,10 +10,10 @@ * 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 @@ -29,13 +29,13 @@ #include "BridgeJSC.h" #include <runtime/InternalFunction.h> #include <runtime/JSGlobalObject.h> -#include <wtf/OwnPtr.h> namespace JSC { -class RuntimeMethod : public InternalFunction { +class WEBCORE_EXPORT RuntimeMethod : public InternalFunction { public: typedef InternalFunction Base; + static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | TypeOfShouldCallGetCallData; static RuntimeMethod* create(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, const String& name, Bindings::Method* method) { @@ -61,13 +61,12 @@ public: protected: RuntimeMethod(JSGlobalObject*, Structure*, Bindings::Method*); void finishCreation(VM&, const String&); - static const unsigned StructureFlags = OverridesGetOwnPropertySlot | InternalFunction::StructureFlags; static CallType getCallData(JSCell*, CallData&); static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&); private: - static EncodedJSValue lengthGetter(ExecState*, EncodedJSValue, EncodedJSValue, PropertyName); + static EncodedJSValue lengthGetter(ExecState*, EncodedJSValue, PropertyName); Bindings::Method* m_method; }; diff --git a/Source/WebCore/bridge/runtime_object.cpp b/Source/WebCore/bridge/runtime_object.cpp index 32524f49a..ebdcbf00c 100644 --- a/Source/WebCore/bridge/runtime_object.cpp +++ b/Source/WebCore/bridge/runtime_object.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2003, 2008, 2009 Apple Inc. All rights reserved. + * Copyright (C) 2003, 2008-2009, 2016 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -35,18 +35,18 @@ using namespace WebCore; namespace JSC { namespace Bindings { -const ClassInfo RuntimeObject::s_info = { "RuntimeObject", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(RuntimeObject) }; +WEBCORE_EXPORT const ClassInfo RuntimeObject::s_info = { "RuntimeObject", &Base::s_info, 0, CREATE_METHOD_TABLE(RuntimeObject) }; -RuntimeObject::RuntimeObject(VM& vm, Structure* structure, PassRefPtr<Instance> instance) +RuntimeObject::RuntimeObject(VM& vm, Structure* structure, RefPtr<Instance>&& instance) : JSDestructibleObject(vm, structure) - , m_instance(instance) + , m_instance(WTFMove(instance)) { } void RuntimeObject::finishCreation(VM& vm) { Base::finishCreation(vm); - ASSERT(inherits(info())); + ASSERT(inherits(vm, info())); } void RuntimeObject::destroy(JSCell* cell) @@ -59,16 +59,19 @@ void RuntimeObject::invalidate() ASSERT(m_instance); if (m_instance) m_instance->willInvalidateRuntimeObject(); - m_instance = 0; + m_instance = nullptr; } -EncodedJSValue RuntimeObject::fallbackObjectGetter(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue, PropertyName propertyName) +EncodedJSValue RuntimeObject::fallbackObjectGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName propertyName) { - RuntimeObject* thisObj = jsCast<RuntimeObject*>(JSValue::decode(slotBase)); + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + RuntimeObject* thisObj = jsCast<RuntimeObject*>(JSValue::decode(thisValue)); RefPtr<Instance> instance = thisObj->m_instance; if (!instance) - return JSValue::encode(throwInvalidAccessError(exec)); + return JSValue::encode(throwInvalidAccessError(exec, scope)); instance->begin(); @@ -80,13 +83,16 @@ EncodedJSValue RuntimeObject::fallbackObjectGetter(ExecState* exec, EncodedJSVal return JSValue::encode(result); } -EncodedJSValue RuntimeObject::fieldGetter(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue, PropertyName propertyName) +EncodedJSValue RuntimeObject::fieldGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName propertyName) { - RuntimeObject* thisObj = jsCast<RuntimeObject*>(JSValue::decode(slotBase)); + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + RuntimeObject* thisObj = jsCast<RuntimeObject*>(JSValue::decode(thisValue)); RefPtr<Instance> instance = thisObj->m_instance; if (!instance) - return JSValue::encode(throwInvalidAccessError(exec)); + return JSValue::encode(throwInvalidAccessError(exec, scope)); instance->begin(); @@ -99,13 +105,16 @@ EncodedJSValue RuntimeObject::fieldGetter(ExecState* exec, EncodedJSValue slotBa return JSValue::encode(result); } -EncodedJSValue RuntimeObject::methodGetter(ExecState* exec, EncodedJSValue slotBase, EncodedJSValue, PropertyName propertyName) +EncodedJSValue RuntimeObject::methodGetter(ExecState* exec, EncodedJSValue thisValue, PropertyName propertyName) { - RuntimeObject* thisObj = jsCast<RuntimeObject*>(JSValue::decode(slotBase)); + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + + RuntimeObject* thisObj = jsCast<RuntimeObject*>(JSValue::decode(thisValue)); RefPtr<Instance> instance = thisObj->m_instance; if (!instance) - return JSValue::encode(throwInvalidAccessError(exec)); + return JSValue::encode(throwInvalidAccessError(exec, scope)); instance->begin(); @@ -118,9 +127,12 @@ EncodedJSValue RuntimeObject::methodGetter(ExecState* exec, EncodedJSValue slotB bool RuntimeObject::getOwnPropertySlot(JSObject* object, ExecState *exec, PropertyName propertyName, PropertySlot& slot) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + RuntimeObject* thisObject = jsCast<RuntimeObject*>(object); if (!thisObject->m_instance) { - throwInvalidAccessError(exec); + throwInvalidAccessError(exec, scope); return false; } @@ -161,25 +173,30 @@ bool RuntimeObject::getOwnPropertySlot(JSObject* object, ExecState *exec, Proper return instance->getOwnPropertySlot(thisObject, exec, propertyName, slot); } -void RuntimeObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot) +bool RuntimeObject::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + RuntimeObject* thisObject = jsCast<RuntimeObject*>(cell); if (!thisObject->m_instance) { - throwInvalidAccessError(exec); - return; + throwInvalidAccessError(exec, scope); + return false; } RefPtr<Instance> instance = thisObject->m_instance; instance->begin(); // Set the value of the property. + bool result = false; Field *aField = instance->getClass()->fieldNamed(propertyName, instance.get()); if (aField) - aField->setValueToInstance(exec, instance.get(), value); + result = aField->setValueToInstance(exec, instance.get(), value); else if (!instance->setValueOfUndefinedField(exec, propertyName, value)) - instance->put(thisObject, exec, propertyName, value, slot); + result = instance->put(thisObject, exec, propertyName, value, slot); instance->end(); + return result; } bool RuntimeObject::deleteProperty(JSCell*, ExecState*, PropertyName) @@ -190,9 +207,12 @@ bool RuntimeObject::deleteProperty(JSCell*, ExecState*, PropertyName) JSValue RuntimeObject::defaultValue(const JSObject* object, ExecState* exec, PreferredPrimitiveType hint) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + const RuntimeObject* thisObject = jsCast<const RuntimeObject*>(object); if (!thisObject->m_instance) - return throwInvalidAccessError(exec); + return throwInvalidAccessError(exec, scope); RefPtr<Instance> instance = thisObject->m_instance; @@ -204,8 +224,8 @@ JSValue RuntimeObject::defaultValue(const JSObject* object, ExecState* exec, Pre static EncodedJSValue JSC_HOST_CALL callRuntimeObject(ExecState* exec) { - ASSERT(exec->callee()->inherits(RuntimeObject::info())); - RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance()); + ASSERT(exec->jsCallee()->inherits(exec->vm(), RuntimeObject::info())); + RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->jsCallee())->getInternalInstance()); instance->begin(); JSValue result = instance->invokeDefaultMethod(exec); instance->end(); @@ -216,21 +236,21 @@ CallType RuntimeObject::getCallData(JSCell* cell, CallData& callData) { RuntimeObject* thisObject = jsCast<RuntimeObject*>(cell); if (!thisObject->m_instance) - return CallTypeNone; + return CallType::None; RefPtr<Instance> instance = thisObject->m_instance; if (!instance->supportsInvokeDefaultMethod()) - return CallTypeNone; + return CallType::None; callData.native.function = callRuntimeObject; - return CallTypeHost; + return CallType::Host; } static EncodedJSValue JSC_HOST_CALL callRuntimeConstructor(ExecState* exec) { - JSObject* constructor = exec->callee(); - ASSERT(constructor->inherits(RuntimeObject::info())); - RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->callee())->getInternalInstance()); + JSObject* constructor = exec->jsCallee(); + ASSERT(constructor->inherits(exec->vm(), RuntimeObject::info())); + RefPtr<Instance> instance(static_cast<RuntimeObject*>(exec->jsCallee())->getInternalInstance()); instance->begin(); ArgList args(exec); JSValue result = instance->invokeConstruct(exec, args); @@ -244,21 +264,24 @@ ConstructType RuntimeObject::getConstructData(JSCell* cell, ConstructData& const { RuntimeObject* thisObject = jsCast<RuntimeObject*>(cell); if (!thisObject->m_instance) - return ConstructTypeNone; + return ConstructType::None; RefPtr<Instance> instance = thisObject->m_instance; if (!instance->supportsConstruct()) - return ConstructTypeNone; + return ConstructType::None; constructData.native.function = callRuntimeConstructor; - return ConstructTypeHost; + return ConstructType::Host; } void RuntimeObject::getOwnPropertyNames(JSObject* object, ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode) { + VM& vm = exec->vm(); + auto scope = DECLARE_THROW_SCOPE(vm); + RuntimeObject* thisObject = jsCast<RuntimeObject*>(object); if (!thisObject->m_instance) { - throwInvalidAccessError(exec); + throwInvalidAccessError(exec, scope); return; } @@ -269,9 +292,9 @@ void RuntimeObject::getOwnPropertyNames(JSObject* object, ExecState* exec, Prope instance->end(); } -JSObject* RuntimeObject::throwInvalidAccessError(ExecState* exec) +JSObject* RuntimeObject::throwInvalidAccessError(ExecState* exec, ThrowScope& scope) { - return exec->vm().throwException(exec, createReferenceError(exec, "Trying to access object from destroyed plug-in.")); + return throwException(exec, scope, createReferenceError(exec, "Trying to access object from destroyed plug-in.")); } } diff --git a/Source/WebCore/bridge/runtime_object.h b/Source/WebCore/bridge/runtime_object.h index 97b55506f..16c0429f5 100644 --- a/Source/WebCore/bridge/runtime_object.h +++ b/Source/WebCore/bridge/runtime_object.h @@ -10,10 +10,10 @@ * 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 @@ -32,13 +32,14 @@ namespace JSC { namespace Bindings { -class RuntimeObject : public JSDestructibleObject { +class WEBCORE_EXPORT RuntimeObject : public JSDestructibleObject { public: typedef JSDestructibleObject Base; + static const unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | OverridesGetPropertyNames | TypeOfShouldCallGetCallData; - static RuntimeObject* create(VM& vm, Structure* structure, PassRefPtr<Instance> instance) + static RuntimeObject* create(VM& vm, Structure* structure, RefPtr<Instance>&& instance) { - RuntimeObject* object = new (NotNull, allocateCell<RuntimeObject>(vm.heap)) RuntimeObject(vm, structure, instance); + RuntimeObject* object = new (NotNull, allocateCell<RuntimeObject>(vm.heap)) RuntimeObject(vm, structure, WTFMove(instance)); object->finishCreation(vm); return object; } @@ -46,7 +47,7 @@ public: static void destroy(JSCell*); static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&); - static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&); + static bool put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&); static bool deleteProperty(JSCell*, ExecState*, PropertyName); static JSValue defaultValue(const JSObject*, ExecState*, PreferredPrimitiveType); static CallType getCallData(JSCell*, CallData&); @@ -58,7 +59,7 @@ public: Instance* getInternalInstance() const { return m_instance.get(); } - static JSObject* throwInvalidAccessError(ExecState*); + static JSObject* throwInvalidAccessError(ExecState*, ThrowScope&); DECLARE_INFO; @@ -73,14 +74,13 @@ public: } protected: - RuntimeObject(VM&, Structure*, PassRefPtr<Instance>); + RuntimeObject(VM&, Structure*, RefPtr<Instance>&&); void finishCreation(VM&); - static const unsigned StructureFlags = OverridesGetOwnPropertySlot | OverridesGetPropertyNames | Base::StructureFlags; private: - static EncodedJSValue fallbackObjectGetter(ExecState*, EncodedJSValue, EncodedJSValue, PropertyName); - static EncodedJSValue fieldGetter(ExecState*, EncodedJSValue, EncodedJSValue, PropertyName); - static EncodedJSValue methodGetter(ExecState*, EncodedJSValue, EncodedJSValue, PropertyName); + static EncodedJSValue fallbackObjectGetter(ExecState*, EncodedJSValue, PropertyName); + static EncodedJSValue fieldGetter(ExecState*, EncodedJSValue, PropertyName); + static EncodedJSValue methodGetter(ExecState*, EncodedJSValue, PropertyName); RefPtr<Instance> m_instance; }; diff --git a/Source/WebCore/bridge/runtime_root.cpp b/Source/WebCore/bridge/runtime_root.cpp index ba03d5d29..ebf877ec2 100644 --- a/Source/WebCore/bridge/runtime_root.cpp +++ b/Source/WebCore/bridge/runtime_root.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -32,8 +32,8 @@ #include <heap/Weak.h> #include <heap/WeakInlines.h> #include <runtime/JSGlobalObject.h> -#include <wtf/HashCountedSet.h> #include <wtf/HashSet.h> +#include <wtf/NeverDestroyed.h> #include <wtf/Ref.h> #include <wtf/StdLibExtras.h> @@ -46,10 +46,10 @@ namespace JSC { namespace Bindings { typedef HashSet<RootObject*> RootObjectSet; -static RootObjectSet* rootObjectSet() +static RootObjectSet& rootObjectSet() { - DEFINE_STATIC_LOCAL(RootObjectSet, staticRootObjectSet, ()); - return &staticRootObjectSet; + static NeverDestroyed<RootObjectSet> staticRootObjectSet; + return staticRootObjectSet; } // FIXME: These two functions are a potential performance problem. We could @@ -57,8 +57,8 @@ static RootObjectSet* rootObjectSet() RootObject* findProtectingRootObject(JSObject* jsObject) { - RootObjectSet::const_iterator end = rootObjectSet()->end(); - for (RootObjectSet::const_iterator it = rootObjectSet()->begin(); it != end; ++it) { + RootObjectSet::const_iterator end = rootObjectSet().end(); + for (RootObjectSet::const_iterator it = rootObjectSet().begin(); it != end; ++it) { if ((*it)->gcIsProtected(jsObject)) return *it; } @@ -67,8 +67,8 @@ RootObject* findProtectingRootObject(JSObject* jsObject) RootObject* findRootObject(JSGlobalObject* globalObject) { - RootObjectSet::const_iterator end = rootObjectSet()->end(); - for (RootObjectSet::const_iterator it = rootObjectSet()->begin(); it != end; ++it) { + RootObjectSet::const_iterator end = rootObjectSet().end(); + for (RootObjectSet::const_iterator it = rootObjectSet().begin(); it != end; ++it) { if ((*it)->globalObject() == globalObject) return *it; } @@ -79,9 +79,9 @@ RootObject::InvalidationCallback::~InvalidationCallback() { } -PassRefPtr<RootObject> RootObject::create(const void* nativeHandle, JSGlobalObject* globalObject) +Ref<RootObject> RootObject::create(const void* nativeHandle, JSGlobalObject* globalObject) { - return adoptRef(new RootObject(nativeHandle, globalObject)); + return adoptRef(*new RootObject(nativeHandle, globalObject)); } RootObject::RootObject(const void* nativeHandle, JSGlobalObject* globalObject) @@ -90,7 +90,7 @@ RootObject::RootObject(const void* nativeHandle, JSGlobalObject* globalObject) , m_globalObject(globalObject->vm(), globalObject) { ASSERT(globalObject); - rootObjectSet()->add(this); + rootObjectSet().add(this); } RootObject::~RootObject() @@ -105,13 +105,10 @@ void RootObject::invalidate() return; { - HashMap<RuntimeObject*, JSC::Weak<RuntimeObject>>::iterator end = m_runtimeObjects.end(); - for (HashMap<RuntimeObject*, JSC::Weak<RuntimeObject>>::iterator it = m_runtimeObjects.begin(); it != end; ++it) { - RuntimeObject* runtimeObject = it->value.get(); - if (!runtimeObject) // Skip zombies. - continue; + // Get the objects from the keys; the values might be nulled. + // Safe because finalized runtime objects are removed from m_runtimeObjects by RootObject::finalize. + for (RuntimeObject* runtimeObject : m_runtimeObjects.keys()) runtimeObject->invalidate(); - } m_runtimeObjects.clear(); } @@ -134,7 +131,7 @@ void RootObject::invalidate() JSC::gcUnprotect(it->key); m_protectCountSet.clear(); - rootObjectSet()->remove(this); + rootObjectSet().remove(this); } void RootObject::gcProtect(JSObject* jsObject) @@ -200,9 +197,9 @@ void RootObject::removeRuntimeObject(RuntimeObject* object) void RootObject::finalize(JSC::Handle<JSC::Unknown> handle, void*) { - RuntimeObject* object = static_cast<RuntimeObject*>(handle.get().asCell()); + RuntimeObject* object = static_cast<RuntimeObject*>(handle.slot()->asCell()); - Ref<RootObject> protect(*this); + Ref<RootObject> protectedThis(*this); object->invalidate(); weakRemove(m_runtimeObjects, object, object); } diff --git a/Source/WebCore/bridge/runtime_root.h b/Source/WebCore/bridge/runtime_root.h index 37b019d71..a998cf07c 100644 --- a/Source/WebCore/bridge/runtime_root.h +++ b/Source/WebCore/bridge/runtime_root.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004 Apple Computer, Inc. All rights reserved. + * Copyright (C) 2004 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 @@ -33,7 +33,6 @@ #include <wtf/HashCountedSet.h> #include <wtf/HashSet.h> #include <wtf/Noncopyable.h> -#include <wtf/PassRefPtr.h> #include <wtf/RefCounted.h> namespace JSC { @@ -55,9 +54,9 @@ class RootObject : public RefCounted<RootObject>, private JSC::WeakHandleOwner { friend class JavaJSObject; public: - virtual ~RootObject(); + WEBCORE_EXPORT virtual ~RootObject(); - static PassRefPtr<RootObject> create(const void* nativeHandle, JSGlobalObject*); + static Ref<RootObject> create(const void* nativeHandle, JSGlobalObject*); bool isValid() { return m_isValid; } void invalidate(); @@ -67,7 +66,7 @@ public: bool gcIsProtected(JSObject*); const void* nativeHandle() const; - JSGlobalObject* globalObject() const; + WEBCORE_EXPORT JSGlobalObject* globalObject() const; void updateGlobalObject(JSGlobalObject*); void addRuntimeObject(VM&, RuntimeObject*); @@ -83,7 +82,7 @@ private: RootObject(const void* nativeHandle, JSGlobalObject*); // WeakHandleOwner - virtual void finalize(JSC::Handle<JSC::Unknown>, void* context) override; + void finalize(JSC::Handle<JSC::Unknown>, void* context) override; bool m_isValid; diff --git a/Source/WebCore/bridge/test.js b/Source/WebCore/bridge/test.js new file mode 100644 index 000000000..5d4f79f00 --- /dev/null +++ b/Source/WebCore/bridge/test.js @@ -0,0 +1,19 @@ +myInterface.logMessage ("Starting test"); + +myInterface.logMessage ("Testing properties:"); +myInterface.logMessage ("myInterface.doubleValue = " + myInterface.doubleValue); +myInterface.logMessage ("myInterface.intValue = " + myInterface.intValue); +myInterface.logMessage ("myInterface.stringValue = " + myInterface.stringValue); +myInterface.logMessage ("myInterface.booleanValue = " + myInterface.booleanValue); +myInterface.logMessage ("myInterface.nullValue = " + myInterface.nullValue); +myInterface.logMessage ("myInterface.undefinedValue = " + myInterface.undefinedValue); + +myInterface.logMessage ("myInterface.setInt_(666) = " + myInterface.setInt_(666)); +myInterface.logMessage ("myInterface.getInt() = " + myInterface.getInt()); +myInterface.logMessage ("myInterface.getString() = " + myInterface.getString()); +myInterface.logMessage ("myInterface.myInt = " + myInterface.myInt); +myInterface.logMessage ("setting myInterface.myInt = 777"); +myInterface.myInt = 777; +myInterface.logMessage ("myInterface.myInt = " + myInterface.myInt); +myInterface.logMessage ("myInterface.getMySecondInterface().doubleValue = " + myInterface.getMySecondInterface().doubleValue); +myInterface.logMessage ("myInterface.getMySecondInterface() = " + myInterface.getMySecondInterface()); diff --git a/Source/WebCore/bridge/testC.js b/Source/WebCore/bridge/testC.js new file mode 100644 index 000000000..44677c76a --- /dev/null +++ b/Source/WebCore/bridge/testC.js @@ -0,0 +1,21 @@ +myInterface.logMessage ("Starting test"); + +myInterface.logMessage ("Testing properties:"); +myInterface.logMessage (" myInterface.doubleValue = " + myInterface.doubleValue); +myInterface.logMessage (" myInterface.intValue = " + myInterface.intValue); +myInterface.logMessage (" myInterface.stringValue = " + myInterface.stringValue); +myInterface.logMessage (" myInterface.booleanValue = " + myInterface.booleanValue); +myInterface.logMessage (" myInterface.nullValue = " + myInterface.nullValue); +myInterface.logMessage (" myInterface.undefinedValue = " + myInterface.undefinedValue); + +myInterface.logMessage ("Testing methods:"); +myInterface.logMessage (" myInterface.setDoubleValue(1234.1234) = " + myInterface.setDoubleValue(1234.1234)); +myInterface.logMessage (" myInterface.setIntValue(5678) = " + myInterface.setIntValue(5678)); +myInterface.logMessage (" myInterface.setStringValue(Goodbye) = " + myInterface.setStringValue('Goodbye')); +myInterface.logMessage (" myInterface.setBooleanValue(false) = " + myInterface.setBooleanValue(false)); + +myInterface.logMessage ("Value of properties after calling setters:"); +myInterface.logMessage (" myInterface.getDoubleValue() = " + myInterface.getDoubleValue()); +myInterface.logMessage (" myInterface.getIntValue() = " + myInterface.getIntValue()); +myInterface.logMessage (" myInterface.getStringValue() = " + myInterface.getStringValue()); +myInterface.logMessage (" myInterface.getBooleanValue() = " + myInterface.getBooleanValue()); diff --git a/Source/WebCore/bridge/testM.js b/Source/WebCore/bridge/testM.js new file mode 100644 index 000000000..7985d2192 --- /dev/null +++ b/Source/WebCore/bridge/testM.js @@ -0,0 +1,29 @@ +myInterface.logMessage ("Starting test"); + +myInterface.logMessage ("Testing properties:"); + +myInterface.jsobject = new Function ("arg1","arg2","return arg1 + arg2;"); +myInterface.logMessage ("myInterface.jsobject =" + myInterface.jsobject); + +var functionBody = 'return arg1*arg2;' + +myInterface.setJSObject_(new Function ("arg1","arg2",functionBody)); +myInterface.logMessage ("myInterface.jsobject =" + myInterface.jsobject); +myInterface.callJSObject__(5,6); +myInterface.callJSObject__(8,9); + +myInterface.logMessage ("myInterface.setInt_(666) = " + myInterface.setInt_(666)); +myInterface.logMessage ("myInterface.getInt() = " + myInterface.getInt()); +myInterface.logMessage ("myInterface.getString().foo() = " + myInterface.getString().foo()); +myInterface.logMessage ("myInterface.myInt = " + myInterface.myInt); +myInterface.logMessage ("setting myInterface.myInt = 777"); +myInterface.myInt = 777; +myInterface.logMessage ("myInterface.myInt = " + myInterface.myInt); +myInterface.logMessage ("myInterface.getMySecondInterface().doubleValue = " + myInterface.getMySecondInterface().doubleValue); +myInterface.logMessage ("myInterface.getMySecondInterface() = " + myInterface.getMySecondInterface()); + +myInterface.logMessageWithPrefix ("msg", "prefix"); + +var strings = [ "one", "two", "three" ]; + +myInterface.logMessages (strings);
\ No newline at end of file diff --git a/Source/WebCore/bridge/testbindings.cpp b/Source/WebCore/bridge/testbindings.cpp new file mode 100644 index 000000000..15bf48ca6 --- /dev/null +++ b/Source/WebCore/bridge/testbindings.cpp @@ -0,0 +1,421 @@ +/* + * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) + * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + */ +#include "config.h" + +#include "BridgeJSC.h" +#include "JSCJSValue.h" +#include "JSObject.h" +#include "interpreter.h" +#include "npruntime_internal.h" +#include "runtime_object.h" +#include "types.h" +#include <assert.h> +#include <stdio.h> +#include <string.h> + + +#define LOG(formatAndArgs...) { \ + fprintf (stderr, "%s: ", __PRETTY_FUNCTION__); \ + fprintf(stderr, formatAndArgs); \ +} + + +// ------------------ NP Interface definition -------------------- +typedef struct +{ + NPObject object; + double doubleValue; + int intValue; + NPVariant stringValue; + bool boolValue; +} MyObject; + + +static bool identifiersInitialized = false; + +#define ID_DOUBLE_VALUE 0 +#define ID_INT_VALUE 1 +#define ID_STRING_VALUE 2 +#define ID_BOOLEAN_VALUE 3 +#define ID_NULL_VALUE 4 +#define ID_UNDEFINED_VALUE 5 +#define NUM_PROPERTY_IDENTIFIERS 6 + +static NPIdentifier myPropertyIdentifiers[NUM_PROPERTY_IDENTIFIERS]; +static const NPUTF8 *myPropertyIdentifierNames[NUM_PROPERTY_IDENTIFIERS] = { + "doubleValue", + "intValue", + "stringValue", + "booleanValue", + "nullValue", + "undefinedValue" +}; + +#define ID_LOG_MESSAGE 0 +#define ID_SET_DOUBLE_VALUE 1 +#define ID_SET_INT_VALUE 2 +#define ID_SET_STRING_VALUE 3 +#define ID_SET_BOOLEAN_VALUE 4 +#define ID_GET_DOUBLE_VALUE 5 +#define ID_GET_INT_VALUE 6 +#define ID_GET_STRING_VALUE 7 +#define ID_GET_BOOLEAN_VALUE 8 +#define NUM_METHOD_IDENTIFIERS 9 + +static NPIdentifier myMethodIdentifiers[NUM_METHOD_IDENTIFIERS]; +static const NPUTF8 *myMethodIdentifierNames[NUM_METHOD_IDENTIFIERS] = { + "logMessage", + "setDoubleValue", + "setIntValue", + "setStringValue", + "setBooleanValue", + "getDoubleValue", + "getIntValue", + "getStringValue", + "getBooleanValue" +}; + +static void initializeIdentifiers() +{ + NPN_GetStringIdentifiers (myPropertyIdentifierNames, NUM_PROPERTY_IDENTIFIERS, myPropertyIdentifiers); + NPN_GetStringIdentifiers (myMethodIdentifierNames, NUM_METHOD_IDENTIFIERS, myMethodIdentifiers); +}; + +bool myHasProperty (NPClass *theClass, NPIdentifier name) +{ + int i; + for (i = 0; i < NUM_PROPERTY_IDENTIFIERS; i++) { + if (name == myPropertyIdentifiers[i]){ + return true; + } + } + return false; +} + +bool myHasMethod (NPClass *theClass, NPIdentifier name) +{ + int i; + for (i = 0; i < NUM_METHOD_IDENTIFIERS; i++) { + if (name == myMethodIdentifiers[i]){ + return true; + } + } + return false; +} + + +void logMessage (const NPVariant *message) +{ + if (message->type == NPVariantStringType) { + char msgBuf[1024]; + strncpy (msgBuf, message->value.stringValue.UTF8Characters, message->value.stringValue.UTF8Length); + msgBuf[message->value.stringValue.UTF8Length] = 0; + printf ("%s\n", msgBuf); + } + else if (message->type == NPVariantDoubleType) + printf ("%f\n", (float)message->value.doubleValue); + else if (message->type == NPVariantInt32Type) + printf ("%d\n", message->value.intValue); + else if (message->type == NPVariantObjectType) + printf ("%p\n", message->value.objectValue); +} + +void setDoubleValue (MyObject *obj, const NPVariant *variant) +{ + if (!NPN_VariantToDouble (variant, &obj->doubleValue)) { + NPUTF8 *msg = "Attempt to set double value with invalid type."; + NPString aString; + aString.UTF8Characters = msg; + aString.UTF8Length = strlen (msg); + NPN_SetException ((NPObject *)obj, &aString); + } +} + +void setIntValue (MyObject *obj, const NPVariant *variant) +{ + if (!NPN_VariantToInt32 (variant, &obj->intValue)) { + NPUTF8 *msg = "Attempt to set int value with invalid type."; + NPString aString; + aString.UTF8Characters = msg; + aString.UTF8Length = strlen (msg); + NPN_SetException ((NPObject *)obj, &aString); + } +} + +void setStringValue (MyObject *obj, const NPVariant *variant) +{ + NPN_ReleaseVariantValue (&obj->stringValue); + NPN_InitializeVariantWithVariant (&obj->stringValue, variant); +} + +void setBooleanValue (MyObject *obj, const NPVariant *variant) +{ + if (!NPN_VariantToBool (variant, (NPBool *)&obj->boolValue)) { + NPUTF8 *msg = "Attempt to set bool value with invalid type."; + NPString aString; + aString.UTF8Characters = msg; + aString.UTF8Length = strlen (msg); + NPN_SetException ((NPObject *)obj, &aString); + } +} + +void getDoubleValue (MyObject *obj, NPVariant *variant) +{ + NPN_InitializeVariantWithDouble (variant, obj->doubleValue); +} + +void getIntValue (MyObject *obj, NPVariant *variant) +{ + NPN_InitializeVariantWithInt32 (variant, obj->intValue); +} + +void getStringValue (MyObject *obj, NPVariant *variant) +{ + NPN_InitializeVariantWithVariant (variant, &obj->stringValue); +} + +void getBooleanValue (MyObject *obj, NPVariant *variant) +{ + NPN_InitializeVariantWithBool (variant, obj->boolValue); +} + +void myGetProperty (MyObject *obj, NPIdentifier name, NPVariant *variant) +{ + if (name == myPropertyIdentifiers[ID_DOUBLE_VALUE]){ + getDoubleValue (obj, variant); + } + else if (name == myPropertyIdentifiers[ID_INT_VALUE]){ + getIntValue (obj, variant); + } + else if (name == myPropertyIdentifiers[ID_STRING_VALUE]){ + getStringValue (obj, variant); + } + else if (name == myPropertyIdentifiers[ID_BOOLEAN_VALUE]){ + getBooleanValue (obj, variant); + } + else if (name == myPropertyIdentifiers[ID_NULL_VALUE]){ + return NPN_InitializeVariantAsNull (variant); + } + else if (name == myPropertyIdentifiers[ID_UNDEFINED_VALUE]){ + return NPN_InitializeVariantAsUndefined (variant); + } + else + NPN_InitializeVariantAsUndefined(variant); +} + +void mySetProperty (MyObject *obj, NPIdentifier name, const NPVariant *variant) +{ + if (name == myPropertyIdentifiers[ID_DOUBLE_VALUE]) { + setDoubleValue (obj, variant); + } + else if (name == myPropertyIdentifiers[ID_INT_VALUE]) { + setIntValue (obj, variant); + } + else if (name == myPropertyIdentifiers[ID_STRING_VALUE]) { + setStringValue (obj, variant); + } + else if (name == myPropertyIdentifiers[ID_BOOLEAN_VALUE]) { + setBooleanValue (obj, variant); + } + else if (name == myPropertyIdentifiers[ID_NULL_VALUE]) { + // Do nothing! + } + else if (name == myPropertyIdentifiers[ID_UNDEFINED_VALUE]) { + // Do nothing! + } +} + +void myInvoke (MyObject *obj, NPIdentifier name, NPVariant *args, unsigned argCount, NPVariant *result) +{ + if (name == myMethodIdentifiers[ID_LOG_MESSAGE]) { + if (argCount == 1 && NPN_VariantIsString(&args[0])) + logMessage (&args[0]); + NPN_InitializeVariantAsVoid (result); + } + else if (name == myMethodIdentifiers[ID_SET_DOUBLE_VALUE]) { + if (argCount == 1 && NPN_VariantIsDouble (&args[0])) + setDoubleValue (obj, &args[0]); + NPN_InitializeVariantAsVoid (result); + } + else if (name == myMethodIdentifiers[ID_SET_INT_VALUE]) { + if (argCount == 1 && (NPN_VariantIsDouble (&args[0]) || NPN_VariantIsInt32 (&args[0]))) + setIntValue (obj, &args[0]); + NPN_InitializeVariantAsVoid (result); + } + else if (name == myMethodIdentifiers[ID_SET_STRING_VALUE]) { + if (argCount == 1 && NPN_VariantIsString (&args[0])) + setStringValue (obj, &args[0]); + NPN_InitializeVariantAsVoid (result); + } + else if (name == myMethodIdentifiers[ID_SET_BOOLEAN_VALUE]) { + if (argCount == 1 && NPN_VariantIsBool (&args[0])) + setBooleanValue (obj, &args[0]); + NPN_InitializeVariantAsVoid (result); + } + else if (name == myMethodIdentifiers[ID_GET_DOUBLE_VALUE]) { + getDoubleValue (obj, result); + } + else if (name == myMethodIdentifiers[ID_GET_INT_VALUE]) { + getIntValue (obj, result); + } + else if (name == myMethodIdentifiers[ID_GET_STRING_VALUE]) { + getStringValue (obj, result); + } + else if (name == myMethodIdentifiers[ID_GET_BOOLEAN_VALUE]) { + getBooleanValue (obj, result); + } + else + NPN_InitializeVariantAsUndefined (result); +} + +NPObject *myAllocate () +{ + MyObject *newInstance = (MyObject *)malloc (sizeof(MyObject)); + + if (!identifiersInitialized) { + identifiersInitialized = true; + initializeIdentifiers(); + } + + + newInstance->doubleValue = 666.666; + newInstance->intValue = 1234; + newInstance->boolValue = true; + newInstance->stringValue.type = NPVariantType_String; + newInstance->stringValue.value.stringValue.UTF8Length = strlen ("Hello world"); + newInstance->stringValue.value.stringValue.UTF8Characters = strdup ("Hello world"); + + return (NPObject *)newInstance; +} + +void myInvalidate () +{ + // Make sure we've released any remaining references to JavaScript objects. +} + +void myDeallocate (MyObject *obj) +{ + free ((void *)obj); +} + +static NPClass _myFunctionPtrs = { + kNPClassStructVersionCurrent, + (NPAllocateFunctionPtr) myAllocate, + (NPDeallocateFunctionPtr) myDeallocate, + (NPInvalidateFunctionPtr) myInvalidate, + (NPHasMethodFunctionPtr) myHasMethod, + (NPInvokeFunctionPtr) myInvoke, + (NPHasPropertyFunctionPtr) myHasProperty, + (NPGetPropertyFunctionPtr) myGetProperty, + (NPSetPropertyFunctionPtr) mySetProperty, +}; +static NPClass *myFunctionPtrs = &_myFunctionPtrs; + +// -------------------------------------------------------- + +using namespace JSC; +using namespace JSC::Bindings; + +class GlobalImp : public ObjectImp { +public: + virtual String className() const { return "global"; } +}; + +#define BufferSize 200000 +static char code[BufferSize]; + +const char *readJavaScriptFromFile (const char *file) +{ + FILE *f = fopen(file, "r"); + if (!f) { + fprintf(stderr, "Error opening %s.\n", file); + return 0; + } + + int num = fread(code, 1, BufferSize, f); + code[num] = '\0'; + if(num >= BufferSize) + fprintf(stderr, "Warning: File may have been too long.\n"); + + fclose(f); + + return code; +} + +int main(int argc, char **argv) +{ + // expecting a filename + if (argc < 2) { + fprintf(stderr, "You have to specify at least one filename\n"); + return -1; + } + + bool ret = true; + { + JSLock lock; + + // create interpreter w/ global object + Object global(new GlobalImp()); + Interpreter interp; + interp.setGlobalObject(global); + ExecState *exec = interp.globalExec(); + + MyObject *myObject = (MyObject *)NPN_CreateObject (myFunctionPtrs); + + global.put(exec, Identifier::fromString(exec, "myInterface"), Instance::createRuntimeObject(Instance::CLanguage, (void *)myObject)); + + for (int i = 1; i < argc; i++) { + const char *code = readJavaScriptFromFile(argv[i]); + + if (code) { + // run + Completion comp(interp.evaluate(code)); + + if (comp.complType() == Throw) { + Value exVal = comp.value(); + String message = exVal.toWTFString(exec); + auto cstring = msg.ascii(); + const char* msg = cstring.data(); + int lineno = -1; + if (exVal.type() == ObjectType) { + Value lineVal = Object::dynamicCast(exVal).get(exec, Identifier::fromString(exec, "line")); + if (lineVal.type() == NumberType) + lineno = int(lineVal.toNumber(exec)); + } + if (lineno != -1) + fprintf(stderr,"Exception, line %d: %s\n",lineno,msg); + else + fprintf(stderr,"Exception: %s\n",msg); + ret = false; + } + else if (comp.complType() == ReturnValue) { + char *msg = comp.value().toString(interp.globalExec()).ascii(); + fprintf(stderr,"Return value: %s\n",msg); + } + } + } + + NPN_ReleaseObject ((NPObject *)myObject); + + } // end block, so that Interpreter and global get deleted + + return ret ? 0 : 3; +} |