jdk.nashorn.api.scripting.AbstractJSObject Java Examples

The following examples show how to use jdk.nashorn.api.scripting.AbstractJSObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: WithObject.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example #2
Source File: BufferArray.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getMember(final String name) {
   switch (name) {
      case "length":
          return buf.capacity();
      case "buf":
          // return a 'function' value for this property
          return new AbstractJSObject() {
              @Override
              public Object call(final Object thiz, final Object... args) {
                  return BufferArray.this.buf;
              }

              // yes, I'm a function !
              @Override
              public boolean isFunction() {
                  return true;
              }
          };
      default:
          break;
   }
   return null;
}
 
Example #3
Source File: PluggableJSObjectTest.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void hidingInternalObjectsForJSObjectTest() throws Exception {
    final ScriptEngineManager engineManager = new ScriptEngineManager();
    final ScriptEngine e = engineManager.getEngineByName("nashorn");

    final String code = "function func(obj) { obj.foo = [5, 5]; obj.bar = {} }";
    e.eval(code);

    // call the exposed function but pass user defined JSObject impl as argument
    ((Invocable)e).invokeFunction("func", new AbstractJSObject() {
        @Override
        public void setMember(final String name, final Object value) {
            // make sure that wrapped objects are passed (and not internal impl. objects)
            assertTrue(value.getClass() == ScriptObjectMirror.class);
        }
    });
}
 
Example #4
Source File: WithObject.java    From jdk8u_nashorn with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example #5
Source File: BufferArray.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getMember(final String name) {
   switch (name) {
      case "length":
          return buf.capacity();
      case "buf":
          // return a 'function' value for this property
          return new AbstractJSObject() {
              @Override
              public Object call(final Object thiz, final Object... args) {
                  return BufferArray.this.buf;
              }

              // yes, I'm a function !
              @Override
              public boolean isFunction() {
                  return true;
              }
          };
      default:
          break;
   }
   return null;
}
 
Example #6
Source File: PluggableJSObjectTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void hidingInternalObjectsForJSObjectTest() throws Exception {
    final ScriptEngineManager engineManager = new ScriptEngineManager();
    final ScriptEngine e = engineManager.getEngineByName("nashorn");

    final String code = "function func(obj) { obj.foo = [5, 5]; obj.bar = {} }";
    e.eval(code);

    // call the exposed function but pass user defined JSObject impl as argument
    ((Invocable)e).invokeFunction("func", new AbstractJSObject() {
        @Override
        public void setMember(final String name, final Object value) {
            // make sure that wrapped objects are passed (and not internal impl. objects)
            assertTrue(value.getClass() == ScriptObjectMirror.class);
        }
    });
}
 
Example #7
Source File: WithObject.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example #8
Source File: BufferArray.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getMember(final String name) {
   switch (name) {
      case "length":
          return buf.capacity();
      case "buf":
          // return a 'function' value for this property
          return new AbstractJSObject() {
              @Override
              public Object call(final Object thiz, final Object... args) {
                  return BufferArray.this.buf;
              }

              // yes, I'm a function !
              @Override
              public boolean isFunction() {
                  return true;
              }
          };
      default:
          break;
   }
   return null;
}
 
Example #9
Source File: PluggableJSObjectTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void hidingInternalObjectsForJSObjectTest() throws Exception {
    final ScriptEngineManager engineManager = new ScriptEngineManager();
    final ScriptEngine e = engineManager.getEngineByName("nashorn");

    final String code = "function func(obj) { obj.foo = [5, 5]; obj.bar = {} }";
    e.eval(code);

    // call the exposed function but pass user defined JSObject impl as argument
    ((Invocable)e).invokeFunction("func", new AbstractJSObject() {
        @Override
        public void setMember(final String name, final Object value) {
            // make sure that wrapped objects are passed (and not internal impl. objects)
            assertTrue(value.getClass() == ScriptObjectMirror.class);
        }
    });
}
 
Example #10
Source File: WithObject.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example #11
Source File: BufferArray.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getMember(final String name) {
   switch (name) {
      case "length":
          return buf.capacity();
      case "buf":
          // return a 'function' value for this property
          return new AbstractJSObject() {
              @Override
              public Object call(final Object thiz, final Object... args) {
                  return BufferArray.this.buf;
              }

              // yes, I'm a function !
              @Override
              public boolean isFunction() {
                  return true;
              }
          };
      default:
          break;
   }
   return null;
}
 
