summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/loader/ModuleEnvironmentProxy.cpp
blob: e70fcb38656c4dd3643bd9fc3bc260ec5923a643 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "ModuleEnvironmentProxy.h"

#include "mozilla/Assertions.h"  // MOZ_ASSERT
#include "mozilla/Maybe.h"       // mozilla::Maybe

#include <stddef.h>  // size_t

#include "js/Class.h"        // JS::ObjectOpResult
#include "js/ErrorReport.h"  // JS_ReportOutOfMemory
#include "js/GCVector.h"     // JS::RootedVector
#include "js/Id.h"           // JS::PropertyKey
#include "js/PropertyAndElement.h"  // JS::IdVector, JS_HasPropertyById, JS_GetPropertyById, JS_Enumerate
#include "js/PropertyDescriptor.h"  // JS::PropertyDescriptor, JS_GetOwnPropertyDescriptorById
#include "js/Proxy.h"  // js::ProxyOptions, js::NewProxyObject, js::GetProxyPrivate
#include "js/RootingAPI.h"  // JS::Rooted, JS::Handle, JS::MutableHandle
#include "js/TypeDecls.h"   // JSContext, JSObject, JS::MutableHandleVector
#include "js/Value.h"       // JS::Value
#include "js/friend/ErrorMessages.h"  // JSMSG_*
#include "js/String.h"
#include "js/Modules.h"

namespace mozilla {
namespace loader {

struct ModuleEnvironmentProxyHandler : public js::BaseProxyHandler {
  ModuleEnvironmentProxyHandler() : js::BaseProxyHandler(&gFamily, false) {}

  bool defineProperty(JSContext* aCx, JS::Handle<JSObject*> aProxy,
                      JS::Handle<JS::PropertyKey> aId,
                      JS::Handle<JS::PropertyDescriptor> aDesc,
                      JS::ObjectOpResult& aResult) const override {
    return aResult.fail(JSMSG_CANT_DEFINE_PROP_OBJECT_NOT_EXTENSIBLE);
  }

  bool getPrototype(JSContext* aCx, JS::Handle<JSObject*> aProxy,
                    JS::MutableHandle<JSObject*> aProtop) const override {
    aProtop.set(nullptr);
    return true;
  }

  bool setPrototype(JSContext* aCx, JS::Handle<JSObject*> aProxy,
                    JS::Handle<JSObject*> aProto,
                    JS::ObjectOpResult& aResult) const override {
    if (!aProto) {
      return aResult.succeed();
    }
    return aResult.failCantSetProto();
  }

  bool getPrototypeIfOrdinary(
      JSContext* aCx, JS::Handle<JSObject*> aProxy, bool* aIsOrdinary,
      JS::MutableHandle<JSObject*> aProtop) const override {
    *aIsOrdinary = false;
    return true;
  }

  bool setImmutablePrototype(JSContext* aCx, JS::Handle<JSObject*> aProxy,
                             bool* aSucceeded) const override {
    *aSucceeded = true;
    return true;
  }

  bool preventExtensions(JSContext* aCx, JS::Handle<JSObject*> aProxy,
                         JS::ObjectOpResult& aResult) const override {
    aResult.succeed();
    return true;
  }

  bool isExtensible(JSContext* aCx, JS::Handle<JSObject*> aProxy,
                    bool* aExtensible) const override {
    *aExtensible = false;
    return true;
  }

  bool set(JSContext* aCx, JS::Handle<JSObject*> aProxy,
           JS::Handle<JS::PropertyKey> aId, JS::Handle<JS::Value> aValue,
           JS::Handle<JS::Value> aReceiver,
           JS::ObjectOpResult& aResult) const override {
    return aResult.failReadOnly();
  }

  bool delete_(JSContext* aCx, JS::Handle<JSObject*> aProxy,
               JS::Handle<JS::PropertyKey> aId,
               JS::ObjectOpResult& aResult) const override {
    return aResult.failCantDelete();
  }

  bool getOwnPropertyDescriptor(
      JSContext* aCx, JS::Handle<JSObject*> aProxy,
      JS::Handle<JS::PropertyKey> aId,
      JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> aDesc)
      const override;
  bool has(JSContext* aCx, JS::Handle<JSObject*> aProxy,
           JS::Handle<JS::PropertyKey> aId, bool* aBp) const override;
  bool get(JSContext* aCx, JS::Handle<JSObject*> aProxy,
           JS::Handle<JS::Value> receiver, JS::Handle<JS::PropertyKey> aId,
           JS::MutableHandle<JS::Value> aVp) const override;
  bool ownPropertyKeys(
      JSContext* aCx, JS::Handle<JSObject*> aProxy,
      JS::MutableHandleVector<JS::PropertyKey> aProps) const override;

