Skip to content

Commit 4ccc0d0

Browse files
committed
Minimize memory allocation in scriptlet helper proxy-apply-fn
Probably beneficial in cases of proxied method called in a tight loop. Additionally, added `throwFunc` as valid constant in script helper `validate-constant.fn`. Does what the name implies.
1 parent e98fdeb commit 4ccc0d0

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

assets/resources/scriptlets.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ function validateConstantFn(trusted, raw, extraArgs = {}) {
535535
value = function(){ return true; };
536536
} else if ( raw === 'falseFunc' ) {
537537
value = function(){ return false; };
538+
} else if ( raw === 'throwFunc' ) {
539+
value = function(){ throw ''; };
538540
} else if ( /^-?\d+$/.test(raw) ) {
539541
value = parseInt(raw);
540542
if ( isNaN(raw) ) { return; }
@@ -1498,31 +1500,57 @@ function proxyApplyFn(
14981500
const fn = context[prop];
14991501
if ( typeof fn !== 'function' ) { return; }
15001502
if ( proxyApplyFn.CtorContext === undefined ) {
1503+
proxyApplyFn.ctorContexts = [];
15011504
proxyApplyFn.CtorContext = class {
1502-
constructor(callFn, callArgs) {
1505+
constructor(...args) {
1506+
this.init(...args);
1507+
}
1508+
init(callFn, callArgs) {
15031509
this.callFn = callFn;
15041510
this.callArgs = callArgs;
1511+
return this;
15051512
}
15061513
reflect() {
1507-
return Reflect.construct(this.callFn, this.callArgs);
1514+
const r = Reflect.construct(this.callFn, this.callArgs);
1515+
this.callFn = this.callArgs = undefined;
1516+
proxyApplyFn.ctorContexts.push(this);
1517+
return r;
1518+
}
1519+
static factory(...args) {
1520+
return proxyApplyFn.ctorContexts.length !== 0
1521+
? proxyApplyFn.ctorContexts.pop().init(...args)
1522+
: new proxyApplyFn.CtorContext(...args);
15081523
}
15091524
};
1525+
proxyApplyFn.applyContexts = [];
15101526
proxyApplyFn.ApplyContext = class {
1511-
constructor(callFn, thisArg, callArgs) {
1527+
constructor(...args) {
1528+
this.init(...args);
1529+
}
1530+
init(callFn, thisArg, callArgs) {
15121531
this.callFn = callFn;
15131532
this.thisArg = thisArg;
15141533
this.callArgs = callArgs;
1534+
return this;
15151535
}
15161536
reflect() {
1517-
return Reflect.apply(this.callFn, this.thisArg, this.callArgs);
1537+
const r = Reflect.apply(this.callFn, this.thisArg, this.callArgs);
1538+
this.callFn = this.thisArg = this.callArgs = undefined;
1539+
proxyApplyFn.applyContexts.push(this);
1540+
return r;
1541+
}
1542+
static factory(...args) {
1543+
return proxyApplyFn.applyContexts.length !== 0
1544+
? proxyApplyFn.applyContexts.pop().init(...args)
1545+
: new proxyApplyFn.ApplyContext(...args);
15181546
}
15191547
};
15201548
}
15211549
const fnStr = fn.toString();
15221550
const toString = (function toString() { return fnStr; }).bind(null);
15231551
const proxyDetails = {
15241552
apply(target, thisArg, args) {
1525-
return handler(new proxyApplyFn.ApplyContext(target, thisArg, args));
1553+
return handler(proxyApplyFn.ApplyContext.factory(target, thisArg, args));
15261554
},
15271555
get(target, prop) {
15281556
if ( prop === 'toString' ) { return toString; }
@@ -1531,7 +1559,7 @@ function proxyApplyFn(
15311559
};
15321560
if ( fn.prototype?.constructor === fn ) {
15331561
proxyDetails.construct = function(target, args) {
1534-
return handler(new proxyApplyFn.CtorContext(target, args));
1562+
return handler(proxyApplyFn.CtorContext.factory(target, args));
15351563
};
15361564
}
15371565
context[prop] = new Proxy(fn, proxyDetails);

0 commit comments

Comments
 (0)