|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Igalia S.L. |
| 3 | + * Copyright (C) 2006, 2008, 2016 Apple Inc. All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * 1. Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in the |
| 12 | + * documentation and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 15 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "config.h" |
| 28 | +#include "JSCCallbackFunction.h" |
| 29 | + |
| 30 | +#include "APICallbackFunction.h" |
| 31 | +#include "APICast.h" |
| 32 | +#include "JSCClassPrivate.h" |
| 33 | +#include "JSCContextPrivate.h" |
| 34 | +#include "JSCExceptionPrivate.h" |
| 35 | +#include "JSCInlines.h" |
| 36 | +#include "JSFunction.h" |
| 37 | +#include "JSGlobalObject.h" |
| 38 | +#include "JSLock.h" |
| 39 | + |
| 40 | +namespace JSC { |
| 41 | + |
| 42 | +static JSValueRef callAsFunction(JSContextRef callerContext, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 43 | +{ |
| 44 | + return static_cast<JSCCallbackFunction*>(toJS(function))->call(callerContext, thisObject, argumentCount, arguments, exception); |
| 45 | +} |
| 46 | + |
| 47 | +static JSObjectRef callAsConstructor(JSContextRef callerContext, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 48 | +{ |
| 49 | + return static_cast<JSCCallbackFunction*>(toJS(constructor))->construct(callerContext, argumentCount, arguments, exception); |
| 50 | +} |
| 51 | + |
| 52 | +const ClassInfo JSCCallbackFunction::s_info = { "CallbackFunction", &InternalFunction::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCCallbackFunction) }; |
| 53 | + |
| 54 | +JSCCallbackFunction* JSCCallbackFunction::create(VM& vm, JSGlobalObject* globalObject, const String& name, Type type, JSCClass* jscClass, GRefPtr<GClosure>&& closure, GType returnType, Vector<GType>&& parameters) |
| 55 | +{ |
| 56 | + Structure* structure = globalObject->glibCallbackFunctionStructure(); |
| 57 | + JSCCallbackFunction* function = new (NotNull, allocateCell<JSCCallbackFunction>(vm.heap)) JSCCallbackFunction(vm, structure, type, jscClass, WTFMove(closure), returnType, WTFMove(parameters)); |
| 58 | + function->finishCreation(vm, name); |
| 59 | + return function; |
| 60 | +} |
| 61 | + |
| 62 | +JSCCallbackFunction::JSCCallbackFunction(VM& vm, Structure* structure, Type type, JSCClass* jscClass, GRefPtr<GClosure>&& closure, GType returnType, Vector<GType>&& parameters) |
| 63 | + : InternalFunction(vm, structure, APICallbackFunction::call<JSCCallbackFunction>, type == Type::Constructor ? APICallbackFunction::construct<JSCCallbackFunction> : nullptr) |
| 64 | + , m_functionCallback(callAsFunction) |
| 65 | + , m_constructCallback(callAsConstructor) |
| 66 | + , m_type(type) |
| 67 | + , m_class(jscClass) |
| 68 | + , m_closure(WTFMove(closure)) |
| 69 | + , m_returnType(returnType) |
| 70 | + , m_parameters(WTFMove(parameters)) |
| 71 | +{ |
| 72 | + RELEASE_ASSERT(type != Type::Constructor || jscClass); |
| 73 | + if (G_CLOSURE_NEEDS_MARSHAL(m_closure.get())) |
| 74 | + g_closure_set_marshal(m_closure.get(), g_cclosure_marshal_generic); |
| 75 | +} |
| 76 | + |
| 77 | +JSValueRef JSCCallbackFunction::call(JSContextRef callerContext, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 78 | +{ |
| 79 | + JSLockHolder locker(toJS(callerContext)); |
| 80 | + auto context = jscContextGetOrCreate(toGlobalRef(globalObject()->globalExec())); |
| 81 | + auto* jsContext = jscContextGetJSContext(context.get()); |
| 82 | + |
| 83 | + if (m_type == Type::Constructor) { |
| 84 | + *exception = toRef(JSC::createTypeError(toJS(jsContext), ASCIILiteral("cannot call a class constructor without |new|"))); |
| 85 | + return JSValueMakeUndefined(jsContext); |
| 86 | + } |
| 87 | + |
| 88 | + gpointer instance = nullptr; |
| 89 | + if (m_type == Type::Method) { |
| 90 | + instance = jscContextWrappedObject(context.get(), thisObject); |
| 91 | + if (!instance) { |
| 92 | + *exception = toRef(JSC::createTypeError(toJS(jsContext), ASCIILiteral("invalid instance type in method"))); |
| 93 | + return JSValueMakeUndefined(jsContext); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + auto callbackData = jscContextPushCallback(context.get(), toRef(this), thisObject, argumentCount, arguments); |
| 98 | + |
| 99 | + // GClosure always expect to have at least the instance parameter. |
| 100 | + bool addInstance = instance || m_parameters.isEmpty(); |
| 101 | + |
| 102 | + auto parameterCount = std::min(m_parameters.size(), argumentCount); |
| 103 | + if (addInstance) |
| 104 | + parameterCount++; |
| 105 | + auto* values = static_cast<GValue*>(g_alloca(sizeof(GValue) * parameterCount)); |
| 106 | + memset(values, 0, sizeof(GValue) * parameterCount); |
| 107 | + |
| 108 | + size_t firstParameter = 0; |
| 109 | + if (addInstance) { |
| 110 | + g_value_init(&values[0], G_TYPE_POINTER); |
| 111 | + g_value_set_pointer(&values[0], instance); |
| 112 | + firstParameter = 1; |
| 113 | + } |
| 114 | + for (size_t i = firstParameter; i < parameterCount && !*exception; ++i) |
| 115 | + jscContextJSValueToGValue(context.get(), arguments[i - firstParameter], m_parameters[i - firstParameter], &values[i], exception); |
| 116 | + |
| 117 | + GValue returnValue = G_VALUE_INIT; |
| 118 | + if (m_returnType != G_TYPE_NONE) |
| 119 | + g_value_init(&returnValue, m_returnType); |
| 120 | + |
| 121 | + if (!*exception) |
| 122 | + g_closure_invoke(m_closure.get(), m_returnType != G_TYPE_NONE ? &returnValue : nullptr, parameterCount, values, nullptr); |
| 123 | + |
| 124 | + for (size_t i = 0; i < parameterCount; ++i) |
| 125 | + g_value_unset(&values[i]); |
| 126 | + |
| 127 | + if (auto* jscException = jsc_context_get_exception(context.get())) |
| 128 | + *exception = jscExceptionGetJSValue(jscException); |
| 129 | + |
| 130 | + jscContextPopCallback(context.get(), WTFMove(callbackData)); |
| 131 | + |
| 132 | + if (m_returnType == G_TYPE_NONE) |
| 133 | + return JSValueMakeUndefined(jsContext); |
| 134 | + |
| 135 | + auto* retval = *exception ? JSValueMakeUndefined(jsContext) : jscContextGValueToJSValue(context.get(), &returnValue, exception); |
| 136 | + g_value_unset(&returnValue); |
| 137 | + return retval; |
| 138 | +} |
| 139 | + |
| 140 | +JSObjectRef JSCCallbackFunction::construct(JSContextRef callerContext, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) |
| 141 | +{ |
| 142 | + JSLockHolder locker(toJS(callerContext)); |
| 143 | + auto context = jscContextGetOrCreate(toGlobalRef(globalObject()->globalExec())); |
| 144 | + auto* jsContext = jscContextGetJSContext(context.get()); |
| 145 | + |
| 146 | + if (m_returnType == G_TYPE_NONE) { |
| 147 | + *exception = toRef(JSC::createTypeError(toJS(jsContext), ASCIILiteral("constructors cannot be void"))); |
| 148 | + return nullptr; |
| 149 | + } |
| 150 | + |
| 151 | + auto callbackData = jscContextPushCallback(context.get(), toRef(this), nullptr, argumentCount, arguments); |
| 152 | + |
| 153 | + GValue returnValue = G_VALUE_INIT; |
| 154 | + g_value_init(&returnValue, m_returnType); |
| 155 | + |
| 156 | + if (m_parameters.isEmpty()) { |
| 157 | + // GClosure always expect to have at least the instance parameter. |
| 158 | + GValue dummyValue = G_VALUE_INIT; |
| 159 | + g_value_init(&dummyValue, G_TYPE_POINTER); |
| 160 | + g_closure_invoke(m_closure.get(), &returnValue, 1, &dummyValue, nullptr); |
| 161 | + g_value_unset(&dummyValue); |
| 162 | + } else { |
| 163 | + auto parameterCount = std::min(m_parameters.size(), argumentCount); |
| 164 | + auto* values = static_cast<GValue*>(g_alloca(sizeof(GValue) * parameterCount)); |
| 165 | + memset(values, 0, sizeof(GValue) * parameterCount); |
| 166 | + |
| 167 | + for (size_t i = 0; i < parameterCount && !*exception; ++i) |
| 168 | + jscContextJSValueToGValue(context.get(), arguments[i], m_parameters[i], &values[i], exception); |
| 169 | + |
| 170 | + if (!*exception) |
| 171 | + g_closure_invoke(m_closure.get(), &returnValue, parameterCount, values, nullptr); |
| 172 | + |
| 173 | + for (size_t i = 0; i < parameterCount; ++i) |
| 174 | + g_value_unset(&values[i]); |
| 175 | + } |
| 176 | + |
| 177 | + if (auto* jscException = jsc_context_get_exception(context.get())) |
| 178 | + *exception = jscExceptionGetJSValue(jscException); |
| 179 | + |
| 180 | + jscContextPopCallback(context.get(), WTFMove(callbackData)); |
| 181 | + |
| 182 | + if (*exception) { |
| 183 | + g_value_unset(&returnValue); |
| 184 | + return nullptr; |
| 185 | + } |
| 186 | + |
| 187 | + switch (g_type_fundamental(G_VALUE_TYPE(&returnValue))) { |
| 188 | + case G_TYPE_POINTER: |
| 189 | + case G_TYPE_OBJECT: |
| 190 | + if (auto* ptr = returnValue.data[0].v_pointer) { |
| 191 | + auto* retval = jscClassGetOrCreateJSWrapper(m_class.get(), ptr); |
| 192 | + g_value_unset(&returnValue); |
| 193 | + return toRef(retval); |
| 194 | + } |
| 195 | + *exception = toRef(JSC::createTypeError(toJS(jsContext), ASCIILiteral("constructor returned null"))); |
| 196 | + break; |
| 197 | + default: |
| 198 | + *exception = toRef(JSC::createTypeError(toJS(jsContext), makeString("invalid type ", g_type_name(G_VALUE_TYPE(&returnValue)), " returned by constructor"))); |
| 199 | + break; |
| 200 | + } |
| 201 | + return nullptr; |
| 202 | +} |
| 203 | + |
| 204 | +void JSCCallbackFunction::destroy(JSCell* cell) |
| 205 | +{ |
| 206 | + static_cast<JSCCallbackFunction*>(cell)->JSCCallbackFunction::~JSCCallbackFunction(); |
| 207 | +} |
| 208 | + |
| 209 | +} // namespace JSC |
0 commit comments