diff --git a/lib/std.js b/lib/std.js index 8ff4a09..69c7b93 100644 --- a/lib/std.js +++ b/lib/std.js @@ -3,9 +3,8 @@ // SPDX-License-Identifier: GPL-3.0-or-later // https://github.com/gnh1201/welsonjs // -// WelsonJS Standard Library +// WelsonJS Standard Library (Polyfills and shared functions) // -// Polyfills if (!Function.prototype.GetResource) { Function.prototype.GetResource = function(ResourceName) { if (!this.Resources) { @@ -23,50 +22,58 @@ if (!Function.prototype.GetResource) { } } -// The provided code snippet has been corrected by ChatGPT. -// https://chat.openai.com/share/eaab056c-d265-4ee3-b355-9f29176a9caa -// Related issues: #75 #42 #30 -if (typeof Enumerator !== "undefined") { - Enumerator.prototype.toArray = function() { - var result = []; - while (!this.atEnd()) { - var currentItem = this.item(); - var currentItemProperties = currentItem.Properties_; - var itemObject = {}; - - 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 arrayValues = []; - var index = 0; - while (true) { - try { - arrayValues.push(property.value(index)); - index++; - } catch (e) { - break; - } - } - itemObject[property.name] = arrayValues; - } - propertiesEnumerator.moveNext(); - } - result.push(itemObject); - this.moveNext(); - } - - return result; - }; -} - -// Global APIs function GetResource(ResourceName) { return arguments.callee.caller.GetResource(ResourceName); } +(function() { + var originalFrom = Array.from; + + function convertToArray(v) { + var result = []; + while (!v.atEnd()) { + var item = v.item(); + result.push(typeof item === "unknown" ? Array.from(item) : item); + v.moveNext(); + } + return result; + } + + function convertToObject(v) { + var obj = {}; + var props = new Enumerator(v.Properties_); + while (!props.atEnd()) { + var prop = props.item(); + obj[prop.name] = typeof prop.value === "unknown" ? + Array.from(prop.value) : + prop.value; + props.moveNext(); + } + return obj; + } + + Array.from = function(value) { + if (value && typeof value.item === "unknown") { + value = new Enumerator(value); + } + + if (value instanceof Enumerator) { + return value && value.Properties_ ? + convertToObject(value) : + convertToArray(value); + } + + return originalFrom ? originalFrom(value) : Array.prototype.slice.call(value); + }; + + Array.from.toString = function() { + return originalFrom.toString(); + }; + + // compatible under version 0.2.7.55 + Enumerator.prototype.toArray = Array.from; +})(); + // [lib/std] the time of `sleep()' function is not accuracy #34 function sleep(ms, callback) { var handler = null; @@ -623,7 +630,7 @@ exports.alert = alert; exports.confirm = confirm; exports.prompt = prompt; -exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.18"; +exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.19"; exports.AUTHOR = "gnh1201@catswords.re.kr"; exports.global = global; exports.require = global.require;