Update std.js

This commit is contained in:
Namhyeon Go 2022-02-10 15:21:22 +09:00 committed by GitHub
parent 255f9dab32
commit 138c3a5fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,7 @@
/////////////////////////////////////////////////////////////////////////////////
// Polyfills
/////////////////////////////////////////////////////////////////////////////////
if (!Function.prototype.GetResource) {
Function.prototype.GetResource = function(ResourceName) {
if (!this.Resources) {
@ -27,16 +28,37 @@ if (!Function.prototype.GetResource) {
}
}
// MS JScript Enumerator to Array
if (!Enumerator.prototype.toArray) {
Enumerator.prototype.toArray = function() {
var items = [];
var a = [];
for (; !this.atEnd(); this.moveNext()) {
var item = this.item();
try {
items.push(item);
} catch (e) {}
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);
}
return items;
return a;
};
}
@ -44,27 +66,175 @@ if (!Enumerator.prototype.toArray) {
// Global APIs
/////////////////////////////////////////////////////////////////////////////////
global.GetResource = function(ResourceName) {
function GetResource(ResourceName) {
return arguments.callee.caller.GetResource(ResourceName);
}
global.sleep = function(ms, callback) {
if (typeof(WScript) !== "undefined") {
// [lib/std] the time of `sleep()' function is not accuracy #34
function sleep(ms, callback) {
var handler = null;
var cur = Date.now();
var end = cur + ms;
if (typeof WScript !== "undefined") {
/*
while (cur < end) {
//WScript.Sleep(1);
cur = Date.now();
}
end = Date.now();
*/
WScript.Sleep(ms);
if (typeof(callback) === "function") {
callback();
}
} else {
if (typeof(callback) === "function") {
setTimeout(callback, ms);
}
if (typeof callback === "function")
callback()
;
} else if (typeof window !== "undefined") {
if (typeof callback === "function")
handler = setTimeout(callback, ms);
;
}
return { 'ms': end, 'handler': handler };
};
function repeat(target, callback, onError) {
switch (typeof target) {
case "number":
case "boolean":
var ms = target;
var i = 0;
var result = null;
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")
var result = callback(i);
if (typeof result === "number") {
i += result;
} else if (result === false) {
break;
}
;
} catch (e) {
if (typeof onError === "function")
if (onError(e) === false)
break
;
;
}
cur = Date.now();
}
end = Date.now();
} else if (typeof window !== "undefined") {
if (typeof callback === "function")
handler = setInterval(callback, ms);
;
}
return { 'ms': end, 'handler': handler };
case "object":
var arr = target;
for (var i = 0; i < arr.length; i++) {
try {
if (typeof callback === "function")
if (callback(i, arr[i]) === false)
break
;
;
} catch (e) {
if (typeof onError === "function")
if (onError(e) === false)
break
;
;
}
}
break;
}
};
global.CHR = function(ord) {
function rotate(target, callback, onError) {
var arr = target;
var i = 0;
var stop = false;
while (!stop) {
try {
if (typeof callback === "function") {
stop = callback(i, arr[i]);
} else {
stop = true;
}
} catch (e) {
if (typeof onError === "function")
stop = onError(e);
;
}
i++;
i = i % keywords.length;
}
};
function range() {
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;
};
function CHR(ord) {
return String.fromCharCode(ord);
};
function splitLn(s) {
return s.split(/\r?\n/);
};
function addslashes(s) {
return s.toString().replace(/\\/g, '\\\\').
replace(/\u0008/g, '\\b').
replace(/\t/g, '\\t').
replace(/\n/g, '\\n').
replace(/\f/g, '\\f').
replace(/\r/g, '\\r').
replace(/'/g, '\\\'').
replace(/"/g, '\\"')
;
};
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////
@ -73,7 +243,7 @@ global.CHR = function(ord) {
// Emulate Server.CreateObject
/////////////////////////////////////////////////////////////////////////////////
exports.CreateObject = function(progId, serverName, callback) {
function CreateObject(progId, serverName, callback) {
var progIds = [];
var _CreateObject = function(p, s) {
if (typeof(WScript) !== "undefined") {
@ -102,42 +272,22 @@ exports.CreateObject = function(progId, serverName, callback) {
}
};
/////////////////////////////////////////////////////////////////////////////////
// Standard Event Object
/////////////////////////////////////////////////////////////////////////////////
var StdEvent = function(eventName) {
this.bubbles = false; // Not supported
this.cancelable = false; // Not supported
this.composed = false; // Not supported
this.currentTarget = null; // Not supported
function StdEvent(eventName) {
this.defaultPrevented = false;
this.eventPhase = null; // TODO
this.isTrusted = true; // Not supported
this.timeStamp = new Date();
this.eventName = eventName;
this.target = null;
// Not supported
this.composedPath = function() {
return null;
};
this.preventDefault = function() {
this.defaultPrevented = true;
};
// Not supported
this.stopImmediatePropagation = function() {
return null;
};
// Not supported
this.setPropagation = function() {
return null;
};
};
var StdEventableObject = function() {
function StdEventableObject() {
this.dispatchEvent = function(event) {
event.target = this;
if(('on' + event.eventName) in this) this['on' + event.eventName](event);
@ -152,7 +302,16 @@ var StdEventableObject = function() {
};
};
exports.VERSIONINFO = "Standard Lib (std.js) version 0.3";
global.GetResource = GetResource;
global.sleep = sleep;
global.repeat = repeat;
global.rotate = rotate;
global.range = range;
global.CHR = CHR;
global.splitLn = splitLn;
global.addslashes = addslashes;
exports.VERSIONINFO = "Standard Lib (std.js) version 0.4";
exports.global = global;
exports.require = global.require;