mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
commit
a174aad071
4 changed files with 81 additions and 0 deletions
|
@ -8,16 +8,22 @@ matrix:
|
|||
include:
|
||||
- rvm: 2.0.0
|
||||
env: EXECJS_RUNTIME=Node
|
||||
- rvm: 2.0.0
|
||||
env: EXECJS_RUNTIME=Duktape
|
||||
- rvm: 2.0.0
|
||||
env: EXECJS_RUNTIME=RubyRacer
|
||||
|
||||
- rvm: 2.1
|
||||
env: EXECJS_RUNTIME=Node
|
||||
- rvm: 2.1
|
||||
env: EXECJS_RUNTIME=Duktape
|
||||
- rvm: 2.1
|
||||
env: EXECJS_RUNTIME=RubyRacer
|
||||
|
||||
- rvm: 2.2
|
||||
env: EXECJS_RUNTIME=Node
|
||||
- rvm: 2.2
|
||||
env: EXECJS_RUNTIME=Duktape
|
||||
- rvm: 2.2
|
||||
env: EXECJS_RUNTIME=RubyRacer
|
||||
|
||||
|
@ -30,5 +36,7 @@ matrix:
|
|||
env: EXECJS_RUNTIME=JavaScriptCore
|
||||
- os: osx
|
||||
env: EXECJS_RUNTIME=Node
|
||||
- os: osx
|
||||
env: EXECJS_RUNTIME=Duktape
|
||||
- os: osx
|
||||
env: EXECJS_RUNTIME=RubyRacer
|
||||
|
|
1
Gemfile
1
Gemfile
|
@ -3,6 +3,7 @@ source 'https://rubygems.org'
|
|||
gemspec
|
||||
|
||||
group :test do
|
||||
gem 'duktape', platform: :mri
|
||||
gem 'therubyracer', platform: :mri
|
||||
gem 'therubyrhino', ">=1.73.3", platform: :jruby
|
||||
gem 'minitest', require: false
|
||||
|
|
68
lib/execjs/duktape_runtime.rb
Normal file
68
lib/execjs/duktape_runtime.rb
Normal file
|
@ -0,0 +1,68 @@
|
|||
require "execjs/runtime"
|
||||
require "json"
|
||||
|
||||
module ExecJS
|
||||
class DuktapeRuntime < Runtime
|
||||
class Context < Runtime::Context
|
||||
def initialize(runtime, source = "")
|
||||
@ctx = Duktape::Context.new(complex_object: nil)
|
||||
@ctx.exec_string(encode(source), '(execjs)')
|
||||
rescue Exception => e
|
||||
raise wrap_error(e)
|
||||
end
|
||||
|
||||
def exec(source, options = {})
|
||||
return unless /\S/ =~ source
|
||||
@ctx.eval_string("(function(){#{encode(source)}})()", '(execjs)')
|
||||
rescue Exception => e
|
||||
raise wrap_error(e)
|
||||
end
|
||||
|
||||
def eval(source, options = {})
|
||||
return unless /\S/ =~ source
|
||||
@ctx.eval_string("(#{encode(source)})", '(execjs)')
|
||||
rescue Exception => e
|
||||
raise wrap_error(e)
|
||||
end
|
||||
|
||||
def call(identifier, *args)
|
||||
@ctx.call_prop(identifier.split("."), *args)
|
||||
rescue Exception => e
|
||||
raise wrap_error(e)
|
||||
end
|
||||
|
||||
private
|
||||
def wrap_error(e)
|
||||
klass = case e
|
||||
when Duktape::SyntaxError
|
||||
RuntimeError
|
||||
when Duktape::Error
|
||||
ProgramError
|
||||
when Duktape::InternalError
|
||||
RuntimeError
|
||||
end
|
||||
|
||||
if klass
|
||||
re = / \(line (\d+)\)$/
|
||||
lineno = e.message[re, 1] || 1
|
||||
error = klass.new(e.message.sub(re, ""))
|
||||
error.set_backtrace(["(execjs):#{lineno}"] + e.backtrace)
|
||||
error
|
||||
else
|
||||
e
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def name
|
||||
"Duktape"
|
||||
end
|
||||
|
||||
def available?
|
||||
require "duktape"
|
||||
true
|
||||
rescue LoadError
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,6 @@
|
|||
require "execjs/module"
|
||||
require "execjs/disabled_runtime"
|
||||
require "execjs/duktape_runtime"
|
||||
require "execjs/external_runtime"
|
||||
require "execjs/ruby_racer_runtime"
|
||||
require "execjs/ruby_rhino_runtime"
|
||||
|
@ -8,6 +9,8 @@ module ExecJS
|
|||
module Runtimes
|
||||
Disabled = DisabledRuntime.new
|
||||
|
||||
Duktape = DuktapeRuntime.new
|
||||
|
||||
RubyRacer = RubyRacerRuntime.new
|
||||
|
||||
RubyRhino = RubyRhinoRuntime.new
|
||||
|
@ -70,6 +73,7 @@ module ExecJS
|
|||
|
||||
def self.runtimes
|
||||
@runtimes ||= [
|
||||
Duktape,
|
||||
RubyRacer,
|
||||
RubyRhino,
|
||||
JavaScriptCore,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue