From 5ec537e142797bc8f841fc18a0d3bca0b7fd8d4f Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Wed, 14 Aug 2024 14:00:02 +0900 Subject: [PATCH] Update std.js --- lib/std.js | 129 +++++++++++++++++------------------------------------ 1 file changed, 42 insertions(+), 87 deletions(-) diff --git a/lib/std.js b/lib/std.js index c593f31..4896f57 100644 --- a/lib/std.js +++ b/lib/std.js @@ -251,39 +251,6 @@ function addslashes(s) { replace(/"/g, '\\"'); }; -// Private APIs / Utility functions - -// Emulate Server.CreateObject -function CreateObject(progId, serverName, callback) { - var progIds = []; - var _CreateObject = function(p, s) { - if (typeof WScript !== "undefined") { - return WScript.CreateObject(p, s); - } else if (typeof ActiveXObject !== "undefined") { - return new ActiveXObject(p); - } - return null; - }; - - if (typeof progId == "object") { - progIds = progId; - } else { - progIds.push(progId); - } - - for (var i = 0; i < progIds.length; i++) { - try { - var obj = _CreateObject(progIds[i], serverName); - if (typeof callback === "function") { - callback(obj, progIds[i]); - } - return obj; - } catch (e) { - console.error(e.message); - }; - } -} - function alert(message) { if (typeof window !== "undefined") { window.alert(message); @@ -301,7 +268,6 @@ function confirm(message) { } // Standard Event Object -// https://developer.mozilla.org/ko/docs/Web/API/Event function StdEvent(type) { this.defaultPrevented = false; this.timeStamp = new Date(); @@ -336,6 +302,7 @@ StdEvent.CAPTURING_PHASE = 1; // Not used but to be compatible StdEvent.AT_TARGET = 2; StdEvent.BUBBLING_PHASE = 3; // Not used but to be compatible +// Standard EventTarget Object function StdEventTarget() { this.__events__ = []; @@ -384,46 +351,35 @@ function StdEventTarget() { }; StdEventTarget.__counter__ = 0; -/* -var a = new AsyncFunction(function() { - console.log("calling A"); -}); - -var _async_b = function(function() { - console.log("calling B"); -}); - -function main(args) { - AsyncFunction.bind(this, args); - console.log("welcome"); -} - -exports.a = a; -exports._async_b = _async_b; -*/ - -function AsyncFunction(f, __filename) { +function AsyncFunction(f) { this.f = f; - this.__filename = __filename; + this.__filename = AsyncFunction.__filename__; this.run = function() { var args = Array.from(arguments); // increase number of async functions - AsyncFunction.counter++; + AsyncFunction.__counter__++; // decrease number of async functions - var _this = this; - var _f = function() { - if (typeof _this.f === "function") _this.f(); - AsyncFunction.counter--; + var dispatch = function(_args, _f) { + if (typeof _f === "function") _f.apply(null, _args); + AsyncFunction._counter--; }; // CLI or Window? if (typeof WScript !== "undefined") { - require("lib/shell").show(["cscript", "app.js", this.__filename, "/async", f].concat(args)); + (function(_args, SHELL) { + SHELL.show(["cscript", "app.js", this.__filename, "/async", f].concat(_args)); + })(args, require("lib/shell")); + } else if (typeof window !== "undefined") { + (function(_args, _f) { + window.setTimeout(function() { + dispatch(_args, _f); + }, 1); + })(args, this.f); } else { - sleep(1, _f); + dispatch(args, this.f); } }; @@ -431,6 +387,15 @@ function AsyncFunction(f, __filename) { return this.f.apply(null, arguments); }; + if (typeof this.__filename === "string") { + this.__filename = __filename; + } else if (typeof WScript !== "undefined") { + this.__filename = (function(path) { + var pos = Math.max.apply(null, [path.lastIndexOf("\\"), path.lastIndexOf("/")]); + return (pos > -1 ? path.substring(pos + 1) : ""); + })(WScript.ScriptFullName); + } + if (typeof this.__filename === "string") { this.__filename = __filename; } else if (typeof WScript !== "undefined") { @@ -443,6 +408,7 @@ function AsyncFunction(f, __filename) { AsyncFunction.__counter__++; }; AsyncFunction.__counter__ = 0; +AsyncFunction.__filename__ = "bootstrap.js"; AsyncFunction.Initialize = function(exports, args) { if (args.length < 2) return; @@ -459,20 +425,19 @@ AsyncFunction.Initialize = function(exports, args) { } } - throw new AsyncFunction.Resolved("Resolved"); + throw new AsyncFunction.Initialized("Initialized"); }; AsyncFunction.bind = function(exports, args) { // compatible under 0.2.7.31 console.warn("AsyncFunction.bind() is deprecated. Use AsyncFunction.Initialize()"); return AsyncFunction.Initialize(exports, args); }; -AsyncFunction.Resolved = function(message) { - this.name = "AsyncFunction.Resolved"; +AsyncFunction.Initialized = function(message) { + this.name = "AsyncFunction.Initialized"; this.message = message; }; -AsyncFunction.Resolved.prototype = new Error(); -AsyncFunction.Resolved.prototype.constructor = AsyncFunction.Resolved; +AsyncFunction.Initialized.prototype = new Error(); +AsyncFunction.Initialized.prototype.constructor = AsyncFunction.Initialized; -// [app] Transpiling ES6 generator functions #75 function GeneratorFunction(f) { var _lastState = 0; var _state = 0; @@ -516,42 +481,31 @@ GeneratorFunction.Yield = function(message) { GeneratorFunction.Yield.prototype = new Error(); GeneratorFunction.Yield.prototype.constructor = GeneratorFunction.Yield; -/* -var a = new GeneratorFunction(function(_yield) { - _yield("a"); - _yield("b"); - _yield("c"); -}); -console.log(a.next().value); -console.log(a.next().value); -console.log(a.next().value); -*/ - function StdStorage() { - var data = {}; - var commit = function() { + this.data = {}; + this.length = 0; + + this.commit = function() { this.length = Object.keys(data).length; }; - - this.length = 0; this.key = function(idx) { var keyName = Object.keys(data)[idx]; return data[keyName]; }; this.setItem = function(keyName, keyValue) { data[keyName] = keyValue; - commit(); + this.commit(); }; this.getItem = function(keyName) { return data[keyName]; }; this.removeItem = function(keyName) { delete data[keyName]; - commit(); + this.commit(); }; this.clear = function() { - data = {}; - commit(); + this.data = {}; + this.commit(); }; } @@ -564,6 +518,7 @@ global.CHR = CHR; global.splitLn = splitLn; global.addslashes = addslashes; global.AsyncFunction = AsyncFunction; +global.GeneratorFunction = GeneratorFunction; exports.Event = StdEvent; exports.EventTarget = StdEventTarget; @@ -571,7 +526,7 @@ exports.EventTarget = StdEventTarget; exports.alert = alert; exports.confirm = confirm; -exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.7"; +exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.9"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require;