Skip to content

Commit 0b7a6c6

Browse files
authored
fix stringify issue with parse block (#495)
Some non-null prototype objects were being made.
1 parent b775bcd commit 0b7a6c6

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

static/extensions/DogeisCut/dogeiscutObject.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
// TODO:
1212
// turn keys values and entires of into a single block with a dropdown
13+
// setPrototypeOf is slow, create new objects where we can instead
14+
// maybe rewrite this entire thing cause a lot of code is leftover from when i wanted recursion and didnt know about null prototypes, and i overcomplicated a lot of stuff
1315

1416
(function(Scratch) {
1517
'use strict';
@@ -84,7 +86,14 @@
8486

8587
if (typeof x === "string") {
8688
try {
87-
const parsed = JSON.parse(x)
89+
const parsed = JSON.parse(x, (key, value, context) => {
90+
if (typeof value === "object") {
91+
if (Object.getPrototypeOf(value) === defaultPrototype) {
92+
return Object.setPrototypeOf(value, null) // slow but im too lazy to do this proper right now
93+
}
94+
}
95+
return value
96+
})
8897
if (isArray(parsed)) {
8998
return new ObjectType(Object.fromEntries(parsed.map((v,i)=>[i+1,v])))
9099
}
@@ -273,7 +282,8 @@
273282
toJSON() {
274283
return Object.fromEntries(
275284
Object.entries(this.object).map(([key, value]) => {
276-
if (typeof value === "object" && value !== null) {
285+
let proto = Object.getPrototypeOf(value)
286+
if (typeof value === "object" && value !== null && (proto !== null && proto !== defaultPrototype /* < lazy fix */)) {
277287
if (typeof value.toJSON === "function") return [key, value.toJSON()]
278288
if (typeof value.toString === "function") return [key, value.toString()]
279289
return [key, JSON.stringify(value)]

0 commit comments

Comments
 (0)