summaryrefslogtreecommitdiffstats
path: root/js/src/wasm/WasmPI.h
blob: a26740d540b5cfc19ac48dec3967d523f7d82a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 *
 * Copyright 2016 Mozilla Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef wasm_pi_h
#define wasm_pi_h

#include "mozilla/DoublyLinkedList.h"  // for DoublyLinkedListElement

#include "js/TypeDecls.h"
#include "wasm/WasmTypeDef.h"

namespace js {

class WasmStructObject;

namespace wasm {

class SuspenderObject;

static const uint32_t SuspenderObjectDataSlot = 0;

enum SuspenderArgPosition {
  None = -1,
  First = 0,
  Last = 1,
};

enum SuspenderState {
  Initial,
  Moribund,
  Active,
  Suspended,
};

class SuspenderObjectData
    : public mozilla::DoublyLinkedListElement<SuspenderObjectData> {
  void* stackMemory_;

  // Stored main stack FP register.
  void* mainFP_;

  // Stored main stack SP register.
  void* mainSP_;

  // Stored suspendable stack FP register.
  void* suspendableFP_;

  // Stored suspendable stack SP register.
  void* suspendableSP_;

  // Stored suspendable stack exit/bottom frame pointer.
  void* suspendableExitFP_;

  // Stored return address for return to suspendable stack.
  void* suspendedReturnAddress_;

  SuspenderState state_;

#ifdef _WIN64
  // The storage of main stack limits during stack switching.
  // See updateTibFields and restoreTibFields below.
  void* savedStackBase_;
  void* savedStackLimit_;
#endif

 public:
  explicit SuspenderObjectData(void* stackMemory);

  inline SuspenderState state() const { return state_; }
  void setState(SuspenderState state) { state_ = state; }

  inline void* stackMemory() const { return stackMemory_; }
  inline void* mainFP() const { return mainFP_; }
  inline void* mainSP() const { return mainSP_; }
  inline void* suspendableFP() const { return suspendableFP_; }
  inline void* suspendableSP() const { return suspendableSP_; }
  inline void* suspendableExitFP() const { return suspendableExitFP_; }
  inline void* suspendedReturnAddress() const {
    return suspendedReturnAddress_;
  }

#ifdef _WIN64
  void updateTIBStackFields();
  void restoreTIBStackFields();
#endif

  static constexpr size_t offsetOfMainFP() {
    return offsetof(SuspenderObjectData, mainFP_);
  }

  static constexpr size_t offsetOfMainSP() {
    return offsetof(SuspenderObjectData, mainSP_);
  }

  static constexpr size_t offsetOfSuspendableFP() {
    return offsetof(SuspenderObjectData, suspendableFP_);
  }

  static constexpr size_t offsetOfSuspendableSP() {
    return offsetof(SuspenderObjectData, suspendableSP_);
  }

  static constexpr size_t offsetOfSuspendableExitFP() {
    return offsetof(SuspenderObjectData, suspendableExitFP_);
  }

  static constexpr size_t offsetOfSuspendedReturnAddress() {
    return offsetof(SuspenderObjectData, suspendedReturnAddress_);
  }
};

#ifdef ENABLE_WASM_JSPI

bool ParseSuspendingPromisingString(JSContext* cx, JS::HandleValue val,
                                    SuspenderArgPosition& result);

bool CallImportOnMainThread(JSContext* cx, Instance* instance,
                            int32_t funcImportIndex, int32_t argc,
                            uint64_t* argv);

void UnwindStackSwitch(JSContext* cx);

JSFunction* WasmSuspendingFunctionCreate(JSContext* cx, HandleObject func,
                                         wasm::ValTypeVector&& params,
                                         wasm::ValTypeVector&& results,
                                         SuspenderArgPosition argPosition);

JSFunction* WasmSuspendingFunctionCreate(JSContext* cx, HandleObject func,
                                         const FuncType& type);

JSFunction* WasmPromisingFunctionCreate(JSContext* cx, HandleObject func,
                                        wasm::ValTypeVector&& params,
                                        wasm::ValTypeVector&& results,
                                        SuspenderArgPosition argPosition);

SuspenderObject* CurrentSuspender(Instance* instance, int reserved);

SuspenderObject* CheckSuspender(Instance* instance, JSObject* maybeSuspender);

JSObject* GetSuspendingPromiseResult(Instance* instance,
                                     SuspenderObject* suspender);

int32_t SetPromisingPromiseResults(Instance* instance,
                                   SuspenderObject* suspender,
                                   WasmStructObject* results);

void UpdateSuspenderState(Instance* instance, SuspenderObject* suspender,
                          UpdateSuspenderStateAction action);

#endif  // ENABLE_WASM_JSPI

}  // namespace wasm
}  // namespace js

#endif  // wasm_pi_h