This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSpecialRef.cs
More file actions
145 lines (120 loc) · 4.46 KB
/
Copy pathSpecialRef.cs
File metadata and controls
145 lines (120 loc) · 4.46 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
//------------------------------------------------------------------------------
// <license file="SpecialRef.cs">
//
// The use and distribution terms for this software are contained in the file
// named 'LICENSE', which can be found in the resources directory of this
// distribution.
//
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// </license>
//------------------------------------------------------------------------------
using System;
namespace EcmaScript.NET
{
class SpecialRef : IRef
{
public enum Types
{
None = 0,
Proto = 1,
Parent = 2
}
private IScriptable target;
private Types type;
private string name;
private SpecialRef (IScriptable target, Types type, string name)
{
this.target = target;
this.type = type;
this.name = name;
}
internal static IRef createSpecial (Context cx, object obj, string name)
{
IScriptable target = ScriptConvert.ToObjectOrNull (cx, obj);
if (target == null) {
throw ScriptRuntime.UndefReadError (obj, name);
}
Types type;
if (name.Equals ("__proto__")) {
type = Types.Proto;
}
else if (name.Equals ("__parent__")) {
type = Types.Parent;
}
else {
throw new ArgumentException (name);
}
if (!cx.HasFeature (Context.Features.ParentProtoProperties)) {
// Clear special after checking for valid name!
type = Types.None;
}
return new SpecialRef (target, type, name);
}
public object Get (Context cx)
{
switch (type) {
case Types.None:
return ScriptRuntime.getObjectProp (target, name, cx);
case Types.Proto:
return target.GetPrototype ();
case Types.Parent:
return target.ParentScope;
default:
throw Context.CodeBug ();
}
}
public object Set (Context cx, object value)
{
switch (type) {
case Types.None:
return ScriptRuntime.setObjectProp (target, name, value, cx);
case Types.Proto:
case Types.Parent: {
IScriptable obj = ScriptConvert.ToObjectOrNull (cx, value);
if (obj != null) {
// Check that obj does not contain on its prototype/scope
// chain to prevent cycles
IScriptable search = obj;
do {
if (search == target) {
throw Context.ReportRuntimeErrorById ("msg.cyclic.value", name);
}
if (type == Types.Proto) {
search = search.GetPrototype ();
}
else {
search = search.ParentScope;
}
}
while (search != null);
}
if (type == Types.Proto) {
target.SetPrototype (obj);
}
else {
target.ParentScope = obj;
}
return obj;
}
default:
throw Context.CodeBug ();
}
}
public bool Has (Context cx)
{
if (type == Types.None) {
return ScriptRuntime.hasObjectElem (target, name, cx);
}
return true;
}
public bool Delete (Context cx)
{
if (type == Types.None) {
return ScriptRuntime.deleteObjectElem (target, name, cx);
}
return false;
}
}
}