2010-07-25 01:23:37 -04:00
|
|
|
x = 1
|
|
|
|
y = {}
|
|
|
|
y.x = -> 3
|
2010-01-13 20:59:57 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok x is 1
|
|
|
|
ok typeof(y.x) is 'function'
|
|
|
|
ok y.x instanceof Function
|
|
|
|
ok y.x() is 3
|
2010-01-13 20:59:57 -05:00
|
|
|
|
|
|
|
|
2010-01-16 15:02:04 -05:00
|
|
|
# The empty function should not cause a syntax error.
|
2010-01-26 10:52:05 -05:00
|
|
|
->
|
2010-02-15 18:03:00 -05:00
|
|
|
() ->
|
2010-01-16 15:02:04 -05:00
|
|
|
|
|
|
|
|
2010-03-02 00:43:01 -05:00
|
|
|
# Multiple nested function declarations mixed with implicit calls should not
|
|
|
|
# cause a syntax error.
|
|
|
|
(one) -> (two) -> three four, (five) -> six seven, eight, (nine) ->
|
|
|
|
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
obj = {
|
2010-07-25 03:15:12 -04:00
|
|
|
name: 'Fred'
|
2010-01-13 20:59:57 -05:00
|
|
|
|
2010-01-26 10:52:05 -05:00
|
|
|
bound: ->
|
2010-10-11 13:04:51 -04:00
|
|
|
(=> eq this, obj)()
|
2010-01-13 20:59:57 -05:00
|
|
|
|
2010-01-26 10:52:05 -05:00
|
|
|
unbound: ->
|
2010-10-11 13:04:51 -04:00
|
|
|
(-> ok this isnt obj)()
|
2010-07-25 03:15:12 -04:00
|
|
|
|
|
|
|
nested: ->
|
|
|
|
(=>
|
|
|
|
(=>
|
|
|
|
(=>
|
2010-10-11 13:04:51 -04:00
|
|
|
eq this, obj
|
2010-07-25 03:15:12 -04:00
|
|
|
)()
|
|
|
|
)()
|
|
|
|
)()
|
2010-01-13 20:59:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
obj.unbound()
|
2010-01-16 11:24:10 -05:00
|
|
|
obj.bound()
|
2010-07-25 03:15:12 -04:00
|
|
|
obj.nested()
|
2010-01-16 15:44:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Python decorator style wrapper that memoizes any function
|
2010-07-25 01:23:37 -04:00
|
|
|
memoize = (fn) ->
|
|
|
|
cache = {}
|
|
|
|
self = this
|
2010-01-26 10:52:05 -05:00
|
|
|
(args...) ->
|
2010-07-25 01:23:37 -04:00
|
|
|
key = args.toString()
|
2010-01-16 15:44:07 -05:00
|
|
|
return cache[key] if cache[key]
|
|
|
|
cache[key] = fn.apply(self, args)
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
Math = {
|
2010-01-26 10:52:05 -05:00
|
|
|
Add: (a, b) -> a + b
|
2010-10-22 13:59:25 -04:00
|
|
|
AnonymousAdd: (a, b) -> a + b
|
2010-01-26 10:52:05 -05:00
|
|
|
FastAdd: memoize (a, b) -> a + b
|
2010-01-16 15:44:07 -05:00
|
|
|
}
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok Math.Add(5, 5) is 10
|
|
|
|
ok Math.AnonymousAdd(10, 10) is 20
|
|
|
|
ok Math.FastAdd(20, 20) is 40
|
2010-01-24 23:56:27 -05:00
|
|
|
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
okFunc = (f) -> ok(f())
|
2010-06-12 19:05:13 -04:00
|
|
|
okFunc -> true
|
2010-01-26 14:49:33 -05:00
|
|
|
|
|
|
|
# Optional parens can be used in a nested fashion.
|
2010-07-25 01:23:37 -04:00
|
|
|
call = (func) -> func()
|
2010-01-26 14:49:33 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
result = call ->
|
|
|
|
inner = call ->
|
2010-01-26 14:49:33 -05:00
|
|
|
Math.Add(5, 5)
|
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok result is 10
|
2010-01-26 21:15:56 -05:00
|
|
|
|
|
|
|
|
2010-03-09 21:54:44 -05:00
|
|
|
# More fun with optional parens.
|
2010-07-25 01:23:37 -04:00
|
|
|
fn = (arg) -> arg
|
2010-03-09 21:54:44 -05:00
|
|
|
|
|
|
|
ok fn(fn {prop: 101}).prop is 101
|
|
|
|
|
|
|
|
|
2010-03-10 09:28:00 -05:00
|
|
|
# Multi-blocks with optional parens.
|
2010-07-25 01:23:37 -04:00
|
|
|
result = fn( ->
|
2010-03-10 09:28:00 -05:00
|
|
|
fn ->
|
|
|
|
"Wrapped"
|
|
|
|
)
|
|
|
|
|
|
|
|
ok result()() is 'Wrapped'
|
|
|
|
|
|
|
|
|
2010-01-26 21:15:56 -05:00
|
|
|
# And even with strange things like this:
|
2010-07-25 01:23:37 -04:00
|
|
|
funcs = [((x) -> x), ((x) -> x * x)]
|
|
|
|
result = funcs[1] 5
|
2010-01-26 21:15:56 -05:00
|
|
|
|
2010-02-16 19:45:25 -05:00
|
|
|
ok result is 25
|
2010-01-26 21:15:56 -05:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
result = ("hello".slice) 3
|
2010-01-26 21:15:56 -05:00
|
|
|
|
2010-02-27 11:03:43 -05:00
|
|
|
ok result is 'lo'
|
|
|
|
|
|
|
|
|
|
|
|
# And with multiple single-line functions on the same line.
|
2010-07-25 01:23:37 -04:00
|
|
|
func = (x) -> (x) -> (x) -> x
|
2010-02-27 11:03:43 -05:00
|
|
|
ok func(1)(2)(3) is 3
|
2010-03-14 17:48:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Ensure that functions with the same name don't clash with helper functions.
|
2010-07-25 01:23:37 -04:00
|
|
|
del = -> 5
|
2010-03-14 17:48:43 -04:00
|
|
|
ok del() is 5
|
2010-03-26 11:44:25 -04:00
|
|
|
|
|
|
|
# Ensure that functions can have a trailing comma in their argument list
|
2010-07-25 01:23:37 -04:00
|
|
|
mult = (x, mids..., y) ->
|
2010-10-19 20:42:12 -04:00
|
|
|
(x *= n) for n in mids
|
2010-07-25 01:23:37 -04:00
|
|
|
x *= y
|
2010-03-26 11:44:25 -04:00
|
|
|
|
|
|
|
ok mult(1, 2,) is 2
|
|
|
|
ok mult(1, 2, 3,) is 6
|
2010-10-13 00:53:56 -04:00
|
|
|
ok mult(10, (i for i from 1 to 6)...) is 7200
|
2010-03-28 16:44:41 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Test for inline functions with parentheses and implicit calls.
|
2010-07-25 01:23:37 -04:00
|
|
|
combine = (func, num) -> func() * num
|
|
|
|
result = combine (-> 1 + 2), 3
|
2010-03-28 16:44:41 -04:00
|
|
|
|
|
|
|
ok result is 9
|
2010-04-01 23:38:20 -04:00
|
|
|
|
2010-04-03 09:58:45 -04:00
|
|
|
|
2010-04-01 23:38:20 -04:00
|
|
|
# Test for calls/parens/multiline-chains.
|
2010-07-25 01:23:37 -04:00
|
|
|
f = (x) -> x
|
|
|
|
result = (f 1).toString()
|
2010-04-01 23:38:20 -04:00
|
|
|
.length
|
|
|
|
|
|
|
|
ok result is 1
|
2010-04-10 18:56:46 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Test implicit calls in functions in parens:
|
2010-07-25 01:23:37 -04:00
|
|
|
result = ((val) ->
|
2010-04-10 18:56:46 -04:00
|
|
|
[].push val
|
|
|
|
val
|
|
|
|
)(10)
|
|
|
|
|
|
|
|
ok result is 10
|
2010-04-20 01:32:12 -04:00
|
|
|
|
|
|
|
|
|
|
|
# More paren compilation tests:
|
2010-07-25 01:23:37 -04:00
|
|
|
reverse = (obj) -> obj.reverse()
|
2010-04-20 01:32:12 -04:00
|
|
|
ok reverse([1, 2].concat 3).join(' ') is '3 2 1'
|
2010-04-30 23:20:22 -04:00
|
|
|
|
|
|
|
# Passing multiple functions without paren-wrapping is legal, and should compile.
|
2010-07-25 01:23:37 -04:00
|
|
|
sum = (one, two) -> one() + two()
|
|
|
|
result = sum ->
|
2010-04-30 23:20:22 -04:00
|
|
|
7 + 9
|
|
|
|
, ->
|
|
|
|
1 + 3
|
2010-06-16 07:24:17 -04:00
|
|
|
|
2010-04-30 23:20:22 -04:00
|
|
|
ok result is 20
|
2010-06-11 08:29:16 -04:00
|
|
|
|
2010-07-10 14:52:54 -04:00
|
|
|
|
|
|
|
# Implicit call with a trailing if statement as a param.
|
2010-07-25 01:23:37 -04:00
|
|
|
func = -> arguments[1]
|
|
|
|
result = func 'one', if false then 100 else 13
|
2010-07-10 14:52:54 -04:00
|
|
|
ok result is 13
|
|
|
|
|
|
|
|
|
2010-06-11 08:29:16 -04:00
|
|
|
# Test more function passing:
|
2010-07-25 01:23:37 -04:00
|
|
|
result = sum( ->
|
2010-06-11 08:29:16 -04:00
|
|
|
1 + 2
|
|
|
|
, ->
|
|
|
|
2 + 1
|
|
|
|
)
|
|
|
|
ok result is 6
|
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
sum = (a, b) -> a + b
|
|
|
|
result = sum(1
|
2010-06-11 08:29:16 -04:00
|
|
|
, 2)
|
2010-06-16 07:24:17 -04:00
|
|
|
|
2010-06-11 08:29:16 -04:00
|
|
|
ok result is 3
|
2010-06-16 07:24:17 -04:00
|
|
|
|
|
|
|
|
2010-07-14 09:40:07 -04:00
|
|
|
# This is a crazy one.
|
2010-07-25 01:23:37 -04:00
|
|
|
x = (obj, func) -> func obj
|
|
|
|
ident = (x) -> x
|
2010-07-14 09:40:07 -04:00
|
|
|
|
2010-07-25 01:23:37 -04:00
|
|
|
result = x {one: ident 1}, (obj) ->
|
|
|
|
inner = ident(obj)
|
2010-07-14 09:40:07 -04:00
|
|
|
ident inner
|
|
|
|
|
|
|
|
ok result.one is 1
|
|
|
|
|
|
|
|
|
2010-06-16 07:24:17 -04:00
|
|
|
# Assignment to a Object.prototype-named variable should not leak to outer scope.
|
2010-10-11 19:54:36 -04:00
|
|
|
# FIXME: fails on IE
|
2010-06-16 07:24:17 -04:00
|
|
|
(->
|
2010-07-25 01:23:37 -04:00
|
|
|
constructor = 'word'
|
2010-06-16 07:24:17 -04:00
|
|
|
)()
|
|
|
|
|
|
|
|
ok constructor isnt 'word'
|
2010-07-26 21:39:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Trying an implicit object call with a trailing function.
|
|
|
|
a = null
|
|
|
|
meth = (arg, obj, func) -> a = [obj.a, arg, func()].join ' '
|
|
|
|
|
|
|
|
meth 'apple', b: 1, a: 13, ->
|
|
|
|
'orange'
|
|
|
|
|
|
|
|
ok a is '13 apple orange'
|
2010-07-28 20:43:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Ensure that empty functions don't return mistaken values.
|
|
|
|
obj =
|
|
|
|
func: (@param, @rest...) ->
|
|
|
|
|
|
|
|
ok obj.func(101, 102, 103, 104) is undefined
|
|
|
|
ok obj.param is 101
|
|
|
|
ok obj.rest.join(' ') is '102 103 104'
|
2010-09-08 20:15:16 -04:00
|
|
|
|
|
|
|
|
|
|
|
# `@` and `this` should both be able to invoke a method.
|
|
|
|
func = (arg) -> ok arg is true
|
|
|
|
func.withAt = -> @ true
|
|
|
|
func.withThis = -> this true
|
|
|
|
|
|
|
|
func.withAt()
|
|
|
|
func.withThis()
|
2010-09-11 09:28:22 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Ensure that constructors invoked with splats return a new object.
|
|
|
|
args = [1, 2, 3]
|
|
|
|
Type = (@args) ->
|
2010-09-20 06:02:16 -04:00
|
|
|
type = new Type args
|
2010-09-11 09:28:22 -04:00
|
|
|
|
|
|
|
ok type and type instanceof Type
|
2010-09-20 06:02:16 -04:00
|
|
|
ok type.args and type.args instanceof Array
|
2010-09-11 09:28:22 -04:00
|
|
|
ok v is args[i] for v, i in type.args
|
2010-09-11 09:49:25 -04:00
|
|
|
|
2010-09-20 06:02:16 -04:00
|
|
|
Type1 = (@a, @b, @c) ->
|
|
|
|
type1 = new Type1 args...
|
|
|
|
|
2010-10-12 08:48:25 -04:00
|
|
|
ok type1 instanceof Type1
|
|
|
|
eq type1.constructor, Type1
|
2010-10-10 11:29:14 -04:00
|
|
|
ok type1.a is args[0] and type1.b is args[1] and type1.c is args[2]
|
2010-09-20 06:02:16 -04:00
|
|
|
|
2010-09-11 09:49:25 -04:00
|
|
|
|
|
|
|
# Ensure that constructors invoked with splats cache the function.
|
|
|
|
called = 0
|
|
|
|
get = -> if called++ then false else class Type
|
|
|
|
new get() args...
|
2010-09-25 05:13:37 -04:00
|
|
|
|
|
|
|
|
2010-10-11 19:54:36 -04:00
|
|
|
# Chained blocks, with proper indentation levels:
|
|
|
|
counter =
|
|
|
|
results: []
|
|
|
|
tick: (func) ->
|
|
|
|
@results.push func()
|
|
|
|
this
|
|
|
|
|
|
|
|
counter
|
|
|
|
.tick ->
|
|
|
|
3
|
|
|
|
.tick ->
|
|
|
|
2
|
|
|
|
.tick ->
|
|
|
|
1
|
|
|
|
|
|
|
|
eq counter.results.join(' '), '3 2 1'
|
|
|
|
|
|
|
|
|
|
|
|
# Make incorrect indentation safe.
|
|
|
|
func = ->
|
|
|
|
obj = {
|
|
|
|
key: 10
|
|
|
|
}
|
|
|
|
obj.key - 5
|
|
|
|
|
|
|
|
eq func(), 5
|
|
|
|
|
|
|
|
|
|
|
|
# Ensure that chained calls with indented implicit object literals below are
|
|
|
|
# alright.
|
|
|
|
result = null
|
|
|
|
obj =
|
|
|
|
method: (val) -> this
|
|
|
|
second: (hash) -> result = hash.three
|
|
|
|
|
|
|
|
|
|
|
|
obj
|
|
|
|
.method(
|
|
|
|
101
|
|
|
|
).second(
|
|
|
|
one:
|
|
|
|
two: 2
|
|
|
|
three: 3
|
|
|
|
)
|
|
|
|
|
|
|
|
eq result, 3
|
|
|
|
|
|
|
|
|
|
|
|
# Test newline-supressed call chains with nested functions.
|
|
|
|
obj =
|
|
|
|
call: -> this
|
|
|
|
func = ->
|
|
|
|
obj
|
|
|
|
.call ->
|
|
|
|
one two
|
|
|
|
.call ->
|
|
|
|
three four
|
|
|
|
101
|
|
|
|
|
|
|
|
eq func(), 101
|
|
|
|
|
|
|
|
|
2010-10-19 11:52:07 -04:00
|
|
|
# `new` shouldn't add extra parens
|
2010-09-25 05:13:37 -04:00
|
|
|
ok new Date().constructor is Date
|
|
|
|
|
2010-10-10 11:29:14 -04:00
|
|
|
|
2010-10-19 11:52:07 -04:00
|
|
|
# `new` works against bare function
|
2010-10-11 15:00:57 -04:00
|
|
|
eq Date, new ->
|
|
|
|
eq this, new => this
|
|
|
|
Date
|
2010-10-10 11:29:14 -04:00
|
|
|
|
|
|
|
|
2010-10-19 11:52:07 -04:00
|
|
|
# Implicit objects with number arguments.
|
2010-10-10 11:29:14 -04:00
|
|
|
func = (x, y) -> y
|
|
|
|
obj =
|
|
|
|
prop: func "a", 1
|
|
|
|
|
|
|
|
ok obj.prop is 1
|
2010-10-19 11:52:07 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Non-spaced unary and binary operators should cause a function call.
|
|
|
|
func = (val) -> val + 1
|
|
|
|
ok (func +5) is 6
|
|
|
|
ok (func -5) is -4
|
|
|
|
|
|
|
|
|
|
|
|
# Prefix unary assignment operators are allowed in parenless calls.
|
|
|
|
val = 5
|
|
|
|
ok (func --val) is 5
|
2010-10-22 13:59:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
eq ok, new ->
|
|
|
|
ok
|
|
|
|
### Should `return` implicitly ###
|
|
|
|
### even with trailing comments. ###
|
2010-10-24 07:02:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Rescoping with the `do` keyword.
|
|
|
|
v1 = 1
|
|
|
|
v2 = 2
|
|
|
|
do (v1) ->
|
|
|
|
v1 = 3
|
|
|
|
v2 = 4
|
|
|
|
ok v1 is 1
|
|
|
|
ok v2 is 4
|
2010-10-24 07:18:54 -04:00
|
|
|
|
|
|
|
cxt = {}
|
|
|
|
val = null
|
|
|
|
(-> do => val = this).call cxt
|
|
|
|
ok val is cxt
|