2010-07-25 05:23:37 +00:00
|
|
|
x = 1
|
|
|
|
y = {}
|
|
|
|
y.x = -> 3
|
2010-01-14 01:59:57 +00:00
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok x is 1
|
|
|
|
ok typeof(y.x) is 'function'
|
|
|
|
ok y.x instanceof Function
|
|
|
|
ok y.x() is 3
|
2010-01-14 01:59:57 +00:00
|
|
|
|
|
|
|
|
2010-01-16 20:02:04 +00:00
|
|
|
# The empty function should not cause a syntax error.
|
2010-01-26 15:52:05 +00:00
|
|
|
->
|
2010-02-15 23:03:00 +00:00
|
|
|
() ->
|
2010-01-16 20:02:04 +00:00
|
|
|
|
|
|
|
|
2010-03-02 05:43:01 +00: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 05:23:37 +00:00
|
|
|
obj = {
|
2010-07-25 07:15:12 +00:00
|
|
|
name: 'Fred'
|
2010-01-14 01:59:57 +00:00
|
|
|
|
2010-01-26 15:52:05 +00:00
|
|
|
bound: ->
|
2010-07-25 07:15:12 +00:00
|
|
|
(=> ok(this.name is 'Fred'))()
|
2010-01-14 01:59:57 +00:00
|
|
|
|
2010-01-26 15:52:05 +00:00
|
|
|
unbound: ->
|
2010-02-17 00:45:25 +00:00
|
|
|
(-> ok(!this.name?))()
|
2010-07-25 07:15:12 +00:00
|
|
|
|
|
|
|
nested: ->
|
|
|
|
(=>
|
|
|
|
(=>
|
|
|
|
(=>
|
|
|
|
ok this.name is 'Fred'
|
|
|
|
)()
|
|
|
|
)()
|
|
|
|
)()
|
2010-01-14 01:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obj.unbound()
|
2010-01-16 16:24:10 +00:00
|
|
|
obj.bound()
|
2010-07-25 07:15:12 +00:00
|
|
|
obj.nested()
|
2010-01-16 20:44:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Python decorator style wrapper that memoizes any function
|
2010-07-25 05:23:37 +00:00
|
|
|
memoize = (fn) ->
|
|
|
|
cache = {}
|
|
|
|
self = this
|
2010-01-26 15:52:05 +00:00
|
|
|
(args...) ->
|
2010-07-25 05:23:37 +00:00
|
|
|
key = args.toString()
|
2010-01-16 20:44:07 +00:00
|
|
|
return cache[key] if cache[key]
|
|
|
|
cache[key] = fn.apply(self, args)
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
Math = {
|
2010-01-26 15:52:05 +00:00
|
|
|
Add: (a, b) -> a + b
|
|
|
|
AnonymousAdd: ((a, b) -> a + b)
|
|
|
|
FastAdd: memoize (a, b) -> a + b
|
2010-01-16 20:44:07 +00:00
|
|
|
}
|
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok Math.Add(5, 5) is 10
|
|
|
|
ok Math.AnonymousAdd(10, 10) is 20
|
|
|
|
ok Math.FastAdd(20, 20) is 40
|
2010-01-25 04:56:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Parens are optional on simple function calls.
|
2010-02-17 00:45:25 +00:00
|
|
|
ok 100 > 1 if 1 > 0
|
|
|
|
ok true unless false
|
|
|
|
ok true for i in [1..3]
|
2010-01-26 07:27:19 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
okFunc = (f) -> ok(f())
|
2010-06-12 23:05:13 +00:00
|
|
|
okFunc -> true
|
2010-01-26 19:49:33 +00:00
|
|
|
|
|
|
|
# Optional parens can be used in a nested fashion.
|
2010-07-25 05:23:37 +00:00
|
|
|
call = (func) -> func()
|
2010-01-26 19:49:33 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
result = call ->
|
|
|
|
inner = call ->
|
2010-01-26 19:49:33 +00:00
|
|
|
Math.Add(5, 5)
|
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok result is 10
|
2010-01-27 02:15:56 +00:00
|
|
|
|
|
|
|
|
2010-03-10 02:54:44 +00:00
|
|
|
# More fun with optional parens.
|
2010-07-25 05:23:37 +00:00
|
|
|
fn = (arg) -> arg
|
2010-03-10 02:54:44 +00:00
|
|
|
|
|
|
|
ok fn(fn {prop: 101}).prop is 101
|
|
|
|
|
2010-04-13 13:05:55 +00:00
|
|
|
# Function calls sans-spacing.
|
2010-05-05 03:44:54 +00:00
|
|
|
ok((fn (x) ->
|
2010-04-13 13:05:55 +00:00
|
|
|
3
|
|
|
|
)() is 3)
|
|
|
|
|
2010-03-10 02:54:44 +00:00
|
|
|
|
2010-03-10 14:28:00 +00:00
|
|
|
# Multi-blocks with optional parens.
|
2010-07-25 05:23:37 +00:00
|
|
|
result = fn( ->
|
2010-03-10 14:28:00 +00:00
|
|
|
fn ->
|
|
|
|
"Wrapped"
|
|
|
|
)
|
|
|
|
|
|
|
|
ok result()() is 'Wrapped'
|
|
|
|
|
|
|
|
|
2010-01-27 02:15:56 +00:00
|
|
|
# And even with strange things like this:
|
2010-07-25 05:23:37 +00:00
|
|
|
funcs = [((x) -> x), ((x) -> x * x)]
|
|
|
|
result = funcs[1] 5
|
2010-01-27 02:15:56 +00:00
|
|
|
|
2010-02-17 00:45:25 +00:00
|
|
|
ok result is 25
|
2010-01-27 02:15:56 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
result = ("hello".slice) 3
|
2010-01-27 02:15:56 +00:00
|
|
|
|
2010-02-27 16:03:43 +00:00
|
|
|
ok result is 'lo'
|
|
|
|
|
|
|
|
|
|
|
|
# And with multiple single-line functions on the same line.
|
2010-07-25 05:23:37 +00:00
|
|
|
func = (x) -> (x) -> (x) -> x
|
2010-02-27 16:03:43 +00:00
|
|
|
ok func(1)(2)(3) is 3
|
2010-03-14 21:48:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Ensure that functions with the same name don't clash with helper functions.
|
2010-07-25 05:23:37 +00:00
|
|
|
del = -> 5
|
2010-03-14 21:48:43 +00:00
|
|
|
ok del() is 5
|
2010-03-26 15:44:25 +00:00
|
|
|
|
|
|
|
# Ensure that functions can have a trailing comma in their argument list
|
2010-07-25 05:23:37 +00:00
|
|
|
mult = (x, mids..., y) ->
|
|
|
|
x *= n for n in mids
|
|
|
|
x *= y
|
2010-03-26 15:44:25 +00:00
|
|
|
|
|
|
|
ok mult(1, 2,) is 2
|
|
|
|
ok mult(1, 2, 3,) is 6
|
2010-03-28 20:44:41 +00:00
|
|
|
ok mult(10,[1..6]...,) is 7200
|
|
|
|
|
|
|
|
|
|
|
|
# Test for inline functions with parentheses and implicit calls.
|
2010-07-25 05:23:37 +00:00
|
|
|
combine = (func, num) -> func() * num
|
|
|
|
result = combine (-> 1 + 2), 3
|
2010-03-28 20:44:41 +00:00
|
|
|
|
|
|
|
ok result is 9
|
2010-04-02 03:38:20 +00:00
|
|
|
|
2010-04-03 13:58:45 +00:00
|
|
|
|
2010-04-02 03:38:20 +00:00
|
|
|
# Test for calls/parens/multiline-chains.
|
2010-07-25 05:23:37 +00:00
|
|
|
f = (x) -> x
|
|
|
|
result = (f 1).toString()
|
2010-04-02 03:38:20 +00:00
|
|
|
.length
|
|
|
|
|
|
|
|
ok result is 1
|
2010-04-10 22:56:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Test implicit calls in functions in parens:
|
2010-07-25 05:23:37 +00:00
|
|
|
result = ((val) ->
|
2010-04-10 22:56:46 +00:00
|
|
|
[].push val
|
|
|
|
val
|
|
|
|
)(10)
|
|
|
|
|
|
|
|
ok result is 10
|
2010-04-20 05:32:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
# More paren compilation tests:
|
2010-07-25 05:23:37 +00:00
|
|
|
reverse = (obj) -> obj.reverse()
|
2010-04-20 05:32:12 +00:00
|
|
|
ok reverse([1, 2].concat 3).join(' ') is '3 2 1'
|
2010-05-01 03:20:22 +00:00
|
|
|
|
|
|
|
# Passing multiple functions without paren-wrapping is legal, and should compile.
|
2010-07-25 05:23:37 +00:00
|
|
|
sum = (one, two) -> one() + two()
|
|
|
|
result = sum ->
|
2010-05-01 03:20:22 +00:00
|
|
|
7 + 9
|
|
|
|
, ->
|
|
|
|
1 + 3
|
2010-06-16 11:24:17 +00:00
|
|
|
|
2010-05-01 03:20:22 +00:00
|
|
|
ok result is 20
|
2010-06-11 12:29:16 +00:00
|
|
|
|
2010-07-10 18:52:54 +00:00
|
|
|
|
|
|
|
# Implicit call with a trailing if statement as a param.
|
2010-07-25 05:23:37 +00:00
|
|
|
func = -> arguments[1]
|
|
|
|
result = func 'one', if false then 100 else 13
|
2010-07-10 18:52:54 +00:00
|
|
|
ok result is 13
|
|
|
|
|
|
|
|
|
2010-06-11 12:29:16 +00:00
|
|
|
# Test more function passing:
|
2010-07-25 05:23:37 +00:00
|
|
|
result = sum( ->
|
2010-06-11 12:29:16 +00:00
|
|
|
1 + 2
|
|
|
|
, ->
|
|
|
|
2 + 1
|
|
|
|
)
|
|
|
|
ok result is 6
|
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
sum = (a, b) -> a + b
|
|
|
|
result = sum(1
|
2010-06-11 12:29:16 +00:00
|
|
|
, 2)
|
2010-06-16 11:24:17 +00:00
|
|
|
|
2010-06-11 12:29:16 +00:00
|
|
|
ok result is 3
|
2010-06-16 11:24:17 +00:00
|
|
|
|
|
|
|
|
2010-07-14 13:40:07 +00:00
|
|
|
# This is a crazy one.
|
2010-07-25 05:23:37 +00:00
|
|
|
x = (obj, func) -> func obj
|
|
|
|
ident = (x) -> x
|
2010-07-14 13:40:07 +00:00
|
|
|
|
2010-07-25 05:23:37 +00:00
|
|
|
result = x {one: ident 1}, (obj) ->
|
|
|
|
inner = ident(obj)
|
2010-07-14 13:40:07 +00:00
|
|
|
ident inner
|
|
|
|
|
|
|
|
ok result.one is 1
|
|
|
|
|
|
|
|
|
2010-06-16 11:24:17 +00:00
|
|
|
# Assignment to a Object.prototype-named variable should not leak to outer scope.
|
|
|
|
(->
|
2010-07-25 05:23:37 +00:00
|
|
|
constructor = 'word'
|
2010-06-16 11:24:17 +00:00
|
|
|
)()
|
|
|
|
|
|
|
|
ok constructor isnt 'word'
|