Skip to content

Commit 5573c12

Browse files
authored
[DI] Allow testing of primitives with instanceof (#5541)
1 parent 76d688e commit 5573c12

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

packages/dd-trace/src/debugger/devtools_client/condition.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ const reservedWords = new Set([
3333
'NaN'
3434
])
3535

36+
const PRIMITIVE_TYPES = new Set(['string', 'number', 'bigint', 'boolean', 'undefined', 'symbol', 'null'])
37+
3638
// TODO: Consider storing some of these functions on `process` so they can be reused across probes
3739
function compile (node) {
3840
if (node === null || typeof node === 'number' || typeof node === 'boolean' || typeof node === 'string') {
@@ -57,7 +59,11 @@ function compile (node) {
5759
}
5860
})()`
5961
} else if (type === 'instanceof') {
60-
return `Function.prototype[Symbol.hasInstance].call(${assertIdentifier(value[1])}, ${compile(value[0])})`
62+
if (isPrimitiveType(value[1])) {
63+
return `(typeof ${compile(value[0])} === '${value[1]}')` // TODO: Is parenthesizing necessary?
64+
} else {
65+
return `Function.prototype[Symbol.hasInstance].call(${assertIdentifier(value[1])}, ${compile(value[0])})`
66+
}
6167
} else if (type === 'ref') {
6268
if (value === '@it') {
6369
return '$dd_it'
@@ -149,6 +155,10 @@ function isString (variable) {
149155
return `(typeof ${variable} === 'string' || ${variable} instanceof String)`
150156
}
151157

158+
function isPrimitiveType (type) {
159+
return PRIMITIVE_TYPES.has(type)
160+
}
161+
152162
function isIterableCollection (variable) {
153163
return `(${isArrayOrTypedArray(variable)} || ${isInstanceOfCoreType('Set', variable)} || ` +
154164
`${isInstanceOfCoreType('WeakSet', variable)})`

packages/dd-trace/test/debugger/devtools_client/condition-test-cases.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,21 @@ const membershipAndMatching = [
621621
]
622622

623623
const typeAndDefinitionChecks = [
624+
// Primitive types
625+
[{ instanceof: [{ ref: 'foo' }, 'string'] }, { foo: 'foo' }, true],
626+
[{ instanceof: [{ ref: 'foo' }, 'number'] }, { foo: 42 }, true],
627+
[{ instanceof: [{ ref: 'foo' }, 'number'] }, { foo: '42' }, false],
628+
[{ instanceof: [{ ref: 'foo' }, 'bigint'] }, { foo: 42n }, true],
629+
[{ instanceof: [{ ref: 'foo' }, 'boolean'] }, { foo: false }, true],
630+
[{ instanceof: [{ ref: 'foo' }, 'boolean'] }, { foo: 0 }, false],
631+
[{ instanceof: [{ ref: 'foo' }, 'undefined'] }, { foo: undefined }, true],
632+
[{ instanceof: [{ ref: 'foo' }, 'symbol'] }, { foo: Symbol('foo') }, true],
633+
[{ instanceof: [{ ref: 'foo' }, 'null'] }, { foo: null }, false], // typeof null is 'object'
634+
635+
// Objects
624636
[{ instanceof: [{ ref: 'bar' }, 'Object'] }, { bar: {} }, true],
625637
[{ instanceof: [{ ref: 'bar' }, 'Error'] }, { bar: new Error() }, true],
638+
[{ instanceof: [{ ref: 'bar' }, 'Error'] }, { bar: {} }, false],
626639
[{ instanceof: [{ ref: 'bar' }, 'CustomObject'] }, { bar: new CustomObject(), CustomObject }, true],
627640
[
628641
{ instanceof: [{ ref: 'bar' }, 'HasInstanceSideEffect'] },

packages/dd-trace/test/debugger/devtools_client/condition.spec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,21 @@ function generateTestCaseName (ast, dataOrSuffix, expected) {
100100
? JSON.stringify(dataOrSuffix)
101101
: Object
102102
.entries(dataOrSuffix)
103-
.map(([key, value]) => `${key} = ${JSON.stringify(value)}`)
103+
.map(([key, value]) => `${key} = ${serialize(value)}`)
104104
.join('; ')
105105

106106
return `${JSON.stringify(ast)} + "${code}" = ${expected}`
107107
}
108108

109+
function serialize (value) {
110+
try {
111+
return JSON.stringify(value)
112+
} catch (e) {
113+
// Some values are not serializable to JSON, so we fall back to stringification
114+
return String(value)
115+
}
116+
}
117+
109118
function runWithDebug (fn, args = []) {
110119
try {
111120
return fn(...args)

0 commit comments

Comments
 (0)