From 1a9469865832121deeabb3dd0d84a6af58ce775f Mon Sep 17 00:00:00 2001 From: Mike Armstrong Date: Fri, 11 Dec 2015 07:28:50 -0800 Subject: [PATCH] expose systemclock time to JS Reviewed By: tadeuzagallo Differential Revision: D2748749 fb-gh-sync-id: 4d1dbae61f69a07b7106cb57caff03cadfb85776 --- .../src/main/jni/react/JSCExecutor.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp index 6e1f493e8..8b954f156 100644 --- a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp +++ b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "Value.h" #include "jni/OnLoad.h" @@ -34,6 +35,9 @@ using fbsystrace::FbSystraceSection; #include #endif +static const int64_t NANOSECONDS_IN_SECOND = 1000000000LL; +static const int64_t NANOSECONDS_IN_MILLISECOND = 1000000LL; + using namespace facebook::jni; namespace facebook { @@ -54,6 +58,13 @@ static JSValueRef nativeLoggingHook( size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception); +static JSValueRef nativePerformanceNow( + JSContextRef ctx, + JSObjectRef function, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef *exception); static JSValueRef evaluateScriptWithJSC( JSGlobalContextRef ctx, @@ -110,6 +121,7 @@ JSCExecutor::JSCExecutor(FlushImmediateCallback cb) : s_globalContextRefToJSCExecutor[m_context] = this; installGlobalFunction(m_context, "nativeFlushQueueImmediate", nativeFlushQueueImmediate); installGlobalFunction(m_context, "nativeLoggingHook", nativeLoggingHook); + installGlobalFunction(m_context, "nativePerformanceNow", nativePerformanceNow); #ifdef WITH_FB_JSC_TUNING configureJSCForAndroid(); @@ -283,4 +295,17 @@ static JSValueRef nativeLoggingHook( return JSValueMakeUndefined(ctx); } +static JSValueRef nativePerformanceNow( + JSContextRef ctx, + JSObjectRef function, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], JSValueRef *exception) { + // This is equivalent to android.os.SystemClock.elapsedRealtime() in native + struct timespec now; + clock_gettime(CLOCK_MONOTONIC_RAW, &now); + int64_t nano = now.tv_sec * NANOSECONDS_IN_SECOND + now.tv_nsec; + return JSValueMakeNumber(ctx, (nano / (double)NANOSECONDS_IN_MILLISECOND)); +} + } }