microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.12.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/cdp-proxy/CDPMessageHandlers/hermesCDPMessageHandler.ts

99lines · 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
4import { Protocol as Cdp } from "devtools-protocol/types/protocol";
5import { ProcessedCDPMessage } from "./ICDPMessageHandler";
6import { CDP_API_NAMES } from "./CDPAPINames";
7import { BaseCDPMessageHandler } from "./baseCDPMessageHandler";
8
9export 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 return {
46 event,
47 sendBack,
48 };
49 }
50
51 private handleCallFunctionOnEvent(event: any): any {
52 return {
53 result: {
54 result: {
55 objectId: event.params.objectId,
56 },
57 },
58 id: event.id,
59 };
60 }
61
62 private handleFunctionTypeResult(event: any): any {
63 if (Array.isArray(event.result.result)) {
64 const results: Cdp.Runtime.PropertyDescriptor[] = event.result.result;
65 results.forEach(resultObj => {
66 if (
67 resultObj.value &&
68 resultObj.value.type === "function" &&
69 !resultObj.value.description
70 ) {
71 resultObj.value.description = "function() { … }";
72 }
73 });
74
75 event.result.result = results;
76 }
77 return event;
78 }
79
80 private handlePausedEvent(event: any): any {
81 let callFrames: Cdp.Debugger.CallFrame[] = event.params.callFrames;
82
83 callFrames = callFrames.filter(
84 callFrame =>
85 callFrame.functionName !== this.HERMES_NATIVE_FUNCTION_NAME &&
86 callFrame.location.scriptId !== this.HERMES_NATIVE_FUNCTION_SCRIPT_ID,
87 );
88 event.params.callFrames = callFrames;
89
90 return event;
91 }
92
93 private handleBreakpointSetting(event: any): any {
94 if (event.params) {
95 delete event.params.location.columnNumber;
96 }
97 return event;
98 }
99}
100