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

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