Skip to content

Commit db38a57

Browse files
author
对二
committed
feat: support revert array in vm
1 parent 662ea81 commit db38a57

2 files changed

Lines changed: 73 additions & 12 deletions

File tree

lib/revert.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ function derecycle(java, refs, path) {
99

1010
// simple type, null, undefined
1111
if (typeof java !== 'object' ||
12-
java === null ||
13-
//java === undefined ||
14-
java instanceof Boolean ||
15-
java instanceof Date ||
16-
java instanceof Number ||
17-
java instanceof RegExp ||
18-
java instanceof String) {
12+
java === null ||
13+
//java === undefined ||
14+
java instanceof Boolean ||
15+
java instanceof Date ||
16+
java instanceof Number ||
17+
java instanceof RegExp ||
18+
java instanceof String ||
19+
Object.prototype.toString.call(java) === '[object Boolean]'||
20+
Object.prototype.toString.call(java) === '[object Date]' ||
21+
Object.prototype.toString.call(java) === '[object Number]' ||
22+
Object.prototype.toString.call(java) === '[object RegExp]' ||
23+
Object.prototype.toString.call(java) === '[object String]') {
1924
return java;
2025
}
2126

@@ -48,12 +53,12 @@ function derecycle(java, refs, path) {
4853
result[k] = derecycle(v, refs, path ? path + DELIMITER + k : k);
4954
});
5055
*/
51-
} else if (java instanceof Array) {
56+
} else if (java instanceof Array || Array.isArray(java)) {
5257
result = [];
5358
java.forEach(function(v, i) {
5459
result[i] = derecycle(v, refs, path ? path + DELIMITER + i : i + '');
5560
});
56-
} else if (java instanceof Error) {
61+
} else if (java instanceof Error || Object.prototype.toString.call(java) === "[object Error]") {
5762
result = java;
5863
} else {
5964
// plain object
@@ -93,7 +98,7 @@ function retrocycle(js, root) {
9398
var i, l, item, path;
9499

95100
if (js && typeof js === 'object') {
96-
if (js instanceof Array) {
101+
if (js instanceof Array || Array.isArray(js)) {
97102
for (i = 0, l = js.length; i < l; i++) {
98103
item = js[i];
99104
if (item && typeof item === 'object') {
@@ -105,7 +110,7 @@ function retrocycle(js, root) {
105110
}
106111
}
107112
}
108-
} else if (js instanceof Error) {
113+
} else if (js instanceof Error || Object.prototype.toString.call(js) === "[object Error]") {
109114
return js;
110115
} else {
111116
for (i in js) {

test/revert.test.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var assert = require('assert');
4+
var vm = require('vm');
35
require('should');
46
var revert = require('../index').revert;
57

@@ -85,7 +87,6 @@ describe('revert: java to js', function() {
8587
revert(java).should.eql(['foo', 'bar', 'zoo']);
8688
});
8789

88-
8990
it('should work when used with cycular reference', function() {
9091
var java, result;
9192

@@ -182,4 +183,59 @@ describe('revert: java to js', function() {
182183
result[1] = result[0];
183184
revert(java).should.eql(result);
184185
});
186+
187+
it('should work in vm', function() {
188+
revert(new vm.Script(`33`).runInNewContext({})).should.equal(33);
189+
revert(new vm.Script(`'foo'`).runInNewContext({})).should.equal('foo');
190+
revert(new vm.Script(`true`).runInNewContext({})).should.equal(true);
191+
192+
assert.deepEqual(revert(new vm.Script(`new Boolean(true)`).runInNewContext({})), new Boolean(true));
193+
assert.deepEqual(revert(new vm.Script(`new Date('2023-01-01')`).runInNewContext({})), new Date('2023-01-01'));
194+
assert.deepEqual(revert(new vm.Script(`new Number(1)`).runInNewContext({})), new Number(1));
195+
assert.deepEqual(revert(new vm.Script(`new RegExp(/[12]/)`).runInNewContext({})), new RegExp(/[12]/));
196+
assert.deepEqual(revert(new vm.Script(`new String('foo')`).runInNewContext({})), new String('foo'));
197+
198+
var java = new vm.Script(`[
199+
{$class: 'string', $: 'foo'},
200+
{$class: 'string', $: 'bar'},
201+
{$class: 'string', $: 'zoo'},
202+
]`).runInNewContext({});
203+
revert(java).should.eql(['foo', 'bar', 'zoo']);
204+
205+
var error = new Error();
206+
error.message = 'this is a java IOException instance';
207+
error.name = 'java.io.IOException';
208+
error.cause = error;
209+
assert.deepEqual(revert(new vm.Script(`var error = new Error();
210+
error.message = 'this is a java IOException instance';
211+
error.name = 'java.io.IOException';
212+
error.cause = error;
213+
var java = {
214+
$class: 'java.io.IOException',
215+
$: error,
216+
};`).runInNewContext({})), error);
217+
218+
class javaError extends Error {
219+
constructor(message) {
220+
super(message);
221+
this.name = 'java.io.IOException';
222+
this.cause = this;
223+
}
224+
}
225+
var error2 = new javaError('this is a java IOException instance');
226+
227+
assert.deepEqual(revert(new vm.Script(`class javaError extends Error {
228+
constructor(message) {
229+
super(message);
230+
this.name = 'java.io.IOException';
231+
this.cause = this;
232+
}
233+
}
234+
var error2 = new javaError('this is a java IOException instance');
235+
var java = {
236+
$class: 'java.io.IOException',
237+
$: error2,
238+
};
239+
java`).runInNewContext({})), error2);
240+
});
185241
});

0 commit comments

Comments
 (0)