therubyracer 0.8.2.pre → 0.8.2.pre2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of therubyracer might be problematic. Click here for more details.

data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .bundle
2
+ .rvmrc
2
3
  Gemfile.lock
3
4
  v8.bundle
4
5
  v8.so
data/Changelog.md CHANGED
@@ -2,7 +2,10 @@
2
2
 
3
3
  ## Edge
4
4
 
5
+ * expose the V8 debugger via V8::C::Debug::EnableAgent()
5
6
  * force UTf-8 encoding on strings returned from javascript in ruby 1.9
7
+ * remove deprecated evaluate() methods
8
+ * make the currently executing JavaScript stack available via Context#stack
6
9
 
7
10
  ## 0.8.1 - 2011/03/07
8
11
 
data/README.md CHANGED
@@ -126,11 +126,11 @@ More documentation can be found on the [github wiki](https://github.com/cowboyd/
126
126
 
127
127
 
128
128
  ## DEVELOP
129
- * git clone git://github.com/cowboyd/therubyracer.git
130
- * cd therubyracer
131
- * git submodule update --init
132
- * bundle install
133
- * rake compile
129
+ git clone git://github.com/cowboyd/therubyracer.git
130
+ cd therubyracer
131
+ git submodule update --init
132
+ bundle install
133
+ rake compile
134
134
 
135
135
  ## LICENSE:
136
136
 
data/ext/v8/v8.cpp CHANGED
@@ -13,6 +13,8 @@
13
13
  #include "v8_external.h"
14
14
  #include "v8_exception.h"
15
15
  #include "v8_locker.h"
16
+ #include "v8_debug.h"
17
+ #include "v8_v8.h"
16
18
 
17
19
  #include <stdio.h>
18
20
 
@@ -37,5 +39,7 @@ extern "C" {
37
39
  rr_init_v8_external();
38
40
  rr_init_v8_exception();
39
41
  rr_init_v8_locker();
42
+ rr_init_v8_debug();
43
+ rr_init_v8_v8();
40
44
  }
41
45
  }
@@ -0,0 +1,17 @@
1
+ #include "rr.h"
2
+ #include "v8_debug.h"
3
+ #include "v8-debug.h"
4
+
5
+ using namespace v8;
6
+
7
+ namespace {
8
+
9
+ VALUE EnableAgent(VALUE self, VALUE application_name, VALUE port) {
10
+ return rr_v82rb(v8::Debug::EnableAgent(RSTRING_PTR(application_name), NUM2INT(port), false));
11
+ }
12
+ }
13
+
14
+ void rr_init_v8_debug() {
15
+ VALUE DebugModule = rr_define_module("Debug");
16
+ rr_define_singleton_method(DebugModule, "EnableAgent", EnableAgent, 2);
17
+ }
data/ext/v8/v8_debug.h ADDED
@@ -0,0 +1,6 @@
1
+ #ifndef _RR_V8_DEBUG_
2
+ #define _RR_V8_DEBUG_
3
+
4
+ void rr_init_v8_debug();
5
+
6
+ #endif
@@ -69,21 +69,27 @@ namespace {
69
69
  return V8_Ref_Get<StackFrame>(value);
70
70
  }
71
71
  VALUE GetLineNumber(VALUE self) {
72
+ HandleScope scope;
72
73
  return rr_v82rb(frame(self)->GetLineNumber());
73
74
  }
74
75
  VALUE GetColumn(VALUE self) {
76
+ HandleScope scope;
75
77
  return rr_v82rb(frame(self)->GetColumn());
76
78
  }
77
79
  VALUE GetScriptName(VALUE self) {
80
+ HandleScope scope;
78
81
  return rr_v82rb(frame(self)->GetScriptName());
79
82
  }
80
83
  VALUE GetFunctionName(VALUE self) {
84
+ HandleScope scope;
81
85
  return rr_v82rb(frame(self)->GetFunctionName());
82
86
  }
83
87
  VALUE IsEval(VALUE self) {
88
+ HandleScope scope;
84
89
  return rr_v82rb(frame(self)->IsEval());
85
90
  }
