All files / near-membrane-base/src environment.ts

85.42% Statements 82/96
38.64% Branches 17/44
72.22% Functions 13/18
84.27% Lines 75/89

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401                                                            4x                                                           23x                         23x   23x     23x                                                                                             23x   23x 23x                                                                               23x 23x                                                                             23x                                                                             23x 23x 23x 23x 23x     322x 23x 575x 23x   30x 23x 575x 23x       23x       15x 15x 15x 15x 154x   15x   23x       23x 23x 23x 23x 3104x   23x   23x         30x 30x 28x       28x   2x                 23x 23x     23x 23x           23x         322x 322x 322x 575x 575x 575x 575x 575x         15x 15x 15x 15x 15x 15x 15x 22x 22x         22x 22x 22x       22x       22x       22x       22x       22x         15x                                        
import {
    ArrayCtor,
    ArrayProtoPush,
    ErrorCtor,
    noop,
    ObjectAssign,
    ReflectApply,
    ReflectOwnKeys,
    SymbolFor,
} from '@locker/near-membrane-shared';
import type { ProxyTarget } from '@locker/near-membrane-shared';
import type {
    CallableDefineProperties,
    CallableDescriptorCallback,
    CallableEvaluate,
    CallableGetPropertyValuePointer,
    CallableInstallLazyPropertyDescriptors,
    CallableIsTargetLive,
    CallableIsTargetRevoked,
    CallableLinkPointers,
    CallableSerializeTarget,
    CallableSetPrototypeOf,
    CallableTrackAsFastTarget,
    GetSelectedTarget,
    GetTransferableValue,
    HooksCallback,
    Pointer,
    VirtualEnvironmentOptions,
} from './types';
 
const LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL = SymbolFor('@@lockerNearMembraneUndefinedValue');
 
export class VirtualEnvironment {
    private readonly blueCallableGetPropertyValuePointer: CallableGetPropertyValuePointer;
 
    private readonly blueCallableLinkPointers: CallableLinkPointers;
 
    private readonly blueGetSelectedTarget: GetSelectedTarget;
 
    private readonly blueGetTransferableValue: GetTransferableValue;
 
    private readonly blueGlobalThisPointer: Pointer;
 
    private readonly redCallableDefineProperties: CallableDefineProperties;
 
    private readonly redCallableEvaluate: CallableEvaluate;
 
    private readonly redCallableGetPropertyValuePointer: CallableGetPropertyValuePointer;
 
    private readonly redCallableInstallLazyPropertyDescriptors: CallableInstallLazyPropertyDescriptors;
 
    private readonly redCallableLinkPointers: CallableLinkPointers;
 
    private readonly redCallableSetPrototypeOf: CallableSetPrototypeOf;
 
    private readonly redCallableTrackAsFastTarget: CallableTrackAsFastTarget;
 
    private readonly redGlobalThisPointer: Pointer;
 
