Skip to content

Commit

Permalink
feat: support revert array in vm
Browse files Browse the repository at this point in the history
  • Loading branch information
对二 committed Jul 10, 2023
1 parent 662ea81 commit 69ae281
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/revert.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function derecycle(java, refs, path) {
result[k] = derecycle(v, refs, path ? path + DELIMITER + k : k);
});
*/
} else if (java instanceof Array) {
} else if (Array.isArray(java)) {
result = [];
java.forEach(function(v, i) {
result[i] = derecycle(v, refs, path ? path + DELIMITER + i : i + '');
Expand Down
13 changes: 13 additions & 0 deletions test/revert.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

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

Expand Down Expand Up @@ -85,6 +86,18 @@ describe('revert: java to js', function() {
revert(java).should.eql(['foo', 'bar', 'zoo']);
});

it('should work with list in vm', function() {
var java = [2, 3, 5];
revert(java).should.eql(java);

java = new vm.Script(`[
{$class: 'string', $: 'foo'},
{$class: 'string', $: 'bar'},
{$class: 'string', $: 'zoo'},
]`).runInNewContext({});
revert(java).should.eql(['foo', 'bar', 'zoo']);
});


it('should work when used with cycular reference', function() {
var java, result;
Expand Down

0 comments on commit 69ae281

Please sign in to comment.