Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell11
提交
aad488ad
D
dragonwell11
项目概览
openanolis
/
dragonwell11
通知
7
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell11
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
aad488ad
编写于
7月 09, 2013
作者:
S
sundar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8014785: Ability to extend global instance by binding properties of another object
Reviewed-by: attila, hannesw, jlaskey, lagergren
上级
c6b069d0
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
224 addition
and
10 deletion
+224
-10
nashorn/src/jdk/nashorn/internal/objects/NativeObject.java
nashorn/src/jdk/nashorn/internal/objects/NativeObject.java
+116
-0
nashorn/src/jdk/nashorn/internal/runtime/AccessorProperty.java
...rn/src/jdk/nashorn/internal/runtime/AccessorProperty.java
+2
-2
nashorn/src/jdk/nashorn/internal/runtime/Context.java
nashorn/src/jdk/nashorn/internal/runtime/Context.java
+3
-5
nashorn/src/jdk/nashorn/internal/runtime/PropertyMap.java
nashorn/src/jdk/nashorn/internal/runtime/PropertyMap.java
+1
-1
nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java
nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java
+31
-1
nashorn/src/jdk/nashorn/internal/runtime/linker/InvokeByName.java
...src/jdk/nashorn/internal/runtime/linker/InvokeByName.java
+1
-1
nashorn/test/script/basic/JDK-8014785.js
nashorn/test/script/basic/JDK-8014785.js
+62
-0
nashorn/test/script/basic/JDK-8014785.js.EXPECTED
nashorn/test/script/basic/JDK-8014785.js.EXPECTED
+8
-0
未找到文件。
nashorn/src/jdk/nashorn/internal/objects/NativeObject.java
浏览文件 @
aad488ad
...
...
@@ -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
;
}
}
nashorn/src/jdk/nashorn/internal/runtime/AccessorProperty.java
浏览文件 @
aad488ad
...
...
@@ -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
Script
Object
delegate
)
{
public
AccessorProperty
(
final
AccessorProperty
property
,
final
Object
delegate
)
{
super
(
property
);
this
.
primitiveGetter
=
bindTo
(
property
.
primitiveGetter
,
delegate
);
...
...
nashorn/src/jdk/nashorn/internal/runtime/Context.java
浏览文件 @
aad488ad
...
...
@@ -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
;
...
...
nashorn/src/jdk/nashorn/internal/runtime/PropertyMap.java
浏览文件 @
aad488ad
...
...
@@ -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
Script
Object
bindTo
)
{
PropertyMap
addPropertyBind
(
final
AccessorProperty
property
,
final
Object
bindTo
)
{
return
addProperty
(
new
AccessorProperty
(
property
,
bindTo
));
}
...
...
nashorn/src/jdk/nashorn/internal/runtime/ScriptObject.java
浏览文件 @
aad488ad
...
...
@@ -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).
...
...
nashorn/src/jdk/nashorn/internal/runtime/linker/InvokeByName.java
浏览文件 @
aad488ad
...
...
@@ -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|get
It
em:"
+
name
,
Object
.
class
,
targetClass
);
getter
=
Bootstrap
.
createDynamicInvoker
(
"dyn:getMethod|getProp|get
El
em:"
+
name
,
Object
.
class
,
targetClass
);
final
Class
<?>[]
finalPtypes
;
final
int
plength
=
ptypes
.
length
;
...
...
nashorn/test/script/basic/JDK-8014785.js
0 → 100644
浏览文件 @
aad488ad
/*
* 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
nashorn/test/script/basic/JDK-8014785.js.EXPECTED
0 → 100644
浏览文件 @
aad488ad
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录