diff --git a/README_BuildProcess.md b/README_BuildProcess.md index c59289bbda..04561ea177 100644 --- a/README_BuildProcess.md +++ b/README_BuildProcess.md @@ -141,7 +141,8 @@ These are set automatically when `SAVE_ON_FLASH` is set (see `jsutils.h`) * `SAVE_ON_FLASH_MATH` - Remove some less-used Maths functions that use a bunch of Flash memory * `ESPR_NO_GET_SET` - No Getter/setter functionality * `ESPR_NO_OBJECT_METHODS` - No methods in objects like `{method() { ... }}` -* `ESPR_NO_LINE_NUMBERS` - disable storing and reporting of Line Numbers. Usually these take 1 var per function, but if we're executing a function from flash we can just work it out from the file when needed +* `ESPR_NO_PROPERTY_SHORTHAND` - No property shorthand in objects like `{a}` +* `ESPR_NO_LINE_NUMBERS` - disable storing and reporting of Line Numbers. Usually these take 1 var per function, but if we're executing a function from flash we can just work it out from the file when needed * `ESPR_NO_LET_SCOPING` - don't create scopes for `let` (treat it like `var`, which was the 2v13 and earlier behaviour) diff --git a/src/jsparse.c b/src/jsparse.c index f2add51f83..32ab3bf1ca 100644 --- a/src/jsparse.c +++ b/src/jsparse.c @@ -1259,8 +1259,12 @@ NO_INLINE JsVar *jspeFactorObject() { JSP_MATCH_WITH_RETURN('{', contents); while (!JSP_SHOULDNT_PARSE && lex->tk != '}') { JsVar *varName = 0; +#ifndef ESPR_NO_PROPERTY_SHORTHAND + bool isIdentifier = 0; +#endif // we only allow strings or IDs on the left hand side of an initialisation if (jslIsIDOrReservedWord()) { + isIdentifier = lex->tk == LEX_ID; if (JSP_SHOULD_EXECUTE) varName = jslGetTokenValueAsVar(); jslGetNextToken(); // skip over current token @@ -1294,6 +1298,20 @@ NO_INLINE JsVar *jspeFactorObject() { jsvUnLock2(jsvSetValueOfName(contentsName, method), method); } } else +#endif +#ifndef ESPR_NO_PROPERTY_SHORTHAND + if (isIdentifier && (lex->tk == ',' || lex->tk == '}') && jsvIsString(varName)) { + if (JSP_SHOULD_EXECUTE) { + varName = jsvAsArrayIndexAndUnLock(varName); + JsVar *contentsName = jsvFindChildFromVar(contents, varName, true); + if (contentsName) { + char buf[JSLEX_MAX_TOKEN_LENGTH]; + jsvGetString(varName, buf, JSLEX_MAX_TOKEN_LENGTH); + JsVar *value = jsvSkipNameAndUnLock(jspGetNamedVariable(buf)); // value can be 0 (could be undefined!) + jsvUnLock2(jsvSetValueOfName(contentsName, value), value); + } + } + } else #endif { JSP_MATCH_WITH_CLEANUP_AND_RETURN(':', jsvUnLock(varName), contents); diff --git a/src/jsutils.h b/src/jsutils.h index 094dc29e1c..9cb8d733c1 100755 --- a/src/jsutils.h +++ b/src/jsutils.h @@ -40,6 +40,7 @@ #ifdef SAVE_ON_FLASH #define SAVE_ON_FLASH_MATH 1 #define ESPR_NO_OBJECT_METHODS 1 +#define ESPR_NO_PROPERTY_SHORTHAND 1 #define ESPR_NO_GET_SET 1 #define ESPR_NO_LINE_NUMBERS 1 #define ESPR_NO_LET_SCOPING 1 @@ -409,7 +410,7 @@ typedef int64_t JsSysTime; #define UNALIGNED_UINT16(addr) ((((uint16_t)*((uint8_t*)(addr)+1)) << 8) | (*(uint8_t*)(addr))) #else #define UNALIGNED_UINT16(addr) (*(uint16_t*)addr) -#endif +#endif bool isWhitespace(char ch); bool isNumeric(char ch); @@ -458,15 +459,15 @@ void jsAssertFail(const char *file, int line, const char *expr); /* #if defined(DEBUG) || __FILE__ == DEBUG_FILE - #define jsDebug(dbg_type, format, ...) jsiConsolePrintf("[" __FILE__ "]:" format, ## __VA_ARGS__) - #else - #define jsDebug(dbg_type, format, ...) do { } while(0) + #define jsDebug(dbg_type, format, ...) jsiConsolePrintf("[" __FILE__ "]:" format, ## __VA_ARGS__) + #else + #define jsDebug(dbg_type, format, ...) do { } while(0) #endif */ #if (defined DEBUG ) || ( defined __FILE__ == DEBUG_FILE) - #define jsDebug(dbg_type, format, ...) jsiConsolePrintf("[" __FILE__ "]:" format, ## __VA_ARGS__) -#else - #define jsDebug(dbg_type, format, ...) do { } while(0) + #define jsDebug(dbg_type, format, ...) jsiConsolePrintf("[" __FILE__ "]:" format, ## __VA_ARGS__) +#else + #define jsDebug(dbg_type, format, ...) do { } while(0) #endif #ifndef USE_FLASH_MEMORY