Skip to content

Add jerry_port_get_time API and use it for Date.now() #969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
14 changes: 2 additions & 12 deletions jerry-core/ecma/builtin-objects/ecma-builtin-date.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
#define BUILTIN_UNDERSCORED_ID date
#include "ecma-builtin-internal-routines-template.inc.h"

#ifdef JERRY_ENABLE_DATE_SYS_CALLS
#include <sys/time.h>
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */

/** \addtogroup ecma ECMA
* @{
*
Expand Down Expand Up @@ -456,17 +452,11 @@ ecma_builtin_date_now (ecma_value_t this_arg __attr_unused___) /**< this argumen
ecma_number_t *now_num_p = ecma_alloc_number ();
*now_num_p = ECMA_NUMBER_ZERO;

#ifdef JERRY_ENABLE_DATE_SYS_CALLS
struct timeval tv;

if (gettimeofday (&tv, NULL) != 0)
if (jerry_port_get_time (now_num_p) != 0)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("gettimeofday failed"));
return ecma_raise_type_error (ECMA_ERR_MSG ("Get time failed"));
}

*now_num_p = ((ecma_number_t) tv.tv_sec) * 1000.0 + ((ecma_number_t) (tv.tv_usec / 1000));
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */

return ecma_make_number_value (now_num_p);
} /* ecma_builtin_date_now */

Expand Down
27 changes: 27 additions & 0 deletions jerry-core/jerry-port.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

#include "jerry-port.h"
#include <stdarg.h>
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
#include <sys/time.h>
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */

/**
* Provide log message to filestream implementation for the engine.
Expand Down Expand Up @@ -52,3 +55,27 @@ int jerry_port_putchar (int c) /**< character to put */
{
return putchar ((unsigned char) c);
} /* jerry_port_putchar */

/**
* Provide datetime implementation for the engine
*/
int jerry_port_get_time (double *out_time)
{
(void) out_time;

#ifdef JERRY_ENABLE_DATE_SYS_CALLS
struct timeval tv;

if (gettimeofday (&tv, NULL) != 0)
{
return -1;
}
else
{
*out_time = ((double) tv.tv_sec) * 1000.0 + ((double) (tv.tv_usec / 1000.0));
return 0;
}
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */

return 0;
} /* jerry_port_get_time */
5 changes: 5 additions & 0 deletions jerry-core/jerry-port.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ int jerry_port_logmsg (FILE *stream, const char *format, ...);
int jerry_port_errormsg (const char *format, ...);
int jerry_port_putchar (int c);

/**
* Target port functions for date and time
*/
int jerry_port_get_time (double *out_time);

/**
* @}
*/
Expand Down