Skip to content

Commit 86f0d6d

Browse files
committed
Add trusted-prune-outbound-object.js scriptlet
Essentially a complement of `trusted-prune-inbound-object.js` added in 1c9da22 To perform object pruning on any object returned synchronously by any given call. The arguments for `trusted-prune-outbound-object` in order are: - The name of the property to trap. Must be a function, and must exist when the scriptlet tries to install the trap. - The properties to prune (as with `json-prune`) - The properties which must all be present for pruning to occur (as with `json-prune`) The scriptlets `json-prune.js` and `evaldata-prune.js` essentially perform the same function, and will eventually be rewritten to internally delegate to generic `trusted-prune-outbound-object.js`.
1 parent 0b9b5a4 commit 86f0d6d

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

β€Žassets/resources/scriptlets.jsβ€Ž

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4091,3 +4091,48 @@ function trustedPruneInboundObject(
40914091
}
40924092

40934093
/******************************************************************************/
4094+
4095+
builtinScriptlets.push({
4096+
name: 'trusted-prune-outbound-object.js',
4097+
requiresTrust: true,
4098+
fn: trustedPruneOutboundObject,
4099+
dependencies: [
4100+
'object-prune.fn',
4101+
'safe-self.fn',
4102+
],
4103+
});
4104+
function trustedPruneOutboundObject(
4105+
entryPoint = '',
4106+
rawPrunePaths = '',
4107+
rawNeedlePaths = ''
4108+
) {
4109+
if ( entryPoint === '' ) { return; }
4110+
let context = globalThis;
4111+
let prop = entryPoint;
4112+
for (;;) {
4113+
const pos = prop.indexOf('.');
4114+
if ( pos === -1 ) { break; }
4115+
context = context[prop.slice(0, pos)];
4116+
if ( context instanceof Object === false ) { return; }
4117+
prop = prop.slice(pos+1);
4118+
}
4119+
if ( typeof context[prop] !== 'function' ) { return; }
4120+
const safe = safeSelf();
4121+
const extraArgs = safe.getExtraArgs(Array.from(arguments), 3);
4122+
context[prop] = new Proxy(context[prop], {
4123+
apply: function(target, thisArg, args) {
4124+
const objBefore = Reflect.apply(target, thisArg, args);
4125+
if ( objBefore instanceof Object === false ) { return objBefore; }
4126+
const objAfter = objectPruneFn(
4127+
objBefore,
4128+
rawPrunePaths,
4129+
rawNeedlePaths,
4130+
{ matchAll: true },
4131+
extraArgs
4132+
);
4133+
return objAfter || objBefore;
4134+
},
4135+
});
4136+
}
4137+
4138+
/******************************************************************************/

0 commit comments

Comments
Β (0)