From af5edf95045212834394ac42c9dcab0805078918 Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Thu, 1 Mar 2018 19:18:49 +0100 Subject: [PATCH] jscript: Added Date.now implementation. Signed-off-by: Jacek Caban Signed-off-by: Alexandre Julliard --- dlls/jscript/date.c | 34 ++++++++++++++++++++++--------- dlls/jscript/tests/api.js | 1 + dlls/mshtml/tests/documentmode.js | 1 + dlls/mshtml/tests/es5.js | 32 +++++++++++++++++++++++++++++ dlls/mshtml/tests/rsrc.rc | 3 +++ dlls/mshtml/tests/script.c | 1 + 6 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 dlls/mshtml/tests/es5.js diff --git a/dlls/jscript/date.c b/dlls/jscript/date.c index 21a359a10f1..f784d51afb3 100644 --- a/dlls/jscript/date.c +++ b/dlls/jscript/date.c @@ -92,6 +92,7 @@ static const WCHAR getYearW[] = {'g','e','t','Y','e','a','r',0}; static const WCHAR setYearW[] = {'s','e','t','Y','e','a','r',0}; static const WCHAR UTCW[] = {'U','T','C',0}; +static const WCHAR nowW[] = {'n','o','w',0}; static const WCHAR parseW[] = {'p','a','r','s','e',0}; static inline DateInstance *date_from_jsdisp(jsdisp_t *jsdisp) @@ -452,6 +453,17 @@ static inline DOUBLE time_clip(DOUBLE time) return floor(time); } +static double date_now(void) +{ + FILETIME ftime; + LONGLONG time; + + GetSystemTimeAsFileTime(&ftime); + time = ((LONGLONG)ftime.dwHighDateTime << 32) + ftime.dwLowDateTime; + + return time/10000 - TIME_EPOCH; +} + static SYSTEMTIME create_systemtime(DOUBLE time) { SYSTEMTIME st; @@ -2361,6 +2373,15 @@ static HRESULT DateConstr_UTC(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, un return hres; } +/* ECMA-262 5.1 Edition 15.9.4.4 */ +static HRESULT DateConstr_now(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) +{ + TRACE("\n"); + + if(r) *r = jsval_number(date_now()); + return S_OK; +} + static HRESULT DateConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { @@ -2373,19 +2394,11 @@ static HRESULT DateConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, case DISPATCH_CONSTRUCT: switch(argc) { /* ECMA-262 3rd Edition 15.9.3.3 */ - case 0: { - FILETIME time; - LONGLONG lltime; - - GetSystemTimeAsFileTime(&time); - lltime = ((LONGLONG)time.dwHighDateTime<<32) - + time.dwLowDateTime; - - hres = create_date(ctx, NULL, lltime/10000-TIME_EPOCH, &date); + case 0: + hres = create_date(ctx, NULL, date_now(), &date); if(FAILED(hres)) return hres; break; - } /* ECMA-262 3rd Edition 15.9.3.2 */ case 1: { @@ -2454,6 +2467,7 @@ static HRESULT DateConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, static const builtin_prop_t DateConstr_props[] = { {UTCW, DateConstr_UTC, PROPF_METHOD}, + {nowW, DateConstr_now, PROPF_HTML|PROPF_METHOD}, {parseW, DateConstr_parse, PROPF_METHOD} }; diff --git a/dlls/jscript/tests/api.js b/dlls/jscript/tests/api.js index 5a78b3a15a1..084aa84b5a5 100644 --- a/dlls/jscript/tests/api.js +++ b/dlls/jscript/tests/api.js @@ -294,6 +294,7 @@ obj = new Date(); ok(!obj.hasOwnProperty('getTime'), "obj.hasOwnProperty('getTime') is true"); ok(!Date.hasOwnProperty('getTime'), "Date.hasOwnProperty('getTime') is true"); ok(Date.prototype.hasOwnProperty('getTime'), "Date.prototype.hasOwnProperty('getTime') is false"); +ok(!("now" in Date), "now found in Date"); obj = new Number(); ok(!obj.hasOwnProperty('toFixed'), "obj.hasOwnProperty('toFixed') is true"); diff --git a/dlls/mshtml/tests/documentmode.js b/dlls/mshtml/tests/documentmode.js index 823f4f9dc06..7287bab16f8 100644 --- a/dlls/mshtml/tests/documentmode.js +++ b/dlls/mshtml/tests/documentmode.js @@ -121,6 +121,7 @@ function test_javascript() { test_exposed("ScriptEngineMajorVersion", g, true); test_exposed("JSON", g, v >= 8); + test_exposed("now", Date, true); next_test(); } diff --git a/dlls/mshtml/tests/es5.js b/dlls/mshtml/tests/es5.js new file mode 100644 index 00000000000..3f77c05d2d0 --- /dev/null +++ b/dlls/mshtml/tests/es5.js @@ -0,0 +1,32 @@ +/* + * Copyright 2018 Jacek Caban for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +function test_date_now() { + var now = Date.now(); + var time = (new Date()).getTime(); + + ok(time >= now && time-now < 50, "unexpected Date.now() result " + now + " expected " + time); + + Date.now(1, 2, 3); + + next_test(); +} + +var tests = [ + test_date_now +]; diff --git a/dlls/mshtml/tests/rsrc.rc b/dlls/mshtml/tests/rsrc.rc index df5b469b4ee..3ce867e312c 100644 --- a/dlls/mshtml/tests/rsrc.rc +++ b/dlls/mshtml/tests/rsrc.rc @@ -52,6 +52,9 @@ xhr.js HTML "xhr.js" /* @makedep: elements.js */ elements.js HTML "elements.js" +/* @makedep: es5.js */ +es5.js HTML "es5.js" + /* @makedep: events.js */ events.js HTML "events.js" diff --git a/dlls/mshtml/tests/script.c b/dlls/mshtml/tests/script.c index b07cce1f3d1..db6ac2548f9 100644 --- a/dlls/mshtml/tests/script.c +++ b/dlls/mshtml/tests/script.c @@ -3465,6 +3465,7 @@ static void run_js_tests(void) run_script_as_http_with_mode("xhr.js", NULL, "11"); run_script_as_http_with_mode("elements.js", NULL, "11"); + run_script_as_http_with_mode("es5.js", NULL, "11"); run_script_as_http_with_mode("events.js", NULL, "9"); run_script_as_http_with_mode("navigation.js", NULL, NULL); run_script_as_http_with_mode("navigation.js", NULL, "11");