Example #12
Source File: PluggableJSObjectTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void hidingInternalObjectsForJSObjectTest() throws Exception {
    final ScriptEngineManager engineManager = new ScriptEngineManager();
    final ScriptEngine e = engineManager.getEngineByName("nashorn");

    final String code = "function func(obj) { obj.foo = [5, 5]; obj.bar = {} }";
    e.eval(code);

    // call the exposed function but pass user defined JSObject impl as argument
    ((Invocable)e).invokeFunction("func", new AbstractJSObject() {
        @Override
        public void setMember(final String name, final Object value) {
            // make sure that wrapped objects are passed (and not internal impl. objects)
            assertTrue(value.getClass() == ScriptObjectMirror.class);
        }
    });
}
 
Example #13
Source File: WithObject.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example #14
Source File: BufferArray.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getMember(final String name) {
   switch (name) {
      case "length":
          return buf.capacity();
      case "buf":
          // return a 'function' value for this property
          return new AbstractJSObject() {
              @Override
              public Object call(final Object thiz, final Object... args) {
                  return BufferArray.this.buf;
              }

              // yes, I'm a function !
              @Override
              public boolean isFunction() {
                  return true;
              }
          };
      default:
          break;
   }
   return null;
}
 
Example #15
Source File: PluggableJSObjectTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void hidingInternalObjectsForJSObjectTest() throws Exception {
    final ScriptEngineManager engineManager = new ScriptEngineManager();
    final ScriptEngine e = engineManager.getEngineByName("nashorn");

    final String code = "function func(obj) { obj.foo = [5, 5]; obj.bar = {} }";
    e.eval(code);

    // call the exposed function but pass user defined JSObject impl as argument
    ((Invocable)e).invokeFunction("func", new AbstractJSObject() {
        @Override
        public void setMember(final String name, final Object value) {
            // make sure that wrapped objects are passed (and not internal impl. objects)
            assertTrue(value.getClass() == ScriptObjectMirror.class);
        }
    });
}
 
Example #16
Source File: PluggableJSObjectTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void hidingInternalObjectsForJSObjectTest() throws Exception {
    final ScriptEngineManager engineManager = new ScriptEngineManager();
    final ScriptEngine e = engineManager.getEngineByName("nashorn");

    final String code = "function func(obj) { obj.foo = [5, 5]; obj.bar = {} }";
    e.eval(code);

    // call the exposed function but pass user defined JSObject impl as argument
    ((Invocable)e).invokeFunction("func", new AbstractJSObject() {
        @Override
        public void setMember(final String name, final Object value) {
            // make sure that wrapped objects are passed (and not internal impl. objects)
            assertTrue(value.getClass() == ScriptObjectMirror.class);
        }
    });
}
 
Example #17
Source File: BufferArray.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getMember(final String name) {
   switch (name) {
      case "length":
          return buf.capacity();
      case "buf":
          // return a 'function' value for this property
          return new AbstractJSObject() {
              @Override
              public Object call(final Object thiz, final Object... args) {
                  return BufferArray.this.buf;
              }

              // yes, I'm a function !
              @Override
              public boolean isFunction() {
                  return true;
              }
          };
      default:
          break;
   }
   return null;
}
 
Example #18
Source File: WithObject.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example #19
Source File: BufferArray.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getMember(final String name) {
   switch (name) {
      case "length":
          return buf.capacity();
      case "buf":
          // return a 'function' value for this property
          return new AbstractJSObject() {
              @Override
              public Object call(final Object thiz, final Object... args) {
                  return BufferArray.this.buf;
              }

              // yes, I'm a function !
              @Override
              public boolean isFunction() {
                  return true;
              }
          };
      default:
          break;
   }
   return null;
}
 
Example #20
Source File: WithObject.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example #21
Source File: JDK_8148140_Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@BeforeClass
public void setupTest() {
    engine = new ScriptEngineManager().getEngineByName("js");
    engine.put("f", new AbstractJSObject() {
        @Override
        public boolean isFunction() {
            return true;
        }
        @Override
        public Object call(final Object thiz, final Object... args) {
            return Arrays.deepToString(args);
        }
    });
}
 
Example #22
Source File: JDK_8148140_Test.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@BeforeClass
public void setupTest() {
    engine = new ScriptEngineManager().getEngineByName("js");
    engine.put("f", new AbstractJSObject() {
        @Override
        public boolean isFunction() {
            return true;
        }
        @Override
        public Object call(Object thiz, Object... args) {
            return Arrays.deepToString(args);
        }
    });
}
 
Example #23
Source File: JDK_8148140_Test.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@BeforeClass
public void setupTest() {
    engine = new ScriptEngineManager().getEngineByName("js");
    engine.put("f", new AbstractJSObject() {
        @Override
        public boolean isFunction() {
            return true;
        }
        @Override
        public Object call(Object thiz, Object... args) {
            return Arrays.deepToString(args);
        }
    });
}
 
