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

149 lines
3.5 KiB
C++
Raw Normal View History

2009-10-01 21:17:06 -05:00
#include "ruby.h"
2009-10-07 23:35:39 -04:00
#include <stdio.h>
#include <string.h>
2009-10-07 23:35:39 -04:00
typedef struct some_struct {
int a;
} some_struct;
2009-10-07 23:35:39 -04:00
extern "C" {
void Init_v8();
2009-10-01 21:17:06 -05:00
}
VALUE v8_allocate(VALUE clazz);
void v8_mark(some_struct *s);
void v8_free(some_struct *s);
2009-10-01 21:17:06 -05:00
VALUE print(VALUE object, VALUE arg);
2009-10-07 23:35:39 -04:00
VALUE rb_mModule;
VALUE rb_cV8;
extern "C" {
void Init_v8() {
printf("Init_v8()\n");
rb_mModule = rb_define_module("V8");
rb_cV8 = rb_define_class_under(rb_mModule, "Class", rb_cObject);
rb_define_alloc_func(rb_cV8, v8_allocate);
rb_define_method(rb_cV8, "print", (VALUE(*)(...)) print, 1);
}
}
2009-10-07 23:35:39 -04:00
VALUE v8_allocate(VALUE clazz) {
printf("v8_allocate()\n");
some_struct *s = (some_struct *)malloc(sizeof(some_struct));
memset(s, 0, sizeof(some_struct));
return Data_Wrap_Struct(clazz, v8_mark, v8_free, s);
}
2009-10-07 23:35:39 -04:00
void v8_mark(some_struct *s) {
printf("v8_mark\n");
// marked for gc?
}
2009-10-07 23:35:39 -04:00
void v8_free(some_struct *s) {
printf("v8_free\n");
free(s);
}
2009-10-07 23:35:39 -04:00
VALUE print(VALUE object, VALUE arg)
{
some_struct* s=0;
Data_Get_Struct(object, struct some_struct, s);
printf("%d %s\n", (s?s->a++:-1), RSTRING(arg)->ptr);
return Qnil;
}
2009-10-07 23:35:39 -04:00
2009-10-01 21:17:06 -05:00
// v8.c: In function Init_v8:
// v8.c:5: error: rb_mV8 undeclared (first use in this function)
// v8.c:5: error: (Each undeclared identifier is reported only once
// v8.c:5: error: for each function it appears in.)
// make: *** [v8.o] Error 1
// #include "glue.h"
//
// VALUE rb_mV8;
// VALUE rb_cContext;
//
//
// VALUE rv8_Context__dealloc(VALUE self) {
// printf("dealloc\n");
// void * context;
// Data_Get_Struct(self, void, context);
// return Qnil;
// }
//
// VALUE rv8_Context__alloc() {
// printf("alloc\n");
// void * context = cpp_v8_Context__new();
// VALUE instance = Data_Wrap_Struct(rb_cContext, -1, rv8_Context__dealloc, context);
// rb_obj_init(instance);
// return instance;
// }
//
// VALUE rv8_Context_pooh(VALUE self) {
// int * i;
// Data_Get_Struct(self, int , i);
// return INT2NUM(*i);
// }
//
// void Init_v8() {
// rb_mV8 = rb_define_module("V8");
// rb_cContext = rb_define_class_under(rb_mV8, "Context", rb_cObject);
// rb_define_alloc_func(rb_cContext, rv8_Context__alloc);
// }
// #include <v8.h>
// #include <stdio.h>
//
//
// using namespace v8;
//
// extern "C" {
// #include "ruby.h"
//
// VALUE rv8;
// VALUE rv8_Context;
//
// VALUE rv8_Context_new(...);
// VALUE rv8_Context_enter(VALUE self);
//
// VALUE rv8_Context__alloc(VALUE klass);
// void rv8_Context_free(VALUE robj);
//
// void Init_rchassis() {
// rv8 = rb_define_module("V8");
// rv8_Context = rb_define_class_under(rv8, "Context", rb_cObject);
// rb_define_alloc_func(rv8_Context, rv8_Context__alloc);
// rb_define_method(rv8_Context, "enter", rv8_Context_enter, 0);
// }
//
// VALUE rv8_Context__alloc(VALUE klass) {
// Persistent<Context> context = Context::New();
// VALUE robj = Data_Wrap_Struct(rv8_Context, -1, rv8_Context_free, &context);
// return robj;
// }
//
// VALUE rv8_Context_enter(VALUE self) {
//
// }
//
// void rv8_Context_free(VALUE robj) {
// Persistent<Context> * context;
// printf("Let my people go!");
// Data_Get_Struct(robj, Persistent<Context> , context);
// printf("Get my people!");
// context->Dispose();
// }
//
// // VALUE rv8_Context_open(...) {
// // HandleScope scopes;
// // Persistent<Context> context = Context::New();
// // Context::Scope scope(context);
// // VALUE result = rb_yield(Qnil);
// // context.Dispose();
// // return result;
// // }
2009-10-07 23:35:39 -04:00
// }