Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions jerry-port/default/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,6 @@ endif()
# (should only be necessary if we used compiler default libc but not checking that)
set(DEFINES_PORT_DEFAULT _BSD_SOURCE _DEFAULT_SOURCE)

INCLUDE (CheckStructHasMember)
# CHECK_STRUCT_HAS_MEMBER works by trying to compile some C code that accesses the
# given field of the given struct. However, our default compiler options break this
# C code, so turn a couple of them off for this.
if(USING_GCC OR USING_CLANG)
set(CMAKE_REQUIRED_FLAGS "-Wno-error=strict-prototypes -Wno-error=old-style-definition -Wno-error=unused-value")
endif()
# tm.tm_gmtoff is non-standard, so glibc doesn't expose it in c99 mode
# (our default). Define some macros to expose it anyway.
set(CMAKE_REQUIRED_DEFINITIONS "-D_BSD_SOURCE -D_DEFAULT_SOURCE")
CHECK_STRUCT_HAS_MEMBER ("struct tm" tm_gmtoff time.h HAVE_TM_GMTOFF)
if(HAVE_TM_GMTOFF)
set(DEFINES_PORT_DEFAULT ${DEFINES_PORT_DEFAULT} HAVE_TM_GMTOFF)
endif()

# Sleep function availability check
INCLUDE (CheckIncludeFiles)
CHECK_INCLUDE_FILES (time.h HAVE_TIME_H)
CHECK_INCLUDE_FILES (unistd.h HAVE_UNISTD_H)
if(HAVE_TIME_H)
set(DEFINES_PORT_DEFAULT ${DEFINES_PORT_DEFAULT} HAVE_TIME_H)
elseif(HAVE_UNISTD_H)
set(DEFINES_PORT_DEFAULT ${DEFINES_PORT_DEFAULT} HAVE_UNISTD_H)
endif()

# Default Jerry port implementation library
add_library(${JERRY_PORT_DEFAULT_NAME} ${SOURCE_PORT_DEFAULT})
target_include_directories(${JERRY_PORT_DEFAULT_NAME} PUBLIC ${INCLUDE_PORT_DEFAULT})
Expand Down
65 changes: 39 additions & 26 deletions jerry-port/default/default-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@
* limitations under the License.
*/

#ifdef HAVE_TM_GMTOFF
#include <time.h>
#endif /* HAVE_TM_GMTOFF */

#ifdef _WIN32
#include <windows.h>
#include <winbase.h>
#include <winnt.h>
#include <time.h>
#endif /* _WIN32 */

#ifdef __GNUC__
#if defined (__GNUC__) || defined (__clang__)
#include <sys/time.h>
#endif /* __GNUC__ */
#endif /* __GNUC__ || __clang__ */

#include "jerryscript-port.h"
#include "jerryscript-port-default.h"
Expand Down Expand Up @@ -54,29 +51,14 @@ static double FileTimeToUnixTimeMs (FILETIME ft)
#endif /* _WIN32 */

/**
* Default implementation of jerry_port_get_local_time_zone_adjustment. Uses the 'tm_gmtoff' field
* of 'struct tm' (a GNU extension) filled by 'localtime_r' if available on the
* system, does nothing otherwise.
* Default implementation of jerry_port_get_local_time_zone_adjustment.
*
* @return offset between UTC and local time at the given unix timestamp, if
* available. Otherwise, returns 0, assuming UTC time.
*/
double jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since unix epoch */
bool is_utc) /**< is the time above in UTC? */
{
#ifdef HAVE_TM_GMTOFF
struct tm tm;
time_t now = (time_t) (unix_ms / 1000);
localtime_r (&now, &tm);
if (!is_utc)
{
now -= tm.tm_gmtoff;
localtime_r (&now, &tm);
}
return ((double) tm.tm_gmtoff) * 1000;
#else /* !HAVE_TM_GMTOFF */
(void) unix_ms;
(void) is_utc;
#ifdef _WIN32
FILETIME fileTime, localFileTime;
SYSTEMTIME systemTime, localSystemTime;
Expand All @@ -94,9 +76,39 @@ double jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since
localTime.HighPart = localFileTime.dwHighDateTime;
return (double) (((LONGLONG) localTime.QuadPart - (LONGLONG) time.QuadPart) / TicksPerMs);
}
#endif /* _WIN32 */
#elif defined (__GNUC__) || defined (__clang__)
time_t now_time = (time_t) (unix_ms / 1000);
double tza_s = 0.0;

while (true)
{
struct tm now_tm;
if (!gmtime_r (&now_time, &now_tm))
{
break;
}
now_tm.tm_isdst = -1; /* if not overridden, DST will not be taken into account */
time_t local_time = mktime (&now_tm);
if (local_time == (time_t) -1)
{
break;
}
tza_s = difftime (now_time, local_time);

if (is_utc)
{
break;
}
now_time -= (time_t) tza_s;
is_utc = true;
}

return tza_s * 1000;
#else /* !_WIN32 && !__GNUC__ && !__clang__ */
(void) unix_ms; /* unused */
(void) is_utc; /* unused */
return 0.0;
#endif /* HAVE_TM_GMTOFF */
#endif /* _WIN32 */
} /* jerry_port_get_local_time_zone_adjustment */

/**
Expand All @@ -113,14 +125,15 @@ double jerry_port_get_current_time (void)
FILETIME ft;
GetSystemTimeAsFileTime (&ft);
return FileTimeToUnixTimeMs (ft);
#elif __GNUC__
#elif defined (__GNUC__) || defined (__clang__)
struct timeval tv;

if (gettimeofday (&tv, NULL) == 0)
{
return ((double) tv.tv_sec) * 1000.0 + ((double) tv.tv_usec) / 1000.0;
}
#endif /* _WIN32 */

return 0.0;
#else /* !_WIN32 && !__GNUC__ && !__clang__ */
return 0.0;
#endif /* _WIN32 */
} /* jerry_port_get_current_time */
20 changes: 5 additions & 15 deletions jerry-port/default/default-debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,22 @@

#ifdef _WIN32
#include <windows.h>
#elif defined (HAVE_TIME_H)
#include <time.h>
#elif defined (HAVE_UNISTD_H)
#else /* !_WIN32 */
#include <unistd.h>
#endif /* _WIN32 */

#include "jerryscript-port.h"
#include "jerryscript-port-default.h"

/**
* Default implementation of jerry_port_sleep. Uses 'nanosleep' or 'usleep' if
* available on the system, does nothing otherwise.
* Default implementation of jerry_port_sleep. Uses 'usleep' if available on the
* system, does nothing otherwise.
*/
void jerry_port_sleep (uint32_t sleep_time) /**< milliseconds to sleep */
{
#ifdef _WIN32
Sleep (sleep_time);
#elif defined (HAVE_TIME_H)
struct timespec sleep_timespec;
sleep_timespec.tv_sec = (time_t) sleep_time / 1000;
sleep_timespec.tv_nsec = ((long int) sleep_time % 1000) * 1000000L;

nanosleep (&sleep_timespec, NULL);
#elif defined (HAVE_UNISTD_H)
#else /* !_WIN32 */
usleep ((useconds_t) sleep_time * 1000);
#else
(void) sleep_time;
#endif /* HAVE_TIME_H */
#endif /* _WIN32 */
} /* jerry_port_sleep */