| 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/Source.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" // for AssertionConditionType, MOZ_ASSERT |
| 10 |
#include "mozilla/Maybe.h" // for Some, Maybe, Nothing |
| 11 |
#include "mozilla/Variant.h" // for AsVariant, Variant |
| 12 |
|
| 13 |
#include <stdint.h> // for uint32_t |
| 14 |
#include <string.h> // for memcpy |
| 15 |
#include <utility> // for move |
| 16 |
|
| 17 |
#include "debugger/Debugger.h" // for DebuggerSourceReferent, Debugger |
| 18 |
#include "debugger/Script.h" // for DebuggerScript |
| 19 |
#include "frontend/FrontendContext.h" // for AutoReportFrontendContext |
| 20 |
#include "gc/Tracer.h" // for TraceManuallyBarrieredCrossCompartmentEdge |
| 21 |
#include "js/ColumnNumber.h" // JS::WasmFunctionIndex, JS::ColumnNumberOneOrigin |
| 22 |
#include "js/CompilationAndEvaluation.h" // for Compile |
| 23 |
#include "js/ErrorReport.h" // for JS_ReportErrorASCII, JS_ReportErrorNumberASCII |
| 24 |
#include "js/experimental/TypedData.h" // for JS_NewUint8Array |
| 25 |
#include "js/friend/ErrorMessages.h" // for GetErrorMessage, JSMSG_* |
| 26 |
#include "js/GCVariant.h" // for GCVariant |
| 27 |
#include "js/SourceText.h" // for JS::SourceOwnership |
| 28 |
#include "js/String.h" // for JS_CopyStringCharsZ |
| 29 |
#include "vm/BytecodeUtil.h" // for JSDVG_SEARCH_STACK |
| 30 |
#include "vm/JSContext.h" // for JSContext (ptr only) |
| 31 |
#include "vm/JSObject.h" // for JSObject, RequireObject |
| 32 |
#include "vm/JSScript.h" // for ScriptSource, ScriptSourceObject |
| 33 |
#include "vm/StringType.h" // for NewStringCopyZ, JSString (ptr only) |
| 34 |
#include "vm/TypedArrayObject.h" // for TypedArrayObject, JSObject::is |
| 35 |
#include "wasm/WasmCode.h" // for Metadata |
| 36 |
#include "wasm/WasmDebug.h" // for DebugState |
| 37 |
#include "wasm/WasmInstance.h" // for Instance |
| 38 |
#include "wasm/WasmJS.h" // for WasmInstanceObject |
| 39 |
#include "wasm/WasmTypeDecls.h" // for Bytes, Rooted<WasmInstanceObject*> |
| 40 |
|
| 41 |
#include "debugger/Debugger-inl.h" // for Debugger::fromJSObject |
| 42 |
#include "vm/JSObject-inl.h" // for InitClass |
| 43 |
#include "vm/NativeObject-inl.h" // for NewTenuredObjectWithGivenProto |
| 44 |
#include "wasm/WasmInstance-inl.h" |
| 45 |
|
| 46 |
namespace js { |
| 47 |
class GlobalObject; |
| 48 |
} |
| 49 |
|
| 50 |
using namespace js; |
| 51 |
|
| 52 |
using mozilla::AsVariant; |
| 53 |
using mozilla::Maybe; |
| 54 |
using mozilla::Nothing; |
| 55 |
using mozilla::Some; |
| 56 |
|
| 57 |
const JSClassOps DebuggerSource::classOps_ = { |
| 58 |
nullptr, // addProperty |
| 59 |
nullptr, // delProperty |
| 60 |
nullptr, // enumerate |
| 61 |
nullptr, // newEnumerate |
| 62 |
nullptr, // resolve |
| 63 |
nullptr, // mayResolve |
| 64 |
nullptr, // finalize |
| 65 |
nullptr, // call |
| 66 |
nullptr, // construct |
| 67 |
CallTraceMethod<DebuggerSource>, // trace |
| 68 |
}; |
| 69 |
|
| 70 |
const JSClass DebuggerSource::class_ = { |
| 71 |
"Source", |
| 72 |
JSCLASS_HAS_RESERVED_SLOTS(RESERVED_SLOTS), |
| 73 |
&classOps_, |
| 74 |
}; |
| 75 |
|
| 76 |
/* static */ |
| 77 |
NativeObject* DebuggerSource::initClass(JSContext* cx, |
| 78 |
Handle<GlobalObject*> global, |
| 79 |
HandleObject debugCtor) { |
| 80 |
return InitClass(cx, debugCtor, nullptr, nullptr, "Source", construct, 0, |
| 81 |
properties_, methods_, nullptr, nullptr); |
| 82 |
} |
| 83 |
|
| 84 |
/* static */ |
| 85 |
DebuggerSource* DebuggerSource::create(JSContext* cx, HandleObject proto, |
| 86 |
Handle<DebuggerSourceReferent> referent, |
| 87 |
Handle<NativeObject*> debugger) { |
| 88 |
Rooted<DebuggerSource*> sourceObj( |
| 89 |
cx, NewTenuredObjectWithGivenProto<DebuggerSource>(cx, proto)); |
| 90 |
if (!sourceObj) { |
| 91 |
return nullptr; |
| 92 |
} |
| 93 |
sourceObj->setReservedSlot(OWNER_SLOT, ObjectValue(*debugger)); |
| 94 |
referent.get().match([&](auto sourceHandle) { |
| 95 |
sourceObj->setReservedSlotGCThingAsPrivate(SOURCE_SLOT, sourceHandle); |
| 96 |
}); |
| 97 |
|
| 98 |
return sourceObj; |
| 99 |
} |
| 100 |
|
| 101 |
Debugger* DebuggerSource::owner() const { |
| 102 |
JSObject* dbgobj = &getReservedSlot(OWNER_SLOT).toObject(); |
| 103 |
return Debugger::fromJSObject(dbgobj); |
| 104 |
} |
| 105 |
|
| 106 |
// For internal use only. |
| 107 |
NativeObject* DebuggerSource::getReferentRawObject() const { |
| 108 |
return maybePtrFromReservedSlot<NativeObject>(SOURCE_SLOT); |
| 109 |
} |
| 110 |
|
| 111 |
DebuggerSourceReferent DebuggerSource::getReferent() const { |
| 112 |
if (NativeObject* referent = getReferentRawObject()) { |
| 113 |
if (referent->is<ScriptSourceObject>()) { |
| 114 |
return AsVariant(&referent->as<ScriptSourceObject>()); |
| 115 |
} |
| 116 |
return AsVariant(&referent->as<WasmInstanceObject>()); |
| 117 |
} |
| 118 |
return AsVariant(static_cast<ScriptSourceObject*>(nullptr)); |
| 119 |
} |
| 120 |
|
| 121 |
void DebuggerSource::trace(JSTracer* trc) { |
| 122 |
// There is a barrier on private pointers, so the Unbarriered marking |
| 123 |
// is okay. |
| 124 |
if (JSObject* referent = getReferentRawObject()) { |
| 125 |
TraceManuallyBarrieredCrossCompartmentEdge(trc, this, &referent, |
| 126 |
"Debugger.Source referent"); |
| 127 |
if (referent != getReferentRawObject()) { |
| 128 |
setReservedSlotGCThingAsPrivateUnbarriered(SOURCE_SLOT, referent); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/* static */ |
| 134 |
bool DebuggerSource::construct(JSContext* cx, unsigned argc, Value* vp) { |
| 135 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_NO_CONSTRUCTOR, |
| 136 |
"Debugger.Source"); |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
/* static */ |
| 141 |
DebuggerSource* DebuggerSource::check(JSContext* cx, HandleValue thisv) { |
| 142 |
JSObject* thisobj = RequireObject(cx, thisv); |
| 143 |
if (!thisobj) { |
| 144 |
return nullptr; |
| 145 |
} |
| 146 |
if (!thisobj->is<DebuggerSource>()) { |
| 147 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 148 |
JSMSG_INCOMPATIBLE_PROTO, "Debugger.Source", |
| 149 |
"method", thisobj->getClass()->name); |
| 150 |
return nullptr; |
| 151 |
} |
| 152 |
|
| 153 |
return &thisobj->as<DebuggerSource>(); |
| 154 |
} |
| 155 |
|
| 156 |
struct MOZ_STACK_CLASS DebuggerSource::CallData { |
| 157 |
JSContext* cx; |
| 158 |
const CallArgs& args; |
| 159 |
|
| 160 |
Handle<DebuggerSource*> obj; |
| 161 |
Rooted<DebuggerSourceReferent> referent; |
| 162 |
|
| 163 |
CallData(JSContext* cx, const CallArgs& args, Handle<DebuggerSource*> obj) |
| 164 |
: cx(cx), args(args), obj(obj), referent(cx, obj->getReferent()) {} |
| 165 |
|
| 166 |
bool getText(); |
| 167 |
bool getBinary(); |
| 168 |
bool getURL(); |
| 169 |
bool getStartLine(); |
| 170 |
bool getStartColumn(); |
| 171 |
bool getId(); |
| 172 |
bool getDisplayURL(); |
| 173 |
bool getElementProperty(); |
| 174 |
bool getIntroductionScript(); |
| 175 |
bool getIntroductionOffset(); |
| 176 |
bool getIntroductionType(); |
| 177 |
bool setSourceMapURL(); |
| 178 |
bool getSourceMapURL(); |
| 179 |
bool reparse(); |
| 180 |
|
| 181 |
using Method = bool (CallData::*)(); |
| 182 |
|
| 183 |
template <Method MyMethod> |
| 184 |
static bool ToNative(JSContext* cx, unsigned argc, Value* vp); |
| 185 |
}; |
| 186 |
|
| 187 |
template <DebuggerSource::CallData::Method MyMethod> |
| 188 |
/* static */ |
| 189 |
bool DebuggerSource::CallData::ToNative(JSContext* cx, unsigned argc, |
| 190 |
Value* vp) { |
| 191 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 192 |
|
| 193 |
Rooted<DebuggerSource*> obj(cx, DebuggerSource::check(cx, args.thisv())); |
| 194 |
if (!obj) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
CallData data(cx, args, obj); |
| 199 |
return (data.*MyMethod)(); |
| 200 |
} |
| 201 |
|
| 202 |
class DebuggerSourceGetTextMatcher { |
| 203 |
JSContext* cx_; |
| 204 |
|
| 205 |
public: |
| 206 |
explicit DebuggerSourceGetTextMatcher(JSContext* cx) : cx_(cx) {} |
| 207 |
|
| 208 |
using ReturnType = JSString*; |
| 209 |
|
| 210 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 211 |
ScriptSource* ss = sourceObject->source(); |
| 212 |
bool hasSourceText; |
| 213 |
if (!ScriptSource::loadSource(cx_, ss, &hasSourceText)) { |
| 214 |
return nullptr; |
| 215 |
} |
| 216 |
if (!hasSourceText) { |
| 217 |
return NewStringCopyZ<CanGC>(cx_, "[no source]"); |
| 218 |
} |
| 219 |
|
| 220 |
// In case of DOM event handler like <div onclick="foo()" the JS code is |
| 221 |
// wrapped into |
| 222 |
// function onclick() {foo()} |
| 223 |
// We want to only return `foo()` here. |
| 224 |
// But only for event handlers, for `new Function("foo()")`, we want to |
| 225 |
// return: |
| 226 |
// function anonymous() {foo()} |
| 227 |
if (ss->hasIntroductionType() && |
| 228 |
strcmp(ss->introductionType(), "eventHandler") == 0 && |
| 229 |
ss->isFunctionBody()) { |
| 230 |
return ss->functionBodyString(cx_); |
| 231 |
} |
| 232 |
|
| 233 |
return ss->substring(cx_, 0, ss->length()); |
| 234 |
} |
| 235 |
|
| 236 |
ReturnType match(Handle<WasmInstanceObject*> instanceObj) { |
| 237 |
wasm::Instance& instance = instanceObj->instance(); |
| 238 |
const char* msg; |
| 239 |
if (!instance.debugEnabled()) { |
| 240 |
msg = "Restart with developer tools open to view WebAssembly source."; |
| 241 |
} else { |
| 242 |
msg = "[debugger missing wasm binary-to-text conversion]"; |
| 243 |
} |
| 244 |
return NewStringCopyZ<CanGC>(cx_, msg); |
| 245 |
} |
| 246 |
}; |
| 247 |
|
| 248 |
bool DebuggerSource::CallData::getText() { |
| 249 |
Value textv = obj->getReservedSlot(TEXT_SLOT); |
| 250 |
if (!textv.isUndefined()) { |
| 251 |
MOZ_ASSERT(textv.isString()); |
| 252 |
args.rval().set(textv); |
| 253 |
return true; |
| 254 |
} |
| 255 |
|
| 256 |
DebuggerSourceGetTextMatcher matcher(cx); |
| 257 |
JSString* str = referent.match(matcher); |
| 258 |
if (!str) { |
| 259 |
return false; |
| 260 |
} |
| 261 |
|
| 262 |
args.rval().setString(str); |
| 263 |
obj->setReservedSlot(TEXT_SLOT, args.rval()); |
| 264 |
return true; |
| 265 |
} |
| 266 |
|
| 267 |
bool DebuggerSource::CallData::getBinary() { |
| 268 |
if (!referent.is<WasmInstanceObject*>()) { |
| 269 |
ReportValueError(cx, JSMSG_DEBUG_BAD_REFERENT, JSDVG_SEARCH_STACK, |
| 270 |
args.thisv(), nullptr, "a wasm source"); |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
Rooted<WasmInstanceObject*> instanceObj(cx, |
| 275 |
referent.as<WasmInstanceObject*>()); |
| 276 |
wasm::Instance& instance = instanceObj->instance(); |
| 277 |
|
| 278 |
if (!instance.debugEnabled()) { |
| 279 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 280 |
JSMSG_DEBUG_NO_BINARY_SOURCE); |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
const wasm::BytecodeSource& bytecode = instance.debug().bytecode(); |
| 285 |
RootedObject arr(cx, JS_NewUint8Array(cx, bytecode.length())); |
| 286 |
if (!arr) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
|
| 290 |
bytecode.copyTo((uint8_t*)arr->as<TypedArrayObject>().dataPointerUnshared()); |
| 291 |
|
| 292 |
args.rval().setObject(*arr); |
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
class DebuggerSourceGetURLMatcher { |
| 297 |
JSContext* cx_; |
| 298 |
|
| 299 |
public: |
| 300 |
explicit DebuggerSourceGetURLMatcher(JSContext* cx) : cx_(cx) {} |
| 301 |
|
| 302 |
using ReturnType = Maybe<JSString*>; |
| 303 |
|
| 304 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 305 |
ScriptSource* ss = sourceObject->source(); |
| 306 |
MOZ_ASSERT(ss); |
| 307 |
if (const char* filename = ss->filename()) { |
| 308 |
JS::UTF8Chars utf8chars(filename, strlen(filename)); |
| 309 |
JSString* str = NewStringCopyUTF8N(cx_, utf8chars); |
| 310 |
return Some(str); |
| 311 |
} |
| 312 |
return Nothing(); |
| 313 |
} |
| 314 |
ReturnType match(Handle<WasmInstanceObject*> instanceObj) { |
| 315 |
return Some(instanceObj->instance().createDisplayURL(cx_)); |
| 316 |
} |
| 317 |
}; |
| 318 |
|
| 319 |
bool DebuggerSource::CallData::getURL() { |
| 320 |
DebuggerSourceGetURLMatcher matcher(cx); |
| 321 |
Maybe<JSString*> str = referent.match(matcher); |
| 322 |
if (str.isSome()) { |
| 323 |
if (!*str) { |
| 324 |
return false; |
| 325 |
} |
| 326 |
args.rval().setString(*str); |
| 327 |
} else { |
| 328 |
args.rval().setNull(); |
| 329 |
} |
| 330 |
return true; |
| 331 |
} |
| 332 |
|
| 333 |
class DebuggerSourceGetStartLineMatcher { |
| 334 |
public: |
| 335 |
using ReturnType = uint32_t; |
| 336 |
|
| 337 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 338 |
ScriptSource* ss = sourceObject->source(); |
| 339 |
return ss->startLine(); |
| 340 |
} |
| 341 |
ReturnType match(Handle<WasmInstanceObject*> instanceObj) { return 0; } |
| 342 |
}; |
| 343 |
|
| 344 |
bool DebuggerSource::CallData::getStartLine() { |
| 345 |
DebuggerSourceGetStartLineMatcher matcher; |
| 346 |
uint32_t line = referent.match(matcher); |
| 347 |
args.rval().setNumber(line); |
| 348 |
return true; |
| 349 |
} |
| 350 |
|
| 351 |
class DebuggerSourceGetStartColumnMatcher { |
| 352 |
public: |
| 353 |
using ReturnType = JS::LimitedColumnNumberOneOrigin; |
| 354 |
|
| 355 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 356 |
ScriptSource* ss = sourceObject->source(); |
| 357 |
return ss->startColumn(); |
| 358 |
} |
| 359 |
ReturnType match(Handle<WasmInstanceObject*> instanceObj) { |
| 360 |
return JS::LimitedColumnNumberOneOrigin( |
| 361 |
JS::WasmFunctionIndex::DefaultBinarySourceColumnNumberOneOrigin); |
| 362 |
} |
| 363 |
}; |
| 364 |
|
| 365 |
bool DebuggerSource::CallData::getStartColumn() { |
| 366 |
DebuggerSourceGetStartColumnMatcher matcher; |
| 367 |
JS::LimitedColumnNumberOneOrigin column = referent.match(matcher); |
| 368 |
args.rval().setNumber(column.oneOriginValue()); |
| 369 |
return true; |
| 370 |
} |
| 371 |
|
| 372 |
class DebuggerSourceGetIdMatcher { |
| 373 |
public: |
| 374 |
using ReturnType = uint32_t; |
| 375 |
|
| 376 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 377 |
ScriptSource* ss = sourceObject->source(); |
| 378 |
return ss->id(); |
| 379 |
} |
| 380 |
ReturnType match(Handle<WasmInstanceObject*> instanceObj) { return 0; } |
| 381 |
}; |
| 382 |
|
| 383 |
bool DebuggerSource::CallData::getId() { |
| 384 |
DebuggerSourceGetIdMatcher matcher; |
| 385 |
uint32_t id = referent.match(matcher); |
| 386 |
args.rval().setNumber(id); |
| 387 |
return true; |
| 388 |
} |
| 389 |
|
| 390 |
struct DebuggerSourceGetDisplayURLMatcher { |
| 391 |
using ReturnType = const char16_t*; |
| 392 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 393 |
ScriptSource* ss = sourceObject->source(); |
| 394 |
MOZ_ASSERT(ss); |
| 395 |
return ss->hasDisplayURL() ? ss->displayURL() : nullptr; |
| 396 |
} |
| 397 |
ReturnType match(Handle<WasmInstanceObject*> wasmInstance) { |
| 398 |
return wasmInstance->instance().codeMetaForAsmJS() |
| 399 |
? wasmInstance->instance() |
| 400 |
.codeMetaForAsmJS() |
| 401 |
->displayURL() // asm.js |
| 402 |
: nullptr; // wasm |
| 403 |
} |
| 404 |
}; |
| 405 |
|
| 406 |
bool DebuggerSource::CallData::getDisplayURL() { |
| 407 |
DebuggerSourceGetDisplayURLMatcher matcher; |
| 408 |
if (const char16_t* displayURL = referent.match(matcher)) { |
| 409 |
JSString* str = JS_NewUCStringCopyZ(cx, displayURL); |
| 410 |
if (!str) { |
| 411 |
return false; |
| 412 |
} |
| 413 |
args.rval().setString(str); |
| 414 |
} else { |
| 415 |
args.rval().setNull(); |
| 416 |
} |
| 417 |
return true; |
| 418 |
} |
| 419 |
|
| 420 |
struct DebuggerSourceGetElementPropertyMatcher { |
| 421 |
using ReturnType = Value; |
| 422 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 423 |
return sourceObject->unwrappedElementAttributeName(); |
| 424 |
} |
| 425 |
ReturnType match(Handle<WasmInstanceObject*> wasmInstance) { |
| 426 |
return UndefinedValue(); |
| 427 |
} |
| 428 |
}; |
| 429 |
|
| 430 |
bool DebuggerSource::CallData::getElementProperty() { |
| 431 |
DebuggerSourceGetElementPropertyMatcher matcher; |
| 432 |
args.rval().set(referent.match(matcher)); |
| 433 |
return obj->owner()->wrapDebuggeeValue(cx, args.rval()); |
| 434 |
} |
| 435 |
|
| 436 |
class DebuggerSourceGetIntroductionScriptMatcher { |
| 437 |
JSContext* cx_; |
| 438 |
Debugger* dbg_; |
| 439 |
MutableHandleValue rval_; |
| 440 |
|
| 441 |
public: |
| 442 |
DebuggerSourceGetIntroductionScriptMatcher(JSContext* cx, Debugger* dbg, |
| 443 |
MutableHandleValue rval) |
| 444 |
: cx_(cx), dbg_(dbg), rval_(rval) {} |
| 445 |
|
| 446 |
using ReturnType = bool; |
| 447 |
|
| 448 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 449 |
Rooted<BaseScript*> script(cx_, |
| 450 |
sourceObject->unwrappedIntroductionScript()); |
| 451 |
if (script) { |
| 452 |
RootedObject scriptDO(cx_, dbg_->wrapScript(cx_, script)); |
| 453 |
if (!scriptDO) { |
| 454 |
return false; |
| 455 |
} |
| 456 |
rval_.setObject(*scriptDO); |
| 457 |
} else { |
| 458 |
rval_.setUndefined(); |
| 459 |
} |
| 460 |
return true; |
| 461 |
} |
| 462 |
|
| 463 |
ReturnType match(Handle<WasmInstanceObject*> wasmInstance) { |
| 464 |
RootedObject ds(cx_, dbg_->wrapWasmScript(cx_, wasmInstance)); |
| 465 |
if (!ds) { |
| 466 |
return false; |
| 467 |
} |
| 468 |
rval_.setObject(*ds); |
| 469 |
return true; |
| 470 |
} |
| 471 |
}; |
| 472 |
|
| 473 |
bool DebuggerSource::CallData::getIntroductionScript() { |
| 474 |
Debugger* dbg = obj->owner(); |
| 475 |
DebuggerSourceGetIntroductionScriptMatcher matcher(cx, dbg, args.rval()); |
| 476 |
return referent.match(matcher); |
| 477 |
} |
| 478 |
|
| 479 |
struct DebuggerGetIntroductionOffsetMatcher { |
| 480 |
using ReturnType = Value; |
| 481 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 482 |
// Regardless of what's recorded in the ScriptSourceObject and |
| 483 |
// ScriptSource, only hand out the introduction offset if we also have |
| 484 |
// the script within which it applies. |
| 485 |
ScriptSource* ss = sourceObject->source(); |
| 486 |
if (ss->hasIntroductionOffset() && |
| 487 |
sourceObject->unwrappedIntroductionScript()) { |
| 488 |
return Int32Value(ss->introductionOffset()); |
| 489 |
} |
| 490 |
return UndefinedValue(); |
| 491 |
} |
| 492 |
ReturnType match(Handle<WasmInstanceObject*> wasmInstance) { |
| 493 |
return UndefinedValue(); |
| 494 |
} |
| 495 |
}; |
| 496 |
|
| 497 |
bool DebuggerSource::CallData::getIntroductionOffset() { |
| 498 |
DebuggerGetIntroductionOffsetMatcher matcher; |
| 499 |
args.rval().set(referent.match(matcher)); |
| 500 |
return true; |
| 501 |
} |
| 502 |
|
| 503 |
struct DebuggerSourceGetIntroductionTypeMatcher { |
| 504 |
using ReturnType = const char*; |
| 505 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 506 |
ScriptSource* ss = sourceObject->source(); |
| 507 |
MOZ_ASSERT(ss); |
| 508 |
return ss->hasIntroductionType() ? ss->introductionType() : nullptr; |
| 509 |
} |
| 510 |
ReturnType match(Handle<WasmInstanceObject*> wasmInstance) { return "wasm"; } |
| 511 |
}; |
| 512 |
|
| 513 |
bool DebuggerSource::CallData::getIntroductionType() { |
| 514 |
DebuggerSourceGetIntroductionTypeMatcher matcher; |
| 515 |
if (const char* introductionType = referent.match(matcher)) { |
| 516 |
JSString* str = NewStringCopyZ<CanGC>(cx, introductionType); |
| 517 |
if (!str) { |
| 518 |
return false; |
| 519 |
} |
| 520 |
args.rval().setString(str); |
| 521 |
} else { |
| 522 |
args.rval().setUndefined(); |
| 523 |
} |
| 524 |
|
| 525 |
return true; |
| 526 |
} |
| 527 |
|
| 528 |
ScriptSourceObject* EnsureSourceObject(JSContext* cx, |
| 529 |
Handle<DebuggerSource*> obj) { |
| 530 |
if (!obj->getReferent().is<ScriptSourceObject*>()) { |
| 531 |
RootedValue v(cx, ObjectValue(*obj)); |
| 532 |
ReportValueError(cx, JSMSG_DEBUG_BAD_REFERENT, JSDVG_SEARCH_STACK, v, |
| 533 |
nullptr, "a JS source"); |
| 534 |
return nullptr; |
| 535 |
} |
| 536 |
return obj->getReferent().as<ScriptSourceObject*>(); |
| 537 |
} |
| 538 |
|
| 539 |
bool DebuggerSource::CallData::setSourceMapURL() { |
| 540 |
Rooted<ScriptSourceObject*> sourceObject(cx, EnsureSourceObject(cx, obj)); |
| 541 |
if (!sourceObject) { |
| 542 |
return false; |
| 543 |
} |
| 544 |
ScriptSource* ss = sourceObject->source(); |
| 545 |
MOZ_ASSERT(ss); |
| 546 |
|
| 547 |
if (!args.requireAtLeast(cx, "set sourceMapURL", 1)) { |
| 548 |
return false; |
| 549 |
} |
| 550 |
|
| 551 |
JSString* str = ToString<CanGC>(cx, args[0]); |
| 552 |
if (!str) { |
| 553 |
return false; |
| 554 |
} |
| 555 |
|
| 556 |
UniqueTwoByteChars chars = JS_CopyStringCharsZ(cx, str); |
| 557 |
if (!chars) { |
| 558 |
return false; |
| 559 |
} |
| 560 |
|
| 561 |
AutoReportFrontendContext fc(cx); |
| 562 |
if (!ss->setSourceMapURL(&fc, std::move(chars))) { |
| 563 |
return false; |
| 564 |
} |
| 565 |
|
| 566 |
args.rval().setUndefined(); |
| 567 |
return true; |
| 568 |
} |
| 569 |
|
| 570 |
class DebuggerSourceGetSourceMapURLMatcher { |
| 571 |
JSContext* cx_; |
| 572 |
MutableHandleString result_; |
| 573 |
|
| 574 |
public: |
| 575 |
explicit DebuggerSourceGetSourceMapURLMatcher(JSContext* cx, |
| 576 |
MutableHandleString result) |
| 577 |
: cx_(cx), result_(result) {} |
| 578 |
|
| 579 |
using ReturnType = bool; |
| 580 |
ReturnType match(Handle<ScriptSourceObject*> sourceObject) { |
| 581 |
ScriptSource* ss = sourceObject->source(); |
| 582 |
MOZ_ASSERT(ss); |
| 583 |
if (!ss->hasSourceMapURL()) { |
| 584 |
result_.set(nullptr); |
| 585 |
return true; |
| 586 |
} |
| 587 |
JSString* str = JS_NewUCStringCopyZ(cx_, ss->sourceMapURL()); |
| 588 |
if (!str) { |
| 589 |
return false; |
| 590 |
} |
| 591 |
result_.set(str); |
| 592 |
return true; |
| 593 |
} |
| 594 |
ReturnType match(Handle<WasmInstanceObject*> instanceObj) { |
| 595 |
wasm::Instance& instance = instanceObj->instance(); |
| 596 |
if (!instance.debugEnabled()) { |
| 597 |
result_.set(nullptr); |
| 598 |
return true; |
| 599 |
} |
| 600 |
|
| 601 |
RootedString str(cx_); |
| 602 |
if (!instance.debug().getSourceMappingURL(cx_, &str)) { |
| 603 |
return false; |
| 604 |
} |
| 605 |
|
| 606 |
result_.set(str); |
| 607 |
return true; |
| 608 |
} |
| 609 |
}; |
| 610 |
|
| 611 |
bool DebuggerSource::CallData::getSourceMapURL() { |
| 612 |
RootedString result(cx); |
| 613 |
DebuggerSourceGetSourceMapURLMatcher matcher(cx, &result); |
| 614 |
if (!referent.match(matcher)) { |
| 615 |
return false; |
| 616 |
} |
| 617 |
if (result) { |
| 618 |
args.rval().setString(result); |
| 619 |
} else { |
| 620 |
args.rval().setNull(); |
| 621 |
} |
| 622 |
return true; |
| 623 |
} |
| 624 |
|
| 625 |
template <typename Unit> |
| 626 |
static JSScript* ReparseSource(JSContext* cx, Handle<ScriptSourceObject*> sso) { |
| 627 |
AutoRealm ar(cx, sso); |
| 628 |
ScriptSource* ss = sso->source(); |
| 629 |
|
| 630 |
JS::CompileOptions options(cx); |
| 631 |
options.setHideScriptFromDebugger(true); |
| 632 |
options.setFileAndLine(ss->filename(), ss->startLine()); |
| 633 |
options.setColumn(JS::ColumnNumberOneOrigin(ss->startColumn())); |
| 634 |
|
| 635 |
UncompressedSourceCache::AutoHoldEntry holder; |
| 636 |
|
| 637 |
ScriptSource::PinnedUnits<Unit> units(cx, ss, holder, 0, ss->length()); |
| 638 |
if (!units.get()) { |
| 639 |
return nullptr; |
| 640 |
} |
| 641 |
|
| 642 |
JS::SourceText<Unit> srcBuf; |
| 643 |
if (!srcBuf.init(cx, units.get(), ss->length(), |
| 644 |
JS::SourceOwnership::Borrowed)) { |
| 645 |
return nullptr; |
| 646 |
} |
| 647 |
|
| 648 |
return JS::Compile(cx, options, srcBuf); |
| 649 |
} |
| 650 |
|
| 651 |
bool DebuggerSource::CallData::reparse() { |
| 652 |
Rooted<ScriptSourceObject*> sourceObject(cx, EnsureSourceObject(cx, obj)); |
| 653 |
if (!sourceObject) { |
| 654 |
return false; |
| 655 |
} |
| 656 |
|
| 657 |
if (!sourceObject->source()->hasSourceText()) { |
| 658 |
JS_ReportErrorASCII(cx, "Source object missing text"); |
| 659 |
return false; |
| 660 |
} |
| 661 |
|
| 662 |
RootedScript script(cx); |
| 663 |
if (sourceObject->source()->hasSourceType<mozilla::Utf8Unit>()) { |
| 664 |
script = ReparseSource<mozilla::Utf8Unit>(cx, sourceObject); |
| 665 |
} else { |
| 666 |
script = ReparseSource<char16_t>(cx, sourceObject); |
| 667 |
} |
| 668 |
|
| 669 |
if (!script) { |
| 670 |
return false; |
| 671 |
} |
| 672 |
|
| 673 |
Debugger* dbg = obj->owner(); |
| 674 |
RootedObject scriptDO(cx, dbg->wrapScript(cx, script)); |
| 675 |
if (!scriptDO) { |
| 676 |
return false; |
| 677 |
} |
| 678 |
|
| 679 |
args.rval().setObject(*scriptDO); |
| 680 |
return true; |
| 681 |
} |
| 682 |
|
| 683 |
const JSPropertySpec DebuggerSource::properties_[] = { |
| 684 |
JS_DEBUG_PSG("text", getText), |
| 685 |
JS_DEBUG_PSG("binary", getBinary), |
| 686 |
JS_DEBUG_PSG("url", getURL), |
| 687 |
JS_DEBUG_PSG("startLine", getStartLine), |
| 688 |
JS_DEBUG_PSG("startColumn", getStartColumn), |
| 689 |
JS_DEBUG_PSG("id", getId), |
| 690 |
JS_DEBUG_PSG("displayURL", getDisplayURL), |
| 691 |
JS_DEBUG_PSG("introductionScript", getIntroductionScript), |
| 692 |
JS_DEBUG_PSG("introductionOffset", getIntroductionOffset), |
| 693 |
JS_DEBUG_PSG("introductionType", getIntroductionType), |
| 694 |
JS_DEBUG_PSG("elementAttributeName", getElementProperty), |
| 695 |
JS_DEBUG_PSGS("sourceMapURL", getSourceMapURL, setSourceMapURL), |
| 696 |
JS_PS_END, |
| 697 |
}; |
| 698 |
|
| 699 |
const JSFunctionSpec DebuggerSource::methods_[] = { |
| 700 |
JS_DEBUG_FN("reparse", reparse, 0), |
| 701 |
JS_FS_END, |
| 702 |
}; |