Closed Bug 1564167 Opened 5 years ago Closed 5 years ago

[jsdbg] Debugger argument checking should not use gnarly macros

Categories

(Core :: JavaScript Engine, task, P3)

task

Tracking

()

RESOLVED FIXED
mozilla71
Tracking Status
firefox71 --- fixed

People

(Reporter: jimb, Assigned: bhackett1024)

References

(Blocks 1 open bug)

Details

(Whiteboard: [debugger-mvp])

Attachments

(2 files)

The Debugger implementation uses multi-line preprocessor macros for common argument validation code. These should be replaced with utility classes, for legibility and better error checking.

Priority: -- → P3
Blocks: dbg-71
Whiteboard: [debugger-mvp]
Assignee: nobody → bhackett1024
Status: NEW → ASSIGNED
Attached patch Debugger.Source patch (deleted) — Splinter Review

This patch removes the THIS_DEBUGSOURCE_* macros from Source.cpp, replacing them with a system that I think is pretty clean. Instead of source accessors/functions being static methods on DebuggerSource, they are instance methods on a new structure, DebuggerSource::CallData. The CallData class is a stack-allocated class which holds everything a method being called on a source needs: the JSContext, CallArgs, handle to the Debugger.Object and handle to its referent. For methods which only operate on ScriptSourceObject Debugger.Sources, CallData also provides an ensureSourceObject() method that fills in another member with the SSO. This required a separate macro before, but the CallData struct makes it easy to cleanly deal with using normal C++.

For an example of how the implementation changes, here is the before/after for Debugger.Source.introductionOffset:

Before:

// class DebuggerSource
  static bool getIntroductionOffset(JSContext* cx, unsigned argc, Value* vp);

// DebuggerSource::properties_
    JS_PSG("introductionOffset", getIntroductionOffset, 0),

/* static */
bool DebuggerSource::getIntroductionOffset(JSContext* cx, unsigned argc,
                                           Value* vp) {
  THIS_DEBUGSOURCE_REFERENT(cx, argc, vp, "(get introductionOffset)", args, obj,
                            referent);
  DebuggerGetIntroductionOffsetMatcher matcher;
  args.rval().set(referent.match(matcher));
  return true;
}

After:

// struct CallData
  bool getIntroductionOffset();

// DebuggerSource::properties_
    SOURCE_PSG("introductionOffset", getIntroductionOffset),

bool DebuggerSource::CallData::getIntroductionOffset() {
  DebuggerGetIntroductionOffsetMatcher matcher;
  args.rval().set(referent.match(matcher));
  return true;
}

I wasn't able to avoid custom macros entirely. CallData has a templated ToNative member which creates a native that initializes the CallData and does the checking which the THIS_DEBUGSOURCE_* macros used to do. Specifying this explicitly in the property/method specifier for Debugger.Source is pretty ugly, so we have a few simple macros like the one below to improve readability:

#define SOURCE_PSG(Name, Getter)                        \
  JS_PSG(Name, CallData::ToNative<&CallData::Getter>, 0)

Still reviewing, but I like the approach overall. The elimination of all macros wasn't really a goal; making the bodies of these functions easier to follow and more strictly checked was.

It'd be nice if we could still get the method names; couldn't that be another template argument to ToNative, passed by the macros?

(In reply to Jim Blandy :jimb from comment #3)

It'd be nice if we could still get the method names; couldn't that be another template argument to ToNative, passed by the macros?

I tried this, but C++ doesn't allow you to use string literals as template arguments. There are complicated workarounds which I started looking into, but then I realized that these method names are only used if people do goofy stuff like Debugger.Object.prototype.getOwnPropertyNames.call([]). Fixing this so that the method name is included just didn't seem like a good use of time.

Pushed by bhackett@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/171d5f099ad0 Use CallData structure instead of macros to handle calls to debugger APIs, r=jimb.
Status: ASSIGNED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla71

Backed out for conflicting with the backout of Bug 1576776

Status: RESOLVED → REOPENED
Flags: needinfo?(bhackett1024)
Resolution: FIXED → ---
Target Milestone: mozilla71 → ---
Pushed by bhackett@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/55ffa7886b3e Use CallData structure instead of macros to handle calls to debugger APIs, r=jimb.
Flags: needinfo?(bhackett1024)
Status: REOPENED → RESOLVED
Closed: 5 years ago5 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla71
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: