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

whoa. it actually evaluated some javascript

This commit is contained in:
Charles Lowell 2009-10-08 22:50:51 -05:00
parent 6d90a69f72
commit f7f591fc9e
2 changed files with 18 additions and 19 deletions

11
t.rb
View file

@ -1,14 +1,7 @@
require 'v8' require 'v8'
e = V8::Context.new c = V8::Context.new
e.print("Hello World") c.eval("5 + 2")
e.print("Hello World")
e.print("Hello World")
e2 = V8::Context.new
e2.print("You Suck!")
e2.print("You Suck!")
e2.print("You RULE!")

26
v8.cpp
View file

@ -2,10 +2,10 @@
#include "v8.h" #include "v8.h"
#include <stdio.h> #include <stdio.h>
typedef struct v8_context { typedef struct v8_context {
v8::Handle<v8::Context> context;
public:
v8_context() : context(v8::Context::New()) {} v8_context() : context(v8::Context::New()) {}
//declare this as Local<v8::Context> ???
v8::Handle<v8::Context> context;
} v8_context; } v8_context;
extern "C" { extern "C" {
@ -16,7 +16,7 @@ VALUE v8_allocate(VALUE clazz);
void v8_mark(v8_context *s); void v8_mark(v8_context *s);
void v8_free(v8_context *s); void v8_free(v8_context *s);
VALUE print(VALUE object, VALUE arg); VALUE eval(VALUE self, VALUE javascript);
VALUE rb_mModule; VALUE rb_mModule;
VALUE rb_cV8; VALUE rb_cV8;
@ -26,7 +26,7 @@ extern "C" {
rb_mModule = rb_define_module("V8"); rb_mModule = rb_define_module("V8");
rb_cV8 = rb_define_class_under(rb_mModule, "Context", 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, "eval", (VALUE(*)(...)) eval, 1);
} }
} }
@ -44,11 +44,17 @@ void v8_free(v8_context *s) {
delete s; delete s;
} }
VALUE print(VALUE object, VALUE arg) VALUE eval(VALUE self, VALUE javascript) {
{ v8_context* s = 0;
v8_context* s=0; Data_Get_Struct(self, struct v8_context, s);
Data_Get_Struct(object, struct v8_context, s); const char* text = RSTRING(javascript)->ptr;
// s->wrapped.print(RSTRING(arg)->ptr);
v8::Context::Scope context_scope(s->context);
v8::HandleScope handle_scope;
v8::Handle<v8::String> source = v8::String::New(text);
v8::Handle<v8::Script> script = v8::Script::Compile(source);
v8::Handle<v8::Value> local_result = script->Run();
printf("number value: %g\n", local_result->NumberValue());
return Qnil; return Qnil;
} }