@@ -506,6 +506,74 @@ jerry_port_print_char (char c) /**< the character to print */
506506 printf ("%c" , c );
507507} /* jerry_port_print_char */
508508
509+ #ifndef CONFIG_DISABLE_ES2015_MODULE_SYSTEM
510+ /**
511+ * Determines the size of the given file.
512+ * @return size of the file
513+ */
514+ static size_t
515+ jerry_port_get_file_size (FILE * file_p ) /**< opened file */
516+ {
517+ fseek (file_p , 0 , SEEK_END );
518+ long size = ftell (file_p );
519+ fseek (file_p , 0 , SEEK_SET );
520+
521+ return (size_t ) size ;
522+ } /* jerry_port_get_file_size */
523+
524+ /**
525+ * Opens file with the given path and reads its source.
526+ * @return the source of the file
527+ */
528+ uint8_t *
529+ jerry_port_read_source (const char * file_name_p , /**< file name */
530+ size_t * out_size_p ) /**< [out] read bytes */
531+ {
532+ FILE * file_p = fopen (file_name_p , "rb" );
533+
534+ if (file_p == NULL )
535+ {
536+ jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Error: failed to open file: %s\n" , file_name_p );
537+ return NULL ;
538+ }
539+
540+ size_t file_size = jerry_port_get_file_size (file_p );
541+ uint8_t * buffer_p = (uint8_t * ) malloc (file_size );
542+
543+ if (buffer_p == NULL )
544+ {
545+ fclose (file_p );
546+
547+ jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Error: failed to allocate memory for module" );
548+ return NULL ;
549+ }
550+
551+ size_t bytes_read = fread (buffer_p , 1u , file_size , file_p );
552+
553+ if (!bytes_read )
554+ {
555+ fclose (file_p );
556+ free (buffer_p );
557+
558+ jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Error: failed to read file: %s\n" , file_name_p );
559+ return NULL ;
560+ }
561+
562+ fclose (file_p );
563+ * out_size_p = bytes_read ;
564+
565+ return buffer_p ;
566+ } /* jerry_port_read_source */
567+
568+ /**
569+ * Release the previously opened file's content.
570+ */
571+ void
572+ jerry_port_release_source (uint8_t * buffer_p ) /**< buffer to free */
573+ {
574+ free (buffer_p );
575+ } /* jerry_port_release_source */
576+ #endif
509577/**
510578* Main program.
511579*
0 commit comments