therubyracer 0.4.0

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 ADDED
@@ -0,0 +1,4 @@
1
+ === 0.0.1 2009-12-14
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,48 @@
1
+ Doxyfile
2
+ History.txt
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ docs/data_conversion.txt
7
+ ext/v8/convert_ruby.cpp
8
+ ext/v8/convert_ruby.h
9
+ ext/v8/convert_string.cpp
10
+ ext/v8/convert_string.h
11
+ ext/v8/convert_v8.cpp
12
+ ext/v8/convert_v8.h
13
+ ext/v8/converters.cpp
14
+ ext/v8/converters.h
15
+ ext/v8/extconf.rb
16
+ ext/v8/v8.cpp
17
+ ext/v8/v8_cxt.cpp
18
+ ext/v8/v8_cxt.h
19
+ ext/v8/v8_func.cpp
20
+ ext/v8/v8_func.h
21
+ ext/v8/v8_msg.cpp
22
+ ext/v8/v8_msg.h
23
+ ext/v8/v8_obj.cpp
24
+ ext/v8/v8_obj.h
25
+ ext/v8/v8_ref.cpp
26
+ ext/v8/v8_ref.h
27
+ ext/v8/v8_script.cpp
28
+ ext/v8/v8_script.h
29
+ ext/v8/v8_standalone.cpp
30
+ ext/v8/v8_standalone.h
31
+ ext/v8/v8_str.cpp
32
+ ext/v8/v8_str.h
33
+ ext/v8/v8_template.cpp
34
+ ext/v8/v8_template.h
35
+ lib/v8.rb
36
+ lib/v8/context.rb
37
+ lib/v8/object.rb
38
+ lib/v8/to.rb
39
+ script/console
40
+ script/destroy
41
+ script/generate
42
+ spec/redjs/README.txt
43
+ spec/redjs/jsapi_spec.rb
44
+ spec/redjs_helper.rb
45
+ spec/spec.opts
46
+ spec/spec_helper.rb
47
+ tasks/rspec.rake
48
+ therubyracer.gemspec
data/README.rdoc ADDED
@@ -0,0 +1,49 @@
1
+ = therubyracer
2
+
3
+ * http://github.com/cowboyd/therubyracer
4
+
5
+ == DESCRIPTION:
6
+
7
+ Embed the V8 Javascript interpreter into Ruby.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Inspires fun.
12
+ * Not close to production ready.
13
+
14
+ == SYNOPSIS:
15
+
16
+ Check back in February 2009 for an alpha release.
17
+
18
+ == REQUIREMENTS:
19
+
20
+ * libv8 >= 0.4.0
21
+
22
+ == INSTALL:
23
+
24
+ * sudo gem install therubyracer
25
+
26
+ == LICENSE:
27
+
28
+ (The MIT License)
29
+
30
+ Copyright (c) 2009 FIXME full name
31
+
32
+ Permission is hereby granted, free of charge, to any person obtaining
33
+ a copy of this software and associated documentation files (the
34
+ 'Software'), to deal in the Software without restriction, including
35
+ without limitation the rights to use, copy, modify, merge, publish,
36
+ distribute, sublicense, and/or sell copies of the Software, and to
37
+ permit persons to whom the Software is furnished to do so, subject to
38
+ the following conditions:
39
+
40
+ The above copyright notice and this permission notice shall be
41
+ included in all copies or substantial portions of the Software.
42
+
43
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
44
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
48
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
49
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ begin
6
+ require './lib/v8'
7
+ rescue LoadError
8
+ #it will fail to load if we don't have the extensions compiled yet
9
+ end
10
+
11
+ gem 'rake-compiler', '>= 0.4.1'
12
+ require "rake/extensiontask"
13
+
14
+
15
+ Hoe.plugin :newgem
16
+
17
+ # Generate all the Rake tasks
18
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
19
+ $hoe = Hoe.spec 'therubyracer' do
20
+ developer 'Charles Lowell', 'cowboyd@thefrontside.net'
21
+ developer 'Bill Robertson', 'billrobertson42@gmail.com'
22
+ self.rubyforge_name = self.name
23
+ self.spec_extras = { :extensions => ["ext/v8/extconf.rb"] }
24
+ self.clean_globs << "lib/v8/*.{o,so,bundle,a,log,dll}"
25
+ end
26
+
27
+ Rake::ExtensionTask.new("v8", $hoe.spec) do |ext|
28
+ ext.lib_dir = "lib/v8"
29
+ end
30
+
31
+ require 'newgem/tasks'
32
+ Dir['tasks/**/*.rake'].each { |t| load t }
33
+
34
+ # TODO - want other tests/tasks run by default? Add them to the list
35
+ # remove_task :default
36
+ # task :default => [:spec, :features]
@@ -0,0 +1,18 @@
1
+ Short list of types that a receiver should handle:
2
+
3
+ type expected method
4
+ -------------------------------
5
+ <null> pushNull
6
+ boolean pushBool
7
+ int64 pushInt (overloaded on 64-bit int)
8
+ double pushDouble
9
+ const std::string& pushString
10
+ ??? pushUndefined
11
+
12
+ int32 pushInt (overloaded on 32-bit int)
13
+
14
+ for lack of any better ideas have these functions return true on
15
+ success and false on error
16
+
17
+ --
18
+ should add more later.
@@ -0,0 +1,8 @@
1
+ #include "convert_ruby.h"
2
+
3
+
4
+ RubyValueDest::RubyValueDest() {
5
+ }
6
+
7
+ RubyValueDest::~RubyValueDest() {
8
+ }
@@ -0,0 +1,106 @@
1
+ #ifndef __convert_ruby_h__
2
+ #define __convert_ruby_h__
3
+
4
+ #include <ruby.h>
5
+ #include <v8.h>
6
+ #include "v8_ref.h"
7
+ #include "v8_obj.h"
8
+ #include <string>
9
+
10
+ /**
11
+ * A RubyValueSource takes a Destination class and a return
12
+ * type as a template argument, it converts a Ruby Value to
13
+ * the appropriate intermediate type and feed it to an
14
+ * instance Destination type.
15
+ *
16
+ * The destination type should have a default constructor,
17
+ * and provide the methods detailed in data_conversion.txt
18
+ * \tparam DEST destination type for the converted data
19
+ * \tparam RET is the return type of T's methods
20
+ */
21
+ template<class DEST, class RET> class RubyValueSource {
22
+
23
+ /**
24
+ * An instance of the destination class
25
+ */
26
+ DEST dest;
27
+
28
+ public:
29
+ RubyValueSource() {}
30
+ ~RubyValueSource() {}
31
+
32
+
33
+ RET operator() (VALUE& value) {
34
+ switch (TYPE(value)) {
35
+ case T_FIXNUM:
36
+ return dest.pushInt(FIX2INT(value));
37
+ case T_FLOAT:
38
+ return dest.pushDouble(NUM2DBL(value));
39
+ case T_STRING:
40
+ return convertString(value);
41
+ case T_NIL:
42
+ return dest.pushNull();
43
+ case T_TRUE:
44
+ return dest.pushBool(true);
45
+ case T_FALSE:
46
+ return dest.pushBool(false);
47
+ case T_DATA:
48
+ VALUE clsProc = rb_eval("::Proc");
49
+ VALUE clsMethod = rb_eval("::Method");
50
+ VALUE smartMatch = rb_intern("===");
51
+ if (RTEST(rb_funcall(clsProc, smartMatch, value)) || RTEST(rbfuncall(clsMethod, smartMatch, value))) {
52
+
53
+ }
54
+ }
55
+ return dest.pushUndefined();
56
+ }
57
+
58
+ private:
59
+ RET convertString(VALUE& value) {
60
+ std::string stringValue(RSTRING(value)->ptr);
61
+ return dest.pushString(stringValue);
62
+ }
63
+ };
64
+
65
+ /**
66
+ * A RubyValueDest class receives on of the types defined in
67
+ * data_conversion.txt, and converts it to the appropriate
68
+ * Ruby VALUE.
69
+ */
70
+ class RubyValueDest {
71
+
72
+ public:
73
+ RubyValueDest();
74
+ ~RubyValueDest();
75
+
76
+ VALUE pushString(const std::string& value) {
77
+ return rb_str_new2(value.c_str());
78
+ }
79
+
80
+ VALUE pushInt(int64_t value) {
81
+ return INT2FIX(value);
82
+ }
83
+
84
+ VALUE pushDouble(double value) {
85
+ return rb_float_new(value);
86
+ }
87
+
88
+ VALUE pushBool(bool value) {
89
+ return value ? Qtrue : Qfalse;
90
+ }
91
+
92
+ VALUE pushNull() {
93
+ return Qnil;
94
+ }
95
+
96
+ VALUE pushUndefined() {
97
+ return Qnil;
98
+ }
99
+
100
+ VALUE pushObject(v8::Handle<v8::Object>& value) {
101
+ return V8_Ref_Create(V8_C_Object, value);
102
+ }
103
+ };
104
+
105
+
106
+ #endif
@@ -0,0 +1,10 @@
1
+ #include "convert_string.h"
2
+
3
+ StringDest::StringDest() {
4
+ }
5
+
6
+ StringDest::~StringDest(){
7
+ }
8
+
9
+ const std::string StringDest::TRUE("true");
10
+ const std::string StringDest::FALSE("false");
@@ -0,0 +1,73 @@
1
+ #ifndef __convert_string_h__
2
+ #define __convert_string_h__
3
+
4
+ #include "v8.h"
5
+ #include <string>
6
+
7
+ /**
8
+ * StringDest is a data type conversion destination that converts
9
+ * any argument into a string. It should have all methods listed
10
+ * in data_conversion.txt so it can be used as a template argument
11
+ * for a conversion source class.
12
+ */
13
+ class StringDest {
14
+
15
+ public:
16
+ StringDest();
17
+ ~StringDest();
18
+
19
+ std::string pushString(const std::string& value) {
20
+ return value;
21
+ }
22
+
23
+ #ifdef FIGURED_OUT_INT_ISSUES
24
+ const char* pushInt(int32_t value) {
25
+ char buffer[64];
26
+ std::sprintf(buffer, "%d", value);
27
+ convertedValue = buffer;
28
+ return convertedValue.c_str();
29
+ }
30
+ #endif
31
+
32
+ std::string pushInt(int64_t value) {
33
+ char buffer[64];
34
+ std::sprintf(buffer, "%lld", value);
35
+ std::string convertedValue(buffer);
36
+ return convertedValue;
37
+ }
38
+
39
+ std::string pushDouble(double value) {
40
+ char buffer[64];
41
+ std::sprintf(buffer, "%g", value);
42
+ std::string convertedValue(buffer);
43
+ return convertedValue;
44
+ }
45
+
46
+ std::string pushBool(bool value) {
47
+ std::string convertedValue(value ? TRUE : FALSE);
48
+ return convertedValue;
49
+ }
50
+
51
+ std::string pushNull() {
52
+ printf("I bet we aren't even getting here<br/>");
53
+ return ""; // this kind of sucks
54
+ }
55
+
56
+ std::string pushUndefined() {
57
+ return "undefined"; // this too
58
+ }
59
+
60
+ private:
61
+
62
+ /**
63
+ * constant "true" used for pushBool
64
+ */
65
+ static const std::string TRUE;
66
+
67
+ /**
68
+ * constant "false" used for pushBool
69
+ */
70
+ static const std::string FALSE;
71
+ };
72
+
73
+ #endif
@@ -0,0 +1,9 @@
1
+ #include "convert_v8.h"
2
+
3
+ V8LocalDest::V8LocalDest() {
4
+
5
+ }
6
+
7
+ V8LocalDest::~V8LocalDest() {
8
+
9
+ }
@@ -0,0 +1,113 @@
1
+ #ifndef __convert_v8_h__
2
+ #define __convert_v8_h__
3
+
4
+ #include <v8.h>
5
+ #include <stdint.h>
6
+ #include <stdio.h>
7
+ #include <string>
8
+
9
+ template<class T, class R> class V8HandleSource {
10
+
11
+ T dest;
12
+
13
+ public:
14
+
15
+ V8HandleSource() {}
16
+ ~V8HandleSource() {}
17
+
18
+ R operator() (v8::Handle<v8::Value>& value) {
19
+
20
+ if (value.IsEmpty()) {
21
+ return dest.pushNull();
22
+ }
23
+
24
+ if (value->IsUndefined()) {
25
+ return dest.pushNull();
26
+ }
27
+
28
+ if(value->IsNull()) {
29
+ return dest.pushNull();
30
+ }
31
+
32
+ if(value->IsTrue()) {
33
+ return dest.pushBool(true);
34
+ }
35
+
36
+ if(value->IsFalse()) {
37
+ return dest.pushBool(false);
38
+ }
39
+
40
+ if(value->IsString()) {
41
+ v8::Local<v8::String> str = value->ToString();
42
+ char buffer[1024];
43
+ int strlen = str->Length();
44
+ std::string output(strlen, 0);
45
+ for (int total = 0; total < strlen;) {
46
+ int written = str->WriteAscii(buffer, total, 1024);
47
+ output.replace(total, written, buffer);
48
+ total += written;
49
+ }
50
+ return dest.pushString(output);
51
+ }
52
+
53
+ if(value->IsInt32()) {
54
+ return dest.pushInt(value->Int32Value());
55
+ }
56
+
57
+ if(value->IsNumber()) {
58
+ return dest.pushDouble(value->NumberValue());
59
+ }
60
+
61
+ if (value->IsObject()) {
62
+ v8::Local<v8::Object> object(v8::Object::Cast(*value));
63
+ return dest.pushObject(object);
64
+ }
65
+
66
+ return dest.pushNull();
67
+ }
68
+
69
+ };
70
+
71
+ /**
72
+ * StringDest is a data type conversion destination that converts
73
+ * any argument into a string. It should have all methods listed
74
+ * in data_conversion.txt so it can be used as a template argument
75
+ * for a conversion source class.
76
+ */
77
+ class V8LocalDest {
78
+
79
+ public:
80
+ V8LocalDest();
81
+ ~V8LocalDest();
82
+
83
+ v8::Local<v8::Value> pushString(const std::string& value) {
84
+ return v8::Local<v8::Value>::New(v8::String::New(value.c_str()));
85
+ }
86
+
87
+ v8::Local<v8::Value> pushInt(int64_t value) {
88
+ return v8::Local<v8::Value>::New(v8::Integer::New(value));
89
+ }
90
+
91
+ v8::Local<v8::Value> pushDouble(double value) {
92
+ return v8::Local<v8::Value>::New(v8::Number::New(value));
93
+ }
94
+
95
+ v8::Local<v8::Value> pushBool(bool value) {
96
+ return v8::Local<v8::Value>::New(v8::Boolean::New(value));
97
+ }
98
+
99
+ v8::Local<v8::Value> pushNull() {
100
+ return v8::Local<v8::Value>::New(v8::Null());
101
+ }
102
+
103
+ v8::Local<v8::Value> pushUndefined() {
104
+ return v8::Local<v8::Value>::New(v8::Undefined());
105
+ }
106
+
107
+ // v8:Local<v8:Value> pushFunction(Function) {
108
+ //
109
+ // }
110
+ };
111
+
112
+
113
+ #endif