microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler.ts
103lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { Protocol as Cdp } from "devtools-protocol/types/protocol"; |
| 5 | import { ProcessedCDPMessage } from "./ICDPMessageHandler"; |
| 6 | import { CDP_API_NAMES } from "./CDPAPINames"; |
| 7 | import { BaseCDPMessageHandler } from "./baseCDPMessageHandler"; |
| 8 | |
| 9 | export class HermesCDPMessageHandler extends BaseCDPMessageHandler { |
| 10 | /** |
| 11 | * @description The Hermes native functions calls mark in call stack |
| 12 | * @type {string} |
| 13 | */ |
| 14 | private readonly HERMES_NATIVE_FUNCTION_NAME: string = "(native)"; |
| 15 | |
| 16 | /** |
| 17 | * @description Equals to 0xfffffff - the scriptId returned by Hermes debugger, that means "invalid script ID" |
| 18 | * @type {string} |
| 19 | */ |
| 20 | private readonly HERMES_NATIVE_FUNCTION_SCRIPT_ID: string = "4294967295"; |
| 21 | |
| 22 | private readonly ARRAY_REQUEST_PHRASE_MARKER: string = "Object.getOwnPropertyDescriptor"; |
| 23 | |
| 24 | public processDebuggerCDPMessage(event: any): ProcessedCDPMessage { |
| 25 | let sendBack = false; |
| 26 | if (event.method === CDP_API_NAMES.DEBUGGER_SET_BREAKPOINT) { |
| 27 | event = this.handleBreakpointSetting(event); |
| 28 | } else if (event.method === CDP_API_NAMES.RUNTIME_CALL_FUNCTION_ON) { |
| 29 | if (event.params.functionDeclaration.includes(this.ARRAY_REQUEST_PHRASE_MARKER)) { |
| 30 | event = this.handleCallFunctionOnEvent(event); |
| 31 | sendBack = true; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | return { |
| 36 | event, |
| 37 | sendBack, |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | public processApplicationCDPMessage(event: any): ProcessedCDPMessage { |
| 42 | let sendBack = false; |
| 43 | if (event.method === CDP_API_NAMES.DEBUGGER_PAUSED) { |
| 44 | event = this.handlePausedEvent(event); |
| 45 | } else if (event.result && event.result.result) { |
| 46 | event = this.handleFunctionTypeResult(event); |
| 47 | } |
| 48 | |
| 49 | return { |
| 50 | event, |
| 51 | sendBack, |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | private handleCallFunctionOnEvent(event: any): any { |
| 56 | return { |
| 57 | result: { |
| 58 | result: { |
| 59 | objectId: event.params.objectId, |
| 60 | }, |
| 61 | }, |
| 62 | id: event.id, |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | private handleFunctionTypeResult(event: any): any { |
| 67 | if (Array.isArray(event.result.result)) { |
| 68 | let results: Cdp.Runtime.PropertyDescriptor[] = event.result.result; |
| 69 | results.forEach(resultObj => { |
| 70 | if ( |
| 71 | resultObj.value && |
| 72 | resultObj.value.type === "function" && |
| 73 | !resultObj.value.description |
| 74 | ) { |
| 75 | resultObj.value.description = "function() { … }"; |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | event.result.result = results; |
| 80 | } |
| 81 | return event; |
| 82 | } |
| 83 | |
| 84 | private handlePausedEvent(event: any): any { |
| 85 | let callFrames: Cdp.Debugger.CallFrame[] = event.params.callFrames; |
| 86 | |
| 87 | callFrames = callFrames.filter( |
| 88 | callFrame => |
| 89 | callFrame.functionName !== this.HERMES_NATIVE_FUNCTION_NAME && |
| 90 | callFrame.location.scriptId !== this.HERMES_NATIVE_FUNCTION_SCRIPT_ID, |
| 91 | ); |
| 92 | event.params.callFrames = callFrames; |
| 93 | |
| 94 | return event; |
| 95 | } |
| 96 | |
| 97 | private handleBreakpointSetting(event: any): any { |
| 98 | if (event.params) { |
| 99 | delete event.params.location.columnNumber; |
| 100 | } |
| 101 | return event; |
| 102 | } |
| 103 | } |
| 104 | |