Skip to content

Commit

Permalink
watcher plugin consolidate
Browse files Browse the repository at this point in the history
  • Loading branch information
olov committed May 2, 2011
1 parent c5a60c6 commit d74d188
Show file tree
Hide file tree
Showing 6 changed files with 327 additions and 27 deletions.
37 changes: 37 additions & 0 deletions src/plugins/watcher/tests/example-shaped.js
@@ -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]
37 changes: 37 additions & 0 deletions src/plugins/watcher/tests/example.js
@@ -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]
122 changes: 122 additions & 0 deletions src/plugins/watcher/tests/forms-shaped.js
@@ -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");
122 changes: 122 additions & 0 deletions src/plugins/watcher/tests/forms.js
@@ -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);
11 changes: 9 additions & 2 deletions src/plugins/watcher.js → src/plugins/watcher/watcher.js
Expand Up @@ -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] = "+=";
Expand All @@ -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 ?
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
25 changes: 0 additions & 25 deletions tests/test-watcher.js

This file was deleted.

0 comments on commit d74d188

Please sign in to comment.