| 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 "debugger/Environment-inl.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" // for AssertionConditionType |
| 10 |
#include "mozilla/Maybe.h" // for Maybe, Some, Nothing |
| 11 |
#include "mozilla/Vector.h" // for Vector |
| 12 |
|
| 13 |
#include <string.h> // for strlen, size_t |
| 14 |
#include <utility> // for move |
| 15 |
|
| 16 |
#include "debugger/Debugger.h" // for Env, Debugger, ValueToIdentifier |
| 17 |
#include "debugger/Object.h" // for DebuggerObject |
| 18 |
#include "debugger/Script.h" // for DebuggerScript |
| 19 |
#include "gc/Tracer.h" // for TraceManuallyBarrieredCrossCompartmentEdge |
| 20 |
#include "js/CallArgs.h" // for CallArgs |
| 21 |
#include "js/friend/ErrorMessages.h" // for GetErrorMessage, JSMSG_* |
| 22 |
#include "js/HeapAPI.h" // for IsInsideNursery |
| 23 |
#include "js/RootingAPI.h" // for Rooted, MutableHandle |
| 24 |
#include "util/Identifier.h" // for IsIdentifier |
| 25 |
#include "vm/Compartment.h" // for Compartment |
| 26 |
#include "vm/JSAtomUtils.h" // for Atomize |
| 27 |
#include "vm/JSContext.h" // for JSContext |
| 28 |
#include "vm/JSFunction.h" // for JSFunction |
| 29 |
#include "vm/JSObject.h" // for JSObject, RequireObject, |
| 30 |
#include "vm/NativeObject.h" // for NativeObject, JSObject::is |
| 31 |
#include "vm/Realm.h" // for AutoRealm, ErrorCopier |
| 32 |
#include "vm/Scope.h" // for ScopeKind, ScopeKindString |
| 33 |
#include "vm/StringType.h" // for JSAtom |
| 34 |
|
| 35 |
#include "gc/StableCellHasher-inl.h" |
| 36 |
#include "vm/Compartment-inl.h" // for Compartment::wrap |
| 37 |
#include "vm/EnvironmentObject-inl.h" // for JSObject::enclosingEnvironment |
| 38 |
#include "vm/JSObject-inl.h" // for IsInternalFunctionObject, NewObjectWithGivenProtoAndKind |
| 39 |
#include "vm/ObjectOperations-inl.h" // for HasProperty, GetProperty |
| 40 |
#include "vm/Realm-inl.h" // for AutoRealm::AutoRealm |
| 41 |
|
| 42 |
namespace js { |
| 43 |
class GlobalObject; |
| 44 |
} |
| 45 |
|
| 46 |
using namespace js; |
| 47 |
|
| 48 |
using mozilla::Maybe; |
| 49 |
using mozilla::Nothing; |
| 50 |
using mozilla::Some; |
| 51 |
|
| 52 |
const JSClassOps DebuggerEnvironment::classOps_ = { |
| 53 |
nullptr, // addProperty |
| 54 |
nullptr, // delProperty |
| 55 |
nullptr, // enumerate |
| 56 |
nullptr, // newEnumerate |
| 57 |
nullptr, // resolve |
| 58 |
nullptr, // mayResolve |
| 59 |
nullptr, // finalize |
| 60 |
nullptr, // call |
| 61 |
nullptr, // construct |
| 62 |
CallTraceMethod<DebuggerEnvironment>, // trace |
| 63 |
}; |
| 64 |
|
| 65 |
const JSClass DebuggerEnvironment::class_ = { |
| 66 |
"Environment", |
| 67 |
JSCLASS_HAS_RESERVED_SLOTS(DebuggerEnvironment::RESERVED_SLOTS), |
| 68 |
&classOps_, |
| 69 |
}; |
| 70 |
|
| 71 |
void DebuggerEnvironment::trace(JSTracer* trc) { |
| 72 |
// There is a barrier on private pointers, so the Unbarriered marking |
| 73 |
// is okay. |
| 74 |
if (Env* referent = maybeReferent()) { |
| 75 |
TraceManuallyBarrieredCrossCompartmentEdge(trc, this, &referent, |
| 76 |
"Debugger.Environment referent"); |
| 77 |
if (referent != maybeReferent()) { |
| 78 |
setReservedSlotGCThingAsPrivateUnbarriered(ENV_SLOT, referent); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static DebuggerEnvironment* DebuggerEnvironment_checkThis( |
| 84 |
JSContext* cx, const CallArgs& args) { |
| 85 |
JSObject* thisobj = RequireObject(cx, args.thisv()); |
| 86 |
if (!thisobj) { |
| 87 |
return nullptr; |
| 88 |
} |
| 89 |
if (!thisobj->is<DebuggerEnvironment>()) { |
| 90 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 91 |
JSMSG_INCOMPATIBLE_PROTO, "Debugger.Environment", |
| 92 |
"method", thisobj->getClass()->name); |
| 93 |
return nullptr; |
| 94 |
} |
| 95 |
|
| 96 |
return &thisobj->as<DebuggerEnvironment>(); |
| 97 |
} |
| 98 |
|
| 99 |
struct MOZ_STACK_CLASS DebuggerEnvironment::CallData { |
| 100 |
JSContext* cx; |
| 101 |
const CallArgs& args; |
| 102 |
|
| 103 |
Handle<DebuggerEnvironment*> environment; |
| 104 |
|
| 105 |
CallData(JSContext* cx, const CallArgs& args, |
| 106 |
Handle<DebuggerEnvironment*> env) |
| 107 |
: cx(cx), args(args), environment(env) {} |
| 108 |
|
| 109 |
bool typeGetter(); |
| 110 |
bool scopeKindGetter(); |
| 111 |
bool parentGetter(); |
| 112 |
bool objectGetter(); |
| 113 |
bool calleeScriptGetter(); |
| 114 |
bool inspectableGetter(); |
| 115 |
bool optimizedOutGetter(); |
| 116 |
|
| 117 |
bool namesMethod(); |
| 118 |
bool findMethod(); |
| 119 |
bool getVariableMethod(); |
| 120 |
bool setVariableMethod(); |
| 121 |
|
| 122 |
using Method = bool (CallData::*)(); |
| 123 |
|
| 124 |
template <Method MyMethod> |
| 125 |
static bool ToNative(JSContext* cx, unsigned argc, Value* vp); |
| 126 |
}; |
| 127 |
|
| 128 |
template <DebuggerEnvironment::CallData::Method MyMethod> |
| 129 |
/* static */ |
| 130 |
bool DebuggerEnvironment::CallData::ToNative(JSContext* cx, unsigned argc, |
| 131 |
Value* vp) { |
| 132 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 133 |
|
| 134 |
Rooted<DebuggerEnvironment*> environment( |
| 135 |
cx, DebuggerEnvironment_checkThis(cx, args)); |
| 136 |
if (!environment) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
CallData data(cx, args, environment); |
| 141 |
return (data.*MyMethod)(); |
| 142 |
} |
| 143 |
|
| 144 |
/* static */ |
| 145 |
bool DebuggerEnvironment::construct(JSContext* cx, unsigned argc, Value* vp) { |
| 146 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_NO_CONSTRUCTOR, |
| 147 |
"Debugger.Environment"); |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
static bool IsDeclarative(Env* env) { |
| 152 |
return env->is<DebugEnvironmentProxy>() && |
| 153 |
env->as<DebugEnvironmentProxy>().isForDeclarative(); |
| 154 |
} |
| 155 |
|
| 156 |
template <typename T> |
| 157 |
static bool IsDebugEnvironmentWrapper(Env* env) { |
| 158 |
return env->is<DebugEnvironmentProxy>() && |
| 159 |
env->as<DebugEnvironmentProxy>().environment().is<T>(); |
| 160 |
} |
| 161 |
|
| 162 |
bool DebuggerEnvironment::CallData::typeGetter() { |
| 163 |
if (!environment->requireDebuggee(cx)) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
DebuggerEnvironmentType type = environment->type(); |
| 168 |
|
| 169 |
const char* s; |
| 170 |
switch (type) { |
| 171 |
case DebuggerEnvironmentType::Declarative: |
| 172 |
s = "declarative"; |
| 173 |
break; |
| 174 |
case DebuggerEnvironmentType::With: |
| 175 |
s = "with"; |
| 176 |
break; |
| 177 |
case DebuggerEnvironmentType::Object: |
| 178 |
s = "object"; |
| 179 |
break; |
| 180 |
} |
| 181 |
|
| 182 |
JSAtom* str = Atomize(cx, s, strlen(s)); |
| 183 |
if (!str) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
args.rval().setString(str); |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
bool DebuggerEnvironment::CallData::scopeKindGetter() { |
| 192 |
if (!environment->requireDebuggee(cx)) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
Maybe<ScopeKind> kind = environment->scopeKind(); |
| 197 |
if (kind.isSome()) { |
| 198 |
const char* s = ScopeKindString(*kind); |
| 199 |
JSAtom* str = Atomize(cx, s, strlen(s)); |
| 200 |
if (!str) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
args.rval().setString(str); |
| 204 |
} else { |
| 205 |
args.rval().setNull(); |
| 206 |
} |
| 207 |
|
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
bool DebuggerEnvironment::CallData::parentGetter() { |
| 212 |
if (!environment->requireDebuggee(cx)) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
Rooted<DebuggerEnvironment*> result(cx); |
| 217 |
if (!environment->getParent(cx, &result)) { |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
args.rval().setObjectOrNull(result); |
| 222 |
return true; |
| 223 |
} |
| 224 |
|
| 225 |
bool DebuggerEnvironment::CallData::objectGetter() { |
| 226 |
if (!environment->requireDebuggee(cx)) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
if (environment->type() == DebuggerEnvironmentType::Declarative) { |
| 231 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 232 |
JSMSG_DEBUG_NO_ENV_OBJECT); |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
Rooted<DebuggerObject*> result(cx); |
| 237 |
if (!environment->getObject(cx, &result)) { |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
args.rval().setObject(*result); |
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
bool DebuggerEnvironment::CallData::calleeScriptGetter() { |
| 246 |
if (!environment->requireDebuggee(cx)) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
Rooted<DebuggerScript*> result(cx); |
| 251 |
if (!environment->getCalleeScript(cx, &result)) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
|
| 255 |
args.rval().setObjectOrNull(result); |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
bool DebuggerEnvironment::CallData::inspectableGetter() { |
| 260 |
args.rval().setBoolean(environment->isDebuggee()); |
| 261 |
return true; |
| 262 |
} |
| 263 |
|
| 264 |
bool DebuggerEnvironment::CallData::optimizedOutGetter() { |
| 265 |
args.rval().setBoolean(environment->isOptimized()); |
| 266 |
return true; |
| 267 |
} |
| 268 |
|
| 269 |
bool DebuggerEnvironment::CallData::namesMethod() { |
| 270 |
if (!environment->requireDebuggee(cx)) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
RootedIdVector ids(cx); |
| 275 |
if (!DebuggerEnvironment::getNames(cx, environment, &ids)) { |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
JSObject* obj = IdVectorToArray(cx, ids); |
| 280 |
if (!obj) { |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
args.rval().setObject(*obj); |
| 285 |
return true; |
| 286 |
} |
| 287 |
|
| 288 |
bool DebuggerEnvironment::CallData::findMethod() { |
| 289 |
if (!args.requireAtLeast(cx, "Debugger.Environment.find", 1)) { |
| 290 |
return false; |
| 291 |
} |
| 292 |
|
| 293 |
RootedId id(cx); |
| 294 |
if (!ValueToIdentifier(cx, args[0], &id)) { |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
if (!environment->requireDebuggee(cx)) { |
| 299 |
return false; |
| 300 |
} |
| 301 |
|
| 302 |
Rooted<DebuggerEnvironment*> result(cx); |
| 303 |
if (!DebuggerEnvironment::find(cx, environment, id, &result)) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
args.rval().setObjectOrNull(result); |
| 308 |
return true; |
| 309 |
} |
| 310 |
|
| 311 |
bool DebuggerEnvironment::CallData::getVariableMethod() { |
| 312 |
if (!args.requireAtLeast(cx, "Debugger.Environment.getVariable", 1)) { |
| 313 |
return false; |
| 314 |
} |
| 315 |
|
| 316 |
RootedId id(cx); |
| 317 |
if (!ValueToIdentifier(cx, args[0], &id)) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
if (!environment->requireDebuggee(cx)) { |
| 322 |
return false; |
| 323 |
} |
| 324 |
|
| 325 |
return DebuggerEnvironment::getVariable(cx, environment, id, args.rval()); |
| 326 |
} |
| 327 |
|
| 328 |
bool DebuggerEnvironment::CallData::setVariableMethod() { |
| 329 |
if (!args.requireAtLeast(cx, "Debugger.Environment.setVariable", 2)) { |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
RootedId id(cx); |
| 334 |
if (!ValueToIdentifier(cx, args[0], &id)) { |
| 335 |
return false; |
| 336 |
} |
| 337 |
|
| 338 |
if (!environment->requireDebuggee(cx)) { |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
if (!DebuggerEnvironment::setVariable(cx, environment, id, args[1])) { |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
args.rval().setUndefined(); |
| 347 |
return true; |
| 348 |
} |
| 349 |
|
| 350 |
bool DebuggerEnvironment::requireDebuggee(JSContext* cx) const { |
| 351 |
if (!isDebuggee()) { |
| 352 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 353 |
JSMSG_DEBUG_NOT_DEBUGGEE, "Debugger.Environment", |
| 354 |
"environment"); |
| 355 |
|
| 356 |
return false; |
| 357 |
} |
| 358 |
|
| 359 |
return true; |
| 360 |
} |
| 361 |
|
| 362 |
const JSPropertySpec DebuggerEnvironment::properties_[] = { |
| 363 |
JS_DEBUG_PSG("type", typeGetter), |
| 364 |
JS_DEBUG_PSG("scopeKind", scopeKindGetter), |
| 365 |
JS_DEBUG_PSG("parent", parentGetter), |
| 366 |
JS_DEBUG_PSG("object", objectGetter), |
| 367 |
JS_DEBUG_PSG("calleeScript", calleeScriptGetter), |
| 368 |
JS_DEBUG_PSG("inspectable", inspectableGetter), |
| 369 |
JS_DEBUG_PSG("optimizedOut", optimizedOutGetter), |
| 370 |
JS_PS_END, |
| 371 |
}; |
| 372 |
|
| 373 |
const JSFunctionSpec DebuggerEnvironment::methods_[] = { |
| 374 |
JS_DEBUG_FN("names", namesMethod, 0), |
| 375 |
JS_DEBUG_FN("find", findMethod, 1), |
| 376 |
JS_DEBUG_FN("getVariable", getVariableMethod, 1), |
| 377 |
JS_DEBUG_FN("setVariable", setVariableMethod, 2), |
| 378 |
JS_FS_END, |
| 379 |
}; |
| 380 |
|
| 381 |
/* static */ |
| 382 |
NativeObject* DebuggerEnvironment::initClass(JSContext* cx, |
| 383 |
Handle<GlobalObject*> global, |
| 384 |
HandleObject dbgCtor) { |
| 385 |
return InitClass(cx, dbgCtor, nullptr, nullptr, "Environment", construct, 0, |
| 386 |
properties_, methods_, nullptr, nullptr); |
| 387 |
} |
| 388 |
|
| 389 |
/* static */ |
| 390 |
DebuggerEnvironment* DebuggerEnvironment::create( |
| 391 |
JSContext* cx, HandleObject proto, HandleObject referent, |
| 392 |
Handle<NativeObject*> debugger) { |
| 393 |
DebuggerEnvironment* obj = |
| 394 |
IsInsideNursery(referent) |
| 395 |
? NewObjectWithGivenProto<DebuggerEnvironment>(cx, proto) |
| 396 |
: NewTenuredObjectWithGivenProto<DebuggerEnvironment>(cx, proto); |
| 397 |
if (!obj) { |
| 398 |
return nullptr; |
| 399 |
} |
| 400 |
|
| 401 |
obj->setReservedSlotGCThingAsPrivate(ENV_SLOT, referent); |
| 402 |
obj->setReservedSlot(OWNER_SLOT, ObjectValue(*debugger)); |
| 403 |
|
| 404 |
return obj; |
| 405 |
} |
| 406 |
|
| 407 |
/* static */ |
| 408 |
DebuggerEnvironmentType DebuggerEnvironment::type() const { |
| 409 |
// Don't bother switching compartments just to check env's type. |
| 410 |
if (IsDeclarative(referent())) { |
| 411 |
return DebuggerEnvironmentType::Declarative; |
| 412 |
} |
| 413 |
if (IsDebugEnvironmentWrapper<WithEnvironmentObject>(referent())) { |
| 414 |
return DebuggerEnvironmentType::With; |
| 415 |
} |
| 416 |
return DebuggerEnvironmentType::Object; |
| 417 |
} |
| 418 |
|
| 419 |
mozilla::Maybe<ScopeKind> DebuggerEnvironment::scopeKind() const { |
| 420 |
if (!referent()->is<DebugEnvironmentProxy>()) { |
| 421 |
return Nothing(); |
| 422 |
} |
| 423 |
EnvironmentObject& env = |
| 424 |
referent()->as<DebugEnvironmentProxy>().environment(); |
| 425 |
Scope* scope = GetEnvironmentScope(env); |
| 426 |
return scope ? Some(scope->kind()) : Nothing(); |
| 427 |
} |
| 428 |
|
| 429 |
bool DebuggerEnvironment::getParent( |
| 430 |
JSContext* cx, MutableHandle<DebuggerEnvironment*> result) const { |
| 431 |
// Don't bother switching compartments just to get env's parent. |
| 432 |
Rooted<Env*> parent(cx, referent()->enclosingEnvironment()); |
| 433 |
if (!parent) { |
| 434 |
result.set(nullptr); |
| 435 |
return true; |
| 436 |
} |
| 437 |
|
| 438 |
return owner()->wrapEnvironment(cx, parent, result); |
| 439 |
} |
| 440 |
|
| 441 |
bool DebuggerEnvironment::getObject( |
| 442 |
JSContext* cx, MutableHandle<DebuggerObject*> result) const { |
| 443 |
MOZ_ASSERT(type() != DebuggerEnvironmentType::Declarative); |
| 444 |
|
| 445 |
// Don't bother switching compartments just to get env's object. |
| 446 |
RootedObject object(cx); |
| 447 |
if (IsDebugEnvironmentWrapper<WithEnvironmentObject>(referent())) { |
| 448 |
object.set(&referent() |
| 449 |
->as<DebugEnvironmentProxy>() |
| 450 |
.environment() |
| 451 |
.as<WithEnvironmentObject>() |
| 452 |
.object()); |
| 453 |
} else if (IsDebugEnvironmentWrapper<NonSyntacticVariablesObject>( |
| 454 |
referent())) { |
| 455 |
object.set(&referent() |
| 456 |
->as<DebugEnvironmentProxy>() |
| 457 |
.environment() |
| 458 |
.as<NonSyntacticVariablesObject>()); |
| 459 |
} else { |
| 460 |
object.set(referent()); |
| 461 |
MOZ_ASSERT(!object->is<DebugEnvironmentProxy>()); |
| 462 |
} |
| 463 |
|
| 464 |
return owner()->wrapDebuggeeObject(cx, object, result); |
| 465 |
} |
| 466 |
|
| 467 |
bool DebuggerEnvironment::getCalleeScript( |
| 468 |
JSContext* cx, MutableHandle<DebuggerScript*> result) const { |
| 469 |
if (!referent()->is<DebugEnvironmentProxy>()) { |
| 470 |
result.set(nullptr); |
| 471 |
return true; |
| 472 |
} |
| 473 |
|
| 474 |
JSObject& scope = referent()->as<DebugEnvironmentProxy>().environment(); |
| 475 |
if (!scope.is<CallObject>()) { |
| 476 |
result.set(nullptr); |
| 477 |
return true; |
| 478 |
} |
| 479 |
|
| 480 |
Rooted<BaseScript*> script(cx, scope.as<CallObject>().callee().baseScript()); |
| 481 |
|
| 482 |
DebuggerScript* scriptObject = owner()->wrapScript(cx, script); |
| 483 |
if (!scriptObject) { |
| 484 |
return false; |
| 485 |
} |
| 486 |
|
| 487 |
result.set(scriptObject); |
| 488 |
return true; |
| 489 |
} |
| 490 |
|
| 491 |
bool DebuggerEnvironment::isDebuggee() const { |
| 492 |
MOZ_ASSERT(referent()); |
| 493 |
MOZ_ASSERT(!referent()->is<EnvironmentObject>()); |
| 494 |
|
| 495 |
return owner()->observesGlobal(&referent()->nonCCWGlobal()); |
| 496 |
} |
| 497 |
|
| 498 |
bool DebuggerEnvironment::isOptimized() const { |
| 499 |
return referent()->is<DebugEnvironmentProxy>() && |
| 500 |
referent()->as<DebugEnvironmentProxy>().isOptimizedOut(); |
| 501 |
} |
| 502 |
|
| 503 |
/* static */ |
| 504 |
bool DebuggerEnvironment::getNames(JSContext* cx, |
| 505 |
Handle<DebuggerEnvironment*> environment, |
| 506 |
MutableHandleIdVector result) { |
| 507 |
MOZ_ASSERT(environment->isDebuggee()); |
| 508 |
MOZ_ASSERT(result.empty()); |
| 509 |
|
| 510 |
Rooted<Env*> referent(cx, environment->referent()); |
| 511 |
{ |
| 512 |
Maybe<AutoRealm> ar; |
| 513 |
ar.emplace(cx, referent); |
| 514 |
|
| 515 |
ErrorCopier ec(ar); |
| 516 |
if (!GetPropertyKeys(cx, referent, JSITER_HIDDEN, result)) { |
| 517 |
return false; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
result.eraseIf([](PropertyKey key) { |
| 522 |
return !key.isAtom() || !IsIdentifier(key.toAtom()); |
| 523 |
}); |
| 524 |
|
| 525 |
for (size_t i = 0; i < result.length(); ++i) { |
| 526 |
cx->markAtom(result[i].toAtom()); |
| 527 |
} |
| 528 |
|
| 529 |
return true; |
| 530 |
} |
| 531 |
|
| 532 |
/* static */ |
| 533 |
bool DebuggerEnvironment::find(JSContext* cx, |
| 534 |
Handle<DebuggerEnvironment*> environment, |
| 535 |
HandleId id, |
| 536 |
MutableHandle<DebuggerEnvironment*> result) { |
| 537 |
MOZ_ASSERT(environment->isDebuggee()); |
| 538 |
|
| 539 |
Rooted<Env*> env(cx, environment->referent()); |
| 540 |
Debugger* dbg = environment->owner(); |
| 541 |
|
| 542 |
{ |
| 543 |
Maybe<AutoRealm> ar; |
| 544 |
ar.emplace(cx, env); |
| 545 |
|
| 546 |
cx->markId(id); |
| 547 |
|
| 548 |
// This can trigger resolve hooks. |
| 549 |
ErrorCopier ec(ar); |
| 550 |
for (; env; env = env->enclosingEnvironment()) { |
| 551 |
bool found; |
| 552 |
if (!HasProperty(cx, env, id, &found)) { |
| 553 |
return false; |
| 554 |
} |
| 555 |
if (found) { |
| 556 |
break; |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
if (!env) { |
| 562 |
result.set(nullptr); |
| 563 |
return true; |
| 564 |
} |
| 565 |
|
| 566 |
return dbg->wrapEnvironment(cx, env, result); |
| 567 |
} |
| 568 |
|
| 569 |
/* static */ |
| 570 |
bool DebuggerEnvironment::getVariable(JSContext* cx, |
| 571 |
Handle<DebuggerEnvironment*> environment, |
| 572 |
HandleId id, MutableHandleValue result) { |
| 573 |
MOZ_ASSERT(environment->isDebuggee()); |
| 574 |
|
| 575 |
Rooted<Env*> referent(cx, environment->referent()); |
| 576 |
Debugger* dbg = environment->owner(); |
| 577 |
|
| 578 |
{ |
| 579 |
Maybe<AutoRealm> ar; |
| 580 |
ar.emplace(cx, referent); |
| 581 |
|
| 582 |
cx->markId(id); |
| 583 |
|
| 584 |
// This can trigger getters. |
| 585 |
ErrorCopier ec(ar); |
| 586 |
|
| 587 |
bool found; |
| 588 |
if (!HasProperty(cx, referent, id, &found)) { |
| 589 |
return false; |
| 590 |
} |
| 591 |
if (!found) { |
| 592 |
result.setUndefined(); |
| 593 |
return true; |
| 594 |
} |
| 595 |
|
| 596 |
// For DebugEnvironmentProxys, we get sentinel values for optimized out |
| 597 |
// slots and arguments instead of throwing (the default behavior). |
| 598 |
// |
| 599 |
// See wrapDebuggeeValue for how the sentinel values are wrapped. |
| 600 |
if (referent->is<DebugEnvironmentProxy>()) { |
| 601 |
Rooted<DebugEnvironmentProxy*> env( |
| 602 |
cx, &referent->as<DebugEnvironmentProxy>()); |
| 603 |
if (!DebugEnvironmentProxy::getMaybeSentinelValue(cx, env, id, result)) { |
| 604 |
return false; |
| 605 |
} |
| 606 |
} else { |
| 607 |
if (!GetProperty(cx, referent, referent, id, result)) { |
| 608 |
return false; |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
// When we've faked up scope chain objects for optimized-out scopes, |
| 614 |
// declarative environments may contain internal JSFunction objects, which |
| 615 |
// we shouldn't expose to the user. |
| 616 |
if (result.isObject()) { |
| 617 |
RootedObject obj(cx, &result.toObject()); |
| 618 |
if (obj->is<JSFunction>() && |
| 619 |
IsInternalFunctionObject(obj->as<JSFunction>())) |
| 620 |
result.setMagic(JS_OPTIMIZED_OUT); |
| 621 |
} |
| 622 |
|
| 623 |
return dbg->wrapDebuggeeValue(cx, result); |
| 624 |
} |
| 625 |
|
| 626 |
/* static */ |
| 627 |
bool DebuggerEnvironment::setVariable(JSContext* cx, |
| 628 |
Handle<DebuggerEnvironment*> environment, |
| 629 |
HandleId id, HandleValue value_) { |
| 630 |
MOZ_ASSERT(environment->isDebuggee()); |
| 631 |
|
| 632 |
Rooted<Env*> referent(cx, environment->referent()); |
| 633 |
Debugger* dbg = environment->owner(); |
| 634 |
|
| 635 |
RootedValue value(cx, value_); |
| 636 |
if (!dbg->unwrapDebuggeeValue(cx, &value)) { |
| 637 |
return false; |
| 638 |
} |
| 639 |
|
| 640 |
{ |
| 641 |
Maybe<AutoRealm> ar; |
| 642 |
ar.emplace(cx, referent); |
| 643 |
if (!cx->compartment()->wrap(cx, &value)) { |
| 644 |
return false; |
| 645 |
} |
| 646 |
cx->markId(id); |
| 647 |
|
| 648 |
// This can trigger setters. |
| 649 |
ErrorCopier ec(ar); |
| 650 |
|
| 651 |
// Make sure the environment actually has the specified binding. |
| 652 |
bool found; |
| 653 |
if (!HasProperty(cx, referent, id, &found)) { |
| 654 |
return false; |
| 655 |
} |
| 656 |
if (!found) { |
| 657 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 658 |
JSMSG_DEBUG_VARIABLE_NOT_FOUND); |
| 659 |
return false; |
| 660 |
} |
| 661 |
|
| 662 |
// Just set the property. |
| 663 |
if (!SetProperty(cx, referent, id, value)) { |
| 664 |
return false; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
return true; |
| 669 |
} |