Update app.js

This commit is contained in:
Namhyeon Go 2024-07-23 00:22:01 +09:00
parent debc806516
commit 1e9533a9a5

77
app.js
View File

@ -43,15 +43,21 @@
// must define main = function(args) {}, which is called once the module is // must define main = function(args) {}, which is called once the module is
// loaded. // loaded.
// //
var exit = function(status) { var exit = function(status) {
if (status == 0) return;
console.error("Exit", status, "caused"); console.error("Exit", status, "caused");
if (typeof WScript !== "undefined") { if (typeof WScript !== "undefined") {
WScript.Quit(status); WScript.Quit(status);
return;
} else if (typeof window !== "undefined") { } else if (typeof window !== "undefined") {
window.close(); window.close();
return;
} }
// to exit completely
throw new Error("Exit " + status + " caused");
}; };
var console = { var console = {
@ -213,7 +219,7 @@ if (typeof CreateObject === "undefined") {
* @FN {string} The name of the file. * @FN {string} The name of the file.
*/ */
function __evalFile(FN) { function __evalFile(FN) {
if (FN.substr(FN.length - 3) !== '.js') FN += ".js"; if (FN.substring(FN.length - 3) !== '.js') FN += ".js";
return eval(require._load(FN)); return eval(require._load(FN));
} }
@ -223,7 +229,7 @@ function __evalFile(FN) {
function require(pathname) { function require(pathname) {
var cache = require._cache = require._cache || {}; var cache = require._cache = require._cache || {};
var suffix = (function(pos, s) { var suffix = (function(pos, s) {
return pos < 0 ? '.' : s.substr(pos); return pos < 0 ? '.' : s.substring(pos);
})(pathname.lastIndexOf('.'), pathname); })(pathname.lastIndexOf('.'), pathname);
var FN = pathname; var FN = pathname;
@ -300,7 +306,7 @@ function require(pathname) {
// check the suffix again // check the suffix again
suffix = (function(pos, s) { suffix = (function(pos, s) {
return pos < 0 ? '.' : s.substr(pos); return pos < 0 ? '.' : s.substring(pos);
})(_filename.lastIndexOf('.'), _filename); })(_filename.lastIndexOf('.'), _filename);
} }
@ -398,19 +404,23 @@ require._getDirName = function(path) {
return (pos > -1 ? path.substring(0, pos) : ""); return (pos > -1 ? path.substring(0, pos) : "");
}; };
require._getCurrentScriptDirectory = function() { require._getCurrentScriptDirectory = function() {
if (typeof WScript !== "undefined") { try {
if ("ScriptFullName" in WScript) { if (typeof WScript !== "undefined") {
return require._getDirName(WScript.ScriptFullName); if ("ScriptFullName" in WScript) {
return require._getDirName(WScript.ScriptFullName);
} else {
throw new Error("No detected an absolute path.");
}
} else if (typeof document !== "undefined") {
return require._getDirName(document.location.pathname);
} else { } else {
console.warn("Could not resolve an absolute path. Use the relative path."); throw new Error("No detected an absolute path.");
return ".";
} }
} else if (typeof document !== "undefined") { } catch (e) {
return require._getDirName(document.location.pathname); console.warn(e.message, "Use the relative path.");
} else {
console.warn("Could not resolve an absolute path. Use the relative path.");
return ".";
} }
return ".";
}; };
require._load = function(FN) { require._load = function(FN) {
// if empty // if empty
@ -469,7 +479,7 @@ require._msie9 = function(FN, params, callback) {
}; };
require._modernie = function(FN, params, callback) { require._modernie = function(FN, params, callback) {
if (typeof FN !== "string" || FN == null) FN = ''; if (typeof FN !== "string" || FN == null) FN = '';
else if (FN.substr(FN.length - 3) !== '.js') FN += ".js"; else if (FN.substring(FN.length - 3) !== '.js') FN += ".js";
var exports = null; var exports = null;
try { try {
@ -525,8 +535,8 @@ require._addScriptProvider = function(f) {
function initializeConsole() { function initializeConsole() {
if (typeof WScript === "undefined") { if (typeof WScript === "undefined") {
console.error("Microsoft JScript or Chakra runtime not detected"); console.error("This is not a console application");
exit(1); return;
} }
var argl = WScript.arguments.length; var argl = WScript.arguments.length;
@ -555,7 +565,7 @@ function initializeConsole() {
function initializeWindow(name, args, w, h) { function initializeWindow(name, args, w, h) {
if (typeof window === "undefined") { if (typeof window === "undefined") {
console.error("This is not a GUI application"); console.error("This is not a GUI application");
exit(1); return;
} }
var app = require(name); var app = require(name);
@ -568,16 +578,16 @@ function initializeWindow(name, args, w, h) {
if (app) { if (app) {
if (app.main) { if (app.main) {
var status = app.main.call(app, args); var status = app.main.call(app, args);
if (status > 0) { if (status > -1) {
exit(status); exit(status);
} }
} else { } else {
console.error("Missing main entry point in", name + ".js"); console.error("Missing main entry point in", name + ".js");
exit(1); return;
} }
} else { } else {
console.error("Could not find", name + ".js"); console.error("Could not find", name + ".js");
exit(1); return;
} }
} }
@ -586,26 +596,43 @@ function initializeService(name, eventType) {
// load the service // load the service
if (app) { if (app) {
(function(action) { return (function(action) {
if (eventType in action) { if (eventType in action) {
try { try {
var f = action[eventType]; var f = action[eventType];
if (typeof f === "function") f(); if (typeof f === "function") return f();
} catch (e) { } catch (e) {
console.error("Exception:", e.message); console.error("Exception:", e.message);
} }
} }
})({ })({
start: app.onServiceStart, start: app.onServiceStart,
stop: app.onServicsStop, stop: app.onServiceStop,
elapsedTime: app.onServiceElapsedTime elapsedTime: app.onServiceElapsedTime
}); });
} else { } else {
console.error("Could not find", name + ".js"); console.error("Could not find", name + ".js");
exit(1); return;
} }
} }
// Date.prototype.toISOString() polyfill for MSScriptControl.ScriptControl
if (!Date.prototype.toISOString) {
Date.prototype.toISOString = function() {
var pad = function(number) {
return number < 10 ? ('0' + number) : number;
};
return this.getUTCFullYear() +
'-' + pad(this.getUTCMonth() + 1) +
'-' + pad(this.getUTCDate()) +
'T' + pad(this.getUTCHours()) +
':' + pad(this.getUTCMinutes()) +
':' + pad(this.getUTCSeconds()) +
'.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) +
'Z';
};
}
// JSON 2 // JSON 2
if (typeof JSON === "undefined") { if (typeof JSON === "undefined") {
__evalFile("app/assets/js/json2"); __evalFile("app/assets/js/json2");