1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*
* Copyright (C) 2018 Igalia S.L.
* Copyright (C) 2006-2018 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.
*/
#include "config.h"
#include "JSCCallbackFunction.h"
#include "APICallbackFunction.h"
#include "APICast.h"
#include "JSCClassPrivate.h"
#include "JSCContextPrivate.h"
#include "JSDestructibleObjectHeapCellType.h"
#include "JSCExceptionPrivate.h"
#include "JSCInlines.h"
#include "JSFunction.h"
#include "JSGlobalObject.h"
#include "JSLock.h"
#include <wtf/text/MakeString.h>
namespace JSC {
static JSValueRef callAsFunction(JSContextRef callerContext, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
return static_cast<JSCCallbackFunction*>(toJS(function))->call(callerContext, thisObject, argumentCount, arguments, exception);
}
static JSObjectRef callAsConstructor(JSContextRef callerContext, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
return static_cast<JSCCallbackFunction*>(toJS(constructor))->construct(callerContext, argumentCount, arguments, exception);
}
const ClassInfo JSCCallbackFunction::s_info = { "CallbackFunction"_s, &InternalFunction::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCCallbackFunction) };
static JSC_DECLARE_HOST_FUNCTION(callJSCCallbackFunction);
static JSC_DECLARE_HOST_FUNCTION(constructJSCCallbackFunction);
JSC_DEFINE_HOST_FUNCTION(callJSCCallbackFunction, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return APICallbackFunction::callImpl<JSCCallbackFunction>(globalObject, callFrame);
}
JSC_DEFINE_HOST_FUNCTION(constructJSCCallbackFunction, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
return APICallbackFunction::constructImpl<JSCCallbackFunction>(globalObject, callFrame);
}
JSCCallbackFunction* JSCCallbackFunction::create(VM& vm, JSGlobalObject* globalObject, const String& name, Type type, JSCClass* jscClass, GRefPtr<GClosure>&& closure, GType returnType, std::optional<Vector<GType>>&& parameters)
{
Structure* structure = globalObject->glibCallbackFunctionStructure();
JSCCallbackFunction* function = new (NotNull, allocateCell<JSCCallbackFunction>(vm)) JSCCallbackFunction(vm, structure, type, jscClass, WTFMove(closure), returnType, WTFMove(parameters));
function->finishCreation(vm, 0, name);
return function;
}
JSCCallbackFunction::JSCCallbackFunction(VM& vm, Structure* structure, Type type, JSCClass* jscClass, GRefPtr<GClosure>&& closure, GType returnType, std::optional<Vector<GType>>&& parameters)
: InternalFunction(vm, structure, callJSCCallbackFunction, type == Type::Constructor ? constructJSCCallbackFunction : nullptr)
, m_functionCallback(callAsFunction)
, m_constructCallback(callAsConstructor)
, m_type(type)
, m_class(jscClass)
, m_closure(WTFMove(closure))
, m_returnType(returnType)
, m_parameters(WTFMove(parameters))
{
ASSERT(type != Type::Constructor || jscClass);
if (G_CLOSURE_NEEDS_MARSHAL(m_closure.get()))
g_closure_set_marshal(m_closure.get(), g_cclosure_marshal_generic);
}
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN // GLib port
JSValueRef JSCCallbackFunction::call(JSContextRef callerContext, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
JSLockHolder locker(toJS(callerContext));
auto context = jscContextGetOrCreate(toGlobalRef(globalObject()));
auto* jsContext = jscContextGetJSContext(context.get());
if (m_type == Type::Constructor) {
*exception = toRef(JSC::createTypeError(toJS(jsContext), "cannot call a class constructor without |new|"_s));
return JSValueMakeUndefined(jsContext);
}
gpointer instance = nullptr;
if (m_type == Type::Method) {
instance = jscContextWrappedObject(context.get(), thisObject);
if (!instance) {
*exception = toRef(JSC::createTypeError(toJS(jsContext), "invalid instance type in method"_s));
return JSValueMakeUndefined(jsContext);
}
}
auto callbackData = jscContextPushCallback(context.get(), toRef(this), thisObject, argumentCount, arguments);
// GClosure always expect to have at least the instance parameter.
bool addInstance = instance || (m_parameters && m_parameters->isEmpty());
auto parameterCount = m_parameters ? m_parameters->size() : 1;
if (addInstance)
parameterCount++;
auto* values = static_cast<GValue*>(g_alloca(sizeof(GValue) * parameterCount));
memset(values, 0, sizeof(GValue) * parameterCount);
size_t firstParameter = 0;
if (addInstance) {
g_value_init(&values[0], G_TYPE_POINTER);
g_value_set_pointer(&values[0], instance);
firstParameter = 1;
}
if (m_parameters) {
for (size_t i = firstParameter; i < parameterCount && !*exception; ++i) {
auto argumentIndex = i - firstParameter;
jscContextJSValueToGValue(context.get(), argumentIndex < argumentCount ? arguments[argumentIndex] : JSValueMakeUndefined(jsContext),
m_parameters.value()[argumentIndex], &values[i], exception);
}
} else {
auto* parameters = g_ptr_array_new_full(argumentCount, g_object_unref);
for (size_t i = 0; i < argumentCount; ++i)
g_ptr_array_add(parameters, jscContextGetOrCreateValue(context.get(), arguments[i]).leakRef());
g_value_init(&values[firstParameter], G_TYPE_PTR_ARRAY);
g_value_take_boxed(&values[firstParameter], parameters);
}
GValue returnValue = G_VALUE_INIT;
if (m_returnType != G_TYPE_NONE)
g_value_init(&returnValue, m_returnType);
if (!*exception)
g_closure_invoke(m_closure.get(), m_returnType != G_TYPE_NONE ? &returnValue : nullptr, parameterCount, values, nullptr);
for (size_t i = 0; i < parameterCount; ++i)
g_value_unset(&values[i]);
if (auto* jscException = jsc_context_get_exception(context.get()))
*exception = jscExceptionGetJSValue(jscException);
jscContextPopCallback(context.get(), WTFMove(callbackData));
if (m_returnType == G_TYPE_NONE)
return JSValueMakeUndefined(jsContext);
auto* retval = *exception ? JSValueMakeUndefined(jsContext) : jscContextGValueToJSValue(context.get(), &returnValue, exception);
g_value_unset(&returnValue);
return retval;
}
JSObjectRef JSCCallbackFunction::construct(JSContextRef callerContext, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
{
JSLockHolder locker(toJS(callerContext));
auto context = jscContextGetOrCreate(toGlobalRef(globalObject()));
auto* jsContext = jscContextGetJSContext(context.get());
if (m_returnType == G_TYPE_NONE) {
*exception = toRef(JSC::createTypeError(toJS(jsContext), "constructors cannot be void"_s));
return nullptr;
}
auto callbackData = jscContextPushCallback(context.get(), toRef(this), nullptr, argumentCount, arguments);
GValue returnValue = G_VALUE_INIT;
g_value_init(&returnValue, m_returnType);
if (m_parameters && m_parameters->isEmpty()) {
// GClosure always expect to have at least the instance parameter.
GValue dummyValue = G_VALUE_INIT;
g_value_init(&dummyValue, G_TYPE_POINTER);
g_closure_invoke(m_closure.get(), &returnValue, 1, &dummyValue, nullptr);
g_value_unset(&dummyValue);
} else {
auto parameterCount = m_parameters ? m_parameters->size() : 1;
auto* values = static_cast<GValue*>(g_alloca(sizeof(GValue) * parameterCount));
memset(values, 0, sizeof(GValue) * parameterCount);
if (m_parameters) {
for (size_t i = 0; i < parameterCount && !*exception; ++i)
jscContextJSValueToGValue(context.get(), i < argumentCount ? arguments[i] : JSValueMakeUndefined(jsContext), m_parameters.value()[i], &values[i], exception);
} else {
auto* parameters = g_ptr_array_new_full(argumentCount, g_object_unref);
for (size_t i = 0; i < argumentCount; ++i)
g_ptr_array_add(parameters, jscContextGetOrCreateValue(context.get(), arguments[i]).leakRef());
g_value_init(&values[0], G_TYPE_PTR_ARRAY);
g_value_take_boxed(&values[0], parameters);
}
if (!*exception)
g_closure_invoke(m_closure.get(), &returnValue, parameterCount, values, nullptr);
for (size_t i = 0; i < parameterCount; ++i)
g_value_unset(&values[i]);
}
if (auto* jscException = jsc_context_get_exception(context.get()))
*exception = jscExceptionGetJSValue(jscException);
jscContextPopCallback(context.get(), WTFMove(callbackData));
if (*exception) {
g_value_unset(&returnValue);
return nullptr;
}
switch (g_type_fundamental(G_VALUE_TYPE(&returnValue))) {
case G_TYPE_POINTER:
case G_TYPE_BOXED:
case G_TYPE_OBJECT:
if (auto* ptr = returnValue.data[0].v_pointer)
return toRef(jscClassGetOrCreateJSWrapper(m_class.get(), context.get(), ptr));
*exception = toRef(JSC::createTypeError(toJS(jsContext), "constructor returned null"_s));
break;
default:
*exception = toRef(JSC::createTypeError(toJS(jsContext), makeString("invalid type "_s, unsafeSpan(g_type_name(G_VALUE_TYPE(&returnValue))), " returned by constructor"_s)));
break;
}
g_value_unset(&returnValue);
return nullptr;
}
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
void JSCCallbackFunction::destroy(JSCell* cell)
{
static_cast<JSCCallbackFunction*>(cell)->JSCCallbackFunction::~JSCCallbackFunction();
}
} // namespace JSC
|