Debian Codegraph @

rhino/src/org/mozilla/javascript/SpecialRef.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 package org.mozilla.javascript;
8
9 class SpecialRef extends Ref
10 {
11 static final long serialVersionUID = -7521596632456797847L;
12
13 private static final int SPECIAL_NONE = 0;
14 private static final int SPECIAL_PROTO = 1;
15 private static final int SPECIAL_PARENT = 2;
16
17 private Scriptable target;
18 private int type;
19 private String name;
20
21 private SpecialRef(Scriptable target, int type, String name)
22 {
23 this.target = target;
24 this.type = type;
25 this.name = name;
26 }
27
28 static Ref createSpecial(Context cx, Scriptable scope, Object object,
29 String name)
30 {
31 Scriptable target = ScriptRuntime.toObjectOrNull(cx, object, scope);
32 if (target == null) {
33 throw ScriptRuntime.undefReadError(object, name);
34 }
35
36 int type;
37 if (name.equals("__proto__")) {
38 type = SPECIAL_PROTO;
39 } else if (name.equals("__parent__")) {
40 type = SPECIAL_PARENT;
41 } else {
42 throw new IllegalArgumentException(name);
43 }
44
45 if (!cx.hasFeature(Context.FEATURE_PARENT_PROTO_PROPERTIES)) {
46 // Clear special after checking for valid name!
47 type = SPECIAL_NONE;
48 }
49
50 return new SpecialRef(target, type, name);
51 }
52
53 @Override
54 public Object get(Context cx)
55 {
56 switch (type) {
57 case SPECIAL_NONE:
58 return ScriptRuntime.getObjectProp(target, name, cx);
59 case SPECIAL_PROTO:
60 return target.getPrototype();
61 case SPECIAL_PARENT:
62 return target.getParentScope();
63 default:
64 throw Kit.codeBug();
65 }
66 }
67
68 @Override
69 @Deprecated
70 public Object set(Context cx, Object value) {
71 throw new IllegalStateException();
72 }
73
74 @Override
75 public Object set(Context cx, Scriptable scope, Object value)
76 {
77 switch (type) {
78 case SPECIAL_NONE:
79 return ScriptRuntime.setObjectProp(target, name, value, cx);
80 case SPECIAL_PROTO:
81 case SPECIAL_PARENT:
82 {
83 Scriptable obj = ScriptRuntime.toObjectOrNull(cx, value, scope);
84 if (obj != null) {
85 // Check that obj does not contain on its prototype/scope
86 // chain to prevent cycles
87 Scriptable search = obj;
88 do {
89 if (search == target) {
90 throw Context.reportRuntimeError1(
91 "msg.cyclic.value", name);
92 }
93 if (type == SPECIAL_PROTO) {
94 search = search.getPrototype();
95 } else {
96 search = search.getParentScope();
97 }
98 } while (search != null);
99 }
100 if (type == SPECIAL_PROTO) {
101 target.setPrototype(obj);
102 } else {
103 target.setParentScope(obj);
104 }
105 return obj;
106 }
107 default:
108 throw Kit.codeBug();
109 }
110 }
111
112 @Override
113 public boolean has(Context cx)
114 {
115 if (type == SPECIAL_NONE) {
116 return ScriptRuntime.hasObjectElem(target, name, cx);
117 }
118 return true;
119 }
120
121 @Override
122 public boolean delete(Context cx)
123 {
124 if (type == SPECIAL_NONE) {
125 return ScriptRuntime.deleteObjectElem(target, name, cx);
126 }
127 return false;
128 }
129 }
130