mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Fix regression with functions at end of input line
This commit is contained in:
parent
8e98a1010b
commit
c0bc852ff0
@ -34,17 +34,18 @@ static void NO_INLINE jslGetNextCh(JsLex *lex) {
|
||||
lex->currCh = jslNextCh(lex);
|
||||
|
||||
lex->it.charIdx++;
|
||||
lex->it.index++;
|
||||
if (lex->it.charIdx >= lex->it.charsInVar) {
|
||||
lex->it.charIdx -= lex->it.charsInVar;
|
||||
if (lex->it.var && lex->it.var->lastChild) {
|
||||
JsVar *next = jsvLock(lex->it.var->lastChild);
|
||||
jsvUnLock(lex->it.var);
|
||||
lex->it.var = next;
|
||||
lex->it.varIndex += lex->it.charsInVar;
|
||||
lex->it.charsInVar = jsvGetCharactersInVar(lex->it.var);
|
||||
} else {
|
||||
jsvUnLock(lex->it.var);
|
||||
lex->it.var = 0;
|
||||
lex->it.varIndex += lex->it.charsInVar;
|
||||
lex->it.charsInVar = 0;
|
||||
}
|
||||
}
|
||||
@ -103,7 +104,7 @@ void jslGetNextToken(JsLex *lex) {
|
||||
return;
|
||||
}
|
||||
// record beginning of this token
|
||||
lex->tokenLastStart = lex->tokenStart.it.index-1;
|
||||
lex->tokenLastStart = jsvStringIteratorGetIndex(&lex->tokenStart.it) - 1;
|
||||
/* we don't lock here, because we know that the string itself will be locked
|
||||
* because of lex->sourceVar */
|
||||
lex->tokenStart.it = lex->it;
|
||||
@ -527,7 +528,7 @@ bool jslMatch(JsLex *lex, int expected_tk) {
|
||||
strncpy(&buf[bufpos], " expected ", JS_ERROR_BUF_SIZE-bufpos);
|
||||
bufpos = strlen(buf);
|
||||
jslTokenAsString(expected_tk, &buf[bufpos], JS_ERROR_BUF_SIZE-bufpos);
|
||||
jsErrorAt(buf, lex, lex->tokenStart.it.index);
|
||||
jsErrorAt(buf, lex, jsvStringIteratorGetIndex(&lex->tokenStart.it));
|
||||
// Sod it, skip this token anyway - stops us looping
|
||||
jslGetNextToken(lex);
|
||||
return false;
|
||||
@ -544,7 +545,7 @@ JsVar *jslNewFromLexer(struct JsLex *lex, JslCharPos *charFrom, size_t charTo) {
|
||||
}
|
||||
|
||||
//jsvAppendStringVar(var, lex->sourceVar, charFrom->it->index, (int)(charTo-charFrom));
|
||||
size_t maxLength = charTo - charFrom->it.index;
|
||||
size_t maxLength = charTo - jsvStringIteratorGetIndex(&charFrom->it);
|
||||
JsVar *block = jsvLockAgain(var);
|
||||
block->varData.str[0] = charFrom->currCh;
|
||||
size_t blockChars = 1;
|
||||
|
||||
@ -2271,9 +2271,10 @@ void jsvStringIteratorNew(JsvStringIterator *it, JsVar *str, int startIdx) {
|
||||
it->var = jsvLockAgain(str);
|
||||
it->charsInVar = jsvGetCharactersInVar(str);
|
||||
it->charIdx = (size_t)startIdx;
|
||||
it->index = (size_t)startIdx;
|
||||
it->varIndex = 0;
|
||||
while (it->charIdx>0 && it->charIdx >= it->charsInVar) {
|
||||
it->charIdx -= it->charsInVar;
|
||||
it->varIndex += it->charsInVar;
|
||||
if (it->var) {
|
||||
if (it->var->lastChild) {
|
||||
JsVar *next = jsvLock(it->var->lastChild);
|
||||
@ -2297,10 +2298,10 @@ void jsvStringIteratorNext(JsvStringIterator *it) {
|
||||
void jsvStringIteratorGotoEnd(JsvStringIterator *it) {
|
||||
assert(it->var);
|
||||
while (it->var->lastChild) {
|
||||
it->index += it->charsInVar;
|
||||
JsVar *next = jsvLock(it->var->lastChild);
|
||||
jsvUnLock(it->var);
|
||||
it->var = next;
|
||||
it->varIndex += it->charsInVar;
|
||||
it->charsInVar = jsvGetCharactersInVar(it->var);
|
||||
}
|
||||
if (it->charsInVar) it->charIdx = it->charsInVar-1;
|
||||
@ -2312,10 +2313,10 @@ void jsvStringIteratorAppend(JsvStringIterator *it, char ch) {
|
||||
if (it->charsInVar>0) {
|
||||
assert(it->charIdx+1 == it->charsInVar /* check at end */);
|
||||
it->charIdx++;
|
||||
it->index++;
|
||||
} else
|
||||
assert(it->charIdx == 0);
|
||||
if (it->charIdx >= jsvGetMaxCharactersInVar(it->var)) {
|
||||
it->varIndex += jsvGetMaxCharactersInVar(it->var);
|
||||
assert(!it->var->lastChild);
|
||||
JsVar *next = jsvNewWithFlags(JSV_STRING_EXT);
|
||||
if (!next) return; // out of memory
|
||||
|
||||
@ -416,7 +416,7 @@ bool jsvIsInternalObjectKey(JsVar *v);
|
||||
typedef struct JsvStringIterator {
|
||||
size_t charIdx; ///< index of character in var
|
||||
size_t charsInVar; ///< total characters in var
|
||||
size_t index; ///< index in string
|
||||
size_t varIndex; ///< index in string of the start of this var
|
||||
JsVar *var; ///< current StringExt we're looking at
|
||||
} JsvStringIterator;
|
||||
|
||||
@ -458,7 +458,7 @@ static inline void jsvStringIteratorSetChar(JsvStringIterator *it, char c) {
|
||||
|
||||
/// Gets the current index in the string
|
||||
static inline size_t jsvStringIteratorGetIndex(JsvStringIterator *it) {
|
||||
return it->index;
|
||||
return it->varIndex + it->charIdx;
|
||||
}
|
||||
|
||||
/// Move to next character
|
||||
@ -467,17 +467,18 @@ void jsvStringIteratorNext(JsvStringIterator *it);
|
||||
/// Move to next character (this one is inlined where speed is needed)
|
||||
static inline void jsvStringIteratorNextInline(JsvStringIterator *it) {
|
||||
it->charIdx++;
|
||||
it->index++;
|
||||
if (it->charIdx >= it->charsInVar) {
|
||||
it->charIdx -= it->charsInVar;
|
||||
if (it->var && it->var->lastChild) {
|
||||
JsVar *next = jsvLock(it->var->lastChild);
|
||||
jsvUnLock(it->var);
|
||||
it->var = next;
|
||||
it->varIndex += it->charsInVar;
|
||||
it->charsInVar = jsvGetCharactersInVar(it->var);
|
||||
} else {
|
||||
jsvUnLock(it->var);
|
||||
it->var = 0;
|
||||
it->varIndex += it->charsInVar;
|
||||
it->charsInVar = 0;
|
||||
}
|
||||
}
|
||||
|
||||
6
tests/test_function_decl.js
Normal file
6
tests/test_function_decl.js
Normal file
@ -0,0 +1,6 @@
|
||||
// issue with function decls when the last token is right at the end of the line
|
||||
|
||||
var a=eval("function a() {\naaaaaaaaa\nssssssss\n}")
|
||||
var r=a.toString();
|
||||
|
||||
result = r=="function () {\naaaaaaaaa\nssssssss\n}";
|
||||
Loading…
x
Reference in New Issue
Block a user