therubyracer 0.7.1 → 0.7.2.pre

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.

Files changed (49) hide show
  1. data/Rakefile +3 -2
  2. data/ext/v8/rr.cpp +5 -18
  3. data/ext/v8/rr.h +0 -3
  4. data/ext/v8/upstream/Makefile +1 -1
  5. data/ext/v8/v8_array.cpp +3 -9
  6. data/ext/v8/v8_callbacks.cpp +1 -1
  7. data/ext/v8/v8_cxt.cpp +19 -36
  8. data/ext/v8/v8_cxt.h +0 -7
  9. data/ext/v8/v8_exception.cpp +2 -1
  10. data/ext/v8/v8_external.cpp +18 -11
  11. data/ext/v8/v8_external.h +1 -0
  12. data/ext/v8/v8_func.cpp +2 -5
  13. data/ext/v8/v8_func.h +0 -2
  14. data/ext/v8/v8_msg.cpp +1 -2
  15. data/ext/v8/v8_obj.cpp +3 -13
  16. data/ext/v8/v8_obj.h +0 -1
  17. data/ext/v8/v8_ref.cpp +1 -6
  18. data/ext/v8/v8_ref.h +1 -2
  19. data/ext/v8/v8_script.cpp +0 -2
  20. data/ext/v8/v8_str.cpp +9 -3
  21. data/ext/v8/v8_str.h +0 -2
  22. data/ext/v8/v8_template.cpp +49 -37
  23. data/ext/v8/v8_template.h +0 -4
  24. data/ext/v8/v8_try_catch.cpp +0 -1
  25. data/ext/v8/v8_value.cpp +1 -2
  26. data/lib/v8.rb +2 -1
  27. data/lib/v8/access.rb +90 -1
  28. data/lib/v8/array.rb +1 -1
  29. data/lib/v8/context.rb +8 -23
  30. data/lib/v8/error.rb +111 -0
  31. data/lib/v8/function.rb +6 -5
  32. data/lib/v8/object.rb +1 -1
  33. data/lib/v8/to.rb +26 -30
  34. data/spec/redjs/jsapi_spec.rb +55 -14
  35. data/spec/v8/error_spec.rb +118 -0
  36. data/therubyracer.gemspec +4 -4
  37. metadata +14 -21
  38. data/ext/v8/callbacks.cpp +0 -185
  39. data/ext/v8/callbacks.h +0 -14
  40. data/ext/v8/convert_ruby.cpp +0 -8
  41. data/ext/v8/convert_ruby.h +0 -99
  42. data/ext/v8/convert_string.cpp +0 -10
  43. data/ext/v8/convert_string.h +0 -73
  44. data/ext/v8/convert_v8.cpp +0 -9
  45. data/ext/v8/convert_v8.h +0 -124
  46. data/ext/v8/converters.cpp +0 -84
  47. data/ext/v8/converters.h +0 -21
  48. data/ext/v8/v8.bundle +0 -0
  49. data/lib/v8/callbacks.rb +0 -88
data/Rakefile CHANGED
@@ -7,7 +7,7 @@ manifest.exclude "lib/v8/*.bundle", "lib/v8/*.so", "ext/**/test/*", "ext/**/test
7
7
  Gem::Specification.new do |gemspec|
8
8
  $gemspec = gemspec
9
9
  gemspec.name = gemspec.rubyforge_project = "therubyracer"
10
- gemspec.version = "0.7.1"
10
+ gemspec.version = "0.7.2.pre"
11
11
  gemspec.summary = "Embed the V8 Javascript interpreter into Ruby"
12
12
  gemspec.description = "Call javascript code and manipulate javascript objects from ruby. Call ruby code and manipulate ruby objects from javascript."
13
13
  gemspec.email = "cowboyd@thefrontside.net"
@@ -33,9 +33,10 @@ task :gemspec => :clean do
33
33
  end
34
34
 
35
35
  task :clean do
36
- sh "rm -f ext/v8/Makefile"
37
36
  sh "rm -rf pkg"
38
37
  sh "rm -rf *.gem"
38
+ sh "rm -rf ext/v8/Makefile"
39
+ sh "rm -rf ext/v8/*.bundle ext/v8/*.so"
39
40
  sh "rm -rf lib/v8/*.bundle lib/v8/*.so"
