summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorVincent LE GOFF <g_n_s@hotmail.fr>2019-04-08 22:22:40 +0200
committerRyan Dahl <ry@tinyclouds.org>2019-04-08 16:22:40 -0400
commit734cf781c6e606a8a836863a391c94cf4fad22d7 (patch)
treed55d5212bdeedf4b9b1e53893478489ec960551a /js
parentf7fdb90fd51e340ea598c055bb3573d3cdfbdaa8 (diff)
Allow high precision performance.now() (#1977)
Diffstat (limited to 'js')
-rw-r--r--js/performance.ts12
-rw-r--r--js/performance_test.ts4
-rw-r--r--js/permissions.ts5
-rw-r--r--js/permissions_test.ts3
-rw-r--r--js/test_util.ts31
5 files changed, 36 insertions, 19 deletions
diff --git a/js/performance.ts b/js/performance.ts
index 1af75809b..51e213f0f 100644
--- a/js/performance.ts
+++ b/js/performance.ts
@@ -5,13 +5,9 @@ import * as flatbuffers from "./flatbuffers";
import { assert } from "./util";
export class Performance {
- timeOrigin = 0;
-
- constructor() {
- this.timeOrigin = new Date().getTime();
- }
-
- /** Returns a current time from Deno's start
+ /** Returns a current time from Deno's start.
+ * In milliseconds. Flag --allow-high-precision give
+ * a precise measure.
*
* const t = performance.now();
* console.log(`${t} ms since start!`);
@@ -23,6 +19,6 @@ export class Performance {
assert(msg.Any.NowRes === baseRes.innerType());
const res = new msg.NowRes();
assert(baseRes.inner(res) != null);
- return res.time().toFloat64() - this.timeOrigin;
+ return res.seconds().toFloat64() * 1e3 + res.subsecNanos() / 1e6;
}
}
diff --git a/js/performance_test.ts b/js/performance_test.ts
index 154dadbbe..8f9dbc990 100644
--- a/js/performance_test.ts
+++ b/js/performance_test.ts
@@ -1,7 +1,7 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-import { test, assert } from "./test_util.ts";
+import { testPerm, assert } from "./test_util.ts";
-test(function now() {
+testPerm({ highPrecision: false }, function now() {
const start = performance.now();
setTimeout(() => {
const end = performance.now();
diff --git a/js/permissions.ts b/js/permissions.ts
index 46c809cf6..d0885ba6a 100644
--- a/js/permissions.ts
+++ b/js/permissions.ts
@@ -11,7 +11,7 @@ export interface Permissions {
net: boolean;
env: boolean;
run: boolean;
-
+ highPrecision: boolean;
// NOTE: Keep in sync with src/permissions.rs
}
@@ -29,7 +29,8 @@ function createPermissions(inner: msg.PermissionsRes): Permissions {
write: inner.write(),
net: inner.net(),
env: inner.env(),
- run: inner.run()
+ run: inner.run(),
+ highPrecision: inner.highPrecision()
};
}
diff --git a/js/permissions_test.ts b/js/permissions_test.ts
index e44be79bb..c49696c01 100644
--- a/js/permissions_test.ts
+++ b/js/permissions_test.ts
@@ -6,7 +6,8 @@ const knownPermissions: Deno.Permission[] = [
"read",
"write",
"net",
- "env"
+ "env",
+ "highPrecision"
];
for (let grant of knownPermissions) {
diff --git a/js/test_util.ts b/js/test_util.ts
index e683d32b1..15bbb521b 100644
--- a/js/test_util.ts
+++ b/js/test_util.ts
@@ -26,6 +26,7 @@ interface DenoPermissions {
net?: boolean;
env?: boolean;
run?: boolean;
+ highPrecision?: boolean;
}
function permToString(perms: DenoPermissions): string {
@@ -34,11 +35,12 @@ function permToString(perms: DenoPermissions): string {
const n = perms.net ? 1 : 0;
const e = perms.env ? 1 : 0;
const u = perms.run ? 1 : 0;
- return `permR${r}W${w}N${n}E${e}U${u}`;
+ const h = perms.highPrecision ? 1 : 0;
+ return `permR${r}W${w}N${n}E${e}U${u}H${h}`;
}
function permFromString(s: string): DenoPermissions {
- const re = /^permR([01])W([01])N([01])E([01])U([01])$/;
+ const re = /^permR([01])W([01])N([01])E([01])U([01])H([01])$/;
const found = s.match(re);
if (!found) {
throw Error("Not a permission string");
@@ -48,7 +50,8 @@ function permFromString(s: string): DenoPermissions {
write: Boolean(Number(found[2])),
net: Boolean(Number(found[3])),
env: Boolean(Number(found[4])),
- run: Boolean(Number(found[5]))
+ run: Boolean(Number(found[5])),
+ highPrecision: Boolean(Number(found[6]))
};
}
@@ -62,7 +65,14 @@ export function testPerm(
export function test(fn: testing.TestFunction): void {
testPerm(
- { read: false, write: false, net: false, env: false, run: false },
+ {
+ read: false,
+ write: false,
+ net: false,
+ env: false,
+ run: false,
+ highPrecision: false
+ },
fn
);
}
@@ -73,8 +83,17 @@ test(function permSerialization() {
for (const env of [true, false]) {
for (const run of [true, false]) {
for (const read of [true, false]) {
- const perms: DenoPermissions = { write, net, env, run, read };
- assertEquals(perms, permFromString(permToString(perms)));
+ for (const highPrecision of [true, false]) {
+ const perms: DenoPermissions = {
+ write,
+ net,
+ env,
+ run,
+ read,
+ highPrecision
+ };
+ assertEquals(perms, permFromString(permToString(perms)));
+ }
}
}
}