therubyracer 0.6.0 → 0.6.1

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/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ === 0.6.1 2010-05-03
2
+ * 1 major enhancement
3
+ * call JavaScript functions from Ruby
4
+
1
5
  === 0.6.0 2010-03-31
2
6
  * 4 major enhancements
3
7
  * ruby 1.9 compatible
data/README.rdoc CHANGED
@@ -1,6 +1,7 @@
1
1
  = therubyracer
2
2
 
3
3
  * http://github.com/cowboyd/therubyracer
4
+ * http://groups.google.com/group/therubyracer
4
5
 
5
6
  == DESCRIPTION:
6
7
 
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ manifest.exclude "ext/**/test/*", "ext/**/test/*", "ext/**/samples/*", "ext/**/b
9
9
  Gem::Specification.new do |gemspec|
10
10
  $gemspec = gemspec
11
11
  gemspec.name = gemspec.rubyforge_project = "therubyracer"
12
- gemspec.version = "0.6.0"
12
+ gemspec.version = "0.6.1"
13
13
  gemspec.summary = "Embed the V8 Javascript interpreter into Ruby"
14
14
  gemspec.description = "Call javascript code and manipulate javascript objects from ruby. Call ruby code and manipulate ruby objects from javascript."
15
15
  gemspec.email = "cowboyd@thefrontside.net"
data/bin/v8 CHANGED
@@ -1,13 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
3
- $:.unshift(lib) if File.exists?(lib) unless $:.member?(lib)
3
+ $:.unshift(lib) if File.exists?(lib)
4
4
 
5
- begin
6
- require 'v8'
7
- rescue LoadError
8
- require 'rubygems'
9
- require 'v8'
10
- end
5
+ require 'v8'
11
6
  require 'v8/cli'
12
7
 
13
8
  V8::CLI.run(File.basename(__FILE__), ARGV)
@@ -3,6 +3,7 @@
3
3
  #include "v8_ref.h"
4
4
  #include "v8_obj.h"
5
5
  #include "v8_cxt.h"
6
+ #include "v8_func.h"
6
7
  #include "v8_template.h"
7
8
 
8
9
  using namespace v8;
@@ -16,6 +17,7 @@ VALUE V8_To;
16
17
  VALUE V82RB(Handle<Value>& value) {
17
18
  convert_v8_to_rb_t convert;
18
19
  VALUE result;
20
+ VALUE type = V8_C_Object;
19
21
  if(convert(value, result)) {
20
22
  return result;
21
23
  }
@@ -30,18 +32,21 @@ VALUE V82RB(Handle<Value>& value) {
30
32
  return rb_array;
31
33
  }
32
34
 
35
+ if (value->IsFunction()) {
36
+ type = V8_C_Function;
37
+ }
38
+
33
39
  if (value->IsObject()) {
34
40
  Local<Object> object(Object::Cast(*value));
35
41
  Local<Value> peer = object->GetHiddenValue(String::New("TheRubyRacer::RubyObject"));
36
42
  if (peer.IsEmpty()) {
37
43
  VALUE context_ref = V8_Ref_Create(V8_C_Context, Context::GetCurrent());
38
44
  object->SetHiddenValue(String::New("TheRubyRacer::Context"), External::Wrap((void *)context_ref));
39
- return V8_Ref_Create(V8_C_Object, value, context_ref);
45
+ return V8_Ref_Create(type, value, context_ref);
40
46
  } else {
41
47
  return (VALUE)External::Unwrap(peer);
42
48
  }
43
49
  }
44
-
45
50
  return Qnil;
46
51
  }
47
52
 
data/ext/v8/v8.cpp CHANGED
@@ -86,5 +86,6 @@ extern "C" {
86
86
 
87
87
 
88
88
  V8_C_Function = rb_define_class_under(rb_mNative, "Function", V8_C_Object);
89
+ rb_define_method(V8_C_Function, "Call", (VALUE(*)(...))v8_C_Function_Call, -1);
89
90
  }
90
91
  }
data/ext/v8/v8_func.cpp CHANGED
@@ -1,4 +1,5 @@
1
1
 
2
+ #include "converters.h"
2
3
  #include "v8_func.h"
3
4
 
4
5
  using namespace v8;
@@ -7,4 +8,21 @@ VALUE V8_C_Function;
7
8
 
8
9
  VALUE V8_Wrap_Function(Handle<Function> f) {
9
10
  return V8_Ref_Create(V8_C_Function, f);
11
+ }
12
+
13
+
14
+ VALUE v8_C_Function_Call(int argc, VALUE *argv, VALUE self) {
15
+ HandleScope handles;
16
+ VALUE recv; VALUE f_argv;
17
+ rb_scan_args(argc, argv, "1*", &recv, &f_argv);
18
+
19
+ Local<Function> function = V8_Ref_Get<Function>(self);
20
+ Local<Object> thisObject(Object::Cast(*RB2V8(recv)));
21
+ int f_argc = argc - 1;
22
+ Local<Value> arguments[f_argc];
23
+ for (int i = 0; i < f_argc; i++) {
24
+ arguments[i] = RB2V8(rb_ary_entry(f_argv,i));
25
+ }
26
+ Local<Value> result = function->Call(thisObject, f_argc, arguments);
27
+ return V82RB(result);
10
28
  }
data/ext/v8/v8_func.h CHANGED
@@ -8,4 +8,6 @@
8
8
  extern VALUE V8_C_Function;
9
9
 
10
10
  VALUE V8_Wrap_Function(v8::Handle<v8::Function> f);
11
+
12
+ VALUE v8_C_Function_Call(int argc, VALUE *argv, VALUE self);
11
13
  #endif
