diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..ad3860c --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,18 @@ +name: CI + +on: + push: + branches: [ master ] + + pull_request: + branches: [ master ] + + workflow_dispatch: {} + +jobs: + Job: + name: Node.js + uses: node-modules/github-actions/.github/workflows/node-test.yml@master + with: + os: 'ubuntu-latest' + version: '12, 14, 16, 18, 20' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..a4e1158 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,15 @@ +name: Release + +on: + push: + branches: [ master ] + +jobs: + release: + name: Node.js + uses: node-modules/github-actions/.github/workflows/node-release.yml@master + secrets: + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} + GIT_TOKEN: ${{ secrets.GIT_TOKEN }} + with: + checkTest: false diff --git a/.gitignore b/.gitignore index dbf42cc..bb986f5 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ npm-debug.log node_modules .DS_Store .idea/ +.nyc_output/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 00392ae..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -sudo: false -language: node_js -node_js: - - '12' - - '10' -script: - - npm run ci -after_script: - - npm i codecov && codecov diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..ed25c4a --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +# Changelog + +## [2.8.0](https://github.com/node-modules/js-to-java/compare/v2.7.0...v2.8.0) (2023-07-11) + + +### Features + +* support revert array in vm ([#69](https://github.com/node-modules/js-to-java/issues/69)) ([dcb2384](https://github.com/node-modules/js-to-java/commit/dcb2384928c5eec1189d8f9fa3420079f42a11d2)) diff --git a/lib/revert.js b/lib/revert.js index ac8e0be..6ba359c 100644 --- a/lib/revert.js +++ b/lib/revert.js @@ -9,13 +9,18 @@ function derecycle(java, refs, path) { // simple type, null, undefined if (typeof java !== 'object' || - java === null || - //java === undefined || - java instanceof Boolean || - java instanceof Date || - java instanceof Number || - java instanceof RegExp || - java instanceof String) { + java === null || + //java === undefined || + java instanceof Boolean || + java instanceof Date || + java instanceof Number || + java instanceof RegExp || + java instanceof String || + Object.prototype.toString.call(java) === '[object Boolean]'|| + Object.prototype.toString.call(java) === '[object Date]' || + Object.prototype.toString.call(java) === '[object Number]' || + Object.prototype.toString.call(java) === '[object RegExp]' || + Object.prototype.toString.call(java) === '[object String]') { return java; } @@ -48,12 +53,12 @@ function derecycle(java, refs, path) { result[k] = derecycle(v, refs, path ? path + DELIMITER + k : k); }); */ - } else if (java instanceof Array) { + } else if (java instanceof Array || Array.isArray(java)) { result = []; java.forEach(function(v, i) { result[i] = derecycle(v, refs, path ? path + DELIMITER + i : i + ''); }); - } else if (java instanceof Error) { + } else if (java instanceof Error || Object.prototype.toString.call(java) === "[object Error]") { result = java; } else { // plain object @@ -93,7 +98,7 @@ function retrocycle(js, root) { var i, l, item, path; if (js && typeof js === 'object') { - if (js instanceof Array) { + if (js instanceof Array || Array.isArray(js)) { for (i = 0, l = js.length; i < l; i++) { item = js[i]; if (item && typeof item === 'object') { @@ -105,7 +110,7 @@ function retrocycle(js, root) { } } } - } else if (js instanceof Error) { + } else if (js instanceof Error || Object.prototype.toString.call(js) === "[object Error]") { return js; } else { for (i in js) { diff --git a/package.json b/package.json index dd40e83..0e18374 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-to-java", - "version": "2.7.0", + "version": "2.8.0", "description": "easy way to wrap js object to java object", "main": "index.js", "files": [ @@ -9,10 +9,9 @@ ], "scripts": { "test": "mocha -R spec -t 3000 -r should test/*.test.js", - "test-cov": "istanbul cover _mocha -- -t 3000 -r should test/*.test.js", + "test-cov": "nyc mocha -- -t 3000 -r should test/*.test.js", "ci": "npm run jshint && npm run test-cov", - "jshint": "jshint .", - "autod": "autod -w --prefix '~' -t test -e examples" + "jshint": "jshint lib index.js" }, "repository": { "type": "git", @@ -22,7 +21,6 @@ "js", "java", "object", - "wraper", "class" ], "author": "dead_horse ", @@ -33,12 +31,11 @@ "homepage": "https://github.com/node-modules/js-to-java", "dependencies": {}, "devDependencies": { - "autod": "*", "contributors": "*", "cov": "*", - "istanbul": "*", + "jshint": "2", "mocha": "*", - "should": "10.0.0", - "jshint": "2" + "nyc": "^15.1.0", + "should": "10.0.0" } } diff --git a/test/revert.test.js b/test/revert.test.js index ca165a5..fa33409 100644 --- a/test/revert.test.js +++ b/test/revert.test.js @@ -1,5 +1,7 @@ 'use strict'; +var assert = require('assert'); +var vm = require('vm'); require('should'); var revert = require('../index').revert; @@ -85,7 +87,6 @@ describe('revert: java to js', function() { revert(java).should.eql(['foo', 'bar', 'zoo']); }); - it('should work when used with cycular reference', function() { var java, result; @@ -182,4 +183,59 @@ describe('revert: java to js', function() { result[1] = result[0]; revert(java).should.eql(result); }); + + it('should work in vm', function() { + revert(new vm.Script(`33`).runInNewContext({})).should.equal(33); + revert(new vm.Script(`'foo'`).runInNewContext({})).should.equal('foo'); + revert(new vm.Script(`true`).runInNewContext({})).should.equal(true); + + assert.deepEqual(revert(new vm.Script(`new Boolean(true)`).runInNewContext({})), new Boolean(true)); + assert.deepEqual(revert(new vm.Script(`new Date('2023-01-01')`).runInNewContext({})), new Date('2023-01-01')); + assert.deepEqual(revert(new vm.Script(`new Number(1)`).runInNewContext({})), new Number(1)); + assert.deepEqual(revert(new vm.Script(`new RegExp(/[12]/)`).runInNewContext({})), new RegExp(/[12]/)); + assert.deepEqual(revert(new vm.Script(`new String('foo')`).runInNewContext({})), new String('foo')); + + var java = new vm.Script(`[ + {$class: 'string', $: 'foo'}, + {$class: 'string', $: 'bar'}, + {$class: 'string', $: 'zoo'}, + ]`).runInNewContext({}); + revert(java).should.eql(['foo', 'bar', 'zoo']); + + var error = new Error(); + error.message = 'this is a java IOException instance'; + error.name = 'java.io.IOException'; + error.cause = error; + assert.deepEqual(revert(new vm.Script(`var error = new Error(); + error.message = 'this is a java IOException instance'; + error.name = 'java.io.IOException'; + error.cause = error; + var java = { + $class: 'java.io.IOException', + $: error, + };`).runInNewContext({})), error); + + class javaError extends Error { + constructor(message) { + super(message); + this.name = 'java.io.IOException'; + this.cause = this; + } + } + var error2 = new javaError('this is a java IOException instance'); + + assert.deepEqual(revert(new vm.Script(`class javaError extends Error { + constructor(message) { + super(message); + this.name = 'java.io.IOException'; + this.cause = this; + } + } + var error2 = new javaError('this is a java IOException instance'); + var java = { + $class: 'java.io.IOException', + $: error2, + }; + java`).runInNewContext({})), error2); + }); });