Created
February 1, 2018 08:36
-
-
Save SrTobi/7bed16a488df8537d973771053c1333f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nextId = 1 | |
var idToEntry = new Map() | |
var objectToId = new Map() | |
function record(obj) { | |
if (obj === null) { | |
return { | |
type: "null" | |
} | |
} | |
switch (typeof obj) { | |
case "string": | |
case "number": | |
case "boolean": | |
return { | |
type: typeof obj, | |
value: obj | |
} | |
case "undefined": | |
return { | |
type: "undefined" | |
} | |
default: | |
} | |
let id = objectToId.get(obj) | |
if (id === undefined) { | |
id = nextId++ | |
const entry = { id: id } | |
objectToId.set(obj, id) | |
const entries = [] | |
for (const prop of Object.getOwnPropertyNames(obj)) { | |
const desc = Object.getOwnPropertyDescriptor(obj, prop) | |
if (desc["value"] !== undefined) { | |
entries.push({ | |
name: prop, | |
value: record(desc.value), | |
writable: desc.writable, | |
configurable: desc.configurable, | |
enumerable: desc.enumerable | |
}) | |
} else { | |
entries.push({ | |
name: prop, | |
"get": record(desc.get), | |
"set": record(desc.set), | |
configurable: desc.configurable, | |
enumerable: desc.enumerable | |
}) | |
} | |
} | |
idToEntry.set(id, entries) | |
} | |
return { | |
type: "ref", | |
target: id | |
} | |
} | |
var me = { | |
test: 3 | |
} | |
me.me = me | |
Object.freeze(me) | |
record(me) | |
for (const [,entry] of idToEntry) { | |
console.log(entry) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment