microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.1.0

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 /**
12 * @description The Hermes native functions calls mark in call stack
13 * @type {string}
14 */
15 private readonly HERMES_NATIVE_FUNCTION_NAME: string = "(native)";
16
17 /**
18 * @description Equals to 0xfffffff - the scriptId returned by Hermes debugger, that means "invalid script ID"
19 * @type {string}
20 */
21 private readonly HERMES_NATIVE_FUNCTION_SCRIPT_ID: string = "4294967295";
22
23 private readonly ARRAY_REQUEST_PHRASE_MARKER: string = "Object.getOwnPropertyDescriptor";
24
25 public processDebuggerCDPMessage(event: any): ProcessedCDPMessage {
26 let sendBack = false;
27 if (event.method === CDP_API_NAMES.DEBUGGER_SET_BREAKPOINT) {
28 event = this.handleBreakpointSetting(event);
29 } else if (event.method === CDP_API_NAMES.RUNTIME_CALL_FUNCTION_ON) {
30 if (event.params.functionDeclaration.includes(this.ARRAY_REQUEST_PHRASE_MARKER)) {
31 event = this.handleCallFunctionOnEvent(event);
32 sendBack = true;
33 }
34 }
35
36 return {
37 event,
38 sendBack,
39 };
40 }
41
42 public processApplicationCDPMessage(event: any): ProcessedCDPMessage {
43 let sendBack = false;
44 if (event.method === CDP_API_NAMES.DEBUGGER_PAUSED) {
45 event = this.handlePausedEvent(event);
46 } else if (event.result && event.result.result) {
47 event = this.handleFunctionTypeResult(event);
48 }
49
50 return {
51 event,
52 sendBack,
53 };
54 }
55
56 private handleCallFunctionOnEvent(event: any): any {
57 return {
58 result: {
59 result: {
60 objectId: event.params.objectId,
61 },
62 },
63 id: event.id,
64 };
65 }
66
67 private handleFunctionTypeResult(event: any): any {
68 if (Array.isArray(event.result.result)) {
69 let results: Cdp.Runtime.PropertyDescriptor[] = event.result.result;
70 results.forEach((resultObj) => {
71 if (resultObj.value && resultObj.value.type === "function" && !resultObj.value.description) {
72 resultObj.value.description = "function() { … }";
73 }
74 });
75
76 event.result.result = results;
77 }
78 return event;
79 }
80
81 private handlePausedEvent(event: any): any {
82 let callFrames: Cdp.Debugger.CallFrame[] = event.params.callFrames;
83
84 callFrames = callFrames.filter(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