Adventures in JSR-292 or How To Be A Duck Without Really...
Transcript of Adventures in JSR-292 or How To Be A Duck Without Really...
<Insert Picture Here>
Adventures in JSR-292 orHow To Be A Duck Without Really Trying
Jim LaskeyMulti-language LeadJava Language and Tools Group
2
The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions.The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
Agenda
• Introduction•Nashorn• JSR-292 Usage In Nashorn•Avoiding Polymorphism At CallSites•Q & A
3
INTRODUCKTION
4
The Importance Of Being Multilingual.
4GLAdaAlgol/SimulaAPLAssemblerBasic/VisualBasicBLISSC/C++/Objective-CC#COBOLEiffelFlexForthFortran/Fortran5FoxProHTML/CSS
HyperTalkJavaJavaScriptLabVIEWLisp/Dylan/SchemeLuaM (Mumps)MEL (Maya)Modula/OberonOccamPascal/ObjectPascalPerlPHPPilot/TudorPL/1Postscript
PrographProlog/MicroPrologPythonQuartzComposerRubySelfSeriusSmalltalkSnobolSQLSymplTorqueScriptWatfor/WatbolXMLYACC/Bison/Antler
5
NASHORN
6
NASHORN Perfect Storm
• JavaFX Script•HTML5/JavaScript•Oracle acquisition•Rhino• John Rose taunt
7
NASHORN Goals
•Make scripting accessible to Java developers– Thin API, low overhead, Java objects, collections, Java Beans
•Based on ECMAScript-262•Example of JSR-292 usage•Promote the JVM relevance as a multi language platform•Customizable to specialized needs
8
NASHORN Tentative Timetable
• Introduction at the JVM Language Summit in summer 2011•Open source is TBD•Release with JDK 8 – probably beta in the GA and fully supported in a later release
9
NASHORN Internals (Simplification)
• 10
var node = { parent: …, sibling: … };node.__proto__ = …;…node.left = …;…node.right = …;
value of siblingsibling
value of parentparent
__proto__mapspill
"sibling""parent"
Setter
indirect left getter"left"indirect right setter
sibling setter
Keyparent setter
sibling getterparent getterGetter
indirect left setter"right" indirect right getter
value of rightright
left value of left
__proto__
spill... ...
...
...
...map
Object (Static)PrototypeMapSpill (Dynamic)
NASHORN Objects Objects Everywhere
•Basic objects–arrays, date, functions, regexp, ...
•Globals–global dynamic object
•Prototypes–delegate
•Scopes–chained objects–not always necessary (eval, arguments, nested functions)
11
NASHORN JSR-292 Usage In Nashorn
12
JSR-292 Handling Java void Methods
if (mh.type().returnType() == void.class) {
mh = MethodHandles.filterReturnValue (mh, undefinedFilter);}
…
static final MethodHandle undefinedFilter = Linker.getMethodHandle
(NativeJavaObject.class, "undefinedFilter");
…
public static Object undefinedFilter() {return ScriptRuntime.UNDEFINED;
}
13
JSR-292 JavaScript Closures
function gen() { var list = []; for (var i = 0; i < 5; i++) { list.push(function() { return i; }); } i = "fish"; return list;}
var funcs = gen();
for (f in funcs) print(funcs[f]());
14
fishfishfishfishfish
JSR-292 JavaScript Closures
static Object gen(Object receiver, ScriptObject scope, …) {scope = new Scope(scope, scope$map, …);ScriptArray list = newScriptArray(0);
…
MethodHandlemh =linker.getMethod(scriptClass, “f$1”, f$1$mt);
mh = MethodHandles.insertArguments(mh, 1, scope);ScriptFunction function = new Function(mh);list.push(function);
…
return list;}
15
JSR-292 Receivers
var x = node.left;
• InvokeDynamic to “left” with a GET bootstrap• “left” could be a member of the object, or, a member of the object’s prototype
16
Binding Receivers
17
value of siblingsibling
value of parentparent
__proto__mapspill
"sibling""parent"
Setter
indirect left getter"left"indirect right setter
sibling setter
Keyparent setter
sibling getterparent getterGetter
indirect left setter"right" indirect right getter
value of rightright
left value of left
__proto__
spill... ...
...
...
...map
Object (Static)PrototypeMapSpill (Dynamic)
JSR-292 Receivers
• If found in the object’s map, bind the getter to the call site as-is•Otherwise, bind the getter’s receiver arg to the prototype and bind the resulting mh to the call site
FindResult find = object.findProperty(“left”);
MethodHandle getter = find.getProperty().getGetter();
if (find.getDepth() == 0) {getter = getter.bindTo(find.getPrototype());
getter = MethodHandles.dropArguments(getter, 0, callSite.type().parameterType(0));}
callSite.setMethod(getter, …);
18
JSR-292 Direct References in Dynamic Situations
• Global is a singleton- Getters/setters can be bound to the global- Can be bound to anything
• Map merging
19
a = 10; b = “bear”; c = true;eval(“d = -1; e = “elephant”; f = false;”);
truec
"bear"b
10a
__proto__mapspill falsef
"elephant"e
d -1
JSR-292 Direct References in Dynamic Situations
20
truec
"bear"b
10a
__proto__mapspill
Keya getter
Getter
b getter
"a"
"c"
Setter
"b"
a setter
c getter
b setter
c setter
falseg
"elephant"f
-1e
__proto__mapspill
Keyd getter
Getter
e getter
"d"
"f"
Setter
"e"
d setter
f getter
e setter
f setter
truec
"bear"b
10a
__proto__mapspill
"a"
c getter
bound e getter
a setter
"e"
bound f getter
bound d setter
bound f setter
a getter
"f"
"c"
Getter
b setter"b"
bound e setter
Setter
bound d getterc setter
Key
b getter
"d"
falseg
"elephant"f
-1e
__proto__mapspill
POLYMORPHISMAvoiding Polymorphism At CallSites
21
value of leftvalue of right
left
… …
right
value of rightvalue of left
right
… …
left
… …
value of leftleftt1
t2
t3
POLYMORPHISM The Problem
22
var node = { parent: …, sibling: … };node.__proto__ = …;…node.left = …;…node.right = …;…f(node.left);
…node.right = …;…node.left = …;
if (node isa t3)return t3.left
else if (node isa t2)return t2.left
else if (node isa t1)return t1.left
elselookup
OR
POLYMORPHISM By Map
•Guard by testing map– Immutable and interned to prevent proliferation
23
Key
"left"
…
"right"
Key
"right"
…
"left"
Key
"left"
…
Key
…
Key
"right"
…
"left" "right"
"right" "left"
POLYMORPHISM By Organization
•Guard by testing organization–Reduce the number of cases by sorting fields
24
Key
"left"
…
"right"
Key
"right"
…
"left"
Key
"left"
…
Key
…
Key
"right"
…
"left" "right"
"right" "left"
Field
"left"
…
"right"
Field
"left"
…
"right"
Field
"left"
…
Field
…
Field
"right"
…
"left" "right"
"right" "left"
POLYMORPHISM By Organization
•Guard by testing organization–Reduce the number of cases by sorting fields
25
Key
"left"
…
"right"
Key
"right"
…
"left"
Key
"left"
…
Key
…
Key
"right"
…
"left" "right"
"right" "left"
Field
"left"
…
"right"
Field
"left"
…
Field
…
Field
"right"
…
"left" "right"
"right" "left"
Field
"left"
…
"right"
Field
…
"left" "right"
POLYMORPHISM By Predicted Organization
•Assume history will repeat itself
26
Key
"left"
…
"right"
Key
"right"
…
"left"
Key
"left"
…
Key
…
Key
"right"
…
"left" "right"
"right" "left"
POLYMORPHISM First To
•No additional structure required.
27
Key
"left"
…
"right"
Key
"right"
…
"left"
Key
"left"
…
Key
…
Key
"right"
…
"left" "right"
"right" "left"
Field
"left"
…
"right"
Field
…
"left" "right"
POLYMORPHISM First To
•No additional structure required.
28
Key
"left"
…
"right"
Key
"right"
…
"left"
Key
"left"
…
Key
…
Key
"right"
…
"left" "right"
"right" "left"
POLYMORPHISM Only The Map Changes
29
value of siblingsibling
value of siblingparent
…
value of parentspill
__proto__ map
organization
Key Getterparent getter
indirect left getter
parent setter"sibling"
indirect left setter
"parent"
Setter
sibling setter"left"
sibling getter
value of rightright
left value of left
"sibling"
"parent"
Setter
indirect left getter"left"
indirect right setter
sibling setter
Keyparent setter
sibling getterparent getter
Getter
indirect left setter"right" indirect right getter
"sibling"
"parent"
Setter
indirect right getter"right"
indirect left setter
sibling setter
Keyparent setter
sibling getterparent getter
Getter
indirect right setter"left" indirect left getter
t1
t2
t3
POLYMORPHISM Cost/Benefit
•The spill is dynamic anyway, no net loss–Allow for new properties,– Implies copying and slop–Quanta, cache lines, yada, yada, yada–Less copying
•Assuming that organization patterns will reoccur, yield of less thrashing at CallSites
30
POLYMORPHISM
31
33
The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions.The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.
Q&A
34