List of usage examples for jdk.nashorn.api.scripting AbstractJSObject AbstractJSObject
public AbstractJSObject()
From source file:BufferArray.java
License:Open Source License
@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/* w w w . j a v a2s .c om*/ 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; }
From source file:com.eas.client.application.ScriptedTests.java
protected void start(String aTestModuleName, long aTimeout) throws InterruptedException { Object success = new Object(); Object failure = new Object(); long started = System.currentTimeMillis(); AtomicReference<Object> completion = new AtomicReference(); withFacade(completion, () -> {/*w w w .j a v a2 s . c om*/ try { ScriptedResource.require(new String[] { aTestModuleName }, null, new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { JSObject testModule = Scripts.getSpace().lookup(aTestModuleName); JSObject testInstance = (JSObject) testModule.newObject(); JSObject execute = (JSObject) testInstance.getMember("execute"); try { execute.call(testInstance, new Object[] { new SystemJSCallback() { @Override public Object call(final Object thiz, final Object... args) { completion.set(success); return null; } }, new SystemJSCallback() { @Override public Object call(final Object thiz, final Object... args) { completion.set(args.length > 0 ? args[0] : failure); return null; } } }); } catch (ECMAException ex) { completion.set(ex); } return null; } }, new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { completion.set(args[0]); return null; } }); } catch (Exception ex) { Logger.getLogger(ScriptedTests.class.getName()).log(Level.SEVERE, null, ex); completion.set(ex); } }); while (completion.get() == null && System.currentTimeMillis() < started + aTimeout) { Thread.sleep(10); } Object lastChance = completion.get(); if (lastChance != success) { String failedText = aTestModuleName + " failed due to "; if (lastChance == null) { fail(failedText + "a timeout"); } else if (lastChance == failure) { fail(failedText + "an unknown problem"); } else if (lastChance instanceof Throwable) { fail(failedText + lastChance.toString()); } else if (lastChance instanceof JSObject) { fail(failedText + Scripts.getSpace().toJson(lastChance)); } else { fail(failedText + lastChance.toString()); } } }
From source file:com.eas.client.application.ScriptedTests.java
private void withFacade(AtomicReference<Object> aCompletion, Runnable withFacade) { Scripts.getSpace().process(() -> { try {/* w ww . ja va 2 s . c o m*/ if (Scripts.getSpace().getDefined().containsKey("facade")) { withFacade.run(); } else { ScriptedResource.require(new String[] { "facade" }, null, new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { JSObject facade = Scripts.getSpace().lookup("facade"); JSObject cacheBust = (JSObject) facade.getMember("cacheBust"); cacheBust.call(facade, new Object[] { true }); JSObject export = (JSObject) facade.getMember("export"); export.call(facade, new Object[] { Scripts.getSpace().getGlobal() }); withFacade.run(); return null; } }, new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { aCompletion.set(args[0]); return null; } }); } } catch (Exception ex) { aCompletion.set(ex); } }); }
From source file:com.eas.client.queries.ScriptedQuery.java
@Override public JSObject execute(Scripts.Space aSpace, Consumer<JSObject> onSuccess, Consumer<Exception> onFailure) throws Exception { assert Scripts.getSpace() == aSpace : "Scripts.Space TLS assumption failed"; if (onSuccess != null) { ScriptedResource._require(new String[] { entityName }, null, aSpace, new HashSet<>(), (Void v) -> { JSObject source = aSpace.createModule(entityName); if (source.hasMember("fetch")) { Object oFetch = source.getMember("fetch"); if (oFetch instanceof JSObject) { JSObject jsFetch = (JSObject) oFetch; if (jsFetch.isFunction()) { JSObject jsParams = aSpace.makeObj(); for (int i = 0; i < params.getParametersCount(); i++) { Parameter p = params.get(i + 1); jsParams.setMember(p.getName(), aSpace.toJs(p.getValue())); }/*from w ww . java 2 s . c o m*/ final ExecutionChecker exChecker = new ExecutionChecker(); Object oRowset = jsFetch.call(source, aSpace.toJs(new Object[] { jsParams, new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { if (exChecker.isExecutionNeeded()) { try { JSObject jsRowset = args.length > 0 ? (JSObject) aSpace.toJava(args[0]) : null; try { onSuccess.accept(jsRowset); } catch (Exception ex) { Logger.getLogger(ScriptedQuery.class.getName()) .log(Level.SEVERE, null, ex); } } catch (Exception ex) { if (onFailure != null) { onFailure.accept(ex); } } } return null; } }, new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { if (exChecker.isExecutionNeeded()) { if (onFailure != null) { if (args.length > 0) { if (args[0] instanceof Exception) { onFailure.accept((Exception) args[0]); } else { onFailure.accept(new Exception( String.valueOf(aSpace.toJava(args[0])))); } } else { onFailure.accept(new Exception( "No error information from fetch method")); } } } return null; } } })); if (!JSType.nullOrUndefined(oRowset)) { onSuccess.accept((JSObject) aSpace.toJava(oRowset)); exChecker.setExecutionNeeded(false); } } } } }, onFailure); return null; } else { JSObject source = aSpace.createModule(entityName); if (source.hasMember("fetch")) { Object oFetch = source.getMember("fetch"); if (oFetch instanceof JSObject) { JSObject jsFetch = (JSObject) oFetch; if (jsFetch.isFunction()) { JSObject jsParams = aSpace.makeObj(); Object oRowset = jsFetch.call(source, aSpace.toJs(new Object[] { jsParams })); if (!JSType.nullOrUndefined(oRowset)) { return (JSObject) aSpace.toJava(oRowset); } } } } return null; } }
From source file:jdk.dynalink.test.DynamicLinkerFactoryTest.java
License:Open Source License
@Test public void nashornExportedLinkerJSObjectTest() { final DynamicLinkerFactory factory = newDynamicLinkerFactory(false); final DynamicLinker linker = factory.createLinker(); final MethodType mt = MethodType.methodType(Object.class, Object.class); final NamedOperation op = new NamedOperation(StandardOperation.GET_PROPERTY, "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 Object obj = new AbstractJSObject() { @Override//from w ww .j av a 2 s.com public Object getMember(String name) { reachedGetMember[0] = true; return name.equals("foo") ? "bar" : "<unknown>"; } }; Object value = null; try { value = cs.getTarget().invoke(obj); } catch (Throwable th) { throw new RuntimeException(th); } Assert.assertTrue(reachedGetMember[0]); Assert.assertEquals(value, "bar"); }
From source file:net.desertconsulting.mocharest.js.DeferredTest.java
License:Apache License
@Test(expected = IllegalArgumentException.class) public void testDoneWithIllegalArgument() { System.out.println("doneWithIllegalArgument"); Object expextedResult = "test"; Deferred instance = new Deferred(); instance.done(new AbstractJSObject() { });/*from w ww. j a v a2s .c o m*/ }
From source file:net.desertconsulting.mocharest.js.DeferredTest.java
License:Apache License
@Test(expected = IllegalArgumentException.class) public void testFailWithIllegalArgument() { System.out.println("failWithIllegalArgument"); Object expextedResult = "test"; Deferred instance = new Deferred(); instance.fail(new AbstractJSObject() { });//from w w w .j a v a 2 s .c o m }
From source file:net.desertconsulting.mocharest.js.DeferredTest.java
License:Apache License
@Test(expected = IllegalArgumentException.class) public void testAlwaysWithIllegalArgument() { System.out.println("alwaysWithIllegalArgument"); Object expextedResult = "test"; Deferred instance = new Deferred(); instance.always(new AbstractJSObject() { });/*from ww w.ja v a2 s . com*/ }
From source file:net.desertconsulting.mocharest.request.MochaRequestHandlerTest.java
License:Apache License
@Test(expected = IllegalArgumentException.class) public void testConstructorWithIllegalHandler() throws MalformedURLException, ScriptException { System.out.println("constructorWithIllegalHandler"); String testUrl = "/test/{test:int}"; JSObject map = (ScriptObjectMirror) new ScriptEngineManager().getEngineByName("js") .eval(String.format("(function(){ return {contentType:'%s', acceptType:'%s'};})()", MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON)); MochaRequestHandler instance = new MochaRequestHandler("/test/{test:int}", map, new AbstractJSObject() { });//from w ww. jav a 2 s . c o m assertEquals(MediaType.APPLICATION_JSON, instance.getContentType()); }
From source file:reactor.js.test.NashornJUnitTestRunner.java
License:Open Source License
private static void addStaticMethods(Class<?> type, Bindings bindings) { for (Method method : type.getDeclaredMethods()) { if (Modifier.isStatic(method.getModifiers())) { final Method methodToInvoke = method; method.setAccessible(true);// w ww .java 2s .co m bindings.put(method.getName(), new AbstractJSObject() { @Override public Object call(Object thiz, Object... args) { try { return methodToInvoke.invoke(null, args); } catch (Exception e) { return new NashornException(e.getMessage(), e) { }; } } @Override public boolean isFunction() { return true; } @Override public boolean isStrictFunction() { return true; } }); } } }