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 "Debugger.h"
31#include "Error.h"
32#include "Exception.h"
33#include "JSCInlines.h"
34#include "JSGlobalObject.h"
35#include "JSObjectInlines.h"
36#include "Microtask.h"
37#include "StrongInlines.h"
38
39namespace JSC {
40
41class JSMicrotask final : public Microtask {
42public:
43 static constexpr unsigned maxArguments = 3;
44 JSMicrotask(VM& vm, JSValue job, JSValue argument0, JSValue argument1, JSValue argument2)
45 {
46 m_job.set(vm, job);
47 m_arguments[0].set(vm, argument0);
48 m_arguments[1].set(vm, argument1);
49 m_arguments[2].set(vm, argument2);
50 }
51
52 JSMicrotask(VM& vm, JSValue job)
53 {
54 m_job.set(vm, job);
55 }
56
57private:
58 void run(JSGlobalObject*) override;
59
60 Strong<Unknown> m_job;
61 Strong<Unknown> m_arguments[maxArguments];
62};
63
64Ref<Microtask> createJSMicrotask(VM& vm, JSValue job)
65{
66 return adoptRef(*new JSMicrotask(vm, job));
67}
68
69Ref<Microtask> createJSMicrotask(VM& vm, JSValue job, JSValue argument0, JSValue argument1, JSValue argument2)
70{
71 return adoptRef(*new JSMicrotask(vm, job, argument0, argument1, argument2));
72}
73
74void JSMicrotask::run(JSGlobalObject* globalObject)
75{
76 VM& vm = globalObject->vm();
77 auto scope = DECLARE_CATCH_SCOPE(vm);
78
79 CallData handlerCallData;
80 CallType handlerCallType = getCallData(vm, m_job.get(), handlerCallData);
81 ASSERT(handlerCallType != CallType::None);
82
83 MarkedArgumentBuffer handlerArguments;
84 for (unsigned index = 0; index < maxArguments; ++index) {
85 JSValue arg = m_arguments[index].get();
86 if (!arg)
87 break;
88 handlerArguments.append(arg);
89 }
90 if (UNLIKELY(handlerArguments.hasOverflowed()))
91 return;
92
93 if (UNLIKELY(globalObject->hasDebugger()))
94 globalObject->debugger()->willRunMicrotask();
95
96 profiledCall(globalObject, ProfilingReason::Microtask, m_job.get(), handlerCallType, handlerCallData, jsUndefined(), handlerArguments);
97 scope.clearException();
98}
99
100} // namespace JSC
101