|
1 | 1 | import type { CompileStacktraceOptions } from './utils' |
| 2 | +import { |
| 3 | + resolveSourceMapDirByCacheDir, |
| 4 | + resolveSourceMapFileBySourceFile, |
| 5 | +} from './utils' |
| 6 | +import { originalPositionFor } from '../sourceMap' |
2 | 7 |
|
3 | 8 | export interface ParseCppStacktraceOptions extends CompileStacktraceOptions { |
4 | 9 | platform: 'app-harmony' | 'app-ios' | 'app-android' |
5 | 10 | language: 'cpp' |
6 | 11 | stacktrace: string |
7 | | - sourceRoot: string |
8 | | - sourceMapFile: string |
| 12 | + cacheDir: string |
9 | 13 | } |
10 | 14 |
|
| 15 | +// TODO 不同平台cpp堆栈是否一致 |
| 16 | +export const CPP_RUNTIME_ERROR_RE = |
| 17 | + /src\/main\/cpp(.*\.cpp):(\d+):(\d+):\serror:\s(.*)/ |
| 18 | + |
| 19 | +/** |
| 20 | + * cpp暂时只处理编译错误 |
| 21 | + * cpp编译错误信息示例: |
| 22 | + * /xxx/unpackage/dist/dev/app-harmony/entry/src/main/cpp/GenPagesIndexIndexSharedData.cpp:17:15: error: no member named 'backgroundColor123' in 'uniappx::NativeViewImpl' |
| 23 | + * e0->backgroundColor123(0xffff0000); |
| 24 | + * ~~ ^ |
| 25 | + * 1 error generated. |
| 26 | + */ |
11 | 27 | export async function parseCppStacktrace( |
12 | 28 | stacktrace: string, |
13 | 29 | options: Omit<ParseCppStacktraceOptions, 'language'> |
14 | | -) {} |
| 30 | +): Promise<string> { |
| 31 | + const matched = stacktrace.match(CPP_RUNTIME_ERROR_RE) |
| 32 | + if (!matched) { |
| 33 | + return stacktrace |
| 34 | + } |
| 35 | + |
| 36 | + const sourceMapDir = resolveSourceMapDirByCacheDir(options.cacheDir) |
| 37 | + const [, filePath, line, column, message] = matched |
| 38 | + const lines: string[] = [] |
| 39 | + lines.push(message) |
| 40 | + const sourceMapFile = resolveSourceMapFileBySourceFile( |
| 41 | + 'cpp/' + filePath, |
| 42 | + sourceMapDir |
| 43 | + ) |
| 44 | + if (!sourceMapFile) { |
| 45 | + return stacktrace |
| 46 | + } |
| 47 | + const originalPosition = await originalPositionFor({ |
| 48 | + sourceMapFile, |
| 49 | + line: parseInt(line), |
| 50 | + column: parseInt(column), |
| 51 | + withSourceContent: true, |
| 52 | + }) |
| 53 | + if (!originalPosition.source) { |
| 54 | + return stacktrace |
| 55 | + } |
| 56 | + lines.push( |
| 57 | + `at ${originalPosition.source.split('?')[0]}:${originalPosition.line}:${ |
| 58 | + originalPosition.column |
| 59 | + }` |
| 60 | + ) |
| 61 | + return lines.join('\n') |
| 62 | +} |
0 commit comments