Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
Rhino is an open-source implementation of JavaScript written entirely in Java. It is typically
embedded into Java applications to provide scripting to end users.
/* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.javascript;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
/**
* This class implements the Proxy object.
*
* @author Ronald Brill
*/
final class NativeProxy extends ScriptableObject implements Callable, Constructable {
private static final long serialVersionUID = 6676871870513494844L;
private static final String PROXY_TAG = "Proxy";
private static final String TRAP_GET_PROTOTYPE_OF = "getPrototypeOf";
private static final String TRAP_SET_PROTOTYPE_OF = "setPrototypeOf";
private static final String TRAP_IS_EXTENSIBLE = "isExtensible";
private static final String TRAP_PREVENT_EXTENSIONS = "preventExtensions";
private static final String TRAP_GET_OWN_PROPERTY_DESCRIPTOR = "getOwnPropertyDescriptor";
private static final String TRAP_DEFINE_PROPERTY = "defineProperty";
private static final String TRAP_HAS = "has";
private static final String TRAP_GET = "get";
private static final String TRAP_SET = "set";
private static final String TRAP_DELETE_PROPERTY = "deleteProperty";
private static final String TRAP_OWN_KEYS = "ownKeys";
private static final String TRAP_APPLY = "apply";
private static final String TRAP_CONSTRUCT = "construct";
private ScriptableObject targetObj;
private Scriptable handlerObj;
private final String typeOf;
private static final class Revoker implements Callable {
private NativeProxy revocableProxy = null;
public Revoker(NativeProxy proxy) {
revocableProxy = proxy;
}
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
if (revocableProxy != null) {
revocableProxy.handlerObj = null;
revocableProxy.targetObj = null;
revocableProxy = null;
}
return Undefined.instance;
}
}
public static void init(Context cx, Scriptable scope, boolean sealed) {
LambdaConstructor constructor =
new LambdaConstructor(
scope,
PROXY_TAG,
2,
LambdaConstructor.CONSTRUCTOR_NEW,
NativeProxy::constructor) {
@Override
public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
NativeProxy obj =
(NativeProxy) getTargetConstructor().construct(cx, scope, args);
// avoid getting trapped
obj.setPrototypeDirect(getClassPrototype());
obj.setParentScope(scope);
return obj;
}
};
constructor.setPrototypeProperty(null);
constructor.defineConstructorMethod(
scope, "revocable", 2, NativeProxy::revocable, DONTENUM, DONTENUM | READONLY);
ScriptableObject.defineProperty(scope, PROXY_TAG, constructor, DONTENUM);
if (sealed) {
constructor.sealObject();
}
}
private NativeProxy(ScriptableObject target, Scriptable handler) {
this.targetObj = target;
this.handlerObj = handler;
if (target == null || !(target instanceof Callable)) {
typeOf = super.getTypeOf();
} else {
typeOf = target.getTypeOf();
}
}
@Override
public String getClassName() {
ScriptableObject target = getTargetThrowIfRevoked();
return target.getClassName();
}
/**
* see
* https://262.ecma-international.org/12.0/#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget
*/
@Override
public Scriptable construct(Context cx, Scriptable scope, Object[] args) {
/*
* 1. Let handler be O.[[ProxyHandler]].
* 2. If handler is null, throw a TypeError exception.
* 3. Assert: Type(handler) is Object.
* 4. Let target be O.[[ProxyTarget]].
* 5. Assert: IsConstructor(target) is true.
* 6. Let trap be ? GetMethod(handler, "construct").
* 7. If trap is undefined, then
* a. Return ? Construct(target, argumentsList, newTarget).
* 8. Let argArray be ! CreateArrayFromList(argumentsList).
* 9. Let newObj be ? Call(trap, handler, « target, argArray, newTarget »).
* 10. If Type(newObj) is not Object, throw a TypeError exception.
* 11. Return newObj.
*/
ScriptableObject target = getTargetThrowIfRevoked();
Callable trap = getTrap(TRAP_CONSTRUCT);
if (trap != null) {
Object result = callTrap(trap, new Object[] {target, args, this});
if (!(result instanceof Scriptable) || ScriptRuntime.isSymbol(result)) {
throw ScriptRuntime.typeError("Constructor trap has to return a scriptable.");
}
return (ScriptableObject) result;
}
return ((Constructable) target).construct(cx, scope, args);
}
/**
* https://262.ecma-international.org/12.0/#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p
*/
@Override
public boolean has(String name, Scriptable start) {
/*
* 1. Assert: IsPropertyKey(P) is true.
* 2. Let handler be O.[[ProxyHandler]].
* 3. If handler is null, throw a TypeError exception.
* 4. Assert: Type(handler) is Object.
* 5. Let target be O.[[ProxyTarget]].
* 6. Let trap be ? GetMethod(handler, "has").
* 7. If trap is undefined, then
* a. Return ? target.[[HasProperty]](P).
* 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P »)).
* 9. If booleanTrapResult is false, then
* a. Let targetDesc be ? target.[[GetOwnProperty]](P).
* b. If targetDesc is not undefined, then
* i. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
* ii. Let extensibleTarget be ? IsExtensible(target).
* iii. If extensibleTarget is false, throw a TypeError exception.
* 10. Return booleanTrapResult.
*/
ScriptableObject target = getTargetThrowIfRevoked();
Callable trap = getTrap(TRAP_HAS);
if (trap != null) {
boolean booleanTrapResult =
ScriptRuntime.toBoolean(callTrap(trap, new Object[] {target, name}));
if (!booleanTrapResult) {
ScriptableObject targetDesc =
target.getOwnPropertyDescriptor(Context.getContext(), name);
if (targetDesc != null) {
if (Boolean.FALSE.equals(targetDesc.get("configurable"))
|| !target.isExtensible()) {
throw ScriptRuntime.typeError(
"proxy can't report an existing own property '"
+ name
+ "' as non-existent on a non-extensible object");
}
}
}
return booleanTrapResult;
}
if (start == this) {
start = target;
}
return target.has(name, start);
}
/**
* see
* https://262.ecma-international.org/12.0/#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p
*/
@Override
public boolean has(int index, Scriptable start) {
/*
* 1. Assert: IsPropertyKey(P) is true.
* 2. Let handler be O.[[ProxyHandler]].
* 3. If handler is null, throw a TypeError exception.
* 4. Assert: Type(handler) is Object.
* 5. Let target be O.[[ProxyTarget]].
* 6. Let trap be ? GetMethod(handler, "has").
* 7. If trap is undefined, then
* a. Return ? target.[[HasProperty]](P).
* 8. Let booleanTrapResult be ! ToBoolean(? Call(trap, handler, « target, P »)).
* 9. If booleanTrapResult is false, then
* a. Let targetDesc be ? target.[[GetOwnProperty]](P).
* b. If targetDesc is not undefined, then
* i. If targetDesc.[[Configurable]] is false, throw a TypeError exception.
* ii. Let extensibleTarget be ? IsExtensible(target).
* iii. If extensibleTarget is false, throw a TypeError exception.
* 10. Return booleanTrapResult.
*/
ScriptableObject target = getTargetThrowIfRevoked();
Callable trap = getTrap(TRAP_HAS);
if (trap != null) {
boolean booleanTrapResult =
ScriptRuntime.toBoolean(
callTrap(trap, new Object[] {target, ScriptRuntime.toString(index)}));
if (!booleanTrapResult) {
ScriptableObject targetDesc =
target.getOwnPropertyDescriptor(Context.getContext(), index);
if (targetDesc != null) {
if (Boolean.FALSE.equals(targetDesc.get("configurable"))
|| !target.isExtensible()) {
throw ScriptRuntime.typeError(
"proxy can't check an existing property ' + name + ' existance on an not configurable or not extensible object");
}
}
}
return booleanTrapResult;
}
if (start == this) {
start = target;
}
return target.has(index, start);
}
/**
* see
* https://262.ecma-international.org/12.0/#sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p
*/
@Override
public boolean has(Symbol key, Scriptable start) {
ScriptableObject target = getTargetThrowIfRevoked();
Callable trap = getTrap(TRAP_HAS);
if (trap != null) {
boolean booleanTrapResult =
ScriptRuntime.toBoolean(callTrap(trap, new Object[] {target, key}));
if (!booleanTrapResult) {
ScriptableObject targetDesc =
target.getOwnPropertyDescriptor(Context.getContext(), key);
if (targetDesc != null) {
if (Boolean.FALSE.equals(targetDesc.get("configurable"))
|| !target.isExtensible()) {
throw ScriptRuntime.typeError(
"proxy can't check an existing property ' + name + ' existance on an not configurable or not extensible object");
}
}
}
return booleanTrapResult;
}
if (start == this) {
start = target;
}
SymbolScriptable symbolScriptableTarget = ensureSymbolScriptable(target);
return symbolScriptableTarget.has(key, start);
}
/**
* see
* https://262.ecma-international.org/12.0/#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys
*/
@Override
Object[] getIds(boolean getNonEnumerable, boolean getSymbols) {
/*
* 1. Let handler be O.[[ProxyHandler]].
* 2. If handler is null, throw a TypeError exception.
* 3. Assert: Type(handler) is Object.
* 4. Let target be O.[[ProxyTarget]].
* 5. Let trap be ? GetMethod(handler, "ownKeys").
* 6. If trap is undefined, then
* a. Return ? target.[[OwnPropertyKeys]]().
* 7. Let trapResultArray be ? Call(trap, handler, « target »).
* 8. Let trapResult be ? CreateListFromArrayLike(trapResultArray, « String, Symbol »).
* 9. If trapResult contains any duplicate entries, throw a TypeError exception.
* 10. Let extensibleTarget be ? IsExtensible(target).
* 11. Let targetKeys be ? target.[[OwnPropertyKeys]]().
* 12. Assert: targetKeys is a List whose elements are only String and Symbol values.
* 13. Assert: targetKeys contains no duplicate entries.
* 14. Let targetConfigurableKeys be a new empty List.
* 15. Let targetNonconfigurableKeys be a new empty List.
* 16. For each element key of targetKeys, do
* a. Let desc be ? target.[[GetOwnProperty]](key).
* b. If desc is not undefined and desc.[[Configurable]] is false, then
* i. i. Append key as an element of targetNonconfigurableKeys.
* c. Else,
i. i. Append key as an element of targetConfigurableKeys.
* 17. If extensibleTarget is true and targetNonconfigurableKeys is empty, then
* a. Return trapResult.
* 18. Let uncheckedResultKeys be a List whose elements are the elements of trapResult.
* 19. For each element key of targetNonconfigurableKeys, do
* a. a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
* b. Remove key from uncheckedResultKeys.
* 20. If extensibleTarget is true, return trapResult.
* 21. For each element key of targetConfigurableKeys, do
* a. a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
* b. Remove key from uncheckedResultKeys.
* 22. If uncheckedResultKeys is not empty, throw a TypeError exception.
* 23. Return trapResult.
*/
ScriptableObject target = getTargetThrowIfRevoked();
Callable trap = getTrap(TRAP_OWN_KEYS);
if (trap != null) {
Object res = callTrap(trap, new Object[] {target});
if (!(res instanceof Scriptable)) {
throw ScriptRuntime.typeError("ownKeys trap must be an object");
}
if (!ScriptRuntime.isArrayLike((Scriptable) res)) {
throw ScriptRuntime.typeError("ownKeys trap must be an array like object");
}
Context cx = Context.getContext();
List