Skip to content

Commit

Permalink
Implemented property shorthand
Browse files Browse the repository at this point in the history
As wished for in #1302 (comment)
  • Loading branch information
mariusGundersen committed May 25, 2022
1 parent 72cfe26 commit 27e1703
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README_BuildProcess.md
Expand Up @@ -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)


Expand Down
18 changes: 18 additions & 0 deletions src/jsparse.c
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 8 additions & 7 deletions src/jsutils.h
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 27e1703

Please sign in to comment.