1/* Copyright JS Foundation and other contributors, http://js.foundation 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include <stdio.h> 17#include <string.h> 18#include <stdlib.h> 19#include <unistd.h> 20 21#include "jerryscript.h" 22#include "jerryscript-port.h" 23 24/** 25 * JerryScript log level 26 */ 27static jerry_log_level_t jerry_log_level = JERRY_LOG_LEVEL_ERROR; 28 29/** 30 * Sets log level. 31 */ 32void set_log_level (jerry_log_level_t level) 33{ 34 jerry_log_level = level; 35} /* set_log_level */ 36 37/** 38 * Aborts the program. 39 */ 40void jerry_port_fatal (jerry_fatal_code_t code) 41{ 42 exit (1); 43} /* jerry_port_fatal */ 44 45/** 46 * Provide log message implementation for the engine. 47 */ 48void 49jerry_port_log (jerry_log_level_t level, /**< log level */ 50 const char *format, /**< format string */ 51 ...) /**< parameters */ 52{ 53 if (level <= jerry_log_level) 54 { 55 va_list args; 56 va_start (args, format); 57 vfprintf (stderr, format, args); 58 va_end (args); 59 } 60} /* jerry_port_log */ 61 62/** 63 * Determines the size of the given file. 64 * @return size of the file 65 */ 66static size_t 67jerry_port_get_file_size (FILE *file_p) /**< opened file */ 68{ 69 fseek (file_p, 0, SEEK_END); 70 long size = ftell (file_p); 71 fseek (file_p, 0, SEEK_SET); 72 73 return (size_t) size; 74} /* jerry_port_get_file_size */ 75 76/** 77 * Opens file with the given path and reads its source. 78 * @return the source of the file 79 */ 80uint8_t * 81jerry_port_read_source (const char *file_name_p, /**< file name */ 82 size_t *out_size_p) /**< [out] read bytes */ 83{ 84 FILE *file_p = fopen (file_name_p, "rb"); 85 86 if (file_p == NULL) 87 { 88 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name_p); 89 return NULL; 90 } 91 92 size_t file_size = jerry_port_get_file_size (file_p); 93 uint8_t *buffer_p = (uint8_t *) malloc (file_size); 94 95 if (buffer_p == NULL) 96 { 97 fclose (file_p); 98 99 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to allocate memory for module"); 100 return NULL; 101 } 102 103 size_t bytes_read = fread (buffer_p, 1u, file_size, file_p); 104 105 if (!bytes_read) 106 { 107 fclose (file_p); 108 free (buffer_p); 109 110 jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name_p); 111 return NULL; 112 } 113 114 fclose (file_p); 115 *out_size_p = bytes_read; 116 117 return buffer_p; 118} /* jerry_port_read_source */ 119 120/** 121 * Release the previously opened file's content. 122 */ 123void 124jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */ 125{ 126 free (buffer_p); 127} /* jerry_port_release_source */ 128 129/** 130 * Normalize a file path 131 * 132 * @return length of the path written to the output buffer 133 */ 134size_t 135jerry_port_normalize_path (const char *in_path_p, /**< input file path */ 136 char *out_buf_p, /**< output buffer */ 137 size_t out_buf_size, /**< size of output buffer */ 138 char *base_file_p) /**< base file path */ 139{ 140 (void) base_file_p; 141 142 size_t len = strlen (in_path_p); 143 if (len + 1 > out_buf_size) 144 { 145 return 0; 146 } 147 148 /* Return the original string. */ 149 strcpy (out_buf_p, in_path_p); 150 return len; 151} /* jerry_port_normalize_path */ 152 153/** 154 * Get the module object of a native module. 155 * 156 * @return undefined 157 */ 158jerry_value_t 159jerry_port_get_native_module (jerry_value_t name) /**< module specifier */ 160{ 161 (void) name; 162 return jerry_create_undefined (); 163} /* jerry_port_get_native_module */ 164 165/** 166 * Dummy function to get the time zone adjustment. 167 * 168 * @return 0 169 */ 170double 171jerry_port_get_local_time_zone_adjustment (double unix_ms, bool is_utc) 172{ 173 /* We live in UTC. */ 174 return 0; 175} /* jerry_port_get_local_time_zone_adjustment */ 176 177/** 178 * Dummy function to get the current time. 179 * 180 * @return 0 181 */ 182double 183jerry_port_get_current_time (void) 184{ 185 return 0; 186} /* jerry_port_get_current_time */ 187 188/** 189 * Provide the implementation of jerry_port_print_char. 190 * Uses 'printf' to print a single character to standard output. 191 */ 192void 193jerry_port_print_char (char c) /**< the character to print */ 194{ 195 printf ("%c", c); 196} /* jerry_port_print_char */ 197 198/** 199 * Provide implementation of jerry_port_sleep. 200 */ 201void jerry_port_sleep (uint32_t sleep_time) /**< milliseconds to sleep */ 202{ 203 usleep ((useconds_t) sleep_time * 1000); 204} /* jerry_port_sleep */ 205 206/** 207 * Pointer to the current context. 208 */ 209static jerry_context_t *current_context_p = NULL; 210 211/** 212 * Set the current_context_p as the passed pointer. 213 */ 214void 215jerry_port_default_set_current_context (jerry_context_t *context_p) /**< points to the created context */ 216{ 217 current_context_p = context_p; 218} /* jerry_port_default_set_current_context */ 219 220/** 221 * Get the current context. 222 * 223 * @return the pointer to the current context 224 */ 225jerry_context_t * 226jerry_port_get_current_context (void) 227{ 228 return current_context_p; 229} /* jerry_port_get_current_context */ 230