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
|
# File 'lib/schmooze/processor_generator.rb', line 6
def generate(imports, methods)
%{try {
#{imports.map {|import| generate_import(import)}.join}} catch (e) {
process.stdout.write(JSON.stringify(['err', e.toString()]));
process.stdout.write("\\n");
process.exit(1);
}
process.stdout.write("[\\"ok\\"]\\n");
var __methods__ = {};
#{methods.map{ |method| generate_method(method[:name], method[:code]) }.join}
function __handle_error__(error) {
if (error instanceof Error) {
process.stdout.write(JSON.stringify(['err', error.toString().replace(new RegExp('^' + error.name + ': '), ''), error.name]));
} else {
process.stdout.write(JSON.stringify(['err', error.toString()]));
}
process.stdout.write("\\n");
}
require('readline').createInterface({
input: process.stdin,
terminal: false,
}).on('line', function(line) {
var input = JSON.parse(line);
try {
Promise.resolve(__methods__[input[0]].apply(null, input[1])
).then(function (result) {
process.stdout.write(JSON.stringify(['ok', result]));
process.stdout.write("\\n");
}).catch(__handle_error__);
} catch(error) {
__handle_error__(error);
}
});
}
end
|