 private:
  static JSObject* getEnvironment(JS::Handle<JSObject*> aProxy) {
    return &js::GetProxyPrivate(aProxy).toObject();
  }

  static bool equalsNamespace(JSContext* aCx, JS::Handle<JS::PropertyKey> aId,
                              bool* aMatch) {
    if (!aId.isString()) {
      *aMatch = false;
      return true;
    }
    return JS_StringEqualsLiteral(aCx, aId.toString(), "*namespace*", aMatch);
  }

 public:
  static const char gFamily;
  static const ModuleEnvironmentProxyHandler gHandler;
};

const ModuleEnvironmentProxyHandler ModuleEnvironmentProxyHandler::gHandler;
const char ModuleEnvironmentProxyHandler::gFamily = 0;

bool ModuleEnvironmentProxyHandler::getOwnPropertyDescriptor(
    JSContext* aCx, JS::Handle<JSObject*> aProxy,
    JS::Handle<JS::PropertyKey> aId,
    JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> aDesc) const {
  bool isNamespace;
  if (!equalsNamespace(aCx, aId, &isNamespace)) {
    return false;
  }
  if (isNamespace) {
    aDesc.reset();
    return true;
  }

  JS::Rooted<JSObject*> envObj(aCx, getEnvironment(aProxy));
  if (!JS_GetOwnPropertyDescriptorById(aCx, envObj, aId, aDesc)) {
    return false;
  }

  if (aDesc.get().isNothing()) {
    return true;
  }

  JS::PropertyDescriptor& desc = *aDesc.get();

  desc.setConfigurable(false);
  desc.setWritable(false);
  desc.setEnumerable(true);

  return true;
}

bool ModuleEnvironmentProxyHandler::has(JSContext* aCx,
                                        JS::Handle<JSObject*> aProxy,
                                        JS::Handle<JS::PropertyKey> aId,
                                        bool* aBp) const {
  bool isNamespace;
  if (!equalsNamespace(aCx, aId, &isNamespace)) {
    return false;
  }
  if (isNamespace) {
    *aBp = false;
    return true;
  }

  JS::Rooted<JSObject*> envObj(aCx, getEnvironment(aProxy));
  return JS_HasOwnPropertyById(aCx, envObj, aId, aBp);
}

bool ModuleEnvironmentProxyHandler::get(
    JSContext* aCx, JS::Handle<JSObject*> aProxy,
    JS::Handle<JS::Value> aReceiver, JS::Handle<JS::PropertyKey> aId,
    JS::MutableHandle<JS::Value> aVp) const {
  bool isNamespace;
  if (!equalsNamespace(aCx, aId, &isNamespace)) {
    return false;
  }
  if (isNamespace) {
    aVp.setUndefined();
    return true;
  }

  JS::Rooted<JSObject*> envObj(aCx, getEnvironment(aProxy));
  return JS_GetPropertyById(aCx, envObj, aId, aVp);
}

bool ModuleEnvironmentProxyHandler::ownPropertyKeys(
    JSContext* aCx, JS::Handle<JSObject*> aProxy,
    JS::MutableHandleVector<JS::PropertyKey> aProps) const {
  JS::Rooted<JSObject*> envObj(aCx, getEnvironment(aProxy));
  JS::Rooted<JS::IdVector> ids(aCx, JS::IdVector(aCx));
  if (!JS_Enumerate(aCx, envObj, &ids)) {
    return false;
  }

  for (size_t i = 0; i < ids.length(); i++) {
    bool isNamespace;
    if (!equalsNamespace(aCx, ids[i], &isNamespace)) {
      return false;
    }
    if (isNamespace) {
      continue;
    }
    if (!aProps.append(ids[i])) {
      JS_ReportOutOfMemory(aCx);
      return false;
    }
  }

  return true;
}

JSObject* CreateModuleEnvironmentProxy(JSContext* aCx,
                                       JS::Handle<JSObject*> aModuleObj) {
  js::ProxyOptions options;
  options.setLazyProto(true);

  JS::Rooted<JSObject*> envObj(aCx, JS::GetModuleEnvironment(aCx, aModuleObj));
  if (!envObj) {
    return nullptr;
  }

  JS::Rooted<JS::Value> envVal(aCx, JS::ObjectValue(*envObj));
  return NewProxyObject(aCx, &ModuleEnvironmentProxyHandler::gHandler, envVal,
                        nullptr, options);
}

}  // namespace loader
}  // namespace mozilla