Example #24
Source File: DynamicLinkerFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void nashornExportedLinkerJSObjectTest() {
    final DynamicLinkerFactory factory = newDynamicLinkerFactory(false);
    final DynamicLinker linker = factory.createLinker();

    final MethodType mt = MethodType.methodType(Object.class, Object.class);
    final Operation op = GET_PROPERTY.named("foo");
    final CallSite cs = linker.link(new SimpleRelinkableCallSite(new CallSiteDescriptor(
            MethodHandles.publicLookup(), op, mt)));
    final boolean[] reachedGetMember = new boolean[1];
    // check that the nashorn exported linker can be used for user defined JSObject
    final Object obj = new AbstractJSObject() {
            @Override
            public Object getMember(final String name) {
                reachedGetMember[0] = true;
                return name.equals("foo")? "bar" : "<unknown>";
            }
        };

    Object value = null;
    try {
        value = cs.getTarget().invoke(obj);
    } catch (final Throwable th) {
        throw new RuntimeException(th);
    }

    Assert.assertTrue(reachedGetMember[0]);
    Assert.assertEquals(value, "bar");
}
 
Example #25
Source File: ReadXmlNGTest.java    From opentest with MIT License 5 votes vote down vote up
@Test
public void testElement() {
    ReadXml readXml = new ReadXml();
    String xml = "<root attr1=\"value1\"><child1 attr1=\"value1\">child1 text</child1></root>";
    readXml.writeArgument("xml", xml);
    readXml.run();

    AbstractJSObject rootNode = (AbstractJSObject) readXml.readOutputValue("rootNode");

    AbstractJSObject child1 = ((Function<String, AbstractJSObject>) rootNode.getMember("node")).apply("child1");
    assertEquals((String) child1.getMember("nodeType"), "element");
    assertEquals((String) child1.getMember("text"), "child1 text");
}
 
Example #26
Source File: ReadXmlNGTest.java    From opentest with MIT License 5 votes vote down vote up
@Test
public void testAttributes() {
    ReadXml readXml = new ReadXml();
    String xml = "<root attr1=\"value1\"><child1 attr1=\"value1\">child1 text</child1></root>";
    readXml.writeArgument("xml", xml);
    readXml.run();

    AbstractJSObject rootNode = (AbstractJSObject) readXml.readOutputValue("rootNode");

    Object attrsObj = rootNode.getMember("attributes");
    if (attrsObj instanceof Map) {
        Map attrs = (Map)attrsObj;
        assertEquals(attrs.get("attr1"), "value1");
    }
}
 
Example #27
Source File: ReadXmlNGTest.java    From opentest with MIT License 5 votes vote down vote up
@Test
public void testAttribute() {
    ReadXml readXml = new ReadXml();
    String xml = "<root attr1=\"value1\"><child1 attr1=\"value1\">child1 text</child1></root>";
    readXml.writeArgument("xml", xml);
    readXml.run();

    AbstractJSObject rootNode = (AbstractJSObject) readXml.readOutputValue("rootNode");

    String attr1 = ((Function<String, String>) rootNode.getMember("attribute")).apply("attr1");
    assertEquals(attr1, "value1");
}
 
Example #28
Source File: ReadXmlNGTest.java    From opentest with MIT License 5 votes vote down vote up
@Test
public void testRootNodeText() {
    ReadXml readXml = new ReadXml();
    String xml = "<root attr1=\"value1\"><child1 attr1=\"value1\">child1 text</child1></root>";
    readXml.writeArgument("xml", xml);
    readXml.run();

    AbstractJSObject rootNode = (AbstractJSObject) readXml.readOutputValue("rootNode");

    assertEquals((String) rootNode.getMember("text"), "child1 text");
}
 
Example #29
Source File: JDK_8148140_Test.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@BeforeClass
public void setupTest() {
    engine = new ScriptEngineManager().getEngineByName("js");
    engine.put("f", new AbstractJSObject() {
        @Override
        public boolean isFunction() {
            return true;
        }
        @Override
        public Object call(Object thiz, Object... args) {
            return Arrays.deepToString(args);
        }
    });
}
 
Example #30
Source File: JDK_8148140_Test.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@BeforeClass
public void setupTest() {
    engine = new ScriptEngineManager().getEngineByName("js");
    engine.put("f", new AbstractJSObject() {
        @Override
        public boolean isFunction() {
            return true;
        }
        @Override
        public Object call(Object thiz, Object... args) {
            return Arrays.deepToString(args);
        }
    });
}