From 707eb80274f7f019057ef5590d89a64bf413b7c6 Mon Sep 17 00:00:00 2001 From: Daniella Barsony Date: Tue, 2 Apr 2019 14:27:04 +0200 Subject: [PATCH] Add missing port functions for artik053 target JerryScript-DCO-1.0-Signed-off-by: Daniella Barsony bella@inf.u-szeged.hu --- .../apps/jerryscript/jerry_main.c | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c b/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c index 90abb439dd..8e31eb2ddb 100644 --- a/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c +++ b/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c @@ -506,6 +506,75 @@ jerry_port_print_char (char c) /**< the character to print */ printf ("%c", c); } /* jerry_port_print_char */ +#ifndef CONFIG_DISABLE_ES2015_MODULE_SYSTEM +/** + * Determines the size of the given file. + * @return size of the file + */ +static size_t +jerry_port_get_file_size (FILE *file_p) /**< opened file */ +{ + fseek (file_p, 0, SEEK_END); + long size = ftell (file_p); + fseek (file_p, 0, SEEK_SET); + + return (size_t) size; +} /* jerry_port_get_file_size */ + +/** + * Opens file with the given path and reads its source. + * @return the source of the file + */ +uint8_t * +jerry_port_read_source (const char *file_name_p, /**< file name */ + size_t *out_size_p) /**< [out] read bytes */ +{ + FILE *file_p = fopen (file_name_p, "rb"); + + if (file_p == NULL) + { + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name_p); + return NULL; + } + + size_t file_size = jerry_port_get_file_size (file_p); + uint8_t *buffer_p = (uint8_t *) malloc (file_size); + + if (buffer_p == NULL) + { + fclose (file_p); + + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to allocate memory for module"); + return NULL; + } + + size_t bytes_read = fread (buffer_p, 1u, file_size, file_p); + + if (!bytes_read) + { + fclose (file_p); + free (buffer_p); + + jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name_p); + return NULL; + } + + fclose (file_p); + *out_size_p = bytes_read; + + return buffer_p; +} /* jerry_port_read_source */ + +/** + * Release the previously opened file's content. + */ +void +jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */ +{ + free (buffer_p); +} /* jerry_port_release_source */ +#endif /* !CONFIG_DISABLE_ES2015_MODULE_SYSTEM */ + /** * Main program. *