welsonjs/lib/std.js

307 lines
8.4 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
/////////////////////////////////////////////////////////////////////////////////
2022-02-08 09:13:10 +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
}
}
2022-02-08 09:13:10 +00:00
// MS JScript Enumerator to Array
2020-11-13 08:44:58 +00:00
if (!Enumerator.prototype.toArray) {
Enumerator.prototype.toArray = function() {
2022-02-08 09:13:10 +00:00
var a = [];
2020-11-13 08:44:58 +00:00
for (; !this.atEnd(); this.moveNext()) {
2022-02-08 09:13:10 +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 {
var i = 0, d = [];
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-08 09:13:10 +00:00
return a;
2020-11-13 08:44:58 +00:00
};
}
/////////////////////////////////////////////////////////////////////////////////
// Global APIs
/////////////////////////////////////////////////////////////////////////////////
2022-02-08 10:48:42 +00:00
function GetResource(ResourceName) {
2020-06-28 14:22:57 +00:00
return arguments.callee.caller.GetResource(ResourceName);
}
2022-02-08 08:49:30 +00:00
// [lib/std] the time of `sleep()' function is not accuracy #34
2022-02-08 10:48:42 +00:00
function sleep(ms, callback) {
2022-02-08 09:10:10 +00:00
var handler = null;
2022-02-08 09:09:29 +00:00
2022-02-08 08:49:30 +00:00
var cur = Date.now();
var end = cur + ms;
2022-02-08 09:09:29 +00:00
if (typeof WScript !== "undefined") {
/*
2022-02-08 08:49:30 +00:00
while (cur < end) {
2022-02-08 08:53:28 +00:00
//WScript.Sleep(1);
2022-02-08 08:49:30 +00:00
cur = Date.now();
2020-06-28 16:37:15 +00:00
}
2022-02-08 08:50:02 +00:00
end = Date.now();
2022-02-08 09:09:29 +00:00
*/
2022-02-08 08:49:30 +00:00
2022-02-08 09:09:29 +00:00
WScript.Sleep(ms);
if (typeof callback === "function")
2022-02-08 08:49:30 +00:00
callback()
;
2022-02-08 09:09:29 +00:00
} else if (typeof window !== "undefined") {
if (typeof callback === "function")
handler = setTimeout(callback, ms);
;
}
return { 'ms': end, 'handler': handler };
};
2022-02-08 10:48:42 +00:00
function repeat(target, callback, onError) {
switch (typeof target) {
case "number":
2022-02-08 10:58:39 +00:00
case "boolean":
2022-02-08 10:48:42 +00:00
var ms = target;
2022-02-08 10:58:39 +00:00
var i = 0;
2022-02-08 11:08:33 +00:00
var result = null;
2022-02-08 10:48:42 +00:00
var handler = null;
var cur = Date.now();
var end = cur + ms;
if (typeof WScript !== "undefined") {
while (ms === true ? true : (cur < end)) {
try {
if (typeof callback === "function")
2022-02-08 11:08:33 +00:00
var result = callback(i);
if (typeof result === "number") {
i += result;
} else if (result === false) {
break;
}
2022-02-08 10:48:42 +00:00
;
} catch (e) {
if (typeof onError === "function")
2022-02-08 11:08:33 +00:00
if (onError(e) === false)
break
;
2022-02-08 10:48:42 +00:00
;
}
cur = Date.now();
}
end = Date.now();
} else if (typeof window !== "undefined") {
if (typeof callback === "function")
handler = setInterval(callback, ms);
;
}
2022-02-08 09:55:29 +00:00
2022-02-08 10:48:42 +00:00
return { 'ms': end, 'handler': handler };
case "object":
var arr = target;
for (var i = 0; i < arr.length; i++) {
try {
if (typeof callback === "function")
2022-02-08 11:08:33 +00:00
if (callback(i, arr[i]) === false)
break
;
2022-02-08 10:48:42 +00:00
;
} catch (e) {
if (typeof onError === "function")
2022-02-08 11:08:33 +00:00
if (onError(e) === false)
break
;
2022-02-08 10:48:42 +00:00
;
}
}
break;
}
};
2022-02-08 09:09:29 +00:00
2022-02-08 10:48:42 +00:00
function rotate(target, callback, onError) {
var arr = target;
var i = 0;
var stop = false;
while (!stop) {
try {
if (typeof callback === "function") {
2022-02-08 11:43:02 +00:00
stop = callback(i, arr[i]);
2022-02-08 10:48:42 +00:00
} else {
stop = true;
2022-02-08 09:21:15 +00:00
}
2022-02-08 10:48:42 +00:00
} catch (e) {
if (typeof onError === "function")
2022-02-08 11:43:02 +00:00
stop = onError(e);
2022-02-08 10:48:42 +00:00
;
2022-02-08 09:10:10 +00:00
}
2022-02-08 08:49:30 +00:00
2022-02-08 10:48:42 +00:00
i++;
i = i % keywords.length;
}
2022-01-08 14:12:59 +00:00
};
2020-06-28 14:22:57 +00:00
2022-02-08 10:48:42 +00:00
function range() {
2022-02-08 09:55:29 +00:00
var args = arguments;
var N = [], start, end, step;
switch(args.length) {
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;
}
for (var i = start; i < end; i = i + step)
N.push(i)
;
return N;
};
2022-02-08 10:48:42 +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-09 02:31:17 +00:00
function splitLn(s) {
return s.split(/\r?\n/);
};
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// Emulate Server.CreateObject
/////////////////////////////////////////////////////////////////////////////////
2022-02-08 10:48:42 +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-02-08 10:48:42 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Standard Event Object
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
2020-11-14 13:16:47 +00:00
2022-02-08 10:48:42 +00:00
function StdEvent(eventName) {
2022-01-08 14:12:59 +00:00
this.defaultPrevented = false;
this.timeStamp = new Date();
this.eventName = eventName;
this.target = null;
this.preventDefault = function() {
this.defaultPrevented = true;
};
};
2022-02-08 10:48:42 +00:00
function StdEventableObject() {
2022-01-08 14:12:59 +00:00
this.dispatchEvent = function(event) {
event.target = this;
2022-01-09 05:33:21 +00:00
if(('on' + event.eventName) in this) this['on' + event.eventName](event);
2022-01-08 14:12:59 +00:00
};
this.addEventListener = function(eventName, fn) {
if (typeof(fn) == "function") {
this['on' + eventName] = fn;
} else {
throw new TypeError("EventListener must be a function");
}
};
};
2022-02-08 10:48:42 +00:00
global.GetResource = GetResource;
global.sleep = sleep;
global.repeat = repeat;
global.rotate = rotate;
global.range = range;
global.CHR = CHR;
2022-02-09 02:31:17 +00:00
global.splitLn = splitLn;
2022-02-08 10:48:42 +00:00
2022-02-08 09:13:10 +00:00
exports.VERSIONINFO = "Standard Lib (std.js) version 0.4";
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;
exports.EventableObject = StdEventableObject;