Skip to content

Instantly share code, notes, and snippets.

@vext01
Created June 23, 2015 16:04
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/68e3c94417cf8cf77348 to your computer and use it in GitHub Desktop.
Save vext01/68e3c94417cf8cf77348 to your computer and use it in GitHub Desktop.
diff --git a/src/bootstrapper.cc b/src/bootstrapper.cc
index ae55f2f..b3be518 100644
--- a/src/bootstrapper.cc
+++ b/src/bootstrapper.cc
@@ -1417,6 +1417,18 @@ void Genesis::InitializeGlobal(Handle<GlobalObject> global_object,
delegate->shared()->DontAdaptArguments();
}
+ {
+ // Set up the call-as-function delegate.
+ Handle<Code> code =
+ Handle<Code>(isolate->builtins()->builtin(
+ Builtins::kClockGettimeMonotonic));
+ Handle<String> internalized_name = factory->InternalizeUtf8String("clock_gettime_monotonic");
+ Handle<JSFunction> delegate = factory->NewFunction(
+ internalized_name, code, JS_OBJECT_TYPE, JSObject::kHeaderSize);
+ native_context()->set_call_as_function_delegate(*delegate);
+ delegate->shared()->DontAdaptArguments();
+ }
+
// Initialize the embedder data slot.
Handle<FixedArray> embedder_data = factory->NewFixedArray(3);
native_context()->set_embedder_data(*embedder_data);
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