Can it go the other way?
Closed this issue · 3 comments
AS3's library ecosystem is basically non-existent, but we have great JS libraries like moment.js. Some projects need to stay in Flex because of reasons (even if I try to convince the PM to consider a rewrite in Phonegap/Cordova, or, IDK, just write a web app). Is there a way this can be used to convert JS code into (even really dumb and really unidiomatic) AS3?
Interesting thought, so your goal would be to be able to use a library such as moment in AS3? That would be useful but could definitely be tricky, since JS lacks a strict organizational structure. But one thing to remember is that AS3 is a superset of JS so it might be possible to port some libraries by hand just by copying and pasting into AS3 (granted they aren't doing anything too fancy).
As an example, I tried to port moment just now and ran into some clear issues. One of which is the usage of +this
being invalid AS3 syntax (my first time seeing that on in JS tbh) and as well as the usage of as
as a function name (which is a reserved keyword in AS3). I'm sure a library that doesn't use any reserved keywords or +this
would work with clever use of .call()
to fix the this
binding. But I'd need to experiment a bit more with it...
Anyway, just to give you an idea of what I did, I swapped out the first several ines of moment.js:
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.moment = factory()
}(this, function () { 'use strict';
with this instead:
var window = {};
((function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.moment = factory()
}).call(null, window, function () { 'use strict';
In theory, this should attach moment to the fake window
object I created, but it won't work unfortunately because of what I mentioned before with the +this
thing and as
being used in the source code.
I'd have to look where that precisely occurs, but +variable is common in
JavaScript and it's a type coercion to Number. Wouldn't converting those to
+(this) work?
On Wed, 2 Sep 2015 03:00 Greg McLeod notifications@github.com wrote:
Interesting thought, so your goal would be to be able to use a library
such as moment in AS3? That would be useful but could definitely be tricky,
since JS lacks a strict organizational structure. But one thing to remember
is that AS3 is a superset of JS so it might be possible to port some
libraries by hand just by copying and pasting into AS3 (granted they aren't
doing anything too fancy).As an example, I tried to port moment just now and ran into some clear
issues. One of which is the usage of +this being invalid AS3 syntax (my
first time seeing that on in JS tbh) and as well as the usage of as as a
function name (which is a reserved keyword in AS3). I'm sure a library that
doesn't use any reserved keywords or +this would work with clever use of
.call() to fix the this binding. But I'd need to experiment a bit more
with it...Anyway, just to give you an idea of what I did, I swapped out the first
several ines of moment.js:(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.moment = factory()
}(this, function () { 'use strict';with this instead:
var window = {};
((function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.moment = factory()
}).call(null, window, function () { 'use strict';In theory, this should attach moment to the fake window object I created,
but it won't work unfortunately because of what I mentioned before with the
+this thing and as being used in the source code.—
Reply to this email directly or view it on GitHub
#4 (comment).
Oh, so it looks like doing +(this)
does resolve those errors, although seems like as
can't even be used as a property name either which is silly :/
Anyway, so if I do that and change as
to something else, I can get a SWF to compile with moment if I turn off "strict mode" and "warnings" in the Flash IDE (this might be compiler flags in Flex I'd imagine). But still no luck after that, some strange error here:
if (key && key._locale && key._locale._abbr) {
key = key._locale._abbr;
}
It doesn't seem to want to access key._locale
, and key
is a string. And AS3 throws an error if you try to access values don't exist on built-in classes like this. So I'm not exactly sure what's going on here, but I'd say moment might be a lost cause. I guess there's not a really enough convention in JS-world to be able to programmatically solve this problem for most libraries