PHPackages                             lantosbro/simple-javascript-compilation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lantosbro/simple-javascript-compilation

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lantosbro/simple-javascript-compilation
=======================================

\[UnArchived\] Compiles JavaScript code through a series of actions.

04PHP

Since Oct 17Pushed 2y ago1 watchersCompare

[ Source](https://github.com/LantosBro/simple-javascript-compilation)[ Packagist](https://packagist.org/packages/lantosbro/simple-javascript-compilation)[ RSS](/packages/lantosbro-simple-javascript-compilation/feed)WikiDiscussions main Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

SimpleJavaScriptCompilation
===========================

[](#simplejavascriptcompilation)

[![CircleCI](https://camo.githubusercontent.com/e0fc7eb85e7c17430fbcbd141d7f26f69e8d7421b8cefc0cbb4e36e7392ccd54/68747470733a2f2f636972636c6563692e636f6d2f67682f4b7972616e52616e612f73696d706c652d6a6176617363726970742d636f6d70696c6174696f6e2e7376673f7374796c653d737667)](https://circleci.com/gh/KyranRana/simple-javascript-compilation)

This library supports compiling simple JavaScript declarations and expressions without the need for PHP V8JS.

Supported in expressions:

```
- Global methods

    - String.fromCharCode
    - atob
    - eval
    - escape

- Primitive types

    - Boolean
    - Integer

        - toFixed

    - String

       - charCodeAt
       - italics
       - length

    - Null
    - Undefined

- Arrays

```

#### Architecture

[](#architecture)

This library consists of 4 main classes.

##### ExpressionStreamReader

[](#expressionstreamreader)

- Reads through and emits events when entering / passing key areas of an expression.
- Events emitted:

    - EXPRESSION\_START

    ```
    {
        castAndNegations:   Array           // casts and negations (! +) before expression
    }

    ```

    - EXPRESSION\_END

    ```
    {
        additionalOps:      Array   // function and index calls on result of expression
        operator:           Operator                // operator
    }

    ```

    - ARRAY\_START

    ```
    {
        castAndNegations:   Array           // casts and negations (! +) before array
    }

    ```

    - ARRAY\_END

    ```
    {
        additionalOps:      Array   // function and index calls on result of array
        operator:           Operator                // operator
    }

    ```

    - BOOLEAN

    ```
    {
        value:              String                  // true / false
        additionalOps:      Array   // function or index calls on boolean
        castsAndNegations:  Array           // casts and negations (! +) before boolean
        operator:           Operator                // operator
    }

    ```

    - INTEGER

    ```
    {
        value:              String                  // integer / NaN / Infinity / -Infinity
        additionalOps:      Array   // function or index calls on integer
        castsAndNegations:  Array           // casts and negations (! +) before integer
        operator:           Operator                // operator
    }

    ```

    - STRING

    ```
    {
       value:              String                  // string
       additionalOps:      Array   // function or index calls on string
       castsAndNegations:  Array           // casts and negations (! +) before string
       operator:           Operator                // operator
    }

    ```

    - NULL

    ```
    {
        value:              String                  // null
        additionalOps:      Array   // functions or index calls on null
        castsAndNegations:  Array           // casts and negations (! +) before null
        operator:           Operator                // operator
    }

    ```

    - UNDEFINED

    ```
    {
        value:              String                  // undefined
        additionalOps:      Array   // functions or index calls on undefined
        castsAndNegations:  Array           // casts and negations (! +) before undefined
        operator:           Operator                // operator
    }

    ```

    - DEFINITION

    ```
    {
        name:               String                  // definition name
        additionalOps:      Array   // function or index calls on definition value
        castsAndNegations:  Array           // casts and negations (! +) before definition
        operator:           Operator                // operator
    }

    ```

    - FUNCTION\_START

    ```
    {
        castsAndNegations:  Array           // casts and negations (! +) before function call
    }

    ```

    - FUNCTION\_NAME

    ```
    {
        name:               String                  // function name
    }

    ```

    - FUNCTION\_ARG

    ```
    {
        argData:            Array            // collection of expression events
    }

    ```

    - FUNCTION\_END

    ```
    {
        additionalOps:      Array   // function or index calls on function
        operator:           Array           // operator
    }

    ```

    - INLINE\_FUNCTION\_START

    ```
    {
        castsAndNegations:  Array           // casts and negations (! +) before inline function
    }

    ```

    - INLINE\_FUNCTION\_ARG

    ```
    {
        arg:                Array            // collection of expression events
    }

    ```

    - INLINE\_FUNCTION\_CODE

    ```
    {
        value:              String                  // inline function code
    }

    ```

    - INLINE\_FUNCTION\_ARG\_DATA

    ```
    {
        argData:             Array            // collection of expression events
    }

    ```

    - INLINE\_FUNCTION\_END

    ```
    {
        additionalOps:      Array   // function or index calls on inline function
        operator:           Operator                // operator
    }

    ```

    - FUNCTION\_ONLY

    ```
    {
        castsAndNegations:  Array           // casts and negations (! +) before function name
        name:               String                  // function name
        operator:           Operator                // operator
    }

    ```
- Models used:

    - AdditionalCall

    ```
    {
        type:               String                  // property or function
        name:               Array           // property or function name
        args:               Array     // function arguments
    }

    ```

##### DeclarationStreamReader

[](#declarationstreamreader)

- Reads through and emits event for each declaration in code consisting of only declarations.
- Event emitted:

    - DECLARATION

    ```
    {
        declaration:        String                  // declaration name
        operator:           Operator                // operator
        value:              String                  // expression
    }

    ```

##### ExpressionInterpreter

[](#expressioninterpreter)

- Interprets expression using `ExpressionStreamReader` and in cases where inline functions are used `DeclarationInterpreter`, ultimately returning a `CustomDataType`.

```

```

- A `Context` is maintained throughout interpreting the expression which holds arbitary data which is required for processing certain areas of the expression. If you want to pass your own definitions to the expression interpreter, you can do the following:

```
