@@ -63,3 +63,102 @@ double jerry_port_get_current_time(void) {
63
63
void jerryx_port_handler_print_char (char c ) { /**< the character to print */
64
64
printf ("%c" , c );
65
65
} /* jerryx_port_handler_print_char */
66
+
67
+ /**
68
+ * Normalize a file path
69
+ *
70
+ * @return length of the path written to the output buffer
71
+ */
72
+ size_t jerry_port_normalize_path (
73
+ const char * in_path_p , /**< input file path */
74
+ char * out_buf_p , /**< output buffer */
75
+ size_t out_buf_size , /**< size of output buffer */
76
+ char * base_file_p ) /**< base file path */
77
+ {
78
+ (void )base_file_p ;
79
+
80
+ size_t len = strlen (in_path_p );
81
+ if (len + 1 > out_buf_size ) {
82
+ return 0 ;
83
+ }
84
+
85
+ /* Return the original string. */
86
+ strcpy (out_buf_p , in_path_p );
87
+ return len ;
88
+ } /* jerry_port_normalize_path */
89
+
90
+ /**
91
+ * Get the module object of a native module.
92
+ *
93
+ * @return undefined
94
+ */
95
+ jerry_value_t jerry_port_get_native_module (
96
+ jerry_value_t name ) /**< module specifier */
97
+ {
98
+ (void )name ;
99
+ return jerry_create_undefined ();
100
+ } /* jerry_port_get_native_module */
101
+
102
+ /**
103
+ * Determines the size of the given file.
104
+ * @return size of the file
105
+ */
106
+ static size_t jerry_port_get_file_size (FILE * file_p ) /**< opened file */
107
+ {
108
+ fseek (file_p , 0 , SEEK_END );
109
+ long size = ftell (file_p );
110
+ fseek (file_p , 0 , SEEK_SET );
111
+
112
+ return (size_t )size ;
113
+ } /* jerry_port_get_file_size */
114
+
115
+ /**
116
+ * Opens file with the given path and reads its source.
117
+ * @return the source of the file
118
+ */
119
+ uint8_t * jerry_port_read_source (const char * file_name_p , /**< file name */
120
+ size_t * out_size_p ) /**< [out] read bytes */
121
+ {
122
+ FILE * file_p = fopen (file_name_p , "rb" );
123
+
124
+ if (file_p == NULL ) {
125
+ jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Error: failed to open file: %s\n" ,
126
+ file_name_p );
127
+ return NULL ;
128
+ }
129
+
130
+ size_t file_size = jerry_port_get_file_size (file_p );
131
+ uint8_t * buffer_p = (uint8_t * )malloc (file_size );
132
+
133
+ if (buffer_p == NULL ) {
134
+ fclose (file_p );
135
+
136
+ jerry_port_log (JERRY_LOG_LEVEL_ERROR ,
137
+ "Error: failed to allocate memory for module" );
138
+ return NULL ;
139
+ }
140
+
141
+ size_t bytes_read = fread (buffer_p , 1u , file_size , file_p );
142
+
143
+ if (!bytes_read ) {
144
+ fclose (file_p );
145
+ free (buffer_p );
146
+
147
+ jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Error: failed to read file: %s\n" ,
148
+ file_name_p );
149
+ return NULL ;
150
+ }
151
+
152
+ fclose (file_p );
153
+ * out_size_p = bytes_read ;
154
+
155
+ return buffer_p ;
156
+ } /* jerry_port_read_source */
157
+
158
+ /**
159
+ * Release the previously opened file's content.
160
+ */
161
+ void jerry_port_release_source (uint8_t * buffer_p ) /**< buffer to free */
162
+ {
163
+ free (buffer_p );
164
+ } /* jerry_port_release_source */
0 commit comments