welsonjs/lib/std.js

546 lines
15 KiB
JavaScript
Raw Normal View History

2020-06-28 14:22:57 +00:00
//////////////////////////////////////////////////////////////////////////////////
//
// std.js
//
// Common routines. Defines LIB object which contains the API, as well as
// a global DBG function.
//
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Polyfills
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
if (!Function.prototype.GetResource) {
2020-06-28 16:37:15 +00:00
Function.prototype.GetResource = function(ResourceName) {
2020-07-03 09:15:23 +00:00
if (!this.Resources) {
var UnNamedResourceIndex = 0,
_this = this;
2020-06-28 16:37:15 +00:00
this.Resources = {};
2020-07-03 09:15:23 +00:00
function f(match, resType, Content) {
_this.Resources[(resType == "[[") ? UnNamedResourceIndex++ : resType.slice(1, -1)] = Content;
2020-06-28 16:37:15 +00:00
}
this.toString().replace(/\/\*(\[(?:[^\[]+)?\[)((?:[\r\n]|.)*?)\]\]\*\//gi, f);
2020-06-28 14:22:57 +00:00
}
2020-07-03 09:15:23 +00:00
2020-06-28 16:37:15 +00:00
return this.Resources[ResourceName];
2020-06-28 14:22:57 +00:00
}
}
2020-11-13 08:44:58 +00:00
if (!Enumerator.prototype.toArray) {
Enumerator.prototype.toArray = function() {
2022-04-11 02:08:40 +00:00
var items = [];
for (; !this.atEnd(); this.moveNext()) {
var item = this.item();
try {
items.push(item);
} catch (e) {}
}
return items;
};
}
if (!Enumerator.prototype.toArray2) {
Enumerator.prototype.toArray2 = function() {
2022-02-10 06:25:19 +00:00
var a = [];
2020-11-13 08:44:58 +00:00
for (; !this.atEnd(); this.moveNext()) {
2022-02-10 06:25:19 +00:00
var x = {};
var b = new Enumerator(this.item().Properties_);
for (; !b.atEnd(); b.moveNext()) {
var c = b.item();
if (typeof c.value !== "unknown") {
try {
x[c.name] = c.value.toString();
} catch (e) {
x[c.name] = c.value;
}
} else {
2023-03-14 04:41:49 +00:00
var i = 0,
d = [];
2022-02-10 06:25:19 +00:00
while (true) {
try {
d.push(c.value(i));
i++;
} catch (e) {
break;
}
}
x[c.name] = d;
}
}
a.push(x);
2020-11-13 08:44:58 +00:00
}
2022-02-10 06:25:19 +00:00
return a;
2020-11-13 08:44:58 +00:00
};
}
/////////////////////////////////////////////////////////////////////////////////
// Global APIs
/////////////////////////////////////////////////////////////////////////////////
2022-02-10 06:25:19 +00:00
function GetResource(ResourceName) {
2020-06-28 14:22:57 +00:00
return arguments.callee.caller.GetResource(ResourceName);
}
2022-02-10 06:25:19 +00:00
// [lib/std] the time of `sleep()' function is not accuracy #34
function sleep(ms, callback) {
var handler = null;
var cur = Date.now();
var end = cur + ms;
if (typeof WScript !== "undefined") {
while (cur < end) {
2022-10-10 18:01:24 +00:00
WScript.Sleep(1);
2022-02-10 06:25:19 +00:00
cur = Date.now();
}
2022-02-10 06:25:19 +00:00
end = Date.now();
2023-03-14 04:41:49 +00:00
2022-10-10 18:01:24 +00:00
//WScript.Sleep(ms);
2022-02-10 06:25:19 +00:00
if (typeof callback === "function")
2023-03-14 04:41:49 +00:00
callback();
2022-02-10 06:25:19 +00:00
} else if (typeof window !== "undefined") {
if (typeof callback === "function")
2023-03-14 04:41:49 +00:00
handler = setTimeout(callback, ms);;
2022-02-10 06:25:19 +00:00
}
2023-03-14 04:41:49 +00:00
return {
'ms': end,
'handler': handler
};
2022-02-10 06:25:19 +00:00
};
2022-10-21 01:46:30 +00:00
function repeat(target, callback, onError, onNextInterval, onNext) {
2022-02-10 06:25:19 +00:00
switch (typeof target) {
case "number":
case "boolean":
var ms = target;
var i = 0;
var result = null;
var handler = null;
var cur = Date.now();
var end = cur + ms;
if (typeof WScript !== "undefined") {
while (ms === true ? true : (cur < end)) {
try {
2022-02-20 22:17:48 +00:00
if (typeof callback === "function") {
2022-02-10 06:25:19 +00:00
var result = callback(i);
if (typeof result === "number") {
i += result;
} else if (result === false) {
break;
2022-02-20 22:17:48 +00:00
} else if (result === true) {
i += 1;
2022-02-10 06:25:19 +00:00
}
2022-02-20 22:17:48 +00:00
}
2022-02-10 06:25:19 +00:00
} catch (e) {
2022-02-20 22:17:48 +00:00
if (typeof onError === "function") {
2022-03-03 05:41:20 +00:00
if (onError(e, i) === false) {
2022-02-20 22:17:48 +00:00
break;
}
}
2022-02-10 06:25:19 +00:00
}
2022-10-10 18:01:24 +00:00
2022-10-21 01:46:30 +00:00
// if use onNextInterval method
2022-10-10 18:01:24 +00:00
if (typeof onNextInterval === "function") {
var nextInterval = onNextInterval();
if (typeof nextInterval === "number") {
var nextEnd = cur + nextInterval;
while (cur < nextEnd) {
WScript.Sleep(1);
cur = Date.now();
}
}
}
2022-10-21 01:46:30 +00:00
// if use onNext method
if (typeof onNext === "function") {
try {
onNext();
} catch (e) {}
}
// set the last time
2022-02-10 06:25:19 +00:00
cur = Date.now();
}
end = Date.now();
} else if (typeof window !== "undefined") {
2022-02-20 22:18:28 +00:00
if (typeof callback === "function") {
2022-02-10 06:25:19 +00:00
handler = setInterval(callback, ms);
2022-02-20 22:18:28 +00:00
}
2022-02-10 06:25:19 +00:00
}
2023-03-14 04:41:49 +00:00
return {
'ms': end, 'handler': handler
};
2022-02-10 06:25:19 +00:00
case "object":
var arr = target;
2022-02-10 08:16:59 +00:00
if (arr.length > 0) {
for (var i = 0; i < arr.length; i++) {
try {
if (typeof callback === "function")
if (callback(i, arr[i]) === false)
2023-03-14 04:41:49 +00:00
break;;
2022-02-10 08:16:59 +00:00
} catch (e) {
if (typeof onError === "function")
2022-03-03 05:41:20 +00:00
if (onError(e, i, arr[i]) === false)
2023-03-14 04:41:49 +00:00
break;;
2022-02-10 08:16:59 +00:00
}
2022-02-10 06:25:19 +00:00
}
}
break;
}
};
function rotate(target, callback, onError) {
var arr = target;
var i = 0;
var stop = false;
while (!stop) {
try {
if (typeof callback === "function") {
stop = callback(i, arr[i]);
} else {
stop = true;
}
} catch (e) {
if (typeof onError === "function")
2023-03-14 04:41:49 +00:00
stop = onError(e, i, arr[i]);;
2022-02-08 09:10:10 +00:00
}
2022-02-10 06:25:19 +00:00
i++;
2022-02-14 06:09:01 +00:00
i = i % target.length;
2022-02-10 06:25:19 +00:00
}
};
function range() {
var args = arguments;
2023-03-14 04:41:49 +00:00
var N = [],
start, end, step;
2022-02-10 06:25:19 +00:00
2023-03-14 04:41:49 +00:00
switch (args.length) {
2022-02-10 06:25:19 +00:00
case 3:
start = args[0];
end = args[1];
step = args[2];
break;
case 2:
start = args[0];
end = args[1];
step = 1;
break;
case 1:
start = 0;
end = args[0];
step = 1;
break;
2022-02-08 09:55:29 +00:00
}
2022-02-10 06:25:19 +00:00
for (var i = start; i < end; i = i + step)
2023-03-14 04:41:49 +00:00
N.push(i);
2022-02-10 06:25:19 +00:00
return N;
2022-02-08 09:55:29 +00:00
};
2022-02-10 06:25:19 +00:00
function CHR(ord) {
2020-11-04 07:30:52 +00:00
return String.fromCharCode(ord);
2022-01-08 14:12:59 +00:00
};
2020-10-26 12:01:05 +00:00
2022-02-10 06:25:19 +00:00
function splitLn(s) {
return s.split(/\r?\n/);
};
function addslashes(s) {
return s.toString().replace(/\\/g, '\\\\').
2023-03-14 04:41:49 +00:00
replace(/\u0008/g, '\\b').
replace(/\t/g, '\\t').
replace(/\n/g, '\\n').
replace(/\f/g, '\\f').
replace(/\r/g, '\\r').
replace(/'/g, '\\\'').
replace(/"/g, '\\"');
2022-02-10 06:25:19 +00:00
};
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Emulate Server.CreateObject
/////////////////////////////////////////////////////////////////////////////////
2022-02-10 06:25:19 +00:00
function CreateObject(progId, serverName, callback) {
2020-10-20 05:18:00 +00:00
var progIds = [];
var _CreateObject = function(p, s) {
if (typeof(WScript) !== "undefined") {
return WScript.CreateObject(p, s);
} else {
2020-11-10 09:13:41 +00:00
return new ActiveXObject(p);
2020-10-20 05:18:00 +00:00
}
};
2020-10-20 02:41:26 +00:00
2020-10-20 05:18:00 +00:00
if (typeof(progId) == "object") {
progIds = progId;
2020-10-20 02:41:26 +00:00
} else {
2020-10-20 05:18:00 +00:00
progIds.push(progId);
2020-10-20 02:41:26 +00:00
}
2020-10-20 05:18:00 +00:00
for (var i = 0; i < progIds.length; i++) {
try {
2020-11-10 09:13:41 +00:00
var obj = _CreateObject(progIds[i], serverName);
if (typeof(callback) === "function") {
callback(obj, progIds[i]);
}
return obj;
2020-11-04 07:30:52 +00:00
} catch (e) {
console.error(e.message);
};
2020-10-20 02:41:26 +00:00
}
2020-07-03 09:15:23 +00:00
};
2020-06-28 14:22:57 +00:00
2022-04-28 04:11:26 +00:00
function alert(msg) {
CreateObject("WScript.Shell").Popup(msg);
}
2022-02-10 06:25:19 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Standard Event Object
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
2020-11-14 13:16:47 +00:00
2022-05-10 02:45:02 +00:00
// https://developer.mozilla.org/ko/docs/Web/API/Event
function StdEvent(type) {
2022-01-08 14:12:59 +00:00
this.defaultPrevented = false;
this.timeStamp = new Date();
2022-05-10 02:45:02 +00:00
this.type = type;
2022-05-10 02:46:12 +00:00
this.isTrusted = true;
this.cancelable = true;
2022-01-08 14:12:59 +00:00
this.target = null;
2022-05-10 02:46:12 +00:00
this.currentTarget = null;
this.eventPhase = StdEvent.NONE;
2023-03-14 04:41:49 +00:00
this.bubbles = false; // Not used but to be compatible
this.composed = false; // Not used but to be compatible
2022-01-08 14:12:59 +00:00
this.preventDefault = function() {
this.defaultPrevented = true;
};
2022-05-10 02:45:02 +00:00
// Not used but to be compatible
2022-05-10 04:40:46 +00:00
this.initEvent = function(type, bubbles, cancelable) {
2022-05-10 02:45:02 +00:00
this.type = type;
this.bubbles = bubbles;
this.cancelable = cancelable;
};
// Not used but to be compatible
this.stopImmediatePropagation = function() {};
// Not used but to be compatible
this.stopPropagation = function() {};
2022-01-08 14:12:59 +00:00
};
2022-05-10 02:45:02 +00:00
StdEvent.NONE = 0;
2023-03-14 04:41:49 +00:00
StdEvent.CAPTURING_PHASE = 1; // Not used but to be compatible
2022-05-10 02:45:02 +00:00
StdEvent.AT_TARGET = 2;
2023-03-14 04:41:49 +00:00
StdEvent.BUBBLING_PHASE = 3; // Not used but to be compatible
2022-01-08 14:12:59 +00:00
2023-02-01 01:50:15 +00:00
function StdEventTarget() {
2022-09-18 19:44:58 +00:00
this.__events__ = [];
2022-09-18 20:11:55 +00:00
this.dispatchEvent = function(event, __exception__) {
2022-01-08 14:12:59 +00:00
event.target = this;
2022-05-10 02:45:02 +00:00
event.isTrusted = false;
event.eventPhase = StdEvent.AT_TARGET;
event.currentTarget = event.target;
2022-09-18 19:44:58 +00:00
for (var i = 0; i < this.__events__.length; i++) {
var e = this.__events__[i];
if (e.type == event.type && typeof(e.listener) === "function") {
2022-09-18 20:12:38 +00:00
try {
e.listener(event, __exception__);
} catch (ex) {
this.dispatchEvent(new StdEvent("error"), ex);
}
2022-09-18 19:44:58 +00:00
}
}
2022-01-08 14:12:59 +00:00
};
2022-09-18 19:44:58 +00:00
this.addEventListener = function(type, listener) {
2022-09-18 19:45:32 +00:00
if (typeof listener === "function") {
2022-09-18 19:44:58 +00:00
this.__events__.push({
"type": type,
"listener": listener,
2023-02-01 01:50:15 +00:00
"counter": StdEventTarget.__counter__
2022-09-18 19:44:58 +00:00
});
2023-02-01 01:50:15 +00:00
StdEventTarget.__counter__++;
2022-09-18 19:44:58 +00:00
} else {
throw new TypeError("EventListener must be a function");
}
};
this.removeEventListener = function(type, listener) {
2022-09-18 19:45:32 +00:00
if (typeof listener === "function") {
2022-09-18 19:44:58 +00:00
for (var i = 0; i < this.__events__.length; i++) {
var e = this.__events__[i];
2022-09-18 20:16:05 +00:00
if (e.type == type && typeof(e.listener) === "function" && e.listener.toString() == listener.toString()) {
2022-09-18 19:44:58 +00:00
delete this.__events__[i];
}
}
2022-01-08 14:12:59 +00:00
} else {
throw new TypeError("EventListener must be a function");
}
};
};
2023-02-01 01:50:15 +00:00
StdEventTarget.__counter__ = 0;
2022-01-08 14:12:59 +00:00
2023-03-14 04:50:26 +00:00
/*
var a = new AsyncFunction(function() {
2023-03-14 05:13:22 +00:00
console.log("calling A");
2023-03-14 04:50:26 +00:00
});
2023-03-14 05:13:22 +00:00
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;
2023-03-14 04:50:26 +00:00
*/
2023-03-14 05:13:22 +00:00
2023-03-14 04:50:26 +00:00
function AsyncFunction(f, __filename) {
2022-02-24 08:13:31 +00:00
this.f = f;
2023-03-14 04:50:26 +00:00
this.__filename = __filename;
2022-02-25 05:42:17 +00:00
2022-02-24 08:13:31 +00:00
this.run = function() {
2022-06-05 10:50:37 +00:00
var args = Array.from(arguments);
2022-05-09 08:55:47 +00:00
2022-06-05 10:50:37 +00:00
// increase number of async functions
AsyncFunction.counter++;
2023-03-14 04:41:49 +00:00
2022-06-05 10:50:37 +00:00
// decrease number of async functions
2022-05-09 08:55:47 +00:00
var _this = this;
2022-06-05 10:50:37 +00:00
var _f = function() {
if (typeof _this.f === "function") _this.f();
AsyncFunction.counter--;
2022-05-09 08:55:47 +00:00
};
2022-06-05 10:50:37 +00:00
// CLI or Window?
if (typeof WScript !== "undefined") {
2023-03-14 04:50:26 +00:00
require("lib/shell").show(["cscript", "app.js", this.__filename, f].concat(args));
2022-02-25 05:42:17 +00:00
} else {
2022-06-05 10:50:37 +00:00
sleep(1, _f);
2022-02-25 05:42:17 +00:00
}
2022-02-24 08:13:31 +00:00
};
2022-04-11 06:35:53 +00:00
2022-02-24 08:13:31 +00:00
this.runSynchronously = function() {
2022-02-25 05:42:17 +00:00
return this.f.apply(null, arguments);
2022-02-24 08:13:31 +00:00
};
2022-06-05 10:50:37 +00:00
};
2022-06-05 11:01:02 +00:00
AsyncFunction.counter = 0;
2022-06-05 10:50:37 +00:00
AsyncFunction.bind = function(exports, args) {
var result = false;
if (args.length > 0) {
2023-03-14 04:50:26 +00:00
var targets = [args[0], '_async_' + args[0]];
2022-06-05 10:50:37 +00:00
2023-03-14 04:50:26 +00:00
for (var i = 0; (i < targets.length && result == false); i++) {
var target = targets[i];
if (target in exports && (typeof exports[target] === "function" || exports[target] instanceof AsyncFunction)) {
try {
exports[target](args);
result = true;
} catch (e) {
2023-03-14 05:13:22 +00:00
console.error("Exception of", target, e.message);
2023-03-14 04:50:26 +00:00
}
2022-06-05 10:50:37 +00:00
}
2022-02-25 05:42:17 +00:00
}
}
2022-06-05 10:50:37 +00:00
2023-03-14 05:13:22 +00:00
throw new ReferenceError("AsyncFunction completed");
2022-02-24 08:13:31 +00:00
};
2023-03-10 08:46:50 +00:00
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");
}
throw new RangeError("OUT OF RANGE");
};
this.next = function() {
this._nextState = 0;
var go = true;
while (go) {
try {
this._f(this._yield);
} catch (e) {
go = (e.message == "CONTINUE");
this._nextState++;
}
}
2023-03-14 04:41:49 +00:00
2023-03-10 08:46:50 +00:00
this._done = (typeof this._callback !== "undefined" ? this._callback(this) : false);
return {
value: this._value,
done: this._done
};
};
this['return'] = function() {
// Not implemented
};
this['throw'] = function() {
// Not implemented
};
}
/*
new GeneratorFunction(function(yield) {
yield(1);
yield(2);
yield(3);
}, function(generator) {
return generator._value == 3;
});
*/
2022-02-10 06:25:19 +00:00
global.GetResource = GetResource;
global.sleep = sleep;
global.repeat = repeat;
global.rotate = rotate;
global.range = range;
global.CHR = CHR;
global.splitLn = splitLn;
global.addslashes = addslashes;
2022-02-24 08:13:31 +00:00
global.AsyncFunction = AsyncFunction;
2022-02-10 06:25:19 +00:00
2023-03-10 08:47:18 +00:00
exports.VERSIONINFO = "Standard Library (std.js) version 0.8.1";
2020-11-14 13:16:47 +00:00
exports.global = global;
exports.require = global.require;
2022-01-08 14:12:59 +00:00
2022-01-08 14:35:07 +00:00
exports.Event = StdEvent;
2023-02-01 01:50:15 +00:00
exports.EventTarget = StdEventTarget;
2022-04-29 07:11:49 +00:00
2022-04-28 04:11:26 +00:00
exports.alert = alert;