Skip to content

Commit b6df2ba

Browse files
author
François Baldassari
committed
Add jerry_port_get_time API and use it for Date.now()
JerryScript-DCO-1.0-Signed-off-by: François Baldassari [email protected]
1 parent 005f73a commit b6df2ba

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-date.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@
3333
#define BUILTIN_UNDERSCORED_ID date
3434
#include "ecma-builtin-internal-routines-template.inc.h"
3535

36-
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
37-
#include <sys/time.h>
38-
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */
39-
4036
/** \addtogroup ecma ECMA
4137
* @{
4238
*
@@ -456,17 +452,11 @@ ecma_builtin_date_now (ecma_value_t this_arg __attr_unused___) /**< this argumen
456452
ecma_number_t *now_num_p = ecma_alloc_number ();
457453
*now_num_p = ECMA_NUMBER_ZERO;
458454

459-
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
460-
struct timeval tv;
461-
462-
if (gettimeofday (&tv, NULL) != 0)
455+
if (jerry_port_get_time (now_num_p) != 0)
463456
{
464-
return ecma_raise_type_error (ECMA_ERR_MSG ("gettimeofday failed"));
457+
return ecma_raise_type_error (ECMA_ERR_MSG ("Get time failed"));
465458
}
466459

467-
*now_num_p = ((ecma_number_t) tv.tv_sec) * 1000.0 + ((ecma_number_t) (tv.tv_usec / 1000));
468-
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */
469-
470460
return ecma_make_number_value (now_num_p);
471461
} /* ecma_builtin_date_now */
472462

jerry-core/jerry-port.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
#include "jerry-port.h"
1717
#include <stdarg.h>
18+
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
19+
#include <sys/time.h>
20+
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */
1821

1922
/**
2023
* Provide log message to filestream implementation for the engine.
@@ -52,3 +55,25 @@ int jerry_port_putchar (int c) /**< character to put */
5255
{
5356
return putchar ((unsigned char) c);
5457
} /* jerry_port_putchar */
58+
59+
/**
60+
* Provide datetime implementation for the engine
61+
*/
62+
int jerry_port_get_time (double *out_time)
63+
{
64+
#ifdef JERRY_ENABLE_DATE_SYS_CALLS
65+
struct timeval tv;
66+
67+
if (gettimeofday (&tv, NULL) != 0)
68+
{
69+
return -1;
70+
}
71+
else
72+
{
73+
*out_time = ((double) tv.tv_sec) * 1000.0 + ((double) (tv.tv_usec / 1000.0));
74+
return 0;
75+
}
76+
#endif /* JERRY_ENABLE_DATE_SYS_CALLS */
77+
78+
return 0;
79+
} /* jerry_port_get_time */

jerry-core/jerry-port.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ int jerry_port_logmsg (FILE *stream, const char *format, ...);
3535
int jerry_port_errormsg (const char *format, ...);
3636
int jerry_port_putchar (int c);
3737

38+
/**
39+
* Target port functions for date and time
40+
*/
41+
int jerry_port_get_time (double *out_time);
42+
3843
/**
3944
* @}
4045
*/

0 commit comments

Comments
 (0)