From 3087daaece9aca87c2d95361e595bf19014fefca Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Sat, 19 Apr 2014 17:48:06 -0700 Subject: [PATCH] Add a closure test. --- src/wren_compiler.c | 8 ++++++++ test/closure/reuse_closure_slot.wren | 1 - test/closure/shadow_closure_with_local.wren | 11 +++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/closure/shadow_closure_with_local.wren diff --git a/src/wren_compiler.c b/src/wren_compiler.c index 50689703..0518e910 100644 --- a/src/wren_compiler.c +++ b/src/wren_compiler.c @@ -1726,6 +1726,14 @@ static void name(Compiler* compiler, bool allowAssignment) return; } + // TODO: The fact that we walk the entire scope chain up to global before + // interpreting a name as an implicit "this" call means that surrounding + // names shadow ones in the class. This is good for things like globals. + // (You wouldn't want `new Fiber` translating to `new this.Fiber`, but may + // not be what we want for other names.) One option is to make capitalized + // names *always* global, and then a lowercase name will become on an + // implicit this if it's not a local in the nearest enclosing class. + // Otherwise, if we are inside a class, it's a call with an implicit "this" // receiver. ClassCompiler* classCompiler = getEnclosingClass(compiler); diff --git a/test/closure/reuse_closure_slot.wren b/test/closure/reuse_closure_slot.wren index da32e266..77b9e015 100644 --- a/test/closure/reuse_closure_slot.wren +++ b/test/closure/reuse_closure_slot.wren @@ -15,4 +15,3 @@ } // TODO: Maximum number of closed-over variables (directly and/or indirect). -// TODO: Shadow variable used in closure. \ No newline at end of file diff --git a/test/closure/shadow_closure_with_local.wren b/test/closure/shadow_closure_with_local.wren new file mode 100644 index 00000000..d991ffaa --- /dev/null +++ b/test/closure/shadow_closure_with_local.wren @@ -0,0 +1,11 @@ +{ + var foo = "closure" + new Fn { + { + IO.print(foo) // expect: closure + var foo = "shadow" + IO.print(foo) // expect: shadow + } + IO.print(foo) // expect: closure + }.call +}