Skip to content

Commit 9043ec6

Browse files
authored
Add debugger support on nuttx-smt32f4 target (#1591)
Using jerry_parse_named_resource to parse script and construct an EcmaScript function. The file name will also passed to this function which is used by the debugger to find the source code. Run the constructed EcmaScript function instead of using the simple jerry runner. JerryScript-DCO-1.0-Signed-off-by: Robert Sipka rsipka.uszeged@partner.samsung.com
1 parent 92eaea2 commit 9043ec6

File tree

1 file changed

+80
-107
lines changed

1 file changed

+80
-107
lines changed

targets/nuttx-stm32f4/jerry_main.c

Lines changed: 80 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "jerryscript.h"
2222
#include "jerry-port.h"
23+
#include "jmem.h"
2324

2425
/**
2526
* Maximum command line arguments number.
@@ -51,126 +52,66 @@ print_help (char *name)
5152
} /* print_help */
5253

5354
/**
54-
* Read source files.
55+
* Read source code into buffer.
5556
*
56-
* @return concatenated source files
57+
* Returned value must be freed with jmem_heap_free_block if it's not NULL.
58+
* @return NULL, if read or allocation has failed
59+
* pointer to the allocated memory block, otherwise
5760
*/
58-
static char*
59-
read_sources (const char *script_file_names[],
60-
int files_count,
61-
size_t *out_source_size_p)
61+
static const uint8_t *
62+
read_file (const char *file_name, /**< source code */
63+
size_t *out_size_p) /**< [out] number of bytes successfully read from source */
6264
{
63-
int i;
64-
char* source_buffer = NULL;
65-
char *source_buffer_tail = NULL;
66-
size_t total_length = 0;
67-
FILE *file = NULL;
68-
69-
for (i = 0; i < files_count; i++)
65+
FILE *file = fopen (file_name, "r");
66+
if (file == NULL)
7067
{
71-
const char *script_file_name = script_file_names[i];
72-
73-
file = fopen (script_file_name, "r");
74-
if (file == NULL)
75-
{
76-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fopen [%s]\n", script_file_name);
77-
return NULL;
78-
}
79-
80-
int fseek_status = fseek (file, 0, SEEK_END);
81-
if (fseek_status != 0)
82-
{
83-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fseek fseek_status(%d)\n", fseek_status);
84-
fclose (file);
85-
return NULL;
86-
}
87-
88-
long script_len = ftell (file);
89-
if (script_len < 0)
90-
{
91-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to ftell script_len(%ld)\n", script_len);
92-
fclose (file);
93-
break;
94-
}
95-
96-
total_length += (size_t) script_len;
97-
98-
fclose (file);
99-
file = NULL;
68+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: cannot open file: %s\n", file_name);
69+
return NULL;
10070
}
10171

102-
if (total_length <= 0)
72+
int fseek_status = fseek (file, 0, SEEK_END);
73+
if (fseek_status != 0)
10374
{
104-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "There's nothing to read\n");
75+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to seek (error: %d)\n", fseek_status);
76+
fclose (file);
10577
return NULL;
10678
}
10779

108-
source_buffer = (char*) malloc (total_length);
109-
if (source_buffer == NULL)
80+
long script_len = ftell (file);
81+
if (script_len < 0)
11082
{
111-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Out of memory error\n");
83+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to get the file size(error %ld)\n", script_len);
84+
fclose (file);
11285
return NULL;
11386
}
114-
memset (source_buffer, 0, sizeof (char) * total_length);
115-
source_buffer_tail = source_buffer;
116-
117-
for (i = 0; i < files_count; i++)
118-
{
119-
const char *script_file_name = script_file_names[i];
120-
file = fopen (script_file_name, "r");
121-
122-
if (file == NULL)
123-
{
124-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fopen [%s]\n", script_file_name);
125-
break;
126-
}
127-
128-
int fseek_status = fseek (file, 0, SEEK_END);
129-
if (fseek_status != 0)
130-
{
131-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fseek fseek_status(%d)\n", fseek_status);
132-
break;
133-
}
13487

135-
long script_len = ftell (file);
136-
if (script_len < 0)
137-
{
138-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to ftell script_len(%ld)\n", script_len);
139-
break;
140-
}
88+
rewind (file);
14189

142-
rewind (file);
90+
uint8_t *buffer = jmem_heap_alloc_block_null_on_error (script_len);
14391

144-
const size_t current_source_size = (size_t) script_len;
145-
size_t bytes_read = fread (source_buffer_tail, 1, current_source_size, file);
146-
if (bytes_read < current_source_size)
147-
{
148-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to fread bytes_read(%d)\n", bytes_read);
149-
break;
150-
}
151-
152-
fclose (file);
153-
file = NULL;
154-
155-
source_buffer_tail += current_source_size;
156-
}
157-
158-
if (file != NULL)
92+
if (buffer == NULL)
15993
{
94+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Out of memory error\n");
16095
fclose (file);
96+
return NULL;
16197
}
16298

163-
if (i < files_count)
99+
size_t bytes_read = fread (buffer, 1u, script_len, file);
100+
101+
if (!bytes_read || bytes_read != script_len)
164102
{
165-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Failed to read script N%d\n", i + 1);
166-
free (source_buffer);
103+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name);
104+
jmem_heap_free_block ((void*) buffer, script_len);
105+
106+
fclose (file);
167107
return NULL;
168108
}
169109

170-
*out_source_size_p = (size_t) total_length;
110+
fclose (file);
171111

172-
return source_buffer;
173-
} /* read_sources */
112+
*out_size_p = bytes_read;
113+
return (const uint8_t *) buffer;
114+
} /* read_file */
174115

175116
/**
176117
* JerryScript log level
@@ -250,27 +191,59 @@ int jerry_main (int argc, char *argv[])
250191
char *source_p = "var a = 3.5; print('Hello world ' + (a + 1.5) + ' times from JerryScript')";
251192

252193
jerry_run_simple ((jerry_char_t *) source_p, strlen (source_p), flags);
253-
return 0;
194+
return JERRY_STANDALONE_EXIT_CODE_OK;
254195
}
255196

256-
size_t source_size;
257-
char *source_p = read_sources (file_names, files_counter, &source_size);
197+
jerry_init (flags);
198+
jerry_value_t ret_value = jerry_create_undefined ();
258199

259-
if (source_p == NULL)
200+
for (i = 0; i < files_counter; i++)
260201
{
261-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "JERRY_STANDALONE_EXIT_CODE_FAIL\n");
262-
return JERRY_STANDALONE_EXIT_CODE_FAIL;
263-
}
202+
size_t source_size;
203+
const jerry_char_t *source_p = read_file (file_names[i], &source_size);
264204

265-
bool success = jerry_run_simple ((jerry_char_t *) source_p, source_size, flags);
205+
if (source_p == NULL)
206+
{
207+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Source file load error\n");
208+
return JERRY_STANDALONE_EXIT_CODE_FAIL;
209+
}
210+
211+
ret_value = jerry_parse_named_resource ((jerry_char_t *) file_names[i],
212+
strlen (file_names[i]),
213+
source_p,
214+
source_size,
215+
false);
266216

267-
free (source_p);
217+
jmem_heap_free_block ((void*) source_p, source_size);
218+
219+
if (!jerry_value_has_error_flag (ret_value))
220+
{
221+
jerry_value_t func_val = ret_value;
222+
ret_value = jerry_run (func_val);
223+
jerry_release_value (func_val);
224+
}
268225

269-
if (!success)
226+
if (jerry_value_has_error_flag (ret_value))
227+
{
228+
break;
229+
}
230+
231+
jerry_release_value (ret_value);
232+
ret_value = jerry_create_undefined ();
233+
}
234+
235+
int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
236+
237+
if (jerry_value_has_error_flag (ret_value))
270238
{
271-
return JERRY_STANDALONE_EXIT_CODE_FAIL;
239+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Unhandled exception: Script Error!\n");
240+
ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
272241
}
273-
return JERRY_STANDALONE_EXIT_CODE_OK;
242+
243+
jerry_release_value (ret_value);
244+
jerry_cleanup ();
245+
246+
return ret_code;
274247
} /* main */
275248

276249
/**

0 commit comments

Comments
 (0)