1/*
2 * Copyright (C) 2016-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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#if ENABLE(WEBASSEMBLY)
29
30#include "CompilationResult.h"
31#include "WasmB3IRGenerator.h"
32#include "WasmEmbedder.h"
33#include "WasmModuleInformation.h"
34#include <wtf/Bag.h>
35#include <wtf/CrossThreadCopier.h>
36#include <wtf/SharedTask.h>
37#include <wtf/ThreadSafeRefCounted.h>
38#include <wtf/Vector.h>
39
40namespace JSC {
41
42class CallLinkInfo;
43
44namespace Wasm {
45
46struct Context;
47
48class Plan : public ThreadSafeRefCounted<Plan> {
49public:
50 typedef void CallbackType(Plan&);
51 using CompletionTask = RefPtr<SharedTask<CallbackType>>;
52
53 static CompletionTask dontFinalize() { return createSharedTask<CallbackType>([](Plan&) { }); }
54 Plan(Context*, Ref<ModuleInformation>, CompletionTask&&, CreateEmbedderWrapper&&, ThrowWasmException);
55 Plan(Context*, Ref<ModuleInformation>, CompletionTask&&);
56
57 // Note: This constructor should only be used if you are not actually building a module e.g. validation/function tests
58 JS_EXPORT_PRIVATE Plan(Context*, CompletionTask&&);
59 virtual JS_EXPORT_PRIVATE ~Plan();
60
61 // If you guarantee the ordering here, you can rely on FIFO of the
62 // completion tasks being called.
63 void addCompletionTask(Context*, CompletionTask&&);
64
65 void setMode(MemoryMode mode) { m_mode = mode; }
66 MemoryMode mode() const { return m_mode; }
67
68 String errorMessage() const { return crossThreadCopy(m_errorMessage); }
69
70 bool WARN_UNUSED_RETURN failed() const { return !m_errorMessage.isNull(); }
71 virtual bool hasWork() const = 0;
72 enum CompilationEffort { All, Partial };
73 virtual void work(CompilationEffort = All) = 0;
74 virtual bool multiThreaded() const = 0;
75
76 void waitForCompletion();
77 // Returns true if it cancelled the plan.
78 bool tryRemoveContextAndCancelIfLast(Context&);
79
80protected:
81 void runCompletionTasks(const AbstractLocker&);
82 void fail(const AbstractLocker&, String&& errorMessage);
83
84 virtual bool isComplete() const = 0;
85 virtual void complete(const AbstractLocker&) = 0;
86
87 Ref<ModuleInformation> m_moduleInformation;
88
89 Vector<std::pair<Context*, CompletionTask>, 1> m_completionTasks;
90
91 CreateEmbedderWrapper m_createEmbedderWrapper;
92 ThrowWasmException m_throwWasmException { nullptr };
93
94 String m_errorMessage;
95 MemoryMode m_mode { MemoryMode::BoundsChecking };
96 Lock m_lock;
97 Condition m_completed;
98};
99
100
101} } // namespace JSC::Wasm
102
103#endif // ENABLE(WEBASSEMBLY)
104