|
95 | 95 | this.filename = `${id}.js`;
|
96 | 96 | this.id = id;
|
97 | 97 | this.exports = {};
|
| 98 | + this.reflect = undefined; |
| 99 | + this.exportKeys = undefined; |
98 | 100 | this.loaded = false;
|
99 | 101 | this.loading = false;
|
100 | 102 | }
|
|
193 | 195 | '\n});'
|
194 | 196 | ];
|
195 | 197 |
|
| 198 | + const { isProxy } = internalBinding('types'); |
| 199 | + const { |
| 200 | + apply: ReflectApply, |
| 201 | + has: ReflectHas, |
| 202 | + get: ReflectGet, |
| 203 | + set: ReflectSet, |
| 204 | + defineProperty: ReflectDefineProperty, |
| 205 | + deleteProperty: ReflectDeleteProperty, |
| 206 | + getOwnPropertyDescriptor: ReflectGetOwnPropertyDescriptor, |
| 207 | + } = Reflect; |
| 208 | + const { |
| 209 | + toString: ObjectToString, |
| 210 | + hasOwnProperty: ObjectHasOwnProperty, |
| 211 | + } = Object.prototype; |
| 212 | + let isNative; |
| 213 | + { |
| 214 | + const { toString } = Function.prototype; |
| 215 | + const re = toString.call(toString) |
| 216 | + .replace(/[\\^$.*+?()[\]{}|]/g, '\\$&') |
| 217 | + .replace(/toString|(function ).*?(?=\\\()/g, '$1.*?'); |
| 218 | + const nativeRegExp = new RegExp(`^${re}$`); |
| 219 | + isNative = (fn) => { |
| 220 | + if (typeof fn === 'function' && |
| 221 | + nativeRegExp.test(toString.call(fn))) { |
| 222 | + const { name } = fn; |
| 223 | + if (typeof name !== 'string' || !/^bound /.test(name)) |
| 224 | + return !isProxy(fn); |
| 225 | + } |
| 226 | + return false; |
| 227 | + }; |
| 228 | + } |
| 229 | + |
196 | 230 | NativeModule.prototype.compile = function() {
|
197 | 231 | let source = NativeModule.getSource(this.id);
|
198 | 232 | source = NativeModule.wrap(source);
|
|
208 | 242 | NativeModule.require;
|
209 | 243 | fn(this.exports, requireFn, this, process);
|
210 | 244 |
|
| 245 | + if (config.experimentalModules) { |
| 246 | + this.exportKeys = Object.keys(this.exports); |
| 247 | + |
| 248 | + const update = (property, value) => { |
| 249 | + if (this.reflect !== undefined && this.exportKeys.includes(property)) |
| 250 | + this.reflect.exports[property].set(value); |
| 251 | + }; |
| 252 | + |
| 253 | + const methodWrapMap = new WeakMap(); |
| 254 | + const methodUnwrapMap = new WeakMap(); |
| 255 | + |
| 256 | + const wrap = (target, name, value) => { |
| 257 | + if (typeof value !== 'function' || !isNative(value)) { |
| 258 | + return value; |
| 259 | + } |
| 260 | + |
| 261 | + if (methodWrapMap.has(value)) |
| 262 | + return methodWrapMap.get(value); |
| 263 | + |
| 264 | + const p = new Proxy(value, { |
| 265 | + apply: (t, thisArg, args) => { |
| 266 | + if (thisArg === proxy || (this.reflect !== undefined && |
| 267 | + this.reflect.namespace !== undefined && |
| 268 | + thisArg === this.reflect.namespace)) { |
| 269 | + thisArg = target; |
| 270 | + } |
| 271 | + return ReflectApply(t, thisArg, args); |
| 272 | + }, |
| 273 | + __proto__: null, |
| 274 | + }); |
| 275 | + |
| 276 | + methodWrapMap.set(value, p); |
| 277 | + methodUnwrapMap.set(p, value); |
| 278 | + |
| 279 | + return p; |
| 280 | + }; |
| 281 | + |
| 282 | + const proxy = new Proxy(this.exports, { |
| 283 | + set: (target, prop, value, receiver) => { |
| 284 | + if (typeof value === 'function') |
| 285 | + value = methodUnwrapMap.get(value) || value; |
| 286 | + if (ReflectSet(target, prop, value, receiver)) { |
| 287 | + update(prop, ReflectGet(target, prop, receiver)); |
| 288 | + return true; |
| 289 | + } |
| 290 | + return false; |
| 291 | + }, |
| 292 | + defineProperty: (target, prop, descriptor) => { |
| 293 | + if (ObjectHasOwnProperty.call(descriptor, 'value')) { |
| 294 | + const { value } = descriptor; |
| 295 | + if (typeof value === 'function') |
| 296 | + descriptor.value = methodUnwrapMap.get(value) || value; |
| 297 | + } |
| 298 | + if (ReflectDefineProperty(target, prop, descriptor)) { |
| 299 | + update(prop, ReflectGet(target, prop)); |
| 300 | + return true; |
| 301 | + } |
| 302 | + return false; |
| 303 | + }, |
| 304 | + deleteProperty: (target, prop) => { |
| 305 | + if (ReflectDeleteProperty(target, prop)) { |
| 306 | + update(prop, undefined); |
| 307 | + return true; |
| 308 | + } |
| 309 | + return false; |
| 310 | + }, |
| 311 | + getOwnPropertyDescriptor: (target, prop) => { |
| 312 | + const descriptor = ReflectGetOwnPropertyDescriptor(target, prop); |
| 313 | + if (descriptor && ReflectHas(descriptor, 'value')) |
| 314 | + descriptor.value = wrap(target, prop, descriptor.value); |
| 315 | + return descriptor; |
| 316 | + }, |
| 317 | + get: (target, prop, receiver) => { |
| 318 | + const value = ReflectGet(target, prop, receiver); |
| 319 | + if (prop === Symbol.toStringTag && |
| 320 | + typeof target !== 'function' && |
| 321 | + typeof value !== 'string') { |
| 322 | + const toStringTag = ObjectToString.call(target).slice(8, -1); |
| 323 | + return toStringTag === 'Object' ? value : toStringTag; |
| 324 | + } |
| 325 | + return wrap(target, prop, value); |
| 326 | + }, |
| 327 | + __proto__: null, |
| 328 | + }); |
| 329 | + this.exports = proxy; |
| 330 | + } |
| 331 | + |
211 | 332 | this.loaded = true;
|
212 | 333 | } finally {
|
213 | 334 | this.loading = false;
|
|
0 commit comments