40
41
  end
41
42
 
data/ext/v8/rr.cpp CHANGED
@@ -7,6 +7,7 @@
7
7
  #include "v8_str.h"
8
8
  #include "v8_date.h"
9
9
  #include "v8_msg.h"
10
+ #include "v8_external.h"
10
11
 
11
12
  using namespace v8;
12
13
 
@@ -25,25 +26,16 @@ VALUE rr_define_const(const char *name, VALUE value) {
25
26
  return value;
26
27
  }
27
28
 
28
- VALUE rr_str_to_perl_case(VALUE str) {
29
- VALUE V8 = rb_define_module("V8");
30
- VALUE to = rb_define_module_under(V8, "To");
31
- return rb_funcall(to, rb_intern("perl_case"), 1, str);
32
- }
33
-
34
- VALUE rr_str_to_camel_case(VALUE str) {
35
- VALUE V8 = rb_define_module("V8");
36
- VALUE to = rb_define_module_under(V8, "To");
37
- return rb_funcall(to, rb_intern("camel_case"), 1, str);
38
- }
39
-
40
29
  VALUE rr_v82rb(Handle<Value> value) {
41
30
  if (value.IsEmpty()) {
42
31
  return rr_cV8_C_Empty;
43
32
  }
44
- if (value.IsEmpty() || value->IsUndefined() || value->IsNull()) {
33
+ if (value->IsUndefined() || value->IsNull()) {
45
34
  return Qnil;
46
35
  }
36
+ if (value->IsExternal()) {
37
+ return rr_reflect_v8_external(value);
38
+ }
47
39
  if (value->IsUint32()) {
48
40
  return UINT2NUM(value->Uint32Value());
49
41
  }
@@ -74,9 +66,6 @@ VALUE rr_v82rb(Handle<Value> value) {
74
66
  return rr_wrap_v8_value(value);
75
67
  }
76
68
 
77
- VALUE rr_v82rb(Handle<Context> value) {
78
- return rr_reflect_v8_context(value);
79
- }
80
69
  VALUE rr_v82rb(Handle<Message> value) {
81
70
  return rr_reflect_v8_message(value);
82
71
  }
@@ -120,7 +109,6 @@ VALUE rr_v82rb(int32_t value) {
120
109
  return INT2FIX(value);
121
110
  }
122
111
 
123
-
124
112
  Handle<Value> rr_rb2v8(VALUE value) {
125
113
  switch (TYPE(value)) {
126
114
  case T_FIXNUM:
@@ -138,7 +126,6 @@ Handle<Value> rr_rb2v8(VALUE value) {
138
126
  case T_DATA:
139
127
  return V8_Ref_Get<Value>(value);
140
128
  case T_OBJECT:
141
- return rr_reflect_rb_object(value);
142
129
  case T_CLASS:
143
130
  case T_ICLASS:
144
131
  case T_MODULE:
data/ext/v8/rr.h CHANGED
@@ -9,8 +9,6 @@
9
9
 
10
10
  VALUE rr_define_class(const char *name, VALUE superclass = rb_cObject);
11
11
  VALUE rr_define_const(const char *name, VALUE value);
12
- VALUE rr_str_to_perl_case(VALUE str);
13
- VALUE rr_str_to_camel_case(VALUE str);
14
12
 
15
13
  VALUE rr_v82rb(v8::Handle<v8::Value> value);
16
14
  VALUE rr_v82rb(v8::Handle<v8::Boolean> value);
@@ -21,7 +19,6 @@ VALUE rr_v82rb(v8::Handle<v8::Function> value);
21
19
  VALUE rr_v82rb(v8::Handle<v8::Integer> value);
22
20
  VALUE rr_v82rb(v8::Handle<v8::Uint32> value);
23
21
  VALUE rr_v82rb(v8::Handle<v8::Int32> value);
24
- VALUE rr_v82rb(v8::Handle<v8::Context> value);
25
22
  VALUE rr_v82rb(v8::Handle<v8::Message> value);
26
23
  VALUE rr_v82rb(bool value);
27
24
  VALUE rr_v82rb(double value);
@@ -5,7 +5,7 @@ V8SRC=build/v8
5
5
  LIBV8=build/v8/libv8.a
6
6
  LIBV8_G=build/v8/libv8_g.a
7
7
  GCC_VERSION=$(shell ruby -e 'puts %x{gcc --version} =~ /(\d)\.(\d)\.\d/ ? $$1 + $$2 : "UNKNOWN"')
8
- ARCH=$(shell ruby -e "puts ['foo'].pack('p').size == 8 ? 'x64' : 'ia32'")
8
+ ARCH=$(shell ruby build_cpu.rb)
9
9
 
10
10
  all: $(LIBV8)
11
11
 
data/ext/v8/v8_array.cpp CHANGED
@@ -24,7 +24,7 @@ namespace {
24
24
  length = INT2FIX(0);
25
25
  }
26
26
  HandleScope scope;
27
- return V8_Ref_Create(self, Array::New(NUM2INT(length)));
27
+ return rr_v8_ref_create(self, Array::New(NUM2INT(length)));
28
28
  }
29
29
 
30
30
  VALUE Length(VALUE self) {
@@ -45,12 +45,6 @@ void rr_init_v8_array() {
45
45
 
46
46
  VALUE rr_reflect_v8_array(Handle<Value> value) {
47
47
  Local<Array> array(Array::Cast(*value));
48
- Local<Value> peer = array->GetHiddenValue(String::New("TheRubyRacer::RubyObject"));
49
- if (peer.IsEmpty()) {
50
- VALUE arr = V8_Ref_Create(ArrayClass, array);
51
- rb_iv_set(arr, "@context", rr_v82rb(Context::GetEntered()));
52
- return arr;
53
- } else {
54
- return (VALUE)External::Unwrap(peer);
55
- }
48
+ Local<Value> peer = array->GetHiddenValue(String::NewSymbol("TheRubyRacer::RubyObject"));
49
+ return peer.IsEmpty() ? rr_v8_ref_create(ArrayClass, value) : (VALUE)External::Unwrap(peer);
56
50
  }
@@ -70,7 +70,7 @@ namespace {
70
70
  return rr_v82rb(args(self)->length);
71
71
  }
72
72
  VALUE Get(VALUE self, VALUE index) {
73
- int i = NUM2INT(rb_to_int(index));
73
+ int i = NUM2INT(index);
74
74
  return rr_v82rb(args(self)->values->Get(i));
75
75
  }
76
76
  VALUE Callee(VALUE self) {
data/ext/v8/v8_cxt.cpp CHANGED
@@ -1,75 +1,63 @@
1
1
  #include "rr.h"
2
+ #include "v8_ref.h"
2
3
  #include "v8_cxt.h"
3
4
  #include "v8_msg.h"
4
5
  #include "v8_template.h"
5
6
  #include "v8_external.h"
6
- #include "converters.h"
7
7
 
8
8
  using namespace v8;
9
9
 
10
- VALUE V8_C_Context;
11
-
12
- //TODO: rename everything to Context_
13
- //TODO: do the object init from within here
10
+ namespace {
14
11
 
12
+ VALUE ContextClass;
15
13
 
16
- namespace {
17
-
18
14
  Local<Context> unwrap(VALUE value) {
19
15
  return V8_Ref_Get<Context>(value);
20
16
  }
21
-
22
- //TODO: make this scriptable and less static
17
+
23
18
  VALUE New(int argc, VALUE *argv, VALUE self) {
24
19
  HandleScope handles;
25
- VALUE scope;
26
- rb_scan_args(argc,argv, "01", &scope);
27
- if (NIL_P(scope)) {
28
- Persistent<Context> cxt(Context::New());
29
- VALUE ref = V8_Ref_Create(self, cxt);
30
- cxt.Dispose();
31
- return ref;
32
- } else {
33
- Persistent<Context> context = Context::New(0, Racer_Create_V8_ObjectTemplate(scope));
34
- Context::Scope enter(context);
35
- context->Global()->SetHiddenValue(String::New("TheRubyRacer::RubyObject"), rr_v8_external_create(scope));
36
- VALUE ref = V8_Ref_Create(self, context, scope);
37
- context.Dispose();
38
- return ref;
39
- }
20
+ VALUE global_template; VALUE global_object;
21
+ rb_scan_args(argc,argv, "02", &global_template, &global_object);
22
+ Handle<ObjectTemplate> v8_global_template(NIL_P(global_template) ? Handle<ObjectTemplate>() : V8_Ref_Get<ObjectTemplate>(global_template));
23
+ Handle<Value> v8_global_object(NIL_P(global_object) ? Handle<Value>() : V8_Ref_Get<Value>(global_object));
24
+ Persistent<Context> cxt(Context::New(0, v8_global_template, v8_global_object));
25
+ VALUE ref = rr_v8_ref_create(self, cxt);
26
+ cxt.Dispose();
27
+ return ref;
40
28
  }
41
29
 
42
30
  VALUE InContext(VALUE self) {
43
31
  return Context::InContext() ? Qtrue : Qfalse;
44
32
  }
45
-
33
+
46
34
  VALUE GetEntered(VALUE self) {
47
35
  HandleScope handles;
48
36
  if (Context::InContext()) {
49
37
  Local<Context> current = Context::GetEntered();
50
- return V8_Ref_Create(self, current);
38
+ return rr_v8_ref_create(self, current);
51
39
  } else {
52
40
  return Qnil;
53
41
  }
54
42
  }
55
-
43
+
56
44
  VALUE Global(VALUE self) {
57
45
  HandleScope handles;
58
46
  return rr_v82rb(unwrap(self)->Global());
59
47
  }
60
-
48
+
61
49
  VALUE Enter(VALUE self) {
62
50
  HandleScope handles;
63
51
  unwrap(self)->Enter();
64
52
  return self;
65
53
  }
66
-
54
+
67
55
  VALUE Exit(VALUE self) {
68
56
  HandleScope handles;
69
57
  unwrap(self)->Exit();
70
58
  return self;
71
59
  }
72
-
60
+
73
61
  VALUE IsEntered(VALUE self) {
74
62
  HandleScope handles;
75
63
  if (Context::InContext()) {
@@ -81,7 +69,7 @@ namespace {
81
69
  }
82
70
 
83
71
  void rr_init_cxt() {
84
- VALUE ContextClass = V8_C_Context = rr_define_class("Context");
72
+ ContextClass = rr_define_class("Context");
85
73
  rr_define_singleton_method(ContextClass, "New", New, -1);
86
74
  rr_define_singleton_method(ContextClass, "InContext", InContext, 0);
87
75
  rr_define_singleton_method(ContextClass, "GetEntered", GetEntered, 0);
@@ -91,8 +79,3 @@ void rr_init_cxt() {
91
79
  rr_define_method(ContextClass, "IsEntered", IsEntered, 0);
92
80
  }
93
81
 
94
- VALUE rr_reflect_v8_context(Handle<Context> value) {
95
- return V8_Ref_Create(V8_C_Context, value);
96
- }
97
-
98
-
data/ext/v8/v8_cxt.h CHANGED
@@ -1,13 +1,6 @@
1
1
  #ifndef _RUBY_V8_CXT_
2
2
  #define _RUBY_V8_CXT_
3
3
 
4
- #include "ruby.h"
5
- #include "v8.h"
6
- #include "v8_ref.h"
7
-
8
- extern VALUE V8_C_Context;
9
-
10
4
  void rr_init_cxt();
11
- VALUE rr_reflect_v8_context(v8::Handle<v8::Context> value);
12
5
 
13
6
  #endif
@@ -51,5 +51,6 @@ void rr_init_v8_exception() {
51
51
  rr_define_singleton_method(ExceptionClass, "Error", Error, 1);
52
52
 
53
53
  v8::V8::SetFatalErrorHandler(fatal);
54
- signal(SIGSEGV, segfault);
54
+ //comment this in for debugging.
55
+ // signal(SIGSEGV, segfault);
55
56
  }
@@ -1,28 +1,31 @@
1
- #include "v8_external.h"
2
1
  #include "rr.h"
2
+ #include "v8_external.h"
3
+
3
4
  #include "v8_ref.h"
5
+ #include "v8_value.h"
4
6
  using namespace v8;
5
7
 
6
8
  namespace {
7
9
  VALUE ExternalClass;
8
10
  VALUE references;
9
- VALUE _Value(VALUE self) {
10
- HandleScope scope;
11
- return (VALUE)V8_Ref_Get<External>(self)->Value();
12
- }
13
- VALUE Wrap(VALUE rbclass, VALUE value) {
11
+
12
+ VALUE New(VALUE rbclass, VALUE value) {
14
13
  HandleScope scope;
15
14
  return rr_v8_ref_create(rbclass, rr_v8_external_create(value));
16
15
  }
17
16
  VALUE Unwrap(VALUE self, VALUE value) {
17
+ HandleScope scope;
18
18
  if (rb_obj_is_kind_of(value, self)) {
19
- return _Value(value);
19
+ return (VALUE)External::Unwrap(V8_Ref_Get<External>(self));
20
20
  } else {
21
21
  rb_raise(rb_eArgError, "cannot unwrap %s. It is not a kind of %s", RSTRING_PTR(rb_class_name(rb_class_of(value))), RSTRING_PTR(rb_class_name(self)));
22
22
  return Qnil;
23
23
  }
24
24
  }
25
-
25
+ VALUE _Value(VALUE self) {
26
+ HandleScope scope;
27
+ return (VALUE)V8_Ref_Get<External>(self)->Value();
28
+ }
26
29
  void GCWeakReferenceCallback(Persistent<Value> object, void* parameter) {
27
30
  // printf("V8 GC!!!!\n");
28
31
  Local<External> external(External::Cast(*object));
@@ -32,17 +35,21 @@ namespace {
32
35
  }
33
36
 
34
37
  void rr_init_v8_external() {
35
- ExternalClass = rr_define_class("External");
38
+ ExternalClass = rr_define_class("External", rr_cV8_C_Value);
36
39
  references = rb_hash_new();
37
40
  rb_define_const(ExternalClass, "OBJECTS_REFERENCED_FROM_WITHIN_V8", references);
38
- rr_define_singleton_method(ExternalClass, "Wrap", Wrap, 1);
41
+ rr_define_singleton_method(ExternalClass, "New", New, 1);
39
42
  rr_define_singleton_method(ExternalClass, "Unwrap", Unwrap, 1);
40
43
  rr_define_method(ExternalClass, "Value", _Value, 0);
41
44
  }
42
45
 
46
+ VALUE rr_reflect_v8_external(Handle<Value> external) {
47
+ return rr_v8_ref_create(ExternalClass, external);
48
+ }
49
+
43
50
  Handle<Value> rr_v8_external_create(VALUE value) {
44
51
  rb_hash_aset(references, rb_obj_id(value), value);
45
- Local<Value> external(External::Wrap((void *)value));
52
+ Local<Value> external(External::New((void *)value));
46
53
  Persistent<Value> record = Persistent<Value>::New(external);
47
54
  // V8::AdjustAmountOfExternalAllocatedMemory(100000000);
48
55
  record.MakeWeak(NULL, GCWeakReferenceCallback);
data/ext/v8/v8_external.h CHANGED
@@ -4,5 +4,6 @@
4
4
  #include "rr.h"
5
5
 
6
6
  void rr_init_v8_external();
7
+ VALUE rr_reflect_v8_external(v8::Handle<v8::Value> value);
7
8
  v8::Handle<v8::Value> rr_v8_external_create(VALUE value);
8
9
  #endif
data/ext/v8/v8_func.cpp CHANGED
@@ -1,12 +1,9 @@
1
1
 
2
- #include "converters.h"
3
2
  #include "v8_func.h"
4
3
  #include "v8_obj.h"
5
4
 
6
5
  using namespace v8;
7
6
 
8
- VALUE rr_cV8_C_Function;
9
-
10
7
  namespace {
11
8
  VALUE FunctionClass;
12
9
 
@@ -39,7 +36,7 @@ namespace {
39
36
  for (int i = 0; i < argc; i++) {
40
37
  argv[i] = args->Get(i);
41
38
  }
42
- return rr_v82rb(function->NewInstance(argc, argv));
39
+ return rr_v8_ref_create(rr_cV8_C_Object, function->NewInstance(argc, argv));
43
40
  }
44
41
  VALUE GetName(VALUE self) {
45
42
  return rr_v82rb(unwrap(self)->GetName());
@@ -65,5 +62,5 @@ void rr_init_func() {
65
62
 
66
63
  VALUE rr_reflect_v8_function(Handle<Value> value) {
67
64
  Local<Function> function(Function::Cast(*value));
68
- return V8_Ref_Create(FunctionClass, function);
65
+ return rr_v8_ref_create(FunctionClass, function);
69
66
  }
data/ext/v8/v8_func.h CHANGED
@@ -5,8 +5,6 @@
5
5
  #include "v8.h"
6
6
  #include "v8_ref.h"
7
7
 
8
- extern VALUE rr_cV8_C_Function;
9
-
10
8
  void rr_init_func();
11
9
 
12
10
  VALUE rr_reflect_v8_function(v8::Handle<v8::Value> value);
data/ext/v8/v8_msg.cpp CHANGED
@@ -1,4 +1,3 @@
1
- #include "converters.h"
2
1
  #include "v8_msg.h"
3
2
  #include "v8_ref.h"
4
3
 
@@ -58,6 +57,6 @@ void rr_init_msg() {
58
57
  }
59
58
 
60
59
  VALUE rr_reflect_v8_message(Handle<Message> value) {
61
- return V8_Ref_Create(MessageClass, value);
60
+ return rr_v8_ref_create(MessageClass, value);
62
61
  }
63
62
 
data/ext/v8/v8_obj.cpp CHANGED
@@ -3,7 +3,6 @@
3
3
  #include "v8_value.h"
4
4
  #include "v8_template.h"
5
5
  #include "v8_external.h"
6
- #include "converters.h"
7
6
 
8
7
  using namespace v8;
9
8
 
@@ -27,13 +26,13 @@ namespace {
27
26
  }
28
27
  }
29
28
 
30
- VALUE New(VALUE clazz) {
29
+ VALUE New(VALUE rbclass) {
31
30
  HandleScope handles;
32
31
  if (!Context::InContext()) {
33
32
  rb_raise(rb_eScriptError, "Object::New() called without an entered Context");
34
33
  return Qnil;
35
34
  }
36
- return V8_Ref_Create(clazz, Object::New());
35
+ return rr_v8_ref_create(rbclass, Object::New());
37
36
  }
38
37
 
39
38
  VALUE Set(VALUE self, VALUE key, VALUE value) {
@@ -59,8 +58,6 @@ namespace {
59
58
  } else {
60
59
  rb_raise(rb_eScriptError, "Object::SetHiddenValue() called without an entered Context");
61
60
  }
62
- //TODO: need to store a reference here? what's the best way
63
- // rr_v8_ref_setref(self, "RubyPeer", )
64
61
  return Qnil;
65
62
  }
66
63
  VALUE GetHiddenValue(VALUE self, VALUE key) {
@@ -71,7 +68,6 @@ namespace {
71
68
 
72
69
  void rr_init_obj() {
73
70
  rr_cV8_C_Object = rr_define_class("Object", rr_cV8_C_Value);
74
- rb_define_attr(rr_cV8_C_Object, "context", 1, 0);
75
71
  rr_define_singleton_method(rr_cV8_C_Object, "New", New, 0);
76
72
  rr_define_method(rr_cV8_C_Object, "Get", Get, 1);
77
73
  rr_define_method(rr_cV8_C_Object, "Set", Set, 2);
@@ -82,13 +78,7 @@ void rr_init_obj() {
82
78
 
83
79
  VALUE rr_reflect_v8_object(Handle<Value> value) {
84
80
  Local<Object> object(Object::Cast(*value));
85
- Local<Value> peer = object->GetHiddenValue(String::New("TheRubyRacer::RubyObject"));
81
+ Local<Value> peer = object->GetHiddenValue(String::NewSymbol("TheRubyRacer::RubyObject"));
86
82
  return peer.IsEmpty() ? rr_v8_ref_create(rr_cV8_C_Object, object) : (VALUE)External::Unwrap(peer);
87
83
  }
88
84
 
89
-
90
- v8::Handle<v8::Value> rr_reflect_rb_object(VALUE value) {
91
- Local<Object> o = Racer_Create_V8_ObjectTemplate(value)->NewInstance();
92
- o->SetHiddenValue(String::New("TheRubyRacer::RubyObject"), rr_v8_external_create(value));
93
- return o;
94
- }