import vm from "vm" ; import { ValueOf } from "@miniflare/shared" ; function ordinaryHasInstance ( C , O ) { if ( typeof C !== "function" ) return false ; if ( typeof O !== "object" && typeof O !== "function" ) return false ; const P = C . prototype ; if ( typeof P !== "object" && typeof P !== "function" ) { throw new TypeError ( `Function has non-object prototype '${ P }' in instanceof check` ) ; } while ( ( O = Object . getPrototypeOf ( O ) ) !== null ) { if ( P === O ) return true ; } return false ; } function instanceOf ( V , insideTarget , outsideTarget ) { return ( ordinaryHasInstance ( insideTarget , V ) || ordinaryHasInstance ( outsideTarget , V ) ) ; } const outsideTargets = { Object , Function , Array , Promise , RegExp , Error , EvalError , RangeError , ReferenceError , SyntaxError , TypeError , URIError , } ; function defineHasInstance ( insideTarget ) { const outsideTarget = outsideTargets [ insideTarget . name as keyof typeof outsideTargets ] ; Object . defineProperty ( insideTarget , Symbol . hasInstance , { value ( value ) { return instanceOf ( value , insideTarget , outsideTarget ) ; } , } ) ; } const defineHasInstancesScript = new vm . Script ( `(function(defineHasInstance) {
  // Only define properties once, would throw if we tried doing this twice
  if (Object.hasOwnProperty(Symbol.hasInstance)) return;
  defineHasInstance(Object);
  defineHasInstance(Function);
  defineHasInstance(Array);
  defineHasInstance(Promise);
  defineHasInstance(RegExp);
  defineHasInstance(Error);
  defineHasInstance(EvalError);
  defineHasInstance(RangeError);
  defineHasInstance(ReferenceError);
  defineHasInstance(SyntaxError);
  defineHasInstance(TypeError);
  defineHasInstance(URIError);
})` , { filename : "<defineHasInstancesScript>" } ) ; export function defineHasInstances ( ctx ) { defineHasInstancesScript . runInContext ( ctx ) ( defineHasInstance ) ; }