Update std.js

This commit is contained in:
Namhyeon Go 2024-08-14 14:00:02 +09:00
parent 243db3edaa
commit 5ec537e142

View File

@ -251,39 +251,6 @@ function addslashes(s) {
replace(/"/g, '\\"'); 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) { function alert(message) {
if (typeof window !== "undefined") { if (typeof window !== "undefined") {
window.alert(message); window.alert(message);
@ -301,7 +268,6 @@ function confirm(message) {
} }
// Standard Event Object // Standard Event Object
// https://developer.mozilla.org/ko/docs/Web/API/Event
function StdEvent(type) { function StdEvent(type) {
this.defaultPrevented = false; this.defaultPrevented = false;
this.timeStamp = new Date(); this.timeStamp = new Date();
@ -336,6 +302,7 @@ StdEvent.CAPTURING_PHASE = 1; // Not used but to be compatible
StdEvent.AT_TARGET = 2; StdEvent.AT_TARGET = 2;
StdEvent.BUBBLING_PHASE = 3; // Not used but to be compatible StdEvent.BUBBLING_PHASE = 3; // Not used but to be compatible
// Standard EventTarget Object
function StdEventTarget() { function StdEventTarget() {
this.__events__ = []; this.__events__ = [];
@ -384,46 +351,35 @@ function StdEventTarget() {
}; };
StdEventTarget.__counter__ = 0; StdEventTarget.__counter__ = 0;
/* function AsyncFunction(f) {
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) {
this.f = f; this.f = f;
this.__filename = __filename; this.__filename = AsyncFunction.__filename__;
this.run = function() { this.run = function() {
var args = Array.from(arguments); var args = Array.from(arguments);
// increase number of async functions // increase number of async functions
AsyncFunction.counter++; AsyncFunction.__counter__++;
// decrease number of async functions // decrease number of async functions
var _this = this; var dispatch = function(_args, _f) {
var _f = function() { if (typeof _f === "function") _f.apply(null, _args);
if (typeof _this.f === "function") _this.f(); AsyncFunction._counter--;
AsyncFunction.counter--;
}; };
// CLI or Window? // CLI or Window?
if (typeof WScript !== "undefined") { 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 { } else {
sleep(1, _f); dispatch(args, this.f);
} }
}; };
@ -440,9 +396,19 @@ function AsyncFunction(f, __filename) {
})(WScript.ScriptFullName); })(WScript.ScriptFullName);
} }
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);
}
AsyncFunction.__counter__++; AsyncFunction.__counter__++;
}; };
AsyncFunction.__counter__ = 0; AsyncFunction.__counter__ = 0;
AsyncFunction.__filename__ = "bootstrap.js";
AsyncFunction.Initialize = function(exports, args) { AsyncFunction.Initialize = function(exports, args) {
if (args.length < 2) if (args.length < 2)
return; 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 AsyncFunction.bind = function(exports, args) { // compatible under 0.2.7.31
console.warn("AsyncFunction.bind() is deprecated. Use AsyncFunction.Initialize()"); console.warn("AsyncFunction.bind() is deprecated. Use AsyncFunction.Initialize()");
return AsyncFunction.Initialize(exports, args); return AsyncFunction.Initialize(exports, args);
}; };
AsyncFunction.Resolved = function(message) { AsyncFunction.Initialized = function(message) {
this.name = "AsyncFunction.Resolved"; this.name = "AsyncFunction.Initialized";
this.message = message; this.message = message;
}; };
AsyncFunction.Resolved.prototype = new Error(); AsyncFunction.Initialized.prototype = new Error();
AsyncFunction.Resolved.prototype.constructor = AsyncFunction.Resolved; AsyncFunction.Initialized.prototype.constructor = AsyncFunction.Initialized;
// [app] Transpiling ES6 generator functions #75
function GeneratorFunction(f) { function GeneratorFunction(f) {
var _lastState = 0; var _lastState = 0;
var _state = 0; var _state = 0;
@ -516,42 +481,31 @@ GeneratorFunction.Yield = function(message) {
GeneratorFunction.Yield.prototype = new Error(); GeneratorFunction.Yield.prototype = new Error();
GeneratorFunction.Yield.prototype.constructor = GeneratorFunction.Yield; 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() { function StdStorage() {
var data = {}; this.data = {};
var commit = function() { this.length = 0;
this.commit = function() {
this.length = Object.keys(data).length; this.length = Object.keys(data).length;
}; };
this.length = 0;
this.key = function(idx) { this.key = function(idx) {
var keyName = Object.keys(data)[idx]; var keyName = Object.keys(data)[idx];
return data[keyName]; return data[keyName];
}; };
this.setItem = function(keyName, keyValue) { this.setItem = function(keyName, keyValue) {
data[keyName] = keyValue; data[keyName] = keyValue;
commit(); this.commit();
}; };
this.getItem = function(keyName) { this.getItem = function(keyName) {
return data[keyName]; return data[keyName];
}; };
this.removeItem = function(keyName) { this.removeItem = function(keyName) {
delete data[keyName]; delete data[keyName];
commit(); this.commit();
}; };
this.clear = function() { this.clear = function() {
data = {}; this.data = {};
commit(); this.commit();
}; };
} }
@ -564,6 +518,7 @@ global.CHR = CHR;
global.splitLn = splitLn; global.splitLn = splitLn;
global.addslashes = addslashes; global.addslashes = addslashes;
global.AsyncFunction = AsyncFunction; global.AsyncFunction = AsyncFunction;
global.GeneratorFunction = GeneratorFunction;
exports.Event = StdEvent; exports.Event = StdEvent;
exports.EventTarget = StdEventTarget; exports.EventTarget = StdEventTarget;
@ -571,7 +526,7 @@ exports.EventTarget = StdEventTarget;
exports.alert = alert; exports.alert = alert;
exports.confirm = confirm; 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.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;