@@ -7,7 +7,7 @@ const EasyCoder_Compare = (program, value1, value2) => {
7
7
var v2 = val2 . content ;
8
8
if ( v1 && val1 . numeric ) {
9
9
if ( ! val2 . numeric ) {
10
- v2 = ( v2 === `` || typeof v2 === `undefined` ) ? 0 : parseInt ( v2 ) ;
10
+ v2 = ( v2 === `` || v2 === `-` || typeof v2 === `undefined` ) ? 0 : parseInt ( v2 ) ;
11
11
}
12
12
} else {
13
13
if ( v2 && val2 . numeric ) {
@@ -234,7 +234,7 @@ const EasyCoder_Compiler = {
234
234
} ) ;
235
235
this . continue = false ;
236
236
}
237
- // Add a 'stop'
237
+ // else add a 'stop'
238
238
else {
239
239
this . addCommand ( {
240
240
domain : `core` ,
@@ -981,8 +981,9 @@ const EasyCoder_Core = {
981
981
982
982
run : program => {
983
983
let parent = EasyCoder . scripts [ program . parent ] ;
984
+ let unblocked = program . unblocked ;
984
985
program . exit ( ) ;
985
- if ( parent ) {
986
+ if ( ! unblocked && parent ) {
986
987
parent . run ( parent . nextPc ) ;
987
988
parent . nextPc = 0 ;
988
989
}
@@ -2067,6 +2068,7 @@ const EasyCoder_Core = {
2067
2068
if ( parent ) {
2068
2069
parent . run ( parent . nextPc ) ;
2069
2070
parent . nextPc = 0 ;
2071
+ program . unblocked = true ;
2070
2072
}
2071
2073
break ;
2072
2074
case `setArray` :
@@ -2101,62 +2103,34 @@ const EasyCoder_Core = {
2101
2103
targetRecord . value [ targetRecord . index ] . content = JSON . stringify ( elements ) ;
2102
2104
break ;
2103
2105
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 ) ;
2111
2106
// This is the name of the property
2112
2107
const itemName = program . getValue ( command . name ) ;
2113
2108
// 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 ) ;
2120
2123
}
2124
+ // Set the property
2125
+ content [ itemName ] = itemValue ;
2126
+ // Put it back
2127
+ content = JSON . stringify ( content ) ;
2121
2128
targetRecord . value [ targetRecord . index ] = {
2122
2129
type : `constant` ,
2123
2130
numeric : false ,
2124
2131
content
2125
2132
} ;
2126
2133
}
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
- // }
2160
2134
break ;
2161
2135
case `setPayload` :
2162
2136
program . getSymbolRecord ( command . callback ) . payload = program . getValue ( command . payload ) ;
@@ -3500,8 +3474,12 @@ const EasyCoder_Core = {
3500
3474
case `not` :
3501
3475
return ! program . getValue ( condition . value ) ;
3502
3476
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 ;
3505
3483
case `includes` :
3506
3484
const value1 = JSON . parse ( program . getValue ( condition . value1 ) ) ;
3507
3485
const value2 = program . getValue ( condition . value2 ) ;
@@ -3611,9 +3589,7 @@ const EasyCoder = {
3611
3589
if ( v . type === `boolean` ) {
3612
3590
return v . content ? `true` : `false` ;
3613
3591
}
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 ) ) {
3617
3593
try {
3618
3594
const parsed = JSON . parse ( v . content ) ;
3619
3595
return JSON . stringify ( parsed , null , 2 ) ;
@@ -3680,6 +3656,15 @@ const EasyCoder = {
3680
3656
return typeof item === `undefined` ;
3681
3657
} ,
3682
3658
3659
+ isJsonString : function ( str ) {
3660
+ try {
3661
+ JSON . parse ( str ) ;
3662
+ } catch ( e ) {
3663
+ return false ;
3664
+ }
3665
+ return true ;
3666
+ } ,
3667
+
3683
3668
runScript : function ( program ) {
3684
3669
const command = program [ program . pc ] ;
3685
3670
const script = program . getValue ( command . script ) ;
@@ -3740,6 +3725,7 @@ const EasyCoder = {
3740
3725
program . domain = this . domain ;
3741
3726
program . require = this . require ;
3742
3727
program . isUndefined = this . isUndefined ;
3728
+ program . isJsonString = this . isJsonString ;
3743
3729
program . checkPlugin = this . checkPlugin ;
3744
3730
program . getPlugin = this . getPlugin ;
3745
3731
program . addLocalPlugin = this . addLocalPlugin ;
@@ -3752,6 +3738,7 @@ const EasyCoder = {
3752
3738
program . reportError = this . reportError ;
3753
3739
program . register = this . register ;
3754
3740
program . symbols = compiler . getSymbols ( ) ;
3741
+ program . unblocked = false ;
3755
3742
program . encoding = `ec` ;
3756
3743
program . popups = [ ] ;
3757
3744
program . stack = [ ] ;
@@ -4319,14 +4306,17 @@ const EasyCoder_Value = {
4319
4306
if ( value ) {
4320
4307
switch ( encoding ) {
4321
4308
case `ec` :
4322
- return value . replace ( / ' / g, `~sq~` )
4309
+ return value . replace ( / \n / g, `%0a` )
4310
+ . replace ( / \r / g, `%0d` )
4323
4311
. replace ( / " / g, `~dq~` )
4324
- . replace ( / \n / g, `%0a ` )
4325
- . replace ( / \r / g, `%0d ` ) ;
4312
+ . replace ( / ' / g, `~sq~ ` )
4313
+ . replace ( / \\ / g, `~bs~ ` ) ;
4326
4314
case `url` :
4327
4315
return encodeURIComponent ( value . replace ( / \s / g, `+` ) ) ;
4328
4316
case `sanitize` :
4329
4317
return value . normalize ( `NFD` ) . replace ( / [ \u0300 - \u036f ] / g, `` ) ;
4318
+ default :
4319
+ return value ;
4330
4320
}
4331
4321
}
4332
4322
return value ;
@@ -4336,13 +4326,16 @@ const EasyCoder_Value = {
4336
4326
if ( value ) {
4337
4327
switch ( encoding ) {
4338
4328
case `ec` :
4339
- return value . replace ( / ~ d q ~ / g, `"` )
4329
+ return value . replace ( / % 0 a / g, `\n` )
4330
+ . replace ( / % 0 d / g, `\r` )
4331
+ . replace ( / ~ d q ~ / g, `"` )
4340
4332
. replace ( / ~ s q ~ / g, `'` )
4341
- . replace ( / % 0 a / g, `\n` )
4342
- . replace ( / % 0 d / g, `\r` ) ;
4333
+ . replace ( / ~ b s ~ / g, `\\` ) ;
4343
4334
case `url` :
4344
4335
const decoded = decodeURIComponent ( value ) ;
4345
4336
return decoded . replace ( / \+ / g, ` ` ) ;
4337
+ default :
4338
+ return value ;
4346
4339
}
4347
4340
}
4348
4341
return value ;
0 commit comments