86
91
  VALUE IsConstructor(VALUE self) {
92
+ HandleScope scope;
87
93
  return rr_v82rb(frame(self)->IsConstructor());
88
94
  }
89
95
  }
@@ -102,7 +108,7 @@ void rr_init_v8_exception() {
102
108
  rr_define_singleton_method(ExceptionClass, "Error", Error, 1);
103
109
 
104
110
  StackTraceClass = rr_define_class("StackTrace");
105
- rr_define_singleton_method(StackTraceClass, "CurrentStackTrace", Trace::CurrentStackTrace, 0);
111
+ rr_define_singleton_method(StackTraceClass, "CurrentStackTrace", Trace::CurrentStackTrace, 1);
106
112
  rr_define_method(StackTraceClass, "GetFrame", Trace::GetFrame, 1);
107
113
  rr_define_method(StackTraceClass, "GetFrameCount", Trace::GetFrameCount, 0);
108
114
  rr_define_method(StackTraceClass, "AsArray", Trace::AsArray, 0);
@@ -123,6 +129,6 @@ void rr_init_v8_exception() {
123
129
  VALUE rr_reflect_v8_stacktrace(Handle<StackTrace> value) {
124
130
  return rr_v8_ref_create(StackTraceClass, value);
125
131
  }
126
- VALUE rr_reflect_v8_stackframe(Handle<StackTrace> value) {
132
+ VALUE rr_reflect_v8_stackframe(Handle<StackFrame> value) {
127
133
  return rr_v8_ref_create(StackFrameClass, value);
128
134
  }
data/ext/v8/v8_func.cpp CHANGED
@@ -39,9 +39,11 @@ namespace {
39
39
  return rr_v8_ref_create(rr_cV8_C_Object, function->NewInstance(argc, argv));
40
40
  }
41
41
  VALUE GetName(VALUE self) {
42
+ HandleScope scope;
42
43
  return rr_v82rb(unwrap(self)->GetName());
43
44
  }
44
45
  VALUE SetName(VALUE self, VALUE name) {
46
+ HandleScope scope;
45
47
  Local<String> str = V8_Ref_Get<String>(name);
46
48
  unwrap(self)->SetName(str);
47
49
  return Qnil;
data/ext/v8/v8_v8.cpp ADDED
@@ -0,0 +1,28 @@
1
+
2
+ #include "rr.h"
3
+
4
+ using namespace v8;
5
+
6
+ namespace {
7
+
8
+ VALUE IsDead(VALUE self) {
9
+ return rr_v82rb(V8::IsDead());
10
+ }
11
+
12
+ VALUE AdjustAmountOfExternalAllocatedMemory(VALUE self, VALUE bytes) {
13
+ V8::AdjustAmountOfExternalAllocatedMemory(NUM2INT(bytes));
14
+ return Qnil;
15
+ }
16
+
17
+ VALUE IdleNotification(VALUE self) {
18
+ return rr_v82rb(V8::IdleNotification());
19
+ }
20
+ }
21
+
22
+
23
+ void rr_init_v8_v8() {
24
+ VALUE V8Module = rr_define_module("V8");
25
+ rr_define_singleton_method(V8Module, "IsDead", IsDead, 0);
26
+ rr_define_singleton_method(V8Module, "AdjustAmountOfExternalAllocatedMemory", AdjustAmountOfExternalAllocatedMemory, 1);
27
+ rr_define_singleton_method(V8Module, "IdleNotification", IdleNotification, 0);
28
+ }
data/ext/v8/v8_v8.h ADDED
@@ -0,0 +1,6 @@
1
+ #ifndef _RR_V8_V8_
2
+ #define _RR_V8_V8_
3
+
4
+ void rr_init_v8_v8();
5
+
6
+ #endif
data/lib/v8.rb CHANGED
@@ -13,4 +13,5 @@ module V8
13
13
  require 'v8/tap'
14
14
  require 'v8/access'
15
15
  require 'v8/error'
16
+ require 'v8/stack'
16
17
  end
data/lib/v8/context.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require 'stringio'
2
2
 
3
3
  module V8
4
- class Context
4
+ class Context
5
5
  attr_reader :native, :scope, :access
6
+
6
7
  def initialize(opts = {})
7
8
  @access = Access.new
8
9
  @to = Portal.new(self, @access)
@@ -11,10 +12,11 @@ module V8
11
12
  @global = @native.Global()
12
13
  @scope = @to.rb(@global)
13
14
  @global.SetHiddenValue(C::String::New("TheRubyRacer::RubyObject"), C::External::New(opts[:with])) if opts[:with]
15
+ @global.SetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyContext"), C::External::New(self))
14
16
  end
15
17
  yield(self) if block_given?
16
18
  end
17
-
19
+
18
20
  def eval(javascript, filename = "<eval>", line = 1)
19
21
  if IO === javascript || StringIO === javascript
20
22
  javascript = javascript.read()
@@ -39,31 +41,29 @@ module V8
39
41
  raise err if err
40
42
  return value
41
43
  end
42
-
43
- def evaluate(*args)
44
- self.eval(*args)
45
- end
46
-
44
+
47
45
  def load(filename)
48
46
  File.open(filename) do |file|
49
- evaluate file, filename, 1
50
- end
47
+ self.eval file, filename, 1
48
+ end
51
49
  end
52
-
50
+
53
51
  def [](key)
54
52
  @scope[key]
55
53
  end
56
-
54
+
57
55
  def []=(key, value)
58
56
  @scope[key] = value
59
57
  end
60
58
 
61
- def self.eval(source)
62
- new.eval(source)
63
- end
64
-
65
- def V8.eval(*args)
66
- V8::Context.eval(*args)
59
+ def self.stack(limit = 99)
60
+ if native = C::Context::GetEntered()
61
+ global = native.Global().instance_eval {@native}
62
+ cxt = global.GetHiddenValue(C::String::NewSymbol("TheRubyRacer::RubyContext")).Value()
63
+ cxt.instance_eval {@to.rb(C::StackTrace::CurrentStackTrace(limit))}
64
+ else
65
+ []
66
+ end
67
67
  end
68
68
  end
69
69
 
data/lib/v8/function.rb CHANGED
@@ -24,5 +24,19 @@ module V8
24
24
  to.rb(@native.NewInstance(to.v8(args)))
25
25
  end
26
26
  end
27
+
28
+ def name
29
+ @portal.open do |to|
30
+ to.rb(@native.GetName())
31
+ end
32
+ end
33
+
34
+ def name=(name)
35
+ name.tap do
36
+ @portal.open do |to|
37
+ @native.SetName(to.v8(name))
38
+ end
39
+ end
40
+ end
27
41
  end
28
42
  end
data/lib/v8/portal.rb CHANGED
@@ -78,12 +78,13 @@ module V8
78
78
 
79
79
  def rb(value)
80
80
  case value
81
- when V8::C::Function then peer(value) {V8::Function}
82
- when V8::C::Array then peer(value) {V8::Array}
83
- when V8::C::Object then peer(value) {V8::Object}
84
- when V8::C::String then value.Utf8Value.tap {|s| return s.respond_to?(:force_encoding) ? s.force_encoding("UTF-8") : s}
85
- when V8::C::Date then Time.at(value.NumberValue() / 1000)
86
- when V8::C::Value then nil if value.IsEmpty()
81
+ when V8::C::Function then peer(value) {V8::Function}
82
+ when V8::C::Array then peer(value) {V8::Array}
83
+ when V8::C::Object then peer(value) {V8::Object}
84
+ when V8::C::String then value.Utf8Value.tap {|s| return s.respond_to?(:force_encoding) ? s.force_encoding("UTF-8") : s}
85
+ when V8::C::Date then Time.at(value.NumberValue() / 1000)
86
+ when V8::C::StackTrace then V8::StackTrace.new(self, value)
87
+ when V8::C::Value then nil if value.IsEmpty()
87
88
  else
88
89
  value
89
90
  end
data/lib/v8/stack.rb ADDED
@@ -0,0 +1,66 @@
1
+
2
+ module V8
3
+
4
+ class StackTrace
5
+ include Enumerable
6
+
7
+ def initialize(to, native)
8
+ @to = to
9
+ @native = native
10
+ end
11
+
12
+ def length
13
+ @native.GetFrameCount()
14
+ end
15
+
16
+ def each
17
+ for i in 0..length - 1
18
+ yield V8::StackFrame.new(@to, @native.GetFrame(i))
19
+ end
20
+ end
21
+
22
+ def to_s
23
+ map {|f|"at #{f}"}.join("\n")
24
+ end
25
+ end
26
+
27
+ class StackFrame
28
+
29
+ def initialize(portal, native)
30
+ @to = portal
31
+ @native = native
32
+ end
33
+
34
+ def script_name
35
+ @to.rb(@native.GetScriptName())
36
+ end
37
+
38
+ def function_name
39
+ @to.rb(@native.GetFunctionName())
40
+ end
41
+
42
+ def line_number
43
+ @native.GetLineNumber()
44
+ end
45
+
46
+ def column
47
+ @native.GetColumn()
48
+ end
49
+
50
+ def eval?
51
+ @native.IsEval()
52
+ end
53
+
54
+ def constructor?
55
+ @native.IsConstructor()
56
+ end
57
+
58
+ def to_s
59
+ if @native.GetFunctionName()
60
+ "#{function_name} (#{script_name}:#{line_number}:#{column})"
61
+ else
62
+ "#{script_name}:#{line_number}:#{column}"
63
+ end
64
+ end
65
+ end
66
+ end
data/lib/v8/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module V8
2
- VERSION = "0.8.2.pre"
2
+ VERSION = "0.8.2.pre2"
3
3
  end
data/spec/ext/cxt_spec.rb CHANGED
@@ -4,9 +4,44 @@ require "#{File.dirname(__FILE__)}/../spec_helper.rb"
4
4
  include V8
5
5
 
6
6
  describe C::Context do
7
-
7
+
8
8
  it "should not have a current context if no context is open" do
9
9
  C::Context::GetEntered().should be_nil
10
10
  end
11
11
 
12
+ it "can get the current javascript execution stack" do
13
+ V8::Context.new do |cxt|
14
+ cxt['getTrace'] = lambda do
15
+ V8::Context.stack
16
+ end
17
+ trace = cxt.eval(<<-JS, 'trace.js')
18
+ function one() {
19
+ return two();
20
+ }
21
+
22
+ function two() {
23
+ return three();
24
+ }
25
+
26
+ function three() {
27
+ return getTrace()
28
+ }
29
+ one();
30
+ JS
31
+
32
+ trace.length.should be(4)
33
+ trace.first.tap do |frame|
34
+ frame.line_number.should == 10
35
+ frame.column.should == 16
36
+ frame.script_name.should == 'trace.js'
37
+ frame.function_name.should == 'three'
38
+ frame.should_not be_eval
39
+ frame.should_not be_constructor
40
+ end
41
+ end
42
+ end
43
+
44
+ it "has an empty stack if there is no enterned context" do
45
+ V8::Context.stack.should be_empty
46
+ end
12
47
  end
@@ -46,4 +46,19 @@ describe C::Function do
46
46
  cxt.eval('say("Hello", 3)').should == "HelloHelloHello"
47
47
  end
48
48
  end
49
+
50
+ it "has a name" do
51
+ Context.new do |cxt|
52
+ f = cxt.eval('(function hi() {return "Hello World"})', '<eval>')
53
+ f.name.should == "hi"
54
+ end
55
+ end
56
+
57
+ it "can have its name set" do
58
+ Context.new do |cxt|
59
+ f = cxt.eval('(function () {return "Goodbye World"})', '<eval>')
60
+ f.name = 'bye'
61
+ f.name.should == 'bye'
62
+ end
63
+ end
49
64
  end
@@ -39,7 +39,7 @@ describe "Ruby Javascript API" do
39
39
  end
40
40
 
41
41
  it "treats nil and the empty string as the same thing when it comes to eval" do
42
- @cxt.eval(nil).should == Context.eval('')
42
+ @cxt.eval(nil).should == @cxt.eval('')
43
43
  end
44
44
 
45
45
  it "can pass back strings to ruby" do
@@ -406,7 +406,8 @@ describe "Ruby Javascript API" do
406
406
  end
407
407
  o = Class.new.class_eval do
408
408
  include m
409
- end.new
409
+ new
410
+ end
410
411
  Context.new(:with => o) do |cxt|
411
412
  cxt.eval('this.foo').should == "FOO"
412
413
  cxt.eval('this.foo = "bar!"')
@@ -787,14 +788,9 @@ describe "Ruby Javascript API" do
787
788
  end
788
789
 
789
790
  it "can load a file into the runtime" do
790
- mock(:JavascriptSourceFile).tap do |file|
791
- File.should_receive(:open).with("path/to/mysource.js").and_yield(file)
792
- Context.new do |cxt|
793
- cxt.should_receive(:evaluate).with(file, "path/to/mysource.js", 1)
794
- cxt.load("path/to/mysource.js")
795
- end
791
+ Context.new do |cxt|
792
+ cxt.load(Pathname(__FILE__).dirname.join("loadme.js")).should == "I am Legend"
796
793
  end
797
-
798
794
  end
799
795
  end
800
796
 
@@ -851,7 +847,7 @@ EOJS
851
847
  describe "Exception Handling" do
852
848
  it "raises javascript exceptions as ruby exceptions" do
853
849
  lambda {
854
- Context.eval('foo')
850
+ Context.new.eval('foo')
855
851
  }.should raise_error(JSError)
856
852
  end
857
853
 
@@ -0,0 +1 @@
1
+ "I am Legend"
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: therubyracer
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 0.8.2.pre
5
+ version: 0.8.2.pre2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Charles Lowell
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-03-09 00:00:00 -06:00
14
+ date: 2011-04-08 00:00:00 -05:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -808,6 +808,8 @@ files:
808
808
  - ext/v8/v8_cxt.h
809
809
  - ext/v8/v8_date.cpp
810
810
  - ext/v8/v8_date.h
811
+ - ext/v8/v8_debug.cpp
812
+ - ext/v8/v8_debug.h
811
813
  - ext/v8/v8_exception.cpp
812
814
  - ext/v8/v8_exception.h
813
815
  - ext/v8/v8_external.cpp
@@ -830,6 +832,8 @@ files:
830
832
  - ext/v8/v8_template.h
831
833
  - ext/v8/v8_try_catch.cpp
832
834
  - ext/v8/v8_try_catch.h
835
+ - ext/v8/v8_v8.cpp
836
+ - ext/v8/v8_v8.h
833
837
  - ext/v8/v8_value.cpp
834
838
  - ext/v8/v8_value.h
835
839
  - lib/v8.rb
@@ -842,6 +846,7 @@ files:
842
846
  - lib/v8/object.rb
843
847
  - lib/v8/portal.rb
844
848
  - lib/v8/portal/functions.rb
849
+ - lib/v8/stack.rb
845
850
  - lib/v8/tap.rb
846
851
  - lib/v8/version.rb
847
852
  - spec/ext/cxt_spec.rb
@@ -853,6 +858,7 @@ files:
853
858
  - therubyracer.gemspec
854
859
  - spec/redjs/README.txt
855
860
  - spec/redjs/jsapi_spec.rb
861
+ - spec/redjs/loadme.js
856
862
  has_rdoc: true
857
863
  homepage: http://github.com/cowboyd/therubyracer
858
864
  licenses: []
@@ -868,7 +874,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
868
874
  requirements:
869
875
  - - ">="
870
876
  - !ruby/object:Gem::Version
871
- hash: 2344231307376719393
877
+ hash: 2038989837834402101
872
878
  segments:
873
879
  - 0
874
880
  version: "0"