Skip to content

Commit f7711a3

Browse files
authored
feat: @W-12499107 add env.installRemapOverrides() for better json support (#418)
1 parent ef26702 commit f7711a3

13 files changed

Lines changed: 419 additions & 203 deletions

File tree

packages/near-membrane-base/src/__tests__/intrinsics.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-nocheck
12
import {
23
assignFilteredGlobalDescriptorsFromPropertyDescriptorMap,
34
createBlueConnector,

packages/near-membrane-base/src/environment.ts

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,37 @@ import {
1010
import type { ProxyTarget } from '@locker/near-membrane-shared/types';
1111
import type {
1212
CallableDefineProperties,
13+
CallableDescriptorCallback,
1314
CallableEvaluate,
1415
CallableGetPropertyValuePointer,
16+
CallableInstallDateProtoToJSON,
17+
CallableInstallJSONStringify,
1518
CallableInstallLazyPropertyDescriptors,
19+
CallableIsTargetLive,
20+
CallableIsTargetRevoked,
1621
CallableLinkPointers,
22+
CallableSerializeTarget,
1723
CallableSetPrototypeOf,
24+
CallableTrackAsFastTarget,
1825
GetSelectedTarget,
1926
GetTransferableValue,
2027
HooksCallback,
2128
Pointer,
2229
VirtualEnvironmentOptions,
23-
CallableTrackAsFastTarget,
24-
CallableDescriptorCallback,
2530
} from './types';
2631

2732
const LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL = Symbol.for(
2833
'@@lockerNearMembraneUndefinedValue'
2934
);
3035

36+
const { prototype: DateProto } = Date;
37+
const { toJSON: DateProtoToJSON } = DateProto;
38+
const WindowJSON = JSON;
39+
40+
const installableDateToJSON = function toJSON(this: Date) {
41+
return ReflectApply(DateProtoToJSON, this, []);
42+
};
43+
3144
export class VirtualEnvironment {
3245
private readonly blueCallableGetPropertyValuePointer: CallableGetPropertyValuePointer;
3346

@@ -39,18 +52,22 @@ export class VirtualEnvironment {
3952

4053
private readonly blueGlobalThisPointer: Pointer;
4154

55+
private readonly redCallableDefineProperties: CallableDefineProperties;
56+
4257
private readonly redCallableEvaluate: CallableEvaluate;
4358

4459
private readonly redCallableGetPropertyValuePointer: CallableGetPropertyValuePointer;
4560

46-
private readonly redCallableLinkPointers: CallableLinkPointers;
61+
private readonly redCallableInstallDateProtoToJSON: CallableInstallDateProtoToJSON;
4762

48-
private readonly redCallableSetPrototypeOf: CallableSetPrototypeOf;
49-
50-
private readonly redCallableDefineProperties: CallableDefineProperties;
63+
private readonly redCallableInstallJSONStringify: CallableInstallJSONStringify;
5164

5265
private readonly redCallableInstallLazyPropertyDescriptors: CallableInstallLazyPropertyDescriptors;
5366

67+
private readonly redCallableLinkPointers: CallableLinkPointers;
68+
69+
private readonly redCallableSetPrototypeOf: CallableSetPrototypeOf;
70+
5471
private readonly redCallableTrackAsFastTarget: CallableTrackAsFastTarget;
5572

5673
private readonly redGlobalThisPointer: Pointer;
@@ -105,22 +122,24 @@ export class VirtualEnvironment {
105122
18: blueCallablePreventExtensions,
106123
19: blueCallableSet,
107124
20: blueCallableSetPrototypeOf,
108-
21: blueCallableDebugInfo,
125+
// 21: blueCallableDebugInfo,
109126
// 22: blueCallableDefineProperties,
110127
23: blueCallableGetLazyPropertyDescriptorStateByTarget,
111128
24: blueCallableGetPropertyValue,
112129
25: blueCallableGetTargetIntegrityTraits,
113130
26: blueCallableGetToStringTagOfTarget,
114-
27: blueCallableInstallErrorPrepareStackTrace,
115-
// 28: blueCallableInstallLazyPropertyDescriptors,
116-
29: blueCallableIsTargetLive,
117-
30: blueCallableIsTargetRevoked,
118-
31: blueCallableSerializeTarget,
119-
32: blueCallableSetLazyPropertyDescriptorStateByTarget,
120-
// 33: blueTrackAsFastTarget,
121-
34: blueCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
122-
35: blueCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
123-
36: blueCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
131+
// 27: blueCallableInstallDateProtoToJSON,
132+
28: blueCallableInstallErrorPrepareStackTrace,
133+
// 29: blueCallableInstallJSONStringify,
134+
// 30: blueCallableInstallLazyPropertyDescriptors,
135+
31: blueCallableIsTargetLive,
136+
// 32: blueCallableIsTargetRevoked,
137+
// 33: blueCallableSerializeTarget,
138+
34: blueCallableSetLazyPropertyDescriptorStateByTarget,
139+
// 35: blueTrackAsFastTarget,
140+
36: blueCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
141+
37: blueCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
142+
38: blueCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
124143
} = blueHooks!;
125144
let redHooks: Parameters<HooksCallback>;
126145
const redConnect = redConnector('red', (...hooks: Parameters<HooksCallback>) => {
@@ -151,19 +170,21 @@ export class VirtualEnvironment {
151170
21: redCallableDebugInfo,
152171
22: redCallableDefineProperties,
153172
23: redCallableGetLazyPropertyDescriptorStateByTarget,
154-
24: redCallableGetPropertyValue,
173+
// 24: redCallableGetPropertyValue,
155174
25: redCallableGetTargetIntegrityTraits,
156175
26: redCallableGetToStringTagOfTarget,
157-
27: redCallableInstallErrorPrepareStackTrace,
158-
28: redCallableInstallLazyPropertyDescriptors,
159-
29: redCallableIsTargetLive,
160-
30: redCallableIsTargetRevoked,
161-
31: redCallableSerializeTarget,
162-
32: redCallableSetLazyPropertyDescriptorStateByTarget,
163-
33: redCallableTrackAsFastTarget,
164-
34: redCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
165-
35: redCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
166-
36: redCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
176+
27: redCallableInstallDateProtoToJSON,
177+
28: redCallableInstallErrorPrepareStackTrace,
178+
29: redCallableInstallJSONStringify,
179+
30: redCallableInstallLazyPropertyDescriptors,
180+
// 31: redCallableIsTargetLive,
181+
32: redCallableIsTargetRevoked,
182+
33: redCallableSerializeTarget,
183+
34: redCallableSetLazyPropertyDescriptorStateByTarget,
184+
35: redCallableTrackAsFastTarget,
185+
36: redCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
186+
37: redCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
187+
38: redCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
167188
} = redHooks!;
168189
blueConnect(
169190
noop, // redGlobalThisPointer,
@@ -190,12 +211,14 @@ export class VirtualEnvironment {
190211
redCallableDebugInfo,
191212
noop, // redCallableDefineProperties,
192213
redCallableGetLazyPropertyDescriptorStateByTarget,
193-
redCallableGetPropertyValue,
214+
noop, // redCallableGetPropertyValue,
194215
redCallableGetTargetIntegrityTraits,
195216
redCallableGetToStringTagOfTarget,
217+
noop, // redCallableInstallDateProtoToJSON,
196218
redCallableInstallErrorPrepareStackTrace,
219+
noop, // redCallableInstallJSONStringify
197220
noop, // redCallableInstallLazyPropertyDescriptors,
198-
redCallableIsTargetLive,
221+
noop as unknown as CallableIsTargetLive, // redCallableIsTargetLive,
199222
redCallableIsTargetRevoked,
200223
redCallableSerializeTarget,
201224
redCallableSetLazyPropertyDescriptorStateByTarget,
@@ -226,17 +249,19 @@ export class VirtualEnvironment {
226249
blueCallablePreventExtensions,
227250
blueCallableSet,
228251
blueCallableSetPrototypeOf,
229-
blueCallableDebugInfo,
252+
noop, // blueCallableDebugInfo
230253
noop, // blueCallableDefineProperties,
231254
blueCallableGetLazyPropertyDescriptorStateByTarget,
232255
blueCallableGetPropertyValue,
233256
blueCallableGetTargetIntegrityTraits,
234257
blueCallableGetToStringTagOfTarget,
235258
blueCallableInstallErrorPrepareStackTrace,
259+
noop, // blueCallableInstallDateProtoToJSON
260+
noop, // blueCallableInstallJSONStringify
236261
noop, // blueCallableInstallLazyPropertyDescriptors,
237262
blueCallableIsTargetLive,
238-
blueCallableIsTargetRevoked,
239-
blueCallableSerializeTarget,
263+
noop as unknown as CallableIsTargetRevoked, // blueCallableIsTargetRevoked,
264+
noop as CallableSerializeTarget, // blueCallableSerializeTarget,,
240265
blueCallableSetLazyPropertyDescriptorStateByTarget,
241266
noop, // blueCallableTrackAsFastTarget,
242267
blueCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
@@ -274,6 +299,8 @@ export class VirtualEnvironment {
274299
}
275300
ReflectApply(redCallableDefineProperties, undefined, args);
276301
};
302+
this.redCallableInstallJSONStringify = (WindowJSONPointer: Pointer) =>
303+
redCallableInstallJSONStringify(WindowJSONPointer);
277304
this.redCallableInstallLazyPropertyDescriptors = (
278305
targetPointer: Pointer,
279306
...ownKeysAndUnforgeableGlobalThisKeys: PropertyKey[]
@@ -286,6 +313,10 @@ export class VirtualEnvironment {
286313
}
287314
ReflectApply(redCallableInstallLazyPropertyDescriptors, undefined, args);
288315
};
316+
this.redCallableInstallDateProtoToJSON = (
317+
DateProtoPointer: Pointer,
318+
DataProtoToJSONPointer: Pointer
319+
) => redCallableInstallDateProtoToJSON(DateProtoPointer, DataProtoToJSONPointer);
289320
this.redCallableTrackAsFastTarget = (targetPointer: Pointer) =>
290321
redCallableTrackAsFastTarget(targetPointer);
291322
}
@@ -303,6 +334,16 @@ export class VirtualEnvironment {
303334
}
304335
}
305336

337+
installRemapOverrides() {
338+
const transferableWindowJSON = this.blueGetTransferableValue(WindowJSON) as Pointer;
339+
this.redCallableTrackAsFastTarget(transferableWindowJSON);
340+
this.redCallableInstallDateProtoToJSON(
341+
this.blueGetTransferableValue(DateProto) as Pointer,
342+
this.blueGetTransferableValue(installableDateToJSON) as Pointer
343+
);
344+
this.redCallableInstallJSONStringify(transferableWindowJSON);
345+
}
346+
306347
lazyRemapProperties(
307348
target: ProxyTarget,
308349
ownKeys: PropertyKey[],

0 commit comments

Comments
 (0)