diff --git a/lib/std.js b/lib/std.js index e42f4aa..c593f31 100644 --- a/lib/std.js +++ b/lib/std.js @@ -252,6 +252,38 @@ function addslashes(s) { }; // Private APIs / Utility functions + +// Emulate Server.CreateObject +function CreateObject(progId, serverName, callback) { + var progIds = []; + var _CreateObject = function(p, s) { + if (typeof WScript !== "undefined") { + return WScript.CreateObject(p, s); + } else if (typeof ActiveXObject !== "undefined") { + return new ActiveXObject(p); + } + return null; + }; + + if (typeof progId == "object") { + progIds = progId; + } else { + progIds.push(progId); + } + + for (var i = 0; i < progIds.length; i++) { + try { + var obj = _CreateObject(progIds[i], serverName); + if (typeof callback === "function") { + callback(obj, progIds[i]); + } + return obj; + } catch (e) { + console.error(e.message); + }; + } +} + function alert(message) { if (typeof window !== "undefined") { window.alert(message); @@ -305,18 +337,18 @@ StdEvent.AT_TARGET = 2; StdEvent.BUBBLING_PHASE = 3; // Not used but to be compatible function StdEventTarget() { - this._events = []; + this.__events__ = []; - this.dispatchEvent = function(event, _exception) { + this.dispatchEvent = function(event, __exception__) { event.target = this; event.isTrusted = false; event.eventPhase = StdEvent.AT_TARGET; event.currentTarget = event.target; - for (var i = 0; i < this._events.length; i++) { - var e = this._events[i]; + for (var i = 0; i < this.__events__.length; i++) { + var e = this.__events__[i]; if (e.type == event.type && typeof(e.listener) === "function") { try { - e.listener(event, _exception); + e.listener(event, __exception__); } catch (ex) { this.dispatchEvent(new StdEvent("error"), ex); } @@ -326,12 +358,12 @@ function StdEventTarget() { this.addEventListener = function(type, listener) { if (typeof listener === "function") { - this._events.push({ + this.__events__.push({ "type": type, "listener": listener, - "counter": StdEventTarget._counter + "counter": StdEventTarget.__counter__ }); - StdEventTarget._counter++; + StdEventTarget.__counter__++; } else { throw new TypeError("EventListener must be a function"); } @@ -339,10 +371,10 @@ function StdEventTarget() { this.removeEventListener = function(type, listener) { if (typeof listener === "function") { - for (var i = 0; i < this._events.length; i++) { - var e = this._events[i]; + for (var i = 0; i < this.__events__.length; i++) { + var e = this.__events__[i]; if (e.type == type && typeof(e.listener) === "function" && e.listener.toString() == listener.toString()) { - delete this._events[i]; + delete this.__events__[i]; } } } else { @@ -350,47 +382,67 @@ function StdEventTarget() { } }; }; -StdEventTarget._counter = 0; +StdEventTarget.__counter__ = 0; -// https://github.com/gnh1201/welsonjs/wiki/AsyncFunction -function AsyncFunction(f) { +/* +var a = new AsyncFunction(function() { + console.log("calling A"); +}); + +var _async_b = function(function() { + console.log("calling B"); +}); + +function main(args) { + AsyncFunction.bind(this, args); + console.log("welcome"); +} + +exports.a = a; +exports._async_b = _async_b; +*/ + +function AsyncFunction(f, __filename) { this.f = f; - this.__filename = AsyncFunction._filename; + this.__filename = __filename; this.run = function() { var args = Array.from(arguments); // increase number of async functions - AsyncFunction._counter++; + AsyncFunction.counter++; // decrease number of async functions - var dispatch = function(_args, _f) { - if (typeof _f === "function") _f.apply(null, _args); - AsyncFunction._counter--; + var _this = this; + var _f = function() { + if (typeof _this.f === "function") _this.f(); + AsyncFunction.counter--; }; // CLI or Window? if (typeof WScript !== "undefined") { - (function(_args, SHELL) { - SHELL.show(["cscript", "app.js", this.__filename, "/async", f].concat(_args)); - })(args, require("lib/shell")); - } else if (typeof window !== "undefined") { - (function(_args, _f) { - window.setTimeout(function() { - dispatch(_args, _f); - }, 1); - })(args, this.f); + require("lib/shell").show(["cscript", "app.js", this.__filename, "/async", f].concat(args)); } else { - dispatch(args, this.f); + sleep(1, _f); } }; this.runSynchronously = function() { return this.f.apply(null, arguments); }; + + if (typeof this.__filename === "string") { + this.__filename = __filename; + } else if (typeof WScript !== "undefined") { + this.__filename = (function(path) { + var pos = Math.max.apply(null, [path.lastIndexOf("\\"), path.lastIndexOf("/")]); + return (pos > -1 ? path.substring(pos + 1) : ""); + })(WScript.ScriptFullName); + } + + AsyncFunction.__counter__++; }; -AsyncFunction._counter = 0; -AsyncFunction._filename = "bootstrap.js"; +AsyncFunction.__counter__ = 0; AsyncFunction.Initialize = function(exports, args) { if (args.length < 2) return; @@ -407,20 +459,20 @@ AsyncFunction.Initialize = function(exports, args) { } } - throw new AsyncFunction.Initialized("Initialized"); + throw new AsyncFunction.Resolved("Resolved"); }; AsyncFunction.bind = function(exports, args) { // compatible under 0.2.7.31 console.warn("AsyncFunction.bind() is deprecated. Use AsyncFunction.Initialize()"); return AsyncFunction.Initialize(exports, args); }; -AsyncFunction.Initialized = function(message) { - this.name = "AsyncFunction.Initialized"; +AsyncFunction.Resolved = function(message) { + this.name = "AsyncFunction.Resolved"; this.message = message; }; -AsyncFunction.Initialized.prototype = new Error(); -AsyncFunction.Initialized.prototype.constructor = AsyncFunction.Initialized; +AsyncFunction.Resolved.prototype = new Error(); +AsyncFunction.Resolved.prototype.constructor = AsyncFunction.Resolved; -// https://github.com/gnh1201/welsonjs/wiki/GeneratorFunction +// [app] Transpiling ES6 generator functions #75 function GeneratorFunction(f) { var _lastState = 0; var _state = 0; @@ -464,6 +516,45 @@ GeneratorFunction.Yield = function(message) { GeneratorFunction.Yield.prototype = new Error(); GeneratorFunction.Yield.prototype.constructor = GeneratorFunction.Yield; +/* +var a = new GeneratorFunction(function(_yield) { + _yield("a"); + _yield("b"); + _yield("c"); +}); +console.log(a.next().value); +console.log(a.next().value); +console.log(a.next().value); +*/ + +function StdStorage() { + var data = {}; + var commit = function() { + this.length = Object.keys(data).length; + }; + + this.length = 0; + this.key = function(idx) { + var keyName = Object.keys(data)[idx]; + return data[keyName]; + }; + this.setItem = function(keyName, keyValue) { + data[keyName] = keyValue; + commit(); + }; + this.getItem = function(keyName) { + return data[keyName]; + }; + this.removeItem = function(keyName) { + delete data[keyName]; + commit(); + }; + this.clear = function() { + data = {}; + commit(); + }; +} + global.GetResource = GetResource; global.sleep = sleep; global.repeat = repeat; @@ -473,7 +564,6 @@ global.CHR = CHR; global.splitLn = splitLn; global.addslashes = addslashes; global.AsyncFunction = AsyncFunction; -global.GeneratorFunction = GeneratorFunction; exports.Event = StdEvent; exports.EventTarget = StdEventTarget; @@ -481,7 +571,7 @@ exports.EventTarget = StdEventTarget; exports.alert = alert; exports.confirm = confirm; -exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.9.0"; +exports.VERSIONINFO = "WelsonJS Standard Library (std.js) version 0.8.7"; exports.AUTHOR = "abuse@catswords.net"; exports.global = global; exports.require = global.require;