9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.runtime;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29
30 import java.lang.invoke.MethodHandle;
31 import java.lang.invoke.MethodHandles;
32 import java.lang.invoke.MethodType;
33 import java.lang.invoke.SwitchPoint;
34 import jdk.internal.dynalink.CallSiteDescriptor;
35 import jdk.internal.dynalink.linker.GuardedInvocation;
36 import jdk.internal.dynalink.linker.LinkRequest;
37 import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
38 import jdk.nashorn.api.scripting.AbstractJSObject;
39 import jdk.nashorn.api.scripting.ScriptObjectMirror;
40 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
41 import jdk.nashorn.internal.runtime.linker.NashornGuards;
42
43 /**
44 * This class supports the handling of scope in a with body.
45 *
46 */
47 public final class WithObject extends Scope {
48 private static final MethodHandle WITHEXPRESSIONGUARD = findOwnMH("withExpressionGuard", boolean.class, Object.class, PropertyMap.class, SwitchPoint.class);
192 *
193 * @param key Property key.
194 * @param deep Whether the search should look up proto chain.
195 * @param start the object on which the lookup was originally initiated
196 *
197 * @return FindPropertyData or null if not found.
198 */
199 @Override
200 protected FindProperty findProperty(final String key, final boolean deep, final ScriptObject start) {
201 // We call findProperty on 'expression' with 'expression' itself as start parameter.
202 // This way in ScriptObject.setObject we can tell the property is from a 'with' expression
203 // (as opposed from another non-scope object in the proto chain such as Object.prototype).
204 final FindProperty exprProperty = expression.findProperty(key, true, expression);
205 if (exprProperty != null) {
206 return exprProperty;
207 }
208 return super.findProperty(key, deep, start);
209 }
210
211 @Override
212 protected Object invokeNoSuchProperty(final String name, final int programPoint) {
213 FindProperty find = expression.findProperty(NO_SUCH_PROPERTY_NAME, true);
214 if (find != null) {
215 final Object func = find.getObjectValue();
216 if (func instanceof ScriptFunction) {
217 return ScriptRuntime.apply((ScriptFunction)func, expression, name);
218 }
219 }
220
221 return getProto().invokeNoSuchProperty(name, programPoint);
222 }
223
224 @Override
225 public void setSplitState(final int state) {
226 ((Scope) getNonWithParent()).setSplitState(state);
227 }
228
229 @Override
230 public int getSplitState() {
231 return ((Scope) getNonWithParent()).getSplitState();
232 }
233
234 @Override
235 public void addBoundProperties(final ScriptObject source, final Property[] properties) {
236 // Declared variables in nested eval go to first normal (non-with) parent scope.
237 getNonWithParent().addBoundProperties(source, properties);
238 }
239
240 /**
241 * Get first parent scope that is not an instance of WithObject.
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.runtime;
27
28 import static jdk.nashorn.internal.lookup.Lookup.MH;
29 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
30
31 import java.lang.invoke.MethodHandle;
32 import java.lang.invoke.MethodHandles;
33 import java.lang.invoke.MethodType;
34 import java.lang.invoke.SwitchPoint;
35 import jdk.internal.dynalink.CallSiteDescriptor;
36 import jdk.internal.dynalink.linker.GuardedInvocation;
37 import jdk.internal.dynalink.linker.LinkRequest;
38 import jdk.internal.dynalink.support.CallSiteDescriptorFactory;
39 import jdk.nashorn.api.scripting.AbstractJSObject;
40 import jdk.nashorn.api.scripting.ScriptObjectMirror;
41 import jdk.nashorn.internal.runtime.linker.NashornCallSiteDescriptor;
42 import jdk.nashorn.internal.runtime.linker.NashornGuards;
43
44 /**
45 * This class supports the handling of scope in a with body.
46 *
47 */
48 public final class WithObject extends Scope {
49 private static final MethodHandle WITHEXPRESSIONGUARD = findOwnMH("withExpressionGuard", boolean.class, Object.class, PropertyMap.class, SwitchPoint.class);
193 *
194 * @param key Property key.
195 * @param deep Whether the search should look up proto chain.
196 * @param start the object on which the lookup was originally initiated
197 *
198 * @return FindPropertyData or null if not found.
199 */
200 @Override
201 protected FindProperty findProperty(final String key, final boolean deep, final ScriptObject start) {
202 // We call findProperty on 'expression' with 'expression' itself as start parameter.
203 // This way in ScriptObject.setObject we can tell the property is from a 'with' expression
204 // (as opposed from another non-scope object in the proto chain such as Object.prototype).
205 final FindProperty exprProperty = expression.findProperty(key, true, expression);
206 if (exprProperty != null) {
207 return exprProperty;
208 }
209 return super.findProperty(key, deep, start);
210 }
211
212 @Override
213 protected Object invokeNoSuchProperty(final String name, final boolean isScope, final int programPoint) {
214 FindProperty find = expression.findProperty(NO_SUCH_PROPERTY_NAME, true);
215 if (find != null) {
216 final Object func = find.getObjectValue();
217 if (func instanceof ScriptFunction) {
218 final ScriptFunction sfunc = (ScriptFunction)func;
219 final Object self = isScope && sfunc.isStrict()? UNDEFINED : expression;
220 return ScriptRuntime.apply(sfunc, self, name);
221 }
222 }
223
224 return getProto().invokeNoSuchProperty(name, isScope, programPoint);
225 }
226
227 @Override
228 public void setSplitState(final int state) {
229 ((Scope) getNonWithParent()).setSplitState(state);
230 }
231
232 @Override
233 public int getSplitState() {
234 return ((Scope) getNonWithParent()).getSplitState();
235 }
236
237 @Override
238 public void addBoundProperties(final ScriptObject source, final Property[] properties) {
239 // Declared variables in nested eval go to first normal (non-with) parent scope.
240 getNonWithParent().addBoundProperties(source, properties);
241 }
242
243 /**
244 * Get first parent scope that is not an instance of WithObject.
|