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
|
The `call()` function allows invoking functions with a modified `this` context
and global environment. It's main use case is binding global variables for
dynamiclly loaded code at runtime.
Returns `null` if the given function value is not callable.
Returns the value returned by the invoked function in all other cases.
Test modifying `this` context
-- Testcase --
{%
let o1 = {
name: "Object #1",
func: function() {
print(`This is ${this.name}\n`);
}
};
let o2 = {
name: "Object #2"
};
o1.func();
call(o1.func, o2);
%}
-- End --
-- Expect stdout --
This is Object #1
This is Object #2
-- End --
Test modifying environment
-- Testcase --
{%
function fn() {
print("Hello world\n");
}
fn();
call(fn, null, { print: (s) => printf("Overridden print(): %s", s) });
%}
-- End --
-- Expect stdout --
Hello world
Overridden print(): Hello world
-- End --
Test isolating environment
-- Testcase --
{%
function fn() {
print("Hello world\n");
}
fn();
call(fn, null, proto({}, {})); // should fail due to unavailable print
%}
-- End --
-- Expect stdout --
Hello world
-- End --
-- Expect stderr --
Type error: left-hand side is not a function
In fn(), line 3, byte 24:
called from function call ([C])
called from anonymous function ([stdin]:7:30)
` print("Hello world\n");`
Near here -------------------^
-- End --
Test passing through arguments
-- Testcase --
{%
function fn(a, b) {
printf("The product of %d * %d is %d\n", a, b, a * b);
}
fn(3, 4);
call(fn, null, null, 5, 6);
call((...args) => printf("Args: %J\n", args), null, null, 1, 2, 3, 4, 5, 6);
%}
-- End --
-- Expect stdout --
The product of 3 * 4 is 12
The product of 5 * 6 is 30
Args: [ 1, 2, 3, 4, 5, 6 ]
-- End --
|