Permalink
Browse files

fix using custom apply() for functions without apply()

  • Loading branch information...
1 parent 4a9a7cc commit 25019f7dbc7c0aae20c0b7552e65a34c5a162889 @substack committed Sep 27, 2011
Showing with 46 additions and 9 deletions.
  1. +1 −0 example/web/index.html
  2. +3 −1 example/web/main.js
  3. +36 −5 index.js
  4. +5 −2 test/no_apply.js
  5. +1 −1 test/sources/no_apply.js
View
@@ -5,6 +5,7 @@
#output {
white-space: pre;
word-wrap: break-word;
+ font-family: monospace;
}
</style>
</head>
View
@@ -9,7 +9,9 @@ var src = [
].join('\n');
window.onload = function () {
- var stack = stackedy(src, { filename : 'stax.js', stoppable : false }).run();
+ var stack = stackedy(src, { filename : 'stax.js', stoppable : false })
+ .run({ global : window });
+
stack.once('error', function (err, c) {
//stack.stop();
write('Error: ' + json.stringify(err));
View
@@ -76,16 +76,31 @@ Stack.prototype.compile = function (context, opts) {
return function () {
stack.splice(0);
stack.push.apply(stack, stack_);
- return f.apply(this, arguments);
+ if (f.apply) {
+ return f.apply(this, arguments);
+ }
+ else {
+ return apply(f, this, arguments);
+ }
};
})(arg);
}
}
- var res = that
- ? that[fn].apply(that, args)
- : fn.apply(that, args)
- ;
+ var res;
+ if (that) {
+ res = that[fn].apply
+ ? that[fn].apply(that, args)
+ : apply(that[fn], that, args)
+ ;
+ }
+ else {
+ res = fn.apply
+ ? fn.apply(that, args)
+ : apply(fn, that, args)
+ ;
+ }
+
stack.shift();
return res;
};
@@ -297,3 +312,19 @@ Stack.prototype.run = function (context, opts) {
return self;
};
+
+function apply (fn, that, args) {
+ switch (args.length) {
+ case 0 : return fn()
+ case 1 : return fn(args[0])
+ case 2 : return fn(args[0], args[1])
+ case 3 : return fn(args[0], args[1], args[2])
+ default :
+ var sig = [];
+ for (var i = 0; i < args.length; i++) sig.push('args[' + i + ']');
+ return Function(
+ [ 'fn', 'args' ],
+ 'return fn(' + sig.join(',') + ')'
+ )(fn, args);
+ }
+}
View
@@ -2,7 +2,7 @@ var stackedy = require('../');
var test = require('tap').test;
var fs = require('fs');
-var src = fs.readFileSync(__dirname + '/sources/timeout.js', 'utf8');
+var src = fs.readFileSync(__dirname + '/sources/no_apply.js', 'utf8');
test('setTimeout has no .apply in the IEs', function (t) {
t.plan(1);
@@ -12,7 +12,10 @@ test('setTimeout has no .apply in the IEs', function (t) {
to.apply = null;
var context = {
- t : t,
+ done : function (n) {
+ t.equal(n, 5);
+ t.end();
+ },
setTimeout : to
};
View
@@ -1,3 +1,3 @@
setTimeout(function () {
- t.end();
+ done(5);
}, 50);

0 comments on commit 25019f7

Please sign in to comment.