mirror of
https://github.com/Microsoft/vscode
synced 2024-10-06 03:17:00 +00:00
Reuse safeStringify
This commit is contained in:
parent
938124008c
commit
bbee9d7864
|
@ -11,7 +11,7 @@ export interface IRemoteConsoleLog {
|
|||
arguments: string;
|
||||
}
|
||||
|
||||
interface IStackArgument {
|
||||
export interface IStackArgument {
|
||||
__$stack: string;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IStackArgument } from 'vs/base/common/console';
|
||||
import { safeStringify } from 'vs/base/common/objects';
|
||||
import { MainContext, MainThreadConsoleShape } from 'vs/workbench/api/common/extHost.protocol';
|
||||
import { IExtHostInitDataService } from 'vs/workbench/api/common/extHostInitDataService';
|
||||
import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
|
||||
|
@ -70,7 +72,6 @@ const MAX_LENGTH = 100000;
|
|||
* Prevent circular stringify and convert arguments to real array
|
||||
*/
|
||||
function safeStringifyArgumentsToArray(args: IArguments, includeStack: boolean): string {
|
||||
const seen: any[] = [];
|
||||
const argsArray = [];
|
||||
|
||||
// Massage some arguments with special treatment
|
||||
|
@ -105,24 +106,12 @@ function safeStringifyArgumentsToArray(args: IArguments, includeStack: boolean):
|
|||
if (includeStack) {
|
||||
const stack = new Error().stack;
|
||||
if (stack) {
|
||||
argsArray.push({ __$stack: stack.split('\n').slice(3).join('\n') });
|
||||
argsArray.push({ __$stack: stack.split('\n').slice(3).join('\n') } as IStackArgument);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const res = JSON.stringify(argsArray, function (key, value) {
|
||||
|
||||
// Objects get special treatment to prevent circles
|
||||
if (isObject(value) || Array.isArray(value)) {
|
||||
if (seen.indexOf(value) !== -1) {
|
||||
return '[Circular]';
|
||||
}
|
||||
|
||||
seen.push(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
});
|
||||
const res = safeStringify(argsArray);
|
||||
|
||||
if (res.length > MAX_LENGTH) {
|
||||
return 'Output omitted for a large object that exceeds the limits';
|
||||
|
@ -133,11 +122,3 @@ function safeStringifyArgumentsToArray(args: IArguments, includeStack: boolean):
|
|||
return `Output omitted for an object that cannot be inspected ('${error.toString()}')`;
|
||||
}
|
||||
}
|
||||
|
||||
function isObject(obj: unknown) {
|
||||
return typeof obj === 'object'
|
||||
&& obj !== null
|
||||
&& !Array.isArray(obj)
|
||||
&& !(obj instanceof RegExp)
|
||||
&& !(obj instanceof Date);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue