diff options
author | 2021-10-30 01:19:10 -0700 | |
---|---|---|
committer | 2021-10-30 01:19:10 -0700 | |
commit | 49b954462d2e027e03f44938181d33f78d6416e6 (patch) | |
tree | 1d7d26e6c00100ea9b2fac8f45fb03af60239e22 | |
parent | c51dac1282b2a24b99258dbfe37e29d355d3e47e (diff) | |
download | bun-49b954462d2e027e03f44938181d33f78d6416e6.tar.gz bun-49b954462d2e027e03f44938181d33f78d6416e6.tar.zst bun-49b954462d2e027e03f44938181d33f78d6416e6.zip |
[Bun.js] Add performance.now()
-rw-r--r-- | src/javascript/jsc/javascript.zig | 40 |
1 files changed, 40 insertions, 0 deletions
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; |