From 49b954462d2e027e03f44938181d33f78d6416e6 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Sat, 30 Oct 2021 01:19:10 -0700 Subject: [Bun.js] Add performance.now() --- src/javascript/jsc/javascript.zig | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/javascript/jsc/javascript.zig b/src/javascript/jsc/javascript.zig index 574f1a158..8ec2ba3ed 100644 --- a/src/javascript/jsc/javascript.zig +++ b/src/javascript/jsc/javascript.zig @@ -40,6 +40,7 @@ pub const GlobalClasses = [_]type{ Bun.Class, Fetch.Class, js_ast.Macro.JSNode.BunJSXCallbackFunction, + Performance.Class, // The last item in this array becomes "process.env" Bun.EnvironmentVariables.Class, @@ -639,6 +640,42 @@ pub const Bun = struct { }; }; +pub const Performance = struct { + pub const Class = NewClass( + void, + .{ + .name = "performance", + .read_only = true, + }, + .{ + .now = .{ + .rfn = Performance.now, + }, + }, + .{}, + ); + + pub fn now( + this: void, + ctx: js.JSContextRef, + function: js.JSObjectRef, + thisObject: js.JSObjectRef, + arguments: []const js.JSValueRef, + exception: js.ExceptionRef, + ) js.JSValueRef { + return js.JSValueMakeNumber( + ctx, + @floatCast( + f64, + @intToFloat( + f128, + VirtualMachine.vm.origin_timer.read(), + ) / std.time.ns_per_ms, + ), + ); + } +}; + const bun_file_import_path = "/node_modules.server.bun"; pub const LazyClasses = [_]type{}; @@ -679,6 +716,8 @@ pub const VirtualMachine = struct { has_any_macro_remappings: bool = false, + origin_timer: std.time.Timer = undefined, + pub const MacroMap = std.AutoArrayHashMap(i32, js.JSObjectRef); pub threadlocal var vm_loaded = false; @@ -735,6 +774,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."), }; vm.bundler.macro_context = null; -- cgit v1.2.3