Skip to content

Commit

Permalink
Fix conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Lopes <[email protected]>
  • Loading branch information
pmlopes committed Mar 11, 2022
2 parents 40509aa + f1955a1 commit 15d3f8f
Show file tree
Hide file tree
Showing 94 changed files with 374 additions and 181 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/graalvm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
version: [latest, dev]
version: ['22.0.0.2', dev]
os: [macos-latest, windows-latest, ubuntu-latest]
steps:
- name: Checkout
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [0.17.1] -
* Bump graaljs to 22.0.0.2
* codegen: fix imports on options
* structured clone: fix handling of graal proxy objects
* polyfill: same type coercion for globals as defined in MDN
* commonjs: loader will also work for `.json` files on `node_modules`
* graaljs: 1 instance per verticle to avoid cross thread access
* Bump vert.x to 4.2.5

## [0.17.0] - 2022-01-20
* Bump vert.x to 4.2.4
Expand Down
4 changes: 2 additions & 2 deletions codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<parent>
<groupId>io.reactiverse</groupId>
<artifactId>es4x-parent</artifactId>
<version>0.17.0</version>
<version>0.17.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>es4x-codegen</artifactId>
<version>0.17.0</version>
<version>0.17.1</version>

<properties>
<tools.jar>${java.home}/../lib/tools.jar</tools.jar>
Expand Down
4 changes: 2 additions & 2 deletions es4x/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<parent>
<groupId>io.reactiverse</groupId>
<artifactId>es4x-parent</artifactId>
<version>0.17.0</version>
<version>0.17.1</version>
<relativePath>../pom.xml</relativePath>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>es4x</artifactId>
<version>0.17.0</version>
<version>0.17.1</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
19 changes: 12 additions & 7 deletions es4x/src/main/java/io/reactiverse/es4x/ESVerticleFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,12 @@
public abstract class ESVerticleFactory implements VerticleFactory {

protected ECMAEngine engine;
private FileSystem fileSystem;
private Vertx vertx;

@Override
public synchronized void init(final Vertx vertx) {
if (engine == null) {
try {
this.fileSystem = new VertxFileSystem(vertx, getManifestAttribute("Import-Map"), defaultExtensions());
} catch (IOException e) {
throw new IllegalStateException("Failed to initialize the file system", e);
}
this.vertx = vertx;
this.engine = new ECMAEngine(vertx);
} else {
throw new IllegalStateException("Engine already initialized");
Expand All @@ -78,7 +74,7 @@ public int order() {
*/
protected Runtime createRuntime(ECMAEngine engine) {
return engine.newContext(
fileSystem,
createFileSystem(),
Source.newBuilder("js", ESVerticleFactory.class.getResource("polyfill/global.js")).buildLiteral(),
Source.newBuilder("js", ESVerticleFactory.class.getResource("polyfill/date.js")).buildLiteral(),
Source.newBuilder("js", ESVerticleFactory.class.getResource("polyfill/console.js")).buildLiteral(),
Expand All @@ -88,6 +84,14 @@ protected Runtime createRuntime(ECMAEngine engine) {
);
}

private FileSystem createFileSystem() {
try {
return new VertxFileSystem(vertx, getManifestAttribute("Import-Map"), defaultExtensions());
} catch (IOException e) {
throw new IllegalStateException("Failed to initialize the file system", e);
}
}

/**
* Create a vertx verticle that wraps the script, it will use the runtime configured in the
* {@link #createRuntime(ECMAEngine)} method.
Expand Down Expand Up @@ -196,6 +200,7 @@ protected final Future<Void> waitFor(Runtime runtime, String callback) {
public void close() {
if (engine != null) {
engine.close();
vertx = null;
}
}
}
19 changes: 16 additions & 3 deletions es4x/src/main/java/io/reactiverse/es4x/impl/StructuredClone.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ private static Object cloneObject(Value oToBeCloned, Map<Object, Object> dejaVu)
return oToBeCloned.asTimeZone();
}

// others
final String fConstrName = getConstructorName(oToBeCloned);

Value fConstr = oToBeCloned.getMember("constructor");
String fConstrName = fConstr.getMember("name").asString();
Object oClone;

switch (fConstrName) {
Expand Down Expand Up @@ -293,4 +291,19 @@ private static ByteBuffer extractTypedArray(Value typedArray) {

return null;
}

private static String getConstructorName(Value oToBeCloned) {
// proxy objects do not behave like native Object/Array, so we need to mask it
if (oToBeCloned.isProxyObject()) {
if (oToBeCloned.hasArrayElements()) {
return "Array";
}
if (oToBeCloned.hasMembers()) {
return "Object";
}
}
// other types, get constructor or return "undefined"
Value fConstr = oToBeCloned.getMember("constructor");
return fConstr == null ? "undefined" : fConstr.getMember("name").asString();
}
}
3 changes: 2 additions & 1 deletion es4x/src/main/resources/io/reactiverse/es4x/jvm-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@

function resolveAsNodeModule(id, root) {
let base = root ? [root, 'node_modules'].join('/') : 'node_modules';
return resolveAsFile(id, base) ||
return resolveAsFile(id, base, '.js') ||
resolveAsFile(id, base, '.json') ||
resolveAsDirectory(id, base) ||
(root ? resolveAsNodeModule(id, io.getParent(root)) : false);
}
Expand Down
28 changes: 24 additions & 4 deletions es4x/src/main/resources/io/reactiverse/es4x/polyfill/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,39 @@

global.setTimeout = function (callback, timeout) {
const args = Array.prototype.slice.call(arguments, 2);
// coerce to number
timeout = +timeout;
if (Number.isNaN(timeout)) {
timeout = 0;
}

if (Number(timeout) === 0) {
if (timeout === 0) {
// special case
vertx.runOnContext(function setTimeout(v) {
callback.apply(global, args);
});
} else {
return vertx.setTimer(Number(timeout), function setTimeout(t) {
return vertx.setTimer(timeout, function setTimeout(t) {
callback.apply(global, args);
});
}
};

global.setInterval = function (callback, timeout) {
const args = Array.prototype.slice.call(arguments, 2);
// coerce to number
timeout = +timeout;
if (Number.isNaN(timeout)) {
timeout = 0;
}

if (Number(timeout) === 0) {
if (timeout === 0) {
// special case
vertx.runOnContext(function setInterval(v) {
callback.apply(global, args);
});
} else {
return vertx.setPeriodic(Number(timeout), function setInterval(t) {
return vertx.setPeriodic(timeout, function setInterval(t) {
callback.apply(global, args);
});
}
Expand All @@ -59,12 +69,22 @@

global.clearTimeout = function (id) {
if (id !== undefined) {
// coerce to number
id = +id;
if (Number.isNaN(id)) {
id = 0;
}
return vertx.cancelTimer(id);
}
};

global.clearInterval = function (id) {
if (id !== undefined) {
// coerce to number
id = +id;
if (Number.isNaN(id)) {
id = 0;
}
return vertx.cancelTimer(id);
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.reactiverse.es4x.test;

import io.reactiverse.es4x.Runtime;
import io.vertx.ext.unit.junit.RunTestOnContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.graalvm.polyglot.Value;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static io.reactiverse.es4x.test.JS.*;
import static org.junit.Assert.assertTrue;

@RunWith(VertxUnitRunner.class)
public class CommonJSExportsTest {

private Runtime runtime;

@Rule
public RunTestOnContext rule = new RunTestOnContext();

@Before
public void initialize() {
runtime = commonjs(rule.vertx());
}

@Test
public void shouldAllowCustomizationOfExports() {
require(runtime, "./lib/exports/index.js");
}

@Test
public void shouldAllowCustomizationOfExportsFromSubmodule() {
require(runtime, "./lib/exports/exports.js");
}
}
30 changes: 30 additions & 0 deletions es4x/src/test/java/io/reactiverse/es4x/test/CommonJs580Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.reactiverse.es4x.test;

import io.reactiverse.es4x.Runtime;
import io.vertx.ext.unit.junit.RunTestOnContext;
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static io.reactiverse.es4x.test.JS.*;

@RunWith(VertxUnitRunner.class)
public class CommonJs580Test {

private Runtime runtime;

@Rule
public RunTestOnContext rule = new RunTestOnContext();

@Before
public void initialize() {
runtime = commonjs(rule.vertx());
}

@Test
public void shouldThrowAnErrorIfFileCantBeFound() {
require(runtime, "./lib/580");
}
}
27 changes: 27 additions & 0 deletions es4x/src/test/java/io/reactiverse/es4x/test/EventBusTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.reactiverse.es4x.test;

import io.reactiverse.es4x.Runtime;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.unit.Async;
Expand Down Expand Up @@ -63,4 +64,30 @@ public void testNativeJSArrayOverEB(TestContext ctx) {

runtime.eval("eb.send('test.address.array', ['foo', 'bar'])");
}

@Test(timeout = 10000)
public void testStructuredCloneWithDataObjectTest(TestContext ctx) {
final Async async = ctx.async();

rule.vertx().eventBus().consumer("test.address.data-object", msg -> {
ctx.assertNotNull(msg);
ctx.assertNotNull(msg.body());
Object res = msg.body();
ctx.assertNotNull(res);
ctx.assertTrue(res instanceof JsonObject);
ctx.assertEquals(
new HttpServerOptions().setHost("somewhere.com").toJson(),
res);
async.complete();
});

// @lang=JavaScript
String script =
"const HttpServerOptions = Java.type('io.vertx.core.http.HttpServerOptions')\n" +
"let data = new HttpServerOptions().setHost(\"somewhere.com\")\n" +
"eb.send('test.address.data-object', data.toJson())";

runtime.eval(script);
}

}
51 changes: 51 additions & 0 deletions es4x/src/test/java/io/reactiverse/es4x/test/GlobalsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,57 @@ public void testSetTimeout0(TestContext ctx) {
runtime.eval(script);
}

@Test(timeout = 10000)
public void testSetTimeoutNaN(TestContext ctx) {
final Async async = ctx.async();

runtime.put("ctx", ctx);
runtime.put("async", async);

/// @language=JavaScript
String script =
"setTimeout(function () {" +
" async.complete();" +
"}, NaN);";


runtime.eval(script);
}

@Test(timeout = 10000)
public void testSetTimeoutText(TestContext ctx) {
final Async async = ctx.async();

runtime.put("ctx", ctx);
runtime.put("async", async);

/// @language=JavaScript
String script =
"setTimeout(function () {" +
" async.complete();" +
"}, 'Durp');";


runtime.eval(script);
}

@Test(timeout = 10000)
public void testSetTimeoutObj(TestContext ctx) {
final Async async = ctx.async();

runtime.put("ctx", ctx);
runtime.put("async", async);

/// @language=JavaScript
String script =
"setTimeout(function () {" +
" async.complete();" +
"}, {});";


runtime.eval(script);
}

@Test(timeout = 10000)
public void testSetTimeoutWithParams(TestContext ctx) {
final Async async = ctx.async();
Expand Down
1 change: 1 addition & 0 deletions es4x/src/test/resources/lib/580.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const someJson = require('some-dependency/some-directory/some-file.json')
1 change: 1 addition & 0 deletions es4x/src/test/resources/lib/exports/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./index')
2 changes: 2 additions & 0 deletions es4x/src/test/resources/lib/exports/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", {value: true});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 15d3f8f

Please sign in to comment.