Skip to content

Commit 3a289d4

Browse files
committed
fix: await repl and console message missing
1 parent ac90c6c commit 3a289d4

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

src/domains/Runtime.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ import stackTrace from 'licia/stackTrace'
1010
import trim from 'licia/trim'
1111
import types from 'licia/types'
1212
import * as objManager from '../lib/objManager'
13-
import evaluateJs, { setGlobal } from '../lib/evaluate'
13+
import { evalJs, evalJsAsync, setGlobal } from '../lib/evaluate'
1414
import Protocol from 'devtools-protocol'
15+
import { getTimestamp } from '../lib/util'
1516
import Runtime = Protocol.Runtime
1617

1718
const executionContext = {
@@ -64,17 +65,30 @@ export function getProperties(
6465
return objManager.getProperties(params)
6566
}
6667

67-
export function evaluate(
68+
let isAsyncSupported = true
69+
70+
try {
71+
eval('async () => {}')
72+
} catch (e) {
73+
isAsyncSupported = false
74+
}
75+
76+
export async function evaluate(
6877
params: Runtime.EvaluateRequest
69-
): Runtime.EvaluateResponse {
78+
): Promise<Runtime.EvaluateResponse> {
7079
const ret: any = {}
7180

7281
let result: any
7382
try {
7483
if (params.throwOnSideEffect && hasSideEffect(params.expression)) {
7584
throw EvalError('Possible side-effect in debug-evaluate')
7685
}
77-
result = evaluateJs(params.expression)
86+
87+
if (isAsyncSupported && (params.replMode || params.awaitPromise)) {
88+
result = await evalJsAsync(params.expression)
89+
} else {
90+
result = evalJs(params.expression)
91+
}
7892
setGlobal('$_', result)
7993
ret.result = objManager.wrap(result, {
8094
generatePreview: true,
@@ -123,6 +137,7 @@ function monitorConsole() {
123137
if (!console[name]) {
124138
return
125139
}
140+
let lastTimestamp = 0
126141
const origin = console[name].bind(console)
127142
console[name] = (...args: any[]) => {
128143
origin(...args)
@@ -133,6 +148,12 @@ function monitorConsole() {
133148
})
134149
)
135150

151+
let timestamp = getTimestamp()
152+
if (timestamp <= lastTimestamp) {
153+
timestamp = lastTimestamp + 0.001
154+
}
155+
lastTimestamp = timestamp
156+
136157
trigger('Runtime.consoleAPICalled', {
137158
type,
138159
args,
@@ -141,7 +162,7 @@ function monitorConsole() {
141162
type === 'error' || type === 'warning' ? getCallFrames() : [],
142163
},
143164
executionContextId: executionContext.id,
144-
timestamp: now(),
165+
timestamp,
145166
})
146167
}
147168
})

src/lib/evaluate.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function setGlobal(name: string, val: any) {
4444
global[name] = val
4545
}
4646

47-
export default function evaluate(expression: string) {
47+
export function evalJs(expression: string) {
4848
let ret
4949

5050
injectGlobal()
@@ -57,3 +57,17 @@ export default function evaluate(expression: string) {
5757

5858
return ret
5959
}
60+
61+
export function evalJsAsync(expression: string) {
62+
let ret
63+
64+
injectGlobal()
65+
try {
66+
ret = eval.call(window, `(async () => (${expression}))()`)
67+
} catch (e) {
68+
ret = eval.call(window, `(async () => {${expression}})()`)
69+
}
70+
clearGlobal()
71+
72+
return ret
73+
}

src/lib/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import convertBin from 'licia/convertBin'
66
import axios from 'axios'
77
import _type from 'licia/type'
88
import _has from 'licia/has'
9+
import perfNow from 'licia/perfNow'
10+
import now from 'licia/now'
911

1012
const prefix = random(1000, 9999) + '.'
1113

@@ -73,6 +75,14 @@ export function has(obj: any, key: string) {
7375
}
7476
}
7577

78+
export function getTimestamp() {
79+
if (window.performance && performance.timeOrigin) {
80+
return performance.timeOrigin + perfNow()
81+
}
82+
83+
return now()
84+
}
85+
7686
async function getContent(url: string, responseType: any, proxy = '') {
7787
try {
7888
const urlObj = new Url(url)

0 commit comments

Comments
 (0)