data/lib/v8.rb CHANGED
@@ -2,10 +2,11 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module V8
5
- VERSION = '0.6.0'
5
+ VERSION = '0.6.1'
6
6
  require 'v8/v8' #native glue
7
7
  require 'v8/to'
8
8
  require 'v8/context'
9
9
  require 'v8/object'
10
+ require 'v8/function'
10
11
  require 'v8/tap'
11
12
  end
@@ -0,0 +1,9 @@
1
+ module V8
2
+ class Function < V8::Object
3
+
4
+ def call(thisObject, *args)
5
+ To.ruby(@native.Call(To.v8(thisObject), *args.map {|a| To.v8(a)}))
6
+ end
7
+
8
+ end
9
+ end
data/lib/v8/to.rb CHANGED
@@ -4,8 +4,9 @@ module V8
4
4
  class << self
5
5
  def ruby(value)
6
6
  case value
7
- when V8::C::Object then V8::Object.new(value)
8
- when V8::C::String then "Wonkers!"
7
+ when V8::C::Function then V8::Function.new(value)
8
+ when V8::C::Object then V8::Object.new(value)
9
+ when V8::C::String then "Wonkers!"
9
10
  else
10
11
  value
11
12
  end
data/lib/v8/v8.bundle CHANGED
Binary file
@@ -0,0 +1,42 @@
1
+ require "#{File.dirname(__FILE__)}/../spec_helper.rb"
2
+
3
+ include V8
4
+
5
+ describe C::Function do
6
+ it "is callable" do
7
+ C::Context.new.open do |cxt|
8
+ f = cxt.eval('(function() {return "Hello World"})', '<eval>');
9
+ f.Call(cxt.Global).should == "Hello World"
10
+ end
11
+ end
12
+
13
+ it "receives proper argument length from ruby" do
14
+ C::Context.new.open do |cxt|
15
+ f = cxt.eval('(function() {return arguments.length})', 'eval')
16
+ f.Call(nil,1, 2, 3).should == 3
17
+ end
18
+ end
19
+
20
+ it "maps all arguments from ruby" do
21
+ C::Context.new.open do |cxt|
22
+ f = cxt.eval('(function(one, two, three) {return one + two + three})', 'eval')
23
+ f.Call(nil, 1,2,3).should == 6
24
+ end
25
+ end
26
+
27
+ it "properly maps ruby objects back and forth from arguments to return value" do
28
+ C::Context.new.open do |cxt|
29
+ Object.new.tap do |this|
30
+ f = cxt.eval('(function() {return this})', 'eval')
31
+ f.Call(this).should be(this)
32
+ end
33
+ end
34
+ end
35
+
36
+ it "can be called outside of a context" do
37
+ C::Context.new.open do |cxt|
38
+ @f = cxt.eval('(function() {return "Call Me"})', 'eval')
39
+ end
40
+ @f.Call(nil).should == "Call Me"
41
+ end
42
+ end
@@ -228,6 +228,22 @@ describe "Ruby Javascript API" do
228
228
 
229
229
  end
230
230
 
231
+ describe "Calling JavaScript Code From Within Ruby" do
232
+ it "allows you to capture a reference to a javascript function and call it" do
233
+ Context.open do |cxt|
234
+ f = cxt.eval('(function add(lhs, rhs) {return lhs + rhs})')
235
+ f.call(nil, 1,2).should == 3
236
+ end
237
+ end
238
+
239
+ it "can be done outside an open context" do
240
+ Context.open do |cxt|
241
+ @f = cxt.eval('(function add(lhs, rhs) {return lhs + rhs})')
242
+ end
243
+ @f.call(nil, 1,1).should == 2
244
+ end
245
+ end
246
+
231
247
  describe "Setting up the Host Environment" do
232
248
  it "can eval javascript with a given ruby object as the scope." do
233
249
  scope = Class.new.class_eval do
metadata CHANGED
@@ -1,12 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: therubyracer
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 6
8
- - 0
9
- version: 0.6.0
4
+ version: 0.6.1
10
5
  platform: ruby
11
6
  authors:
12
7
  - Charles Lowell
@@ -15,7 +10,7 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2010-03-31 00:00:00 -05:00
13
+ date: 2010-05-04 00:00:00 -05:00
19
14
  default_executable:
20
15
  dependencies: []
21
16
 
@@ -651,6 +646,7 @@ files:
651
646
  - History.txt
652
647
  - lib/v8/cli.rb
653
648
  - lib/v8/context.rb
649
+ - lib/v8/function.rb
654
650
  - lib/v8/object.rb
655
651
  - lib/v8/tap.rb
656
652
  - lib/v8/to.rb
@@ -662,6 +658,7 @@ files:
662
658
  - script/destroy
663
659
  - script/generate
664
660
  - spec/ext/cxt_spec.rb
661
+ - spec/ext/func_spec.rb
665
662
  - spec/ext/obj_spec.rb
666
663
  - spec/redjs/jsapi_spec.rb
667
664
  - spec/redjs/README.txt
@@ -686,20 +683,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
686
683
  requirements:
687
684
  - - ">="
688
685
  - !ruby/object:Gem::Version
689
- segments:
690
- - 0
691
686
  version: "0"
687
+ version:
692
688
  required_rubygems_version: !ruby/object:Gem::Requirement
693
689
  requirements:
694
690
  - - ">="
695
691
  - !ruby/object:Gem::Version
696
- segments:
697
- - 0
698
692
  version: "0"
693
+ version:
699
694
  requirements: []
700
695
 
701
696
  rubyforge_project: therubyracer
702
- rubygems_version: 1.3.6
697
+ rubygems_version: 1.3.5
703
698
  signing_key:
704
699
  specification_version: 3
705
700
  summary: Embed the V8 Javascript interpreter into Ruby