forked from patriksimek/vm2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* VMError. | ||
* | ||
* @param {String} message Error message. | ||
* | ||
* @class | ||
* @extends {Error} | ||
* @property {String} stack Call stack. | ||
* @property {String} message Error message. | ||
*/ | ||
|
||
class VMError extends Error { | ||
constructor(message) { | ||
super(message); | ||
|
||
this.name = 'VMError'; | ||
|
||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} | ||
|
||
module.exports = VMError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const VMError = require('./VMError') | ||
|
||
/** | ||
* Compile code to JS | ||
* | ||
* @param {String} code Code to compile. | ||
* @param {*} compiler Compiler to use. | ||
* @return {String} The compiled code. | ||
*/ | ||
|
||
const compileToJS = function(code, compiler) { | ||
if ('function' === typeof compiler) return compiler(code); | ||
|
||
switch (compiler) { | ||
case 'coffeescript': | ||
case 'coffee-script': | ||
case 'cs': | ||
case 'text/coffeescript': | ||
return require('coffee-script').compile(code, {header: false, bare: true}); | ||
|
||
case 'javascript': | ||
case 'java-script': | ||
case 'js': | ||
case 'text/javascript': | ||
return code; | ||
|
||
default: | ||
throw new VMError(`Unsupported compiler '${compiler}'.`); | ||
} | ||
}; | ||
|
||
module.exports = compileToJS; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters