UNPKG

blueshell

Version:

A Behavior Tree implementation in modern Javascript

159 lines 6.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Utils = exports.RuntimeWrappers = exports.BreakPointIdRequiredError = exports.NoBreakpointIdForBreakpointError = exports.NoBreakpointForBreakpointError = exports.NoFunctionObjectIdError = exports.NoObjectIdError = void 0; const util_1 = require("util"); class NoObjectIdError extends Error { constructor(key) { super(`ObjectId did not exist for: ${key}`); } } exports.NoObjectIdError = NoObjectIdError; class NoFunctionObjectIdError extends Error { constructor(objectId) { super(`FunctionObjectId did not exist for: ${objectId}`); } } exports.NoFunctionObjectIdError = NoFunctionObjectIdError; class NoBreakpointForBreakpointError extends Error { constructor(functionObjectId) { super(`Got no result for function object id: ${functionObjectId}`); } } exports.NoBreakpointForBreakpointError = NoBreakpointForBreakpointError; class NoBreakpointIdForBreakpointError extends Error { constructor(functionObjectId) { super(`Got no breakpointId from result for function object id: ${functionObjectId}`); } } exports.NoBreakpointIdForBreakpointError = NoBreakpointIdForBreakpointError; class BreakPointIdRequiredError extends Error { constructor() { super('breakpointInfo.breakpointId is required'); } } exports.BreakPointIdRequiredError = BreakPointIdRequiredError; var RuntimeWrappers; (function (RuntimeWrappers) { async function getObjectIdFromRuntimeEvaluate(session, key) { const post = (0, util_1.promisify)(session.post.bind(session)); const res = await post('Runtime.evaluate', { expression: `global.breakpointMethods.get('${key}')`, }); const objectId = res.result.objectId; if (!objectId) { throw new NoObjectIdError(key); } return objectId; } RuntimeWrappers.getObjectIdFromRuntimeEvaluate = getObjectIdFromRuntimeEvaluate; async function getFunctionObjectIdFromRuntimeProperties(session, objectId) { const post = (0, util_1.promisify)(session.post.bind(session)); const res = await post('Runtime.getProperties', { objectId }); const funcObjId = Array.isArray(res.internalProperties) ? res.internalProperties[0].value?.objectId : undefined; if (!funcObjId) { throw new NoFunctionObjectIdError(objectId); } return funcObjId; } RuntimeWrappers.getFunctionObjectIdFromRuntimeProperties = getFunctionObjectIdFromRuntimeProperties; // set the breakpoint in the Runtime Debugger async function setBreakpointOnFunctionCall(session, functionObjectId, condition, breakpointInfo) { // HACK: types are not defined for Debugger.setBreakpointOnFunctionCall const post = (0, util_1.promisify)(session.post.bind(session)); const res = await post('Debugger.setBreakpointOnFunctionCall', { objectId: functionObjectId, condition, }); if (!res) { throw new NoBreakpointForBreakpointError(functionObjectId); } const breakpointId = res.breakpointId; if (!breakpointId) { throw new NoBreakpointIdForBreakpointError(functionObjectId); } breakpointInfo.breakpointId = breakpointId; } RuntimeWrappers.setBreakpointOnFunctionCall = setBreakpointOnFunctionCall; async function removeBreakpointFromFunction(session, breakpointInfo) { if (!breakpointInfo.breakpointId) { throw new BreakPointIdRequiredError(); } const post = (0, util_1.promisify)(session.post.bind(session)); await post('Debugger.removeBreakpoint', { breakpointId: breakpointInfo.breakpointId }); } RuntimeWrappers.removeBreakpointFromFunction = removeBreakpointFromFunction; })(RuntimeWrappers || (exports.RuntimeWrappers = RuntimeWrappers = {})); var Utils; (function (Utils) { function createConditionString(breakpointDataArray) { // build up the condition for each node that has a breakpoint at this class/method let condition = ''; breakpointDataArray.forEach((breakpointData, index) => { if (index > 0) { condition += ' || '; } condition += `(this.path === '${breakpointData.nodePath}'` + (!!breakpointData.condition ? ` && ${breakpointData.condition}` : '') + ')'; }); return condition; } Utils.createConditionString = createConditionString; // eslint-disable-next-line @typescript-eslint/ban-types function getMethodInfoForObject(obj) { const setOfMethods = new Set(); const methodsData = []; let targetObj = obj; do { const methods = Object.getOwnPropertyNames(targetObj) .filter((prop) => { const nodePropDescriptor = Object.getOwnPropertyDescriptor(targetObj, prop); // if the prop name is a getter or setter, if we simply just check that it's a function // that will end up invoking the getter or setter, which could lead to a crash if (nodePropDescriptor?.get || nodePropDescriptor?.set) { return true; } return typeof targetObj[prop] === 'function'; }) .flatMap((prop) => { const props = []; const nodePropDescriptor = Object.getOwnPropertyDescriptor(targetObj, prop); if (nodePropDescriptor?.get) { props.push(`get ${prop}`); } if (nodePropDescriptor?.set) { props.push(`set ${prop}`); } if (!nodePropDescriptor?.get && !nodePropDescriptor?.set) { props.push(prop); } return props; }); const className = targetObj.constructor.name; methods.forEach((methodName) => { // de-duplicate any inherited methods if (!setOfMethods.has(methodName)) { setOfMethods.add(methodName); methodsData.push({ methodName, className }); } }); // climb up the inheritance tree until we get to Object targetObj = Object.getPrototypeOf(targetObj); } while (!!targetObj && targetObj.constructor.name !== 'Object'); methodsData.sort((a, b) => { if (a.methodName < b.methodName) { return -1; } if (a.methodName > b.methodName) { return 1; } return 0; }); return methodsData; } Utils.getMethodInfoForObject = getMethodInfoForObject; })(Utils || (exports.Utils = Utils = {})); //# sourceMappingURL=nodeManagerHelper.js.map