From cb2a7f0820db270b0585c821379feefbd80eb4c2 Mon Sep 17 00:00:00 2001 From: satyr Date: Tue, 12 Oct 2010 21:48:25 +0900 Subject: [PATCH] simplified splatting `new` compilation --- lib/nodes.js | 19 +++---------------- src/nodes.coffee | 19 ++++++------------- test/test_functions.coffee | 3 ++- 3 files changed, 11 insertions(+), 30 deletions(-) diff --git a/lib/nodes.js b/lib/nodes.js index 2513aff0..d76a19b9 100644 --- a/lib/nodes.js +++ b/lib/nodes.js @@ -582,7 +582,7 @@ return "" + (this.superReference(o)) + ".call(this" + (args.length ? ', ' : '') + args + ")"; }; Call.prototype.compileSplat = function(o) { - var _i, _len, _ref2, arg, argvar, base, call, ctor, fun, idt, name, ref, result, splatargs; + var base, fun, idt, name, ref, splatargs; splatargs = this.compileSplatArguments(o); if (this.isSuper) { return ("" + (this.superReference(o)) + ".apply(this, " + splatargs + ")"); @@ -602,21 +602,8 @@ } return ("" + fun + ".apply(" + ref + ", " + splatargs + ")"); } - call = 'call(this)'; - argvar = function(node) { - return node instanceof Literal && node.value === 'arguments'; - }; - for (_i = 0, _len = (_ref2 = this.args).length; _i < _len; _i++) { - arg = _ref2[_i]; - if (arg.contains(argvar)) { - call = 'apply(this, arguments)'; - break; - } - } - ctor = o.scope.freeVariable('ctor'); - ref = o.scope.freeVariable('ref'); - result = o.scope.freeVariable('result'); - return "(function() {\n" + (idt = this.idt(1)) + "var ctor = function() {};\n" + idt + (utility('extends')) + "(ctor, " + ctor + " = " + (this.variable.compile(o)) + ");\n" + idt + "return typeof (" + result + " = " + ctor + ".apply(" + ref + " = new ctor, " + splatargs + ")) === \"object\" ? " + result + " : " + ref + ";\n" + (this.tab) + "})." + call; + idt = this.idt(1); + return "(function(func, args, ctor) {\n" + idt + "ctor.prototype = func.prototype;\n" + idt + "var child = new ctor, result = func.apply(child, args);\n" + idt + "return typeof result === \"object\" ? result : child;\n" + (this.tab) + "})(" + (this.variable.compile(o)) + ", " + splatargs + ", function() {})"; }; return Call; })(); diff --git a/src/nodes.coffee b/src/nodes.coffee index 1ae77560..22be68fd 100644 --- a/src/nodes.coffee +++ b/src/nodes.coffee @@ -534,20 +534,13 @@ exports.Call = class Call extends Base fun = ref = base.compile o fun += name.compile o if name return "#{fun}.apply(#{ref}, #{splatargs})" - call = 'call(this)' - argvar = (node) -> node instanceof Literal and node.value is 'arguments' - for arg in @args when arg.contains argvar - call = 'apply(this, arguments)' - break - ctor = o.scope.freeVariable 'ctor' - ref = o.scope.freeVariable 'ref' - result = o.scope.freeVariable 'result' + idt = @idt 1 """ - (function() { - #{idt = @idt 1}var ctor = function() {}; - #{idt}#{utility 'extends'}(ctor, #{ctor} = #{ @variable.compile o }); - #{idt}return typeof (#{result} = #{ctor}.apply(#{ref} = new ctor, #{splatargs})) === "object" ? #{result} : #{ref}; - #{@tab}}).#{call} + (function(func, args, ctor) { + #{idt}ctor.prototype = func.prototype; + #{idt}var child = new ctor, result = func.apply(child, args); + #{idt}return typeof result === "object" ? result : child; + #{@tab}})(#{ @variable.compile o }, #{splatargs}, function() {}) """ #### Extends diff --git a/test/test_functions.coffee b/test/test_functions.coffee index 5873f2b1..7bd3dd17 100644 --- a/test/test_functions.coffee +++ b/test/test_functions.coffee @@ -244,7 +244,8 @@ ok v is args[i] for v, i in type.args Type1 = (@a, @b, @c) -> type1 = new Type1 args... -ok type1 and type1 instanceof Type1 +ok type1 instanceof Type1 +eq type1.constructor, Type1 ok type1.a is args[0] and type1.b is args[1] and type1.c is args[2]