Merge pull request #307 from gnh1201/dev

Introduce `UseObject(progId, callback)` and more fixes
This commit is contained in:
Namhyeon Go 2025-08-05 17:51:47 +09:00 committed by GitHub
commit 9e3b4f1c3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 220 additions and 242 deletions

22
app.js
View File

@ -187,6 +187,24 @@ if (typeof CreateObject === "undefined") {
}; };
} }
if (typeof UseObject === "undefined") {
var UseObject = function(progId, callback) {
var _dispose = function(obj) {
try {
obj.Close();
} catch (e) { /* ignore */ }
};
var obj = CreateObject(progId);
try {
return callback(obj);
} finally {
_dispose(obj);
obj = null;
}
}
}
/** /**
* @FN {string} The name of the file. * @FN {string} The name of the file.
*/ */
@ -283,7 +301,9 @@ function require(pathname) {
return filename; return filename;
})({ })({
existsSync: function(filename) { existsSync: function(filename) {
return CreateObject("Scripting.FileSystemObject").FileExists(filename); return UseObject("Scripting.FileSystemObject", function(fso) {
return fso.FileExists(filename);
});
} }
}, { }, {
join: function() { join: function() {

View File

@ -8,207 +8,144 @@
var STD = require("lib/std"); var STD = require("lib/std");
var PipeIPC = require("lib/pipe-ipc"); var PipeIPC = require("lib/pipe-ipc");
///////////////////////////////////////////////////////////////////////////////// function fileExists(path) {
// fileExists return UseObject("Scripting.FileSystemObject", function(fso) {
///////////////////////////////////////////////////////////////////////////////// return fso.FileExists(path);
});
function fileExists(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var exists = FSO.FileExists(FN);
FSO = null;
return exists;
} }
///////////////////////////////////////////////////////////////////////////////// function folderExists(path) {
// folderExists return UseObject("Scripting.FileSystemObject", function(fso) {
///////////////////////////////////////////////////////////////////////////////// return fso.FolderExists(path);
});
function folderExists(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var exists = FSO.FolderExists(FN);
FSO = null;
return exists;
} }
///////////////////////////////////////////////////////////////////////////////// function fileGet(path) {
// fileGet return UseObject("Scripting.FileSystemObject", function(fso) {
///////////////////////////////////////////////////////////////////////////////// return fso.GetFile(path);
});
function fileGet(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var file = FSO.GetFile(FN);
FSO = null;
return file;
} }
/////////////////////////////////////////////////////////////////////////////////
// readFile
// Read the conents of the pass filename and return as a string
/////////////////////////////////////////////////////////////////////////////////
function readFile(FN, charset) { function readFile(path, charset) {
if (typeof charset === "undefined") { if (typeof charset === "undefined") {
console.warn("CHARSET has not been passed. Force to UTF-8."); console.warn("CHARSET not passed. Defaulting to UTF-8.");
charset = PipeIPC.CdoUTF_8; charset = PipeIPC.CdoUTF_8;
} }
var text = '';
var pipe = PipeIPC.connect("volatile"); var pipe = PipeIPC.connect("volatile");
pipe.setCharset(charset); pipe.setCharset(charset);
pipe.loadFromFile(FN, charset); pipe.loadFromFile(path, charset);
text += pipe.read(); var text = pipe.read();
pipe.destroy(); pipe.destroy();
return text; return text;
} }
///////////////////////////////////////////////////////////////////////////////// function writeFile(path, content, charset) {
// writeFile
// Write the passed content to named disk file
/////////////////////////////////////////////////////////////////////////////////
function writeFile(FN, content, charset) {
if (typeof content === "undefined") { if (typeof content === "undefined") {
console.warn("CONTENT has not been passed. Force to empty string."); console.warn("CONTENT not passed. Defaulting to empty string.");
content = ''; content = '';
} }
if (typeof charset === "undefined") { if (typeof charset === "undefined") {
console.warn("CHARSET has not been passed. Force to UTF-8."); console.warn("CHARSET not passed. Defaulting to UTF-8.");
charset = PipeIPC.CdoUTF_8; charset = PipeIPC.CdoUTF_8;
} }
var pipe = PipeIPC.connect("volatile"); var pipe = PipeIPC.connect("volatile");
pipe.setCharset(charset); pipe.setCharset(charset);
pipe.startRecorder(FN, PipeIPC.ForWriting); pipe.startRecorder(path, PipeIPC.ForWriting);
pipe.write(content); pipe.write(content);
pipe.destroy(); pipe.destroy();
return true; return true;
} }
///////////////////////////////////////////////////////////////////////////////// function writeBinaryFile(path, data) {
// writeBinaryFile return UseObject("ADODB.Stream", function(stream) {
///////////////////////////////////////////////////////////////////////////////// stream.Type = PipeIPC.adTypeBinary;
stream.Open();
function writeBinaryFile(FN, DATA) { stream.Write(data);
var BinaryStream = CreateObject("ADODB.Stream"); stream.SaveToFile(path, adSaveCreateOverWrite);
BinaryStream.Type = PipeIPC.adTypeBinary; stream.Close();
BinaryStream.Open(); });
BinaryStream.Write(DATA);
BinaryStream.SaveToFile(FN, adSaveCreateOverWrite);
BinaryStream.Close();
} }
///////////////////////////////////////////////////////////////////////////////// function moveFile(fromPath, toPath) {
// moveFile return UseObject("Scripting.FileSystemObject", function(fso) {
///////////////////////////////////////////////////////////////////////////////// return fso.MoveFile(fromPath, toPath);
});
function moveFile(FROM, TO) {
var FSO = CreateObject("Scripting.FileSystemObject");
var result = FSO.MoveFile(FROM, TO);
FSO = null;
return result;
} }
///////////////////////////////////////////////////////////////////////////////// function createFolder(path) {
// createFolder return UseObject("Scripting.FileSystemObject", function(fso) {
///////////////////////////////////////////////////////////////////////////////// return fso.CreateFolder(path);
});
function createFolder(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var result = FSO.CreateFolder(FN);
FSO = null;
return result;
} }
///////////////////////////////////////////////////////////////////////////////// function deleteFolder(path) {
// deleteFolder return UseObject("Scripting.FileSystemObject", function(fso) {
///////////////////////////////////////////////////////////////////////////////// return fso.DeleteFolder(path);
});
function deleteFolder(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var result = FSO.DeleteFolder(FN);
FSO = null;
return result;
} }
///////////////////////////////////////////////////////////////////////////////// function deleteFile(path) {
// deleteFile return UseObject("Scripting.FileSystemObject", function(fso) {
///////////////////////////////////////////////////////////////////////////////// return fso.DeleteFile(path);
});
function deleteFile(FN) {
var FSO = CreateObject("Scripting.FileSystemObject");
var result = FSO.DeleteFile(FN);
FSO = null;
return result;
} }
///////////////////////////////////////////////////////////////////////////////// function includeFile(path) {
// includeFile
/////////////////////////////////////////////////////////////////////////////////
function includeFile(FN) {
try { try {
eval(readFile(FN)); eval(readFile(path));
} catch (e) { } catch (e) {
console.error(e.message, "in", FN); console.error(e.message, "in", path);
} }
} }
///////////////////////////////////////////////////////////////////////////////// function getFilesFromFolder(path) {
// appendFile if (!folderExists(path)) return [];
/////////////////////////////////////////////////////////////////////////////////
function appendFile(FN, content, charset) { var folder = UseObject("Scripting.FileSystemObject", function(fso) {
var result = false; return fso.GetFolder(path);
var pipe = PipeIPC.connect(PipeIPC.CRC32(FN)); });
return Array.from(folder.Files);
}
function appendFile(path, content, charset) {
var pipe = PipeIPC.connect(PipeIPC.CRC32(path));
pipe.setCharset(charset); pipe.setCharset(charset);
pipe.startRecorder(FN, PipeIPC.ForAppending); pipe.startRecorder(path, PipeIPC.ForAppending);
result = pipe.write(content); var result = pipe.write(content);
pipe.close(); pipe.close();
return result; return result;
} }
///////////////////////////////////////////////////////////////////////////////// function prependFile(path, content, charset) {
// prependFile
/////////////////////////////////////////////////////////////////////////////////
function prependFile(FN, content, charset) {
var pipe = PipeIPC.connect("volatile"); var pipe = PipeIPC.connect("volatile");
pipe.setCharset(charset); pipe.setCharset(charset);
pipe.startRecorder(FN, PipeIPC.ForWriting); pipe.startRecorder(path, PipeIPC.ForWriting);
pipe.write(content); pipe.write(content);
pipe.destroy(); pipe.destroy();
return true; return true;
} }
///////////////////////////////////////////////////////////////////////////////// function rotateFile(path, content, numOfLines, charset) {
// rotateFile var pipe = PipeIPC.connect(PipeIPC.CRC32(path));
/////////////////////////////////////////////////////////////////////////////////
function rotateFile(FN, content, numOfLines, charset) {
var result = false;
var pipe = PipeIPC.connect(PipeIPC.CRC32(FN));
pipe.setCharset(charset); pipe.setCharset(charset);
pipe.setMaxSentences(numOfLines); pipe.setMaxSentences(numOfLines);
pipe.startRecorder(FN, PipeIPC.ForAppending); pipe.startRecorder(path, PipeIPC.ForAppending);
result = pipe.write(content); var result = pipe.write(content);
pipe.close(); pipe.close();
return result; return result;
} }
// Function to load and parse the .env file function loadEnvFromFile(path, callback) {
var loadEnvFromFile = function(envFilePath, callback) {
try { try {
// Read the file content using PipeIPC.CdoCharset.CdoUTF_8 encoding var envString = readFile(path, PipeIPC.CdoCharset.CdoUTF_8);
var envString = readFile(envFilePath, PipeIPC.CdoCharset.CdoUTF_8);
// Parse the environment variables
var envConfig = parseEnv(envString); var envConfig = parseEnv(envString);
console.log('Environment variables loaded from ' + path);
console.log('Environment variables loaded from ' + envFilePath);
// Call the callback function if provided
if (typeof callback === "function") { if (typeof callback === "function") {
try { try {
callback(envConfig); callback(envConfig);
@ -217,23 +154,22 @@ var loadEnvFromFile = function(envFilePath, callback) {
} }
} }
} catch (e) { } catch (e) {
console.error('Error reading environment file:', envFilePath, e.message); console.error('Error reading environment file:', path, e.message);
} }
}; }
// Function to find --env-file argument in the args array and load the environment file function loadEnvFromArgs(args, callback) {
var loadEnvFromArgs = function(args, callback) {
var envFileArg = args.find(function(x) { var envFileArg = args.find(function(x) {
return x.startsWith('--env-file='); return x.startsWith('--env-file=');
}); });
if (envFileArg) { if (envFileArg) {
var envFilePath = envFileArg.split('=')[1]; var path = envFileArg.split('=')[1];
loadEnvFromFile(envFilePath, callback); loadEnvFromFile(path, callback);
} else { } else {
console.warn('No --env-file argument provided.'); console.warn('No --env-file argument provided.');
} }
}; }
exports.fileExists = fileExists; exports.fileExists = fileExists;
exports.folderExists = folderExists; exports.folderExists = folderExists;
@ -246,6 +182,7 @@ exports.createFolder = createFolder;
exports.deleteFolder = deleteFolder; exports.deleteFolder = deleteFolder;
exports.deleteFile = deleteFile; exports.deleteFile = deleteFile;
exports.includeFile = includeFile; exports.includeFile = includeFile;
exports.getFilesFromFolder = getFilesFromFolder;
exports.appendFile = appendFile; exports.appendFile = appendFile;
exports.rotateFile = rotateFile; exports.rotateFile = rotateFile;
exports.loadEnvFromFile = loadEnvFromFile; exports.loadEnvFromFile = loadEnvFromFile;
@ -253,7 +190,7 @@ exports.loadEnvFromArgs = loadEnvFromArgs;
exports.CdoCharset = PipeIPC.CdoCharset; exports.CdoCharset = PipeIPC.CdoCharset;
exports.VERSIONINFO = "File IO Library (file.js) version 0.2.13"; exports.VERSIONINFO = "File IO Library (file.js) version 0.2.15";
exports.AUTHOR = "gnh1201@catswords.re.kr"; exports.AUTHOR = "gnh1201@catswords.re.kr";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;

