microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler.ts
109lines · 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 | public processDebuggerCDPMessage(event: any): ProcessedCDPMessage { |
| 23 | let sendBack = false; |
| 24 | if (event.method === CDP_API_NAMES.DEBUGGER_SET_BREAKPOINT) { |
| 25 | event = this.handleBreakpointSetting(event); |
| 26 | } else if (event.method === CDP_API_NAMES.RUNTIME_CALL_FUNCTION_ON) { |
| 27 | event = this.handleCallFunctionOnEvent(event); |
| 28 | sendBack = true; |
| 29 | } |
| 30 | |
| 31 | return { |
| 32 | event, |
| 33 | sendBack, |
| 34 | }; |
| 35 | } |
| 36 | |
| 37 | public processApplicationCDPMessage(event: any): ProcessedCDPMessage { |
| 38 | const sendBack = false; |
| 39 | if (event.method === CDP_API_NAMES.DEBUGGER_PAUSED) { |
| 40 | event = this.handlePausedEvent(event); |
| 41 | } else if (event.result && event.result.result) { |
| 42 | event = this.handleFunctionTypeResult(event); |
| 43 | } |
| 44 | |
| 45 | // Handle unused console log in cdp event |
| 46 | if ( |
| 47 | event.method === CDP_API_NAMES.RUNTIME_CONSOLE_API_CALLED && |
| 48 | String(event.params.args[0].value).includes( |
| 49 | "You are using an unsupported debugging client", |
| 50 | ) |
| 51 | ) { |
| 52 | event.params.args[0].value = ""; |
| 53 | } |
| 54 | |
| 55 | return { |
| 56 | event, |
| 57 | sendBack, |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | private handleCallFunctionOnEvent(event: any): any { |
| 62 | return { |
| 63 | result: { |
| 64 | result: { |
| 65 | objectId: event.params.objectId, |
| 66 | }, |
| 67 | }, |
| 68 | id: event.id, |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | private handleFunctionTypeResult(event: any): any { |
| 73 | if (Array.isArray(event.result.result)) { |
| 74 | const results: Cdp.Runtime.PropertyDescriptor[] = event.result.result; |
| 75 | results.forEach(resultObj => { |
| 76 | if ( |
| 77 | resultObj.value && |
| 78 | resultObj.value.type === "function" && |
| 79 | !resultObj.value.description |
| 80 | ) { |
| 81 | resultObj.value.description = "function() { … }"; |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | event.result.result = results; |
| 86 | } |
| 87 | return event; |
| 88 | } |
| 89 | |
| 90 | private handlePausedEvent(event: any): any { |
| 91 | let callFrames: Cdp.Debugger.CallFrame[] = event.params.callFrames; |
| 92 | |
| 93 | callFrames = callFrames.filter( |
| 94 | callFrame => |
| 95 | callFrame.functionName !== this.HERMES_NATIVE_FUNCTION_NAME && |
| 96 | callFrame.location.scriptId !== this.HERMES_NATIVE_FUNCTION_SCRIPT_ID, |
| 97 | ); |
| 98 | event.params.callFrames = callFrames; |
| 99 | |
| 100 | return event; |
| 101 | } |
| 102 | |
| 103 | private handleBreakpointSetting(event: any): any { |
| 104 | if (event.params) { |
| 105 | delete event.params.location.columnNumber; |
| 106 | } |
| 107 | return event; |
| 108 | } |
| 109 | } |
| 110 | |