aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/javascript.zig
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-10-18 13:36:44 -0700
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2022-10-18 13:36:44 -0700
commit693b1c9b2329a016901dd5b45146dc1cf4a9dab8 (patch)
tree5e17cf214c33ed07c2333e7ce8bd9d1dbfdd00d2 /src/bun.js/javascript.zig
parentce0efc37cc99cb9476697afc232aafcb57f48051 (diff)
downloadbun-693b1c9b2329a016901dd5b45146dc1cf4a9dab8.tar.gz
bun-693b1c9b2329a016901dd5b45146dc1cf4a9dab8.tar.zst
bun-693b1c9b2329a016901dd5b45146dc1cf4a9dab8.zip
Implement `performance.timeOrigin`
Diffstat (limited to 'src/bun.js/javascript.zig')
-rw-r--r--src/bun.js/javascript.zig13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/bun.js/javascript.zig b/src/bun.js/javascript.zig
index 2e733b188..c163d99ae 100644
--- a/src/bun.js/javascript.zig
+++ b/src/bun.js/javascript.zig
@@ -262,6 +262,12 @@ export fn Bun__readOriginTimer(vm: *JSC.VirtualMachine) u64 {
return vm.origin_timer.read();
}
+export fn Bun__readOriginTimerStart(vm: *JSC.VirtualMachine) f64 {
+ // timespce to milliseconds
+ // use f128 to reduce precision loss when converting to f64
+ return @floatCast(f64, (@intToFloat(f128, vm.origin_timestamp) + JSC.VirtualMachine.origin_relative_epoch) / 1_000_000.0);
+}
+
comptime {
if (!JSC.is_bindgen) {
_ = Bun__getDefaultGlobal;
@@ -271,6 +277,7 @@ comptime {
_ = Bun__handleRejectedPromise;
_ = Bun__readOriginTimer;
_ = Bun__onDidAppendPlugin;
+ _ = Bun__readOriginTimerStart;
}
}
@@ -365,6 +372,7 @@ pub const VirtualMachine = struct {
global_api_constructors: [GlobalConstructors.len]JSC.JSValue = undefined,
origin_timer: std.time.Timer = undefined,
+ origin_timestamp: u64 = 0,
macro_event_loop: EventLoop = EventLoop{},
regular_event_loop: EventLoop = EventLoop{},
event_loop: *EventLoop = undefined,
@@ -527,6 +535,10 @@ pub const VirtualMachine = struct {
return this.bun_dev_watcher != null or this.bun_watcher != null;
}
+ /// Instead of storing timestamp as a i128, we store it as a u64.
+ /// We subtract the timestamp from Jan 1, 2000 (Y2K)
+ pub const origin_relative_epoch = 975628800000 * std.time.ns_per_ms;
+
pub fn init(
allocator: std.mem.Allocator,
_args: Api.TransformOptions,
@@ -570,6 +582,7 @@ pub const VirtualMachine = struct {
.macros = MacroMap.init(allocator),
.macro_entry_points = @TypeOf(VirtualMachine.vm.macro_entry_points).init(allocator),
.origin_timer = std.time.Timer.start() catch @panic("Please don't mess with timers."),
+ .origin_timestamp = @truncate(u64, @intCast(u128, @maximum(std.time.nanoTimestamp(), origin_relative_epoch)) - origin_relative_epoch),
.ref_strings = JSC.RefString.Map.init(allocator),
.file_blobs = JSC.WebCore.Blob.Store.Map.init(allocator),
};