-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeTransform.js
More file actions
64 lines (51 loc) · 1.94 KB
/
codeTransform.js
File metadata and controls
64 lines (51 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module.exports = {
transformCode: transform
}
var fs = require('fs');
var exec = require('child_process').exec;
var moduleName = 'FayFromJs';
var remoteFnName = 'remoteFn';
function transform(language, code, cb) {
switch (language) {
case 'Javascript':
cb(code); // no transform
break;
case 'Haskell':
var hsFile = "/tmp/" + moduleName +".hs";
fs.writeFile(hsFile, frameHaskell(code), function(err) {
if(err) {
console.log(err);
} else {
console.log("The haskell file was written. calling fay...");
var compile = fayCompileCommand(hsFile)
child = exec(compile, function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
var jsFile = "/tmp/" + moduleName + '.js';
fs.readFile(jsFile, function (err, data) {
console.log("finished compiling to js succesfully");
cb(wrapInFunc(haskellInjectJs(data))); // finally calling the original callback
});
});
}
});
break;
}
}
function frameHaskell(hsCode) {return 'module FayFromJs where\n' + hsCode;}
// injecting a function call to the designated user function at the end
// of the js code
function haskellInjectJs(jsCode) {
return jsCode + ';return ' + 'Strict.' + moduleName + '.' + remoteFnName + '(n);';
}
function wrapInFunc(code ) {return 'function remote_fn(n){' + code + '}';}
function fayCompileCommand(hsFile) { return "HASKELL_PACKAGE_SANDBOX=/home/dan/tools/fay/.cabal-sandbox/x86_64-linux-ghc-7.8.2-packages.conf.d fay " + hsFile + " --strict " + moduleName; }
/*
example function of how to use it
transform('Haskell', 'remoteFn x = x + 13\n', function (jsCode) {
args = ['n', "n = 2;" + jsCode];
myFunc = Function.apply(null, args);
console.log(myFunc());
});
*/