Skip to content

Commit f308fe7

Browse files
committed
Fix codex bug
1 parent 3a9c789 commit f308fe7

File tree

19 files changed

+1535
-1562
lines changed

19 files changed

+1535
-1562
lines changed

easycoder/easycoder-min.js

Lines changed: 56 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

easycoder/easycoder.js

Lines changed: 53 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const EasyCoder_Compare = (program, value1, value2) => {
77
var v2 = val2.content;
88
if (v1 && val1.numeric) {
99
if (!val2.numeric) {
10-
v2 = (v2 === `` || typeof v2 === `undefined`) ? 0 : parseInt(v2);
10+
v2 = (v2 === `` || v2 === `-` || typeof v2 === `undefined`) ? 0 : parseInt(v2);
1111
}
1212
} else {
1313
if (v2 && val2.numeric) {
@@ -234,7 +234,7 @@ const EasyCoder_Compiler = {
234234
});
235235
this.continue = false;
236236
}
237-
// Add a 'stop'
237+
// else add a 'stop'
238238
else {
239239
this.addCommand({
240240
domain: `core`,
@@ -981,8 +981,9 @@ const EasyCoder_Core = {
981981

982982
run: program => {
983983
let parent = EasyCoder.scripts[program.parent];
984+
let unblocked = program.unblocked;
984985
program.exit();
985-
if (parent) {
986+
if (!unblocked && parent) {
986987
parent.run(parent.nextPc);
987988
parent.nextPc = 0;
988989
}
@@ -2067,6 +2068,7 @@ const EasyCoder_Core = {
20672068
if (parent) {
20682069
parent.run(parent.nextPc);
20692070
parent.nextPc = 0;
2071+
program.unblocked = true;
20702072
}
20712073
break;
20722074
case `setArray`:
@@ -2101,62 +2103,34 @@ const EasyCoder_Core = {
21012103
targetRecord.value[targetRecord.index].content = JSON.stringify(elements);
21022104
break;
21032105
case `setProperty`:
2104-
targetRecord = program.getSymbolRecord(command.target);
2105-
let targetValue = program.getValue(targetRecord.value[targetRecord.index]);
2106-
if (!targetValue) {
2107-
targetValue = `{}`;
2108-
}
2109-
// This is object whose property is being set
2110-
let targetJSON = JSON.parse(targetValue);
21112106
// This is the name of the property
21122107
const itemName = program.getValue(command.name);
21132108
// This is the value of the property
2114-
const itemValue = program.evaluate(command.value);
2115-
let content = itemValue.content;
2116-
if (itemValue) {
2117-
if (content.length >= 2 && [`[`, `{`].includes(content[0])) {
2118-
targetJSON[itemName] = JSON.parse(itemValue.content);
2119-
content = JSON.stringify(targetJSON);
2109+
let itemValue = program.getValue(command.value);
2110+
if (program.isJsonString(itemValue)) {
2111+
itemValue = JSON.parse(itemValue);
2112+
}
2113+
targetRecord = program.getSymbolRecord(command.target);
2114+
let targetValue = targetRecord.value[targetRecord.index];
2115+
// Get the existing JSON
2116+
if (!targetValue.numeric) {
2117+
let content = targetValue.content;
2118+
if (content === ``) {
2119+
content = {};
2120+
}
2121+
else if (program.isJsonString(content)) {
2122+
content = JSON.parse(content);
21202123
}
2124+
// Set the property
2125+
content[itemName] = itemValue;
2126+
// Put it back
2127+
content = JSON.stringify(content);
21212128
targetRecord.value[targetRecord.index] = {
21222129
type: `constant`,
21232130
numeric: false,
21242131
content
21252132
};
21262133
}
2127-
2128-
// let targetValue = program.getFormattedValue(targetRecord.value[targetRecord.index]);
2129-
// if (!targetValue) {
2130-
// targetValue = `{}`;
2131-
// }
2132-
// let targetJSON = ``;
2133-
// try {
2134-
// targetJSON = JSON.parse(targetValue);
2135-
// } catch (err) {
2136-
// program.runtimeError(command.lino, `Can't parse ${targetRecord.name}`);
2137-
// return 0;
2138-
// }
2139-
// const itemName = program.getValue(command.name);
2140-
// const itemValue = program.evaluate(command.value);
2141-
// let content = itemValue.content;
2142-
// if (itemValue) {
2143-
// if (content instanceof Array) {
2144-
// targetJSON[itemName] = content;
2145-
// } else if (itemValue.type === `boolean`) {
2146-
// targetJSON[itemName] = content;
2147-
// } else if (itemValue.numeric) {
2148-
// targetJSON[itemName] = content;
2149-
// } else if (content.length >= 2 && [`["`, `{"`].includes(content.substr(0, 2))) {
2150-
// targetJSON[itemName] = JSON.parse(itemValue.content);
2151-
// } else {
2152-
// targetJSON[itemName] = content.split(`"`).join(`\\"`);
2153-
// }
2154-
// targetRecord.value[targetRecord.index] = {
2155-
// type: `constant`,
2156-
// numeric: false,
2157-
// content: JSON.stringify(targetJSON)
2158-
// };
2159-
// }
21602134
break;
21612135
case `setPayload`:
21622136
program.getSymbolRecord(command.callback).payload = program.getValue(command.payload);
@@ -3500,8 +3474,12 @@ const EasyCoder_Core = {
35003474
case `not`:
35013475
return !program.getValue(condition.value);
35023476
case `moduleRunning`:
3503-
const running = program.getSymbolRecord(condition.name).program;
3504-
return condition.sense ? running : !running;
3477+
let moduleRecord = program.getSymbolRecord(condition.name);
3478+
if (EasyCoder.scripts.hasOwnProperty(moduleRecord.program) ) {
3479+
let p = EasyCoder.scripts[moduleRecord.program];
3480+
return condition.sense ? p.running : !p.running;
3481+
}
3482+
return !condition.sense;
35053483
case `includes`:
35063484
const value1 = JSON.parse(program.getValue(condition.value1));
35073485
const value2 = program.getValue(condition.value2);
@@ -3611,9 +3589,7 @@ const EasyCoder = {
36113589
if (v.type === `boolean`) {
36123590
return v.content ? `true` : `false`;
36133591
}
3614-
if (typeof v.content !==`undefined` && v.content.length >= 2
3615-
// && (v.content.substr(0, 2) === `{"` || v.content[0] === `[`)) {
3616-
&& [`[`, `{`].includes(v.content[0])) {
3592+
if (this.isJsonString(v.content)) {
36173593
try {
36183594
const parsed = JSON.parse(v.content);
36193595
return JSON.stringify(parsed, null, 2);
@@ -3680,6 +3656,15 @@ const EasyCoder = {
36803656
return typeof item === `undefined`;
36813657
},
36823658

3659+
isJsonString: function (str) {
3660+
try {
3661+
JSON.parse(str);
3662+
} catch (e) {
3663+
return false;
3664+
}
3665+
return true;
3666+
},
3667+
36833668
runScript: function (program) {
36843669
const command = program[program.pc];
36853670
const script = program.getValue(command.script);
@@ -3740,6 +3725,7 @@ const EasyCoder = {
37403725
program.domain = this.domain;
37413726
program.require = this.require;
37423727
program.isUndefined = this.isUndefined;
3728+
program.isJsonString = this.isJsonString;
37433729
program.checkPlugin = this.checkPlugin;
37443730
program.getPlugin = this.getPlugin;
37453731
program.addLocalPlugin = this.addLocalPlugin;
@@ -3752,6 +3738,7 @@ const EasyCoder = {
37523738
program.reportError = this.reportError;
37533739
program.register = this.register;
37543740
program.symbols = compiler.getSymbols();
3741+
program.unblocked = false;
37553742
program.encoding = `ec`;
37563743
program.popups = [];
37573744
program.stack = [];
@@ -4319,14 +4306,17 @@ const EasyCoder_Value = {
43194306
if (value) {
43204307
switch (encoding) {
43214308
case `ec`:
4322-
return value.replace(/'/g, `~sq~`)
4309+
return value.replace(/\n/g, `%0a`)
4310+
.replace(/\r/g, `%0d`)
43234311
.replace(/"/g, `~dq~`)
4324-
.replace(/\n/g, `%0a`)
4325-
.replace(/\r/g, `%0d`);
4312+
.replace(/'/g, `~sq~`)
4313+
.replace(/\\/g, `~bs~`);
43264314
case `url`:
43274315
return encodeURIComponent(value.replace(/\s/g, `+`));
43284316
case `sanitize`:
43294317
return value.normalize(`NFD`).replace(/[\u0300-\u036f]/g, ``);
4318+
default:
4319+
return value;
43304320
}
43314321
}
43324322
return value;
@@ -4336,13 +4326,16 @@ const EasyCoder_Value = {
43364326
if (value) {
43374327
switch (encoding) {
43384328
case `ec`:
4339-
return value.replace(/~dq~/g, `"`)
4329+
return value.replace(/%0a/g, `\n`)
4330+
.replace(/%0d/g, `\r`)
4331+
.replace(/~dq~/g, `"`)
43404332
.replace(/~sq~/g, `'`)
4341-
.replace(/%0a/g, `\n`)
4342-
.replace(/%0d/g, `\r`);
4333+
.replace(/~bs~/g, `\\`);
43434334
case `url`:
43444335
const decoded = decodeURIComponent(value);
43454336
return decoded.replace(/\+/g, ` `);
4337+
default:
4338+
return value;
43464339
}
43474340
}
43484341
return value;

js/easycoder/Compare.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const EasyCoder_Compare = (program, value1, value2) => {
77
var v2 = val2.content;
88
if (v1 && val1.numeric) {
99
if (!val2.numeric) {
10-
v2 = (v2 === `` || typeof v2 === `undefined`) ? 0 : parseInt(v2);
10+
v2 = (v2 === `` || v2 === `-` || typeof v2 === `undefined`) ? 0 : parseInt(v2);
1111
}
1212
} else {
1313
if (v2 && val2.numeric) {

js/easycoder/Compile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ const EasyCoder_Compiler = {
204204
});
205205
this.continue = false;
206206
}
207-
// Add a 'stop'
207+
// else add a 'stop'
208208
else {
209209
this.addCommand({
210210
domain: `core`,

js/easycoder/Core.js

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,9 @@ const EasyCoder_Core = {
585585

586586
run: program => {
587587
let parent = EasyCoder.scripts[program.parent];
588+
let unblocked = program.unblocked;
588589
program.exit();
589-
if (parent) {
590+
if (!unblocked && parent) {
590591
parent.run(parent.nextPc);
591592
parent.nextPc = 0;
592593
}
@@ -1671,6 +1672,7 @@ const EasyCoder_Core = {
16711672
if (parent) {
16721673
parent.run(parent.nextPc);
16731674
parent.nextPc = 0;
1675+
program.unblocked = true;
16741676
}
16751677
break;
16761678
case `setArray`:
@@ -1705,65 +1707,34 @@ const EasyCoder_Core = {
17051707
targetRecord.value[targetRecord.index].content = JSON.stringify(elements);
17061708
break;
17071709
case `setProperty`:
1708-
targetRecord = program.getSymbolRecord(command.target);
1709-
let targetValue = program.getValue(targetRecord.value[targetRecord.index]);
1710-
if (!targetValue) {
1711-
targetValue = `{}`;
1712-
}
1713-
// This is object whose property is being set
1714-
let targetJSON = targetValue;
1715-
if (program.isJsonString(targetValue)) {
1716-
targetJSON = JSON.parse(targetValue);
1717-
}
17181710
// This is the name of the property
17191711
const itemName = program.getValue(command.name);
17201712
// This is the value of the property
1721-
const itemValue = program.evaluate(command.value);
1722-
let content = itemValue.content;
1723-
if (itemValue) {
1724-
if (program.isJsonString(itemValue.content)) {
1725-
targetJSON[itemName] = JSON.parse(itemValue.content);
1726-
content = JSON.stringify(targetJSON);
1713+
let itemValue = program.getValue(command.value);
1714+
if (program.isJsonString(itemValue)) {
1715+
itemValue = JSON.parse(itemValue);
1716+
}
1717+
targetRecord = program.getSymbolRecord(command.target);
1718+
let targetValue = targetRecord.value[targetRecord.index];
1719+
// Get the existing JSON
1720+
if (!targetValue.numeric) {
1721+
let content = targetValue.content;
1722+
if (content === ``) {
1723+
content = {};
1724+
}
1725+
else if (program.isJsonString(content)) {
1726+
content = JSON.parse(content);
17271727
}
1728+
// Set the property
1729+
content[itemName] = itemValue;
1730+
// Put it back
1731+
content = JSON.stringify(content);
17281732
targetRecord.value[targetRecord.index] = {
17291733
type: `constant`,
17301734
numeric: false,
17311735
content
17321736
};
17331737
}
1734-
1735-
// let targetValue = program.getFormattedValue(targetRecord.value[targetRecord.index]);
1736-
// if (!targetValue) {
1737-
// targetValue = `{}`;
1738-
// }
1739-
// let targetJSON = ``;
1740-
// try {
1741-
// targetJSON = JSON.parse(targetValue);
1742-
// } catch (err) {
1743-
// program.runtimeError(command.lino, `Can't parse ${targetRecord.name}`);
1744-
// return 0;
1745-
// }
1746-
// const itemName = program.getValue(command.name);
1747-
// const itemValue = program.evaluate(command.value);
1748-
// let content = itemValue.content;
1749-
// if (itemValue) {
1750-
// if (content instanceof Array) {
1751-
// targetJSON[itemName] = content;
1752-
// } else if (itemValue.type === `boolean`) {
1753-
// targetJSON[itemName] = content;
1754-
// } else if (itemValue.numeric) {
1755-
// targetJSON[itemName] = content;
1756-
// } else if (content.length >= 2 && [`["`, `{"`].includes(content.substr(0, 2))) {
1757-
// targetJSON[itemName] = JSON.parse(itemValue.content);
1758-
// } else {
1759-
// targetJSON[itemName] = content.split(`"`).join(`\\"`);
1760-
// }
1761-
// targetRecord.value[targetRecord.index] = {
1762-
// type: `constant`,
1763-
// numeric: false,
1764-
// content: JSON.stringify(targetJSON)
1765-
// };
1766-
// }
17671738
break;
17681739
case `setPayload`:
17691740
program.getSymbolRecord(command.callback).payload = program.getValue(command.payload);
@@ -3107,8 +3078,12 @@ const EasyCoder_Core = {
31073078
case `not`:
31083079
return !program.getValue(condition.value);
31093080
case `moduleRunning`:
3110-
const running = program.getSymbolRecord(condition.name).program;
3111-
return condition.sense ? running : !running;
3081+
let moduleRecord = program.getSymbolRecord(condition.name);
3082+
if (EasyCoder.scripts.hasOwnProperty(moduleRecord.program) ) {
3083+
let p = EasyCoder.scripts[moduleRecord.program];
3084+
return condition.sense ? p.running : !p.running;
3085+
}
3086+
return !condition.sense;
31123087
case `includes`:
31133088
const value1 = JSON.parse(program.getValue(condition.value1));
31143089
const value2 = program.getValue(condition.value2);

js/easycoder/Main.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ const EasyCoder = {
9898
if (v.type === `boolean`) {
9999
return v.content ? `true` : `false`;
100100
}
101-
if (typeof v.content !==`undefined` && v.content.length >= 2
102-
// && (v.content.substr(0, 2) === `{"` || v.content[0] === `[`)) {
103-
&& [`[`, `{`].includes(v.content[0])) {
101+
if (this.isJsonString(v.content)) {
104102
try {
105103
const parsed = JSON.parse(v.content);
106104
return JSON.stringify(parsed, null, 2);
@@ -249,6 +247,7 @@ const EasyCoder = {
249247
program.reportError = this.reportError;
250248
program.register = this.register;
251249
program.symbols = compiler.getSymbols();
250+
program.unblocked = false;
252251
program.encoding = `ec`;
253252
program.popups = [];
254253
program.stack = [];

0 commit comments

Comments
 (0)