Skip to content
Browse files

callProperty function fixes the id tests

  • Loading branch information...
1 parent c515743 commit 444b4376fc0bf6e4654b2e4adf710ac164c906d7 @substack committed May 15, 2012
Showing with 41 additions and 10 deletions.
  1. +34 −3 index.js
  2. +0 −2 test/sources/id.js
  3. +3 −4 test/sources/traverse.js
  4. +4 −1 test/traverse.js
View
37 index.js
@@ -22,7 +22,8 @@ function Fritter (context, opts) {
expr : identifier(6),
stopped : identifier(6),
callPush : identifier(6),
- callPop : identifier(6)
+ callPop : identifier(6),
+ callProperty : identifier(6)
};
this.stack = [];
this.current = undefined;
@@ -64,7 +65,6 @@ Fritter.prototype.defineContext = function () {
return apply(f, this, arguments);
}
};
-
return f_;
}
@@ -77,6 +77,24 @@ Fritter.prototype.defineContext = function () {
if (longStacks) self.stack.shift();
};
+ context[names.callProperty] = function (ix, obj, name) {
+ return context[names.call](ix, function () {
+ var fn = obj[name];
+ if (fn === undefined) {
+ throw new TypeError(
+ String(obj) + ' has no method \'' + name + '\''
+ );
+ }
+ else if (typeof fn !== 'function') {
+ throw new TypeError(
+ 'Property \'' + name + '\' of '
+ + String(obj) + ' is not a function'
+ );
+ }
+ return obj[name].apply(obj, arguments);
+ });
+ };
+
context[names.call] = function (ix, fn) {
if (typeof fn !== 'function') return fn;
@@ -94,7 +112,7 @@ Fritter.prototype.defineContext = function () {
}
}
- var res = fn.apply(undefined, arguments);
+ var res = fn.apply(this, arguments);
self.stack.shift();
return res;
};
@@ -179,6 +197,19 @@ Fritter.prototype.include = function (src, opts) {
+ '}');
pushNode(node);
}
+ else if (node.type === 'CallExpression'
+ && node.callee.type === 'MemberExpression') {
+ node.callee.update(
+ '(' + names.callProperty + '('
+ + nodes.length
+ + ','
+ + node.callee.object.source()
+ + ','
+ + JSON.stringify(node.callee.property.name)
+ + '))'
+ );
+ pushNode(node);
+ }
else if (node.type === 'CallExpression') {
node.callee.update(
'(' + names.call + '('
View
2 test/sources/id.js
@@ -1,5 +1,3 @@
-function id (x) { return x };
-
var xs = [1,2,3].map(function (x) {
return x + 10;
});
View
7 test/sources/traverse.js
@@ -1,11 +1,10 @@
-var traverse = module.exports;
-
var xs = [];
-traverse({ a : 1, b : 2, c : [ 3, 4 ]}, function (node) {
+console.log(traverse.forEach);
+traverse.forEach({ a : 1, b : 2, c : [ 3, 4 ]}, function (node) {
if (this.isLeaf) xs.push(node);
});
t.same(xs, [ 1, 2, 3, 4 ]);
-traverse({ a : 1, b : 2, c : [ 3, 4 ]}, function (node) {
+traverse.forEach({ a : 1, b : 2, c : [ 3, 4 ]}, function (node) {
if (this.isRoot) throw 'beep';
});
View
5 test/traverse.js
@@ -9,9 +9,11 @@ var src = fs.readFileSync(require.resolve('traverse'), 'utf8')
test('fff', function (t) {
t.plan(4);
+
var fry = fritter({
- t : t,
+ t : { same : t.same.bind(t) },
module : { exports : {} },
+ console : console
});
fry.include(src);
@@ -24,6 +26,7 @@ test('fff', function (t) {
});
try {
+//console.log(fry.source);
vm.runInNewContext(fry.source, fry.context);
}
catch (err) {

0 comments on commit 444b437

Please sign in to comment.
Something went wrong with that request. Please try again.