提交 aad488ad 编写于 作者: S sundar

8014785: Ability to extend global instance by binding properties of another object

Reviewed-by: attila, hannesw, jlaskey, lagergren
上级 c6b069d0
......@@ -27,18 +27,24 @@ package jdk.nashorn.internal.objects;
import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
import java.lang.invoke.MethodHandle;
import java.util.ArrayList;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.internal.objects.annotations.Attribute;
import jdk.nashorn.internal.objects.annotations.Constructor;
import jdk.nashorn.internal.objects.annotations.Function;
import jdk.nashorn.internal.objects.annotations.ScriptClass;
import jdk.nashorn.internal.objects.annotations.Where;
import jdk.nashorn.internal.runtime.AccessorProperty;
import jdk.nashorn.internal.runtime.ECMAException;
import jdk.nashorn.internal.runtime.JSType;
import jdk.nashorn.internal.runtime.Property;
import jdk.nashorn.internal.runtime.PropertyMap;
import jdk.nashorn.internal.runtime.ScriptFunction;
import jdk.nashorn.internal.runtime.ScriptObject;
import jdk.nashorn.internal.runtime.ScriptRuntime;
import jdk.nashorn.internal.runtime.linker.Bootstrap;
import jdk.nashorn.internal.runtime.linker.InvokeByName;
/**
......@@ -471,4 +477,114 @@ public final class NativeObject {
return false;
}
/**
* Nashorn extension: Object.bindProperties
*
* Binds the source object's properties to the target object. Binding
* properties allows two-way read/write for the properties of the source object.
*
* Example:
* <pre>
* var obj = { x: 34, y: 100 };
* var foo = {}
*
* // bind properties of "obj" to "foo" object
* Object.bindProperties(foo, obj);
*
* // now, we can access/write on 'foo' properties
* print(foo.x); // prints obj.x which is 34
*
* // update obj.x via foo.x
* foo.x = "hello";
* print(obj.x); // prints "hello" now
*
* obj.x = 42; // foo.x also becomes 42
* print(foo.x); // prints 42
* </pre>
* <p>
* The source object bound can be a ScriptObject or a ScriptOjectMirror.
* null or undefined source object results in TypeError being thrown.
* </p>
* Example:
* <pre>
* var obj = loadWithNewGlobal({
* name: "test",
* script: "obj = { x: 33, y: 'hello' }"
* });
*
* // bind 'obj's properties to global scope 'this'
* Object.bindProperties(this, obj);
* print(x); // prints 33
* print(y); // prints "hello"
* x = Math.PI; // changes obj.x to Math.PI
* print(obj.x); // prints Math.PI
* </pre>
*
* Limitations of property binding:
* <ul>
* <li> Only enumerable, immediate (not proto inherited) properties of the source object are bound.
* <li> If the target object already contains a property called "foo", the source's "foo" is skipped (not bound).
* <li> Properties added to the source object after binding to the target are not bound.
* <li> Property configuration changes on the source object (or on the target) is not propagated.
* <li> Delete of property on the target (or the source) is not propagated -
* only the property value is set to 'undefined' if the property happens to be a data property.
* </ul>
* <p>
* It is recommended that the bound properties be treated as non-configurable
* properties to avoid surprises.
* </p>
*
* @param self self reference
* @param target the target object to which the source object's properties are bound
* @param source the source object whose properties are bound to the target
* @return the target object after property binding
*/
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object bindProperties(final Object self, final Object target, final Object source) {
// target object has to be a ScriptObject
Global.checkObject(target);
// check null or undefined source object
Global.checkObjectCoercible(source);
final ScriptObject targetObj = (ScriptObject)target;
if (source instanceof ScriptObject) {
final ScriptObject sourceObj = (ScriptObject)source;
final Property[] properties = sourceObj.getMap().getProperties();
// filter non-enumerable properties
final ArrayList<Property> propList = new ArrayList<>();
for (Property prop : properties) {
if (prop.isEnumerable()) {
propList.add(prop);
}
}
if (! propList.isEmpty()) {
targetObj.addBoundProperties(sourceObj, propList.toArray(new Property[propList.size()]));
}
} else if (source instanceof ScriptObjectMirror) {
// get enumerable, immediate properties of mirror
final ScriptObjectMirror mirror = (ScriptObjectMirror)source;
final String[] keys = mirror.getOwnKeys(false);
if (keys.length == 0) {
// nothing to bind
return target;
}
// make accessor properties using dynamic invoker getters and setters
final AccessorProperty[] props = new AccessorProperty[keys.length];
for (int idx = 0; idx < keys.length; idx++) {
final String name = keys[idx];
final MethodHandle getter = Bootstrap.createDynamicInvoker("dyn:getMethod|getProp|getElem:" + name, Object.class, ScriptObjectMirror.class);
final MethodHandle setter = Bootstrap.createDynamicInvoker("dyn:setProp|setElem:" + name, Object.class, ScriptObjectMirror.class, Object.class);
props[idx] = (AccessorProperty.create(name, 0, getter, setter));
}
targetObj.addBoundProperties(source, props);
}
return target;
}
}
......@@ -147,9 +147,9 @@ public class AccessorProperty extends Property {
* and are thus rebound with that as receiver
*
* @param property accessor property to rebind
* @param delegate delegate script object to rebind receiver to
* @param delegate delegate object to rebind receiver to
*/
public AccessorProperty(final AccessorProperty property, final ScriptObject delegate) {
public AccessorProperty(final AccessorProperty property, final Object delegate) {
super(property);
this.primitiveGetter = bindTo(property.primitiveGetter, delegate);
......
......@@ -199,6 +199,7 @@ public final class Context {
private static final ClassLoader myLoader = Context.class.getClassLoader();
private static final StructureLoader sharedLoader;
private static final AccessControlContext NO_PERMISSIONS_CONTEXT;
static {
sharedLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() {
......@@ -207,6 +208,7 @@ public final class Context {
return new StructureLoader(myLoader, null);
}
});
NO_PERMISSIONS_CONTEXT = new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
}
/**
......@@ -564,7 +566,7 @@ public final class Context {
sm.checkPackageAccess(fullName.substring(0, index));
return null;
}
}, createNoPermissionsContext());
}, NO_PERMISSIONS_CONTEXT);
}
}
......@@ -707,10 +709,6 @@ public final class Context {
return (context != null) ? context : Context.getContextTrusted();
}
private static AccessControlContext createNoPermissionsContext() {
return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) });
}
private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
ScriptFunction script = null;
......
......@@ -260,7 +260,7 @@ public final class PropertyMap implements Iterable<Object>, PropertyListener {
*
* @return New {@link PropertyMap} with {@link Property} added.
*/
PropertyMap addPropertyBind(final AccessorProperty property, final ScriptObject bindTo) {
PropertyMap addPropertyBind(final AccessorProperty property, final Object bindTo) {
return addProperty(new AccessorProperty(property, bindTo));
}
......
......@@ -203,9 +203,19 @@ public abstract class ScriptObject extends PropertyListenerManager implements Pr
* @param source The source object to copy from.
*/
public void addBoundProperties(final ScriptObject source) {
addBoundProperties(source, source.getMap().getProperties());
}
/**
* Copy all properties from the array with their receiver bound to the source.
*
* @param source The source object to copy from.
* @param properties The array of properties to copy.
*/
public void addBoundProperties(final ScriptObject source, final Property[] properties) {
PropertyMap newMap = this.getMap();
for (final Property property : source.getMap().getProperties()) {
for (final Property property : properties) {
final String key = property.getKey();
if (newMap.findProperty(key) == null) {
......@@ -221,6 +231,26 @@ public abstract class ScriptObject extends PropertyListenerManager implements Pr
this.setMap(newMap);
}
/**
* Copy all properties from the array with their receiver bound to the source.
*
* @param source The source object to copy from.
* @param properties The collection of accessor properties to copy.
*/
public void addBoundProperties(final Object source, final AccessorProperty[] properties) {
PropertyMap newMap = this.getMap();
for (final AccessorProperty property : properties) {
final String key = property.getKey();
if (newMap.findProperty(key) == null) {
newMap = newMap.addPropertyBind(property, source);
}
}
this.setMap(newMap);
}
/**
* Bind the method handle to the specified receiver, while preserving its original type (it will just ignore the
* first argument in lieu of the bound argument).
......
......@@ -83,7 +83,7 @@ public class InvokeByName {
*/
public InvokeByName(final String name, final Class<?> targetClass, final Class<?> rtype, final Class<?>... ptypes) {
this.name = name;
getter = Bootstrap.createDynamicInvoker("dyn:getMethod|getProp|getItem:" + name, Object.class, targetClass);
getter = Bootstrap.createDynamicInvoker("dyn:getMethod|getProp|getElem:" + name, Object.class, targetClass);
final Class<?>[] finalPtypes;
final int plength = ptypes.length;
......
/*
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* JDK-8014785: Ability to extend global instance by binding properties of another object
*
* @test
* @run
*/
var obj = { x: 34, y: 100 };
var foo = {}
// bind properties of "obj" to "foo" obj
Object.bindProperties(foo, obj);
// now we can access/write on foo properties
print("foo.x = " + foo.x); // prints obj.x which is 34
// update obj.x via foo.x
foo.x = "hello";
print("obj.x = " + obj.x); // prints "hello" now
obj.x = 42; // foo.x also becomes 42
print("obj.x = " + obj.x); // prints 42
print("foo.x = " + foo.x); // prints 42
// now bind a mirror object to an object
var obj = loadWithNewGlobal({
name: "test",
script: "obj = { x: 33, y: 'hello' }"
});
Object.bindProperties(this, obj);
print("x = " + x); // prints 33
print("y = " + y); // prints "hello"
x = Math.PI; // changes obj.x to Math.PI
print("obj.x = " +obj.x); // prints Math.PI
obj.y = 32;
print("y = " + y); // should print 32
foo.x = 34
obj.x = hello
obj.x = 42
foo.x = 42
x = 33
y = hello
obj.x = 3.141592653589793
y = 32
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册