welsonjs/lib/std.js

92 lines
2.9 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.
//
// References:
2020-06-28 14:22:57 +00:00
// * https://github.com/redskyit/wsh-appjs
// * https://github.com/JSman-/JS-Framework
//
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
// 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
}
}
// ECMAScript 5 compatibility shims for legacy (and modern) JavaScript engines
require("node_modules/es5-shim/es5-shim");
require("node_modules/es5-shim/es5-sham");
2020-07-19 22:05:42 +00:00
// JSON 3 was a JSON polyfill for older JavaScript platforms
require("node_modules/json3/lib/json3");
// ECMAScript 6 compatibility shims for legacy JS engines
require("node_modules/es6-shim/es6-shim");
require("node_modules/es6-shim/es6-sham");
/////////////////////////////////////////////////////////////////////////////////
// Global APIs
/////////////////////////////////////////////////////////////////////////////////
2020-06-28 16:37:15 +00:00
global.GetResource = function(ResourceName) {
2020-06-28 14:22:57 +00:00
return arguments.callee.caller.GetResource(ResourceName);
}
global.sleep = function(ms, callback) {
2020-07-03 09:15:23 +00:00
if (typeof(WScript) !== "undefined") {
2020-06-28 16:37:15 +00:00
WScript.Sleep(ms);
2020-07-03 09:15:23 +00:00
if (typeof(callback) === "function") {
2020-06-28 16:37:15 +00:00
callback();
}
} else {
2020-07-03 09:15:23 +00:00
if (typeof(callback) === "function") {
2020-06-28 16:37:15 +00:00
setTimeout(callback, ms);
}
2020-06-28 14:22:57 +00:00
}
}
global.exit = function() {
2020-07-03 09:15:23 +00:00
if (typeof(WScript) !== "undefined") {
2020-06-28 16:37:15 +00:00
WScript.Quit();
2020-07-03 09:15:23 +00:00
} else if (typeof(window) !== "undefined") {
2020-06-28 16:37:15 +00:00
window.close();
}
2020-06-28 14:22:57 +00:00
}
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////
exports.VERSIONINFO = "Standard Lib (std.js) version 0.2";
exports.global = global;
exports.require = global.require;
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Emulate Server.CreateObject
/////////////////////////////////////////////////////////////////////////////////
exports.CreateObject = function(n) {
2020-07-03 09:15:23 +00:00
return new ActiveXObject(n);
};
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////