Class: Execjs::Async::Context

Inherits:
ExecJS::ExternalRuntime::Context
  • Object
show all
Defined in:
lib/execjs-async.rb

Overview

extend from nodes external runtime context, and overwrite compile to use the async source.

Constant Summary collapse

ASYNC_SOURCE =
<<-'JAVASCRIPT'
  (function(program, execJS, module, exports, require) { execJS(program) })(function(callback) { #{source}
  }, function(program) {
    var output, print = function(string) {
      process.stdout.write('' + string);
    };
    try {
      program(function(result){
        if (typeof result == 'undefined' && result !== null) {
          print('["ok"]');
        } else {
          try {
            print(JSON.stringify(['ok', result]));
          } catch (err) {
            print('["err"]');
          }
        } 
      });
    } catch (err) {
      print(JSON.stringify(['err', '' + err]));
    }
  });
JAVASCRIPT

Instance Method Summary collapse

Instance Method Details

#compile(source) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/execjs-async.rb', line 34

def compile(source)        
  ASYNC_SOURCE.dup.tap do |output|
    output.sub!('#{source}') do
      source
    end
    output.sub!('#{encoded_source}') do
      encoded_source = encode_unicode_codepoints(source)
      MultiJson.encode("(function(){ #{encoded_source} })()")
    end
    output.sub!('#{json2_source}') do
      IO.read(ExecJS.root + "/support/json2.js")
    end
  end
end