    constructor(options: VirtualEnvironmentOptions) {
        Iif (options === undefined) {
            throw new ErrorCtor('Missing required VirtualEnvironment options.');
        }
        // prettier-ignore
        const {
            blueConnector,
            redConnector,
            distortionCallback,
            instrumentation,
            liveTargetCallback,
            revokedProxyCallback,
            signSourceCallback,
            // eslint-disable-next-line prefer-object-spread
        } = ObjectAssign({ __proto__: null }, options);
        let blueHooks: Parameters<HooksCallback>;
        const blueConnect = blueConnector(
            'blue',
            (...hooks: Parameters<HooksCallback>) => {
                blueHooks = hooks;
            },
            {
                distortionCallback,
                instrumentation,
                liveTargetCallback,
                revokedProxyCallback,
            }
        );
        const {
            0: blueGlobalThisPointer,
            1: blueGetSelectedTarget,
            2: blueGetTransferableValue,
            3: blueCallableGetPropertyValuePointer,
            // 4: blueCallableEvaluate,
            5: blueCallableLinkPointers,
            6: blueCallablePushErrorTarget,
            7: blueCallablePushTarget,
            8: blueCallableApply,
            9: blueCallableConstruct,
            10: blueCallableDefineProperty,
            11: blueCallableDeleteProperty,
            12: blueCallableGet,
            13: blueCallableGetOwnPropertyDescriptor,
            14: blueCallableGetPrototypeOf,
            15: blueCallableHas,
            16: blueCallableIsExtensible,
            17: blueCallableOwnKeys,
            18: blueCallablePreventExtensions,
            19: blueCallableSet,
            20: blueCallableSetPrototypeOf,
            // 21: blueCallableDebugInfo,
            // 22: blueCallableDefineProperties,
            23: blueCallableGetLazyPropertyDescriptorStateByTarget,
            24: blueCallableGetPropertyValue,
            25: blueCallableGetTargetIntegrityTraits,
            26: blueCallableGetToStringTagOfTarget,
            27: blueCallableInstallErrorPrepareStackTrace,
            // 28: blueCallableInstallLazyPropertyDescriptors,
            29: blueCallableIsTargetLive,
            // 30: blueCallableIsTargetRevoked,
            // 31: blueCallableSerializeTarget,
            32: blueCallableSetLazyPropertyDescriptorStateByTarget,
            // 33: blueTrackAsFastTarget,
            34: blueCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
            35: blueCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
            36: blueCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
        } = blueHooks!;
        let redHooks: Parameters<HooksCallback>;
        const redConnect = redConnector('red', (...hooks: Parameters<HooksCallback>) => {
            redHooks = hooks;
        });
        const {
            0: redGlobalThisPointer,
            // 1: redGetSelectedTarget,
            // 2: redGetTransferableValue,
            3: redCallableGetPropertyValuePointer,
            4: redCallableEvaluate,
            5: redCallableLinkPointers,
            6: redCallablePushErrorTarget,
            7: redCallablePushTarget,
            8: redCallableApply,
            9: redCallableConstruct,
            10: redCallableDefineProperty,
            11: redCallableDeleteProperty,
            12: redCallableGet,
            13: redCallableGetOwnPropertyDescriptor,
            14: redCallableGetPrototypeOf,
            15: redCallableHas,
            16: redCallableIsExtensible,
            17: redCallableOwnKeys,
            18: redCallablePreventExtensions,
            19: redCallableSet,
            20: redCallableSetPrototypeOf,
            21: redCallableDebugInfo,
            22: redCallableDefineProperties,
            23: redCallableGetLazyPropertyDescriptorStateByTarget,
            // 24: redCallableGetPropertyValue,
            25: redCallableGetTargetIntegrityTraits,
            26: redCallableGetToStringTagOfTarget,
            27: redCallableInstallErrorPrepareStackTrace,
            28: redCallableInstallLazyPropertyDescriptors,
            // 29: redCallableIsTargetLive,
            30: redCallableIsTargetRevoked,
            31: redCallableSerializeTarget,
            32: redCallableSetLazyPropertyDescriptorStateByTarget,
            33: redCallableTrackAsFastTarget,
            34: redCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
            35: redCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
            36: redCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor,
        } = redHooks!;
        blueConnect(
            noop, // redGlobalThisPointer,
            noop, // redGetSelectedTarget,
            noop as GetTransferableValue, // redGetTransferableValue,
            noop as unknown as CallableGetPropertyValuePointer, // redCallableGetPropertyValuePointer,
            noop as CallableEvaluate, // redCallableEvaluate,
            noop, // redCallableLinkPointers,
            redCallablePushErrorTarget,
            redCallablePushTarget,
            redCallableApply,
            redCallableConstruct,
            redCallableDefineProperty,
            redCallableDeleteProperty,
            redCallableGet,
            redCallableGetOwnPropertyDescriptor,
            redCallableGetPrototypeOf,
            redCallableHas,
            redCallableIsExtensible,
            redCallableOwnKeys,
            redCallablePreventExtensions,
            redCallableSet,
            redCallableSetPrototypeOf,
            redCallableDebugInfo,
            noop, // redCallableDefineProperties,
            redCallableGetLazyPropertyDescriptorStateByTarget,
            noop, // redCallableGetPropertyValue,
            redCallableGetTargetIntegrityTraits,
            redCallableGetToStringTagOfTarget,
            redCallableInstallErrorPrepareStackTrace,
            noop, // redCallableInstallLazyPropertyDescriptors,
            noop as unknown as CallableIsTargetLive, // redCallableIsTargetLive,
            redCallableIsTargetRevoked,
            redCallableSerializeTarget,
            redCallableSetLazyPropertyDescriptorStateByTarget,
            redCallableTrackAsFastTarget,
            redCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
            redCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
            redCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor
        );
        redConnect(
            noop, // blueGlobalThisPointer,
            noop, // blueGetSelectedTarget,
            noop as GetTransferableValue, // blueGetTransferableValue,
            noop as unknown as CallableGetPropertyValuePointer, // blueCallableGetPropertyValuePointer,
            noop as CallableEvaluate, // blueCallableEvaluate,
            noop, // blueCallableLinkPointers,
            blueCallablePushErrorTarget,
            blueCallablePushTarget,
            blueCallableApply,
            blueCallableConstruct,
            blueCallableDefineProperty,
            blueCallableDeleteProperty,
            blueCallableGet,
            blueCallableGetOwnPropertyDescriptor,
            blueCallableGetPrototypeOf,
            blueCallableHas,
            blueCallableIsExtensible,
            blueCallableOwnKeys,
            blueCallablePreventExtensions,
            blueCallableSet,
            blueCallableSetPrototypeOf,
            noop, // blueCallableDebugInfo
            noop, // blueCallableDefineProperties,
            blueCallableGetLazyPropertyDescriptorStateByTarget,
            blueCallableGetPropertyValue,
            blueCallableGetTargetIntegrityTraits,
            blueCallableGetToStringTagOfTarget,
            blueCallableInstallErrorPrepareStackTrace,
            noop, // blueCallableInstallLazyPropertyDescriptors,
            blueCallableIsTargetLive,
            noop as unknown as CallableIsTargetRevoked, // blueCallableIsTargetRevoked,
            noop as CallableSerializeTarget, // blueCallableSerializeTarget,,
            blueCallableSetLazyPropertyDescriptorStateByTarget,
            noop, // blueCallableTrackAsFastTarget,
            blueCallableBatchGetPrototypeOfAndGetOwnPropertyDescriptors,
            blueCallableBatchGetPrototypeOfWhenHasNoOwnProperty,
            blueCallableBatchGetPrototypeOfWhenHasNoOwnPropertyDescriptor
        );
        this.blueGlobalThisPointer = blueGlobalThisPointer;
        this.blueGetSelectedTarget = blueGetSelectedTarget;
        this.blueGetTransferableValue = blueGetTransferableValue;
        this.blueCallableGetPropertyValuePointer = blueCallableGetPropertyValuePointer;
        this.blueCallableLinkPointers = blueCallableLinkPointers;
 
        // Ensure the `this` context of red callable functions is `undefined`.
        this.redGlobalThisPointer = () => redGlobalThisPointer();
        this.redCallableGetPropertyValuePointer = (targetPointer: Pointer, key: PropertyKey) =>
            redCallableGetPropertyValuePointer(targetPointer, key);
        this.redCallableEvaluate = signSourceCallback
            ? (sourceText: string) => redCallableEvaluate(signSourceCallback(sourceText))
            : (sourceText: string) => redCallableEvaluate(sourceText);
        this.redCallableLinkPointers = (targetPointer: Pointer, foreignTargetPointer: Pointer) =>
            redCallableLinkPointers(targetPointer, foreignTargetPointer);
        this.redCallableSetPrototypeOf = (
            targetPointer: Pointer,
            protoPointerOrNull: Pointer | null
        ) => redCallableSetPrototypeOf(targetPointer, protoPointerOrNull);
        this.redCallableDefineProperties = (
            targetPointer: Pointer,
            ...descriptorTuples: [...Parameters<CallableDescriptorCallback>]
        ) => {
            const { length } = descriptorTuples;
            const args = new ArrayCtor(length + 1);
            args[0] = targetPointer;
            for (let i = 0; i < length; i += 1) {
                args[i + 1] = descriptorTuples[i];
            }
            ReflectApply(redCallableDefineProperties, undefined, args);
        };
        this.redCallableInstallLazyPropertyDescriptors = (
            targetPointer: Pointer,
            ...ownKeysAndUnforgeableGlobalThisKeys: PropertyKey[]
        ) => {
            const { length } = ownKeysAndUnforgeableGlobalThisKeys;
            const args = new ArrayCtor(length + 1);
            args[0] = targetPointer;
            for (let i = 0; i < length; i += 1) {
                args[i + 1] = ownKeysAndUnforgeableGlobalThisKeys[i];
            }
            ReflectApply(redCallableInstallLazyPropertyDescriptors, undefined, args);
        };
        this.redCallableTrackAsFastTarget = (targetPointer: Pointer) =>
            redCallableTrackAsFastTarget(targetPointer);
    }
 
