Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsScript.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2026  Moddable Tech, Inc.
3
 *
4
 *   This file is part of the Moddable SDK Runtime.
5
 * 
6
 *   The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
 *   it under the terms of the GNU Lesser General Public License as published by
8
 *   the Free Software Foundation, either version 3 of the License, or
9
 *   (at your option) any later version.
10
 * 
11
 *   The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *   GNU Lesser General Public License for more details.
15
 * 
16
 *   You should have received a copy of the GNU Lesser General Public License
17
 *   along with the Moddable SDK Runtime.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * This file incorporates work covered by the following copyright and  
20
 * permission notice:  
21
 *
22
 *       Copyright (C) 2010-2016 Marvell International Ltd.
23
 *       Copyright (C) 2002-2010 Kinoma, Inc.
24
 *
25
 *       Licensed under the Apache License, Version 2.0 (the "License");
26
 *       you may not use this file except in compliance with the License.
27
 *       You may obtain a copy of the License at
28
 *
29
 *        http://www.apache.org/licenses/LICENSE-2.0
30
 *
31
 *       Unless required by applicable law or agreed to in writing, software
32
 *       distributed under the License is distributed on an "AS IS" BASIS,
33
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34
 *       See the License for the specific language governing permissions and
35
 *       limitations under the License.
36
 */
37
38
#include "xsScript.h"
39
extern void fxAbort(void* console, int status);
40
41
#if defined(__clang__) || defined (__GNUC__)
42
  __attribute__((no_sanitize_address))
