From 1b66ea9184312fb96c9dbed97482533a6809893a Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Tue, 28 Apr 2020 00:47:21 +0200 Subject: [PATCH] Fix ReferenceError --- release/main.js | 17 +++++++++++++++++ src/grammar.ts | 23 +++++++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/release/main.js b/release/main.js index 781fcc2..91e596a 100644 --- a/release/main.js +++ b/release/main.js @@ -2272,6 +2272,15 @@ var utils_1 = require("./utils"); var rule_1 = require("./rule"); var matcher_1 = require("./matcher"); var debug_1 = require("./debug"); +var performanceNow = (function () { + if (typeof performance === 'undefined') { + // performance.now() is not available in this environment, so use Date.now() + return function () { return Date.now(); }; + } + else { + return function () { return performance.now(); }; + } +})(); function createGrammar(grammar, initialLanguage, embeddedLanguages, tokenTypes, grammarRepository, onigLib) { return new Grammar(grammar, initialLanguage, embeddedLanguages, tokenTypes, grammarRepository, onigLib); //TODO } @@ -2829,8 +2838,16 @@ function matchInjections(injections, grammar, lineText, isFirstLine, linePos, st function matchRule(grammar, lineText, isFirstLine, linePos, stack, anchorPosition) { var rule = stack.getRule(grammar); var ruleScanner = rule.compile(grammar, stack.endRule, isFirstLine, linePos === anchorPosition); + var perfStart = 0; + if (debug_1.DebugFlags.InDebugMode) { + perfStart = performanceNow(); + } var r = ruleScanner.scanner.findNextMatchSync(lineText, linePos); if (debug_1.DebugFlags.InDebugMode) { + var elapsedMillis = performanceNow() - perfStart; + if (elapsedMillis > 5) { + console.warn("Rule " + rule.debugName + " (" + rule.id + ") matching took " + elapsedMillis + " against '" + lineText + "'"); + } // console.log(` scanning for (linePos: ${linePos}, anchorPosition: ${anchorPosition})`); // console.log(debugCompiledRuleToString(ruleScanner)); if (r) { diff --git a/src/grammar.ts b/src/grammar.ts index b712a23..d621f6a 100644 --- a/src/grammar.ts +++ b/src/grammar.ts @@ -11,9 +11,14 @@ import { DebugFlags } from './debug'; import { FontStyle, ThemeTrieElementRule } from './theme'; declare let performance: { now: () => number } | undefined; -if (typeof process !== 'undefined' && process.release.name === 'node') { - performance = require('perf_hooks').performance; -} +const performanceNow = (function() { + if (typeof performance === 'undefined') { + // performance.now() is not available in this environment, so use Date.now() + return () => Date.now(); + } else { + return () => performance!.now(); + } +})(); export const enum TemporaryStandardTokenType { Other = 0, @@ -710,18 +715,16 @@ function matchRule(grammar: Grammar, lineText: OnigString, isFirstLine: boolean, const ruleScanner = rule.compile(grammar, stack.endRule, isFirstLine, linePos === anchorPosition); let perfStart = 0; - if (DebugFlags.InDebugMode && performance) { - perfStart = performance.now(); + if (DebugFlags.InDebugMode) { + perfStart = performanceNow(); } const r = ruleScanner.scanner.findNextMatchSync(lineText, linePos); if (DebugFlags.InDebugMode) { - if (performance) { - const timeMillis = performance.now() - perfStart; - if (timeMillis > 5) { - console.warn(`Rule ${rule.debugName} (${rule.id}) matching took ${timeMillis} against '${lineText}'`); - } + const elapsedMillis = performanceNow() - perfStart; + if (elapsedMillis > 5) { + console.warn(`Rule ${rule.debugName} (${rule.id}) matching took ${elapsedMillis} against '${lineText}'`); } // console.log(` scanning for (linePos: ${linePos}, anchorPosition: ${anchorPosition})`); // console.log(debugCompiledRuleToString(ruleScanner));