2.0.1 • Published 7 years ago

al-compiler v2.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
7 years ago

al-compiler

Could we add macro to javascript or something similar ? Could we generate function, or module at runtime and use them ?

"May it was not a good practice, but so funny" :)

So, use it only if you known what you do, and may you shouldn't use this in production...

so, let's go to the dark side of the force.

How to generate function/module

We can do it (yes !) by :

  • use a template system: Mustache
  • call this template with parameters in order to generate our business

The result will be our complete module definition, or a single function definition.

If we generate an object or a function, we can add an export directive like this one:

'use strict';
module.exports = YOUR_GENERATED_CODE

At this point, we have generate some code, how to dynamicaly load it in our application ?

Old way

  • save it to a temporary file (at build time, or at runtime)
  • make a 'require' on it

Cons:

  • file system based
  • no context isolation

Pro:

  • simple to use
  • module is used in classic way (single init, ...)

A better way

  • use 'vm' module from node js.
  • use context definition to exchange data and code between caller/target

Cons:

  • if singleton is needed, we have to implement it.

Example with a simple function

Goal: generate a filter function like this one bellow, with a dynamic list of properties to test.

With 'A' and 'B':

(e) => true && !!e['A'] && !!e['B'];

With 'A' and 'B' and 'C':

(e) => true && !!e['A'] && !!e['B'] && !!e['C'];

So, by using this template:

(e) => true{{#tags}}  && (!!e['{{.}}']){{/tags}};

and call template with a array of tags we're done.

Sample code:

const ALC = require('al-compiler');

const TEMPLATE= "(e) => true{{#tags}}  && (!!e['{{.}}']){{/tags}};";

// fn is a function which return true if e has 'a' and 'b' property.
const fn = ALC.fnFactory(TEMPLATE, {tags: ['a', 'b']})();

const a = {a:1, b:2};
const b = { c: 1};

console.log("a: " + fn(a)); // ==> true
console.log("b: " + fn(b)); // ==> false

Behind the scene this tool:

  • use mustache to create template function
  • call this template with view of {tags: ... }
  • load it in your current context

Install

npm install al-compiler

API

See documentation

This module exports :

  • pop: instanciate a module from source code
  • fnFactory: a function to generate a set of function based on same template/view
  • Mustache: mustache instance what else.

Change log

2.0.1

  • add test unit with require directive

2.0.0

  • use mustache has template engine (no dependencies, more functionality, ...)
  • use node vm module to instance code
  • new API

1.0.1

  • expose internal method: compile, cleanup and addModuleExportDirective
  • bug fix on generate: don't add export if exportCheck === false

1.0.0

  • initial API : instanciate, generate, instanciateFromCode
2.0.1

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago