Commit 5109edb6 authored by oliver@apple.com's avatar oliver@apple.com
Browse files

fourthTier: Refactor ObjCCallbackFunction to inherit directly from InternalFunction

https://bugs.webkit.org/show_bug.cgi?id=117595

Reviewed by Geoffrey Garen.

* API/APICallbackFunction.h: Added. New struct that allows JSCallbackFunction and
ObjCCallbackFunction to share their host call() implementation through the magic of
templates.
(JSC::APICallbackFunction::call):
* API/JSCallbackFunction.cpp:
(JSC::JSCallbackFunction::getCallData): Changed to get the template-ized version of
the host function.
* API/JSCallbackFunction.h:
* API/ObjCCallbackFunction.h: Now inherits directly from InternalFunction.
* API/ObjCCallbackFunction.mm:
(JSC::ObjCCallbackFunction::ObjCCallbackFunction):
(JSC::ObjCCallbackFunction::getCallData): Ditto.
* GNUmakefile.list.am: Build files!
* JavaScriptCore.vcxproj/JavaScriptCore.vcxproj:
* JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters:
* JavaScriptCore.xcodeproj/project.pbxproj:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@153233 268f45cc-cd09-0410-ab3c-d52691b4dbfc
parent 3fd94fff
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE 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 INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef APICallbackFunction_h
#define APICallbackFunction_h
#include "APICast.h"
#include "APIShims.h"
#include "Error.h"
#include <wtf/Vector.h>
namespace JSC {
struct APICallbackFunction {
template <typename T> static EncodedJSValue JSC_HOST_CALL call(ExecState*);
};
template <typename T>
EncodedJSValue APICallbackFunction::call(ExecState* exec)
{
JSContextRef execRef = toRef(exec);
JSObjectRef functionRef = toRef(exec->callee());
JSObjectRef thisObjRef = toRef(jsCast<JSObject*>(exec->hostThisValue().toThis(exec, NotStrictMode)));
int argumentCount = static_cast<int>(exec->argumentCount());
Vector<JSValueRef, 16> arguments;
arguments.reserveInitialCapacity(argumentCount);
for (int i = 0; i < argumentCount; i++)
arguments.uncheckedAppend(toRef(exec, exec->argument(i)));
JSValueRef exception = 0;
JSValueRef result;
{
APICallbackShim callbackShim(exec);
result = jsCast<T*>(toJS(functionRef))->m_callback(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception);
}
if (exception)
throwError(exec, toJS(exec, exception));
// result must be a valid JSValue.
if (!result)
return JSValue::encode(jsUndefined());
return JSValue::encode(toJS(exec, result));
}
} // namespace JSC
#endif // APICallbackFunction_h
......@@ -26,8 +26,9 @@
#include "config.h"
#include "JSCallbackFunction.h"
#include "APIShims.h"
#include "APICallbackFunction.h"
#include "APICast.h"
#include "APIShims.h"
#include "CodeBlock.h"
#include "Error.h"
#include "ExceptionHelpers.h"
......@@ -63,37 +64,9 @@ JSCallbackFunction* JSCallbackFunction::create(ExecState* exec, JSGlobalObject*
return function;
}
EncodedJSValue JSCallbackFunction::call(ExecState* exec)
{
JSContextRef execRef = toRef(exec);
JSObjectRef functionRef = toRef(exec->callee());
JSObjectRef thisObjRef = toRef(jsCast<JSObject*>(exec->hostThisValue().toThis(exec, NotStrictMode)));
size_t argumentCount = exec->argumentCount();
Vector<JSValueRef, 16> arguments;
arguments.reserveInitialCapacity(argumentCount);
for (size_t i = 0; i < argumentCount; ++i)
arguments.uncheckedAppend(toRef(exec, exec->argument(i)));
JSValueRef exception = 0;
JSValueRef result;
{
APICallbackShim callbackShim(exec);
result = jsCast<JSCallbackFunction*>(toJS(functionRef))->m_callback(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception);
}
if (exception)
throwError(exec, toJS(exec, exception));
// result must be a valid JSValue.
if (!result)
return JSValue::encode(jsUndefined());
return JSValue::encode(toJS(exec, result));
}
CallType JSCallbackFunction::getCallData(JSCell*, CallData& callData)
{
callData.native.function = call;
callData.native.function = APICallbackFunction::call<JSCallbackFunction>;
return CallTypeHost;
}
......
......@@ -32,10 +32,7 @@
namespace JSC {
class JSCallbackFunction : public InternalFunction {
protected:
JSCallbackFunction(JSGlobalObject*, Structure*, JSObjectCallAsFunctionCallback);
void finishCreation(VM&, const String& name);
friend struct APICallbackFunction;
public:
typedef InternalFunction Base;
......@@ -50,11 +47,11 @@ public:
return Structure::create(vm, globalObject, proto, TypeInfo(ObjectType, StructureFlags), &s_info);
}
protected:
static CallType getCallData(JSCell*, CallData&);
private:
static EncodedJSValue JSC_HOST_CALL call(ExecState*);
JSCallbackFunction(JSGlobalObject*, Structure*, JSObjectCallAsFunctionCallback);
void finishCreation(VM&, const String& name);
static CallType getCallData(JSCell*, CallData&);
JSObjectCallAsFunctionCallback m_callback;
};
......
......@@ -42,9 +42,10 @@ namespace JSC {
class ObjCCallbackFunctionImpl;
class ObjCCallbackFunction : public JSCallbackFunction {
class ObjCCallbackFunction : public InternalFunction {
friend struct APICallbackFunction;
public:
typedef JSCallbackFunction Base;
typedef InternalFunction Base;
static ObjCCallbackFunction* create(ExecState*, JSGlobalObject*, const String& name, PassOwnPtr<ObjCCallbackFunctionImpl>);
static void destroy(JSCell*);
......@@ -63,6 +64,9 @@ protected:
ObjCCallbackFunction(JSGlobalObject*, JSObjectCallAsFunctionCallback, PassOwnPtr<ObjCCallbackFunctionImpl>);
private:
static CallType getCallData(JSCell*, CallData&);
JSObjectCallAsFunctionCallback m_callback;
OwnPtr<ObjCCallbackFunctionImpl> m_impl;
};
......
......@@ -28,6 +28,7 @@
#if JSC_OBJC_API_ENABLED
#import "APICallbackFunction.h"
#import "APICast.h"
#import "APIShims.h"
#import "Error.h"
......@@ -469,7 +470,8 @@ static JSValueRef objCCallbackFunctionCallAsFunction(JSContextRef callerContext,
const JSC::ClassInfo ObjCCallbackFunction::s_info = { "CallbackFunction", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(ObjCCallbackFunction) };
ObjCCallbackFunction::ObjCCallbackFunction(JSC::JSGlobalObject* globalObject, JSObjectCallAsFunctionCallback callback, PassOwnPtr<ObjCCallbackFunctionImpl> impl)
: Base(globalObject, globalObject->objcCallbackFunctionStructure(), callback)
: Base(globalObject, globalObject->objcCallbackFunctionStructure())
, m_callback(callback)
, m_impl(impl)
{
}
......@@ -486,6 +488,12 @@ void ObjCCallbackFunction::destroy(JSCell* cell)
static_cast<ObjCCallbackFunction*>(cell)->ObjCCallbackFunction::~ObjCCallbackFunction();
}
CallType ObjCCallbackFunction::getCallData(JSCell*, CallData& callData)
{
callData.native.function = APICallbackFunction::call<ObjCCallbackFunction>;
return CallTypeHost;
}
JSValueRef ObjCCallbackFunctionImpl::call(JSContext *context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
JSGlobalContextRef contextRef = [context JSGlobalContextRef];
......
2013-06-19 Mark Hahnenberg <mhahnenberg@apple.com>
Refactor ObjCCallbackFunction to inherit directly from InternalFunction
https://bugs.webkit.org/show_bug.cgi?id=117595
Reviewed by Geoffrey Garen.
* API/APICallbackFunction.h: Added. New struct that allows JSCallbackFunction and
ObjCCallbackFunction to share their host call() implementation through the magic of
templates.
(JSC::APICallbackFunction::call):
* API/JSCallbackFunction.cpp:
(JSC::JSCallbackFunction::getCallData): Changed to get the template-ized version of
the host function.
* API/JSCallbackFunction.h:
* API/ObjCCallbackFunction.h: Now inherits directly from InternalFunction.
* API/ObjCCallbackFunction.mm:
(JSC::ObjCCallbackFunction::ObjCCallbackFunction):
(JSC::ObjCCallbackFunction::getCallData): Ditto.
* GNUmakefile.list.am: Build files!
* JavaScriptCore.vcxproj/JavaScriptCore.vcxproj:
* JavaScriptCore.vcxproj/JavaScriptCore.vcxproj.filters:
* JavaScriptCore.xcodeproj/project.pbxproj:
2013-06-19 Michael Saboff <msaboff@apple.com>
fourthTier: Arity fixup should be done while on same stack
......
......@@ -32,6 +32,7 @@ javascriptcore_built_nosources += \
DerivedSources/JavaScriptCore/LLIntAssembly.h
javascriptcore_sources += \
Source/JavaScriptCore/API/APICallbackFunction.h \
Source/JavaScriptCore/API/APICast.h \
Source/JavaScriptCore/API/APIShims.h \
Source/JavaScriptCore/API/JSAPIWrapperObject.h \
......
......@@ -533,6 +533,7 @@
<ClInclude Include="$(ConfigurationBuildDir)\obj32\$(ProjectName)\DerivedSources\StringConstructor.lut.h" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\API\APICallbackFunction.h" />
<ClInclude Include="..\API\APICast.h" />
<ClInclude Include="..\API\JavaScript.h" />
<ClInclude Include="..\API\JavaScriptCore.h" />
......@@ -935,4 +936,4 @@
<ImportGroup Label="ExtensionTargets">
<Import Project="$(VCTargetsPath)\BuildCustomizations\masm.targets" />
</ImportGroup>
</Project>
\ No newline at end of file
</Project>
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="assembler">
......@@ -767,6 +767,9 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\API\APICallbackFunction.h">
<Filter>API</Filter>
</ClInclude>
<ClInclude Include="..\API\APICast.h">
<Filter>API</Filter>
</ClInclude>
......
......@@ -579,6 +579,7 @@
1ACF7377171CA6FB00C9BB1E /* Weak.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 1ACF7376171CA6FB00C9BB1E /* Weak.cpp */; };
2600B5A6152BAAA70091EE5F /* JSStringJoiner.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 2600B5A4152BAAA70091EE5F /* JSStringJoiner.cpp */; };
2600B5A7152BAAA70091EE5F /* JSStringJoiner.h in Headers */ = {isa = PBXBuildFile; fileRef = 2600B5A5152BAAA70091EE5F /* JSStringJoiner.h */; };
2A48D1911772365B00C65A5F /* APICallbackFunction.h in Headers */ = {isa = PBXBuildFile; fileRef = C211B574176A224D000E2A23 /* APICallbackFunction.h */; };
41359CF30FDD89AD00206180 /* DateConversion.h in Headers */ = {isa = PBXBuildFile; fileRef = D21202290AD4310C00ED79B6 /* DateConversion.h */; };
4443AE3316E188D90076F110 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 51F0EB6105C86C6B00E6DF1B /* Foundation.framework */; };
451539B912DC994500EF7AC4 /* Yarr.h in Headers */ = {isa = PBXBuildFile; fileRef = 451539B812DC994500EF7AC4 /* Yarr.h */; settings = {ATTRIBUTES = (Private, ); }; };
......@@ -1947,6 +1948,7 @@
C21122DE15DD9AB300790E3A /* GCThreadSharedData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GCThreadSharedData.cpp; sourceTree = "<group>"; };
C21122DF15DD9AB300790E3A /* GCThreadSharedData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GCThreadSharedData.h; sourceTree = "<group>"; };
C21122E015DD9AB300790E3A /* MarkStackInlines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MarkStackInlines.h; sourceTree = "<group>"; };
C211B574176A224D000E2A23 /* APICallbackFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = APICallbackFunction.h; sourceTree = "<group>"; };
C218D13F1655CFD50062BB81 /* CopyWorkList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CopyWorkList.h; sourceTree = "<group>"; };
C2239D1216262BDD005AC5FD /* CopyVisitor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CopyVisitor.cpp; sourceTree = "<group>"; };
C2239D1316262BDD005AC5FD /* CopyVisitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CopyVisitor.h; sourceTree = "<group>"; };
......@@ -2484,6 +2486,7 @@
1432EBD70A34CAD400717B9F /* API */ = {
isa = PBXGroup;
children = (
C211B574176A224D000E2A23 /* APICallbackFunction.h */,
1482B78A0A4305AB00517CFC /* APICast.h */,
865F408710E7D56300947361 /* APIShims.h */,
1CAA8B4A0D32C39A0041BCFF /* JavaScript.h */,
......@@ -3801,6 +3804,7 @@
0FD2C92416D01EE900C7803F /* StructureInlines.h in Headers */,
C2FE18A416BAEC4000AF3061 /* StructureRareData.h in Headers */,
C20BA92D16BB1C1500B3AEA2 /* StructureRareDataInlines.h in Headers */,
2A48D1911772365B00C65A5F /* APICallbackFunction.h in Headers */,
0F9332A514CA7DDD0085F3C6 /* StructureSet.h in Headers */,
0F766D3915AE4A1F008F363E /* StructureStubClearingWatchpoint.h in Headers */,
BCCF0D080EF0AAB900413C8F /* StructureStubInfo.h in Headers */,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment