Skip to content

Commit

Permalink
Refactored Tomato methods into a Tomato C++ class to better resemble …
Browse files Browse the repository at this point in the history
…the corresponding Ruby class.
  • Loading branch information
sinisterchipmunk committed Jul 12, 2010
1 parent 1e767c6 commit af7c243
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 157 deletions.
14 changes: 7 additions & 7 deletions ext/tomato/binding_classes.cpp
Expand Up @@ -3,19 +3,19 @@

static VALUE create_new(VALUE args);
static v8::Handle<v8::Value> ruby_class_constructor(const Arguments &args);
static Handle<Value> bind_methods(Local<Object> js, VALUE rb, V8Tomato *tomato);
static Handle<Value> bind_methods(Local<Object> js, VALUE rb, Tomato *tomato);
static Handle<Value> bound_getter(Local<String> property, const AccessorInfo &info);
static void bound_setter(Local<String> property, Local<Value> value, const AccessorInfo &info);
static VALUE protected_get(VALUE args);
static VALUE protected_set(VALUE args);

VALUE fTomato_bind_class(VALUE self, VALUE receiver, VALUE chain)
{
V8Tomato *tomato;
Data_Get_Struct(self, V8Tomato, tomato);
Tomato *tomato;
Data_Get_Struct(self, Tomato, tomato);

HandleScope handle_scope;
Context::Scope context_scope(tomato_v8_context(tomato));
Context::Scope context_scope(tomato->context());
VALUE js_class_name = rb_ary_pop(chain);
// This is kind of a misnomer. We're creating a JavaScript function ("method") to stand in for
// the Ruby class. So the method_name has to be the Ruby class name. Consider: "new" is not a
Expand Down Expand Up @@ -60,7 +60,7 @@ v8::Handle<v8::Value> ruby_class_constructor(const Arguments &args)

VALUE receiver;
ID rb_method_id;
V8Tomato *tomato;
Tomato *tomato;
TRY_JS(store_rb_message(args, &tomato, &receiver, &rb_method_id));

VALUE rbargs = rb_ary_new2(1+args.Length());
Expand All @@ -79,12 +79,12 @@ v8::Handle<v8::Value> ruby_class_constructor(const Arguments &args)
return bind_methods(holder, result, tomato);
}

