Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_nashorn
提交
b63d95f4
D
dragonwell8_nashorn
项目概览
openanolis
/
dragonwell8_nashorn
通知
2
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_nashorn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b63d95f4
编写于
11月 05, 2013
作者:
S
sundar
浏览文件
操作
浏览文件
下载
差异文件
Merge
上级
777e0f5c
baa9fc17
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
180 addition
and
10 deletion
+180
-10
src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
+5
-5
src/jdk/nashorn/api/scripting/ScriptUtils.java
src/jdk/nashorn/api/scripting/ScriptUtils.java
+57
-0
src/jdk/nashorn/internal/runtime/linker/NashornLinker.java
src/jdk/nashorn/internal/runtime/linker/NashornLinker.java
+34
-5
test/script/basic/JDK-8027753.js
test/script/basic/JDK-8027753.js
+50
-0
test/script/basic/JDK-8027753.js.EXPECTED
test/script/basic/JDK-8027753.js.EXPECTED
+3
-0
test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java
test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java
+12
-0
test/src/jdk/nashorn/api/scripting/Window.java
test/src/jdk/nashorn/api/scripting/Window.java
+19
-0
未找到文件。
src/jdk/nashorn/api/scripting/ScriptObjectMirror.java
浏览文件 @
b63d95f4
...
@@ -601,9 +601,9 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
...
@@ -601,9 +601,9 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
* @param homeGlobal global to which this object belongs. Not used for ConsStrings.
* @param homeGlobal global to which this object belongs. Not used for ConsStrings.
* @return wrapped/converted object
* @return wrapped/converted object
*/
*/
public
static
Object
wrap
(
final
Object
obj
,
final
Script
Object
homeGlobal
)
{
public
static
Object
wrap
(
final
Object
obj
,
final
Object
homeGlobal
)
{
if
(
obj
instanceof
ScriptObject
)
{
if
(
obj
instanceof
ScriptObject
)
{
return
homeGlobal
!=
null
?
new
ScriptObjectMirror
((
ScriptObject
)
obj
,
homeGlobal
)
:
obj
;
return
homeGlobal
instanceof
ScriptObject
?
new
ScriptObjectMirror
((
ScriptObject
)
obj
,
(
ScriptObject
)
homeGlobal
)
:
obj
;
}
}
if
(
obj
instanceof
ConsString
)
{
if
(
obj
instanceof
ConsString
)
{
return
obj
.
toString
();
return
obj
.
toString
();
...
@@ -618,7 +618,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
...
@@ -618,7 +618,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
* @param homeGlobal global to which this object belongs
* @param homeGlobal global to which this object belongs
* @return unwrapped object
* @return unwrapped object
*/
*/
public
static
Object
unwrap
(
final
Object
obj
,
final
Script
Object
homeGlobal
)
{
public
static
Object
unwrap
(
final
Object
obj
,
final
Object
homeGlobal
)
{
if
(
obj
instanceof
ScriptObjectMirror
)
{
if
(
obj
instanceof
ScriptObjectMirror
)
{
final
ScriptObjectMirror
mirror
=
(
ScriptObjectMirror
)
obj
;
final
ScriptObjectMirror
mirror
=
(
ScriptObjectMirror
)
obj
;
return
(
mirror
.
global
==
homeGlobal
)?
mirror
.
sobj
:
obj
;
return
(
mirror
.
global
==
homeGlobal
)?
mirror
.
sobj
:
obj
;
...
@@ -634,7 +634,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
...
@@ -634,7 +634,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
* @param homeGlobal global to which this object belongs
* @param homeGlobal global to which this object belongs
* @return wrapped array
* @return wrapped array
*/
*/
public
static
Object
[]
wrapArray
(
final
Object
[]
args
,
final
Script
Object
homeGlobal
)
{
public
static
Object
[]
wrapArray
(
final
Object
[]
args
,
final
Object
homeGlobal
)
{
if
(
args
==
null
||
args
.
length
==
0
)
{
if
(
args
==
null
||
args
.
length
==
0
)
{
return
args
;
return
args
;
}
}
...
@@ -655,7 +655,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
...
@@ -655,7 +655,7 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin
* @param homeGlobal global to which this object belongs
* @param homeGlobal global to which this object belongs
* @return unwrapped array
* @return unwrapped array
*/
*/
public
static
Object
[]
unwrapArray
(
final
Object
[]
args
,
final
Script
Object
homeGlobal
)
{
public
static
Object
[]
unwrapArray
(
final
Object
[]
args
,
final
Object
homeGlobal
)
{
if
(
args
==
null
||
args
.
length
==
0
)
{
if
(
args
==
null
||
args
.
length
==
0
)
{
return
args
;
return
args
;
}
}
...
...
src/jdk/nashorn/api/scripting/ScriptUtils.java
浏览文件 @
b63d95f4
...
@@ -25,7 +25,9 @@
...
@@ -25,7 +25,9 @@
package
jdk.nashorn.api.scripting
;
package
jdk.nashorn.api.scripting
;
import
jdk.nashorn.internal.runtime.Context
;
import
jdk.nashorn.internal.runtime.ScriptFunction
;
import
jdk.nashorn.internal.runtime.ScriptFunction
;
import
jdk.nashorn.internal.runtime.ScriptObject
;
import
jdk.nashorn.internal.runtime.ScriptRuntime
;
import
jdk.nashorn.internal.runtime.ScriptRuntime
;
/**
/**
...
@@ -71,4 +73,59 @@ public final class ScriptUtils {
...
@@ -71,4 +73,59 @@ public final class ScriptUtils {
return
func
.
makeSynchronizedFunction
(
sync
);
return
func
.
makeSynchronizedFunction
(
sync
);
}
}
/**
* Make a script object mirror on given object if needed.
*
* @param obj object to be wrapped
* @return wrapped object
*/
public
static
Object
wrap
(
final
Object
obj
)
{
if
(
obj
instanceof
ScriptObject
)
{
return
ScriptObjectMirror
.
wrap
(
obj
,
Context
.
getGlobal
());
}
return
obj
;
}
/**
* Unwrap a script object mirror if needed.
*
* @param obj object to be unwrapped
* @return unwrapped object
*/
public
static
Object
unwrap
(
final
Object
obj
)
{
if
(
obj
instanceof
ScriptObjectMirror
)
{
return
ScriptObjectMirror
.
unwrap
(
obj
,
Context
.
getGlobal
());
}
return
obj
;
}
/**
* Wrap an array of object to script object mirrors if needed.
*
* @param args array to be unwrapped
* @return wrapped array
*/
public
static
Object
[]
wrapArray
(
final
Object
[]
args
)
{
if
(
args
==
null
||
args
.
length
==
0
)
{
return
args
;
}
return
ScriptObjectMirror
.
wrapArray
(
args
,
Context
.
getGlobal
());
}
/**
* Unwrap an array of script object mirrors if needed.
*
* @param args array to be unwrapped
* @return unwrapped array
*/
public
static
Object
[]
unwrapArray
(
final
Object
[]
args
)
{
if
(
args
==
null
||
args
.
length
==
0
)
{
return
args
;
}
return
ScriptObjectMirror
.
unwrapArray
(
args
,
Context
.
getGlobal
());
}
}
}
src/jdk/nashorn/internal/runtime/linker/NashornLinker.java
浏览文件 @
b63d95f4
...
@@ -32,6 +32,8 @@ import java.lang.invoke.MethodHandles;
...
@@ -32,6 +32,8 @@ import java.lang.invoke.MethodHandles;
import
java.lang.reflect.Modifier
;
import
java.lang.reflect.Modifier
;
import
java.util.Deque
;
import
java.util.Deque
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
javax.script.Bindings
;
import
jdk.internal.dynalink.CallSiteDescriptor
;
import
jdk.internal.dynalink.CallSiteDescriptor
;
import
jdk.internal.dynalink.linker.ConversionComparator
;
import
jdk.internal.dynalink.linker.ConversionComparator
;
import
jdk.internal.dynalink.linker.GuardedInvocation
;
import
jdk.internal.dynalink.linker.GuardedInvocation
;
...
@@ -40,7 +42,11 @@ import jdk.internal.dynalink.linker.LinkRequest;
...
@@ -40,7 +42,11 @@ import jdk.internal.dynalink.linker.LinkRequest;
import
jdk.internal.dynalink.linker.LinkerServices
;
import
jdk.internal.dynalink.linker.LinkerServices
;
import
jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker
;
import
jdk.internal.dynalink.linker.TypeBasedGuardingDynamicLinker
;
import
jdk.internal.dynalink.support.Guards
;
import
jdk.internal.dynalink.support.Guards
;
import
jdk.nashorn.api.scripting.JSObject
;
import
jdk.nashorn.api.scripting.ScriptObjectMirror
;
import
jdk.nashorn.api.scripting.ScriptUtils
;
import
jdk.nashorn.internal.objects.NativeArray
;
import
jdk.nashorn.internal.objects.NativeArray
;
import
jdk.nashorn.internal.runtime.Context
;
import
jdk.nashorn.internal.runtime.JSType
;
import
jdk.nashorn.internal.runtime.JSType
;
import
jdk.nashorn.internal.runtime.ScriptFunction
;
import
jdk.nashorn.internal.runtime.ScriptFunction
;
import
jdk.nashorn.internal.runtime.ScriptObject
;
import
jdk.nashorn.internal.runtime.ScriptObject
;
...
@@ -115,9 +121,14 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
...
@@ -115,9 +121,14 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
return
new
GuardedInvocation
(
mh
,
canLinkTypeStatic
(
sourceType
)
?
null
:
IS_NASHORN_OR_UNDEFINED_TYPE
);
return
new
GuardedInvocation
(
mh
,
canLinkTypeStatic
(
sourceType
)
?
null
:
IS_NASHORN_OR_UNDEFINED_TYPE
);
}
}
GuardedInvocation
inv
=
getArrayConverter
(
sourceType
,
targetType
);
final
GuardedInvocation
arrayConverter
=
getArrayConverter
(
sourceType
,
targetType
);
if
(
inv
!=
null
)
{
if
(
arrayConverter
!=
null
)
{
return
inv
;
return
arrayConverter
;
}
final
GuardedInvocation
mirrorConverter
=
getMirrorConverter
(
sourceType
,
targetType
);
if
(
mirrorConverter
!=
null
)
{
return
mirrorConverter
;
}
}
return
getSamTypeConverter
(
sourceType
,
targetType
);
return
getSamTypeConverter
(
sourceType
,
targetType
);
...
@@ -181,6 +192,18 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
...
@@ -181,6 +192,18 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
return
MH
.
asType
(
converter
,
converter
.
type
().
changeReturnType
(
type
));
return
MH
.
asType
(
converter
,
converter
.
type
().
changeReturnType
(
type
));
}
}
private
static
GuardedInvocation
getMirrorConverter
(
Class
<?>
sourceType
,
Class
<?>
targetType
)
{
// Could've also used (targetType.isAssignableFrom(ScriptObjectMirror.class) && targetType != Object.class) but
// it's probably better to explicitly spell out the supported target types
if
(
targetType
==
Map
.
class
||
targetType
==
Bindings
.
class
||
targetType
==
JSObject
.
class
||
targetType
==
ScriptObjectMirror
.
class
)
{
if
(
ScriptObject
.
class
.
isAssignableFrom
(
sourceType
))
{
return
new
GuardedInvocation
(
CREATE_MIRROR
,
null
);
}
return
new
GuardedInvocation
(
CREATE_MIRROR
,
IS_SCRIPT_OBJECT
);
}
return
null
;
}
private
static
boolean
isAutoConvertibleFromFunction
(
final
Class
<?>
clazz
)
{
private
static
boolean
isAutoConvertibleFromFunction
(
final
Class
<?>
clazz
)
{
return
isAbstractClass
(
clazz
)
&&
!
ScriptObject
.
class
.
isAssignableFrom
(
clazz
)
&&
return
isAbstractClass
(
clazz
)
&&
!
ScriptObject
.
class
.
isAssignableFrom
(
clazz
)
&&
JavaAdapterFactory
.
isAutoConvertibleFromFunction
(
clazz
);
JavaAdapterFactory
.
isAutoConvertibleFromFunction
(
clazz
);
...
@@ -235,17 +258,23 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
...
@@ -235,17 +258,23 @@ final class NashornLinker implements TypeBasedGuardingDynamicLinker, GuardingTyp
return
clazz
==
List
.
class
||
clazz
==
Deque
.
class
;
return
clazz
==
List
.
class
||
clazz
==
Deque
.
class
;
}
}
private
static
final
MethodHandle
IS_SCRIPT_OBJECT
=
Guards
.
isInstance
(
ScriptObject
.
class
,
MH
.
type
(
Boolean
.
TYPE
,
Object
.
class
));
private
static
final
MethodHandle
IS_SCRIPT_FUNCTION
=
Guards
.
isInstance
(
ScriptFunction
.
class
,
MH
.
type
(
Boolean
.
TYPE
,
Object
.
class
));
private
static
final
MethodHandle
IS_SCRIPT_FUNCTION
=
Guards
.
isInstance
(
ScriptFunction
.
class
,
MH
.
type
(
Boolean
.
TYPE
,
Object
.
class
));
private
static
final
MethodHandle
IS_NATIVE_ARRAY
=
Guards
.
isOfClass
(
NativeArray
.
class
,
MH
.
type
(
Boolean
.
TYPE
,
Object
.
class
));
private
static
final
MethodHandle
IS_NATIVE_ARRAY
=
Guards
.
isOfClass
(
NativeArray
.
class
,
MH
.
type
(
Boolean
.
TYPE
,
Object
.
class
));
private
static
final
MethodHandle
IS_NASHORN_OR_UNDEFINED_TYPE
=
findOwnMH
(
"isNashornTypeOrUndefined"
,
private
static
final
MethodHandle
IS_NASHORN_OR_UNDEFINED_TYPE
=
findOwnMH
(
"isNashornTypeOrUndefined"
,
Boolean
.
TYPE
,
Object
.
class
);
Boolean
.
TYPE
,
Object
.
class
);
private
static
final
MethodHandle
CREATE_MIRROR
=
findOwnMH
(
"createMirror"
,
Object
.
class
,
Object
.
class
);
@SuppressWarnings
(
"unused"
)
@SuppressWarnings
(
"unused"
)
private
static
boolean
isNashornTypeOrUndefined
(
final
Object
obj
)
{
private
static
boolean
isNashornTypeOrUndefined
(
final
Object
obj
)
{
return
obj
instanceof
ScriptObject
||
obj
instanceof
Undefined
;
return
obj
instanceof
ScriptObject
||
obj
instanceof
Undefined
;
}
}
@SuppressWarnings
(
"unused"
)
private
static
Object
createMirror
(
final
Object
obj
)
{
return
ScriptUtils
.
wrap
(
obj
);
}
private
static
MethodHandle
findOwnMH
(
final
String
name
,
final
Class
<?>
rtype
,
final
Class
<?>...
types
)
{
private
static
MethodHandle
findOwnMH
(
final
String
name
,
final
Class
<?>
rtype
,
final
Class
<?>...
types
)
{
return
MH
.
findStatic
(
MethodHandles
.
lookup
(),
NashornLinker
.
class
,
name
,
MH
.
type
(
rtype
,
types
));
return
MH
.
findStatic
(
MethodHandles
.
lookup
(),
NashornLinker
.
class
,
name
,
MH
.
type
(
rtype
,
types
));
}
}
...
...
test/script/basic/JDK-8027753.js
0 → 100644
浏览文件 @
b63d95f4
/*
* 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-8027753: Support ScriptObject to JSObject, ScriptObjectMirror, Map, Bindings auto-conversion as well as explicit wrap, unwrap
*
* @test
* @run
*/
var
ScriptUtils
=
Java
.
type
(
"
jdk.nashorn.api.scripting.ScriptUtils
"
);
var
ScriptObjectMirror
=
Java
.
type
(
"
jdk.nashorn.api.scripting.ScriptObjectMirror
"
);
var
obj
=
{
foo
:
34
,
bar
:
'
hello
'
};
var
wrapped
=
ScriptUtils
.
wrap
(
obj
);
if
(
!
(
wrapped
instanceof
ScriptObjectMirror
))
{
fail
(
"
ScriptUtils.wrap does not return a ScriptObjectMirror
"
);
}
print
(
"
wrapped.foo =
"
+
wrapped
.
foo
);
print
(
"
wrapped.bar =
"
+
wrapped
.
bar
);
var
unwrapped
=
ScriptUtils
.
unwrap
(
wrapped
);
if
(
!
(
unwrapped
instanceof
Object
))
{
fail
(
"
ScriptUtils.unwrap does not return a ScriptObject
"
);
}
// same object unwrapped?
print
(
unwrapped
===
obj
);
test/script/basic/JDK-8027753.js.EXPECTED
0 → 100644
浏览文件 @
b63d95f4
wrapped.foo = 34
wrapped.bar = hello
true
test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java
浏览文件 @
b63d95f4
...
@@ -523,6 +523,18 @@ public class ScriptEngineTest {
...
@@ -523,6 +523,18 @@ public class ScriptEngineTest {
assertEquals
(
sw
.
toString
(),
println
(
"34 true hello"
));
assertEquals
(
sw
.
toString
(),
println
(
"34 true hello"
));
}
}
@Test
public
void
scriptObjectAutoConversionTest
()
throws
ScriptException
{
final
ScriptEngineManager
m
=
new
ScriptEngineManager
();
final
ScriptEngine
e
=
m
.
getEngineByName
(
"nashorn"
);
e
.
eval
(
"obj = { foo: 'hello' }"
);
e
.
put
(
"Window"
,
e
.
eval
(
"Packages.jdk.nashorn.api.scripting.Window"
));
assertEquals
(
e
.
eval
(
"Window.funcJSObject(obj)"
),
"hello"
);
assertEquals
(
e
.
eval
(
"Window.funcScriptObjectMirror(obj)"
),
"hello"
);
assertEquals
(
e
.
eval
(
"Window.funcMap(obj)"
),
"hello"
);
assertEquals
(
e
.
eval
(
"Window.funcJSObject(obj)"
),
"hello"
);
}
private
static
final
String
LINE_SEPARATOR
=
System
.
getProperty
(
"line.separator"
);
private
static
final
String
LINE_SEPARATOR
=
System
.
getProperty
(
"line.separator"
);
// Returns String that would be the result of calling PrintWriter.println
// Returns String that would be the result of calling PrintWriter.println
...
...
test/src/jdk/nashorn/api/scripting/Window.java
浏览文件 @
b63d95f4
...
@@ -25,6 +25,9 @@
...
@@ -25,6 +25,9 @@
package
jdk.nashorn.api.scripting
;
package
jdk.nashorn.api.scripting
;
import
java.util.Map
;
import
javax.script.Bindings
;
public
class
Window
{
public
class
Window
{
private
String
location
=
"http://localhost:8080/window"
;
private
String
location
=
"http://localhost:8080/window"
;
...
@@ -63,4 +66,20 @@ public class Window {
...
@@ -63,4 +66,20 @@ public class Window {
System
.
out
.
println
(
"window.setTimeout: "
+
delay
+
", code: "
+
code
);
System
.
out
.
println
(
"window.setTimeout: "
+
delay
+
", code: "
+
code
);
return
0
;
return
0
;
}
}
public
static
Object
funcJSObject
(
final
JSObject
jsobj
)
{
return
jsobj
.
getMember
(
"foo"
);
}
public
static
Object
funcScriptObjectMirror
(
final
ScriptObjectMirror
sobj
)
{
return
sobj
.
get
(
"foo"
);
}
public
static
Object
funcMap
(
final
Map
<?,?>
map
)
{
return
map
.
get
(
"foo"
);
}
public
static
Object
funcBindings
(
final
Bindings
bindings
)
{
return
bindings
.
get
(
"foo"
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录