Change variable naming convention, Remove dupilicate code (std.js)

This commit is contained in:
Namhyeon Go 2024-08-07 11:52:34 +09:00
parent 00868b708b
commit 3aa5a140d1

View File

@ -252,38 +252,6 @@ function addslashes(s) {
}; };
// Private APIs / Utility functions // 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);
@ -337,18 +305,18 @@ StdEvent.AT_TARGET = 2;
StdEvent.BUBBLING_PHASE = 3; // Not used but to be compatible StdEvent.BUBBLING_PHASE = 3; // Not used but to be compatible
function StdEventTarget() { function StdEventTarget() {
this.__events__ = []; this._events = [];
this.dispatchEvent = function(event, __exception__) { this.dispatchEvent = function(event, _exception) {
event.target = this; event.target = this;
event.isTrusted = false; event.isTrusted = false;
event.eventPhase = StdEvent.AT_TARGET; event.eventPhase = StdEvent.AT_TARGET;
event.currentTarget = event.target; event.currentTarget = event.target;
for (var i = 0; i < this.__events__.length; i++) { for (var i = 0; i < this._events.length; i++) {
var e = this.__events__[i]; var e = this._events[i];
if (e.type == event.type && typeof(e.listener) === "function") { if (e.type == event.type && typeof(e.listener) === "function") {
try { try {
e.listener(event, __exception__); e.listener(event, _exception);
} catch (ex) { } catch (ex) {
this.dispatchEvent(new StdEvent("error"), ex); this.dispatchEvent(new StdEvent("error"), ex);
} }
@ -358,12 +326,12 @@ function StdEventTarget() {
this.addEventListener = function(type, listener) { this.addEventListener = function(type, listener) {
if (typeof listener === "function") { if (typeof listener === "function") {
this.__events__.push({ this._events.push({
"type": type, "type": type,
"listener": listener, "listener": listener,
"counter": StdEventTarget.__counter__ "counter": StdEventTarget._counter
}); });
StdEventTarget.__counter__++; StdEventTarget._counter++;
} else { } else {
throw new TypeError("EventListener must be a function"); throw new TypeError("EventListener must be a function");
} }
@ -371,10 +339,10 @@ function StdEventTarget() {
this.removeEventListener = function(type, listener) { this.removeEventListener = function(type, listener) {
if (typeof listener === "function") { if (typeof listener === "function") {
for (var i = 0; i < this.__events__.length; i++) { for (var i = 0; i < this._events.length; i++) {
var e = this.__events__[i]; var e = this._events[i];
if (e.type == type && typeof(e.listener) === "function" && e.listener.toString() == listener.toString()) { if (e.type == type && typeof(e.listener) === "function" && e.listener.toString() == listener.toString()) {
delete this.__events__[i]; delete this._events[i];
} }
} }
} else { } else {
@ -382,67 +350,47 @@ function StdEventTarget() {
} }
}; };
}; };
StdEventTarget.__counter__ = 0; StdEventTarget._counter = 0;
/* // https://github.com/gnh1201/welsonjs/wiki/AsyncFunction
var a = new AsyncFunction(function() { function AsyncFunction(f) {
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);
} }
}; };
this.runSynchronously = function() { this.runSynchronously = function() {
return this.f.apply(null, arguments); 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);
}
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 +407,20 @@ 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 // https://github.com/gnh1201/welsonjs/wiki/GeneratorFunction
function GeneratorFunction(f) { function GeneratorFunction(f) {
var _lastState = 0; var _lastState = 0;
var _state = 0; var _state = 0;
@ -516,45 +464,6 @@ 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() {
var data = {};
var 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.getItem = function(keyName) {
return data[keyName];
};
this.removeItem = function(keyName) {
delete data[keyName];
commit();
};
this.clear = function() {
data = {};
commit();
};
}
global.GetResource = GetResource; global.GetResource = GetResource;
global.sleep = sleep; global.sleep = sleep;
global.repeat = repeat; global.repeat = repeat;
@ -564,6 +473,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;
@ -572,7 +482,7 @@ exports.Storage = StdStorage;
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.9.0";
exports.AUTHOR = "abuse@catswords.net"; exports.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;