static Handle<Value> bind_methods(Local<Object> js_object, VALUE rb_reference, V8Tomato *tomato)
static Handle<Value> bind_methods(Local<Object> js_object, VALUE rb_reference, Tomato *tomato)
{
VALUE methods = rb_funcall(rb_reference, rb_intern("public_methods"), 0);

HandleScope handle_scope;
Context::Scope context_scope(tomato_v8_context(tomato));
Context::Scope context_scope(tomato->context());
for (int i = 0; i < RARRAY_LEN(methods); i++)
{
const char *method_name = StringValuePtr(*(RARRAY_PTR(methods)+i));
Expand Down
16 changes: 8 additions & 8 deletions ext/tomato/binding_methods.cpp
Expand Up @@ -4,7 +4,7 @@
static VALUE call_rb_bound_method(VALUE args);
static Handle<Value> bound_method(const Arguments& args);

void tomatofy_function(Handle<Function> function, V8Tomato *tomato, VALUE receiver, Handle<Value> rb_method_name)
void tomatofy_function(Handle<Function> function, Tomato *tomato, VALUE receiver, Handle<Value> rb_method_name)
{
register_value_wrapper(function, tomato, receiver);
function->Set(String::New("_tomato_rb_method_name"), rb_method_name);
Expand All @@ -14,7 +14,7 @@ static Handle<Value> call_js_bound_method(const Arguments& args)
{
VALUE receiver;
ID rb_method_id;
V8Tomato *tomato;
Tomato *tomato;
VALUE result;

TRY_JS(store_rb_message(args, &tomato, &receiver, &rb_method_id));
Expand All @@ -30,7 +30,7 @@ static Handle<Value> call_js_bound_method(const Arguments& args)
return js_value_of(tomato, result);
}

void store_args(V8Tomato *tomato, VALUE rbargs, const Arguments &args)
void store_args(Tomato *tomato, VALUE rbargs, const Arguments &args)
{
int length = args.Length();
int offset = RARRAY_LEN(rbargs);
Expand All @@ -46,7 +46,7 @@ static VALUE call_rb_bound_method(VALUE args)
return result;
}

int store_rb_message(const Arguments &args, V8Tomato **out_tomato, VALUE *out_receiver, ID *out_method_id)
int store_rb_message(const Arguments &args, Tomato **out_tomato, VALUE *out_receiver, ID *out_method_id)
{
// get the function
Handle<Function> function = args.Callee();
Expand All @@ -66,7 +66,7 @@ int store_rb_message(const Arguments &args, V8Tomato **out_tomato, VALUE *out_re
return 0;
}

void bind_method(V8Tomato *tomato, VALUE reference, Handle<Object> object, const char *rb_method_name, const char *js_method_name)
void bind_method(Tomato *tomato, VALUE reference, Handle<Object> object, const char *rb_method_name, const char *js_method_name)
{
Handle<Function> function = FunctionTemplate::New(call_js_bound_method)->GetFunction();
tomatofy_function(function, tomato, reference, String::New(rb_method_name));
Expand All @@ -82,11 +82,11 @@ VALUE fTomato_bind_method(int argc, VALUE *argv, VALUE self)
const char *js_method_name = rb_id2name(rb_to_id(argv[3]));
VALUE reference = argv[1];

V8Tomato *tomato;
Data_Get_Struct(self, V8Tomato, tomato);
Tomato *tomato;
Data_Get_Struct(self, Tomato, tomato);

HandleScope handle_scope;
Context::Scope context_scope(tomato_v8_context(tomato));
Context::Scope context_scope(tomato->context());
Handle<Value> value = find_or_create_object_chain(tomato, argv[2]);
if (value->IsObject())
{
Expand Down
8 changes: 4 additions & 4 deletions ext/tomato/bindings.h
Expand Up @@ -4,16 +4,16 @@
#define TRY_JS(x) try { x; } \
catch(const std::string &msg) { return ThrowException(js_error_new(msg.c_str())); }

extern void bind_method(V8Tomato *tomato,
extern void bind_method(Tomato *tomato,
VALUE reference,
Handle<Object> object,
const char *rb_method_name,
const char *js_method_name
);

extern int store_rb_message(const Arguments &args, V8Tomato **out_tomato, VALUE *out_receiver, ID *out_method_id);
extern void store_args(V8Tomato *tomato, VALUE rbargs, const Arguments &args);
extern int store_rb_message(const Arguments &args, Tomato **out_tomato, VALUE *out_receiver, ID *out_method_id);
extern void store_args(Tomato *tomato, VALUE rbargs, const Arguments &args);

extern void tomatofy_function(Handle<Function> function, V8Tomato *tomato, VALUE receiver, Handle<Value> method_name);
extern void tomatofy_function(Handle<Function> function, Tomato *tomato, VALUE receiver, Handle<Value> method_name);

#endif//BINDING_METHODS_H
10 changes: 5 additions & 5 deletions ext/tomato/conversions_to_js.cpp
@@ -1,12 +1,12 @@
#include "tomato.h"

static Handle<Value> js_array_from(V8Tomato *tomato, VALUE value);
static Handle<Value> js_hash_from(V8Tomato *tomato, VALUE value);
static Handle<Value> js_array_from(Tomato *tomato, VALUE value);
static Handle<Value> js_hash_from(Tomato *tomato, VALUE value);
static Handle<Value> js_symbol_to_string(const Arguments& args);
static Handle<Value> js_symbol_from(VALUE value);
static Handle<Value> js_date_from(VALUE value);

Handle<Value> js_value_of(V8Tomato *tomato, VALUE value)
Handle<Value> js_value_of(Tomato *tomato, VALUE value)
{
switch(TYPE(value))
{
Expand Down Expand Up @@ -46,7 +46,7 @@ Handle<Value> js_value_of(V8Tomato *tomato, VALUE value)
return inspect_rb(value);
}

Handle<Value> js_array_from(V8Tomato *tomato, VALUE value)
Handle<Value> js_array_from(Tomato *tomato, VALUE value)
{
Handle<Array> array;
int size, i;
Expand Down Expand Up @@ -91,7 +91,7 @@ static Handle<Value> js_symbol_to_string(const Arguments& args)
return symbol->Get(String::New("symbol"));
}

Handle<Value> js_hash_from(V8Tomato *tomato, VALUE value)
Handle<Value> js_hash_from(Tomato *tomato, VALUE value)
{
VALUE rb_keys = rb_funcall(value, rb_intern("keys"), 0);
VALUE rb_values = rb_funcall(value, rb_intern("values"), 0);
Expand Down
20 changes: 10 additions & 10 deletions ext/tomato/conversions_to_rb.cpp
@@ -1,14 +1,14 @@
#include "tomato.h"

static VALUE ruby_array_from(V8Tomato *tomato, Handle<Array> result);
static VALUE ruby_array_from(Tomato *tomato, Handle<Array> result);
static VALUE ruby_numeric_from(const Handle<Value> &number);
static VALUE ruby_date_from(const Handle<Value> &date);
static VALUE ruby_string_from(const Handle<Value> &value);
static VALUE ruby_symbol_from(const Handle<Object> &value);
static VALUE ruby_object_from(V8Tomato *tomato, Handle<Value> result);
static VALUE ruby_hash_from(V8Tomato *tomato, const Handle<Object> &object);
static VALUE ruby_object_from(Tomato *tomato, Handle<Value> result);
static VALUE ruby_hash_from(Tomato *tomato, const Handle<Object> &object);

VALUE ruby_value_of(V8Tomato *tomato, Handle<Value> result)
VALUE ruby_value_of(Tomato *tomato, Handle<Value> result)
{
if (result->IsUndefined()) { return ID2SYM(rb_intern("undefined")); }
if (result->IsNull()) { return Qnil; }
Expand All @@ -31,7 +31,7 @@ VALUE ruby_value_of(V8Tomato *tomato, Handle<Value> result)

/* First checks for any special cases set up internally (i.e. Hash). If all else fails, returns the JSON for this
object. */
static VALUE ruby_object_from(V8Tomato *tomato, Handle<Value> result)
static VALUE ruby_object_from(Tomato *tomato, Handle<Value> result)
{
if (result->IsObject())
{
Expand All @@ -49,7 +49,7 @@ static VALUE ruby_object_from(V8Tomato *tomato, Handle<Value> result)
}

/* Call Javascript's JSON.stringify(object) method. If that can't be done for any reason, return nil. */
Handle<Value> json = tomato_v8_context(tomato)->Global()->Get(String::New("JSON"));
Handle<Value> json = tomato->context()->Global()->Get(String::New("JSON"));
if (json->IsObject())
{
Handle<Value> stringify = Handle<Object>::Cast(json)->Get(String::New("stringify"));
Expand All @@ -67,7 +67,7 @@ static VALUE ruby_object_from(V8Tomato *tomato, Handle<Value> result)
return ID2SYM(rb_intern("unknown"));
}

static VALUE ruby_hash_from(V8Tomato *tomato, const Handle<Object> &object)
static VALUE ruby_hash_from(Tomato *tomato, const Handle<Object> &object)
{
VALUE hash = rb_hash_new();

Expand Down Expand Up @@ -102,7 +102,7 @@ static VALUE ruby_date_from(const Handle<Value> &value)
return rb_funcall(rb_cTime, rb_intern("at"), 1, DBL2NUM(date->NumberValue() / 1000.0));
}

static VALUE ruby_array_from(V8Tomato *tomato, Handle<Array> array)
static VALUE ruby_array_from(Tomato *tomato, Handle<Array> array)
{
unsigned int length = array->Length();
VALUE rbarr = rb_ary_new2(length);
Expand All @@ -120,10 +120,10 @@ static VALUE ruby_numeric_from(const Handle<Value> &number)
return DBL2NUM(number->NumberValue());
}

Handle<Value> inspect_js(V8Tomato *tomato, Handle<Value> obj)
Handle<Value> inspect_js(Tomato *tomato, Handle<Value> obj)
{
/* Call Javascript's JSON.stringify(object) method. If that can't be done for any reason, return an error. */
Handle<Value> json = tomato_v8_context(tomato)->Global()->Get(String::New("JSON"));
Handle<Value> json = tomato->context()->Global()->Get(String::New("JSON"));
if (json->IsObject())
{
Handle<Value> stringify = Handle<Object>::Cast(json)->Get(String::New("stringify"));
Expand Down
4 changes: 2 additions & 2 deletions ext/tomato/object_chain.cpp
@@ -1,8 +1,8 @@
#include "tomato.h"

Handle<Value> find_or_create_object_chain(V8Tomato *tomato, VALUE chain)
Handle<Value> find_or_create_object_chain(Tomato *tomato, VALUE chain)
{
Handle<Value> value = tomato_v8_context(tomato)->Global();
Handle<Value> value = tomato->context()->Global();
if (value->IsObject())
{
Handle<Object> object = Handle<Object>::Cast(value);
Expand Down
4 changes: 2 additions & 2 deletions ext/tomato/references.cpp
Expand Up @@ -8,7 +8,7 @@ static VALUE rb_reference_ary(VALUE ref)
return ary;
}

void push_rb_reference(V8Tomato *tomato, VALUE ref)
void push_rb_reference(Tomato *tomato, VALUE ref)
{
VALUE ary = rb_reference_ary(ref);
int count = 0;
Expand All @@ -18,7 +18,7 @@ void push_rb_reference(V8Tomato *tomato, VALUE ref)
rb_hash_aset(tomato->rb_references, ary, count+1);
}

void pop_rb_reference(V8Tomato *tomato, VALUE ref)
void pop_rb_reference(Tomato *tomato, VALUE ref)
{
VALUE ary = rb_reference_ary(ref);
int count;
Expand Down

0 comments on commit af7c243

Please sign in to comment.