emscripten-library-decorator
Advanced tools
Comparing version 0.1.5 to 0.1.6
92
index.ts
@@ -1,6 +0,21 @@ | ||
// @dep decorator. | ||
// Apply to a function, to export other variables whenever it's used. | ||
// Arguments can be functions to export or names of global variables. | ||
// This file is part of emscripten-library-decorator, | ||
// copyright (c) 2015-2016 BusFaster Ltd. | ||
// Released under the MIT license, see LICENSE. | ||
function dep(...depList: any[]) { | ||
var evil: (code: string) => any; | ||
/** Allow decorators to call eval() in the context that called them. | ||
* This is needed for various transformations. | ||
* @param otherEval must be this function: (code: string) => eval(code) */ | ||
export function setEvil(otherEval: (code: string) => any) { | ||
evil = otherEval; | ||
} | ||
/** @dep decorator. | ||
* Apply to a function, to list other required variables needing protection | ||
* from dead code removal. | ||
* Arguments can be functions or names of global variables. */ | ||
export function dep(...depList: (((...args: any[]) => any) | string)[]) { | ||
return((target: Object, functionName: string) => { | ||
@@ -26,3 +41,3 @@ // Export names of other functions required by <functionName> | ||
// looking it up by name in current scope. | ||
if(name != 'initNamespaces') lib[name] = eval('(' + dep + ')'); | ||
if(name != 'initNamespaces') lib[name] = evil('(' + dep + ')'); | ||
} | ||
@@ -36,6 +51,6 @@ | ||
// @exportLibrary decorator. | ||
// Apply to a class with static methods, to export them as functions. | ||
/** @exportLibrary decorator. | ||
* Apply to a class with static methods, to export them as functions. */ | ||
function exportLibrary(target: any) { | ||
export function exportLibrary(target: any) { | ||
mergeInto(LibraryManager.library, target); | ||
@@ -46,8 +61,27 @@ } | ||
// @exportNamespace decorator. | ||
// Apply to an empty, named dummy class defined at the end of the namespace, | ||
// to prepare the entire namespace for exporting and merge its content | ||
// defined in several source files into a single object. | ||
/** @prepareNamespace decorator. | ||
* Apply to an empty, named dummy class defined at the end of the namespace | ||
* block, to prepare its contents for export in an Emscripten library. | ||
* Namespaces with matching names in different files are merged together. | ||
* All code in the block is separated because Emscripten only outputs global | ||
* functions, not methods. */ | ||
function exportNamespace(name: string) { | ||
export function prepareNamespace(name: string) { | ||
return((target: any) => { | ||
var body = evil('__decorate').caller.caller.toString(); | ||
var prefix = new RegExp('^[ (]*function *\\( *' + name + ' *\\) *\\{'); | ||
var suffix = new RegExp('var +' + target.name + ' *= *[^]*$'); | ||
body = (namespaceBodyTbl[name] || '') + body.replace(prefix, '').replace(suffix, ''); | ||
namespaceBodyTbl[name] = body; | ||
}); | ||
} | ||
/** Call once per namespace at the global level, after all files with contents | ||
* in that namespace have been imported. Clears the namespace and exports a | ||
* "postset" function to populate it using its original code. */ | ||
export function publishNamespace(name: string) { | ||
var exportName = name.substr(1); | ||
@@ -58,8 +92,8 @@ | ||
eval(name + '={};'); | ||
evil(name + '={};'); | ||
var lib: _Library = { | ||
_extends: __extends, | ||
_decorate: __decorate, | ||
defineHidden: _defineHidden | ||
_extends: evil('__extends'), | ||
_decorate: evil('__decorate'), | ||
defineHidden: defineHidden | ||
}; | ||
@@ -71,10 +105,9 @@ | ||
mergeInto(LibraryManager.library, lib); | ||
return((target: any) => {}); | ||
} | ||
// @_defineHidden decorator. | ||
// Apply to a property to protect it from modifications and hide it. | ||
/** @_defineHidden decorator. | ||
* Assign to a local variable called _defineHidden before using. | ||
* Apply to a property to protect it from modifications and hide it. */ | ||
function _defineHidden(value?: any) { | ||
export function defineHidden(value?: any) { | ||
return((target: Object, key: string) => { | ||
@@ -89,16 +122,1 @@ Object.defineProperty(target, key, { | ||
} | ||
// Typescript internal shim functions. | ||
declare var __decorate: any; | ||
declare var __extends: any; | ||
// Declarations of some globals provided by Emscripten to its libraries. | ||
interface _Library { [name: string]: any } | ||
declare var LibraryManager: { | ||
library: _Library; | ||
}; | ||
declare function mergeInto(target: _Library, extension: _Library): void; |
{ | ||
"name": "emscripten-library-decorator", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Decorators for writing Emscripten libraries", | ||
"homepage": "https://github.com/charto/emscripten-library-decorator", | ||
"bugs": { | ||
"url": "https://github.com/charto/emscripten-library-decorator/issues" | ||
"main": "index.js", | ||
"typings": "index.d.ts", | ||
"scripts": { | ||
"tsc": "tsc", | ||
"docts": "docts", | ||
"prepublish": "tsc", | ||
"test": "node index.js" | ||
}, | ||
"author": "Juha Järvi", | ||
"license": "MIT", | ||
"main": "index.ts", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/charto/emscripten-library-decorator.git" | ||
"url": "git+https://github.com/charto/emscripten-library-decorator.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/charto/emscripten-library-decorator/issues" | ||
}, | ||
"homepage": "https://github.com/charto/emscripten-library-decorator", | ||
"devDependencies": { | ||
"docts": "~0.0.4", | ||
"typescript": "^1.8.10" | ||
} | ||
} |
emscripten-library-decorator | ||
============================ | ||
This package provides decorators for writing Emscripten libraries. Include them in a TypeScript source file by adding a `reference path` in the beginning pointing to `index.ts` like: | ||
This package provides decorators for writing Emscripten libraries. | ||
```typescript | ||
/// <reference path="node_modules/emscripten-library-decorator/index.ts" /> | ||
function _print(message: string) { | ||
@@ -32,2 +30,62 @@ console.log(message); | ||
API | ||
=== | ||
Docs generated using [`docts`](https://github.com/charto/docts) | ||
> | ||
> <a name="api-defineHidden"></a> | ||
> ### Function [`defineHidden`](#api-defineHidden) | ||
> <em>@_defineHidden decorator.</em> | ||
> <em>Assign to a local variable called _defineHidden before using.</em> | ||
> <em>Apply to a property to protect it from modifications and hide it.</em> | ||
> Source code: [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L107-L116) | ||
> > **defineHidden( )** <sup>⇒ <code>(target: Object, key: string) => void</code></sup> [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L107-L116) | ||
> >  ▫ value<sub>?</sub> <sup><code>any</code></sup> | ||
> | ||
> <a name="api-dep"></a> | ||
> ### Function [`dep`](#api-dep) | ||
> <em>@dep decorator.</em> | ||
> <em>Apply to a function, to list other required variables needing protection</em> | ||
> <em>from dead code removal.</em> | ||
> <em>Arguments can be functions or names of global variables.</em> | ||
> Source code: [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L20-L48) | ||
> > **dep( )** <sup>⇒ <code>(target: Object, functionName: string) => void</code></sup> [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L20-L48) | ||
> >  ▪ depList <sup><code>((...args: any[]) => any | string)[]</code></sup> | ||
> | ||
> <a name="api-exportLibrary"></a> | ||
> ### Function [`exportLibrary`](#api-exportLibrary) | ||
> <em>@exportLibrary decorator.</em> | ||
> <em>Apply to a class with static methods, to export them as functions.</em> | ||
> Source code: [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L53-L55) | ||
> > **exportLibrary( )** <sup>⇒ <code>void</code></sup> [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L53-L55) | ||
> >  ▪ target <sup><code>any</code></sup> | ||
> | ||
> <a name="api-prepareNamespace"></a> | ||
> ### Function [`prepareNamespace`](#api-prepareNamespace) | ||
> <em>@prepareNamespace decorator.</em> | ||
> <em>Apply to an empty, named dummy class defined at the end of the namespace</em> | ||
> <em>block, to prepare its contents for export in an Emscripten library.</em> | ||
> <em>Namespaces with matching names in different files are merged together.</em> | ||
> <em>All code in the block is separated because Emscripten only outputs global</em> | ||
> <em>functions, not methods.</em> | ||
> Source code: [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L66-L77) | ||
> > **prepareNamespace( )** <sup>⇒ <code>(target: any) => void</code></sup> [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L66-L77) | ||
> >  ▪ name <sup><code>string</code></sup> | ||
> | ||
> <a name="api-publishNamespace"></a> | ||
> ### Function [`publishNamespace`](#api-publishNamespace) | ||
> <em>Call once per namespace at the global level, after all files with contents</em> | ||
> <em>in that namespace have been imported. Clears the namespace and exports a</em> | ||
> <em>"postset" function to populate it using its original code.</em> | ||
> Source code: [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L83-L101) | ||
> > **publishNamespace( )** <sup>⇒ <code>void</code></sup> [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L83-L101) | ||
> >  ▪ name <sup><code>string</code></sup> | ||
> | ||
> <a name="api-setEvil"></a> | ||
> ### Function [`setEvil`](#api-setEvil) | ||
> <em>Allow decorators to call eval() in the context that called them.</em> | ||
> <em>This is needed for various transformations.</em> | ||
> Source code: [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L11-L13) | ||
> > **setEvil( )** <sup>⇒ <code>void</code></sup> [`<>`](http://github.com/charto/emscripten-library-decorator/blob/74128b8/index.ts#L11-L13) | ||
> >  ▪ otherEval <sup><code>(code: string) => any</code></sup> <em>must be this function: (code: string) => eval(code)</em> | ||
License | ||
@@ -37,3 +95,2 @@ ------- | ||
Copyright (c) 2015 BusFaster Ltd | ||
Copyright (c) 2015-2016 BusFaster Ltd |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
17215
9
238
95
2
1