1/*
2 * Copyright (C) 2013-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "JSMicrotask.h"
28
29#include "CatchScope.h"
30#include "Error.h"
31#include "Exception.h"
32#include "JSCInlines.h"
33#include "JSGlobalObject.h"
34#include "JSObjectInlines.h"
35#include "Microtask.h"
36#include "StrongInlines.h"
37
38namespace JSC {
39
40class JSMicrotask final : public Microtask {
41public:
42 JSMicrotask(VM& vm, JSValue job, JSArray* arguments)
43 {
44 m_job.set(vm, job);
45 m_arguments.set(vm, arguments);
46 }
47
48 JSMicrotask(VM& vm, JSValue job)
49 {
50 m_job.set(vm, job);
51 }
52
53private:
54 void run(ExecState*) override;
55
56 Strong<Unknown> m_job;
57 Strong<JSArray> m_arguments;
58};
59
60Ref<Microtask> createJSMicrotask(VM& vm, JSValue job)
61{
62 return adoptRef(*new JSMicrotask(vm, job));
63}
64
65Ref<Microtask> createJSMicrotask(VM& vm, JSValue job, JSArray* arguments)
66{
67 return adoptRef(*new JSMicrotask(vm, job, arguments));
68}
69
70void JSMicrotask::run(ExecState* exec)
71{
72 VM& vm = exec->vm();
73 auto scope = DECLARE_CATCH_SCOPE(vm);
74
75 CallData handlerCallData;
76 CallType handlerCallType = getCallData(vm, m_job.get(), handlerCallData);
77 ASSERT(handlerCallType != CallType::None);
78
79 MarkedArgumentBuffer handlerArguments;
80 if (m_arguments) {
81 for (unsigned index = 0, length = m_arguments->length(); index < length; ++index) {
82 JSValue arg = m_arguments->JSArray::get(exec, index);
83 CLEAR_AND_RETURN_IF_EXCEPTION(scope, handlerArguments.overflowCheckNotNeeded());
84 handlerArguments.append(arg);
85 }
86 if (UNLIKELY(handlerArguments.hasOverflowed()))
87 return;
88 }
89 profiledCall(exec, ProfilingReason::Microtask, m_job.get(), handlerCallType, handlerCallData, jsUndefined(), handlerArguments);
90 scope.clearException();
91}
92
93} // namespace JSC
94