@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 14.8 kB
JavaScript
export class RemoteObject{static fromLocalObject(e){return new LocalJSONObject(e)}static type(e){if(null===e)return"null";const t=typeof e;return"object"!==t&&"function"!==t?t:e.type}static isNullOrUndefined(e){if(void 0===e)return!0;switch(e.type){case"object":return"null"===e.subtype;case"undefined":return!0;default:return!1}}static arrayNameFromDescription(e){return e.replace(_descriptionLengthParenRegex,"").replace(_descriptionLengthSquareRegex,"")}static arrayLength(e){if("array"!==e.subtype&&"typedarray"!==e.subtype)return 0;const t=e.description&&e.description.match(_descriptionLengthParenRegex),r=e.description&&e.description.match(_descriptionLengthSquareRegex);return t?parseInt(t[1],10):r?parseInt(r[1],10):0}static arrayBufferByteLength(e){if("arraybuffer"!==e.subtype)return 0;const t=e.description&&e.description.match(_descriptionLengthParenRegex);return t?parseInt(t[1],10):0}static unserializableDescription(e){const t=typeof e;if("number"===t){const t=String(e);if(0===e&&1/e<0)return"-0";if("NaN"===t||"Infinity"===t||"-Infinity"===t)return t}return"bigint"===t?e+"n":null}static toCallArgument(e){const t=typeof e;if("undefined"===t)return{};const r=RemoteObject.unserializableDescription(e);if("number"===t)return null!==r?{unserializableValue:r}:{value:e};if("bigint"===t)return{unserializableValue:r};if("string"===t||"boolean"===t)return{value:e};if(!e)return{value:null};const n=e;if(e instanceof RemoteObject){const t=e.unserializableValue();if(void 0!==t)return{unserializableValue:t}}else if(void 0!==n.unserializableValue)return{unserializableValue:n.unserializableValue};return void 0!==n.objectId?{objectId:n.objectId}:{value:n.value}}static async loadFromObjectPerProto(e,t,r=!1){const n=await Promise.all([e.getAllProperties(!0,t,r),e.getOwnProperties(t,r)]),i=n[0].properties,o=n[1].properties,s=n[1].internalProperties;if(!o||!i)return{properties:null,internalProperties:null};const a=new Map,l=[];for(let e=0;e<i.length;e++){const t=i[e];t.symbol?l.push(t):(t.isOwn||"__proto__"!==t.name)&&a.set(t.name,t)}for(let e=0;e<o.length;e++){const t=o[e];t.isAccessorProperty()||(t.private||t.symbol?l.push(t):a.set(t.name,t))}return{properties:[...a.values()].concat(l),internalProperties:s||null}}customPreview(){return null}get objectId(){return"Not implemented"}get type(){throw"Not implemented"}get subtype(){throw"Not implemented"}get value(){throw"Not implemented"}unserializableValue(){throw"Not implemented"}get description(){throw"Not implemented"}set description(e){throw"Not implemented"}get hasChildren(){throw"Not implemented"}get preview(){}get className(){return null}arrayLength(){throw"Not implemented"}arrayBufferByteLength(){throw"Not implemented"}getOwnProperties(e,t){throw"Not implemented"}getAllProperties(e,t,r){throw"Not implemented"}async deleteProperty(e){throw"Not implemented"}async setPropertyValue(e,t){throw"Not implemented"}callFunction(e,t){throw"Not implemented"}callFunctionJSON(e,t){throw"Not implemented"}release(){}debuggerModel(){throw new Error("DebuggerModel-less object")}runtimeModel(){throw new Error("RuntimeModel-less object")}isNode(){return!1}webIdl}export class RemoteObjectImpl extends RemoteObject{runtimeModelInternal;#e;#t;#r;#n;#i;hasChildrenInternal;#o;#s;#a;#l;#c;constructor(e,t,r,n,i,o,s,a,l,c){super(),this.runtimeModelInternal=e,this.#e=e.target().runtimeAgent(),this.#t=r,this.#r=n,t?(this.#n=t,this.#i=s,this.hasChildrenInternal="symbol"!==r,this.#o=a):(this.#i=s,!this.description&&o&&(this.#i=o),this.#i||"object"==typeof i&&null!==i||(this.#i=String(i)),this.hasChildrenInternal=!1,"string"==typeof o?(this.#s=o,"Infinity"===o||"-Infinity"===o||"-0"===o||"NaN"===o?this.#a=Number(o):"bigint"===r&&o.endsWith("n")?this.#a=BigInt(o.substring(0,o.length-1)):this.#a=o):this.#a=i),this.#l=l||null,this.#c="string"==typeof c?c:null}customPreview(){return this.#l}get objectId(){return this.#n}get type(){return this.#t}get subtype(){return this.#r}get value(){return this.#a}unserializableValue(){return this.#s}get description(){return this.#i}set description(e){this.#i=e}get hasChildren(){return this.hasChildrenInternal}get preview(){return this.#o}get className(){return this.#c}getOwnProperties(e,t=!1){return this.doGetProperties(!0,!1,t,e)}getAllProperties(e,t,r=!1){return this.doGetProperties(!1,e,r,t)}async createRemoteObject(e){return this.runtimeModelInternal.createRemoteObject(e)}async doGetProperties(e,t,r,n){if(!this.#n)return{properties:null,internalProperties:null};const i=await this.#e.invoke_getProperties({objectId:this.#n,ownProperties:e,accessorPropertiesOnly:t,nonIndexedPropertiesOnly:r,generatePreview:n});if(i.getError())return{properties:null,internalProperties:null};if(i.exceptionDetails)return this.runtimeModelInternal.exceptionThrown(Date.now(),i.exceptionDetails),{properties:null,internalProperties:null};const{result:o=[],internalProperties:s=[],privateProperties:a=[]}=i,l=[];for(const e of o){const t=e.value?await this.createRemoteObject(e.value):null,r=e.symbol?this.runtimeModelInternal.createRemoteObject(e.symbol):null,n=new RemoteObjectProperty(e.name,t,Boolean(e.enumerable),Boolean(e.writable),Boolean(e.isOwn),Boolean(e.wasThrown),r);void 0===e.value&&(e.get&&"undefined"!==e.get.type&&(n.getter=this.runtimeModelInternal.createRemoteObject(e.get)),e.set&&"undefined"!==e.set.type&&(n.setter=this.runtimeModelInternal.createRemoteObject(e.set))),l.push(n)}for(const e of a){const t=e.value?this.runtimeModelInternal.createRemoteObject(e.value):null,r=new RemoteObjectProperty(e.name,t,!0,!0,!0,!1,void 0,!1,void 0,!0);void 0===e.value&&(e.get&&"undefined"!==e.get.type&&(r.getter=this.runtimeModelInternal.createRemoteObject(e.get)),e.set&&"undefined"!==e.set.type&&(r.setter=this.runtimeModelInternal.createRemoteObject(e.set))),l.push(r)}const c=[];for(const e of s){if(!e.value)continue;if("[[StableObjectId]]"===e.name)continue;const t=this.runtimeModelInternal.createRemoteObject(e.value);c.push(new RemoteObjectProperty(e.name,t,!0,!1,void 0,void 0,void 0,!0))}return{properties:l,internalProperties:c}}async setPropertyValue(e,t){if(!this.#n)return"Can’t set a property of non-object.";const r=await this.#e.invoke_evaluate({expression:t,silent:!0});if(r.getError()||r.exceptionDetails)return r.getError()||("string"!==r.result.type?r.result.description:r.result.value);"string"==typeof e&&(e=RemoteObject.toCallArgument(e));const n=this.doSetObjectPropertyValue(r.result,e);return r.result.objectId&&this.#e.invoke_releaseObject({objectId:r.result.objectId}),n}async doSetObjectPropertyValue(e,t){const r=[t,RemoteObject.toCallArgument(e)],n=await this.#e.invoke_callFunctionOn({objectId:this.#n,functionDeclaration:"function(a, b) { this[a] = b; }",arguments:r,silent:!0}),i=n.getError();return i||n.exceptionDetails?i||n.result.description:void 0}async deleteProperty(e){if(!this.#n)return"Can’t delete a property of non-object.";const t=await this.#e.invoke_callFunctionOn({objectId:this.#n,functionDeclaration:"function(a) { delete this[a]; return !(a in this); }",arguments:[e],silent:!0});return t.getError()||t.exceptionDetails?t.getError()||t.result.description:t.result.value?void 0:"Failed to delete property."}async callFunction(e,t){const r=await this.#e.invoke_callFunctionOn({objectId:this.#n,functionDeclaration:e.toString(),arguments:t,silent:!0});return r.getError()?{object:null,wasThrown:!1}:{object:this.runtimeModelInternal.createRemoteObject(r.result),wasThrown:Boolean(r.exceptionDetails)}}async callFunctionJSON(e,t){const r=await this.#e.invoke_callFunctionOn({objectId:this.#n,functionDeclaration:e.toString(),arguments:t,silent:!0,returnByValue:!0});return r.getError()||r.exceptionDetails?null:r.result.value}release(){this.#n&&this.#e.invoke_releaseObject({objectId:this.#n})}arrayLength(){return RemoteObject.arrayLength(this)}arrayBufferByteLength(){return RemoteObject.arrayBufferByteLength(this)}debuggerModel(){return this.runtimeModelInternal.debuggerModel()}runtimeModel(){return this.runtimeModelInternal}isNode(){return Boolean(this.#n)&&"object"===this.type&&"node"===this.subtype}}export class ScopeRemoteObject extends RemoteObjectImpl{#u;#h;constructor(e,t,r,n,i,o,s,a,l){super(e,t,n,i,o,s,a,l),this.#u=r,this.#h=void 0}async doGetProperties(e,t,r){if(t)return{properties:[],internalProperties:[]};if(this.#h)return{properties:this.#h.slice(),internalProperties:null};const n=await super.doGetProperties(e,t,!1,!0);if(this.#u&&Array.isArray(n.properties)&&(this.#h=n.properties.slice(),!this.#u.callFrameId))for(const e of this.#h)e.writable=!1;return n}async doSetObjectPropertyValue(e,t){const r=t.value,n=await this.debuggerModel().setVariableValue(this.#u.number,r,RemoteObject.toCallArgument(e),this.#u.callFrameId);if(n)return n;if(this.#h)for(const t of this.#h)t.name===r&&(t.value=this.runtimeModel().createRemoteObject(e))}}export class ScopeRef{number;callFrameId;constructor(e,t){this.number=e,this.callFrameId=t}}export class RemoteObjectProperty{name;value;enumerable;writable;isOwn;wasThrown;symbol;synthetic;syntheticSetter;private;getter;setter;webIdl;constructor(e,t,r,n,i,o,s,a,l,c){this.name=e,this.value=null!==t?t:void 0,this.enumerable=void 0===r||r;const u=!a||Boolean(l);this.writable=void 0!==n?n:u,this.isOwn=Boolean(i),this.wasThrown=Boolean(o),s&&(this.symbol=s),this.synthetic=Boolean(a),l&&(this.syntheticSetter=l),this.private=Boolean(c)}async setSyntheticValue(e){if(!this.syntheticSetter)return!1;const t=await this.syntheticSetter(e);return t&&(this.value=t),Boolean(t)}isAccessorProperty(){return Boolean(this.getter||this.setter)}match({includeNullOrUndefinedValues:e,regex:t}){return!(null!==t&&!t.test(this.name)&&!t.test(this.value?.description??""))&&!(!e&&!this.isAccessorProperty()&&RemoteObject.isNullOrUndefined(this.value))}cloneWithNewName(e){const t=new RemoteObjectProperty(e,this.value??null,this.enumerable,this.writable,this.isOwn,this.wasThrown,this.symbol,this.synthetic,this.syntheticSetter,this.private);return t.getter=this.getter,t.setter=this.setter,t}}export class LocalJSONObject extends RemoteObject{valueInternal;#p;#d;constructor(e){super(),this.valueInternal=e}get objectId(){}get value(){return this.valueInternal}unserializableValue(){return RemoteObject.unserializableDescription(this.valueInternal)||void 0}get description(){if(this.#p)return this.#p;if("object"===this.type)switch(this.subtype){case"array":this.#p=this.concatenate("[","]",function(e){return this.formatValue(e.value||null)}.bind(this));break;case"date":this.#p=String(this.valueInternal);break;case"null":this.#p="null";break;default:this.#p=this.concatenate("{","}",function(e){let t=e.name;return/^\s|\s$|^$|\n/.test(t)&&(t='"'+t.replace(/\n/g,"↵")+'"'),t+": "+this.formatValue(e.value||null)}.bind(this))}else this.#p=String(this.valueInternal);return this.#p}formatValue(e){if(!e)return"undefined";const t=e.description||"";return"string"===e.type?'"'+t.replace(/\n/g,"↵")+'"':t}concatenate(e,t,r){let n=e;const i=this.children();for(let e=0;e<i.length;++e){const t=r(i[e]);if(n.length+t.length>100){n+=",…";break}e&&(n+=", "),n+=t}return n+=t,n}get type(){return typeof this.valueInternal}get subtype(){return null===this.valueInternal?"null":Array.isArray(this.valueInternal)?"array":this.valueInternal instanceof Date?"date":void 0}get hasChildren(){return"object"==typeof this.valueInternal&&null!==this.valueInternal&&Boolean(Object.keys(this.valueInternal).length)}async getOwnProperties(e,t=!1){let r=this.children();return t&&(r=r.filter((e=>!function(e){const t=Number(e)>>>0;return String(t)===e}(e.name)))),{properties:r,internalProperties:null}}async getAllProperties(e,t,r=!1){return e?{properties:[],internalProperties:null}:await this.getOwnProperties(t,r)}children(){if(!this.hasChildren)return[];const e=this.valueInternal;return this.#d||(this.#d=Object.keys(e).map((function(t){let r=e[t];return r instanceof RemoteObject||(r=RemoteObject.fromLocalObject(r)),new RemoteObjectProperty(t,r)}))),this.#d}arrayLength(){return Array.isArray(this.valueInternal)?this.valueInternal.length:0}async callFunction(e,t){const r=this.valueInternal,n=t?t.map((e=>e.value)):[];let i,o=!1;try{i=e.apply(r,n)}catch(e){o=!0}return{object:RemoteObject.fromLocalObject(i),wasThrown:o}}async callFunctionJSON(e,t){const r=this.valueInternal,n=t?t.map((e=>e.value)):[];let i;try{i=e.apply(r,n)}catch(e){i=null}return i}}export class RemoteArrayBuffer{#b;constructor(e){if("object"!==e.type||"arraybuffer"!==e.subtype)throw new Error("Object is not an arraybuffer");this.#b=e}byteLength(){return this.#b.arrayBufferByteLength()}async bytes(e=0,t=this.byteLength()){if(e<0||e>=this.byteLength())throw new RangeError("start is out of range");if(t<e||t>this.byteLength())throw new RangeError("end is out of range");return await this.#b.callFunctionJSON((function(e,t){return[...new Uint8Array(this,e,t)]}),[{value:e},{value:t-e}])}object(){return this.#b}}export class RemoteArray{#b;constructor(e){this.#b=e}static objectAsArray(e){if(!e||"object"!==e.type||"array"!==e.subtype&&"typedarray"!==e.subtype)throw new Error("Object is empty or not an array");return new RemoteArray(e)}static createFromRemoteObjects(e){if(!e.length)throw new Error("Input array is empty");const t=[];for(let r=0;r<e.length;++r)t.push(RemoteObject.toCallArgument(e[r]));return e[0].callFunction((function(){if(arguments.length>1)return new Array(arguments);return[arguments[0]]}),t).then((function(e){if(e.wasThrown||!e.object)throw new Error("Call function throws exceptions or returns empty value");return RemoteArray.objectAsArray(e.object)}))}at(e){if(e<0||e>this.#b.arrayLength())throw new Error("Out of range");return this.#b.callFunction((function(e){return this[e]}),[RemoteObject.toCallArgument(e)]).then((function(e){if(e.wasThrown||!e.object)throw new Error("Exception in callFunction or result value is empty");return e.object}))}length(){return this.#b.arrayLength()}map(e){const t=[];for(let r=0;r<this.length();++r)t.push(this.at(r).then(e));return Promise.all(t)}object(){return this.#b}}export class RemoteFunction{#b;constructor(e){this.#b=e}static objectAsFunction(e){if(!e||"function"!==e.type)throw new Error("Object is empty or not a function");return new RemoteFunction(e)}targetFunction(){return this.#b.getOwnProperties(!1).then(function(e){if(!e.internalProperties)return this.#b;const t=e.internalProperties;for(const e of t)if("[[TargetFunction]]"===e.name)return e.value;return this.#b}.bind(this))}targetFunctionDetails(){return this.targetFunction().then(function(t){const r=e.bind(null,this.#b!==t?t:null);return t.debuggerModel().functionDetailsPromise(t).then(r)}.bind(this));function e(e,t){return e&&e.release(),t}}object(){return this.#b}}const _descriptionLengthParenRegex=/\(([0-9]+)\)/,_descriptionLengthSquareRegex=/\[([0-9]+)\]/;