• This repository has been archived on 05/Jun/2020
  • Stars
    star
    134
  • Rank 270,967 (Top 6 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created almost 11 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Sophisticated meta programming in JavaScript, e.g. to build different versions of a library from a single source tree.

MetaScript

Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at compile time that would otherwise be done at runtime.

MetaScript is a tool for build time meta programming using JavaScript as the meta language. Written between the lines it enables developers to transform sources in pretty much every way possible.

Donate

How does it work?

If you already know JavaScript, adding some meta is as simple as remembering that:

  • //? begins a line of meta.
  • /*? begins a block of meta and */ ends it.
  • //?... begins a snippet of meta and //?. ends it.
  • ?= writes the expression's raw result to the document.
  • ?== writes the expression's typed result to the document (runs it through JSON.stringify).

MetaScript then turns the meta inside out, making it the actual program, that outputs the contents in between.

A simple example

Let's assume that you have a library and that you want its version number to be included as the constant MyLibrary.VERSION. With meta, this is as simple as:

MyLibrary.VERSION = /*?== VERSION */;
// or, alternatively, if VERSION is always string-safe:
MyLibrary.VERSION = "/*?= VERSION */";

This is what the meta program, when compiled, will look like:

  write('MyLibrary.VERSION = ');
write(JSON.stringify(VERSION));
  write(';\n');

Accordingly, a transformation of the source done by running that exact meta program with a scope of { VERSION: "1.0" } will result in:

MyLibrary.VERSION = "1.0";

It's just that simple and everything else is, of course, up to your imagination.

Advanced examples

Of course it's possible to do much more with it, like declaring macros and defining an entire set of useful utility functions, just like with any sort of preprocessor:

That's a globally available utility function as a snippet:

//?...
simpleIncludeExample = function(file) {
    write(indent(require("fs").readFileSync(file).toString("utf8")), __);
}
//?.

or, as a block:

/*? simpleIncludeExample = function(file) {
    write(indent(require("fs").readFileSync(file).toString("utf8")), __);
} */

Using it:

//? simpleIncludeExample("some/other/file.js")

This is, of course, just an example. See built-in utility for what's actually available out of the box.

That's a globally available macro using inline blocks:

//? ASSERT_OFFSET = function(varname) {
    if (/*?= varname */ < 0 || /*?= varname */ > this.capacity()) {
        throw RangeError("Illegal /*?= varname */");
    }
//? }

Using it:

function writeInt8(value, offset) {
    //? ASSERT_OFFSET('offset');
    ...
}

Results in:

function writeInt8(value, offset) {
    if (offset < 0 || offset > this.capacity()) {
        throw RangeError("Illegal offset");
    }
    ...
}

Some examples are available in the tests folder. While these are JavaScript examples, MetaScript should fit nicely with any other programming language that uses // ... and /* ... */ style comments.

API

The API is pretty much straight forward:

  • new MetaScript(source:string, filename:string)
    Creates a new instance with source, located at filename, compiled to a meta program.
  • MetaScript#program
    Contains the meta program's source.
  • MetaScript#transform(scope:Object):string
    Runs the meta program in the current context, transforming the source depending on what's defined in scope and returns the final source.

One step compilation / transformation:

  • MetaScript.compile(source:string):string
    Compiles the specified source to a meta program and returns its source.
  • MetaScript.transform(source:string, filename:string, scope:Object):string
    Compiles source, located at filename, to a meta program and transforms it using the specified scope in a new VM context.

Command line

Transforming sources on the fly is simple with node:

npm install -g metascript

 Usage: metascript sourcefile -SOMEDEFINE="some" -OTHERDEFINE="thing" [> outfile]

And in the case that you have to craft your own runtime, the raw compiler is also available as metac:

 Usage: metac sourcefile [> outfile]

Built-in utility

There are a few quite useful utility functions available to every meta program:

  • write(contents:string)
    Writes some raw data to the resulting document, which is equal to using /*?= contents */.

  • writeln(contents:string)
    Writes some raw data, followed by a line break, to the resulting document, which is equal to using //?= __+contents.

  • dirname(filename:string)
    Gets the directory name from a file name.

  • include(filename:string, absolute:boolean=)
    Includes another source file or multiple ones when using a glob expression. absolute defaults to false (relative)

  • indent(str:string, indent:string|number):string indents a block of text using the specified indentation given either as a whitespace string or number of whitespaces to use.

  • escapestr(str:string):string
    Escapes a string to be used inside of a single or double quote enclosed JavaScript string.

  • snip()
    Begins a snipping operation at the current offset of the output.

  • snap():string
    Ends a snipping operation, returning the (suppressed) output between the two calls to snip() and snap().

Additionally, there are a few internal variables. Most notably there is the variable __ (2x underscore) that remembers the current indentation level. This is used for example to indent included sources exactly like the meta block that contains the include call.

Using utility dependencies

In case this isn't obvious: Add the dependency to your package.json and, in MetaScript, use:

//? myutility = require('metascript-myutility')

Usage as a task

Documentation

License: Apache License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.html

More Repositories

1

bcrypt.js

Optimized bcrypt in plain JavaScript with zero dependencies.
JavaScript
3,300
star
2

long.js

A Long class for representing a 64-bit two's-complement integer value.
JavaScript
854
star
3

webassembly

A minimal toolkit and runtime to produce and run WebAssembly modules.
C
814
star
4

PSON

A super efficient binary serialization format for JSON.
JavaScript
459
star
5

ClosureCompiler.js

Closure Compiler for node.js - the all-round carefree package.
JavaScript
143
star
6

node.js-closure-compiler-externs

node.js externs for use with Closure Compiler.
JavaScript
76
star
7

Preprocessor.js

A JavaScript source file preprocessor in pure JavaScript, e.g. to build different versions of a library.
JavaScript
70
star
8

node-memcpy

Copies data between node Buffers and/or ArrayBuffers up to ~75 times faster than in pure JS.
JavaScript
54
star
9

utfx

A compact library to encode, decode and convert UTF8 / UTF16 in JavaScript.
JavaScript
53
star
10

btree.js

A ridiculously lean B-tree of variable orders in plain JavaScript.
JavaScript
45
star
11

WebAssembly-prototype

[OUTDATED] JavaScript tools for working with WebAssembly (WASM) binaries.
JavaScript
41
star
12

colour.js

A cored, fixed, documented and optimized version of the popular `colors.js`: Get colors in your node.js console like what...
JavaScript
33
star
13

node-harmonize

Enables --harmony flags programmatically.
JavaScript
25
star
14

BattleCon

A Battlefield / Frostbite engine RCON layer on node.js.
JavaScript
20
star
15

WebRcon

RCON over WebSocket client library and command line interface.
JavaScript
18
star
16

setup-node-nvm

Set up your GitHub Actions workflow with a specific version of node.js using nvm.
JavaScript
16
star
17

grunt-closurecompiler

The ClosureCompiler.js Grunt Task
JavaScript
15
star
18

test.js

A compact testing module for node.js.
JavaScript
14
star
19

dcodeio.github.io

JavaScript
13
star
20

purerc

JavaScript
12
star
21

IntN.js

A library for representing arbitrary byte size integers in JavaScript, both signed and unsigned.
JavaScript
12
star
22

JustMath.js

A rich toolset for two dimensional vector math.
JavaScript
11
star
23

opt.js

Probably the sole command line option parser you'll ever need to...
JavaScript
10
star
24

SharpJS

An embeddable, node.js-like JavaScript environment for Mono and .NET.
C#
10
star
25

lxiv

A compact library to encode and decode base64 data in JavaScript.
JavaScript
10
star
26

___wildcards

A party game for horrible people.
JavaScript
9
star
27

asc-native

AssemblyScript, JavaScript, WebAssembly, C, Binaryen, WABT, Clang, LLVM, CMake, Node.js, Visual Studio and Win32 walk into a bar...
C
9
star
28

ascli

A uniform foundation for unobtrusive (ASCII art in) cli apps.
JavaScript
9
star
29

wapi

A minimal yet viable Web-first Wasm/JS bridge.
TypeScript
7
star
30

quickjs

C
7
star
31

endecrypt

Password based en-/decryption of arbitrary data with and for node.js. http://dcode.io
JavaScript
5
star
32

node-BufferView

A DataView for node Buffers.
JavaScript
5
star
33

FalseSkill

A literal implementation of the Glicko-2 rating system in TypeScript.
JavaScript
4
star
34

doco

A JavaScript Documentation Generator.
JavaScript
4
star
35

PSONSharp

An implementation of Protocol JSON for .NET and Mono.
C#
4
star
36

SourceCon

Simple SRCDS RCON for node.js
JavaScript
3
star
37

BPlusTree.js

Santanu Basu's B+ tree implementation. Streamlined.
JavaScript
3
star
38

esm2umd

Transforms ESM to UMD, i.e. to use ESM by default with UMD as a legacy fallback.
JavaScript
2
star
39

SourceQry

JavaScript
1
star
40

raytrace

TypeScript
1
star
41

Friends

Universal friends plugin for the Oxide modding framework.
C#
1
star
42

llvm.js

C
1
star
43

asext

JavaScript
1
star