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.
*/
@ -283,7 +301,9 @@ function require(pathname) {
return filename;
})({
existsSync: function(filename) {
return CreateObject("Scripting.FileSystemObject").FileExists(filename);
return UseObject("Scripting.FileSystemObject", function(fso) {
return fso.FileExists(filename);
});
}
}, {
join: function() {

View File

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

View File

@ -72,6 +72,10 @@ function Excel() {
} else {
this.currentWorksheet = this.currentWorkbook.Worksheets(idx);
}
// switch to the worksheet
this.currentWorksheet.Activate();
return this;
};
@ -223,7 +227,7 @@ exports.Excel = Excel;
exports.PowerPoint = PowerPoint;
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.global = global;
exports.require = global.require;

View File

@ -98,25 +98,31 @@ function makeProbabilityBit(p) {
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) {
return makeInterface(0).OpenTextFile(filename, iomode, true, TristateTrue);
return UseObject("Scripting.FileSystemObject", function(fso) {
return fso.OpenTextFile(filename, iomode, true, TristateTrue);
});
}
function checkFileExists(filename) {
return makeInterface(0).FileExists(filename);;
return UseObject("Scripting.FileSystemObject", function(fso) {
return fso.FileExists(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) {
try {
return makeInterface(0).GetFile(filename).Size;
return UseObject("Scripting.FileSystemObject", function(fso) {
return fso.GetFile(filename).Size;
});
} catch (e) {
return -1;
}
@ -130,7 +136,7 @@ function Converter() {
};
this.getBinaryFromText = function() {
var stream = makeInterface(1);
return UseObject("ADODB.Stream", function(stream) {
stream.Type = adTypeText;
stream.CharSet = CdoCharset.CdoUTF_8;
stream.Open();
@ -138,10 +144,11 @@ function Converter() {
stream.Position = 0;
stream.Type = adTypeBinary;
return stream.Read();
});
};
this.getTextFromBinary = function() {
var stream = makeInterface(1);
return UseObject("ADODB.Stream", function(stream) {
stream.Type = adTypeBinary;
stream.Open();
stream.Write(this.value);
@ -149,21 +156,23 @@ function Converter() {
stream.Type = adTypeText;
stream.CharSet = CdoCharset.CdoUTF_8;
return stream.ReadText();
});
};
this.repositionObject = function(stream, position) {
position = (position !== "number" ? 0 : position);
var _stream = makeInterface(1);
_stream.Type = adTypeBinary;
_stream.Mode = adModeReadWrite;
_stream.Open();
return UseObject("ADODB.Stream", function(newStream) {
newStream.Type = adTypeBinary;
newStream.Mode = adModeReadWrite;
newStream.Open();
stream.Position = position;
stream.CopyTo(_stream);
stream.CopyTo(newStream);
stream.Flush();
stream.Close();
return newStream;
});
return _stream;
};
}
@ -399,7 +408,7 @@ function PipeIPC() {
}
// Convert UTF-16 BOM to a character set
var stream = makeInterface(1);
UseObject("ADODB.Stream", function(stream) {
stream.Type = adTypeText;
stream.Charset = charset;
stream.Open();
@ -407,6 +416,7 @@ function PipeIPC() {
stream = (new Converter()).repositionObject(stream, 3);
stream.SaveToFile(dst, adSaveCreateOverWrite);
stream.Close();
});
// Set a result
isCommited = true;
@ -495,15 +505,15 @@ function PipeIPC() {
var isReadCompleted = false;
while (!isReadCompleted) {
try {
var ado = makeInterface(1);
ado.Charset = charset;
ado.Open();
ado.LoadFromFile(filename);
text += ado.ReadText();
ado.Close();
UseObject("ADODB.Stream", function(stream) {
stream.Charset = charset;
stream.Open();
stream.LoadFromFile(filename);
text += stream.ReadText();
stream.Close();
});
isReadCompleted = true;
} catch (e) {
//console.log(e.message);
isReadCompleted = false;
}
}
@ -555,7 +565,7 @@ exports.adSaveCreateNotExist = adSaveCreateNotExist;
exports.adSaveCreateOverWrite = adSaveCreateOverWrite;
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.global = global;
exports.require = require;

View File

@ -3,9 +3,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// https://github.com/gnh1201/welsonjs
//
// WelsonJS Standard Library
// WelsonJS Standard Library (Polyfills and shared functions)
//
// Polyfills
if (!Function.prototype.GetResource) {
Function.prototype.GetResource = function(ResourceName) {
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) {
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
function sleep(ms, callback) {
var handler = null;
@ -623,7 +630,7 @@ exports.alert = alert;
exports.confirm = confirm;
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.global = global;
exports.require = global.require;