Skip to content

Commit

Permalink
refactor(api/internal/monkeypatch.js): improve installed native perse…
Browse files Browse the repository at this point in the history
…rvation
  • Loading branch information
jwerle committed Oct 11, 2023
1 parent 2609596 commit 75252c4
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions api/internal/monkeypatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,43 @@ export function init () {
}

const actualName = name.split('.').slice(-1)[0]
let nativeImplementation = null

if (typeof prefix === 'string') {
name = `${prefix}.${name}`
}

nativeImplementation = target[actualName] || null
if (typeof target[actualName] === 'object' && target[actualName] !== null) {
for (const key in implementation) {
const nativeImplementation = target[actualName][key] || null
// let this fail, the environment implementation may not be writable
try {
target[actualName][key] = implementation[key]
} catch {}

if (nativeImplementation !== null) {
const nativeName = ['_', 'native', ...name.split('.'), key].join('_')
Object.defineProperty(globalThis, nativeName, {
enumerable: false,
configurable: false,
value: nativeImplementation
})
}
}
} else {
const nativeImplementation = target[actualName] || null
// let this fail, the environment implementation may not be writable
try {
target[actualName] = implementation
} catch {}
}

if (nativeImplementation !== null) {
const nativeName = ['_', 'native', ...name.split('.')].join('_')
Object.defineProperty(globalThis, nativeName, {
enumerable: false,
configurable: false,
value: nativeImplementation
})
if (nativeImplementation !== null) {
const nativeName = ['_', 'native', ...name.split('.')].join('_')
Object.defineProperty(globalThis, nativeName, {
enumerable: false,
configurable: false,
value: nativeImplementation
})
}
}
}

Expand Down

0 comments on commit 75252c4

Please sign in to comment.