43
#endif
44
void fxCheckParserStack(txParser* parser, txInteger line)
45
52.9M
{
46
52.9M
    char x;
47
52.9M
    char *stack = &x;
48
52.9M
    if (stack <= parser->stackLimit) {
49
4
      fxAbort(parser->console, XS_NATIVE_STACK_OVERFLOW_EXIT);
50
4
    }
51
52.9M
}
52
53
txString fxCombinePath(txParser* parser, txString base, txString name)
54
0
{
55
0
  txSize baseLength, nameLength;
56
0
  txString path;
57
0
  txString separator ;
58
#if mxWindows
59
  separator = name;
60
  while (*separator) {
61
    if (*separator == '/')
62
      *separator = '\\';
63
    separator++;
64
  }
65
  separator = strrchr(base, '\\');
66
#else
67
0
  separator = strrchr(base, '/');
68
0
#endif
69
0
  if (separator) {
70
0
    separator++;
71
0
    baseLength = mxPtrDiff(separator - base);
72
0
  }
73
0
  else
74
0
    baseLength = 0;
75
0
  nameLength = mxStringLength(name);
76
0
  path = fxNewParserChunk(parser, baseLength + nameLength + 1);
77
0
  if (baseLength)
78
0
    c_memcpy(path, base, baseLength);
79
0
  c_memcpy(path + baseLength, name, nameLength + 1);
80
0
  return path;
81
0
}
82
83
void fxDisposeParserChunks(txParser* parser)
84
1.33M
{
85
1.33M
  txParserChunk** address = &parser->first;
86
1.33M
  txParserChunk* block;
87
6.09M
  while ((block = *address)) {
88
4.75M
    *address = block->next;
89
4.75M
    c_free(block);
90
4.75M
  }
91
1.33M
}
92
93
void fxInitializeParser(txParser* parser, void* console, txSize bufferSize, txSize symbolModulo)
94
1.33M
{
95
1.33M
  c_memset(parser, 0, sizeof(txParser));
96
1.33M
  parser->first = C_NULL;
97
1.33M
  parser->console = console;
98
1.33M
  parser->stackLimit = fxCStackLimit();
99
  
100
1.33M
  parser->symbolModulo = symbolModulo;
101
1.33M
  parser->symbolTable = fxNewParserChunkClear(parser, parser->symbolModulo * sizeof(txSymbol*));
102
103
1.33M
  parser->emptyString = fxNewParserString(parser, "", 0);
104
105
1.33M
  parser->ObjectSymbol = fxNewParserSymbol(parser, "Object");
106
1.33M
  parser->__dirnameSymbol = fxNewParserSymbol(parser, "__dirname");
107
1.33M
  parser->__filenameSymbol = fxNewParserSymbol(parser, "__filename");
108
1.33M
  parser->__jsx__Symbol = fxNewParserSymbol(parser, "__jsx__");
109
1.33M
  parser->__proto__Symbol = fxNewParserSymbol(parser, "__proto__");
110
1.33M
  parser->allSymbol = fxNewParserSymbol(parser, "*");
111
1.33M
  parser->argsSymbol = fxNewParserSymbol(parser, "args");
112
1.33M
  parser->argumentsSymbol = fxNewParserSymbol(parser, "arguments");
113
1.33M
  parser->arrowSymbol = fxNewParserSymbol(parser, "=>");
114
1.33M
  parser->asSymbol = fxNewParserSymbol(parser, "as");
115
1.33M
  parser->asyncSymbol = fxNewParserSymbol(parser, "async");
116
1.33M
  parser->awaitSymbol = fxNewParserSymbol(parser, "await");
117
1.33M
  parser->callSymbol = fxNewParserSymbol(parser, "call");
118
1.33M
  parser->callerSymbol = fxNewParserSymbol(parser, "caller");
119
1.33M
  parser->constructorSymbol = fxNewParserSymbol(parser, "constructor");
120
1.33M
  parser->defaultSymbol = fxNewParserSymbol(parser, "default");
121
1.33M
  parser->doneSymbol = fxNewParserSymbol(parser, "done");
122
1.33M
  parser->evalSymbol = fxNewParserSymbol(parser, "eval");
123
1.33M
  parser->exportsSymbol = fxNewParserSymbol(parser, "exports");
124
1.33M
  parser->fillSymbol = fxNewParserSymbol(parser, "fill");
125
1.33M
  parser->freezeSymbol = fxNewParserSymbol(parser, "freeze");
126
1.33M
  parser->fromSymbol = fxNewParserSymbol(parser, "from");
127
1.33M
  parser->getSymbol = fxNewParserSymbol(parser, "get");
128
1.33M
  parser->idSymbol = fxNewParserSymbol(parser, "id");
129
1.33M
  parser->includeSymbol = fxNewParserSymbol(parser, "include");
130
1.33M
  parser->InfinitySymbol = fxNewParserSymbol(parser, "Infinity");
131
1.33M
  parser->jsonSymbol = fxNewParserSymbol(parser, "json");
132
1.33M
  parser->lengthSymbol = fxNewParserSymbol(parser, "length");
133
1.33M
  parser->letSymbol = fxNewParserSymbol(parser, "let");
134
1.33M
  parser->metaSymbol = fxNewParserSymbol(parser, "meta");
135
1.33M
  parser->moduleSymbol = fxNewParserSymbol(parser, "module");
136
1.33M
  parser->nameSymbol = fxNewParserSymbol(parser, "name");
137
1.33M
  parser->NaNSymbol = fxNewParserSymbol(parser, "NaN");
138
1.33M
  parser->NativeSymbol = fxNewParserSymbol(parser, "Native");
139
1.33M
  parser->nativeSymbol = fxNewParserSymbol(parser, "native");
140
1.33M
  parser->nextSymbol = fxNewParserSymbol(parser, "next");
141
1.33M
  parser->newTargetSymbol = fxNewParserSymbol(parser, "new.target");
142
1.33M
  parser->ofSymbol = fxNewParserSymbol(parser, "of");
143
1.33M
  parser->privateConstructorSymbol = fxNewParserSymbol(parser, "#constructor");
144
1.33M
  parser->prototypeSymbol = fxNewParserSymbol(parser, "prototype");
145
1.33M
  parser->RangeErrorSymbol = fxNewParserSymbol(parser, "RangeError");
146
1.33M
  parser->rawSymbol = fxNewParserSymbol(parser, "raw");
147
1.33M
  parser->returnSymbol = fxNewParserSymbol(parser, "return");
148
1.33M
  parser->setSymbol = fxNewParserSymbol(parser, "set");
149
1.33M
  parser->sliceSymbol = fxNewParserSymbol(parser, "slice");
150
1.33M
  parser->SyntaxErrorSymbol = fxNewParserSymbol(parser, "SyntaxError");
151
1.33M
  parser->staticSymbol = fxNewParserSymbol(parser, "static");
152
1.33M
  parser->StringSymbol = fxNewParserSymbol(parser, "String");
153
1.33M
  parser->targetSymbol = fxNewParserSymbol(parser, "target");
154
1.33M
  parser->thisSymbol = fxNewParserSymbol(parser, "this");
155
1.33M
  parser->throwSymbol = fxNewParserSymbol(parser, "throw");
156
1.33M
  parser->toStringSymbol = fxNewParserSymbol(parser, "toString");
157
1.33M
  parser->undefinedSymbol = fxNewParserSymbol(parser, "undefined");
158
1.33M
  parser->uriSymbol = fxNewParserSymbol(parser, "uri");
159
1.33M
  parser->usingSymbol = fxNewParserSymbol(parser, "using");
160
1.33M
  parser->valueSymbol = fxNewParserSymbol(parser, "value");
161
1.33M
  parser->withSymbol = fxNewParserSymbol(parser, "with");
162
1.33M
  parser->yieldSymbol = fxNewParserSymbol(parser, "yield");
163
  
164
1.33M
  parser->errorSymbol = NULL;
165
1.33M
  parser->reportError = fxVReportError;
166
1.33M
  parser->reportWarning = fxVReportWarning;
167
168
1.33M
  parser->buffer = fxNewParserChunk(parser, bufferSize);
169
1.33M
  parser->bufferSize = bufferSize;
170
1.33M
}
171
172
9.03M
#define kParserChunkSize (4096)
173
174
void* fxNewParserChunk(txParser* parser, txSize size)
175
186M
{
176
186M
  size = (size + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
177
186M
  if (size <= parser->chunkSize) {
178
181M
    void *result = parser->chunk;
179
181M
    parser->chunk += size;
180
181M
    parser->chunkSize -= size;
181
181M
    return result;
182
181M
  }
183
184
4.79M
  if (size > (txSize)(kParserChunkSize - sizeof(txParserChunk))) {
185
2.67M
    txParserChunk *block = c_malloc(sizeof(txParserChunk) + size);
186
2.67M
    if (!block)
187
0
      fxAbort(parser->console, XS_NOT_ENOUGH_MEMORY_EXIT);
188
2.67M
    parser->total += sizeof(txParserChunk) + size;
189
2.67M
    block->next = parser->first;
190
2.67M
    parser->first = block;
191
2.67M
    return block + 1;
192
2.67M
  }
193
194
2.11M
  txParserChunk *block = c_malloc(kParserChunkSize);
195
2.11M
  if (!block)
196
0
    fxAbort(parser->console, XS_NOT_ENOUGH_MEMORY_EXIT);
197
2.11M
  parser->total += kParserChunkSize;
198
2.11M
  block->next = parser->first;
199
2.11M
  parser->first = block;
200
2.11M
  parser->chunk = (txByte*)(block + 1) + size;
201
2.11M
  parser->chunkSize = kParserChunkSize - sizeof(txParserChunk) - size;
202
2.11M
  return block + 1;
203
4.79M
}
204
205
void* fxNewParserChunkClear(txParser* parser, txSize size)
206
70.0M
{
207
70.0M
  void* result = fxNewParserChunk(parser, size);
208
70.0M
    c_memset(result, 0, size);
209
70.0M
  return result;
210
70.0M
}
211
212
txString fxNewParserString(txParser* parser, txString buffer, txSize size)
213
85.9M
{
214
85.9M
  if (parser->buffer) { 
215
6.93M
    txString result = fxNewParserChunk(parser, size + 1);
216
6.93M
    c_memcpy(result, buffer, size);
217
6.93M
    result[size] = 0;
218
6.93M
    return result;
219
6.93M
  }
220
78.9M
  return buffer;
221
85.9M
}
222
223
txSymbol* fxNewParserSymbol(txParser* parser, txString theString)
224
84.9M
{
225
84.9M
  txString aString;
226
84.9M
  txSize aLength;
227
84.9M
  txU4 aSum;
228
84.9M
  txU4 aModulo;
229
84.9M
  txSymbol* aSymbol;
230
  
231
84.9M
  aString = theString;
232
84.9M
  aLength = 0;
233
84.9M
  aSum = 0;
234
561M
  while(*aString != 0) {
235
476M
    aLength++;
236
476M
    aSum = (aSum << 1) + *aString++;
237
476M
  }
238
84.9M
  aSum &= 0x7FFFFFFF;
239
84.9M
  aModulo = aSum % parser->symbolModulo;
240
84.9M
  aSymbol = parser->symbolTable[aModulo];
241
91.9M
  while (aSymbol != C_NULL) {
242
9.04M
    if (aSymbol->sum == aSum)
243
7.55M
      if (c_strcmp(aSymbol->string, theString) == 0)
244
2.12M
        break;
245
6.91M
    aSymbol = aSymbol->next;
246
6.91M
  }
247
84.9M
  if (aSymbol == C_NULL) {
248
82.8M
    aSymbol = fxNewParserChunk(parser, sizeof(txSymbol));
249
82.8M
    aSymbol->next = parser->symbolTable[aModulo];
250
82.8M
    aSymbol->ID = -1;
251
82.8M
    aSymbol->length = aLength + 1;
252
82.8M
    aSymbol->string = fxNewParserString(parser, theString, aLength);
253
82.8M
    aSymbol->sum = aSum;
254
82.8M
    aSymbol->usage = 0;
255
82.8M
    parser->symbolTable[aModulo] = aSymbol;
256
82.8M
  }
257
84.9M
  return aSymbol;
258
84.9M
}
259
260
void fxReportMemoryError(txParser* parser, txInteger line, txString theFormat, ...)
261
43
{
262
43
  c_va_list arguments;
263
43
  parser->error = C_ENOMEM;
264
43
  parser->errorCount++;
265
43
  c_va_start(arguments, theFormat);
266
43
    (*parser->reportError)(parser->console, parser->path ? parser->path->string : C_NULL, line, theFormat, arguments);
267
43
  c_va_end(arguments);
268
43
  if (parser->console) {
269
43
    parser->errorSymbol = parser->RangeErrorSymbol;
270
43
    if (parser->buffer != theFormat) {
271
43
      c_va_start(arguments, theFormat);
272
43
      c_vsnprintf(parser->buffer, parser->bufferSize, theFormat, arguments);
273
43
      c_va_end(arguments);
274
43
    }
275
43
    parser->errorMessage = fxNewParserString(parser, parser->buffer, mxStringLength(parser->buffer));
276
43
  }
277
43
  c_longjmp(parser->firstJump->jmp_buf, 1);
278
43
}
279
280
void fxReportParserError(txParser* parser, txInteger line, txString theFormat, ...)
281
893k
{
282
893k
  c_va_list arguments;
283
893k
  parser->error = C_EINVAL;
284
893k
  parser->errorCount++;
285
893k
  c_va_start(arguments, theFormat);
286
893k
    (*parser->reportError)(parser->console, parser->path ? parser->path->string : C_NULL, line, theFormat, arguments);
287
893k
  c_va_end(arguments);
288
893k
  if (parser->console) {
289
893k
    parser->errorSymbol = parser->SyntaxErrorSymbol;
290
893k
    if (parser->buffer != theFormat) {
291
893k
      c_va_start(arguments, theFormat);
292
893k
      c_vsnprintf(parser->buffer, parser->bufferSize, theFormat, arguments);
293
893k
      c_va_end(arguments);
294
893k
    }
295
893k
    parser->errorMessage = fxNewParserString(parser, parser->buffer, mxStringLength(parser->buffer));
296
893k
    c_longjmp(parser->firstJump->jmp_buf, 1);
297
893k
  }
298
893k
}
299
300
void fxTerminateParser(txParser* parser)
301
1.33M
{
302
1.33M
  fxDisposeParserChunks(parser);
303
1.33M
}
304
305
306
307
308
309
310
311
312
313
314
315
316