View File

@ -72,6 +72,10 @@ function Excel() {
} else { } else {
this.currentWorksheet = this.currentWorkbook.Worksheets(idx); this.currentWorksheet = this.currentWorkbook.Worksheets(idx);
} }
// switch to the worksheet
this.currentWorksheet.Activate();
return this; return this;
}; };
@ -119,7 +123,7 @@ function PowerPoint() {
console.info("Microsoft Office PowerPoint", this.version); console.info("Microsoft Office PowerPoint", this.version);
this.currentPresentation = null; this.currentPresentation = null;
this.open = function(filename) { this.open = function(filename) {
if (typeof filename !== "undefined") { if (typeof filename !== "undefined") {
@ -129,16 +133,16 @@ function PowerPoint() {
} }
if (FILE.fileExists(filename)) { if (FILE.fileExists(filename)) {
console.info("FOUND", filename); console.info("FOUND", filename);
this.application.Presentations.Open(filename); this.application.Presentations.Open(filename);
this.currentPresentation = this.application.ActivePresentation; this.currentPresentation = this.application.ActivePresentation;
} else { } else {
console.warn("NOT FOUND", filename); console.warn("NOT FOUND", filename);
this.currentPresentation = this.application.Presentations.Add(true); this.currentPresentation = this.application.Presentations.Add(true);
} }
} else { } else {
this.currentPresentation = this.application.Presentations.Add(true); this.currentPresentation = this.application.Presentations.Add(true);
} }
//this.selectPresentation(1); //this.selectPresentation(1);
}; };
this.selectPresentation = function(idx) { this.selectPresentation = function(idx) {
@ -223,7 +227,7 @@ exports.Excel = Excel;
exports.PowerPoint = PowerPoint; exports.PowerPoint = PowerPoint;
exports.Word = Word; exports.Word = Word;
exports.VERSIONINFO = "Microsoft Office interface (msoffice.js) version 0.2.0"; exports.VERSIONINFO = "Microsoft Office interface (msoffice.js) version 0.2.1";
exports.AUTHOR = "gnh1201@catswords.re.kr"; exports.AUTHOR = "gnh1201@catswords.re.kr";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;

View File

@ -98,25 +98,31 @@ function makeProbabilityBit(p) {
return !( p > 0.0 ? ( (randomize() / p) > 1.0 ) : true) ? 1 : 0; return !( p > 0.0 ? ( (randomize() / p) > 1.0 ) : true) ? 1 : 0;
} }
function makeInterface(i) {
return CreateObject(["Scripting.FileSystemObject", "ADODB.Stream"][i]);
}
function openTextFile(filename, iomode) { function openTextFile(filename, iomode) {
return makeInterface(0).OpenTextFile(filename, iomode, true, TristateTrue); return UseObject("Scripting.FileSystemObject", function(fso) {
return fso.OpenTextFile(filename, iomode, true, TristateTrue);
});
} }
function checkFileExists(filename) { function checkFileExists(filename) {
return makeInterface(0).FileExists(filename);; return UseObject("Scripting.FileSystemObject", function(fso) {
return fso.FileExists(filename);
});
} }
function deleteFile(filename) { function deleteFile(filename) {
if (checkFileExists(filename)) makeInterface(0).DeleteFile(filename); if (checkFileExists(filename)) {
return UseObject("Scripting.FileSystemObject", function(fso) {
fso.DeleteFile(filename);
});
}
} }
function getFileSize(filename) { function getFileSize(filename) {
try { try {
return makeInterface(0).GetFile(filename).Size; return UseObject("Scripting.FileSystemObject", function(fso) {
return fso.GetFile(filename).Size;
});
} catch (e) { } catch (e) {
return -1; return -1;
} }
@ -130,40 +136,43 @@ function Converter() {
}; };
this.getBinaryFromText = function() { this.getBinaryFromText = function() {
var stream = makeInterface(1); return UseObject("ADODB.Stream", function(stream) {
stream.Type = adTypeText; stream.Type = adTypeText;
stream.CharSet = CdoCharset.CdoUTF_8; stream.CharSet = CdoCharset.CdoUTF_8;
stream.Open(); stream.Open();
stream.WriteText(this.value); stream.WriteText(this.value);
stream.Position = 0; stream.Position = 0;
stream.Type = adTypeBinary; stream.Type = adTypeBinary;
return stream.Read(); return stream.Read();
});
}; };
this.getTextFromBinary = function() { this.getTextFromBinary = function() {
var stream = makeInterface(1); return UseObject("ADODB.Stream", function(stream) {
stream.Type = adTypeBinary; stream.Type = adTypeBinary;
stream.Open(); stream.Open();
stream.Write(this.value); stream.Write(this.value);
stream.Position = 0; stream.Position = 0;
stream.Type = adTypeText; stream.Type = adTypeText;
stream.CharSet = CdoCharset.CdoUTF_8; stream.CharSet = CdoCharset.CdoUTF_8;
return stream.ReadText(); return stream.ReadText();
});
}; };
this.repositionObject = function(stream, position) { this.repositionObject = function(stream, position) {
position = (position !== "number" ? 0 : position); position = (position !== "number" ? 0 : position);
return UseObject("ADODB.Stream", function(newStream) {
newStream.Type = adTypeBinary;
newStream.Mode = adModeReadWrite;
newStream.Open();
stream.Position = position;
stream.CopyTo(newStream);
stream.Flush();
stream.Close();
return newStream;
});
var _stream = makeInterface(1);
_stream.Type = adTypeBinary;
_stream.Mode = adModeReadWrite;
_stream.Open();
stream.Position = position;
stream.CopyTo(_stream);
stream.Flush();
stream.Close();
return _stream;
}; };
} }
@ -399,14 +408,15 @@ function PipeIPC() {
} }
// Convert UTF-16 BOM to a character set // Convert UTF-16 BOM to a character set
var stream = makeInterface(1); UseObject("ADODB.Stream", function(stream) {
stream.Type = adTypeText; stream.Type = adTypeText;
stream.Charset = charset; stream.Charset = charset;
stream.Open(); stream.Open();
stream.WriteText(str); stream.WriteText(str);
stream = (new Converter()).repositionObject(stream, 3); stream = (new Converter()).repositionObject(stream, 3);
stream.SaveToFile(dst, adSaveCreateOverWrite); stream.SaveToFile(dst, adSaveCreateOverWrite);
stream.Close(); stream.Close();
});
// Set a result // Set a result
isCommited = true; isCommited = true;
@ -462,7 +472,7 @@ function PipeIPC() {
this.openReader(); this.openReader();
text += this._read(this.reader); text += this._read(this.reader);
isReadCompleted = true; isReadCompleted = true;
this.lastReadTime = this.getCurrentTime(); this.lastReadTime = this.getCurrentTime();
this.closeReader(); this.closeReader();
} catch (e) { } catch (e) {
@ -495,15 +505,15 @@ function PipeIPC() {
var isReadCompleted = false; var isReadCompleted = false;
while (!isReadCompleted) { while (!isReadCompleted) {
try { try {
var ado = makeInterface(1); UseObject("ADODB.Stream", function(stream) {
ado.Charset = charset; stream.Charset = charset;
ado.Open(); stream.Open();
ado.LoadFromFile(filename); stream.LoadFromFile(filename);
text += ado.ReadText(); text += stream.ReadText();
ado.Close(); stream.Close();
});
isReadCompleted = true; isReadCompleted = true;
} catch (e) { } catch (e) {
//console.log(e.message);
isReadCompleted = false; isReadCompleted = false;
} }
} }
@ -555,7 +565,7 @@ exports.adSaveCreateNotExist = adSaveCreateNotExist;
exports.adSaveCreateOverWrite = adSaveCreateOverWrite; exports.adSaveCreateOverWrite = adSaveCreateOverWrite;
exports.adModeReadWrite = adModeReadWrite; exports.adModeReadWrite = adModeReadWrite;
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.23"; exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.24";
exports.AUTHOR = "gnh1201@catswords.re.kr"; exports.AUTHOR = "gnh1201@catswords.re.kr";
exports.global = global; exports.global = global;
exports.require = require; exports.require = require;

View File

@ -3,9 +3,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
// https://github.com/gnh1201/welsonjs // https://github.com/gnh1201/welsonjs
// //
// WelsonJS Standard Library // WelsonJS Standard Library (Polyfills and shared functions)
// //
// Polyfills
if (!Function.prototype.GetResource) { if (!Function.prototype.GetResource) {
Function.prototype.GetResource = function(ResourceName) { Function.prototype.GetResource = function(ResourceName) {
if (!this.Resources) { if (!this.Resources) {
@ -23,50 +22,58 @@ if (!Function.prototype.GetResource) {
} }
} }
// The provided code snippet has been corrected by ChatGPT.
// https://chat.openai.com/share/eaab056c-d265-4ee3-b355-9f29176a9caa
// Related issues: #75 #42 #30
if (typeof Enumerator !== "undefined") {
Enumerator.prototype.toArray = function() {
var result = [];
while (!this.atEnd()) {
var currentItem = this.item();
var currentItemProperties = currentItem.Properties_;
var itemObject = {};
var propertiesEnumerator = new Enumerator(currentItemProperties);
while (!propertiesEnumerator.atEnd()) {
var property = propertiesEnumerator.item();
if (typeof property.value !== "unknown") { // The type "Unknown" is Array
itemObject[property.name] = property.value;
} else {
var arrayValues = [];
var index = 0;
while (true) {
try {
arrayValues.push(property.value(index));
index++;
} catch (e) {
break;
}
}
itemObject[property.name] = arrayValues;
}
propertiesEnumerator.moveNext();
}
result.push(itemObject);
this.moveNext();
}
return result;
};
}
// Global APIs
function GetResource(ResourceName) { function GetResource(ResourceName) {
return arguments.callee.caller.GetResource(ResourceName); return arguments.callee.caller.GetResource(ResourceName);
} }
(function() {
var originalFrom = Array.from;
function convertToArray(v) {
var result = [];
while (!v.atEnd()) {
var item = v.item();
result.push(typeof item === "unknown" ? Array.from(item) : item);
v.moveNext();
}
return result;
}
function convertToObject(v) {
var obj = {};
var props = new Enumerator(v.Properties_);
while (!props.atEnd()) {
var prop = props.item();
obj[prop.name] = typeof prop.value === "unknown" ?
Array.from(prop.value) :
prop.value;
props.moveNext();
}
return obj;
}
Array.from = function(value) {
if (value && typeof value.item === "unknown") {
value = new Enumerator(value);
}
if (value instanceof Enumerator) {
return value && value.Properties_ ?
convertToObject(value) :
convertToArray(value);
}
return originalFrom ? originalFrom(value) : Array.prototype.slice.call(value);
};
Array.from.toString = function() {
return originalFrom.toString();
};
// compatible under version 0.2.7.55
Enumerator.prototype.toArray = Array.from;
})();
// [lib/std] the time of `sleep()' function is not accuracy #34 // [lib/std] the time of `sleep()' function is not accuracy #34
function sleep(ms, callback) { function sleep(ms, callback) {
var handler = null; var handler = null;
@ -623,7 +630,7 @@ exports.alert = alert;
exports.confirm = confirm; exports.confirm = confirm;
exports.prompt = prompt; exports.prompt = prompt;
exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.18"; exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.19";
exports.AUTHOR = "gnh1201@catswords.re.kr"; exports.AUTHOR = "gnh1201@catswords.re.kr";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;