From: Mark Wooding Date: Fri, 27 Jun 2014 15:35:16 +0000 (+0100) Subject: dep.js, dep-ui.js: Remove `with (MOD) { ... }' wrappers from modules. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/dep-ui/commitdiff_plain/6cfd4191b11af6db92fd970b7b6f9e2f9c876d83 dep.js, dep-ui.js: Remove `with (MOD) { ... }' wrappers from modules. Using `with' is apparently an excellent way of inhibiting optimization in V8 (and probably other implementations). --- diff --git a/dep-ui.js b/dep-ui.js index 54f97b5..8e8e767 100644 --- a/dep-ui.js +++ b/dep-ui.js @@ -21,18 +21,19 @@ * along with this program; if not, see . */ -var DEP_UI = {}; (function () { with (DEP_UI) { +var DEP_UI = {}; (function () { /*----- Utility functions and classes -------------------------------------*/ -DEP_UI.debug = function(msg) { +function debug(msg) { /* Write the string MSG to the `trace' element, if there is one. */ var e = elt('trace'); if (e !== null) e.textContent += msg; } +DEP_UI.debug = debug; - DEP_UI.trap = function(what, func) { +function trap(what, func) { try { func(); } catch (e) { @@ -40,30 +41,34 @@ DEP_UI.debug = function(msg) { throw e; } } +DEP_UI.trap = trap; -DEP_UI.elt = function (id) { +function elt(id) { /* Find and return the element with the given ID. */ return document.getElementById(id); } +DEP_UI.elt = elt; -DEP_UI.add_elt_class = function (elt, cls) { +function add_elt_class(elt, cls) { /* Add the class name CLS to element ELT's `class' attribute. */ if (!elt.className.match('\\b' + cls + '\\b')) elt.className += ' ' + cls } +DEP_UI.add_elt_class = add_elt_class; -DEP_UI.rm_elt_class = function (elt, cls) { +function rm_elt_class(elt, cls) { /* Remove the class name CLS from element ELT's `class' attribute. */ elt.className = elt.className.replace( new RegExp ('\\s*\\b' + cls + '\\b\\s*'), ' '); } +DEP_UI.rm_elt_class = rm_elt_class; /* A gadget which can arrange to perform an idempotent action (the FUNC * argument) again `soon'. */ -DEP_UI.Soon = function (func) { +function Soon(func) { this.timer = null; this.func = func; } @@ -79,15 +84,16 @@ Soon.prototype = { this.timer = setTimeout(function () { me.func(); }, 50); } }; +DEP.Soon = Soon; /*----- Conversion machinery ----------------------------------------------*/ /* An exception, thrown if a conversion function doesn't like what it * sees. */ -DEP_UI.BadValue = new DEP.Tag('BadValue'); +BadValue = new DEP.Tag('BadValue'); DEP.BadValue = BadValue; -DEP_UI.convert_to_numeric = function (string) { +function convert_to_numeric(string) { /* Convert the argument STRING to a number. */ if (!string.match('\\S')) throw BadValue; @@ -95,18 +101,20 @@ DEP_UI.convert_to_numeric = function (string) { if (n !== n) throw BadValue; return n; } +DEP_UI.convert_to_numeric = convert_to_numeric; -DEP_UI.convert_from_numeric = function (num) { +function convert_from_numeric(num) { /* Convert the argument number NUM to a string, in a useful way. */ return num.toFixed(3); } +DEP_UI.convert_from_numeric = convert_from_numeric; /*----- User interface functions ------------------------------------------*/ /* A list of input fields which might need periodic kicking. */ var KICK_INPUT_FIELDS = []; -DEP_UI.input_field = function (id, dep, convert) { +function input_field(id, dep, convert) { /* Bind an input field (with the given ID) to a DEP, converting the user's * input with the CONVERT function. */ @@ -138,7 +146,7 @@ DEP_UI.input_field = function (id, dep, convert) { // Arrange to update the dep `shortly after' updates. var soon = new Soon(kick); - function kick_soon () { soon.kick(); } + function kick_soon() { soon.kick(); } e.addEventListener('click', kick_soon, false); e.addEventListener('blur', kick_soon, false); e.addEventListener('keypress', kick_soon, false); @@ -149,15 +157,16 @@ DEP_UI.input_field = function (id, dep, convert) { // such functions to be run periodically just in case. KICK_INPUT_FIELDS.push(kick); } +DEP_UI.input_field = input_field; -DEP_UI.input_radio = function (id, dep) { +function input_radio(id, dep) { /* Bind a radio button (with the given ID) to a DEP. When the user frobs * the button, set the dep to the element's `value' attribute. */ var e = elt(id); - function kick () { + function kick() { // Make sure we're actually chosen. We get called periodically // regardless of user input. if (e.checked) dep.set_value(e.value); @@ -168,7 +177,7 @@ DEP_UI.input_radio = function (id, dep) { // Arrange to update the dep `shortly after' updates. var soon = new Soon(kick); - function kick_soon () { soon.kick(); } + function kick_soon() { soon.kick(); } e.addEventListener('click', kick_soon, false); e.addEventListener('changed', kick_soon, false); @@ -176,8 +185,9 @@ DEP_UI.input_radio = function (id, dep) { // but let's be on the safe side. KICK_INPUT_FIELDS.push(kick); } +DEP_UI.input_radio = input_radio; -DEP_UI.output_field = function (id, dep, convert) { +function output_field(id, dep, convert) { /* Bind a DEP to an output element (given by ID), converting the dep's * value using the CONVERT function. */ @@ -202,6 +212,7 @@ DEP_UI.output_field = function (id, dep, convert) { dep.add_listener(kicked); kicked(); } +DEP_UI.output_field = output_field; /*----- Periodic maintenance ----------------------------------------------*/ @@ -219,4 +230,4 @@ setInterval(kick_all, 500); window.addEventListener('load', kick_all, false); /*----- That's all, folks -------------------------------------------------*/ -} })(); +})(); diff --git a/dep.js b/dep.js index 4376b89..9dd42b4 100644 --- a/dep.js +++ b/dep.js @@ -21,11 +21,11 @@ * along with this program; if not, see . */ -var DEP = { }; (function () { with (DEP) { +var DEP = { }; (function () { /*----- Utility functions and classes -------------------------------------*/ -DEP.dolist = function (list, func) { +function dolist(list, func) { /* Apply FUNC to each element of LIST, discarding the results. * * JavaScript's looping iterates over indices rather than values, which is @@ -36,6 +36,7 @@ DEP.dolist = function (list, func) { var i; for (i in list) func(list[i]); } +DEP.dolist = dolist; function eql(x, y) { /* A standard equality function, suitable for detecting changes to `Dep' @@ -50,18 +51,19 @@ function eql(x, y) { * that the set of Tags is basically determined by the static structure of * the program. */ -DEP.Tag = function (what) { +function Tag(what) { this.what = what; } Tag.prototype = { toString: function () { return '#{Tag ' + this.what + '}'; } } +DEP.Tag = Tag; /* A Generation is like a Tag, except that it's expected that a program will * manufacture Generations repeatedly, and perhaps use them to determine * whether an object is `up-to-date' in some sense. */ -DEP.Generation = function (what) { +function Generation(what) { this.what = what; this.seq = Generation._next++; } @@ -95,8 +97,11 @@ var PENDING = []; // Deps awaiting recomputation. /*----- Exceptions --------------------------------------------------------*/ -DEP.CircularDependency = new Tag('CircularDependency'); -DEP.BusyRecomputing = new Tag('BusyRecomputing'); +CircularDependency = new Tag('CircularDependency'); +DEP.CircularDependency = CircularDependency; + +BusyRecomputing = new Tag('BusyRecomputing'); +DEP.BusyRecomputing = BusyRecomputing; /*----- Main code ---------------------------------------------------------*/ @@ -112,7 +117,7 @@ DEP.BusyRecomputing = new Tag('BusyRecomputing'); * causes that `Dep' to become bad in turn. */ -DEP.Dep = function (value, maybefunc) { +function Dep(value, maybefunc) { /* Initialize a new `Dep' object. * * Handling of the arguments is a little fiddly. If two arguments are @@ -177,6 +182,7 @@ DEP.Dep = function (value, maybefunc) { } } +DEP.Dep = Dep; Dep._seq = 0; // Next sequence number to allocate. Dep.prototype = { @@ -416,7 +422,7 @@ Dep.prototype = { } }; -DEP.orelse = function (thunk, errthunk) { +function orelse(thunk, errthunk) { /* Call THUNK. If it succeeds, then return its result. If THUNK * reads a bad dep then call ERRTHINK and return its result instead. */ @@ -428,11 +434,13 @@ DEP.orelse = function (thunk, errthunk) { else throw e; } } +DEP.orelse = orelse; -DEP.bad = function () { +function bad() { /* For use in a value-function: make the dep be bad. */ throw BAD; } +DEP.bad = bad; function recompute_pending() { /* Recompute any pending dependencies. @@ -463,7 +471,7 @@ function recompute_pending() { } } -DEP.with_frozen = function (body, delay) { +function with_frozen(body, delay) { /* Call BODY with dependencies frozen. * * If the BODY function changes any dep values (using D.set_value(...)) @@ -507,6 +515,7 @@ DEP.with_frozen = function (body, delay) { break; } } +DEP.with_frozen = with_frozen; /*----- That's all, folks -------------------------------------------------*/ -} })(); +})();