summaryrefslogtreecommitdiffstats
path: root/tools/lint/eslint/eslint-plugin-spidermonkey-js/lib/environments/self-hosted.js
blob: 37ae42bfa33678c4fc48babc4ea7c5e5c8ab7ef1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
 * @fileoverview Add environment defaults to SpiderMonkey's self-hosted JS.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

"use strict";

const path = require("path");
const fs = require("fs");

let gRootDir = null;

// Copied from `tools/lint/eslint/eslint-plugin-mozilla/lib/helpers.js`.
function getRootDir() {
  if (!gRootDir) {
    function searchUpForIgnore(dirName, filename) {
      let parsed = path.parse(dirName);
      while (parsed.root !== dirName) {
        if (fs.existsSync(path.join(dirName, filename))) {
          return dirName;
        }
        // Move up a level
        dirName = parsed.dir;
        parsed = path.parse(dirName);
      }
      return null;
    }

    let possibleRoot = searchUpForIgnore(
      path.dirname(module.filename),
      ".eslintignore"
    );
    if (!possibleRoot) {
      possibleRoot = searchUpForIgnore(path.resolve(), ".eslintignore");
    }
    if (!possibleRoot) {
      possibleRoot = searchUpForIgnore(path.resolve(), "package.json");
    }
    if (!possibleRoot) {
      // We've couldn't find a root from the module or CWD, so lets just go
      // for the CWD. We really don't want to throw if possible, as that
      // tends to give confusing results when used with ESLint.
      possibleRoot = process.cwd();
    }

    gRootDir = possibleRoot;
  }

  return gRootDir;
}

function tryReadFile(filePath) {
  let absPath = path.join(getRootDir(), filePath);
  if (!fs.existsSync(absPath)) {
    // Safely handle the case when the file wasn't found, because throwing
    // errors can lead to confusing result when used with ESLint.
    return "";
  }
  return fs.readFileSync(absPath, "utf-8");
}

// Search for top-level declarations, #defines, and #includes.
function addGlobalsFrom(dirName, fileName, globals) {
  let filePath = path.join(dirName, fileName);

  // Definitions are separated by line.
  let lines = tryReadFile(filePath).split("\n");

  // We don't have to fully parse the source code, because it's formatted
  // through "prettier", which means we know the exact code structure.
  //
  // |class| is disallowed in self-hosted code, so we don't have to handle it.
  for (let line of lines) {
    if (
      line.startsWith("function") ||
      line.startsWith("function*") ||
      line.startsWith("async function") ||
      line.startsWith("async function*")
    ) {
      let m = line.match(/^(?:async )?function(?:\*)?\s+([\w\$]+)\s*\(/);
      if (m) {
        globals[m[1]] = "readonly";
      }
    } else if (
      line.startsWith("var") ||
      line.startsWith("let") ||
      line.startsWith("const")
    ) {
      let m = line.match(/^(?:var|let|const)\s+([\w\$]+)\s*[;=]/);
      if (m) {
        globals[m[1]] = "readonly";
      }
    } else if (line.startsWith("#define")) {
      let m = line.match(/^#define (\w+)/);
      if (m) {
        globals[m[1]] = "readonly";
      }
    } else if (line.startsWith("#include")) {
      let m = line.match(/^#include \"([\w\.]+)\"$/);
      if (m) {
        // Also process definitions from includes.
        addGlobalsFrom(dirName, m[1], globals);
      }
    }
  }
}

function selfHostingDefines(dirName = "js/src/builtin/") {
  let absDir = path.join(getRootDir(), dirName);
  if (!fs.existsSync(absDir)) {
    // See |tryReadFile| for why we avoid to throw any errors.
    return {};
  }

  // Search sub-directories and js-files within |dirName|.
  let dirs = [];
  let jsFiles = [];
  for (let name of fs.readdirSync(absDir)) {
    let stat = fs.statSync(path.join(absDir, name));
    if (stat.isDirectory()) {
      dirs.push(name);
    } else if (stat.isFile() && name.endsWith(".js")) {
      jsFiles.push(name);
    }
  }

  let globals = Object.create(null);

  // Process each js-file.
  for (let jsFile of jsFiles) {
    addGlobalsFrom(dirName, jsFile, globals);
  }

  // Recursively traverse all sub-directories.
  for (let dir of dirs) {
    globals = { ...globals, ...selfHostingDefines(path.join(dirName, dir)) };
  }

  return globals;
}

function selfHostingFunctions() {
  // Definitions can be spread across multiple lines and may have extra
  // whitespace, so we simply remove all whitespace and match over the complete
  // file.
  let content = tryReadFile("js/src/vm/SelfHosting.cpp").replace(/\s+/g, "");

  let globals = Object.create(null);
  for (let m of content.matchAll(/(?:JS_FN|JS_INLINABLE_FN)\("(\w+)"/g)) {
    globals[m[1]] = "readonly";
  }
  return globals;
}

function errorNumbers() {
  // Definitions are separated by line.
  let lines = tryReadFile("js/public/friend/ErrorNumbers.msg").split("\n");

  let globals = Object.create(null);
  for (let line of lines) {
    let m = line.match(/^MSG_DEF\((\w+),/);
    if (m) {
      globals[m[1]] = "readonly";
    }
  }
  return globals;
}

const globals = {
  ...selfHostingDefines(),
  ...selfHostingFunctions(),
  ...errorNumbers(),
};

module.exports = {
  globals,
};