Permalink
Browse files

watcher plugin consolidate

  • Loading branch information...
olov committed May 2, 2011
1 parent c5a60c6 commit d74d188c10b0fe17d55b761b9e30485801120512
@@ -0,0 +1,37 @@
"use strict"; "use restrict";
var require = require || function(f) { load(f); };
var Watch = Watch || require("./plugins/watcher/watch.js") || Watch;
var Log = Log || require("./log.js") || Log;
// watch o for invalid age [1]
var o = {name: "marvin", age: 100000};
Watch(o, "age", function() {
if (o.age < 0) {
Log.verb("plugins/watcher/tests/pre-example.js", 11, "<anonymous>", "invalid age ({0})", o.age);
}
});
// watch arr for non-string assignments [2]
var arr = ["asdf", "zxcv"];
Watch(arr, null, function(arr, name, op) {
var v = arr[name];
if (typeof v !== "string") {
Log.verb("plugins/watcher/tests/pre-example.js", 20, "<anonymous>", "arr[{0}] assigned with {1} of type {2}", name, v, typeof v);
}
});
// watch Array.prototype for patches [3]
Watch(Array.prototype, null, function(obj, name, op) {
Log.verb("plugins/watcher/tests/pre-example.js", 26, "<anonymous>", "patched Array.prototype.{0}", name);
});
// modify push so that pushes are watched, too. triggers [3]
Watch.set("=", Array.prototype, "push", function(v) {
return Watch.set("=", this, String(this.length), v);
});
Watch.set("+=", o, "age", 1);
Watch.set("/=", o, "age", -1); // triggers [1]
Watch.set("=", arr, String(2), "ok");
arr.push(0); // triggers [2]
@@ -0,0 +1,37 @@
"use strict"; "use restrict";
var require = require || function(f) { load(f); };
var Watch = Watch || require("./plugins/watcher/watch.js") || Watch;
var Log = Log || require("./log.js") || Log;
// watch o for invalid age [1]
var o = {name: "marvin", age: 100000};
Watch(o, "age", function() {
if (o.age < 0) {
Log("invalid age ({0})", o.age);
}
});
// watch arr for non-string assignments [2]
var arr = ["asdf", "zxcv"];
Watch(arr, null, function(arr, name, op) {
var v = arr[name];
if (typeof v !== "string") {
Log("arr[{0}] assigned with {1} of type {2}", name, v, typeof v);
}
});
// watch Array.prototype for patches [3]
Watch(Array.prototype, null, function(obj, name, op) {
Log("patched Array.prototype.{0}", name);
});
// modify push so that pushes are watched, too. triggers [3]
Array.prototype.push = function(v) {
return this[this.length] = v;
};
o.age += 1;
o["age"] /= -1; // triggers [1]
arr[2] = "ok";
arr.push(0); // triggers [2]
@@ -0,0 +1,122 @@
"use strict"; "use restrict";
var require = require || function(f) { load(f); };
var Assert = Assert || require("./assert.js") || Assert;
var Watch = Watch || require("./plugins/watcher/watch.js") || Watch;
var v = 123;
var o = {id: v};
var strid = "id";
// o.id
Watch.set("++v", o, "id");
Watch.set("v++", o, "id");
Watch.set("--v", o, "id");
Watch.set("v--", o, "id");
Watch.set("=", o, "id", null || v);
Watch.set("+=", o, "id", null || v);
Watch.set("-=", o, "id", null || v);
Watch.set("*=", o, "id", null || v);
Watch.set("/=", o, "id", null || v);
Watch.set("%=", o, "id", null || 2);
Watch.set("&=", o, "id", null || v);
Watch.set("|=", o, "id", null || v);
Watch.set("^=", o, "id", null || -1);
Watch.set("<<=", o, "id", null || v);
Watch.set(">>=", o, "id", null || v);
Watch.set(">>>=", o, "id", null || 2);
Assert(o.id === 1, "o.id === 1, function <script>, file plugins/watcher/tests/pre-forms.js, line 27");
// expr.id
Watch.set("++v", (null || o), "id");
Watch.set("v++", (null || o), "id");
Watch.set("--v", (null || o), "id");
Watch.set("v--", (null || o), "id");
Watch.set("=", (null || o), "id", null || v);
Watch.set("+=", (null || o), "id", null || v);
Watch.set("-=", (null || o), "id", null || v);
Watch.set("*=", (null || o), "id", null || v);
Watch.set("/=", (null || o), "id", null || v);
Watch.set("%=", (null || o), "id", null || 2);
Watch.set("&=", (null || o), "id", null || v);
Watch.set("|=", (null || o), "id", null || v);
Watch.set("^=", (null || o), "id", null || -1);
Watch.set("<<=", (null || o), "id", null || v);
Watch.set(">>=", (null || o), "id", null || v);
Watch.set(">>>=", (null || o), "id", null || 2);
Assert(o.id === 1, "o.id === 1, function <script>, file plugins/watcher/tests/pre-forms.js, line 46");
// o["str"]
Watch.set("++v", o, "id");
Watch.set("v++", o, "id");
Watch.set("--v", o, "id");
Watch.set("v--", o, "id");
Watch.set("=", o, "id", null || v);
Watch.set("+=", o, "id", null || v);
Watch.set("-=", o, "id", null || v);
Watch.set("*=", o, "id", null || v);
Watch.set("/=", o, "id", null || v);
Watch.set("%=", o, "id", null || 2);
Watch.set("&=", o, "id", null || v);
Watch.set("|=", o, "id", null || v);
Watch.set("^=", o, "id", null || -1);
Watch.set("<<=", o, "id", null || v);
Watch.set(">>=", o, "id", null || v);
Watch.set(">>>=", o, "id", null || 2);
Assert(o.id === 1, "o.id === 1, function <script>, file plugins/watcher/tests/pre-forms.js, line 65");
// expr["str"]
Watch.set("++v", (null || o), "id");
Watch.set("v++", (null || o), "id");
Watch.set("--v", (null || o), "id");
Watch.set("v--", (null || o), "id");
Watch.set("=", (null || o), "id", null || v);
Watch.set("+=", (null || o), "id", null || v);
Watch.set("-=", (null || o), "id", null || v);
Watch.set("*=", (null || o), "id", null || v);
Watch.set("/=", (null || o), "id", null || v);
Watch.set("%=", (null || o), "id", null || 2);
Watch.set("&=", (null || o), "id", null || v);
Watch.set("|=", (null || o), "id", null || v);
Watch.set("^=", (null || o), "id", null || -1);
Watch.set("<<=", (null || o), "id", null || v);
Watch.set(">>=", (null || o), "id", null || v);
Watch.set(">>>=", (null || o), "id", null || 2);
Assert(o.id === 1, "o.id === 1, function <script>, file plugins/watcher/tests/pre-forms.js, line 84");
// o[expr]
Watch.set("++v", o, String(null || strid));
Watch.set("v++", o, String(null || strid));
Watch.set("--v", o, String(null || strid));
Watch.set("v--", o, String(null || strid));
Watch.set("=", o, String(null || strid), null || v);
Watch.set("+=", o, String(null || strid), null || v);
Watch.set("-=", o, String(null || strid), null || v);
Watch.set("*=", o, String(null || strid), null || v);
Watch.set("/=", o, String(null || strid), null || v);
Watch.set("%=", o, String(null || strid), null || 2);
Watch.set("&=", o, String(null || strid), null || v);
Watch.set("|=", o, String(null || strid), null || v);
Watch.set("^=", o, String(null || strid), null || -1);
Watch.set("<<=", o, String(null || strid), null || v);
Watch.set(">>=", o, String(null || strid), null || v);
Watch.set(">>>=", o, String(null || strid), null || 2);
Assert(o.id === 1, "o.id === 1, function <script>, file plugins/watcher/tests/pre-forms.js, line 103");
// expr1[expr2]
Watch.set("++v", (null || o), String(null || strid));
Watch.set("v++", (null || o), String(null || strid));
Watch.set("--v", (null || o), String(null || strid));
Watch.set("v--", (null || o), String(null || strid));
Watch.set("=", (null || o), String(null || strid), null || v);
Watch.set("+=", (null || o), String(null || strid), null || v);
Watch.set("-=", (null || o), String(null || strid), null || v);
Watch.set("*=", (null || o), String(null || strid), null || v);
Watch.set("/=", (null || o), String(null || strid), null || v);
Watch.set("%=", (null || o), String(null || strid), null || 2);
Watch.set("&=", (null || o), String(null || strid), null || v);
Watch.set("|=", (null || o), String(null || strid), null || v);
Watch.set("^=", (null || o), String(null || strid), null || -1);
Watch.set("<<=", (null || o), String(null || strid), null || v);
Watch.set(">>=", (null || o), String(null || strid), null || v);
Watch.set(">>>=", (null || o), String(null || strid), null || 2);
Assert(o.id === 1, "o.id === 1, function <script>, file plugins/watcher/tests/pre-forms.js, line 122");
@@ -0,0 +1,122 @@
"use strict"; "use restrict";
var require = require || function(f) { load(f); };
var Assert = Assert || require("./assert.js") || Assert;
var Watch = Watch || require("./plugins/watcher/watch.js") || Watch;
var v = 123;
var o = {id: v};
var strid = "id";
// o.id
++o.id;
o.id++;
--o.id;
o.id--;
o.id = null || v;
o.id += null || v;
o.id -= null || v;
o.id *= null || v;
o.id /= null || v;
o.id %= null || 2;
o.id &= null || v;
o.id |= null || v;
o.id ^= null || -1;
o.id <<= null || v;
o.id >>= null || v;
o.id >>>= null || 2;
Assert(o.id === 1);
// expr.id
++(null || o).id;
(null || o).id++;
--(null || o).id;
(null || o).id--;
(null || o).id = null || v;
(null || o).id += null || v;
(null || o).id -= null || v;
(null || o).id *= null || v;
(null || o).id /= null || v;
(null || o).id %= null || 2;
(null || o).id &= null || v;
(null || o).id |= null || v;
(null || o).id ^= null || -1;
(null || o).id <<= null || v;
(null || o).id >>= null || v;
(null || o).id >>>= null || 2;
Assert(o.id === 1);
// o["str"]
++o["id"];
o["id"]++;
--o["id"];
o["id"]--;
o["id"] = null || v;
o["id"] += null || v;
o["id"] -= null || v;
o["id"] *= null || v;
o["id"] /= null || v;
o["id"] %= null || 2;
o["id"] &= null || v;
o["id"] |= null || v;
o["id"] ^= null || -1;
o["id"] <<= null || v;
o["id"] >>= null || v;
o["id"] >>>= null || 2;
Assert(o.id === 1);
// expr["str"]
++(null || o)["id"];
(null || o)["id"]++;
--(null || o)["id"];
(null || o)["id"]--;
(null || o)["id"] = null || v;
(null || o)["id"] += null || v;
(null || o)["id"] -= null || v;
(null || o)["id"] *= null || v;
(null || o)["id"] /= null || v;
(null || o)["id"] %= null || 2;
(null || o)["id"] &= null || v;
(null || o)["id"] |= null || v;
(null || o)["id"] ^= null || -1;
(null || o)["id"] <<= null || v;
(null || o)["id"] >>= null || v;
(null || o)["id"] >>>= null || 2;
Assert(o.id === 1);
// o[expr]
++o[null || strid];
o[null || strid]++;
--o[null || strid];
o[null || strid]--;
o[null || strid] = null || v;
o[null || strid] += null || v;
o[null || strid] -= null || v;
o[null || strid] *= null || v;
o[null || strid] /= null || v;
o[null || strid] %= null || 2;
o[null || strid] &= null || v;
o[null || strid] |= null || v;
o[null || strid] ^= null || -1;
o[null || strid] <<= null || v;
o[null || strid] >>= null || v;
o[null || strid] >>>= null || 2;
Assert(o.id === 1);
// expr1[expr2]
++(null || o)[null || strid];
(null || o)[null || strid]++;
--(null || o)[null || strid];
(null || o)[null || strid]--;
(null || o)[null || strid] = null || v;
(null || o)[null || strid] += null || v;
(null || o)[null || strid] -= null || v;
(null || o)[null || strid] *= null || v;
(null || o)[null || strid] /= null || v;
(null || o)[null || strid] %= null || 2;
(null || o)[null || strid] &= null || v;
(null || o)[null || strid] |= null || v;
(null || o)[null || strid] ^= null || -1;
(null || o)[null || strid] <<= null || v;
(null || o)[null || strid] >>= null || v;
(null || o)[null || strid] >>>= null || 2;
Assert(o.id === 1);
@@ -3,6 +3,10 @@
var Fmt = Fmt || require("../fmt.js") || Fmt;
var Shaper = Shaper || require("../shaper.js") || Shaper;
// Shapes object property assignments into Watch.set calls
// handles expr.id, expr1[expr2]
// covers all assignment operators (=, +=, -=, ...) and unary ++, --
Shaper("watcher", function(root) {
var opStr = [];
opStr[tkn.PLUS] = "+=";
@@ -23,6 +27,7 @@ Shaper("watcher", function(root) {
var prop;
var v;
var call;
var strid;
if (node.type === tkn.INCREMENT || node.type === tkn.DECREMENT) {
var c = node.children[0];
op = node.type === tkn.INCREMENT ?
@@ -37,7 +42,8 @@ Shaper("watcher", function(root) {
else if (c.type === tkn.INDEX) { // expr1[expr2]++
expr = c.children[0];
prop = c.children[1];
call = Shaper.parseExpression(Fmt('Watch.set("{0}", $, String($))', op));
strid = (prop.type === tkn.STRING) ? "$" : "String($)";
call = Shaper.parseExpression(Fmt('Watch.set("{0}", $, {1})', op, strid));
Shaper.replace(call, expr, prop);
}
else {
@@ -58,7 +64,8 @@ Shaper("watcher", function(root) {
else if (lvalue.type === tkn.INDEX) { // expr1[expr2] += v
expr = lvalue.children[0];
prop = lvalue.children[1];
call = Shaper.parseExpression(Fmt('Watch.set("{0}", $, String($), $)', op));
strid = (prop.type === tkn.STRING) ? "$" : "String($)";
call = Shaper.parseExpression(Fmt('Watch.set("{0}", $, {1}, $)', op, strid));
Shaper.replace(call, expr, prop, v);
}
else {
View

This file was deleted.

Oops, something went wrong.

0 comments on commit d74d188

Please sign in to comment.