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

couple of tweaks in prep for using v8. switched malloc/free to new/

delete.  Changed class name to "Context"  Also tried to link V8,
but I don't know anything about the ruby make script.
This commit is contained in:
Bill Robertson 2009-10-08 21:22:29 -04:00
parent 138ef05d6a
commit e7caf25d09
3 changed files with 15 additions and 20 deletions

View file

@ -2,10 +2,7 @@ require 'mkmf'
CONFIG['LDSHARED'] = "g++ -shared" CONFIG['LDSHARED'] = "g++ -shared"
#CPP.sub!(CONFIG['CPP'], 'g++ -E') #dir_config('v8')
#LINK.sub!(CONFIG['CC'], 'g++') #have_library('v8')
# dir_config('v8')
# have_library('v8')
create_makefile('v8') create_makefile('v8')

2
t.rb
View file

@ -1,6 +1,6 @@
require 'v8' require 'v8'
e = V8::Class.new e = V8::Context.new
e.print("Hello World") e.print("Hello World")
e.print("Hello World") e.print("Hello World")
e.print("Hello World") e.print("Hello World")

26
v8.cpp
View file

@ -2,16 +2,16 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
typedef struct some_struct { typedef struct v8_context {
int a; int a;
} some_struct; } v8_context;
extern "C" { extern "C" {
void Init_v8(); void Init_v8();
} }
VALUE v8_allocate(VALUE clazz); VALUE v8_allocate(VALUE clazz);
void v8_mark(some_struct *s); void v8_mark(v8_context *s);
void v8_free(some_struct *s); void v8_free(v8_context *s);
VALUE print(VALUE object, VALUE arg); VALUE print(VALUE object, VALUE arg);
@ -22,7 +22,7 @@ extern "C" {
void Init_v8() { void Init_v8() {
printf("Init_v8()\n"); printf("Init_v8()\n");
rb_mModule = rb_define_module("V8"); rb_mModule = rb_define_module("V8");
rb_cV8 = rb_define_class_under(rb_mModule, "Class", rb_cObject); rb_cV8 = rb_define_class_under(rb_mModule, "Context", rb_cObject);
rb_define_alloc_func(rb_cV8, v8_allocate); rb_define_alloc_func(rb_cV8, v8_allocate);
rb_define_method(rb_cV8, "print", (VALUE(*)(...)) print, 1); rb_define_method(rb_cV8, "print", (VALUE(*)(...)) print, 1);
} }
@ -30,25 +30,23 @@ extern "C" {
VALUE v8_allocate(VALUE clazz) { VALUE v8_allocate(VALUE clazz) {
printf("v8_allocate()\n"); printf("v8_allocate()\n");
some_struct *s = (some_struct *)malloc(sizeof(some_struct)); v8_context *s = new v8_context;
memset(s, 0, sizeof(some_struct)); memset(s, 0, sizeof(v8_context));
return Data_Wrap_Struct(clazz, v8_mark, v8_free, s); return Data_Wrap_Struct(clazz, v8_mark, v8_free, s);
} }
void v8_mark(some_struct *s) { void v8_mark(v8_context *s) {
printf("v8_mark\n");
// marked for gc? // marked for gc?
} }
void v8_free(some_struct *s) { void v8_free(v8_context *s) {
printf("v8_free\n"); delete s;
free(s);
} }
VALUE print(VALUE object, VALUE arg) VALUE print(VALUE object, VALUE arg)
{ {
some_struct* s=0; v8_context* s=0;
Data_Get_Struct(object, struct some_struct, s); Data_Get_Struct(object, struct v8_context, s);
printf("%d %s\n", (s?s->a++:-1), RSTRING(arg)->ptr); printf("%d %s\n", (s?s->a++:-1), RSTRING(arg)->ptr);
return Qnil; return Qnil;
} }