Adventures in JSR-292 or How To Be A Duck Without Really...

34
<Insert Picture Here> Adventures in JSR-292 or How To Be A Duck Without Really Trying Jim Laskey Multi-language Lead Java Language and Tools Group

Transcript of Adventures in JSR-292 or How To Be A Duck Without Really...

Page 1: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

<Insert Picture Here>

Adventures in JSR-292 orHow To Be A Duck Without Really Trying

Jim LaskeyMulti-language LeadJava Language and Tools Group

Page 2: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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.

Page 3: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

Agenda

• Introduction•Nashorn• JSR-292 Usage In Nashorn•Avoiding Polymorphism At CallSites•Q & A

3

Page 4: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

INTRODUCKTION

4

Page 5: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 6: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

NASHORN

6

Page 7: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

NASHORN Perfect Storm

• JavaFX Script•HTML5/JavaScript•Oracle acquisition•Rhino• John Rose taunt

7

Page 8: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 9: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 10: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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)

Page 11: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 12: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

NASHORN JSR-292 Usage In Nashorn

12

Page 13: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 14: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 15: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 16: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 17: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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)

Page 18: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 19: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 20: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 21: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

POLYMORPHISMAvoiding Polymorphism At CallSites

21

Page 22: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 23: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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"

Page 24: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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"

Page 25: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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"

Page 26: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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"

Page 27: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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"

Page 28: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

POLYMORPHISM First To

•No additional structure required.

28

Key

"left"

"right"

Key

"right"

"left"

Key

"left"

Key

Key

"right"

"left" "right"

"right" "left"

Page 29: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 30: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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

Page 31: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

POLYMORPHISM

31

Page 32: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

NASHORN Contacts

Jim [email protected]

[email protected]

32

Page 33: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

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.

Page 34: Adventures in JSR-292 or How To Be A Duck Without Really ...wiki.jvmlangsummit.com/images/c/ce/Nashorn.pdfJSR-292 Receivers •If found in the object’s map, bind the getter to the

Q&A

34