Skip to content

Instantly share code, notes, and snippets.

@canalun
Created June 14, 2024 08:35
Show Gist options
  • Save canalun/67164839b74ca810e6c549d6646c6cfd to your computer and use it in GitHub Desktop.
Save canalun/67164839b74ca810e6c549d6646c6cfd to your computer and use it in GitHub Desktop.
list all changeable built-in names
const changeableBuiltInNames = [];
getChangeableBuiltInNamesRecursively(globalThis, "globalThis");
function getChangeableBuiltInNamesRecursively(obj, accessors) {
if (
obj === undefined ||
obj === null ||
obj instanceof String ||
obj.__proto__ === String.prototype ||
(accessors !== "globalThis" && obj === globalThis)
) {
return;
}
const keys = Reflect.ownKeys(Object.getOwnPropertyDescriptors(obj));
for (let i = 0; i < keys.length; i++) {
const descName = keys[i];
// Skip to avoid infinite loop.
if (
isGettingConstructorOfPrototype(accessors, descName) ||
isGettingNameOrLengthOfFunction(obj, descName)
) {
continue;
}
const desc = Object.getOwnPropertyDescriptor(obj, descName);
console.log(`${accessors}.${descName.toString()}`);
if (!desc || (!desc.value && !desc.get)) {
continue;
}
// If the property is either configurable or writable,
// add it to changeableBuiltInNames
if (desc.configurable || desc.writable) {
changeableBuiltInNames.push(`${accessors}.${descName.toString()}`);
console.log(`${accessors}.${descName.toString()} pushed`);
}
if (descName.toString().startsWith("Symbol(")) {
continue;
}
getChangeableBuiltInNamesRecursively(
desc.value,
`${accessors}.${descName.toString()}`
);
}
};
function isGettingConstructorOfPrototype(accessors, descName) {
return (
accessors.split(".").at(-1) === "prototype" && descName === "constructor"
);
};
function isGettingNameOrLengthOfFunction(obj, descName) {
return (
obj instanceof Function && (descName === "name" || descName === "length")
);
};
//// Node: output the result to result.txt
// const fs = require("fs");
// const path = require("path");
// fs.writeFileSync(
// path.join(__dirname, "result.txt"),
// changeableBuiltInNames.join("\n")
// );
//// Browser: output the result to console
// console.log(changeableBuiltInNames.join("\n"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment