DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6d82b1a6b02)

VCS Links

Line Code
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * 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/. */

#include "frontend/SharedContext.h"

#include "frontend/ModuleSharedContext.h"

#include "frontend/ParseContext-inl.h"
#include "vm/EnvironmentObject-inl.h"

namespace js {
namespace frontend {

void SharedContext::computeAllowSyntax(Scope* scope) {
  for (ScopeIter si(scope); si; si++) {
    if (si.kind() == ScopeKind::Function) {
      FunctionScope* funScope = &si.scope()->as<FunctionScope>();
      JSFunction* fun = funScope->canonicalFunction();
      if (fun->isArrow()) {
        continue;
      }
      allowNewTarget_ = true;
      allowSuperProperty_ = fun->allowSuperProperty();
      allowSuperCall_ = fun->isDerivedClassConstructor();
      if (funScope->isFieldInitializer() == IsFieldInitializer::Yes) {
        allowSuperCall_ = false;
        allowArguments_ = false;
      }
      return;
    }
  }
}

void SharedContext::computeThisBinding(Scope* scope) {
  for (ScopeIter si(scope); si; si++) {
    if (si.kind() == ScopeKind::Module) {
      thisBinding_ = ThisBinding::Module;
      return;
    }

    if (si.kind() == ScopeKind::Function) {
      JSFunction* fun = si.scope()->as<FunctionScope>().canonicalFunction();

      // Arrow functions don't have their own `this` binding.
      if (fun->isArrow()) {
        continue;
      }

      // Derived class constructors (including nested arrow functions and
      // eval) need TDZ checks when accessing |this|.
      if (fun->isDerivedClassConstructor()) {
        needsThisTDZChecks_ = true;
      }

      thisBinding_ = ThisBinding::Function;
      return;
    }
  }

  thisBinding_ = ThisBinding::Global;
}

void SharedContext::computeInWith(Scope* scope) {
  for (ScopeIter si(scope); si; si++) {
    if (si.kind() == ScopeKind::With) {
      inWith_ = true;
      break;
    }
  }
}

EvalSharedContext::EvalSharedContext(JSContext* cx, JSObject* enclosingEnv,
                                     Scope* enclosingScope,
                                     Directives directives, bool extraWarnings)
    : SharedContext(cx, Kind::Eval, directives, extraWarnings),
      enclosingScope_(cx, enclosingScope),
      bindings(cx) {
  computeAllowSyntax(enclosingScope);
  computeInWith(enclosingScope);
  computeThisBinding(enclosingScope);

  // If this eval is in response to Debugger.Frame.eval, we may have been
  // passed an incomplete scope chain. In order to better determine the 'this'
  // binding type, we traverse the environment chain, looking for a CallObject
  // and recompute the binding type based on its body scope.
  //
  // NOTE: A non-debug eval in a non-syntactic environment will also trigger
  // this code. In that case, we should still compute the same binding type.
  if (enclosingEnv && enclosingScope->hasOnChain(ScopeKind::NonSyntactic)) {
    JSObject* env = enclosingEnv;
    while (env) {
      // Look at target of any DebugEnvironmentProxy, but be sure to use
      // enclosingEnvironment() of the proxy itself.
      JSObject* unwrapped = env;
      if (env->is<DebugEnvironmentProxy>()) {
        unwrapped = &env->as<DebugEnvironmentProxy>().environment();
      }

      if (unwrapped->is<CallObject>()) {
        JSFunction* callee = &unwrapped->as<CallObject>().callee();
        computeThisBinding(callee->nonLazyScript()->bodyScope());
        break;
      }

      env = env->enclosingEnvironment();
    }
  }
}

#ifdef DEBUG
bool FunctionBox::atomsAreKept() { return cx_->zone()->hasKeptAtoms(); }
#endif

FunctionBox::FunctionBox(JSContext* cx, TraceListNode* traceListHead,
                         uint32_t toStringStart, Directives directives,
                         bool extraWarnings, GeneratorKind generatorKind,
                         FunctionAsyncKind asyncKind, bool isArrow,
                         bool isNamedLambda, bool isGetter, bool isSetter,
                         bool isMethod, bool isInterpreted,
                         bool isInterpretedLazy,
                         FunctionFlags::FunctionKind kind, JSAtom* explicitName)
    : ObjectBox(traceListHead),
      SharedContext(cx, Kind::FunctionBox, directives, extraWarnings),
      enclosingScope_(nullptr),
      namedLambdaBindings_(nullptr),
      functionScopeBindings_(nullptr),
      extraVarScopeBindings_(nullptr),
      functionNode(nullptr),
      bufStart(0),
      bufEnd(0),
      startLine(1),
      startColumn(0),
      toStringStart(toStringStart),
      toStringEnd(0),
      length(0),
      isGenerator_(generatorKind == GeneratorKind::Generator),
      isAsync_(asyncKind == FunctionAsyncKind::AsyncFunction),
      hasDestructuringArgs(false),
      hasParameterExprs(false),
      hasDirectEvalInParameterExpr(false),
      hasDuplicateParameters(false),
      useAsm(false),
      isAnnexB(false),
      wasEmitted(false),
      declaredArguments(false),
      usesArguments(false),
      usesApply(false),
      usesThis(false),
      usesReturn(false),
      hasRest_(false),
      hasExprBody_(false),
      hasExtensibleScope_(false),
      argumentsHasLocalBinding_(false),
      definitelyNeedsArgsObj_(false),
      needsHomeObject_(false),
      isDerivedClassConstructor_(false),
      hasThisBinding_(false),
      hasInnerFunctions_(false),
      isArrow_(isArrow),
      isNamedLambda_(isNamedLambda),
      isGetter_(isGetter),
      isSetter_(isSetter),
      isMethod_(isMethod),
      isInterpreted_(isInterpreted),
      isInterpretedLazy_(isInterpretedLazy),
      kind_(kind),
      explicitName_(explicitName),
      nargs_(0) {}

FunctionBox::FunctionBox(JSContext* cx, TraceListNode* traceListHead,
                         JSFunction* fun, uint32_t toStringStart,
                         Directives directives, bool extraWarnings,
                         GeneratorKind generatorKind,
                         FunctionAsyncKind asyncKind)
    : FunctionBox(cx, traceListHead, toStringStart, directives, extraWarnings,
                  generatorKind, asyncKind, fun->isArrow(),
                  fun->isNamedLambda(), fun->isGetter(), fun->isSetter(),
                  fun->isMethod(), fun->isInterpreted(),
                  fun->isInterpretedLazy(), fun->kind(), fun->explicitName()) {
  gcThing = fun;
  // Functions created at parse time may be set singleton after parsing and
  // baked into JIT code, so they must be allocated tenured. They are held by
  // the JSScript so cannot be collected during a minor GC anyway.
  MOZ_ASSERT(fun->isTenured());
}

FunctionBox::FunctionBox(JSContext* cx, TraceListNode* traceListHead,
                         Handle<FunctionCreationData> data,
                         uint32_t toStringStart, Directives directives,
                         bool extraWarnings, GeneratorKind generatorKind,
                         FunctionAsyncKind asyncKind)
    : FunctionBox(cx, traceListHead, toStringStart, directives, extraWarnings,
                  generatorKind, asyncKind, data.get().flags.isArrow(),
                  data.get().flags.isNamedLambda(data.get().atom),
                  data.get().flags.isGetter(), data.get().flags.isSetter(),
                  data.get().flags.isMethod(), data.get().flags.isInterpreted(),
                  data.get().flags.isInterpretedLazy(), data.get().flags.kind(),
                  data.get().atom) {
  functionCreationData_.emplace(data);
}

void FunctionBox::initFromLazyFunction(JSFunction* fun) {
  if (fun->lazyScript()->isDerivedClassConstructor()) {
    setDerivedClassConstructor();
  }
  if (fun->lazyScript()->needsHomeObject()) {
    setNeedsHomeObject();
  }
  if (fun->lazyScript()->hasEnclosingScope()) {
    enclosingScope_ = fun->lazyScript()->enclosingScope();
  } else {
    enclosingScope_ = nullptr;
  }
  initWithEnclosingScope(enclosingScope_, fun);
}

void FunctionBox::initStandaloneFunction(Scope* enclosingScope) {
  // Standalone functions are Function or Generator constructors and are
  // always scoped to the global.
  MOZ_ASSERT(enclosingScope->is<GlobalScope>());
  enclosingScope_ = enclosingScope;
  allowNewTarget_ = true;
  thisBinding_ = ThisBinding::Function;
}

void FunctionBox::initWithEnclosingParseContext(ParseContext* enclosing,
                                                FunctionSyntaxKind kind,
                                                bool isArrow,
                                                bool allowSuperProperty) {
  SharedContext* sc = enclosing->sc();
  useAsm = sc->isFunctionBox() && sc->asFunctionBox()->useAsmOrInsideUseAsm();

  // Arrow functions don't have their own `this` binding.
  if (isArrow) {
    allowNewTarget_ = sc->allowNewTarget();
    allowSuperProperty_ = sc->allowSuperProperty();
    allowSuperCall_ = sc->allowSuperCall();
    allowArguments_ = sc->allowArguments();
    needsThisTDZChecks_ = sc->needsThisTDZChecks();
    thisBinding_ = sc->thisBinding();
  } else {
    allowNewTarget_ = true;
    allowSuperProperty_ = allowSuperProperty;

    if (IsConstructorKind(kind)) {
      auto stmt =
          enclosing->findInnermostStatement<ParseContext::ClassStatement>();
      MOZ_ASSERT(stmt);
      stmt->constructorBox = this;

      if (kind == FunctionSyntaxKind::DerivedClassConstructor) {
        setDerivedClassConstructor();
        allowSuperCall_ = true;
        needsThisTDZChecks_ = true;
      }
    }

    thisBinding_ = ThisBinding::Function;
  }

  if (sc->inWith()) {
    inWith_ = true;
  } else {
    auto isWith = [](ParseContext::Statement* stmt) {
      return stmt->kind() == StatementKind::With;
    };

    inWith_ = enclosing->findInnermostStatement(isWith);
  }
}

void FunctionBox::initFieldInitializer(ParseContext* enclosing,
                                       Handle<FunctionCreationData> data,
                                       HasHeritage hasHeritage) {
  this->initWithEnclosingParseContext(enclosing, data,
                                      FunctionSyntaxKind::Expression);
  allowSuperProperty_ = true;
  allowSuperCall_ = false;
  allowArguments_ = false;
  needsThisTDZChecks_ = hasHeritage == HasHeritage::Yes;
}

void FunctionBox::initWithEnclosingScope(Scope* enclosingScope,
                                         JSFunction* fun) {
  if (!isArrow()) {
    allowNewTarget_ = true;
    allowSuperProperty_ = fun->allowSuperProperty();

    if (isDerivedClassConstructor()) {
      setDerivedClassConstructor();
      allowSuperCall_ = true;
      needsThisTDZChecks_ = true;
    }

    thisBinding_ = ThisBinding::Function;
  } else {
    computeAllowSyntax(enclosingScope);
    computeThisBinding(enclosingScope);
  }

  computeInWith(enclosingScope);
}

void FunctionBox::setEnclosingScopeForInnerLazyFunction(Scope* enclosingScope) {
  MOZ_ASSERT(isLazyFunctionWithoutEnclosingScope());

  // For lazy functions inside a function which is being compiled, we cache
  // the incomplete scope object while compiling, and store it to the
  // LazyScript once the enclosing script successfully finishes compilation
  // in FunctionBox::finish.
  enclosingScope_ = enclosingScope;
}

void FunctionBox::finish() {
  if (!isLazyFunctionWithoutEnclosingScope()) {
    return;
  }
  MOZ_ASSERT(enclosingScope_);
  function()->lazyScript()->setEnclosingScope(enclosingScope_);
}

ModuleSharedContext::ModuleSharedContext(JSContext* cx, ModuleObject* module,
                                         Scope* enclosingScope,
                                         ModuleBuilder& builder)
    : SharedContext(cx, Kind::Module, Directives(true), false),
      module_(cx, module),
      enclosingScope_(cx, enclosingScope),
      bindings(cx),
      builder(builder) {
  thisBinding_ = ThisBinding::Module;
}

}  // namespace frontend

}  // namespace js