polyfill

136 views
Skip to first unread message

Devicharan

unread,
Sep 15, 2017, 2:15:54 AM9/15/17
to vert.x
Hi ,

I am trying to inject some javascript functions so that i can access it in vertx-lang-js . Basically certain polyfills into nashorn . Any one has some code snippet ? Do we have a Promise polyfill available ?

Thanks,
charan

Paulo Lopes

unread,
Sep 15, 2017, 5:35:41 AM9/15/17
to vert.x
Hi Devicharan,

Please have a look at this PoC:

https://github.com/pmlopes/js-vertx-verticle-factory

You're looking for this script:

https://github.com/pmlopes/js-vertx-verticle-factory/blob/master/src/main/resources/polyfill.js

It will provide a vert.x specific Promise implementation plus Object.asign

This is the minimum required to use webpack to transform async/await to promises and JSX from react in your code.

Cheers,
Paulo

Devicharan

unread,
Sep 15, 2017, 8:16:13 AM9/15/17
to vert.x
Thanks Paulo !

Now it says 'require" not defined. Require should exists as part of verx-lang-js i think . Looks like all the vertx js files that were getting loaded earlier were not loading now ..

I might be using it in the wrong way !

Vertx vertx = Vertx.vertx();

vertx.deployVerticle(new ApiVerticle());
NashornVerticleFactory factory = new NashornVerticleFactory();
factory.init(vertx);

try {
vertx.deployVerticle(factory.createVerticle("server.js",vertx.getClass().getClassLoader()));
} catch (Exception e) {
e.printStackTrace();
}

SEVERE: ReferenceError: "require" is not defined in /Users/XXXXXX/src/vertx-examples/reactjs-server-side-rendering/server.js at line number 1086
javax.script.ScriptException: ReferenceError: "require" is not defined in /Users/XXXXXX/src/vertx-examples/reactjs-server-side-rendering/server.js at line number 1086
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:392)
at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190)
at xyz.jetdrone.vertx.factory.nashorn.Loader.main(Loader.java:106)
at xyz.jetdrone.vertx.factory.nashorn.NashornVerticleFactory$1.start(NashornVerticleFactory.java:66)

Thanks,
Charan

Paulo Lopes

unread,
Sep 15, 2017, 8:39:14 AM9/15/17
to vert.x
Hi,

Hold on, the project I've shared is a plain nashorn loader. It was a poc on how we could have modern javascript running on nashorn. As you might noticed nashorn only supports ES5 so if we use modern ES6 then we probably use Webpack to bundle the application and transpile from ES6 to ES5.

Since Webpack bundles everything on a single file, there is no need to have a require function implemented on nashorn, since at the end all you have is a single js file.

You can however just load the polyfill in your app using the official js way and the require is present. Sorry for the confusion.

Devicharan

unread,
Sep 16, 2017, 7:06:14 AM9/16/17
to vert.x
Thanks Paulo !

I have taken the promise polyfill  .  I feel that i am almost through with server side redux , getting some exception in the promise implementation . I get the data on the server as well for filling up the store .. but hit here..

Refject promise::java.lang.IllegalArgumentException: Cannot schedule a timer with delay < 1 ms

function reject(self, newValue) {
self._state = 2;
self._value = newValue;
console.log('Refject promise::'+newValue);
console.log('Self reject'+self._state);
finale(self);
}

Devicharan

unread,
Sep 16, 2017, 8:50:34 AM9/16/17
to vert.x
  It kind of hangs with or without the promise polyfill.  The middleware on the server side uses  vertx httpclient api for fetching data and fill up the redux store . On the client side i use superagent .  Some kind of hang once i get the data on the server side.

Paulo Lopes

unread,
Sep 16, 2017, 9:23:12 AM9/16/17
to vert.x
Hi the timeout error does not come from the polyfill it probably comes from a call to setTimeout which is emulated by vertx timers and it should be vertx.runOnContext()

Devicharan

unread,
Sep 17, 2017, 10:01:10 AM9/17/17
to vert.x
Thanks Paulo .  Debugging nashorn is crazy , i finally figured out that i should have used bodycodec or get the response in string atleast . There was no error shown any where , the browser spins forever . It got stuck in the react render method.
Reply all
Reply to author
Forward
0 new messages