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/SharedTask.h>
36#include <wtf/ThreadSafeRefCounted.h>
37#include <wtf/Vector.h>
38
39namespace JSC {
40
41class CallLinkInfo;
42
43namespace Wasm {
44
45struct Context;
46
47class Plan : public ThreadSafeRefCounted<Plan> {
48public:
49 typedef void CallbackType(Plan&);
50 using CompletionTask = RefPtr<SharedTask<CallbackType>>;
51
52 static CompletionTask dontFinalize() { return createSharedTask<CallbackType>([](Plan&) { }); }
53 Plan(Context*, Ref<ModuleInformation>, CompletionTask&&, CreateEmbedderWrapper&&, ThrowWasmException);
54 Plan(Context*, Ref<ModuleInformation>, CompletionTask&&);
55
56 // Note: This constructor should only be used if you are not actually building a module e.g. validation/function tests
57 JS_EXPORT_PRIVATE Plan(Context*, CompletionTask&&);
58 virtual JS_EXPORT_PRIVATE ~Plan();
59
60 // If you guarantee the ordering here, you can rely on FIFO of the
61 // completion tasks being called.
62 void addCompletionTask(Context*, CompletionTask&&);
63
64 void setMode(MemoryMode mode) { m_mode = mode; }
65 MemoryMode mode() const { return m_mode; }
66
67 const String& errorMessage() const { return m_errorMessage; }
68
69 bool WARN_UNUSED_RETURN failed() const { return !errorMessage().isNull(); }
70 virtual bool hasWork() const = 0;
71 enum CompilationEffort { All, Partial };
72 virtual void work(CompilationEffort = All) = 0;
73 virtual bool multiThreaded() const = 0;
74
75 void waitForCompletion();
76 // Returns true if it cancelled the plan.
77 bool tryRemoveContextAndCancelIfLast(Context&);
78
79protected:
80 void runCompletionTasks(const AbstractLocker&);
81 void fail(const AbstractLocker&, String&& errorMessage);
82
83 virtual bool isComplete() const = 0;
84 virtual void complete(const AbstractLocker&) = 0;
85
86 Ref<ModuleInformation> m_moduleInformation;
87
88 Vector<std::pair<Context*, CompletionTask>, 1> m_completionTasks;
89
90 CreateEmbedderWrapper m_createEmbedderWrapper;
91 ThrowWasmException m_throwWasmException { nullptr };
92
93 String m_errorMessage;
94 MemoryMode m_mode { MemoryMode::BoundsChecking };
95 Lock m_lock;
96 Condition m_completed;
97};
98
99
100} } // namespace JSC::Wasm
101
102#endif // ENABLE(WEBASSEMBLY)
103