-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathscope.in
106 lines (86 loc) · 2.62 KB
/
scope.in
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
//
// All variables are global - with the single exception that you will
// will get a LOCAL scope solely for function-arguments.
//
// We'll compare three variables here:
//
// x is a global, which becomes scoped inside the function body.
// y is also global.
// z which is created inside a function, and accessible outside it.
// test is created inside the function, locally.
//
// The key observations with this script are:
//
// 1. When `x` is changed it will be used as-is, but when the
// function-call is over the value will be lost
//
// 2. The variable `z` is NIL until it is defined inside the
// function-body, after which it can be used freely.
//
// You can run this via the `evalfilter` command like so:
//
// $ evalfilter run scope.script
//
// Once you do so you'll see the output, and the return-code displayed:
//
// $ evalfilter run scope.script
// Start: x => 1, y => 2, z is not defined (<nil>)
// Inside the function we have a local-variable: test=>33
// Inside the function the local-variable can be updated: test=>66
// At function end: x => 10, y => 100, z => 1000, test => 66
// End: x => 1, y => 100, z is defined (1000)
// The local variable did not leak, and is null as expected: <nil>
// Script gave result type:INTEGER value:1 - which is 'true'.
//
//
// We setup the default values for x & y.
//
x = 1;
y = 2;
//
// Confirm they work as expected.
//
printf("Start: x => %d, y => %d, z is not defined (%v)\n", x, y, z );
//
// Call the function.
//
tmp(x);
//
// Now one value is updated, and another is created.
//
printf("End: x => %d, y => %d, z is defined (%d)\n", x, y, z );
//
// Confirm the `local` variable didn't leak outside the function-body
//
if ( test ) {
printf("FATAL-ERROR: The local variable shouldn't have leaked\n");
}
else {
printf("The local variable did not leak, and is null as expected: %v\n", test);
}
//
// Scripting time is over now.
//
return 1;
//
// function tmp() shows how scoping works
//
function tmp(x) {
// change the local-variable
x = 10;
// change the global variable
y = 100;
// Created a variable inside the function?
// That'll be visible afterward.
z = 1000;
// This is local-only variable and will not leak outside
// The scope of this function.
local test;
test = 33;
// Confirm that works
printf(" Inside the function we have a local-variable: test=>%d\n", test);
test = test * 2;
printf(" Inside the function the local-variable can be updated: test=>%d\n", test);
// Show the result
printf(" At function end: x => %d, y => %d, z => %d, test => %d\n", x, y, z, test );
}