File: newscoping.bsh

package info (click to toggle)
bsh 2.0b4-15
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 4,212 kB
  • sloc: java: 23,430; xml: 4,500; sh: 139; makefile: 30
file content (133 lines) | stat: -rw-r--r-- 2,269 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/java bsh.Interpreter

source("TestHarness.bsh");

// everything in here is for testing the new scoping rules
if ( Interpreter.LOCALSCOPING )
{
	complete();
	return; // note: return from source() doesn't exit caller
}

/*
	New BeanShell 1.3 scoping rules:
*/

// method assignment sees global x
x=0;
incrementX() {
	x=x+1;
}
incrementX();
assert( x == 1 );


boolean goobaflag = false;
// new untyped assignment defaults to global
setFlag() {
	goobaflag=true;
	secret = 1;
}
setFlag();
assert( goobaflag == true );
assert( secret == void );

var p;
foo() {
	p=1;
	this.p=2; // explicit this reference is a local assignment
	foolocal=2;
	int x = 99;
	int y=1;
	super.y=2;
	return this;
}
x=1;
foo=foo();

g=1; // global
assert( foo.g == 1 ); // can see through closure
assert( p==1 );
assert( foo.p==2 ); // locally scoped assignment in foo is different
assert( x == 1 ); // still one, typed x in foo was local scope
assert( foo.x == 99 ); // typed x in foo is different
assert( y == 2 ); // super.y in foo reached global
assert( foo.y == 1 ); //

// check behavior of deeply nested and 'super'
ameth() {
	int x; // declared here
	int y1;
	int z1;
	bmeth() {
		cmeth() {
			x=42; // x assigned in ameth
			// This demonstrates 'super' recursive behavior
			super.y1=43; // should reach y1 in ameth
			super.super.z1=44;  // should reach z1 in ameth
		}
		return this;
	}
	return this;
}
x=1;
a=ameth();
a.bmeth().cmeth();
assert( x == 1 );
assert( a.x==42 );
assert( a.y1==43 );
assert( y1==void );
assert( a.z1==44 );
assert( z1==void );

// more explicit 'this' behavior
obj=object();
obj.pat=42;
assert( obj.pat == 42 );
assert( pat == void );

// blocks work
for(ii=0; ii<3; ii++)
{
	if ( ii == 0 ) flag();
	if ( ii == 1 ) flag();
	if ( ii == 2 ) flag();
	this.ix=1;
}
assert( flag() == 3 );
assert(ii==3);
assert(ix==1);

globThis=this;
{
	assert( this == globThis );
	blockThis = this;
	this.foo=5;
}
assert( foo == 5 );
assert( blockThis == this );

// local assignment is equivalent to declaration
bar() {
	this.q=1;
	q=2;
	return this;
}
bar=bar();
assert( q==void );
assert( bar.q==2 );

// 'var' type
var foo;
assert( foo == null );
var bar = 1;
assert( bar == 1 );

goo() {
	var gooi = 1 ;
	assert( gooi == 1 );
}
assert( gooi == void );

complete();