Fix Enumerator.toArray method and related issues

This commit is contained in:
Namhyeon Go 2023-11-06 11:23:09 +09:00
parent da36ad57be
commit 4906799170
3 changed files with 37 additions and 50 deletions

View File

@ -27,52 +27,41 @@ if (!Function.prototype.GetResource) {
}
}
if (!Enumerator.prototype.toArray) {
Enumerator.prototype.toArray = function() {
var items = [];
for (; !this.atEnd(); this.moveNext()) {
var item = this.item();
try {
items.push(item);
} catch (e) {}
}
return items;
};
}
// The provided code snippet has been corrected by ChatGPT.
// https://chat.openai.com/share/eaab056c-d265-4ee3-b355-9f29176a9caa
// Related issues: #75 #42 #30
Enumerator.prototype.toArray = function() {
var result = [];
while (!this.atEnd()) {
var currentItem = this.item();
var currentItemProperties = currentItem.Properties_;
var itemObject = {};
if (!Enumerator.prototype.toArray2) {
Enumerator.prototype.toArray2 = function() {
var a = [];
for (; !this.atEnd(); this.moveNext()) {
var x = {};
var b = new Enumerator(this.item().Properties_);
for (; !b.atEnd(); b.moveNext()) {
var c = b.item();
if (typeof c.value !== "unknown") {
try {
x[c.name] = c.value.toString();
} catch (e) {
x[c.name] = c.value;
}
var propertiesEnumerator = new Enumerator(currentItemProperties);
while (!propertiesEnumerator.atEnd()) {
var property = propertiesEnumerator.item();
if (typeof property.value !== "unknown") { // The type "Unknown" is Array
itemObject[property.name] = property.value;
} else {
var i = 0,
d = [];
var arrayValues = [];
var index = 0;
while (true) {
try {
d.push(c.value(i));
i++;
arrayValues.push(property.value(index));
index++;
} catch (e) {
break;
}
}
x[c.name] = d;
itemObject[property.name] = arrayValues;
}
propertiesEnumerator.moveNext();
}
a.push(x);
result.push(itemObject);
this.moveNext();
}
return a;
};
}
return result;
};
/////////////////////////////////////////////////////////////////////////////////
// Global APIs
@ -535,7 +524,7 @@ global.splitLn = splitLn;
global.addslashes = addslashes;
global.AsyncFunction = AsyncFunction;
exports.VERSIONINFO = "Standard Library (std.js) version 0.8.1";
exports.VERSIONINFO = "Standard Library (std.js) version 0.8.2";
exports.global = global;
exports.require = global.require;

View File

@ -58,10 +58,6 @@ var WMIQueryObject = function() {
return this.cursor.toArray();
};
this.fetchAll2 = function() {
return this.cursor.toArray2();
};
this.get = function(key) {
if (key in this.current) {
return this.current[key];
@ -162,6 +158,6 @@ exports.create = create;
exports.execQuery = execQuery;
exports.setClass = setClass;
exports.VERSIONINFO = "WMI interface (wmi.js) version 0.1.1";
exports.VERSIONINFO = "WMI interface (wmi.js) version 0.1.2";
exports.global = global;
exports.require = global.require;

View File

@ -261,8 +261,10 @@ var test_implements = {
"system_get_network_interfaces": function() {
var SYS = require("lib/system");
var net_interfaces = SYS.getNetworkInterfaces();
console.log(JSON.stringify(net_interfaces));
var netInterfaces = SYS.getNetworkInterfaces();
netInterfaces.forEach(function(x) {
console.log(x.Caption);
});
},
"system_get_process_list": function() {
@ -270,7 +272,7 @@ var test_implements = {
var processList = SYS.getProcessList();
processList.forEach(function(x) {
console.log(x.Caption, x.ProcessID);
console.log(x.Caption, x.ProcessId);
});
},
@ -279,7 +281,7 @@ var test_implements = {
var processList = SYS.getProcessListByName("explorer.exe");
processList.forEach(function(x) {
console.log(x.Caption, x.ProcessID);
console.log(x.Caption, x.ProcessId);
});
},