1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00

Refactor converters. Consistent class names, typedefs, filenames grouped together, replace push method with operator ()

This commit is contained in:
Bill Robertson 2009-12-13 14:44:28 -05:00
parent 1a4b0232a9
commit 14ed2e9b5b
14 changed files with 77 additions and 73 deletions

8
convert_ruby.cpp Normal file
View file

@ -0,0 +1,8 @@
#include "convert_ruby.h"
RubyValueDest::RubyValueDest() {
}
RubyValueDest::~RubyValueDest() {
}

View file

@ -1,11 +1,11 @@
#ifndef __ruby_data_h__
#define __ruby_data_h__
#ifndef __convert_ruby_h__
#define __convert_ruby_h__
#include <ruby.h>
#include <v8.h>
#include <v8_object.h>
#include <stdio.h>
#include <string>
#include "v8_object.h"
/**
* A RubyValueSource takes a Destination class and a return
@ -30,7 +30,7 @@ template<class DEST, class RET> class RubyValueSource {
~RubyValueSource() {}
RET push(VALUE& value) {
RET operator() (VALUE& value) {
switch (TYPE(value)) {
case T_FIXNUM:
return dest.pushInt(FIX2INT(value));
@ -60,15 +60,15 @@ private:
};
/**
* A RubyDest class receives on of the types defined in
* A RubyValueDest class receives on of the types defined in
* data_conversion.txt, and converts it to the appropriate
* Ruby VALUE.
*/
class RubyDest {
class RubyValueDest {
public:
RubyDest();
~RubyDest();
RubyValueDest();
~RubyValueDest();
VALUE pushString(const std::string& value) {
return rb_str_new2(value.c_str());

View file

@ -1,4 +1,4 @@
#include "generic_data.h"
#include "convert_string.h"
StringDest::StringDest() {
}

View file

@ -1,5 +1,5 @@
#ifndef __generic_data_h__
#define __generic_data_h__
#ifndef __convert_string_h__
#define __convert_string_h__
#include "v8.h"
#include <string>

9
convert_v8.cpp Normal file
View file

@ -0,0 +1,9 @@
#include "convert_v8.h"
V8LocalDest::V8LocalDest() {
}
V8LocalDest::~V8LocalDest() {
}

View file

@ -1,8 +1,8 @@
#ifndef __v8_data_h__
#define __v8_data_h__
#ifndef __convert_v8_h__
#define __convert_v8_h__
#include "v8.h"
#include "stdint.h"
#include <v8.h>
#include <stdint.h>
#include <stdio.h>
#include <string>
@ -15,7 +15,7 @@ template<class T, class R> class V8HandleSource {
V8HandleSource() {}
~V8HandleSource() {}
R push(v8::Handle<v8::Value>& value) {
R operator() (v8::Handle<v8::Value>& value) {
if (value->IsUndefined()) {
return dest.pushNull();
@ -70,11 +70,11 @@ template<class T, class R> class V8HandleSource {
* in data_conversion.txt so it can be used as a template argument
* for a conversion source class.
*/
class V8HandleDest {
class V8LocalDest {
public:
V8HandleDest();
~V8HandleDest();
V8LocalDest();
~V8LocalDest();
v8::Local<v8::Value> pushString(const std::string& value) {
return v8::Local<v8::Value>::New(v8::String::New(value.c_str()));

14
converters.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef __converters_h__
#define __converters_h__
#include "convert_ruby.h"
#include "convert_string.h"
#include "convert_v8.h"
typedef RubyValueSource<V8LocalDest, v8::Local<v8::Value> > convert_rb_to_v8_t;
typedef V8HandleSource<RubyValueDest, VALUE> convert_v8_to_rb_t;
typedef RubyValueSource<StringDest, std::string> convert_rb_to_string_t;
typedef V8HandleSource<StringDest, std::string> convert_v8_to_string_t;
#endif

View file

@ -1,8 +0,0 @@
#include "ruby_data.h"
RubyDest::RubyDest() {
}
RubyDest::~RubyDest() {
}

4
v8.cpp
View file

@ -1,6 +1,4 @@
#include "ruby_data.h"
#include "generic_data.h"
#include "v8_data.h"
#include "v8_object.h"
#include "v8_context.h"
#include "v8_cxt.h"
#include "v8_str.h"

View file

@ -1,6 +1,4 @@
#include "ruby_data.h"
#include "v8_data.h"
#include "generic_data.h"
#include "converters.h"
#include "v8_context.h"
#include<stdio.h>
@ -35,12 +33,12 @@ VALUE v8_context_eval(VALUE self, VALUE javascript) {
HandleScope handles;
RubyValueSource<StringDest, std::string> tostring;
const std::string source(tostring.push(javascript));
const std::string source(tostring(javascript));
Local<Script> script = Script::Compile(String::New(source.c_str()));
Local<Value> result = script->Run();
V8HandleSource<RubyDest, VALUE> toValue;
return toValue.push(result);
convert_v8_to_rb_t toValue;
return toValue(result);
}
VALUE v8_context_inject(VALUE self, VALUE key, VALUE value) {
@ -50,15 +48,15 @@ VALUE v8_context_inject(VALUE self, VALUE key, VALUE value) {
Context::Scope enter(context->handle);
HandleScope handles;
RubyValueSource<StringDest, std::string> tostring;
RubyValueSource<V8HandleDest, Local<Value> > toHandle;
convert_rb_to_string_t tostring;
convert_rb_to_v8_t toHandle;
const std::string key_string(tostring.push(key));
const std::string key_string(tostring(key));
// does this need to be a persistent handle ?
Local<Value> key_handle(String::New(key_string.c_str()));
Local<Value> value_handle(toHandle.push(value));
Local<Value> value_handle(toHandle(value));
Local<Object> global = context->handle->Global();
global->Set(key_handle, value_handle);

View file

@ -1,9 +0,0 @@
#include "v8_data.h"
V8HandleDest::V8HandleDest() {
}
V8HandleDest::~V8HandleDest() {
}

View file

@ -1,9 +1,6 @@
#include <v8_object.h>
#include <v8_context.h>
#include <ruby_data.h>
#include <v8_data.h>
#include <generic_data.h>
#include "v8_object.h"
#include "v8_context.h"
#include "converters.h"
#include <stdio.h>
VALUE rb_cV8_JSObject;
@ -39,12 +36,12 @@ VALUE v8_object_hash_access(VALUE self, VALUE key) {
v8_object* object = 0;
Data_Get_Struct(self, struct v8_object, object);
RubyValueSource<StringDest, std::string> tostring;
const std::string cppkey(tostring.push(key));
convert_rb_to_string_t tostring;
const std::string cppkey(tostring(key));
HandleScope handles;
Handle<Value> result = object->handle->Get(String::New(cppkey.c_str()));
V8HandleSource<RubyDest, VALUE> toValue;
return toValue.push(result);
convert_v8_to_rb_t toValue;
return toValue(result);
}
VALUE v8_object_hash_assignment(VALUE self, VALUE key, VALUE value) {

View file

@ -2,9 +2,7 @@
#include "v8_ref.h"
#include "v8_script.h"
#include "v8_context.h"
#include "ruby_data.h"
#include "v8_data.h"
#include "converters.h"
using namespace v8;
@ -18,7 +16,7 @@ VALUE v8_script_new(VALUE self, VALUE source) {
VALUE v8_script_Run(VALUE self) {
HandleScope handles;
Local<Script> script = V8_Ref_Get<Script>(self);
V8HandleSource<RubyDest, VALUE> toValue;
convert_v8_to_rb_t toValue;
Local<Value> result = script->Run();
return toValue.push(result);
return toValue(result);
}

View file

@ -1,9 +1,8 @@
#include "ruby.h"
#include "v8.h"
#include <ruby.h>
#include <v8.h>
#include "v8_ref.h"
#include "v8_template.h"
#include "ruby_data.h"
#include "v8_data.h"
#include "converters.h"
using namespace v8;
@ -12,19 +11,19 @@ Handle<Value> RubyInvocationCallback(const Arguments& args) {
if (NIL_P(code)) {
return Null();
} else {
V8HandleSource<RubyDest, VALUE> argConverter;
RubyValueSource<V8HandleDest, Local<Value> > retvalConverter;
convert_v8_to_rb_t arg_cvt;
convert_rb_to_v8_t ret_cvt;
VALUE* arguments = new VALUE[args.Length()];
for(int c=0;c<args.Length(); ++c) {
Handle<Value> val = args[c];
arguments[c] = argConverter.push(val);
arguments[c] = arg_cvt(val);
}
VALUE result = rb_funcall2(code, rb_intern("call"), args.Length(), arguments);
delete [] arguments;
Handle<Value> convertedResult = retvalConverter.push(result);
Handle<Value> convertedResult = ret_cvt(result);
return convertedResult ;
}
}