Skip to content

Commit 2f140e3

Browse files
authored
Add strtol to jerry-libc and make use of it in jerry-main (#1891)
As a side effect, refactor the variable types in `print_unhandled_exception` to reduce the overuse of sized integer types (generic `unsigned int` is better than `uint32_t` if there is no actual requirement on integer width). JerryScript-DCO-1.0-Signed-off-by: Akos Kiss [email protected]
1 parent b153475 commit 2f140e3

File tree

3 files changed

+87
-34
lines changed

3 files changed

+87
-34
lines changed

jerry-libc/include/stdlib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void __attribute__ ((noreturn)) exit (int);
3333
void __attribute__ ((noreturn)) abort (void);
3434
int rand (void);
3535
void srand (unsigned int);
36+
long int strtol (const char *, char **, int);
3637

3738
#ifdef __cplusplus
3839
}

jerry-libc/jerry-libc.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Jerry libc's common functions implementation
1818
*/
1919

20+
#include <assert.h>
2021
#include <stdio.h>
2122
#include <stdlib.h>
2223
#include <string.h>
@@ -280,3 +281,76 @@ srand (unsigned int seed) /**< new seed */
280281
libc_random_gen_state[2] =
281282
libc_random_gen_state[3] = seed;
282283
} /* srand */
284+
285+
/**
286+
* Convert a string to a long integer.
287+
*
288+
* The function first discards leading whitespace characters. Then takes an
289+
* optional sign followed by as many digits as possible and interprets them as a
290+
* numerical value. Additional characters after those that form the number are
291+
* ignored.
292+
*
293+
* Note:
294+
* If base is not 10, the behaviour is undefined.
295+
* If the value read is out-of-range, the behaviour is undefined.
296+
* The implementation never sets errno.
297+
*
298+
* @return the integer value of str.
299+
*/
300+
long int
301+
strtol (const char *nptr, /**< string representation of an integer number */
302+
char **endptr, /**< [out] the address of the first non-number character */
303+
int base) /**< numerical base or radix (MUST be 10) */
304+
{
305+
assert (base == 10);
306+
(void) base; /* Unused. */
307+
308+
const char *str = nptr;
309+
310+
/* Skip leading whitespaces. */
311+
while (*str == ' ' || *str == '\t' || *str == '\r' || *str == '\n')
312+
{
313+
str++;
314+
}
315+
316+
bool digits = false;
317+
bool positive = true;
318+
long int num = 0;
319+
320+
/* Process optional sign. */
321+
if (*str == '-')
322+
{
323+
positive = false;
324+
str++;
325+
}
326+
else if (*str == '+')
327+
{
328+
str++;
329+
}
330+
331+
/* Process base-10 digits. */
332+
while (*str >= '0' && *str <= '9')
333+
{
334+
num = num * 10 + (*str - '0');
335+
digits = true;
336+
str++;
337+
}
338+
339+
/* Set endptr and return result*/
340+
if (digits)
341+
{
342+
if (endptr)
343+
{
344+
*endptr = (char *) str;
345+
}
346+
return positive ? num : -num;
347+
}
348+
else
349+
{
350+
if (endptr)
351+
{
352+
*endptr = (char *) nptr;
353+
}
354+
return 0L;
355+
}
356+
} /* strtol */

jerry-main/main-unix.c

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -126,30 +126,6 @@ jerry_value_is_syntax_error (jerry_value_t error_value) /**< error value */
126126
return false;
127127
} /* jerry_value_is_syntax_error */
128128

129-
/**
130-
* Convert string into unsigned integer
131-
*
132-
* @return converted number
133-
*/
134-
static uint32_t
135-
str_to_uint (const char *num_str_p) /**< string to convert */
136-
{
137-
assert (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES));
138-
139-
uint32_t result = 0;
140-
141-
while (*num_str_p != '\0')
142-
{
143-
assert (*num_str_p >= '0' && *num_str_p <= '9');
144-
145-
result *= 10;
146-
result += (uint32_t) (*num_str_p - '0');
147-
num_str_p++;
148-
}
149-
150-
return result;
151-
} /* str_to_uint */
152-
153129
/**
154130
* Print error value
155131
*/
@@ -177,18 +153,18 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
177153

178154
if (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES) && jerry_value_is_syntax_error (error_value))
179155
{
180-
uint32_t err_line = 0;
181-
uint32_t err_col = 0;
156+
unsigned int err_line = 0;
157+
unsigned int err_col = 0;
182158

183159
/* 1. parse column and line information */
184-
for (uint32_t i = 0; i < sz; i++)
160+
for (jerry_size_t i = 0; i < sz; i++)
185161
{
186162
if (!strncmp ((char *) (err_str_buf + i), "[line: ", 7))
187163
{
188164
i += 7;
189165

190166
char num_str[8];
191-
uint32_t j = 0;
167+
unsigned int j = 0;
192168

193169
while (i < sz && err_str_buf[i] != ',')
194170
{
@@ -198,7 +174,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
198174
}
199175
num_str[j] = '\0';
200176

201-
err_line = str_to_uint (num_str);
177+
err_line = (unsigned int) strtol (num_str, NULL, 10);
202178

203179
if (strncmp ((char *) (err_str_buf + i), ", column: ", 10))
204180
{
@@ -216,17 +192,17 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
216192
}
217193
num_str[j] = '\0';
218194

219-
err_col = str_to_uint (num_str);
195+
err_col = (unsigned int) strtol (num_str, NULL, 10);
220196
break;
221197
}
222198
} /* for */
223199

224200
if (err_line != 0 && err_col != 0)
225201
{
226-
uint32_t curr_line = 1;
202+
unsigned int curr_line = 1;
227203

228204
bool is_printing_context = false;
229-
uint32_t pos = 0;
205+
unsigned int pos = 0;
230206

231207
/* 2. seek and print */
232208
while (buffer[pos] != '\0')
@@ -519,10 +495,12 @@ main (int argc,
519495
}
520496
case OPT_LOG_LEVEL:
521497
{
522-
check_usage (strlen (cli_state.arg[1]) == 1 && cli_state.arg[1][0] >= '0' && cli_state.arg[1][0] <= '3',
498+
char *endptr;
499+
long int log_level = strtol (cli_state.arg[1], &endptr, 10);
500+
check_usage (log_level >= 0 && log_level <= 3 && !*endptr,
523501
argv[0], "Error: wrong format for ", cli_state.arg[0]);
524502

525-
jerry_port_default_set_log_level ((jerry_log_level_t) (cli_state.arg[1][0] - '0'));
503+
jerry_port_default_set_log_level ((jerry_log_level_t) log_level);
526504
break;
527505
}
528506
case OPT_ABORT_ON_FAIL:

0 commit comments

Comments
 (0)