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