Closed Bug 223686 Opened 21 years ago Closed 19 years ago

Support XUL-based assertions and warnings?

Categories

(Core :: XUL, enhancement)

x86
All
enhancement
Not set
normal

Tracking

()

RESOLVED WONTFIX

People

(Reporter: WeirdAl, Assigned: hyatt)

Details

In C++ code, developers have the power to set assertions and warnings for bugs
in the design as they see fit.  I'd like to have the same power from JavaScript,
without an onerous requirement.

I would suggest the following for chrome://global/content/ecma_assert.js:

function assert(assertString, thisObj) {
  var throwException = arguments.length > 2 ? arguments[2] : false;
  var argsObj = arguments.length > 3 ? arguments[3] : {}
  var argsString = "";
  for (var property in argsObj) {
    argsString += ("var " + property + " = " + argsObj[property] +";\n");
  }
  var func = new Function(argsString + "return (" + assertString + ");");
  dump (func);
  var mustBeTrue = false;
  try {
    mustBeTrue = func.apply(thisObj);
  }
  catch(e) {
    dump(e + "\n" + e.stack);
  }
  
  try {
    if (!mustBeTrue) {
     throw new Error("ECMAScript assertion failed:  (" + assertString + ")");
    }
  }
  catch(e) {
    if (throwException) {
      throw new Error(e.message + " stack:\n" + e.stack);
    } else {
      dump ("Warning: " + e.message + " stack:\n" + e.stack);
    }
  }
}

Usage:  From whatever JS function wishes to make a warning check, call
assert(assertString, this).  If you want an assertion to actually go out as an
exception, assert(assertString, this, true).  If you want to pass arguments,
pass them as named string properties of an object:

assert("x.ownerDocument == document", this, false, {x: "this.foo"});

I can generate a patch very quickly if this is a desired feature.  As I do not
know whether XUL application developers would want it, I'm filing this as an
RFE.  Comments on making the assert() function better are also welcome.
Hm, a couple things I need to think about:

(1) Optimized builds shouldn't see these assertions.  A blank assert() script
should be provided for those.  Maybe some Perl or Makefile magic can help there.
 biesi also suggests an XUL preprocessor, which I don't know anything about.

(2) dump(func) is a bad idea.  That's something that slipped in from my original
code, sorry.  Ditto the dump right after setting mustBeTrue.
There's no way mozilla.org will accept this.
Status: NEW → RESOLVED
Closed: 19 years ago
Resolution: --- → WONTFIX
Component: XP Toolkit/Widgets: XUL → XUL
QA Contact: shrir → xptoolkit.widgets
You need to log in before you can comment on or make changes to this bug.