|
20 | 20 |
|
21 | 21 | #include "jerryscript.h" |
22 | 22 | #include "jerry-port.h" |
| 23 | +#include "jmem.h" |
23 | 24 |
|
24 | 25 | /** |
25 | 26 | * Maximum command line arguments number. |
@@ -51,126 +52,66 @@ print_help (char *name) |
51 | 52 | } /* print_help */ |
52 | 53 |
|
53 | 54 | /** |
54 | | - * Read source files. |
| 55 | + * Read source code into buffer. |
55 | 56 | * |
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 |
57 | 60 | */ |
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 */ |
62 | 64 | { |
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) |
70 | 67 | { |
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; |
100 | 70 | } |
101 | 71 |
|
102 | | - if (total_length <= 0) |
| 72 | + int fseek_status = fseek (file, 0, SEEK_END); |
| 73 | + if (fseek_status != 0) |
103 | 74 | { |
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); |
105 | 77 | return NULL; |
106 | 78 | } |
107 | 79 |
|
108 | | - source_buffer = (char*) malloc (total_length); |
109 | | - if (source_buffer == NULL) |
| 80 | + long script_len = ftell (file); |
| 81 | + if (script_len < 0) |
110 | 82 | { |
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); |
112 | 85 | return NULL; |
113 | 86 | } |
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 | | - } |
134 | 87 |
|
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); |
141 | 89 |
|
142 | | - rewind (file); |
| 90 | + uint8_t *buffer = jmem_heap_alloc_block_null_on_error (script_len); |
143 | 91 |
|
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) |
159 | 93 | { |
| 94 | + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Out of memory error\n"); |
160 | 95 | fclose (file); |
| 96 | + return NULL; |
161 | 97 | } |
162 | 98 |
|
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) |
164 | 102 | { |
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); |
167 | 107 | return NULL; |
168 | 108 | } |
169 | 109 |
|
170 | | - *out_source_size_p = (size_t) total_length; |
| 110 | + fclose (file); |
171 | 111 |
|
172 | | - return source_buffer; |
173 | | -} /* read_sources */ |
| 112 | + *out_size_p = bytes_read; |
| 113 | + return (const uint8_t *) buffer; |
| 114 | +} /* read_file */ |
174 | 115 |
|
175 | 116 | /** |
176 | 117 | * JerryScript log level |
@@ -250,27 +191,59 @@ int jerry_main (int argc, char *argv[]) |
250 | 191 | char *source_p = "var a = 3.5; print('Hello world ' + (a + 1.5) + ' times from JerryScript')"; |
251 | 192 |
|
252 | 193 | jerry_run_simple ((jerry_char_t *) source_p, strlen (source_p), flags); |
253 | | - return 0; |
| 194 | + return JERRY_STANDALONE_EXIT_CODE_OK; |
254 | 195 | } |
255 | 196 |
|
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 (); |
258 | 199 |
|
259 | | - if (source_p == NULL) |
| 200 | + for (i = 0; i < files_counter; i++) |
260 | 201 | { |
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); |
264 | 204 |
|
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); |
266 | 216 |
|
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 | + } |
268 | 225 |
|
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)) |
270 | 238 | { |
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; |
272 | 241 | } |
273 | | - return JERRY_STANDALONE_EXIT_CODE_OK; |
| 242 | + |
| 243 | + jerry_release_value (ret_value); |
| 244 | + jerry_cleanup (); |
| 245 | + |
| 246 | + return ret_code; |
274 | 247 | } /* main */ |
275 | 248 |
|
276 | 249 | /** |
|
0 commit comments