Skip to content

Commit

Permalink
utils: Support injecting properties that are not on the main object
Browse files Browse the repository at this point in the history
  • Loading branch information
3v1n0 committed Aug 24, 2024
1 parent 4756955 commit 611690c
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export class InjectionsHandler extends BasicHandler {
const original = object[name];

if (!(original instanceof Function))
throw new Error(`Virtual function ${name}() is not available for ${object}`);
throw new Error(`Function ${name}() is not available for ${object}`);

object[name] = function (...args) {
return injectedFunction.call(this, original, ...args);
Expand Down Expand Up @@ -380,8 +380,13 @@ export class VFuncInjectionsHandler extends BasicHandler {
* and restored
*/
export class PropertyInjectionsHandler extends BasicHandler {
constructor(params) {
super();
this._params = params;
}

_create(instance, name, injectedPropertyDescriptor) {
if (!(name in instance))
if (!this._params?.allowNewProperty && !(name in instance))
throw new Error(`Object ${instance} has no '${name}' property`);

const {prototype} = instance.constructor;
Expand Down

0 comments on commit 611690c

Please sign in to comment.