    evaluate(sourceText: string): any {
        try {
            const bluePointerOrPrimitiveValue = this.redCallableEvaluate(sourceText);
            Iif (typeof bluePointerOrPrimitiveValue === 'function') {
                bluePointerOrPrimitiveValue();
                return this.blueGetSelectedTarget();
            }
            return bluePointerOrPrimitiveValue;
        } catch (error: any) {
            throw this.blueGetSelectedTarget() ?? error;
        }
    }
 
    lazyRemapProperties(
        target: ProxyTarget,
        ownKeys: PropertyKey[],
        unforgeableGlobalThisKeys?: PropertyKey[]
    ) {
        Eif ((typeof target === 'object' && target !== null) || typeof target === 'function') {
            const args: Parameters<CallableInstallLazyPropertyDescriptors> = [
                this.blueGetTransferableValue(target) as Pointer,
            ];
            ReflectApply(ArrayProtoPush, args, ownKeys);
            Iif (unforgeableGlobalThisKeys?.length) {
                // Use `LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL` to delimit
                // `ownKeys` and `unforgeableGlobalThisKeys`.
                args[args.length] = LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL;
                ReflectApply(ArrayProtoPush, args, unforgeableGlobalThisKeys);
            }
            ReflectApply(this.redCallableInstallLazyPropertyDescriptors, undefined, args);
        }
    }
 
