FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: support revert array in vm by mytEcust · Pull Request #69 · node-modules/js-to-java · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
27 changes: 16 additions & 11 deletions lib/revert.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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]' ||

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

原来的 instanceof 也留着?全靠 toString 判断我也担心没法覆盖全。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

已保留

Object.prototype.toString.call(java) === '[object String]') {
return java;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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') {
Expand All @@ -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) {
Expand Down
58 changes: 57 additions & 1 deletion test/revert.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var assert = require('assert');
var vm = require('vm');
require('should');
var revert = require('../index').revert;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
});
});

Back | FazBrowse Home | New Git URL