| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "builtin/Eval.h" |
| 8 |
|
| 9 |
#include "mozilla/HashFunctions.h" |
| 10 |
#include "mozilla/Range.h" |
| 11 |
|
| 12 |
#include "frontend/BytecodeCompiler.h" // frontend::CompileEvalScript |
| 13 |
#include "gc/HashUtil.h" |
| 14 |
#include "js/CompilationAndEvaluation.h" |
| 15 |
#include "js/EnvironmentChain.h" // JS::EnvironmentChain |
| 16 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 17 |
#include "js/friend/JSMEnvironment.h" // JS::NewJSMEnvironment, JS::ExecuteInJSMEnvironment, JS::GetJSMEnvironmentOfScriptedCaller, JS::IsJSMEnvironment |
| 18 |
#include "js/friend/WindowProxy.h" // js::IsWindowProxy |
| 19 |
#include "js/SourceText.h" |
| 20 |
#include "js/StableStringChars.h" |
| 21 |
#include "vm/EnvironmentObject.h" |
| 22 |
#include "vm/FrameIter.h" |
| 23 |
#include "vm/GlobalObject.h" |
| 24 |
#include "vm/Interpreter.h" |
| 25 |
#include "vm/JSContext.h" |
| 26 |
#include "vm/JSONParser.h" |
| 27 |
|
| 28 |
#include "gc/Marking-inl.h" |
| 29 |
#include "vm/EnvironmentObject-inl.h" |
| 30 |
#include "vm/JSContext-inl.h" |
| 31 |
#include "vm/Stack-inl.h" |
| 32 |
|
| 33 |
using namespace js; |
| 34 |
|
| 35 |
using mozilla::AddToHash; |
| 36 |
|
| 37 |
using JS::AutoCheckCannotGC; |
| 38 |
using JS::AutoStableStringChars; |
| 39 |
using JS::CompileOptions; |
| 40 |
using JS::SourceText; |
| 41 |
|
| 42 |
// We should be able to assert this for *any* fp->environmentChain(). |
| 43 |
static void AssertInnerizedEnvironmentChain(JSContext* cx, JSObject& env) { |
| 44 |
#ifdef DEBUG |
| 45 |
RootedObject obj(cx); |
| 46 |
for (obj = &env; obj; obj = obj->enclosingEnvironment()) { |
| 47 |
MOZ_ASSERT(!IsWindowProxy(obj)); |
| 48 |
} |
| 49 |
#endif |
| 50 |
} |
| 51 |
|
| 52 |
static bool IsEvalCacheCandidate(JSScript* script) { |
| 53 |
if (!script->isDirectEvalInFunction()) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
// Make sure there are no inner objects (which may be used directly by script |
| 58 |
// and clobbered) or inner functions (which may have wrong scope). |
| 59 |
for (JS::GCCellPtr gcThing : script->gcthings()) { |
| 60 |
if (gcThing.is<JSObject>()) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
/* static */ |
| 69 |
HashNumber EvalCacheHashPolicy::hash(const EvalCacheLookup& l) { |
| 70 |
HashNumber hash = HashStringChars(l.str); |
| 71 |
return AddToHash(hash, l.callerScript, l.pc); |
| 72 |
} |
| 73 |
|
| 74 |
/* static */ |
| 75 |
bool EvalCacheHashPolicy::match(const EvalCacheEntry& cacheEntry, |
| 76 |
const EvalCacheLookup& l) { |
| 77 |
MOZ_ASSERT(IsEvalCacheCandidate(cacheEntry.script)); |
| 78 |
|
| 79 |
return EqualStrings(cacheEntry.str, l.str) && |
| 80 |
cacheEntry.callerScript == l.callerScript && cacheEntry.pc == l.pc; |
| 81 |
} |
| 82 |
|
| 83 |
void EvalCacheLookup::trace(JSTracer* trc) { |
| 84 |
TraceNullableRoot(trc, &str, "EvalCacheLookup::str"); |
| 85 |
TraceNullableRoot(trc, &callerScript, "EvalCacheLookup::callerScript"); |
| 86 |
} |
| 87 |
|
| 88 |
// Add the script to the eval cache when EvalKernel is finished |
| 89 |
class EvalScriptGuard { |
| 90 |
JSContext* cx_; |
| 91 |
Rooted<JSScript*> script_; |
| 92 |
|
| 93 |
/* These fields are only valid if lookup_.str is non-nullptr. */ |
| 94 |
Rooted<EvalCacheLookup> lookup_; |
| 95 |
mozilla::Maybe<DependentAddPtr<EvalCache>> p_; |
| 96 |
|
| 97 |
Rooted<JSLinearString*> lookupStr_; |
| 98 |
|
| 99 |
public: |
| 100 |
explicit EvalScriptGuard(JSContext* cx) |
| 101 |
: cx_(cx), script_(cx), lookup_(cx), lookupStr_(cx) {} |
| 102 |
|
| 103 |
~EvalScriptGuard() { |
| 104 |
if (script_ && !cx_->isExceptionPending()) { |
| 105 |
script_->cacheForEval(); |
| 106 |
EvalCacheLookup& lookup = lookup_.get(); |
| 107 |
EvalCacheEntry cacheEntry = {lookupStr_, script_, lookup.callerScript, |
| 108 |
lookup.pc}; |
| 109 |
lookup.str = lookupStr_; |
| 110 |
if (lookup.str && IsEvalCacheCandidate(script_)) { |
| 111 |
// Ignore failure to add cache entry. |
| 112 |
if (!p_->add(cx_, cx_->caches().evalCache, lookup, cacheEntry)) { |
| 113 |
cx_->recoverFromOutOfMemory(); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void lookupInEvalCache(JSLinearString* str, JSScript* callerScript, |
| 120 |
jsbytecode* pc) { |
| 121 |
lookupStr_ = str; |
| 122 |
EvalCacheLookup& lookup = lookup_.get(); |
| 123 |
lookup.str = str; |
| 124 |
lookup.callerScript = callerScript; |
| 125 |
lookup.pc = pc; |
| 126 |
p_.emplace(cx_, cx_->caches().evalCache, lookup); |
| 127 |
if (*p_) { |
| 128 |
script_ = (*p_)->script; |
| 129 |
p_->remove(cx_, cx_->caches().evalCache, lookup); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
void setNewScript(JSScript* script) { |
| 134 |
// JSScript::fullyInitFromStencil has already called js_CallNewScriptHook. |
| 135 |
MOZ_ASSERT(!script_ && script); |
| 136 |
script_ = script; |
| 137 |
} |
| 138 |
|
| 139 |
bool foundScript() { return !!script_; } |
| 140 |
|
| 141 |
HandleScript script() { |
| 142 |
MOZ_ASSERT(script_); |
| 143 |
return script_; |
| 144 |
} |
| 145 |
}; |
| 146 |
|
| 147 |
enum class EvalJSONResult { Failure, Success, NotJSON }; |
| 148 |
|
| 149 |
template <typename CharT> |
| 150 |
static bool EvalStringMightBeJSON(const mozilla::Range<const CharT> chars) { |
| 151 |
// If the eval string starts with '(' or '[' and ends with ')' or ']', it |
| 152 |
// may be JSON. Try the JSON parser first because it's much faster. If |
| 153 |
// the eval string isn't JSON, JSON parsing will probably fail quickly, so |
| 154 |
// little time will be lost. |
| 155 |
size_t length = chars.length(); |
| 156 |
if (length < 2) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
// It used to be that strings in JavaScript forbid U+2028 LINE SEPARATOR |
| 161 |
// and U+2029 PARAGRAPH SEPARATOR, so something like |
| 162 |
// |
| 163 |
// eval("['" + "\u2028" + "']"); |
| 164 |
// |
| 165 |
// i.e. an array containing a string with a line separator in it, *would* |
| 166 |
// be JSON but *would not* be valid JavaScript. Handing such a string to |
| 167 |
// the JSON parser would then fail to recognize a syntax error. As of |
| 168 |
// <https://tc39.github.io/proposal-json-superset/> JavaScript strings may |
| 169 |
// contain these two code points, so it's safe to JSON-parse eval strings |
| 170 |
// that contain them. |
| 171 |
|
| 172 |
CharT first = chars[0], last = chars[length - 1]; |
| 173 |
return (first == '[' && last == ']') || (first == '(' && last == ')'); |
| 174 |
} |
| 175 |
|
| 176 |
template <typename CharT> |
| 177 |
static EvalJSONResult ParseEvalStringAsJSON( |
| 178 |
JSContext* cx, const mozilla::Range<const CharT> chars, |
| 179 |
MutableHandleValue rval) { |
| 180 |
size_t len = chars.length(); |
| 181 |
MOZ_ASSERT((chars[0] == '(' && chars[len - 1] == ')') || |
| 182 |
(chars[0] == '[' && chars[len - 1] == ']')); |
| 183 |
|
| 184 |
auto jsonChars = (chars[0] == '[') ? chars |
| 185 |
: mozilla::Range<const CharT>( |
| 186 |
chars.begin().get() + 1U, len - 2); |
| 187 |
|
| 188 |
Rooted<JSONParser<CharT>> parser( |
| 189 |
cx, cx, jsonChars, JSONParser<CharT>::ParseType::AttemptForEval); |
| 190 |
if (!parser.parse(rval)) { |
| 191 |
return EvalJSONResult::Failure; |
| 192 |
} |
| 193 |
|
| 194 |
return rval.isUndefined() ? EvalJSONResult::NotJSON : EvalJSONResult::Success; |
| 195 |
} |
| 196 |
|
| 197 |
static EvalJSONResult TryEvalJSON(JSContext* cx, JSLinearString* str, |
| 198 |
MutableHandleValue rval) { |
| 199 |
if (str->hasLatin1Chars()) { |
| 200 |
AutoCheckCannotGC nogc; |
| 201 |
if (!EvalStringMightBeJSON(str->latin1Range(nogc))) { |
| 202 |
return EvalJSONResult::NotJSON; |
| 203 |
} |
| 204 |
} else { |
| 205 |
AutoCheckCannotGC nogc; |
| 206 |
if (!EvalStringMightBeJSON(str->twoByteRange(nogc))) { |
| 207 |
return EvalJSONResult::NotJSON; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
AutoStableStringChars linearChars(cx); |
| 212 |
if (!linearChars.init(cx, str)) { |
| 213 |
return EvalJSONResult::Failure; |
| 214 |
} |
| 215 |
|
| 216 |
return linearChars.isLatin1() |
| 217 |
? ParseEvalStringAsJSON(cx, linearChars.latin1Range(), rval) |
| 218 |
: ParseEvalStringAsJSON(cx, linearChars.twoByteRange(), rval); |
| 219 |
} |
| 220 |
|
| 221 |
enum EvalType { DIRECT_EVAL, INDIRECT_EVAL }; |
| 222 |
|
| 223 |
// 18.2.1.1 PerformEval |
| 224 |
// |
| 225 |
// Common code implementing direct and indirect eval. |
| 226 |
// |
| 227 |
// Evaluate v, if it is a string, in the context of the given calling |
| 228 |
// frame, with the provided scope chain, with the semantics of either a direct |
| 229 |
// or indirect eval (see ES5 10.4.2). If this is an indirect eval, env |
| 230 |
// must be the global lexical environment. |
| 231 |
// |
| 232 |
// On success, store the completion value in call.rval and return true. |
| 233 |
static bool EvalKernel(JSContext* cx, HandleValue v, EvalType evalType, |
| 234 |
AbstractFramePtr caller, HandleObject env, |
| 235 |
jsbytecode* pc, MutableHandleValue vp) { |
| 236 |
MOZ_ASSERT((evalType == INDIRECT_EVAL) == !caller); |
| 237 |
MOZ_ASSERT((evalType == INDIRECT_EVAL) == !pc); |
| 238 |
MOZ_ASSERT_IF(evalType == INDIRECT_EVAL, |
| 239 |
env->is<GlobalLexicalEnvironmentObject>()); |
| 240 |
AssertInnerizedEnvironmentChain(cx, *env); |
| 241 |
|
| 242 |
// "Dynamic Code Brand Checks" adds support for Object values. |
| 243 |
// https://tc39.es/proposal-dynamic-code-brand-checks/#sec-performeval |
| 244 |
// Steps 2-4. |
| 245 |
RootedString str(cx); |
| 246 |
if (v.isString()) { |
| 247 |
str = v.toString(); |
| 248 |
} else if (v.isObject()) { |
| 249 |
RootedObject obj(cx, &v.toObject()); |
| 250 |
if (!cx->getCodeForEval(obj, &str)) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
} |
| 254 |
if (!str) { |
| 255 |
vp.set(v); |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
// Steps 6-8. |
| 260 |
JS::RootedVector<JSString*> parameterStrings(cx); |
| 261 |
JS::RootedVector<Value> parameterArgs(cx); |
| 262 |
bool canCompileStrings = false; |
| 263 |
if (!cx->isRuntimeCodeGenEnabled( |
| 264 |
JS::RuntimeCode::JS, str, |
| 265 |
evalType == DIRECT_EVAL ? JS::CompilationType::DirectEval |
| 266 |
: JS::CompilationType::IndirectEval, |
| 267 |
parameterStrings, str, parameterArgs, v, &canCompileStrings)) { |
| 268 |
return false; |
| 269 |
} |
| 270 |
if (!canCompileStrings) { |
| 271 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 272 |
JSMSG_CSP_BLOCKED_EVAL); |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
// Step 9 ff. |
| 277 |
|
| 278 |
// Per ES5, indirect eval runs in the global scope. (eval is specified this |
| 279 |
// way so that the compiler can make assumptions about what bindings may or |
| 280 |
// may not exist in the current frame if it doesn't see 'eval'.) |
| 281 |
MOZ_ASSERT_IF( |
| 282 |
evalType != DIRECT_EVAL, |
| 283 |
cx->global() == &env->as<GlobalLexicalEnvironmentObject>().global()); |
| 284 |
|
| 285 |
Rooted<JSLinearString*> linearStr(cx, str->ensureLinear(cx)); |
| 286 |
if (!linearStr) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
RootedScript callerScript(cx, caller ? caller.script() : nullptr); |
| 291 |
EvalJSONResult ejr = TryEvalJSON(cx, linearStr, vp); |
| 292 |
if (ejr != EvalJSONResult::NotJSON) { |
| 293 |
return ejr == EvalJSONResult::Success; |
| 294 |
} |
| 295 |
|
| 296 |
EvalScriptGuard esg(cx); |
| 297 |
|
| 298 |
if (evalType == DIRECT_EVAL && caller.isFunctionFrame()) { |
| 299 |
esg.lookupInEvalCache(linearStr, callerScript, pc); |
| 300 |
} |
| 301 |
|
| 302 |
if (!esg.foundScript()) { |
| 303 |
RootedScript maybeScript(cx); |
| 304 |
uint32_t lineno; |
| 305 |
const char* filename; |
| 306 |
bool mutedErrors; |
| 307 |
uint32_t pcOffset; |
| 308 |
if (evalType == DIRECT_EVAL) { |
| 309 |
DescribeScriptedCallerForDirectEval(cx, callerScript, pc, &filename, |
| 310 |
&lineno, &pcOffset, &mutedErrors); |
| 311 |
maybeScript = callerScript; |
| 312 |
} else { |
| 313 |
DescribeScriptedCallerForCompilation(cx, &maybeScript, &filename, &lineno, |
| 314 |
&pcOffset, &mutedErrors); |
| 315 |
} |
| 316 |
|
| 317 |
const char* introducerFilename = filename; |
| 318 |
if (maybeScript && maybeScript->scriptSource()->introducerFilename()) { |
| 319 |
introducerFilename = maybeScript->scriptSource()->introducerFilename(); |
| 320 |
} |
| 321 |
|
| 322 |
Rooted<Scope*> enclosing(cx); |
| 323 |
if (evalType == DIRECT_EVAL) { |
| 324 |
enclosing = callerScript->innermostScope(pc); |
| 325 |
} else { |
| 326 |
enclosing = &cx->global()->emptyGlobalScope(); |
| 327 |
} |
| 328 |
|
| 329 |
CompileOptions options(cx); |
| 330 |
options.setIsRunOnce(true) |
| 331 |
.setNoScriptRval(false) |
| 332 |
.setMutedErrors(mutedErrors) |
| 333 |
.setDeferDebugMetadata(); |
| 334 |
|
| 335 |
RootedScript introScript(cx); |
| 336 |
|
| 337 |
if (evalType == DIRECT_EVAL && IsStrictEvalPC(pc)) { |
| 338 |
options.setForceStrictMode(); |
| 339 |
} |
| 340 |
|
| 341 |
if (introducerFilename) { |
| 342 |
options.setFileAndLine(filename, 1); |
| 343 |
options.setIntroductionInfo(introducerFilename, "eval", lineno, pcOffset); |
| 344 |
introScript = maybeScript; |
| 345 |
} else { |
| 346 |
options.setFileAndLine("eval", 1); |
| 347 |
options.setIntroductionType("eval"); |
| 348 |
} |
| 349 |
options.setNonSyntacticScope( |
| 350 |
enclosing->hasOnChain(ScopeKind::NonSyntactic)); |
| 351 |
|
| 352 |
AutoStableStringChars linearChars(cx); |
| 353 |
if (!linearChars.initTwoByte(cx, linearStr)) { |
| 354 |
return false; |
| 355 |
} |
| 356 |
|
| 357 |
SourceText<char16_t> srcBuf; |
| 358 |
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) { |
| 359 |
return false; |
| 360 |
} |
| 361 |
|
| 362 |
RootedScript script( |
| 363 |
cx, frontend::CompileEvalScript(cx, options, srcBuf, enclosing, env)); |
| 364 |
if (!script) { |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
RootedValue undefValue(cx); |
| 369 |
JS::InstantiateOptions instantiateOptions(options); |
| 370 |
if (!JS::UpdateDebugMetadata(cx, script, instantiateOptions, undefValue, |
| 371 |
nullptr, introScript, maybeScript)) { |
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
esg.setNewScript(script); |
| 376 |
} |
| 377 |
|
| 378 |
return ExecuteKernel(cx, esg.script(), env, NullFramePtr() /* evalInFrame */, |
| 379 |
vp); |
| 380 |
} |
| 381 |
|
| 382 |
bool js::IndirectEval(JSContext* cx, unsigned argc, Value* vp) { |
| 383 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 384 |
|
| 385 |
RootedObject globalLexical(cx, &cx->global()->lexicalEnvironment()); |
| 386 |
|
| 387 |
// Note we'll just pass |undefined| here, then return it directly (or throw |
| 388 |
// if runtime codegen is disabled), if no argument is provided. |
| 389 |
return EvalKernel(cx, args.get(0), INDIRECT_EVAL, NullFramePtr(), |
| 390 |
globalLexical, nullptr, args.rval()); |
| 391 |
} |
| 392 |
|
| 393 |
bool js::DirectEval(JSContext* cx, HandleValue v, MutableHandleValue vp) { |
| 394 |
// Direct eval can assume it was called from an interpreted or baseline frame. |
| 395 |
ScriptFrameIter iter(cx); |
| 396 |
AbstractFramePtr caller = iter.abstractFramePtr(); |
| 397 |
|
| 398 |
MOZ_ASSERT(JSOp(*iter.pc()) == JSOp::Eval || |
| 399 |
JSOp(*iter.pc()) == JSOp::StrictEval || |
| 400 |
JSOp(*iter.pc()) == JSOp::SpreadEval || |
| 401 |
JSOp(*iter.pc()) == JSOp::StrictSpreadEval); |
| 402 |
MOZ_ASSERT(caller.realm() == caller.script()->realm()); |
| 403 |
|
| 404 |
RootedObject envChain(cx, caller.environmentChain()); |
| 405 |
return EvalKernel(cx, v, DIRECT_EVAL, caller, envChain, iter.pc(), vp); |
| 406 |
} |
| 407 |
|
| 408 |
bool js::IsAnyBuiltinEval(JSFunction* fun) { |
| 409 |
return fun->maybeNative() == IndirectEval; |
| 410 |
} |
| 411 |
|
| 412 |
static bool ExecuteInExtensibleLexicalEnvironment( |
| 413 |
JSContext* cx, HandleScript scriptArg, |
| 414 |
Handle<ExtensibleLexicalEnvironmentObject*> env) { |
| 415 |
CHECK_THREAD(cx); |
| 416 |
cx->check(env); |
| 417 |
cx->check(scriptArg); |
| 418 |
MOZ_RELEASE_ASSERT(scriptArg->hasNonSyntacticScope()); |
| 419 |
|
| 420 |
RootedValue rval(cx); |
| 421 |
return ExecuteKernel(cx, scriptArg, env, NullFramePtr() /* evalInFrame */, |
| 422 |
&rval); |
| 423 |
} |
| 424 |
|
| 425 |
JS_PUBLIC_API bool js::ExecuteInFrameScriptEnvironment( |
| 426 |
JSContext* cx, HandleObject objArg, HandleScript scriptArg, |
| 427 |
MutableHandleObject envArg) { |
| 428 |
Rooted<NonSyntacticVariablesObject*> varEnv( |
| 429 |
cx, NonSyntacticVariablesObject::create(cx)); |
| 430 |
if (!varEnv) { |
| 431 |
return false; |
| 432 |
} |
| 433 |
|
| 434 |
JS::EnvironmentChain envChain(cx, JS::SupportUnscopables::No); |
| 435 |
if (!envChain.append(objArg)) { |
| 436 |
return false; |
| 437 |
} |
| 438 |
|
| 439 |
Rooted<WithEnvironmentObject*> env( |
| 440 |
cx, js::CreateObjectsForEnvironmentChain(cx, envChain, varEnv)); |
| 441 |
if (!env) { |
| 442 |
return false; |
| 443 |
} |
| 444 |
|
| 445 |
// Create lexical environment with |this| == objArg, which should be a Gecko |
| 446 |
// MessageManager. |
| 447 |
// NOTE: This is required behavior for Gecko FrameScriptLoader, where some |
| 448 |
// callers try to bind methods from the message manager in their scope chain |
| 449 |
// to |this|, and will fail if it is not bound to a message manager. |
| 450 |
ObjectRealm& realm = ObjectRealm::get(varEnv); |
| 451 |
Rooted<NonSyntacticLexicalEnvironmentObject*> lexicalEnv( |
| 452 |
cx, realm.getOrCreateNonSyntacticLexicalEnvironment(cx, env, varEnv)); |
| 453 |
if (!lexicalEnv) { |
| 454 |
return false; |
| 455 |
} |
| 456 |
|
| 457 |
if (!ExecuteInExtensibleLexicalEnvironment(cx, scriptArg, lexicalEnv)) { |
| 458 |
return false; |
| 459 |
} |
| 460 |
|
| 461 |
envArg.set(lexicalEnv); |
| 462 |
return true; |
| 463 |
} |
| 464 |
|
| 465 |
JS_PUBLIC_API JSObject* JS::NewJSMEnvironment(JSContext* cx) { |
| 466 |
Rooted<NonSyntacticVariablesObject*> varEnv( |
| 467 |
cx, NonSyntacticVariablesObject::create(cx)); |
| 468 |
if (!varEnv) { |
| 469 |
return nullptr; |
| 470 |
} |
| 471 |
|
| 472 |
// Force the NonSyntacticLexicalEnvironmentObject to be created. |
| 473 |
ObjectRealm& realm = ObjectRealm::get(varEnv); |
| 474 |
MOZ_ASSERT(!realm.getNonSyntacticLexicalEnvironment(varEnv)); |
| 475 |
if (!realm.getOrCreateNonSyntacticLexicalEnvironment(cx, varEnv)) { |
| 476 |
return nullptr; |
| 477 |
} |
| 478 |
|
| 479 |
return varEnv; |
| 480 |
} |
| 481 |
|
| 482 |
JS_PUBLIC_API bool JS::ExecuteInJSMEnvironment(JSContext* cx, |
| 483 |
HandleScript scriptArg, |
| 484 |
HandleObject varEnv) { |
| 485 |
JS::EnvironmentChain emptyChain(cx, JS::SupportUnscopables::No); |
| 486 |
return ExecuteInJSMEnvironment(cx, scriptArg, varEnv, emptyChain); |
| 487 |
} |
| 488 |
|
| 489 |
JS_PUBLIC_API bool JS::ExecuteInJSMEnvironment( |
| 490 |
JSContext* cx, HandleScript scriptArg, HandleObject varEnv, |
| 491 |
const EnvironmentChain& targetObj) { |
| 492 |
cx->check(varEnv); |
| 493 |
MOZ_ASSERT( |
| 494 |
ObjectRealm::get(varEnv).getNonSyntacticLexicalEnvironment(varEnv)); |
| 495 |
MOZ_DIAGNOSTIC_ASSERT(scriptArg->noScriptRval()); |
| 496 |
|
| 497 |
Rooted<ExtensibleLexicalEnvironmentObject*> env( |
| 498 |
cx, ExtensibleLexicalEnvironmentObject::forVarEnvironment(varEnv)); |
| 499 |
|
| 500 |
// If the Gecko subscript loader specifies target objects, we need to add |
| 501 |
// them to the environment. These are added after the NSVO environment. |
| 502 |
if (!targetObj.empty()) { |
| 503 |
// The environment chain will be as follows: |
| 504 |
// GlobalObject / SystemGlobal |
| 505 |
// GlobalLexicalEnvironmentObject[this=global] |
| 506 |
// NonSyntacticVariablesObject (the JSMEnvironment) |
| 507 |
// NonSyntacticLexicalEnvironmentObject[this=nsvo] |
| 508 |
// WithEnvironmentObject[target=targetObj] |
| 509 |
// NonSyntacticLexicalEnvironmentObject[this=targetObj] (*) |
| 510 |
// |
| 511 |
// (*) This environment intercepts JSOp::GlobalThis. |
| 512 |
|
| 513 |
// Wrap the target objects in WithEnvironments. |
| 514 |
Rooted<WithEnvironmentObject*> envChain( |
| 515 |
cx, js::CreateObjectsForEnvironmentChain(cx, targetObj, env)); |
| 516 |
if (!envChain) { |
| 517 |
return false; |
| 518 |
} |
| 519 |
|
| 520 |
// See CreateNonSyntacticEnvironmentChain |
| 521 |
if (!JSObject::setQualifiedVarObj(cx, envChain)) { |
| 522 |
return false; |
| 523 |
} |
| 524 |
|
| 525 |
// Create an extensible lexical environment for the target object. |
| 526 |
env = ObjectRealm::get(envChain).getOrCreateNonSyntacticLexicalEnvironment( |
| 527 |
cx, envChain); |
| 528 |
if (!env) { |
| 529 |
return false; |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
return ExecuteInExtensibleLexicalEnvironment(cx, scriptArg, env); |
| 534 |
} |
| 535 |
|
| 536 |
JS_PUBLIC_API JSObject* JS::GetJSMEnvironmentOfScriptedCaller(JSContext* cx) { |
| 537 |
FrameIter iter(cx); |
| 538 |
if (iter.done()) { |
| 539 |
return nullptr; |
| 540 |
} |
| 541 |
|
| 542 |
// WASM frames don't always provide their environment, but we also shouldn't |
| 543 |
// expect to see any calling into here. |
| 544 |
MOZ_RELEASE_ASSERT(!iter.isWasm()); |
| 545 |
|
| 546 |
RootedObject env(cx, iter.environmentChain(cx)); |
| 547 |
while (env && !env->is<NonSyntacticVariablesObject>()) { |
| 548 |
env = env->enclosingEnvironment(); |
| 549 |
} |
| 550 |
|
| 551 |
return env; |
| 552 |
} |
| 553 |
|
| 554 |
JS_PUBLIC_API bool JS::IsJSMEnvironment(JSObject* obj) { |
| 555 |
// NOTE: This also returns true if the NonSyntacticVariablesObject was |
| 556 |
// created for reasons other than the JSM loader. |
| 557 |
return obj->is<NonSyntacticVariablesObject>(); |
| 558 |
} |
| 559 |
|
| 560 |
#ifdef JSGC_HASH_TABLE_CHECKS |
| 561 |
void RuntimeCaches::checkEvalCacheAfterMinorGC() { |
| 562 |
gc::CheckTableAfterMovingGC(evalCache, [](const auto& entry) { |
| 563 |
CheckGCThingAfterMovingGC(entry.str); |
| 564 |
CheckGCThingAfterMovingGC(entry.script); |
| 565 |
CheckGCThingAfterMovingGC(entry.callerScript); |
| 566 |
return EvalCacheLookup(entry.str, entry.callerScript, entry.pc); |
| 567 |
}); |
| 568 |
} |
| 569 |
#endif |