    link(...keys: PropertyKey[]) {
        let bluePointer = this.blueGlobalThisPointer;
        let redPointer = this.redGlobalThisPointer;
        for (let i = 0, { length } = keys; i < length; i += 1) {
            const key = keys[i];
            bluePointer = this.blueCallableGetPropertyValuePointer(bluePointer, key);
            redPointer = this.redCallableGetPropertyValuePointer(redPointer, key);
            this.redCallableLinkPointers(redPointer, bluePointer);
            this.blueCallableLinkPointers(bluePointer, redPointer);
        }
    }
 
    remapProperties(target: ProxyTarget, unsafeBlueDescs: PropertyDescriptorMap) {
        Eif ((typeof target === 'object' && target !== null) || typeof target === 'function') {
            const targetPointer = this.blueGetTransferableValue(target) as Pointer;
            const ownKeys = ReflectOwnKeys(unsafeBlueDescs);
            const { length } = ownKeys;
            const args = new ArrayCtor(1 + length * 7) as Parameters<CallableDefineProperties>;
            args[0] = targetPointer;
            for (let i = 0, j = 1; i < length; i += 1, j += 7) {
                const ownKey = ownKeys[i];
                const unsafeBlueDesc = (unsafeBlueDescs as any)[ownKey];
                // Avoid poisoning by only installing own properties from unsafeBlueDescs.
                // We don't use a toSafeDescriptor() style helper since that mutates
                // the unsafeBlueDesc.
                // eslint-disable-next-line prefer-object-spread
                const safeBlueDesc = ObjectAssign({ __proto__: null }, unsafeBlueDesc);
                args[j] = ownKey;
                args[j + 1] =
                    'configurable' in safeBlueDesc
                        ? !!safeBlueDesc.configurable
                        : LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL;
                args[j + 2] =
                    'enumerable' in safeBlueDesc
                        ? !!safeBlueDesc.enumerable
                        : LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL;
                args[j + 3] =
                    'writable' in safeBlueDesc
                        ? !!safeBlueDesc.writable
                        : LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL;
                args[j + 4] =
                    'value' in safeBlueDesc
                        ? this.blueGetTransferableValue(safeBlueDesc.value)
                        : LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL;
                args[j + 5] =
                    'get' in safeBlueDesc
                        ? (this.blueGetTransferableValue(safeBlueDesc.get) as Pointer)
                        : LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL;
                args[j + 6] =
                    'set' in safeBlueDesc
                        ? (this.blueGetTransferableValue(safeBlueDesc.set) as Pointer)
                        : LOCKER_NEAR_MEMBRANE_UNDEFINED_VALUE_SYMBOL;
            }
            ReflectApply(this.redCallableDefineProperties, this, args);
        }
    }
 
    remapProto(target: ProxyTarget, proto: object | null) {
        if ((typeof target === 'object' && target !== null) || typeof target === 'function') {
            const foreignTargetPointer = this.blueGetTransferableValue(target) as Pointer;
            const transferableProto = proto
                ? (this.blueGetTransferableValue(proto) as Pointer)
                : proto;
            this.redCallableSetPrototypeOf(foreignTargetPointer, transferableProto);
        }
    }
 
    trackAsFastTarget(target: ProxyTarget) {
        if ((typeof target === 'object' && target !== null) || typeof target === 'function') {
            this.redCallableTrackAsFastTarget(this.blueGetTransferableValue(target) as Pointer);
        }
    }
}