summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/40_proto
blob: d7d0c2c3366e5e9c5942cf8419c668ed19c9e0b0 (plain)
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
The `proto()` function retrievs or sets the prototype of the given object
or resource value.

Throws an exception if given value does not support setting prototypes.

When invoked with one argument, returns the prototype of the given value
(if any).

When invoked with two arguments, returns the given value.

-- Testcase --
{%
	let fs = require("fs");

	// create a "class instance" by attaching a function dictionary to
	// a plain object.
	let obj = proto({}, {
		greeting: function(name) {
			printf("Hello, %s!\n", name);
		}
	});

	// accessing a property on `obj` will look up the prototype chain
	// if the object itself does not have it
	obj.greeting("World");

	printf("%.J\n", [
		// retrieve prototype of `fs.file` resource
		proto(fs.stdout),

		// retrieve prototype of `obj`
		proto(obj)
	]);
%}
-- End --

-- Expect stdout --
Hello, World!
[
	{
		"error": "function error(...) { [native code] }",
		"fileno": "function fileno(...) { [native code] }",
		"flush": "function flush(...) { [native code] }",
		"close": "function close(...) { [native code] }",
		"tell": "function tell(...) { [native code] }",
		"seek": "function seek(...) { [native code] }",
		"write": "function write(...) { [native code] }",
		"read": "function read(...) { [native code] }"
	},
	{
		"greeting": "function(name) { ... }"
	}
]
-- End --



Passing an invalid value throws an exception.

-- Testcase --
{%
	proto("inval", {});
%}
-- End --

-- Expect stderr --
Type error: Passed value is neither a prototype, resource or object
In line 2, byte 19:

 `    proto("inval", {});`
  Near here -----------^


-- End --

-- Testcase --
{%
	proto({}, "inval");
%}
-- End --

-- Expect stderr --
Type error: Passed value is neither a prototype, resource or object
In line 2, byte 19:

 `    proto({}, "inval");`
  Near here -----------^


-- End --