APICallbackFunction.h [plain text]
#ifndef APICallbackFunction_h
#define APICallbackFunction_h
#include "APICast.h"
#include "APIShims.h"
#include "Error.h"
#include "JSCallbackConstructor.h"
#include <wtf/Vector.h>
namespace JSC {
struct APICallbackFunction {
template <typename T> static EncodedJSValue JSC_HOST_CALL call(ExecState*);
template <typename T> static EncodedJSValue JSC_HOST_CALL construct(ExecState*);
};
template <typename T>
EncodedJSValue APICallbackFunction::call(ExecState* exec)
{
JSContextRef execRef = toRef(exec);
JSObjectRef functionRef = toRef(exec->callee());
JSObjectRef thisObjRef = toRef(exec->hostThisValue().toThisObject(exec));
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))->functionCallback()(execRef, functionRef, thisObjRef, argumentCount, arguments.data(), &exception);
}
if (exception)
throwError(exec, toJS(exec, exception));
if (!result)
return JSValue::encode(jsUndefined());
return JSValue::encode(toJS(exec, result));
}
template <typename T>
EncodedJSValue JSC_HOST_CALL APICallbackFunction::construct(ExecState* exec)
{
JSObject* constructor = exec->callee();
JSContextRef ctx = toRef(exec);
JSObjectRef constructorRef = toRef(constructor);
JSObjectCallAsConstructorCallback callback = jsCast<T*>(constructor)->constructCallback();
if (callback) {
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;
JSObjectRef result;
{
APICallbackShim callbackShim(exec);
result = callback(ctx, constructorRef, argumentCount, arguments.data(), &exception);
}
if (exception)
throwError(exec, toJS(exec, exception));
if (!result)
return throwVMTypeError(exec);
return JSValue::encode(toJS(result));
}
return JSValue::encode(toJS(JSObjectMake(ctx, jsCast<JSCallbackConstructor*>(constructor)->classRef(), 0)));
}
}
#endif // APICallbackFunction_h