Skip to content

Commit 8c04610

Browse files
Add support for unsupported var declaration
1 parent b1fc38b commit 8c04610

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

integration-test/test-pages/runtime-checks/config/script-overload.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
"navigator.mediaSession.playbackState": {
2525
"type": "string",
2626
"value": "playing"
27+
},
28+
"navigator.mediaSession.doesNotExist.depth.a.lot": {
29+
"type": "string",
30+
"value": "boop"
2731
}
2832
}
2933
}

integration-test/test-pages/runtime-checks/pages/script-overload.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
pb: navigator.mediaSession.playbackState,
2828
wpb: window.navigator.mediaSession.playbackState,
2929
gpb: globalThis.navigator.mediaSession.playbackState,
30+
undef: navigator.mediaSession.doesNotExist.depth.a.lot
3031
}
3132
`;
3233
scriptElement.id = 'overloadedScript';
@@ -51,7 +52,8 @@
5152
g: expectedUserAgentOverload,
5253
pb: expectedPlaybackState,
5354
wpb: expectedPlaybackState,
54-
gpb: expectedPlaybackState
55+
gpb: expectedPlaybackState,
56+
undef: 'boop'
5557
} },
5658
{ name: 'user agent doesnt match parent context', result: doesntMatchParentContext, expected: true }
5759
];

src/features/runtime-checks.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,20 @@ class DDGRuntimeChecks extends HTMLElement {
187187
for (const [key, value] of scope) {
188188
const varOutName = [...scopePath, key].join('_')
189189
if (value instanceof Map) {
190+
const proxyName = `_proxyFor_${varOutName}`
191+
output += `
192+
let ${proxyName}
193+
if (${scopePath.join('?.')}?.${key} === undefined) {
194+
${proxyName} = Object.bind(null);
195+
} else {
196+
${proxyName} = ${scopePath.join('.')}.${key};
197+
}
198+
`
190199
const keys = Array.from(value.keys())
191200
output += stringifyScope(value, [...scopePath, key])
192201
const proxyOut = keys.map((keyName) => `${keyName}: ${[...scopePath, key, keyName].join('_')}`)
193202
output += `
194-
let ${varOutName} = constructProxy(${scopePath.join('.')}.${key}, {${proxyOut.join(', ')}});
203+
let ${varOutName} = constructProxy(${proxyName}, {${proxyOut.join(', ')}});
195204
`
196205
// If we're at the top level, we need to add the window and globalThis variables (Eg: let navigator = parentScope_navigator)
197206
if (scopePath.length === 1) {

0 commit comments

Comments
 (0)