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

View File

@ -58,10 +58,6 @@ var WMIQueryObject = function() {
return this.cursor.toArray(); return this.cursor.toArray();
}; };
this.fetchAll2 = function() {
return this.cursor.toArray2();
};
this.get = function(key) { this.get = function(key) {
if (key in this.current) { if (key in this.current) {
return this.current[key]; return this.current[key];
@ -162,6 +158,6 @@ exports.create = create;
exports.execQuery = execQuery; exports.execQuery = execQuery;
exports.setClass = setClass; 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.global = global;
exports.require = global.require; exports.require = global.require;

View File

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