Update lib/std.js, lib/toolkit.js

This commit is contained in:
Namhyeon Go 2023-12-20 17:01:24 +09:00
parent 31b28f2945
commit 0d3052e659
2 changed files with 112 additions and 56 deletions

View File

@ -84,13 +84,15 @@ function sleep(ms, callback) {
cur = Date.now(); cur = Date.now();
} }
end = Date.now(); end = Date.now();
//WScript.Sleep(ms); //WScript.Sleep(ms);
if (typeof callback === "function")
if (typeof callback === "function") {
callback(); callback();
}
} else if (typeof window !== "undefined") { } else if (typeof window !== "undefined") {
if (typeof callback === "function") if (typeof callback === "function") {
handler = setTimeout(callback, ms);; handler = setTimeout(callback, ms);
}
} }
return { return {
@ -267,14 +269,15 @@ function addslashes(s) {
function CreateObject(progId, serverName, callback) { function CreateObject(progId, serverName, callback) {
var progIds = []; var progIds = [];
var _CreateObject = function(p, s) { var _CreateObject = function(p, s) {
if (typeof(WScript) !== "undefined") { if (typeof WScript !== "undefined") {
return WScript.CreateObject(p, s); return WScript.CreateObject(p, s);
} else { } else if (typeof ActiveXObject !== "undefined") {
return new ActiveXObject(p); return new ActiveXObject(p);
} }
return null;
}; };
if (typeof(progId) == "object") { if (typeof progId == "object") {
progIds = progId; progIds = progId;
} else { } else {
progIds.push(progId); progIds.push(progId);
@ -283,7 +286,7 @@ function CreateObject(progId, serverName, callback) {
for (var i = 0; i < progIds.length; i++) { for (var i = 0; i < progIds.length; i++) {
try { try {
var obj = _CreateObject(progIds[i], serverName); var obj = _CreateObject(progIds[i], serverName);
if (typeof(callback) === "function") { if (typeof callback === "function") {
callback(obj, progIds[i]); callback(obj, progIds[i]);
} }
return obj; return obj;
@ -291,10 +294,22 @@ function CreateObject(progId, serverName, callback) {
console.error(e.message); console.error(e.message);
}; };
} }
}; }
function alert(msg) { function alert(message) {
CreateObject("WScript.Shell").Popup(msg); if (typeof window !== "undefined") {
window.alert(message);
} else {
CreateObject("WScript.Shell").Popup(message);
}
}
function confirm(message) {
if (typeof window !== "undefined") {
return window.confirm(message);
} else {
CreateObject("WScript.Shell").Popup(message);
}
} }
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
@ -453,65 +468,102 @@ AsyncFunction.bind = function(exports, args) {
} }
} }
throw new ReferenceError("AsyncFunction completed"); throw new AsyncFunction.TaskDone("AsyncFunction completed");
}; };
AsyncFunction.TaskDone = function(message) {
this.name = "AsyncFunction.TaskDone";
this.message = message;
};
AsyncFunction.TaskDone.prototype = new Error();
AsyncFunction.TaskDone.prototype.constructor = AsyncFunction.TaskDone;
function GeneratorFunction(f, callback) { // [app] Transpiling ES6 generator functions #75
this._f = f; function GeneratorFunction(f) {
this._callback = callback; var _lastState = 0;
this._nextState = 0; var _state = 0;
this._state = 0; var _yield = function(value) {
this._value = undefined; _state++;
this._done = false; if (_state > _lastState) {
this._yield = function(value) { throw new GeneratorFunction.Yield(value);
if (this._nextState < this._state) {
throw new ReferenceError("CONTINUE");
} else {
this._value = value;
this._state++;
throw new ReferenceError("BREAK");
} }
throw new RangeError("OUT OF RANGE");
}; };
this.next = function() { this.next = function() {
this._nextState = 0;
var go = true; var go = true;
var value = undefined;
_state = 0;
while (go) { while (go) {
try { try {
this._f(this._yield); f(_yield);
} catch (e) { } catch (e) {
go = (e.message == "CONTINUE"); if (e instanceof GeneratorFunction.Yield) {
this._nextState++; value = e.message;
go = false;
_lastState = _state;
} else {
console.error(e.message);
}
} }
} }
this._done = (typeof this._callback !== "undefined" ? this._callback(this) : false);
return { return {
value: this._value, "value": value,
done: this._done "done": false
}
}; };
}
GeneratorFunction.Yield = function(message) {
this.name = "GeneratorFunction.Yield";
this.message = 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.length = Object.keys(data).length;
}; };
this['return'] = function() { this.length = 0;
// Not implemented this.key = function(idx) {
var keyName = Object.keys(data)[idx];
return data[keyName];
}; };
this.setItem = function(keyName, keyValue) {
this['throw'] = function() { data[keyName] = keyValue;
// Not implemented commit();
};
this.getItem = function(keyName) {
return data[keyName];
};
this.removeItem = function(keyName) {
delete data[keyName];
commit();
};
this.clear = function() {
data = {};
commit();
}; };
} }
/* /*
new GeneratorFunction(function(yield) { var a = new StdStorage();
yield(1); a.setItem("a", "1");
yield(2); console.log(a.getItem("a"));
yield(3);
}, function(generator) {
return generator._value == 3;
});
*/ */
global.GetResource = GetResource; global.GetResource = GetResource;
@ -524,11 +576,14 @@ global.splitLn = splitLn;
global.addslashes = addslashes; global.addslashes = addslashes;
global.AsyncFunction = AsyncFunction; global.AsyncFunction = AsyncFunction;
exports.VERSIONINFO = "Standard Library (std.js) version 0.8.2";
exports.global = global;
exports.require = global.require;
exports.Event = StdEvent; exports.Event = StdEvent;
exports.EventTarget = StdEventTarget; exports.EventTarget = StdEventTarget;
exports.Storage = StdStorage;
exports.alert = alert; exports.alert = alert;
exports.confirm = confirm;
exports.VERSIONINFO = "Standard Library (std.js) version 0.8.3";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = global.require;

View File

@ -10,8 +10,9 @@ var ToolkitObject = function() {
this.interface = CreateObject("WelsonJS.Toolkit"); this.interface = CreateObject("WelsonJS.Toolkit");
return this; return this;
} catch (e) { } catch (e) {
console.warn("WelsonJS.Toolkit is disabled"); console.info("WelsonJS.Toolkit not installed");
console.warn(e.message); console.info("It could be download from https://github.com/gnh1201/welsonjs");
console.error(e.message);
} }
}; };
@ -69,7 +70,7 @@ exports.alert = alert;
exports.confirm = confirm; exports.confirm = confirm;
exports.prompt = prompt; exports.prompt = prompt;
exports.VERSIONINFO = "WelsonJS.Toolkit API version 0.3"; exports.VERSIONINFO = "WelsonJS.Toolkit Native API version 0.3.1";
exports.AUTHOR = "abuse@catswords.net"; exports.AUTHOR = "abuse@catswords.net";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;