Giter Site home page Giter Site logo

johnbrandle / as3-js Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 1.0 44.16 MB

ActionScript 3 to JavaScript compiler

Home Page: https://johnbrandle.github.io/as3-js/

License: Other

JavaScript 86.91% AngelScript 0.03% ActionScript 13.07%
as3 actionscript actionscript3 flash swf swc compiler transcompiler javascript nodejs npm

as3-js's Introduction

as3-js

Build Status Dependency Status Known Vulnerabilities contributions welcome

as3-js is an ActionScript 3 to JavaScript compiler, with both browser and Node.js support.

  • IE11, Edge, Chrome, Firefox, Safari (iOS9+)
  • Node.js 10.15.3+

Supports:

  • Run-time compilation
  • SWC files
  • flash.* API
  • XML, XMLList, Proxy, Dictionary, and E4X* (see docs)
  • Almost all AS3 language features

Does not support:

  • with
  • package-level static initializers (class-level supported)
  • lazy initialization of non-static property values:
private var foo1:* = foo2; //undefined, not 5
private var foo2:* = 5;
  • weak references
  • named closures in conditionals:
if (false) function foo() {} //compilation error
  • non-unique custom namespace identifiers:
public var myNamespace;
namespace myNamespace = 'foo'; //conflict

Install:

npm install @johnbrandle/as3-js

Basic Usage:

Browser
/* c:/projects/hello/source/com/foo/Hello.as */

package com.foo
{
    public class Hello
    {
        public function say():String
        {
            return document.getElementById('hello_world').innerHTML;
        }
    }
}
/* node compilation script */

const as3_js = require('@johnbrandle/as3-js');

let props =
{
	srcDir:'c:/projects/hello/source', //where *.as files are located
	mainFile:'/com/foo/Hello.as', //main class file relative location to srcDir
	expose:'hello', //what variable name to set main class instance to
	platform:'browser' //environment where compiled js will run, options: "node|browser|player"
}

let result = as3_js.compile(props);

fs.writeFileSync('c:/projects/hello/build/js/hello.js', result.js, 'utf8');
<!-- c:/projects/hello/build/index.html -->

<div id="hello_world">hello world</div>

<script src="js/hello.js"></script>
<script>
    trace(hello.say()); //outputs hello world to console
</script>  
Browser (Runtime)
<script src="node_modules/@johnbrandle/as3-js/_excluded/_generated/as3_js.browser.3.js"></script>
<script>
    let request = new XMLHttpRequest();
    request.open('GET', 'node_modules/@johnbrandle/as3-js/_excluded/_generated/builtin.browser.swc', false); //recommend changing this to asynch
    request.send(null);

    let constructs = as3_js.getSwcUtil().parseSWCString(request.responseText); //recommend caching result

    let script = 'trace("hello world");';
    let code = 'package { public function actionScript(scope:Object, args:*):* { return (function() {' + script + '\n\n}).apply(scope, args || []); } }';  

    let construct = as3_js.getAnalyzer().analyze(as3_js.getParser().parse(as3_js.getLexer().lex(code).tokens), constructs, 3, true, true);  
    let result = as3_js.getTranslator().translate(construct, constructs, true, false, false);  

    let value = '(function() { var $window = this; var window = $window.parent || $window; var document = window.document; var $es4 = window.$es4 || (window.$es4 = {}); var _ = window._; var $ = window.$;\n\n' + result + '\n\n})();';  

    eval(value); //outputs hello world to console
</script>
Node
/* c:/projects/hello/source/com/foo/Hello.as */

package com.foo
{
    public class Hello
    {
        public function say():String
        {
            const os = require('os');

            return os.platform() == 'aix' ? global.Buffer.from('hello world').toString() : 'hello world';
        }
    }
}
/* node compilation script */

const as3_js = require('@johnbrandle/as3-js');

let props =
{
	srcDir:'c:/projects/hello/source', //where *.as files are located
	mainFile:'/com/foo/Hello.as', //main class file relative location to srcDir
	expose:'hello', //module export name
	platform:'node' //environment where compiled js will run, options: "node|browser|player"
}

let result = as3_js.compile(props);

fs.writeFileSync('c:/projects/hello/tools/modules/hello/index.js', result.js, 'utf8');
/* node test module script */

const hello = require('./modules/hello');

trace(hello.say()); //outputs hello world to console

Advanced Usage:

see docs

Notes:

  • For the "browser" platform, recommend loading compiled js files in an iframe (see docs for details)
  • The original purpose of this project was to convert valid AS3 to JS, so consider first compiling with Apache Flex for improved error checking.
  • Outstanding bug: missing semicolons will cause compilation errors in some instances...recommend always using semicolons for now.
  • Recommended IDE: IntelliJ

as3-js's People

Contributors

johnbrandle avatar johnsrush avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

geekstakulus

as3-js's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.