Skip to content

Instantly share code, notes, and snippets.

@vext01
Created June 23, 2015 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vext01/8113498639589121bc9a to your computer and use it in GitHub Desktop.
Save vext01/8113498639589121bc9a to your computer and use it in GitHub Desktop.
diff --git a/src/bootstrapper.cc b/src/bootstrapper.cc
index ae55f2f..5b50cfb 100644
--- a/src/bootstrapper.cc
+++ b/src/bootstrapper.cc
@@ -1273,6 +1273,12 @@ void Genesis::InitializeGlobal(Handle<GlobalObject> global_object,
InstallFunction(global, "WeakSet", JS_WEAK_SET_TYPE, JSWeakSet::kSize,
isolate->initial_object_prototype(), Builtins::kIllegal);
+ // clock_gettime_monotonic()
+ InstallFunction(
+ global, "clock_gettime_monotonic", JS_OBJECT_TYPE,
+ JSObject::kHeaderSize, MaybeHandle<JSObject>(),
+ Builtins::kClockGettimeMonotonic);
+
{ // --- sloppy arguments map
// Make sure we can recognize argument objects at runtime.
// This is done by introducing an anonymous function with
diff --git a/src/builtins.cc b/src/builtins.cc
index 7ac60da..b51a9e7 100644
--- a/src/builtins.cc
+++ b/src/builtins.cc
@@ -20,6 +20,23 @@
#include "src/prototype.h"
#include "src/vm-state-inl.h"
+
+/*
+ * Stuff for clock_gettime_monotonic()
+ */
+#include <time.h>
+#include <stdlib.h>
+#include <math.h>
+#include <errno.h>
+#include <stdio.h>
+
+#if defined(__linux__)
+#define ACTUAL_CLOCK_MONOTONIC CLOCK_MONOTONIC_RAW
+#else
+#define ACTUAL_CLOCK_MONOTONIC CLOCK_MONOTONIC
+#endif
+
+
namespace v8 {
namespace internal {
@@ -1013,6 +1030,22 @@ BUILTIN(ArrayConcat) {
}
+BUILTIN(ClockGettimeMonotonic) {
+ HandleScope scope(isolate);
+
+ struct timespec ts;
+ double result;
+
+ if ((clock_gettime(ACTUAL_CLOCK_MONOTONIC, &ts)) < 0) {
+ perror("clock_gettime");
+ exit(1);
+ }
+
+ result = ts.tv_sec + ts.tv_nsec * pow(10, -9);
+
+ return *isolate->factory()->NewNumber(result);
+}
+
// -----------------------------------------------------------------------------
// Throwers for restricted function properties and strict arguments object
// properties
diff --git a/src/builtins.h b/src/builtins.h
index 7d0d6c2..754905f 100644
--- a/src/builtins.h
+++ b/src/builtins.h
@@ -61,7 +61,9 @@ enum BuiltinExtraArguments {
V(HandleApiCallAsConstructor, NO_EXTRA_ARGUMENTS) \
\
V(RestrictedFunctionPropertiesThrower, NO_EXTRA_ARGUMENTS) \
- V(RestrictedStrictArgumentsPropertiesThrower, NO_EXTRA_ARGUMENTS)
+ V(RestrictedStrictArgumentsPropertiesThrower, NO_EXTRA_ARGUMENTS) \
+ \
+ V(ClockGettimeMonotonic, NO_EXTRA_ARGUMENTS)
// Define list of builtins implemented in assembly.
#define BUILTIN_LIST_A(V) \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment